HYDRA_development_version
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
htreeformula.cc
Go to the documentation of this file.
1 //_HADES_CLASS_DESCRIPTION
2 //////////////////////////////////////////////////////////////////////////
3 // //
4 // HMultTreeFormula //
5 // //
6 // The multiplicity formula class //
7 // //
8 //////////////////////////////////////////////////////////////////////////
9 
10 #include "htreeformula.h"
11 #include "htree.h"
12 #include "hcutg.h"
13 #include "TObjArray.h"
14 #include "TCutG.h"
15 #include "TLeafC.h"
16 #include "TLeafObject.h"
17 
19 
20 
22 }
23 
25  const Char_t *name,const Char_t *formula, TTree *tree) :
26  TTreeFormula(name, formula, tree) {
27 
28 }
29 
31  Int_t i;
32  TLeaf* leaf;
33  static Char_t lname[64];
34  TString name;
35  Int_t code;
36 
37  for (i = 0; i < GetNcodes(); i++) {
38  if (fCodes[i] < 0) {
39  fBranchCodes[i] = -1;
40  HCutG *pCutG = (HCutG*)fMethods.At(i);
41  pCutG->GroupLeaves();
42 
43  if (fMultiplicity != 1)
44  fMultiplicity = pCutG->GetFormulaX()->GetMultiplicity();
45  if (fMultiplicity != 1)
46  fMultiplicity = pCutG->GetFormulaY()->GetMultiplicity();
47 
48  continue;
49  }
50 
51  leaf = GetLeaf(i);
52  if (leaf) {
53  name = leaf->GetBranch()->GetName();
54  strcpy(lname, name.Data());
55  code = -1;
56 
57  Int_t len = strlen(lname);
58  for(Int_t j = len; j--;) {
59  if (lname[j] == '.') {
60  lname[j]='_';
61  lname[j+1] = 0;
62  code = LookForGroupCode(lname, i);
63  break;
64  }
65  }
66 
67  if (code < 0)
68  fBranchCodes[i] = fCodes[i];
69  else
70  fBranchCodes[i] = code;
71  }
72  }
73 }
74 
76  if ((i < 0) || (i > fNcodes)) return 0;
77 
78  if (fCodes[i] < 0)
79  return (HCutG*)fMethods.At(i);
80  else
81  return 0;
82 }
83 
84 Int_t HTreeFormula::LookForGroupCode(Char_t* lname, Int_t indcode) {
85  Int_t i;
86  Char_t branchname[128];
87 
88  TObjArray *lleaves = fTree->GetListOfLeaves();
89  Int_t nleaves = lleaves->GetEntriesFast();
90 
91  if ((fCodes[indcode] < 0) || (fCodes[indcode] >= nleaves)) return -1;
92 
93 // Look for a data member
94  for (i=fCodes[indcode]; i--;) {
95  TLeaf *leaf = (TLeaf*)lleaves->UncheckedAt(i);
96  if (!strcmp(lname,leaf->GetBranch()->GetName() ) ) {
97  fBranchCodes[indcode] = i;
98  return i;
99  }
100  }
101 
102 // Look for branchname.leafname
103  for (i=fCodes[indcode]; i--;) {
104  TLeaf *leaf = (TLeaf*)lleaves->UncheckedAt(i);
105  sprintf(branchname,"%s.%s",leaf->GetBranch()->GetName(),leaf->GetName());
106  if (!strcmp(lname,branchname)) {
107  fBranchCodes[indcode] = i;
108  return i;
109  }
110  }
111 // Look for a member function
112  for (i=fCodes[indcode]; i--;) {
113  TLeaf *leaf = (TLeaf*)lleaves->UncheckedAt(i);
114  if (!strcmp(lname,leaf->GetBranch()->GetName())) {
115  fBranchCodes[indcode] = i;
116  return i;
117  }
118  }
119 
120  return -1;
121 }
122 
123 
124 Double_t HTreeFormula::EvalInstance(HCombinedIndex* pInstancesIndex) {
125 //*-*-*-*-*-*-*-*-*-*-*Evaluate this treeformula*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
126 //*-* =========================
127 //
128 
129  Int_t i,pos,pos2,int1,int2;
130  Float_t aresult;
131  Double_t tab[255];
132  Float_t param[50];
133  Double_t dexp;
134  Char_t *tab2[20];
135 
136  for(i = 0; i < fNcodes; i++)
137  fInstances[i] = pInstancesIndex->getIndexByCode(fBranchCodes[i]);
138 
139  if (fNoper == 1) {
140  if (fCodes[0] < 0) {
141  HCutG *gcut = (HCutG*)fMethods.At(0);
142  gcut->EvalInstance(pInstancesIndex);
143  return gcut->EvalInstance(pInstancesIndex);
144  }
145  TLeaf *leaf = GetLeaf(0);
146  if (fInstances[0]) {
147  if (fInstances[0] < leaf->GetNdata())
148  return leaf->GetValue(fInstances[0]);
149  else
150  return leaf->GetValue(0);
151  } else {
152  leaf->GetBranch()->GetEntry(fTree->GetReadEntry());
153  if (!(leaf->IsA() == TLeafObject::Class())) return leaf->GetValue(fIndex[0]);
154  return GetValueLeafObject(fIndex[0],(TLeafObject *)leaf);
155  }
156  }
157 
158  for(i=0;i<fNval;i++) {
159  if (fCodes[i] < 0) {
160  HCutG *gcut = (HCutG*)fMethods.At(i);
161  param[i] = gcut->EvalInstance(pInstancesIndex);
162  } else {
163  TLeaf *leaf = GetLeaf(i);
164  if (fInstances[i]) {
165  if (fInstances[i] < leaf->GetNdata())
166  param[i] = leaf->GetValue(fInstances[i]);
167  else
168  param[i] = leaf->GetValue(0);
169  } else {
170  leaf->GetBranch()->GetEntry(fTree->GetReadEntry());
171  if (!(leaf->IsA() == TLeafObject::Class())) param[i] = leaf->GetValue(fIndex[i]);
172  else param[i] = GetValueLeafObject(fIndex[i],(TLeafObject *)leaf);
173  }
174  }
175  }
176 
177  pos = 0;
178  pos2 = 0;
179  for (i=0; i<fNoper; i++) {
180  Int_t action = fOper[i];
181 //*-*- a tree string
182  if (action >= 105000) {
183 // if (!precalculated_str) {
184 // precalculated_str=1;
185 // for (i=0;i<fNstring;i++) string_calc[i]=DefinedString(i);
186 // }
187  TLeafC *leafc = (TLeafC*)GetLeaf(action-105000);
188  leafc->GetBranch()->GetEntry(fTree->GetReadEntry());
189  pos2++; tab2[pos2-1] = leafc->GetValueString();
190 // pos2++; tab2[pos2-1] = string_calc[action-105000];
191  continue;
192  }
193 //*-*- a tree variable
194  if (action >= 100000) {
195  pos++; tab[pos-1] = param[action-100000];
196  continue;
197  }
198 //*-*- String
199  if (action == 80000) {
200  pos2++; tab2[pos2-1] = (Char_t*)fExpr[i].Data();
201  continue;
202  }
203 //*-*- numerical value
204  if (action >= 50000) {
205  pos++; tab[pos-1] = fConst[action-50000];
206  continue;
207  }
208  if (action == 0) {
209  pos++;
210  sscanf((const Char_t*)fExpr[i],"%g",&aresult);
211  tab[pos-1] = aresult;
212 //*-*- basic operators and mathematical library
213  } else if (action < 100) {
214  switch(action) {
215  case 1 : pos--; tab[pos-1] += tab[pos]; break;
216  case 2 : pos--; tab[pos-1] -= tab[pos]; break;
217  case 3 : pos--; tab[pos-1] *= tab[pos]; break;
218  case 4 : if (tab[pos-1] == 0) {tab[pos-1] = 0;} // division by 0
219  else { pos--; tab[pos-1] /= tab[pos]; }
220  break;
221  case 5 : {pos--; int1=Int_t(tab[pos-1]); int2=Int_t(tab[pos]); tab[pos-1] = Double_t(int1%int2); break;}
222  case 10 : tab[pos-1] = TMath::Cos(tab[pos-1]); break;
223  case 11 : tab[pos-1] = TMath::Sin(tab[pos-1]); break;
224  case 12 : if (TMath::Cos(tab[pos-1]) == 0) {tab[pos-1] = 0;} // { tangente indeterminee }
225  else tab[pos-1] = TMath::Tan(tab[pos-1]);
226  break;
227  case 13 : if (TMath::Abs(tab[pos-1]) > 1) {tab[pos-1] = 0;} // indetermination
228  else tab[pos-1] = TMath::ACos(tab[pos-1]);
229  break;
230  case 14 : if (TMath::Abs(tab[pos-1]) > 1) {tab[pos-1] = 0;} // indetermination
231  else tab[pos-1] = TMath::ASin(tab[pos-1]);
232  break;
233  case 15 : tab[pos-1] = TMath::ATan(tab[pos-1]); break;
234  case 70 : tab[pos-1] = TMath::CosH(tab[pos-1]); break;
235  case 71 : tab[pos-1] = TMath::SinH(tab[pos-1]); break;
236  case 72 : if (TMath::CosH(tab[pos-1]) == 0) {tab[pos-1] = 0;} // { tangente indeterminee }
237  else tab[pos-1] = TMath::TanH(tab[pos-1]);
238  break;
239  case 73 : if (tab[pos-1] < 1) {tab[pos-1] = 0;} // indetermination
240  else tab[pos-1] = TMath::ACosH(tab[pos-1]);
241  break;
242  case 74 : tab[pos-1] = TMath::ASinH(tab[pos-1]); break;
243  case 75 : if (TMath::Abs(tab[pos-1]) > 1) {tab[pos-1] = 0;} // indetermination
244  else tab[pos-1] = TMath::ATanH(tab[pos-1]); break;
245  case 16 : pos--; tab[pos-1] = TMath::ATan2(tab[pos-1],tab[pos]); break;
246  case 20 : pos--; tab[pos-1] = TMath::Power(tab[pos-1],tab[pos]); break;
247  case 21 : tab[pos-1] = tab[pos-1]*tab[pos-1]; break;
248  case 22 : tab[pos-1] = TMath::Sqrt(TMath::Abs(tab[pos-1])); break;
249  case 23 : pos2 -= 2; pos++;if (strstr(tab2[pos2],tab2[pos2+1])) tab[pos-1]=1;
250  else tab[pos-1]=0; break;
251  case 30 : if (tab[pos-1] > 0) tab[pos-1] = TMath::Log(tab[pos-1]);
252  else {tab[pos-1] = 0;} //{indetermination }
253  break;
254  case 31 : dexp = tab[pos-1];
255  if (dexp < -70) {tab[pos-1] = 0; break;}
256  if (dexp > 70) {tab[pos-1] = TMath::Exp(70); break;}
257  tab[pos-1] = TMath::Exp(dexp); break;
258  case 32 : if (tab[pos-1] > 0) tab[pos-1] = TMath::Log10(tab[pos-1]);
259  else {tab[pos-1] = 0;} //{indetermination }
260  break;
261  case 40 : pos++; tab[pos-1] = TMath::ACos(-1); break;
262  case 41 : tab[pos-1] = TMath::Abs(tab[pos-1]); break;
263  case 42 : if (tab[pos-1] < 0) tab[pos-1] = -1; else tab[pos-1] = 1; break;
264  // case 50 : tab[pos-1] = gRandom->Rndm(1); break;
265  case 50 : tab[pos-1] = 0.5; break;
266  case 60 : pos--; if (tab[pos-1]!=0 && tab[pos]!=0) tab[pos-1]=1;
267  else tab[pos-1]=0; break;
268  case 61 : pos--; if (tab[pos-1]!=0 || tab[pos]!=0) tab[pos-1]=1;
269  else tab[pos-1]=0; break;
270  case 62 : pos--; if (tab[pos-1] == tab[pos]) tab[pos-1]=1;
271  else tab[pos-1]=0; break;
272  case 63 : pos--; if (tab[pos-1] != tab[pos]) tab[pos-1]=1;
273  else tab[pos-1]=0; break;
274  case 64 : pos--; if (tab[pos-1] < tab[pos]) tab[pos-1]=1;
275  else tab[pos-1]=0; break;
276  case 65 : pos--; if (tab[pos-1] > tab[pos]) tab[pos-1]=1;
277  else tab[pos-1]=0; break;
278  case 66 : pos--; if (tab[pos-1]<=tab[pos]) tab[pos-1]=1;
279  else tab[pos-1]=0; break;
280  case 67 : pos--; if (tab[pos-1]>=tab[pos]) tab[pos-1]=1;
281  else tab[pos-1]=0; break;
282  case 68 : if (tab[pos-1]!=0) tab[pos-1] = 0; else tab[pos-1] = 1; break;
283  case 76 : pos2 -= 2; pos++; if (!strcmp(tab2[pos2+1],tab2[pos2])) tab[pos-1]=1;
284  else tab[pos-1]=0; break;
285  case 77 : pos2 -= 2; pos++;if (strcmp(tab2[pos2+1],tab2[pos2])) tab[pos-1]=1;
286  else tab[pos-1]=0; break;
287  case 78 : pos--; tab[pos-1]= ((Int_t) tab[pos-1]) & ((Int_t) tab[pos]); break;
288  case 79 : pos--; tab[pos-1]= ((Int_t) tab[pos-1]) | ((Int_t) tab[pos]); break;
289  }
290  }
291  }
292  Double_t result = tab[0];
293  return result;
294 }
295 
virtual void GroupLeaves()
Definition: hcutg.cc:56
HCutG * GetCutG(int)
Definition: htreeformula.cc:75
TTreeFormula * GetFormulaX()
Definition: hcutg.h:23
Int_t LookForGroupCode(Char_t *, int)
Definition: htreeformula.cc:84
ClassImp(HTreeFormula) HTreeFormula
Definition: htreeformula.cc:18
Short_t fInstances[100]
Definition: htreeformula.h:32
Short_t getIndexByCode(Short_t code)
virtual Double_t EvalInstance(HCombinedIndex *pInstancesIndex)
TTreeFormula * GetFormulaY()
Definition: hcutg.h:24
virtual Double_t EvalInstance(HCombinedIndex *pInstancesIndex)
Definition: hcutg.cc:30
virtual void GroupLeaves()
Definition: htreeformula.cc:30
Definition: hcutg.h:17
Short_t fBranchCodes[100]
Definition: htreeformula.h:31