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