Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TNtupleD.cxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Rene Brun 12/08/2001
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TNtupleD.h"
13#include "TTree.h"
14#include "TBranch.h"
15#include "TLeaf.h"
16#include "TBrowser.h"
17#include "TBuffer.h"
18#define ROOT_TreeUtils_cxx
19#include "TreeUtils.h"
20#include "strlcpy.h"
21
22#include <cstdio>
23
24
25/** \class TNtupleD
26\ingroup tree
27
28A simple TTree restricted to a list of double variables only.
29
30Each variable goes to a separate branch.
31
32A Ntuple is created via
33~~~ {.cpp}
34 TNtupleD(name,title,varlist,bufsize)
35~~~
36It is filled via:
37~~~ {.cpp}
38 TNtupleD::Fill(*x) or
39 TNtupleD::Fill(v1,v2,v3.....)
40~~~
41*/
42
43////////////////////////////////////////////////////////////////////////////////
44/// Default constructor for Ntuple.
45
47{
48 fNvar = 0;
49 fArgs = nullptr;
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Create an Ntuple.
54///
55/// The parameter varlist describes the list of the ntuple variables
56/// separated by a colon:
57///
58/// Example: `x:y:z:energy`
59///
60/// For each variable in the list a separate branch is created.
61///
62/// NOTE:
63/// - Use TTree to create branches with variables of different data types.
64/// - Use TTree when the number of branches is large (> 100).
65
66TNtupleD::TNtupleD(const char *name, const char *title, const char *varlist, Int_t bufsize)
67 :TTree(name,title)
68{
69 Int_t i;
70 fNvar = 0;
71 fArgs = nullptr;
72
73// Count number of variables (separated by :)
74 Int_t nch = strlen(varlist);
75 if (nch == 0) return;
76 char *vars = new char[nch+1];
77 strlcpy(vars,varlist,nch+1);
78 Int_t *pvars = new Int_t[nch+1];
79 fNvar = 1;
80 pvars[0] = 0;
81 for (i=1;i<nch;i++) {
82 if (vars[i] == ':') {
83 pvars[fNvar] = i+1;
84 vars[i] = 0;
85 fNvar++;
86 }
87 }
88 fArgs = new Double_t[fNvar];
89
90// Create one branch for each variable
91 char descriptor[100];
92 for (i=0;i<fNvar;i++) {
93 Int_t pv = pvars[i];
94 snprintf(descriptor,100,"%s/D",&vars[pv]);
95 TTree::Branch(&vars[pv],&fArgs[i],descriptor,bufsize);
96 }
97
98 delete [] vars;
99 delete [] pvars;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Default destructor for an Ntuple.
104
106{
107 delete [] fArgs;
108 fArgs = nullptr;
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Reset the branch addresses to the internal fArgs array. Use this
113/// method when the addresses were changed via calls to SetBranchAddress().
114
116{
117 if (branch) {
119 if (index>0) {
120 branch->SetAddress(&fArgs[index]);
121 }
122 }
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Reset the branch addresses to the internal fArgs array. Use this
127/// method when the addresses were changed via calls to SetBranchAddress().
128
130{
131 for (Int_t i = 0; i < fNvar; i++) {
133 if (branch) branch->SetAddress(&fArgs[i]);
134 }
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Browse content.
139
141{
142 fLeaves.Browse( b );
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// Fill a Ntuple with current values in fArgs.
147///
148/// Note that this function is protected.
149/// Currently called only by TChain::Merge
150
152{
153 return TTree::Fill();
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Fill a Ntuple with an array of floats.
158
160{
161//*-*- Store array x into buffer
162 for (Int_t i=0;i<fNvar;i++) {
163 fArgs[i] = x[i];
164 }
165
166 return TTree::Fill();
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Fill a Ntuple: Each Ntuple item is an argument.
171
175{
176 if (fNvar > 0) fArgs[0] = x0;
177 if (fNvar > 1) fArgs[1] = x1;
178 if (fNvar > 2) fArgs[2] = x2;
179 if (fNvar > 3) fArgs[3] = x3;
180 if (fNvar > 4) fArgs[4] = x4;
181 if (fNvar > 5) fArgs[5] = x5;
182 if (fNvar > 6) fArgs[6] = x6;
183 if (fNvar > 7) fArgs[7] = x7;
184 if (fNvar > 8) fArgs[8] = x8;
185 if (fNvar > 9) fArgs[9] = x9;
186 if (fNvar > 10) fArgs[10] = x10;
187 if (fNvar > 11) fArgs[11] = x11;
188 if (fNvar > 12) fArgs[12] = x12;
189 if (fNvar > 13) fArgs[13] = x13;
190 if (fNvar > 14) fArgs[14] = x14;
191
192 return TTree::Fill();
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Read from filename as many columns as variables in the ntuple
197/// the function returns the number of rows found in the file
198/// The second argument "branchDescriptor" is currently not used.
199/// Lines in the input file starting with "#" are ignored.
200
201Long64_t TNtupleD::ReadStream(std::istream &inputStream, const char * /*branchDescriptor*/, char delimiter)
202{
203 /*
204 Long64_t nlines = 0;
205 char newline = GetNewlineValue(inputStream);
206 while (1) {
207 if ( inputStream.peek() != '#' ) {
208 for (Int_t i=0;i<fNvar;i++) {
209 inputStream >> fArgs[i];
210 if (inputStream.peek() == delimiter) {
211 inputStream.get(); // skip delimiter.
212 }
213 }
214 if (!inputStream.good()) break;
215 TTree::Fill();
216 ++nlines;
217 }
218 inputStream.ignore(8192,newline);
219 }
220 return nlines;
221 */
222
223 //The last argument - true == strict mode.
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// Stream a class object.
229
231{
232 if (b.IsReading()) {
234 Version_t R__v = b.ReadVersion(&R__s, &R__c);
235 b.ReadClassBuffer(TNtupleD::Class(), this, R__v, R__s, R__c);
236 if (fNvar <= 0) return;
237 fArgs = new Double_t[fNvar];
238 for (Int_t i=0;i<fNvar;i++) {
240 if (branch) branch->SetAddress(&fArgs[i]);
241 }
242 } else {
243 b.WriteClassBuffer(TNtupleD::Class(),this);
244 }
245}
#define b(i)
Definition RSha256.hxx:100
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
char name[80]
Definition TGX11.cxx:142
A TTree is a list of TBranches.
Definition TBranch.h:93
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Buffer base class used for serializing objects.
Definition TBuffer.h:43
void Browse(TBrowser *b) override
Browse this collection (called by TBrowser).
~TNtupleD() override
Default destructor for an Ntuple.
Definition TNtupleD.cxx:105
void Browse(TBrowser *b) override
Browse content.
Definition TNtupleD.cxx:140
Long64_t ReadStream(std::istream &inputstream, const char *branchDescriptor="", char delimiter=' ') override
Read from filename as many columns as variables in the ntuple the function returns the number of rows...
Definition TNtupleD.cxx:201
void ResetBranchAddress(TBranch *) override
Reset the branch addresses to the internal fArgs array.
Definition TNtupleD.cxx:115
TNtupleD()
Default constructor for Ntuple.
Definition TNtupleD.cxx:46
static TClass * Class()
void Streamer(TBuffer &) override
Stream a class object.
Definition TNtupleD.cxx:230
void ResetBranchAddresses() override
Reset the branch addresses to the internal fArgs array.
Definition TNtupleD.cxx:129
Int_t Fill() override
Fill a Ntuple with current values in fArgs.
Definition TNtupleD.cxx:151
Int_t fNvar
Number of columns.
Definition TNtupleD.h:31
Double_t * fArgs
! [fNvar] Array of variables
Definition TNtupleD.h:32
Int_t IndexOf(const TObject *obj) const override
TObject * UncheckedAt(Int_t i) const
Definition TObjArray.h:90
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4674
TObjArray fBranches
List of Branches.
Definition TTree.h:132
TObjArray fLeaves
Direct pointers to individual branch leaves.
Definition TTree.h:133
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
Definition TTree.h:405
Double_t x[n]
Definition legend1.C:17
template Long64_t FillNtupleFromStream< Double_t, TNtupleD >(std::istream &, TNtupleD &, char, bool)