ROOT  6.06/09
Reference Guide
TNtuple.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 06/04/96
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 "TNtuple.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 
21 #include <string>
22 
24 
25 /** \class TNtuple
26 A simple TTree restricted to a list of float variables only.
27 
28 Each variable goes to a separate branch.
29 
30 A Ntuple is created via
31 ~~~ {.cpp}
32  TNtuple(name,title,varlist,bufsize)
33 ~~~
34 It is filled via:
35 ~~~ {.cpp}
36  TNtuple::Fill(*x) or
37  TNtuple::Fill(v1,v2,v3.....)
38 ~~~
39 */
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Default constructor for Ntuple.
43 
44 TNtuple::TNtuple(): TTree()
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 TNtuple::TNtuple(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 Float_t[fNvar];
87 
88 // Create one branch for each variable
89  for (i=0;i<fNvar;i++) {
90  Int_t pv = pvars[i];
91  TTree::Branch(&vars[pv],&fArgs[i],&vars[pv],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 /// Create a clone of this tree and copy nentries.
109 ///
110 /// By default copy all entries.
111 /// Note that only active branches are copied.
112 /// The compression level of the cloned tree is set to the destination file's
113 /// compression level.
114 ///
115 /// See TTree::CloneTree for more details.
116 
117 TTree* TNtuple::CloneTree(Long64_t nentries /* = -1 */, Option_t* option /* = "" */)
118 {
119  TNtuple *newtuple = dynamic_cast<TNtuple*> (TTree::CloneTree(nentries,option) );
120  if (newtuple) {
121  // To deal with the cases of some of the branches where dropped.
122  newtuple->fNvar = newtuple->fBranches.GetEntries();
123  }
124  return newtuple;
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// Reset the branch addresses to the internal fArgs array. Use this
129 /// method when the addresses were changed via calls to SetBranchAddress().
130 
132 {
133  if (branch) {
134  Int_t index = fBranches.IndexOf(branch);
135  if (index>=0) {
136  branch->SetAddress(&fArgs[index]);
137  }
138  }
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Reset the branch addresses to the internal fArgs array. Use this
143 /// method when the addresses were changed via calls to SetBranchAddress().
144 
146 {
147  for (Int_t i = 0; i < fNvar; i++) {
148  TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
149  if (branch) branch->SetAddress(&fArgs[i]);
150  }
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Browse content of the ntuple
155 
157 {
158  fLeaves.Browse( b );
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Fill a Ntuple with current values in fArgs.
163 ///
164 /// Note that this function is protected.
165 /// Currently called only by TChain::Merge
166 
168 {
169  return TTree::Fill();
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Fill a Ntuple with an array of floats
174 
176 {
177 
178  // Store array x into buffer
179  for (Int_t i=0;i<fNvar;i++) {
180  fArgs[i] = x[i];
181  }
182 
183  return TTree::Fill();
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Fill a Ntuple: Each Ntuple item is an argument
188 
190  ,Float_t x5,Float_t x6,Float_t x7,Float_t x8,Float_t x9
191  ,Float_t x10,Float_t x11,Float_t x12,Float_t x13,Float_t x14)
192 {
193  if (fNvar > 0) fArgs[0] = x0;
194  if (fNvar > 1) fArgs[1] = x1;
195  if (fNvar > 2) fArgs[2] = x2;
196  if (fNvar > 3) fArgs[3] = x3;
197  if (fNvar > 4) fArgs[4] = x4;
198  if (fNvar > 5) fArgs[5] = x5;
199  if (fNvar > 6) fArgs[6] = x6;
200  if (fNvar > 7) fArgs[7] = x7;
201  if (fNvar > 8) fArgs[8] = x8;
202  if (fNvar > 9) fArgs[9] = x9;
203  if (fNvar > 10) fArgs[10] = x10;
204  if (fNvar > 11) fArgs[11] = x11;
205  if (fNvar > 12) fArgs[12] = x12;
206  if (fNvar > 13) fArgs[13] = x13;
207  if (fNvar > 14) fArgs[14] = x14;
208 
209  return TTree::Fill();
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// Read from filename as many columns as variables in the ntuple
214 /// the function returns the number of rows found in the file
215 /// The second argument "branchDescriptor" is currently not used.
216 /// Lines in the input file starting with "#" are ignored.
217 
218 Long64_t TNtuple::ReadStream(std::istream &inputStream, const char * /*branchDescriptor*/, char delimiter)
219 {
220  /*
221  Long64_t nlines = 0;
222  char newline = GetNewlineValue(inputStream);
223  while (1) {
224  if ( inputStream.peek() != '#' ) {
225  for (Int_t i=0;i<fNvar;i++) {
226  inputStream >> fArgs[i];
227  if (inputStream.peek() == delimiter) {
228  inputStream.get(); // skip delimiter.
229  }
230  }
231  if (!inputStream.good()) break;
232  TTree::Fill();
233  ++nlines;
234  }
235  inputStream.ignore(8192,newline);
236  }
237  return nlines;
238  */
239 
240  //The last argument - true == strict mode.
241  return ROOT::TreeUtils::FillNtupleFromStream<Float_t, TNtuple>(inputStream, *this, delimiter, true);
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// Stream a class object.
246 
247 void TNtuple::Streamer(TBuffer &b)
248 {
249  if (b.IsReading()) {
250  UInt_t R__s, R__c;
251  Version_t R__v = b.ReadVersion(&R__s, &R__c);
252  if (R__v > 1) {
253  b.ReadClassBuffer(TNtuple::Class(), this, R__v, R__s, R__c);
254  } else {
255  //====process old versions before automatic schema evolution
256  TTree::Streamer(b);
257  b >> fNvar;
258  b.CheckByteCount(R__s, R__c, TNtuple::IsA());
259  //====end of old versions
260  }
261  if (fNvar <= 0) return;
262  fArgs = new Float_t[fNvar];
263  for (Int_t i=0;i<fNvar;i++) {
264  TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
265  if (branch) branch->SetAddress(&fArgs[i]);
266  }
267  } else {
269  }
270 }
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2048
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: TNtuple.cxx:218
long long Long64_t
Definition: RtypesCore.h:69
Bool_t IsReading() const
Definition: TBuffer.h:81
short Version_t
Definition: RtypesCore.h:61
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4328
template Long64_t FillNtupleFromStream< Float_t, TNtuple >(std::istream &, TNtuple &, char, bool)
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
int Int_t
Definition: RtypesCore.h:41
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TTree.cxx:2961
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
void Class()
Definition: Class.C:29
static const double x4[22]
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:91
TObjArray fLeaves
Definition: TTree.h:131
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
Int_t fNvar
Definition: TNtuple.h:33
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:551
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition: TNtuple.cxx:117
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:30
TClass * IsA() const
TObjArray fBranches
Pointer to directory holding this tree.
Definition: TTree.h:130
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void ResetBranchAddress(TBranch *)
Reset the branch addresses to the internal fArgs array.
Definition: TNtuple.cxx:131
virtual void Browse(TBrowser *b)
Browse content of the ntuple.
Definition: TNtuple.cxx:156
void ResetBranchAddresses()
Reset the branch addresses to the internal fArgs array.
Definition: TNtuple.cxx:145
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
int nentries
Definition: THbookFile.cxx:89
virtual Int_t Fill()
[fNvar] Array of variables
Definition: TNtuple.cxx:167
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:493
Float_t * fArgs
Definition: TNtuple.h:34
#define name(a, b)
Definition: linkTestLib0.cpp:5
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:1624
A TTree object has a header with a name and a title.
Definition: TTree.h:94
A TTree is a list of TBranches.
Definition: TBranch.h:58
virtual ~TNtuple()
Default destructor for an Ntuple.
Definition: TNtuple.cxx:101
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
static const double x3[11]