Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TNtupleD.cxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Rene Brun 12/08/2001
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 "TNtupleD.h"
13#include "TTree.h"
14#include "TBranch.h"
15#include "TLeaf.h"
16#include "TBrowser.h"
17#include "TBuffer.h"
18#include "TreeUtils.h"
19#include "strlcpy.h"
20#include "snprintf.h"
21
23
24/** \class TNtupleD
25\ingroup tree
26
27A simple TTree restricted to a list of double variables only.
28
29Each variable goes to a separate branch.
30
31A Ntuple is created via
32~~~ {.cpp}
33 TNtupleD(name,title,varlist,bufsize)
34~~~
35It is filled via:
36~~~ {.cpp}
37 TNtupleD::Fill(*x) or
38 TNtupleD::Fill(v1,v2,v3.....)
39~~~
40*/
41
42////////////////////////////////////////////////////////////////////////////////
43/// Default constructor for Ntuple.
44
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
65TNtupleD::TNtupleD(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 Double_t[fNvar];
88
89// Create one branch for each variable
90 char descriptor[100];
91 for (i=0;i<fNvar;i++) {
92 Int_t pv = pvars[i];
93 snprintf(descriptor,100,"%s/D",&vars[pv]);
94 TTree::Branch(&vars[pv],&fArgs[i],descriptor,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/// Reset the branch addresses to the internal fArgs array. Use this
112/// method when the addresses were changed via calls to SetBranchAddress().
113
115{
116 if (branch) {
117 UInt_t index = fBranches.IndexOf(branch);
118 if (index>0) {
119 branch->SetAddress(&fArgs[index]);
120 }
121 }
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Reset the branch addresses to the internal fArgs array. Use this
126/// method when the addresses were changed via calls to SetBranchAddress().
127
129{
130 for (Int_t i = 0; i < fNvar; i++) {
131 TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
132 if (branch) branch->SetAddress(&fArgs[i]);
133 }
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Browse content.
138
140{
141 fLeaves.Browse( b );
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Fill a Ntuple with current values in fArgs.
146///
147/// Note that this function is protected.
148/// Currently called only by TChain::Merge
149
151{
152 return TTree::Fill();
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// Fill a Ntuple with an array of floats.
157
159{
160//*-*- Store array x into buffer
161 for (Int_t i=0;i<fNvar;i++) {
162 fArgs[i] = x[i];
163 }
164
165 return TTree::Fill();
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Fill a Ntuple: Each Ntuple item is an argument.
170
173 ,Double_t x10,Double_t x11,Double_t x12,Double_t x13,Double_t x14)
174{
175 if (fNvar > 0) fArgs[0] = x0;
176 if (fNvar > 1) fArgs[1] = x1;
177 if (fNvar > 2) fArgs[2] = x2;
178 if (fNvar > 3) fArgs[3] = x3;
179 if (fNvar > 4) fArgs[4] = x4;
180 if (fNvar > 5) fArgs[5] = x5;
181 if (fNvar > 6) fArgs[6] = x6;
182 if (fNvar > 7) fArgs[7] = x7;
183 if (fNvar > 8) fArgs[8] = x8;
184 if (fNvar > 9) fArgs[9] = x9;
185 if (fNvar > 10) fArgs[10] = x10;
186 if (fNvar > 11) fArgs[11] = x11;
187 if (fNvar > 12) fArgs[12] = x12;
188 if (fNvar > 13) fArgs[13] = x13;
189 if (fNvar > 14) fArgs[14] = x14;
190
191 return TTree::Fill();
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Read from filename as many columns as variables in the ntuple
196/// the function returns the number of rows found in the file
197/// The second argument "branchDescriptor" is currently not used.
198/// Lines in the input file starting with "#" are ignored.
199
200Long64_t TNtupleD::ReadStream(std::istream &inputStream, const char * /*branchDescriptor*/, char delimiter)
201{
202 /*
203 Long64_t nlines = 0;
204 char newline = GetNewlineValue(inputStream);
205 while (1) {
206 if ( inputStream.peek() != '#' ) {
207 for (Int_t i=0;i<fNvar;i++) {
208 inputStream >> fArgs[i];
209 if (inputStream.peek() == delimiter) {
210 inputStream.get(); // skip delimiter.
211 }
212 }
213 if (!inputStream.good()) break;
214 TTree::Fill();
215 ++nlines;
216 }
217 inputStream.ignore(8192,newline);
218 }
219 return nlines;
220 */
221
222 //The last argument - true == strict mode.
223 return ROOT::TreeUtils::FillNtupleFromStream<Double_t, TNtupleD>(inputStream, *this, delimiter, true);
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Stream a class object.
228
229void TNtupleD::Streamer(TBuffer &b)
230{
231 if (b.IsReading()) {
232 UInt_t R__s, R__c;
233 Version_t R__v = b.ReadVersion(&R__s, &R__c);
234 b.ReadClassBuffer(TNtupleD::Class(), this, R__v, R__s, R__c);
235 if (fNvar <= 0) return;
236 fArgs = new Double_t[fNvar];
237 for (Int_t i=0;i<fNvar;i++) {
238 TBranch *branch = (TBranch*)fBranches.UncheckedAt(i);
239 if (branch) branch->SetAddress(&fArgs[i]);
240 }
241 } else {
242 b.WriteClassBuffer(TNtupleD::Class(),this);
243 }
244}
#define b(i)
Definition RSha256.hxx:100
static const double x2[5]
static const double x4[22]
static const double x1[5]
static const double x3[11]
int Int_t
Definition RtypesCore.h:45
short Version_t
Definition RtypesCore.h:65
unsigned int UInt_t
Definition RtypesCore.h:46
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:80
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
#define snprintf
Definition civetweb.c:1540
A TTree is a list of TBranches.
Definition TBranch.h:89
virtual void SetAddress(void *add)
Set address of this branch.
Definition TBranch.cxx:2620
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)
Browse this collection (called by TBrowser).
A simple TTree restricted to a list of double variables only.
Definition TNtupleD.h:28
void ResetBranchAddresses()
Reset the branch addresses to the internal fArgs array.
Definition TNtupleD.cxx:128
virtual void Browse(TBrowser *b)
Browse content.
Definition TNtupleD.cxx:139
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 TNtupleD.cxx:200
virtual void ResetBranchAddress(TBranch *)
Reset the branch addresses to the internal fArgs array.
Definition TNtupleD.cxx:114
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition TNtupleD.cxx:150
TNtupleD()
Default constructor for Ntuple.
Definition TNtupleD.cxx:45
virtual ~TNtupleD()
Default destructor for an Ntuple.
Definition TNtupleD.cxx:104
Int_t fNvar
Number of columns.
Definition TNtupleD.h:31
Double_t * fArgs
! [fNvar] Array of variables
Definition TNtupleD.h:32
Int_t IndexOf(const TObject *obj) const
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:4594
TObjArray fBranches
List of Branches.
Definition TTree.h:119
TObjArray fLeaves
Direct pointers to individual branch leaves.
Definition TTree.h:120
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:350
Double_t x[n]
Definition legend1.C:17
template Long64_t FillNtupleFromStream< Double_t, TNtupleD >(std::istream &, TNtupleD &, char, bool)