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
23
24/** \class TNtuple
25\ingroup tree
26
27A simple TTree restricted to a list of float variables only.
28
29Each variable goes to a separate branch.
30
31A Ntuple is created via
32~~~ {.cpp}
33 TNtuple(name,title,varlist,bufsize)
34~~~
35It 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
46{
47 fNvar = 0;
48 fArgs = nullptr;
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
65TNtuple::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 = nullptr;
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 = nullptr;
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
119{
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) {
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++) {
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
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
219Long64_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.
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// Stream a class object.
247
249{
250 if (b.IsReading()) {
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
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++) {
266 if (branch) branch->SetAddress(&fArgs[i]);
267 }
268 } else {
269 b.WriteClassBuffer(TNtuple::Class(),this);
270 }
271}
#define b(i)
Definition RSha256.hxx:100
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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:219
~TNtuple() override
Default destructor for an Ntuple.
Definition TNtuple.cxx:102
TClass * IsA() const override
Definition TNtuple.h:61
void ResetBranchAddresses() override
Reset the branch addresses to the internal fArgs array.
Definition TNtuple.cxx:146
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:132
static TClass * Class()
void Browse(TBrowser *b) override
Browse content of the ntuple.
Definition TNtuple.cxx:157
TNtuple()
Default constructor for Ntuple.
Definition TNtuple.cxx:45
void Streamer(TBuffer &) override
Stream a class object.
Definition TNtuple.cxx:248
Int_t Fill() override
Fill a Ntuple with current values in fArgs.
Definition TNtuple.cxx:168
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:118
Int_t IndexOf(const TObject *obj) const override
TObject * UncheckedAt(Int_t i) const
Definition TObjArray.h:84
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4653
void Streamer(TBuffer &) override
Stream a class object.
Definition TTree.cxx:9765
TObjArray fBranches
List of Branches.
Definition TTree.h:132
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
Create a clone of this tree and copy nentries.
Definition TTree.cxx:3173
TObjArray fLeaves
Direct pointers to individual branch leaves.
Definition TTree.h:133
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:389
Double_t x[n]
Definition legend1.C:17
template Long64_t FillNtupleFromStream< Float_t, TNtuple >(std::istream &, TNtuple &, char, bool)