Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
28A simple TTree restricted to a list of float variables only.
29
30Each variable goes to a separate branch.
31
32A Ntuple is created via
33~~~ {.cpp}
34 TNtuple(name,title,varlist,bufsize)
35~~~
36It 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 = nullptr;
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
66TNtuple::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 = nullptr;
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 = nullptr;
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
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
220Long64_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
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
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}
#define b(i)
Definition RSha256.hxx:100
short Version_t
Definition RtypesCore.h:65
float Float_t
Definition RtypesCore.h:57
long long Long64_t
Definition RtypesCore.h:80
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
char name[80]
Definition TGX11.cxx:110
int nentries
A TTree is a list of TBranches.
Definition TBranch.h:93
virtual void SetAddress(void *add)
Set address of this branch.
Definition TBranch.cxx:2682
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
Buffer base class used for serializing objects.
Definition TBuffer.h:43
void Browse(TBrowser *b) override
Browse this collection (called by TBrowser).
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Long64_t ReadStream(std::istream &inputStream, const char *branchDescriptor="", char delimiter=' ') override
Read from filename as many columns as variables in the ntuple the function returns the number of rows...
Definition TNtuple.cxx:220
~TNtuple() override
Default destructor for an Ntuple.
Definition TNtuple.cxx:103
TClass * IsA() const override
Definition TNtuple.h:61
void ResetBranchAddresses() override
Reset the branch addresses to the internal fArgs array.
Definition TNtuple.cxx:147
Int_t fNvar
Number of columns.
Definition TNtuple.h:31
void ResetBranchAddress(TBranch *) override
Reset the branch addresses to the internal fArgs array.
Definition TNtuple.cxx:133
static TClass * Class()
void Browse(TBrowser *b) override
Browse content of the ntuple.
Definition TNtuple.cxx:158
TNtuple()
Default constructor for Ntuple.
Definition TNtuple.cxx:46
void Streamer(TBuffer &) override
Stream a class object.
Definition TNtuple.cxx:249
Int_t Fill() override
Fill a Ntuple with current values in fArgs.
Definition TNtuple.cxx:169
Float_t * fArgs
! [fNvar] Array of variables
Definition TNtuple.h:32
TTree * CloneTree(Long64_t nentries=-1, Option_t *option="") override
Create a clone of this tree and copy nentries.
Definition TNtuple.cxx:119
Int_t IndexOf(const TObject *obj) const override
Int_t GetEntries() const override
Return the number of objects in array (i.e.
TObject * UncheckedAt(Int_t i) const
Definition TObjArray.h:84
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4603
void Streamer(TBuffer &) override
Stream a class object.
Definition TTree.cxx:9535
TObjArray fBranches
List of Branches.
Definition TTree.h:122
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition TTree.cxx:3139
TObjArray fLeaves
Direct pointers to individual branch leaves.
Definition TTree.h:123
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:353
Double_t x[n]
Definition legend1.C:17
template Long64_t FillNtupleFromStream< Float_t, TNtuple >(std::istream &, TNtuple &, char, bool)