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