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