Logo ROOT   6.14/05
Reference Guide
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 "Riostream.h"
18 #include "TClass.h"
19 #include "TreeUtils.h"
20 
22 
23 /** \class TNtupleD
24 \ingroup tree
25 
26 A simple TTree restricted to a list of double variables only.
27 
28 Each variable goes to a separate branch.
29 
30 A Ntuple is created via
31 ~~~ {.cpp}
32  TNtupleD(name,title,varlist,bufsize)
33 ~~~
34 It is filled via:
35 ~~~ {.cpp}
36  TNtupleD::Fill(*x) or
37  TNtupleD::Fill(v1,v2,v3.....)
38 ~~~
39 */
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Default constructor for Ntuple.
43 
45 {
46  fNvar = 0;
47  fArgs = 0;
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Create an Ntuple.
52 ///
53 /// The parameter varlist describes the list of the ntuple variables
54 /// separated by a colon:
55 ///
56 /// Example: `x:y:z:energy`
57 ///
58 /// For each variable in the list a separate branch is created.
59 ///
60 /// NOTE:
61 /// - Use TTree to create branches with variables of different data types.
62 /// - Use TTree when the number of branches is large (> 100).
63 
64 TNtupleD::TNtupleD(const char *name, const char *title, const char *varlist, Int_t bufsize)
65  :TTree(name,title)
66 {
67  Int_t i;
68  fNvar = 0;
69  fArgs = 0;
70 
71 // Count number of variables (separated by :)
72  Int_t nch = strlen(varlist);
73  if (nch == 0) return;
74  char *vars = new char[nch+1];
75  strlcpy(vars,varlist,nch+1);
76  Int_t *pvars = new Int_t[nch+1];
77  fNvar = 1;
78  pvars[0] = 0;
79  for (i=1;i<nch;i++) {
80  if (vars[i] == ':') {
81  pvars[fNvar] = i+1;
82  vars[i] = 0;
83  fNvar++;
84  }
85  }
86  fArgs = new Double_t[fNvar];
87 
88 // Create one branch for each variable
89  char descriptor[100];
90  for (i=0;i<fNvar;i++) {
91  Int_t pv = pvars[i];
92  snprintf(descriptor,100,"%s/D",&vars[pv]);
93  TTree::Branch(&vars[pv],&fArgs[i],descriptor,bufsize);
94  }
95 
96  delete [] vars;
97  delete [] pvars;
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Default destructor for an Ntuple.
102 
104 {
105  delete [] fArgs;
106  fArgs = 0;
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Reset the branch addresses to the internal fArgs array. Use this
111 /// method when the addresses were changed via calls to SetBranchAddress().
112 
114 {
115  if (branch) {
116  UInt_t index = fBranches.IndexOf(branch);
117  if (index>0) {
118  branch->SetAddress(&fArgs[index]);
119  }
120  }
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Reset the branch addresses to the internal fArgs array. Use this
125 /// method when the addresses were changed via calls to SetBranchAddress().
126 
128 {
129  for (Int_t i = 0; i < fNvar; i++) {
130  TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
131  if (branch) branch->SetAddress(&fArgs[i]);
132  }
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Browse content.
137 
139 {
140  fLeaves.Browse( b );
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Fill a Ntuple with current values in fArgs.
145 ///
146 /// Note that this function is protected.
147 /// Currently called only by TChain::Merge
148 
150 {
151  return TTree::Fill();
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Fill a Ntuple with an array of floats.
156 
158 {
159 //*-*- Store array x into buffer
160  for (Int_t i=0;i<fNvar;i++) {
161  fArgs[i] = x[i];
162  }
163 
164  return TTree::Fill();
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// Fill a Ntuple: Each Ntuple item is an argument.
169 
171  ,Double_t x5,Double_t x6,Double_t x7,Double_t x8,Double_t x9
172  ,Double_t x10,Double_t x11,Double_t x12,Double_t x13,Double_t x14)
173 {
174  if (fNvar > 0) fArgs[0] = x0;
175  if (fNvar > 1) fArgs[1] = x1;
176  if (fNvar > 2) fArgs[2] = x2;
177  if (fNvar > 3) fArgs[3] = x3;
178  if (fNvar > 4) fArgs[4] = x4;
179  if (fNvar > 5) fArgs[5] = x5;
180  if (fNvar > 6) fArgs[6] = x6;
181  if (fNvar > 7) fArgs[7] = x7;
182  if (fNvar > 8) fArgs[8] = x8;
183  if (fNvar > 9) fArgs[9] = x9;
184  if (fNvar > 10) fArgs[10] = x10;
185  if (fNvar > 11) fArgs[11] = x11;
186  if (fNvar > 12) fArgs[12] = x12;
187  if (fNvar > 13) fArgs[13] = x13;
188  if (fNvar > 14) fArgs[14] = x14;
189 
190  return TTree::Fill();
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 /// Read from filename as many columns as variables in the ntuple
195 /// the function returns the number of rows found in the file
196 /// The second argument "branchDescriptor" is currently not used.
197 /// Lines in the input file starting with "#" are ignored.
198 
199 Long64_t TNtupleD::ReadStream(std::istream &inputStream, const char * /*branchDescriptor*/, char delimiter)
200 {
201  /*
202  Long64_t nlines = 0;
203  char newline = GetNewlineValue(inputStream);
204  while (1) {
205  if ( inputStream.peek() != '#' ) {
206  for (Int_t i=0;i<fNvar;i++) {
207  inputStream >> fArgs[i];
208  if (inputStream.peek() == delimiter) {
209  inputStream.get(); // skip delimiter.
210  }
211  }
212  if (!inputStream.good()) break;
213  TTree::Fill();
214  ++nlines;
215  }
216  inputStream.ignore(8192,newline);
217  }
218  return nlines;
219  */
220 
221  //The last argument - true == strict mode.
222  return ROOT::TreeUtils::FillNtupleFromStream<Double_t, TNtupleD>(inputStream, *this, delimiter, true);
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Stream a class object.
227 
228 void TNtupleD::Streamer(TBuffer &b)
229 {
230  if (b.IsReading()) {
231  UInt_t R__s, R__c;
232  Version_t R__v = b.ReadVersion(&R__s, &R__c);
233  b.ReadClassBuffer(TNtupleD::Class(), this, R__v, R__s, R__c);
234  if (fNvar <= 0) return;
235  fArgs = new Double_t[fNvar];
236  for (Int_t i=0;i<fNvar;i++) {
237  TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
238  if (branch) branch->SetAddress(&fArgs[i]);
239  }
240  } else {
242  }
243 }
TNtupleD()
Default constructor for Ntuple.
Definition: TNtupleD.cxx:44
virtual ~TNtupleD()
Default destructor for an Ntuple.
Definition: TNtupleD.cxx:103
Bool_t IsReading() const
Definition: TBuffer.h:83
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition: TNtupleD.cxx:149
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2258
long long Long64_t
Definition: RtypesCore.h:69
A simple TTree restricted to a list of double variables only.
Definition: TNtupleD.h:28
short Version_t
Definition: RtypesCore.h:61
virtual void Browse(TBrowser *b)
Browse content.
Definition: TNtupleD.cxx:138
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual Long64_t ReadStream(std::istream &inputstream, const char *branchDescriptor="", char delimiter=' ')
Read from filename as many columns as variables in the ntuple the function returns the number of rows...
Definition: TNtupleD.cxx:199
int Int_t
Definition: RtypesCore.h:41
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
void Class()
Definition: Class.C:29
Double_t * fArgs
! [fNvar] Array of variables
Definition: TNtupleD.h:32
static const double x4[22]
Int_t fNvar
Number of columns.
Definition: TNtupleD.h:31
TObjArray fLeaves
Direct pointers to individual branch leaves.
Definition: TTree.h:111
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
TObjArray fBranches
List of Branches.
Definition: TTree.h:110
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:89
static const double x1[5]
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:589
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
virtual void ResetBranchAddress(TBranch *)
Reset the branch addresses to the internal fArgs array.
Definition: TNtupleD.cxx:113
template Long64_t FillNtupleFromStream< Double_t, TNtupleD >(std::istream &, TNtupleD &, char, bool)
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1711
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
#define snprintf
Definition: civetweb.c:1351
A TTree object has a header with a name and a title.
Definition: TTree.h:70
A TTree is a list of TBranches.
Definition: TBranch.h:62
void ResetBranchAddresses()
Reset the branch addresses to the internal fArgs array.
Definition: TNtupleD.cxx:127
char name[80]
Definition: TGX11.cxx:109
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
static const double x3[11]