FairRoot/PandaRoot
RhoTuple.cxx
Go to the documentation of this file.
1 // //
3 // RhoTuple //
4 // //
5 // Nested class hierarchy to hold information about RhoTuple columns. //
6 // //
7 // Author List: //
8 // Marcel Kunze, RUB,Mar. 99 //
9 // Apr.2001 (MK),Faster implementation based on THashList //
10 // Copyright (C) 1999-2001,Ruhr-University Bochum. //
11 // Ralf Kliemt, HIM/GSI Feb.2013 (Cleanup & Restructuring) //
12 // //
14 
15 #include "TTree.h"
16 #include "TBranch.h"
17 #include "TString.h"
18 #include "TFile.h"
19 
20 #include "RhoHistogram/RhoTuple.h"
21 
23 
24 #include <iostream>
25 using namespace std;
26 
28 {
29  fMap = new THashList();
30 }
31 
32 // Constructor to create a tuple with name and title:
33 RhoTuple::RhoTuple ( const char* name,const char* title ) :
34  TNamed ( name,title )
35 {
36  fMap = new THashList();
37  fTree = new TTree ( name,title );
38  fTree->SetDirectory ( 0 );
39 }
40 
41 // Destructor:
43 {
44  if(fTree) delete fTree;
45  if(fMap) delete fMap;
46 }
47 
48 // Column booking/filling. All these have the same name - Column(...)
49 
50 // Specify the data for a Column. The string is to the key to
51 // the Column,so it must be unique.
52 
53 
54 // ===== = Bool type ======
55 // Make/fill Column with a single value
56 void RhoTuple::Column ( const char* label,
57  Bool_t value,
58  Bool_t defval,
59  const char* ) // block//[R.K.03/2017] unused variable(s)
60 {
61  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
62  if ( colp ) {
63  // Column exists,fill corresponding memory location with value:
64  colp->SetValue ( &value );
65  colp->SetUseDefValue ( 0 );
66  } else {
67  // Create a new Column:
68  colp = new RhoBoolColumn ( label,value,defval,fTree );
69  fMap->Add ( colp );
70  }
71 
72 }
73 
74 // Make/fill Column-array. Length is fixed at creation time.
75 void RhoTuple::Column ( const char* label,
76  const RhoHTAbsValVector<Bool_t> &vector,
77  Bool_t defval,
78  const char* ) // block //[R.K.03/2017] unused variable(s)
79 {
80  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
81  if ( colp ) {
82  // Column exists,fill corresponding memory location with value:
83  colp->SetValue ( &vector );
84  colp->SetUseDefValue ( kFALSE );
85  } else {
86  // Create a new Column:
87  colp = new RhoBoolArrColumn ( label,vector,defval,fTree );
88  fMap->Add ( colp );
89  }
90 
91 }
92 
93 // Make/fill Column-array. Length is variable and is taken from
94 // another Column.
95 void RhoTuple::Column ( const char* label,
96  const RhoHTAbsValVector<Bool_t> &vector,
97  const char* ilab,
98  Bool_t defval,
99  const char* ) // block //[R.K.03/2017] unused variable(s)
100 {
101  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
102  if ( colp ) {
103  // Column exists,fill corresponding memory location with value:
104  colp->SetValue ( &vector, ( RhoColumn* ) fMap->FindObject ( ilab ) );
105  colp->SetUseDefValue ( kFALSE );
106  } else {
107  // Create a new branch:
108  RhoColumn* indexPtr = ( RhoColumn* ) fMap->FindObject ( ilab );
109  colp = new RhoBoolDynArrColumn ( label,vector,defval,indexPtr,fTree );
110  fMap->Add ( colp );
111  }
112 
113 }
114 
115 
116 // ===== = Int type ======
117 // Make/fill Column with a single value
118 void RhoTuple::Column ( const char* label,
119  Int_t value,
120  Int_t defval,
121  const char* , // block //[R.K.03/2017] unused variable(s)
122  const RhoHTRange<Int_t> & ) // range //[R.K.03/2017] unused variable(s)
123 {
124  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
125  if ( colp ) {
126  // Column exists,fill corresponding memory location with value:
127  colp->SetValue ( &value );
128  colp->SetUseDefValue ( 0 );
129  } else {
130  // Create a new Column:
131  colp = new RhoIntColumn ( label,value,defval,fTree );
132  fMap->Add ( colp );
133  }
134 
135 }
136 
137 // Make/fill Column-array. Length is fixed at creation time.
138 void RhoTuple::Column ( const char* label,
139  const RhoHTAbsValVector<Int_t> &vector,
140  Int_t defval,
141  const char* , // block //[R.K.03/2017] unused variable(s)
142  const RhoHTRange<Int_t> & ) // range //[R.K.03/2017] unused variable(s)
143 {
144  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
145  if ( colp ) {
146  // Column exists,fill corresponding memory location with value:
147  colp->SetValue ( &vector );
148  colp->SetUseDefValue ( kFALSE );
149  } else {
150  // Create a new Column:
151  colp = new RhoIntArrColumn ( label,vector,defval,fTree );
152  fMap->Add ( colp );
153  }
154 
155 }
156 
157 // Make/fill Column-array. Length is variable and is taken from
158 // another Column.
159 void RhoTuple::Column ( const char* label,
160  const RhoHTAbsValVector<Int_t> &vector,
161  const char* ilab,
162  Int_t defval,
163  const char* , // block //[R.K.03/2017] unused variable(s)
164  const RhoHTRange<Int_t> & ) // range //[R.K.03/2017] unused variable(s)
165 {
166  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
167  if ( colp ) {
168  // Column exists,fill corresponding memory location with value:
169  colp->SetValue ( &vector, ( RhoColumn* ) fMap->FindObject ( ilab ) );
170  colp->SetUseDefValue ( kFALSE );
171  } else {
172  // Create a new branch:
173  RhoColumn* indexPtr = ( RhoColumn* ) fMap->FindObject ( ilab );
174  colp = new RhoIntDynArrColumn ( label,vector,defval,indexPtr,fTree );
175  fMap->Add ( colp );
176  }
177 
178 }
179 
180 
181 // ===== = Float type ======
182 // Make/fill Column with a single value
183 void RhoTuple::Column ( const char* label,
184  Float_t value,
185  Float_t defval,
186  const char* , // block //[R.K.03/2017] unused variable(s)
187  const RhoHTRange<Float_t> & ) // range//[R.K.03/2017] unused variable(s)
188 {
189  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
190  if ( colp ) {
191  // Column exists,fill corresponding memory location with value:
192  colp->SetValue ( &value );
193  colp->SetUseDefValue ( 0 );
194  } else {
195  // Create a new Column:
196  colp = new RhoFloatColumn ( label,value,defval,fTree );
197  fMap->Add ( colp );
198  }
199 
200 
201 }
202 
203 // Make/fill Column-array. Length is fixed at creation time.
204 void RhoTuple::Column ( const char* label,
205  const TVector& vector,
206  Float_t defval,
207  const char* , // block //[R.K.03/2017] unused variable(s)
208  const RhoHTRange<Float_t> & ) // range //[R.K.03/2017] unused variable(s)
209 {
210  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
211  if ( colp ) {
212  // Column exists,fill corresponding memory location with value:
213  colp->SetValue ( &vector );
214  colp->SetUseDefValue ( 0 );
215  } else {
216  // Create a new Column:
217  colp = new RhoFloatArrColumn ( label,vector,defval,fTree );
218  fMap->Add ( colp );
219  }
220 
221 
222 }
223 
224 // Make/fill Column-array. Length is variable and is taken from
225 // another Column.
226 void RhoTuple::Column ( const char* label,
227  const TVector& vector,
228  const char* ilab,
229  Float_t defval,
230  const char* , // block //[R.K.03/2017] unused variable(s)
231  const RhoHTRange<Float_t> & ) // range //[R.K.03/2017] unused variable(s)
232 {
233  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
234  if ( colp ) {
235  // Column exists,fill corresponding memory location with value:
236  colp->SetValue ( &vector, ( RhoColumn* ) fMap->FindObject ( ilab ) );
237  colp->SetUseDefValue ( 0 );
238  } else {
239  // Create a new branch:
240  RhoColumn* indexPtr = ( RhoColumn* ) fMap->FindObject ( ilab );
241  colp = new RhoFloatDynArrColumn ( label,vector,defval,indexPtr,fTree );
242  fMap->Add ( colp );
243  }
244 
245 }
246 
247 // Make/fill Column-array. Length is fixed at creation time.
248 void RhoTuple::Column ( const char* label,
249  const RhoHTAbsValVector<Float_t> &vector,
250  Float_t defval,
251  const char* , // block //[R.K.03/2017] unused variable(s)
252  const RhoHTRange<Float_t> & ) // range //[R.K.03/2017] unused variable(s)
253 {
254  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
255  if ( colp ) {
256  // Column exists,fill corresponding memory location with value:
257  colp->SetValue ( &vector );
258  colp->SetUseDefValue ( kFALSE );
259  } else {
260  // Create a new Column:
261  colp = new RhoFloatArrColumn ( label,vector,defval,fTree );
262  fMap->Add ( colp );
263  }
264 
265 
266 }
267 
268 // Make/fill Column-array. Length is variable and is taken from
269 // another Column.
270 void RhoTuple::Column ( const char* label,
271  const RhoHTAbsValVector<Float_t> &vector,
272  const char* ilab,
273  Float_t defval,
274  const char* , // block //[R.K.03/2017] unused variable(s)
275  const RhoHTRange<Float_t> & ) // range //[R.K.03/2017] unused variable(s)
276 {
277  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
278  if ( colp ) {
279  // Column exists,fill corresponding memory location with value:
280  colp->SetValue ( &vector, ( RhoColumn* ) fMap->FindObject ( ilab ) );
281  colp->SetUseDefValue ( kFALSE );
282  } else {
283  // Create a new branch:
284  RhoColumn* indexPtr = ( RhoColumn* ) fMap->FindObject ( ilab );
285  colp = new RhoFloatDynArrColumn ( label,vector,defval,indexPtr,fTree );
286  fMap->Add ( colp );
287  }
288 
289 }
290 
291 // ===== = Double type ======
292 // Make/fill Column with a single value
293 void RhoTuple::Column ( const char* label,
294  Double_t value,
295  Double_t defval,
296  const char* , // block //[R.K.03/2017] unused variable(s)
297  const RhoHTRange<Double_t> & ) // range //[R.K.03/2017] unused variable(s)
298 {
299  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
300  if ( colp ) {
301  // Column exists,fill corresponding memory location with value:
302  colp->SetValue ( &value );
303  colp->SetUseDefValue ( 0 );
304  } else {
305  // Create a new Column:
306  colp = new RhoDoubleColumn ( label,value,defval,fTree );
307  fMap->Add ( colp );
308  }
309 
310 
311 }
312 
313 // Make/fill Column-array. Length is fixed at creation time.
314 void RhoTuple::Column ( const char* label,
315  const RhoHTAbsValVector<Double_t> &vector,
316  Double_t defval,
317  const char* , // block //[R.K.03/2017] unused variable(s)
318  const RhoHTRange<Double_t> & ) // range //[R.K.03/2017] unused variable(s)
319 {
320  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
321  if ( colp ) {
322  // Column exists,fill corresponding memory location with value:
323  colp->SetValue ( &vector );
324  colp->SetUseDefValue ( kFALSE );
325  } else {
326  // Create a new Column:
327  colp = new RhoDoubleArrColumn ( label,vector,defval,fTree );
328  fMap->Add ( colp );
329  }
330 
331 
332 }
333 
334 // Make/fill Column-array. Length is variable and is taken from
335 // another Column.
336 void RhoTuple::Column ( const char* label,
337  const RhoHTAbsValVector<Double_t> &vector,
338  const char* ilab,
339  Double_t defval,
340  const char* , // block //[R.K.03/2017] unused variable(s)
341  const RhoHTRange<Double_t> & ) // range //[R.K.03/2017] unused variable(s)
342 {
343  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
344  if ( colp ) {
345  // Column exists,fill corresponding memory location with value:
346  colp->SetValue ( &vector, ( RhoColumn* ) fMap->FindObject ( ilab ) );
347  colp->SetUseDefValue ( kFALSE );
348  } else {
349  // Create a new branch:
350  RhoColumn* indexPtr = ( RhoColumn* ) fMap->FindObject ( ilab );
351  colp = new RhoDoubleDynArrColumn ( label,vector,defval,indexPtr,fTree );
352  fMap->Add ( colp );
353  }
354 
355 }
356 
357 // ===== = string Columns ======
358 // Can actually be variable length in ROOT,N is ignored:
359 void RhoTuple::Column ( const char* label,
360  const char* value,
361  Int_t , // N //[R.K.03/2017] unused variable(s)
362  const char* defval,
363  const char* ) // block //[R.K.03/2017] unused variable(s)
364 {
365  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
366  if ( colp ) {
367  // Branch exists,fill corresponding memory location with value:
368  Column ( label,value );
369  } else {
370  // Create a new branch:
371  colp = new RhoStringColumn ( label,value,defval,fTree );
372  fMap->Add ( colp );
373  }
374 
375 }
376 
377 void RhoTuple::Column ( const char* label,const char* value )
378 {
379  RhoColumn* colp = ( RhoColumn* ) fMap->FindObject ( label );
380  if ( colp ) {
381  colp->SetValue ( value );
382  colp->SetUseDefValue ( 0 );
383  } else {
384  cerr << "TTuple::Column: Column "<< label << " does not exist" << endl;
385  }
386 
387 }
388 
389 
390 // Dump all the data into the ntuple and then clear
392 {
393  for ( Int_t i=0; i<fMap->GetSize(); i++ ) {
394  RhoColumn* col = ( RhoColumn* ) fMap->At ( i );
395  if ( col->GetUseDefValue() ) { col->SetDefValue(); }
396  col->SetUseDefValue ( 1 );
397  }
398  fTree->Fill();
399 
400 }
401 
402 // Set all the data to their default values:
404 {
405  for ( Int_t i=0; i<fMap->GetSize(); i++ ) {
406  RhoColumn* col = ( RhoColumn* ) fMap->At ( i );
407  col->SetDefValue();
408  col->SetUseDefValue ( 1 );
409  }
410  return;
411 
412 }
413 
414 // Return title of ntuple:
415 const char* RhoTuple::Title() const
416 {
417  return fTree->GetTitle();
418 }
419 
420 // number of Columns
421 Int_t RhoTuple::NColumns() const
422 {
423  return fMap->GetSize();
424 }
425 
426 // Return label for a particular Column with index i:
427 const char* RhoTuple::Label ( Int_t i ) const
428 {
429  TString str;
430  if ( i >= 0 && i < fMap->GetSize() ) { str = ( ( RhoColumn* ) fMap->At ( i ) )->GetLabel(); }
431  else { str = "unknown Column index"; }
432  return str.Data();
433 
434 }
435 
436 // Print info about ntuple:
438 {
439  cout << "TTuple: ntuple " << Title() << " has " << NColumns()
440  << " Columns." << endl;
441  cout << "Complete printout follows: " << endl;
442  fTree->Print();
443 }
444 
446 {
447  TFile* f=new TFile ( fname,opt );
448  fTree->Write();
449  f->Close();
450  delete f;
451 }
452 
454 {
455  TFile* f=new TFile ( fname,"UPDATE" );
456  fTree->Write();
457  f->Close();
458  delete f;
459 }
Int_t i
Definition: run_full.C:25
int col
Definition: anaLmdDigi.C:67
RhoTuple()
Definition: RhoTuple.cxx:27
virtual void SetDefValue()=0
TTree * fTree
Do not stream.
Definition: RhoTuple.h:215
virtual void SetValue(const void *, RhoColumn *cp=0)=0
void WriteToFile(TString fname="ntpdata.root", TString opt="RECREATE")
Definition: RhoTuple.cxx:445
void ClearData()
Definition: RhoTuple.cxx:403
Double_t
void AddToFile(TString fname="ntpdata.root")
Definition: RhoTuple.cxx:453
void SetUseDefValue(Int_t b)
Definition: RhoColumn.h:140
const char * Title() const
Definition: RhoTuple.cxx:415
TFile * f
Definition: bump_analys.C:12
void Column(const char *label, Bool_t value, Bool_t defval=0, const char *block=0)
Definition: RhoTuple.cxx:56
ClassImp(RhoTuple) using namespace std
const Int_t & GetUseDefValue() const
Definition: RhoColumn.h:143
THashList * fMap
Definition: RhoTuple.h:214
TString name
void DumpData()
Definition: RhoTuple.cxx:391
virtual ~RhoTuple()
Definition: RhoTuple.cxx:42
const char * Label(Int_t) const
Definition: RhoTuple.cxx:427
virtual void PrintOn(std::ostream &) const
Definition: RhoTuple.cxx:437
Int_t NColumns() const
Definition: RhoTuple.cxx:421