FairRoot/PandaRoot
PndSimpleNtuple.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------------
2 // --------- PndSimpleNtuple --------
3 // -------------------------------------------------------------------------------
4 //
5 // Represents a simplified N-Tuple based on a ROOT TTree.
6 //
7 // Branches of type double, float, int or bool are created on the fly with the
8 // command
9 //
10 // 'Column(TString name, double/float/int/bool value);'
11 //
12 // At the end of each event (i.e. when the N-Tuple should be filled) either
13 //
14 // 'DumpData()' or
15 // 'AcceptData()'
16 //
17 // has to be called. The method 'AcceptData()' is a conditional fill in case a
18 // certain precut is fulfilled, which can be set either in the constructor with
19 //
20 // PndSimpleNtuple(TString name, TString title, TString <precut>)
21 //
22 // or with the method
23 //
24 // SetPrecut(TString precut)
25 //
26 // which can even be changed inbetween. The precut is a TFormula directly based
27 // on the branches of the TTree.
28 //
29 // -------------------------------------------------------------------------------
30 
31 #include "PndSimpleNtuple.h"
32 
33 #include "TRegexp.h"
34 #include <iostream>
35 
36 using std::cout;
37 using std::endl;
38 
39 TString typearr[8] = {"D","F","I","B","D[]","F[]","I[]","B[]"};
40 
41 
42 // -------------------------------------------------------------------------------
43 
45  fTree(new TTree(name, title)),fTmpTree(new TTree("tmp", "")), fPrecut(precut), fFml(0)
46 {
47  fTree->SetDirectory(0);
48  fTmpTree->SetDirectory(0);
49 };
50 
51 // ----------------------------------
52 // DOUBLE VALUES
53 // ----------------------------------
54 void PndSimpleNtuple::Column(const TString &name, double value)
55 {
56  // new branch?
57  if (fBrTypes.find(name) == fBrTypes.end())
58  {
59  fBrTypes[name] = 0; // type double
60  fDValues[name] = value;
61  fTree->Branch(name.Data(), &(fDValues[name]), (name+"/D").Data());
62  fTmpTree->Branch(name.Data(), &(fDValues[name]), (name+"/D").Data());
63  }
64  else if ( fBrTypes[name] != 0)
65  {
66  std::cout<<" - WARNING - Type mismatch of branch '"<<name<<"/"<<typearr[fBrTypes[name]]<<"' with type '"<<typearr[0]<<"'"<<std::endl;
67  return;
68  }
69  else
70  fDValues[name] = value;
71 }
72 
73 // ----------------------------------
74 // FLOAT VALUES
75 // ----------------------------------
76 void PndSimpleNtuple::Column(const TString &name, float value)
77 {
78  // new branch?
79  if (fBrTypes.find(name) == fBrTypes.end())
80  {
81  fBrTypes[name] = 1; // type double
82  fFValues[name] = value;
83  fTree->Branch(name.Data(), &(fFValues[name]), (name+"/F").Data());
84  fTmpTree->Branch(name.Data(), &(fFValues[name]), (name+"/F").Data());
85  }
86  else if ( fBrTypes[name] != 1)
87  {
88  std::cout<<" - WARNING - Type mismatch of branch '"<<name<<"/"<<typearr[fBrTypes[name]]<<"' with type '"<<typearr[1]<<"'"<<std::endl;
89  return;
90  }
91  else
92  fFValues[name] = value;
93 }
94 
95 // ----------------------------------
96 // INT VALUES
97 // ----------------------------------
98 void PndSimpleNtuple::Column(const TString &name, int value)
99 {
100  // new branch?
101  if (fBrTypes.find(name) == fBrTypes.end())
102  {
103  fBrTypes[name] = 2; // type double
104  fIValues[name] = value;
105  fTree->Branch(name.Data(), &(fIValues[name]), (name+"/I").Data());
106  fTmpTree->Branch(name.Data(), &(fIValues[name]), (name+"/I").Data());
107  }
108  else if ( fBrTypes[name] != 2)
109  {
110  std::cout<<" - WARNING - Type mismatch of branch '"<<name<<"/"<<typearr[fBrTypes[name]]<<"' with type '"<<typearr[2]<<"'"<<std::endl;
111  return;
112  }
113  else
114  fIValues[name] = value;
115 }
116 
117 // ----------------------------------
118 // BOOL VALUES
119 // ----------------------------------
120 void PndSimpleNtuple::Column(const TString &name, bool value)
121 {
122  // new branch?
123  if (fBrTypes.find(name) == fBrTypes.end())
124  {
125  fBrTypes[name] = 3; // type double
126  fBValues[name] = value;
127  fTree->Branch(name.Data(), &(fBValues[name]), (name+"/B").Data());
128  fTmpTree->Branch(name.Data(), &(fBValues[name]), (name+"/B").Data());
129  }
130  else if ( fBrTypes[name] != 3)
131  {
132  std::cout<<" - WARNING - Type mismatch of branch '"<<name<<"/"<<typearr[fBrTypes[name]]<<"' with type '"<<typearr[3]<<"'"<<std::endl;
133  return;
134  }
135  else
136  fBValues[name] = value;
137 }
138 
139 // ----------------------------------
140 
142 {
143  // branch does not exist
144  if (fBrTypes.find(name)==fBrTypes.end()) return sqrt(-1.);
145 
146  int brtype = fBrTypes[name];
147 
148  switch (brtype)
149  {
150  case 0: return fDValues[name];
151  case 1: return (double) fFValues[name];
152  case 2: return (double) fIValues[name];
153  case 3: return (double) fBValues[name];
154  }
155 
156  return sqrt(-1);
157 }
158 
159 // ----------------------------------
160 
162 {
163  if (fPrecut=="") return true;
164 
165  if (fFml==0) fFml = new TTreeFormula("fFml", fPrecut, fTmpTree);
166 
167  fTmpTree->Fill();
168  fTmpTree->GetEntry(fTmpTree->GetEntriesFast()-1);
169  return fFml->EvalInstance();
170 }
171 
172 // ----------------------------------
173 
175 {
176  int cnt=0;
177  for (auto x: fBrTypes)
178  {
179  cout <<cnt++<<" : "<<x.first<<"/"<<typearr[x.second]<<endl;
180  }
181 
182  return fBrTypes.size();
183 }
184 
185 // ----------------------------------
186 
187 void PndSimpleNtuple::SetPrecut(TString precut) //{ if (fFml) delete fFml; fFml = 0; if (precut!="") fFml = new TTreeFormula("fFml",precut,fTmpTree);}
188 {
189  if (precut!=fPrecut)
190  {
191  if (fFml!=0) {delete fFml; fFml=0;}
192  fPrecut=precut;
193  }
194 }
195 
196 
void SetPrecut(TString precut)
std::map< TString, double > fDValues
TString typearr[8]
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
std::map< TString, float > fFValues
TTreeFormula * fFml
std::map< TString, int > fIValues
PndSimpleNtuple(TString name, TString title, TString precut="")
TString name
Double_t x
Int_t cnt
Definition: hist-t7.C:106
std::map< TString, char > fBValues
void Column(const TString &name, double value)
double GetCurrentValue(TString name)
std::map< TString, int > fBrTypes