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