ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TTreeFormula.cxx
Go to the documentation of this file.
1 // @(#)root/treeplayer:$Id$
2 // Author: Rene Brun 19/01/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 "TROOT.h"
13 #include "TTreeFormula.h"
14 #include "TTree.h"
15 #include "TBranch.h"
16 #include "TBranchObject.h"
17 #include "TFunction.h"
18 #include "TClonesArray.h"
19 #include "TLeafB.h"
20 #include "TLeafC.h"
21 #include "TLeafObject.h"
22 #include "TDataMember.h"
23 #include "TMethodCall.h"
24 #include "TCutG.h"
25 #include "TRandom.h"
26 #include "TInterpreter.h"
27 #include "TDataType.h"
28 #include "TStreamerInfo.h"
29 #include "TStreamerElement.h"
30 #include "TBranchElement.h"
31 #include "TLeafElement.h"
32 #include "TArrayI.h"
33 #include "TAxis.h"
34 #include "TError.h"
36 #include "TString.h"
37 #include "TTimeStamp.h"
38 #include "TMath.h"
39 
40 #include "TVirtualRefProxy.h"
41 #include "TTreeFormulaManager.h"
42 #include "TFormLeafInfo.h"
43 #include "TMethod.h"
44 #include "TBaseClass.h"
45 #include "TFormLeafInfoReference.h"
46 
47 #include "TEntryList.h"
48 
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <math.h>
52 #include <stdlib.h>
53 #include <typeinfo>
54 #include <algorithm>
55 
56 const Int_t kMaxLen = 1024;
57 
58 /** \class TTreeFormula
59 Used to pass a selection expression to the Tree drawing routine. See TTree::Draw
60 
61 A TreeFormula can contain any arithmetic expression including
62 standard operators and mathematical functions separated by operators.
63 Examples of valid expression:
64 ~~~{.cpp}
65  "x<y && sqrt(z)>3.2"
66 ~~~
67 TTreeFormula now relies on a variety of TFormLeafInfo classes to handle the
68 reading of the information. Here is the list of theses classes:
69  - TFormLeafInfo
70  - TFormLeafInfoDirect
71  - TFormLeafInfoNumerical
72  - TFormLeafInfoClones
73  - TFormLeafInfoCollection
74  - TFormLeafInfoPointer
75  - TFormLeafInfoMethod
76  - TFormLeafInfoMultiVarDim
77  - TFormLeafInfoMultiVarDimDirect
78  - TFormLeafInfoCast
79 
80 The following method are available from the TFormLeafInfo interface:
81 
82  - AddOffset(Int_t offset, TStreamerElement* element)
83  - GetCounterValue(TLeaf* leaf) : return the size of the array pointed to.
84  - GetObjectAddress(TLeafElement* leaf) : Returns the the location of the object pointed to.
85  - GetMultiplicity() : Returns info on the variability of the number of elements
86  - GetNdata(TLeaf* leaf) : Returns the number of elements
87  - GetNdata() : Used by GetNdata(TLeaf* leaf)
88  - GetValue(TLeaf *leaf, Int_t instance = 0) : Return the value
89  - GetValuePointer(TLeaf *leaf, Int_t instance = 0) : Returns the address of the value
90  - GetLocalValuePointer(TLeaf *leaf, Int_t instance = 0) : Returns the address of the value of 'this' LeafInfo
91  - IsString()
92  - ReadValue(char *where, Int_t instance = 0) : Internal function to interpret the location 'where'
93  - Update() : react to the possible loading of a shared library.
94 */
95 
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 
100 inline static void R__LoadBranch(TBranch* br, Long64_t entry, Bool_t quickLoad)
101 {
102  if (!quickLoad || (br->GetReadEntry() != entry)) {
103  br->GetEntry(entry);
104  }
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// \class TDimensionInfo
109 /// A small helper class to help in keeping track of the array
110 /// dimensions encountered in the analysis of the expression.
111 
112 class TDimensionInfo : public TObject {
113 public:
114  Int_t fCode; // Location of the leaf in TTreeFormula::fCode
115  Int_t fOper; // Location of the Operation using the leaf in TTreeFormula::fOper
116  Int_t fSize;
117  TFormLeafInfoMultiVarDim* fMultiDim;
118  TDimensionInfo(Int_t code, Int_t oper, Int_t size, TFormLeafInfoMultiVarDim* multiDim)
119  : fCode(code), fOper(oper), fSize(size), fMultiDim(multiDim) {};
120  ~TDimensionInfo() {};
121 };
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 
125 TTreeFormula::TTreeFormula(): ROOT::v5::TFormula(), fQuickLoad(kFALSE), fNeedLoading(kTRUE),
126  fDidBooleanOptimization(kFALSE), fDimensionSetup(0)
127 
128 {
129  // Tree Formula default constructor
130 
131  fTree = 0;
132  fLookupType = 0;
133  fNindex = 0;
134  fNcodes = 0;
135  fAxis = 0;
136  fHasCast = 0;
137  fManager = 0;
138  fMultiplicity = 0;
139  fConstLD = 0;
140 
141  Int_t j,k;
142  for (j=0; j<kMAXCODES; j++) {
143  fNdimensions[j] = 0;
144  fCodes[j] = 0;
145  fNdata[j] = 1;
147  for (k = 0; k<kMAXFORMDIM; k++) {
148  fIndexes[j][k] = -1;
149  fCumulSizes[j][k] = 1;
150  fVarIndexes[j][k] = 0;
151  }
152  }
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Normal TTree Formula Constuctor
157 
158 TTreeFormula::TTreeFormula(const char *name,const char *expression, TTree *tree)
159  :ROOT::v5::TFormula(), fTree(tree), fQuickLoad(kFALSE), fNeedLoading(kTRUE),
160  fDidBooleanOptimization(kFALSE), fDimensionSetup(0)
161 {
162  Init(name,expression);
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Constructor used during the expansion of an alias
167 
168 TTreeFormula::TTreeFormula(const char *name,const char *expression, TTree *tree,
169  const std::vector<std::string>& aliases)
170  :ROOT::v5::TFormula(), fTree(tree), fQuickLoad(kFALSE), fNeedLoading(kTRUE),
171  fDidBooleanOptimization(kFALSE), fDimensionSetup(0), fAliasesUsed(aliases)
172 {
173  Init(name,expression);
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Initialiation called from the constructors.
178 
179 void TTreeFormula::Init(const char*name, const char* expression)
180 {
181  TDirectory *const savedir=gDirectory;
182 
183  fNindex = kMAXFOUND;
184  fLookupType = new Int_t[fNindex];
185  fNcodes = 0;
186  fMultiplicity = 0;
187  fAxis = 0;
188  fHasCast = 0;
189  fConstLD = 0;
190  Int_t i,j,k;
192  fManager->Add(this);
193 
194  for (j=0; j<kMAXCODES; j++) {
195  fNdimensions[j] = 0;
196  fLookupType[j] = kDirect;
197  fCodes[j] = 0;
198  fNdata[j] = 1;
200  for (k = 0; k<kMAXFORMDIM; k++) {
201  fIndexes[j][k] = -1;
202  fCumulSizes[j][k] = 1;
203  fVarIndexes[j][k] = 0;
204  }
205  }
206 
207  fDimensionSetup = new TList;
208 
209  if (Compile(expression)) {
210  fTree = 0; fNdim = 0;
211  if(savedir) savedir->cd();
212  return;
213  }
214 
215  if (fNcodes >= kMAXFOUND) {
216  Warning("TTreeFormula","Too many items in expression:%s",expression);
217  fNcodes = kMAXFOUND;
218  }
219  SetName(name);
220 
221  for (i=0;i<fNoper;i++) {
222 
223  if (GetAction(i)==kDefinedString) {
224  Int_t string_code = GetActionParam(i);
225  TLeaf *leafc = (TLeaf*)fLeaves.UncheckedAt(string_code);
226  if (!leafc) continue;
227 
228  // We have a string used as a string
229 
230  // This dormant portion of code would be used if (when?) we allow the histogramming
231  // of the integral content (as opposed to the string content) of strings
232  // held in a variable size container delimited by a null (as opposed to
233  // a fixed size container or variable size container whose size is controlled
234  // by a variable). In GetNdata, we will then use strlen to grab the current length.
235  //fCumulSizes[i][fNdimensions[i]-1] = 1;
236  //fUsedSizes[fNdimensions[i]-1] = -TMath::Abs(fUsedSizes[fNdimensions[i]-1]);
237  //fUsedSizes[0] = - TMath::Abs( fUsedSizes[0]);
238 
239  if (fNoper == 1) {
240  // If the string is by itself, then it can safely be histogrammed as
241  // in a string based axis. To histogram the number inside the string
242  // just make it part of a useless expression (for example: mystring+0)
244  }
245  continue;
246  }
247  if (GetAction(i)==kJump && GetActionParam(i)==(fNoper-1)) {
248  // We have cond ? string1 : string2
249  if (IsString(fNoper-1)) SetBit(kIsCharacter);
250  }
251  }
252  if (fNoper == 1 && GetAction(0)==kStringConst) {
254  }
255  if (fNoper==1 && GetAction(0)==kAliasString) {
256  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(0));
257  R__ASSERT(subform);
258  if (subform->IsString()) SetBit(kIsCharacter);
259  } else if (fNoper==2 && GetAction(0)==kAlternateString) {
260  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(0));
261  R__ASSERT(subform);
262  if (subform->IsString()) SetBit(kIsCharacter);
263  }
264 
265  fManager->Sync();
266 
267  // Let's verify the indexes and dies if we need to.
268  Int_t k0,k1;
269  for(k0 = 0; k0 < fNcodes; k0++) {
270  for(k1 = 0; k1 < fNdimensions[k0]; k1++ ) {
271  // fprintf(stderr,"Saw %d dim %d and index %d\n",k1, fFixedSizes[k0][k1], fIndexes[k0][k1]);
272  if ( fIndexes[k0][k1]>=0 && fFixedSizes[k0][k1]>=0
273  && fIndexes[k0][k1]>=fFixedSizes[k0][k1]) {
274  Error("TTreeFormula",
275  "Index %d for dimension #%d in %s is too high (max is %d)",
276  fIndexes[k0][k1],k1+1, expression,fFixedSizes[k0][k1]-1);
277  fTree = 0; fNdim = 0;
278  if(savedir) savedir->cd();
279  return;
280  }
281  }
282  }
283 
284  // Create a list of uniques branches to load.
285  for(k=0; k<fNcodes ; k++) {
286  TLeaf *leaf = k <= fLeaves.GetLast() ? (TLeaf*)fLeaves.UncheckedAt(k) : 0;
287  TBranch *branch = 0;
288  if (leaf) {
289  branch = leaf->GetBranch();
290  if (fBranches.FindObject(branch)) branch = 0;
291  }
292  fBranches.AddAtAndExpand(branch,k);
293  }
294 
296 
298  // Call TTree::GetEntries() to insure that it is already calculated.
299  // This will need to be done anyway at the first iteration and insure
300  // that it will not mess up the branch reading (because TTree::GetEntries
301  // opens all the file in the chain and 'stays' on the last file.
302 
303  Long64_t readentry = fTree->GetReadEntry();
304  Int_t treenumber = fTree->GetTreeNumber();
305  fTree->GetEntries();
306  if (treenumber != fTree->GetTreeNumber()) {
307  if (readentry >= 0) {
308  fTree->LoadTree(readentry);
309  }
311  } else {
312  if (readentry >= 0) {
313  fTree->LoadTree(readentry);
314  }
315  }
316 
317  }
318 
319  if(savedir) savedir->cd();
320 }
321 
322 ////////////////////////////////////////////////////////////////////////////////
323 /// Tree Formula default destructor.
324 
326 {
327  if (fManager) {
328  fManager->Remove(this);
329  if (fManager->fFormulas.GetLast()<0) {
330  delete fManager;
331  fManager = 0;
332  }
333  }
334  // Objects in fExternalCuts are not owned and should not be deleted
335  // fExternalCuts.Clear();
336  fLeafNames.Delete();
338  fMethods.Delete();
339  fAliases.Delete();
340  if (fLookupType) delete [] fLookupType;
341  for (int j=0; j<fNcodes; j++) {
342  for (int k = 0; k<fNdimensions[j]; k++) {
343  if (fVarIndexes[j][k]) delete fVarIndexes[j][k];
344  fVarIndexes[j][k] = 0;
345  }
346  }
347  if (fDimensionSetup) {
349  delete fDimensionSetup;
350  }
351  delete[] fConstLD;
352 }
353 
354 ////////////////////////////////////////////////////////////////////////////////
355 /// This method is used internally to decode the dimensions of the variables.
356 
359  Int_t& virt_dim) {
360  if (info) {
362  //if (fIndexes[code][info->fDim]<0) { // removed because the index might be out of bounds!
363  info->fVirtDim = virt_dim;
364  fManager->AddVarDims(virt_dim); // if (!fVarDims[virt_dim]) fVarDims[virt_dim] = new TArrayI;
365  //}
366  }
367 
368  Int_t vsize = 0;
369 
370  if (fIndexes[code][fNdimensions[code]]==-2) {
371  TTreeFormula *indexvar = fVarIndexes[code][fNdimensions[code]];
372  // ASSERT(indexvar!=0);
373  Int_t index_multiplicity = indexvar->GetMultiplicity();
374  switch (index_multiplicity) {
375  case -1:
376  case 0:
377  case 2:
378  vsize = indexvar->GetNdata();
379  break;
380  case 1:
381  vsize = -1;
382  break;
383  };
384  } else vsize = size;
385 
386  fCumulSizes[code][fNdimensions[code]] = size;
387 
388  if ( fIndexes[code][fNdimensions[code]] < 0 ) {
389  fManager->UpdateUsedSize(virt_dim, vsize);
390  }
391 
392  fNdimensions[code] ++;
393 
394 }
395 
396 ////////////////////////////////////////////////////////////////////////////////
397 /// This method is used internally to decode the dimensions of the variables.
398 
400 {
401  // We assume that there are NO white spaces in the info string
402  const char * current;
403  Int_t size, scanindex, vardim;
404 
405  current = info;
406  vardim = 0;
407  // the next value could be before the string but
408  // that's okay because the next operation is ++
409  // (this is to avoid (?) a if statement at the end of the
410  // loop)
411  if (current[0] != '[') current--;
412  while (current) {
413  current++;
414  scanindex = sscanf(current,"%d",&size);
415  // if scanindex is 0 then we have a name index thus a variable
416  // array (or TClonesArray!).
417 
418  if (scanindex==0) size = -1;
419 
420  vardim += RegisterDimensions(code, size);
421 
422  if (fNdimensions[code] >= kMAXFORMDIM) {
423  // NOTE: test that fNdimensions[code] is NOT too big!!
424 
425  break;
426  }
427  current = (char*)strstr( current, "[" );
428  }
429  return vardim;
430 }
431 
432 
433 ////////////////////////////////////////////////////////////////////////////////
434 /// This method stores the dimension information for later usage.
435 
437  TDimensionInfo * info = new TDimensionInfo(code,fNoper,size,multidim);
438  fDimensionSetup->Add(info);
439  fCumulSizes[code][fNdimensions[code]] = size;
440  fNdimensions[code] ++;
441  return (size==-1) ? 1 : 0;
442 }
443 
444 ////////////////////////////////////////////////////////////////////////////////
445 /// This method is used internally to decode the dimensions of the variables.
446 
448  TFormLeafInfo * /* maininfo */,
449  Bool_t useCollectionObject) {
450  Int_t ndim, size, current, vardim;
451  vardim = 0;
452 
453  const TStreamerElement * elem = leafinfo->fElement;
454  TClass* c = elem ? elem->GetClassPointer() : 0;
455 
456  TFormLeafInfoMultiVarDim * multi = dynamic_cast<TFormLeafInfoMultiVarDim * >(leafinfo);
457  if (multi) {
458  // We have a second variable dimensions
460  multi->fDim = fNdimensions[code];
461  return RegisterDimensions(code, -1, multi);
462  }
463  if (elem->IsA() == TStreamerBasicPointer::Class()) {
464 
465  if (elem->GetArrayDim()>0) {
466 
467  ndim = elem->GetArrayDim();
468  size = elem->GetMaxIndex(0);
469  vardim += RegisterDimensions(code, -1);
470  } else {
471  ndim = 1;
472  size = -1;
473  }
474 
476  TClass *cl = leafinfo->fClass;
477  Int_t offset;
478  TStreamerElement* counter = ((TStreamerInfo*)cl->GetStreamerInfo())->GetStreamerElement(array->GetCountName(),offset);
479 #if 1
480  leafinfo->fCounter = new TFormLeafInfo(cl,offset,counter);
481 #else /* Code is not ready yet see revision 14078 */
482  if (maininfo==0 || maininfo==leafinfo || 1) {
483  leafinfo->fCounter = new TFormLeafInfo(cl,offset,counter);
484  } else {
485  leafinfo->fCounter = maininfo->DeepCopy();
486  TFormLeafInfo *currentinfo = leafinfo->fCounter;
487  while(currentinfo->fNext && currentinfo->fNext->fNext) currentinfo=currentinfo->fNext;
488  delete currentinfo->fNext;
489  currentinfo->fNext = new TFormLeafInfo(cl,offset,counter);
490  }
491 #endif
492  } else if (!useCollectionObject && elem->GetClassPointer() == TClonesArray::Class() ) {
493 
494  ndim = 1;
495  size = -1;
496 
497  TClass * clonesClass = TClonesArray::Class();
498  Int_t c_offset;
499  TStreamerElement *counter = ((TStreamerInfo*)clonesClass->GetStreamerInfo())->GetStreamerElement("fLast",c_offset);
500  leafinfo->fCounter = new TFormLeafInfo(clonesClass,c_offset,counter);
501 
502  } else if (!useCollectionObject && elem->GetClassPointer() && elem->GetClassPointer()->GetCollectionProxy() ) {
503 
504  if ( typeid(*leafinfo) == typeid(TFormLeafInfoCollection) ) {
505  ndim = 1;
506  size = -1;
507  } else {
508  R__ASSERT( fHasMultipleVarDim[code] );
509  ndim = 1;
510  size = 1;
511  }
512 
513  } else if ( c && c->GetReferenceProxy() && c->GetReferenceProxy()->HasCounter() ) {
514  ndim = 1;
515  size = -1;
516  } else if (elem->GetArrayDim()>0) {
517 
518  ndim = elem->GetArrayDim();
519  size = elem->GetMaxIndex(0);
520 
521  } else if ( elem->GetNewType()== TStreamerInfo::kCharStar) {
522 
523  // When we implement being able to read the length from
524  // strlen, we will have:
525  // ndim = 1;
526  // size = -1;
527  // until then we more or so die:
528  ndim = 1;
529  size = 1; //NOTE: changed from 0
530 
531  } else return 0;
532 
533  current = 0;
534  do {
535  vardim += RegisterDimensions(code, size);
536 
537  if (fNdimensions[code] >= kMAXFORMDIM) {
538  // NOTE: test that fNdimensions[code] is NOT too big!!
539 
540  break;
541  }
542  current++;
543  size = elem->GetMaxIndex(current);
544  } while (current<ndim);
545 
546  return vardim;
547 }
548 
549 ////////////////////////////////////////////////////////////////////////////////
550 /// This method is used internally to decode the dimensions of the variables.
551 
553  TBranchElement * leafcount2 = branch->GetBranchCount2();
554  if (leafcount2) {
555  // With have a second variable dimensions
556  TBranchElement *leafcount = dynamic_cast<TBranchElement*>(branch->GetBranchCount());
557 
558  R__ASSERT(leafcount); // The function should only be called on a functional TBranchElement object
559 
562  fDataMembers.AddAtAndExpand(info, code);
563  fHasMultipleVarDim[code] = kTRUE;
564 
565  info->fCounter = new TFormLeafInfoDirect(leafcount);
566  info->fCounter2 = new TFormLeafInfoDirect(leafcount2);
567  info->fDim = fNdimensions[code];
568  //if (fIndexes[code][info->fDim]<0) {
569  // info->fVirtDim = virt_dim;
570  // if (!fVarDims[virt_dim]) fVarDims[virt_dim] = new TArrayI;
571  //}
572  return RegisterDimensions(code, -1, info);
573  }
574  return 0;
575 }
576 
577 ////////////////////////////////////////////////////////////////////////////////
578 /// This method is used internally to decode the dimensions of the variables.
579 
581  Int_t numberOfVarDim = 0;
582 
583  // Let see if we can understand the structure of this branch.
584  // Usually we have: leafname[fixed_array] leaftitle[var_array]\type
585  // (with fixed_array that can be a multi-dimension array.
586  const char *tname = leaf->GetTitle();
587  char *leaf_dim = (char*)strstr( tname, "[" );
588 
589  const char *bname = leaf->GetBranch()->GetName();
590  char *branch_dim = (char*)strstr(bname,"[");
591  if (branch_dim) branch_dim++; // skip the '['
592 
593  Bool_t isString = kFALSE;
594  if (leaf->IsA() == TLeafElement::Class()) {
595  Int_t type =((TBranchElement*)leaf->GetBranch())->GetStreamerType();
596  isString = (type == TStreamerInfo::kOffsetL+TStreamerInfo::kChar)
597  || (type == TStreamerInfo::kCharStar);
598  } else {
599  isString = (leaf->IsA() == TLeafC::Class());
600  }
601  if (leaf_dim) {
602  leaf_dim++; // skip the '['
603  if (!branch_dim || strncmp(branch_dim,leaf_dim,strlen(branch_dim))) {
604  // then both are NOT the same so do the leaf title first:
605  numberOfVarDim += RegisterDimensions( leaf_dim, code);
606  } else if (branch_dim && strncmp(branch_dim,leaf_dim,strlen(branch_dim))==0
607  && strlen(leaf_dim)>strlen(branch_dim)
608  && (leaf_dim+strlen(branch_dim))[0]=='[') {
609  // we have extra info in the leaf title
610  numberOfVarDim += RegisterDimensions( leaf_dim+strlen(branch_dim)+1, code);
611  }
612  }
613  if (branch_dim) {
614  // then both are NOT same so do the branch name next:
615  if (isString) {
616  numberOfVarDim += RegisterDimensions( code, 1);
617  } else {
618  numberOfVarDim += RegisterDimensions( branch_dim, code);
619  }
620  }
621  if (leaf->IsA() == TLeafElement::Class()) {
622  TBranchElement* branch = (TBranchElement*) leaf->GetBranch();
623  if (branch->GetBranchCount2()) {
624 
625  if (!branch->GetBranchCount()) {
626  Warning("DefinedVariable",
627  "Noticed an incorrect in-memory TBranchElement object (%s).\nIt has a BranchCount2 but no BranchCount!\nThe result might be incorrect!",
628  branch->GetName());
629  return numberOfVarDim;
630  }
631 
632  // Switch from old direct style to using a TLeafInfo
633  if (fLookupType[code] == kDataMember)
634  Warning("DefinedVariable",
635  "Already in kDataMember mode when handling multiple variable dimensions");
636  fLookupType[code] = kDataMember;
637 
638  // Feed the information into the Dimensions system
639  numberOfVarDim += RegisterDimensions( code, branch);
640 
641  }
642  }
643  return numberOfVarDim;
644 }
645 
646 ////////////////////////////////////////////////////////////////////////////////
647 /// This method check for treat the case where expression contains $Atl and load up
648 /// both fAliases and fExpr.
649 /// We return:
650 /// - -1 in case of failure
651 /// - 0 in case we did not find $Alt
652 /// - the action number in case of success.
653 
654 Int_t TTreeFormula::DefineAlternate(const char *expression)
655 {
656  static const char *altfunc = "Alt$(";
657  static const char *minfunc = "MinIf$(";
658  static const char *maxfunc = "MaxIf$(";
659  Int_t action = 0;
660  Int_t start = 0;
661 
662  if ( strncmp(expression,altfunc,strlen(altfunc))==0
663  && expression[strlen(expression)-1]==')' ) {
664  action = kAlternate;
665  start = strlen(altfunc);
666  }
667  if ( strncmp(expression,maxfunc,strlen(maxfunc))==0
668  && expression[strlen(expression)-1]==')' ) {
669  action = kMaxIf;
670  start = strlen(maxfunc);
671  }
672  if ( strncmp(expression,minfunc,strlen(minfunc))==0
673  && expression[strlen(expression)-1]==')' ) {
674  action = kMinIf;
675  start = strlen(minfunc);
676  }
677 
678  if (action) {
679  TString full = expression;
680  TString part1;
681  TString part2;
682  int paran = 0;
683  int instr = 0;
684  int brack = 0;
685  for(unsigned int i=start;i<strlen(expression);++i) {
686  switch (expression[i]) {
687  case '(': paran++; break;
688  case ')': paran--; break;
689  case '"': instr = instr ? 0 : 1; break;
690  case '[': brack++; break;
691  case ']': brack--; break;
692  };
693  if (expression[i]==',' && paran==0 && instr==0 && brack==0) {
694  part1 = full( start, i-start );
695  part2 = full( i+1, full.Length() -1 - (i+1) );
696  break; // out of the for loop
697  }
698  }
699  if (part1.Length() && part2.Length()) {
700  TTreeFormula *primary = new TTreeFormula("primary",part1,fTree);
701  TTreeFormula *alternate = new TTreeFormula("alternate",part2,fTree);
702 
703  short isstring = 0;
704 
705  if (action == kAlternate) {
706  if (alternate->GetManager()->GetMultiplicity() != 0 ) {
707  Error("DefinedVariable","The 2nd arguments in %s can not be an array (%s,%d)!",
708  expression,alternate->GetTitle(),
709  alternate->GetManager()->GetMultiplicity());
710  return -1;
711  }
712 
713  // Should check whether we have strings.
714  if (primary->IsString()) {
715  if (!alternate->IsString()) {
716  Error("DefinedVariable",
717  "The 2nd arguments in %s has to return the same type as the 1st argument (string)!",
718  expression);
719  return -1;
720  }
721  isstring = 1;
722  } else if (alternate->IsString()) {
723  Error("DefinedVariable",
724  "The 2nd arguments in %s has to return the same type as the 1st argument (numerical type)!",
725  expression);
726  return -1;
727  }
728  } else {
729  primary->GetManager()->Add( alternate );
730  primary->GetManager()->Sync();
731  if (primary->IsString() || alternate->IsString()) {
732  if (!alternate->IsString()) {
733  Error("DefinedVariable",
734  "The arguments of %s can not be strings!",
735  expression);
736  return -1;
737  }
738  }
739  }
740 
741  fAliases.AddAtAndExpand(primary,fNoper);
742  fExpr[fNoper] = "";
743  SetAction(fNoper, (Int_t)action + isstring, 0 );
744  ++fNoper;
745 
746  fAliases.AddAtAndExpand(alternate,fNoper);
747  return (Int_t)kAlias + isstring;
748  }
749  }
750  return 0;
751 }
752 
753 ////////////////////////////////////////////////////////////////////////////////
754 /// Decompose 'expression' as pointing to something inside the leaf
755 /// Returns:
756 /// - -2 Error: some information is missing (message already printed)
757 /// - -1 Error: Syntax is incorrect (message already printed)
758 /// - 0
759 /// - >0 the value returns is the action code.
760 
761 Int_t TTreeFormula::ParseWithLeaf(TLeaf* leaf, const char* subExpression, Bool_t final, UInt_t paran_level, TObjArray& castqueue, Bool_t useLeafCollectionObject, const char* fullExpression)
762 {
763  Int_t action = 0;
764 
765  Int_t numberOfVarDim = 0;
766  char *current;
767 
768  char scratch[kMaxLen]; scratch[0] = '\0';
769  char work[kMaxLen]; work[0] = '\0';
770 
771  const char *right = subExpression;
772  TString name = fullExpression;
773 
774  TBranch *branch = leaf ? leaf->GetBranch() : 0;
775  Long64_t readentry = fTree->GetTree()->GetReadEntry();
776  if (readentry < 0) readentry=0;
777 
778  Bool_t useLeafReferenceObject = false;
779  Int_t code = fNcodes-1;
780 
781  // Make a check to prevent problem with some corrupted files (missing TStreamerInfo).
782  if (leaf && leaf->IsA()==TLeafElement::Class()) {
783  TBranchElement *br = 0;
784  if( branch->IsA() == TBranchElement::Class() )
785  {
786  br = ((TBranchElement*)branch);
787 
788  if ( br->GetInfo() == 0 ) {
789  Error("DefinedVariable","Missing StreamerInfo for %s. We will be unable to read!",
790  name.Data());
791  return -2;
792  }
793  }
794 
795  TBranch *bmom = branch->GetMother();
796  if( bmom->IsA() == TBranchElement::Class() )
797  {
798  TBranchElement *mom = (TBranchElement*)br->GetMother();
799  if (mom!=br) {
800  if (mom->GetInfo()==0) {
801  Error("DefinedVariable","Missing StreamerInfo for %s."
802  " We will be unable to read!",
803  mom->GetName());
804  return -2;
805  }
806  if ((mom->GetType()) < -1 && !mom->GetAddress()) {
807  Error("DefinedVariable", "Address not set when the type of the branch is negative for for %s. We will be unable to read!", mom->GetName());
808  return -2;
809  }
810  }
811  }
812  }
813 
814  // We need to record the location in the list of leaves because
815  // the tree might actually be a chain and in that case the leaf will
816  // change from tree to tree!.
817 
818  // Let's reconstruct the name of the leaf, including the possible friend alias
819  TTree *realtree = fTree->GetTree();
820  const char* alias = 0;
821  if (leaf) {
822  if (realtree) alias = realtree->GetFriendAlias(leaf->GetBranch()->GetTree());
823  if (!alias && realtree!=fTree) {
824  // Let's try on the chain
825  alias = fTree->GetFriendAlias(leaf->GetBranch()->GetTree());
826  }
827  }
828  if (alias) snprintf(scratch,kMaxLen-1,"%s.%s",alias,leaf->GetName());
829  else if (leaf) strlcpy(scratch,leaf->GetName(),kMaxLen);
830 
831  TTree *tleaf = realtree;
832  if (leaf) {
833  tleaf = leaf->GetBranch()->GetTree();
834  fCodes[code] = tleaf->GetListOfLeaves()->IndexOf(leaf);
835  const char *mother_name = leaf->GetBranch()->GetMother()->GetName();
836  TString br_extended_name; // Could do ( strlen(mother_name)+strlen( leaf->GetBranch()->GetName() ) + 2 )
837  if (leaf->GetBranch()!=leaf->GetBranch()->GetMother()) {
838  if (mother_name[strlen(mother_name)-1]!='.') {
839  br_extended_name = mother_name;
840  br_extended_name.Append('.');
841  }
842  }
843  br_extended_name.Append( leaf->GetBranch()->GetName() );
844  Ssiz_t dim = br_extended_name.First('[');
845  if (dim >= 0) br_extended_name.Remove(dim);
846 
847  TNamed *named = new TNamed(scratch,br_extended_name.Data());
848  fLeafNames.AddAtAndExpand(named,code);
849  fLeaves.AddAtAndExpand(leaf,code);
850  }
851 
852  // If the leaf belongs to a friend tree which has an index, we might
853  // be in the case where some entry do not exist.
854  if (tleaf != realtree && tleaf->GetTreeIndex()) {
855  // reset the multiplicity
856  if (fMultiplicity >= 0) fMultiplicity = 1;
857  }
858 
859  // Analyze the content of 'right'
860 
861  // Try to find out the class (if any) of the object in the leaf.
862  TClass * cl = 0;
863  TFormLeafInfo *maininfo = 0;
864  TFormLeafInfo *previnfo = 0;
865  Bool_t unwindCollection = kFALSE;
866  const static TClassRef stdStringClass = TClass::GetClass("string");
867 
868  if (leaf==0) {
869  TNamed *names = (TNamed*)fLeafNames.UncheckedAt(code);
870  fLeafNames.AddAt(0,code);
871  TTree *what = (TTree*)fLeaves.UncheckedAt(code);
872  fLeaves.AddAt(0,code);
873 
874  cl = what ? what->IsA() : TTree::Class();
875  maininfo = new TFormLeafInfoTTree(fTree,names->GetName(),what);
876  previnfo = maininfo;
877 
878  delete names;
879  } else if (leaf->InheritsFrom(TLeafObject::Class()) ) {
880  TBranchObject *bobj = (TBranchObject*)leaf->GetBranch();
881  cl = TClass::GetClass(bobj->GetClassName());
882  } else if (leaf->InheritsFrom(TLeafElement::Class())) {
883  TBranchElement *branchEl = (TBranchElement *)leaf->GetBranch();
884  branchEl->SetupAddresses();
885  TStreamerInfo *info = branchEl->GetInfo();
886  TStreamerElement *element = 0;
887  Int_t type = branchEl->GetStreamerType();
888  switch(type) {
894  case TStreamerInfo::kAny:
897  case TStreamerInfo::kSTL:
901  element = info->GetElement(branchEl->GetID());
902  if (element) cl = element->GetClassPointer();
903  }
904  break;
913  element = info->GetElement(branchEl->GetID());
914  if (element){
915  cl = element->GetClassPointer();
916  }
917  }
918  break;
919  case -1: {
920  cl = info->GetClass();
921  }
922  break;
923  }
924 
925  // If we got a class object, we need to verify whether it is on a
926  // split TClonesArray sub branch.
927  if (cl && branchEl->GetBranchCount()) {
928  if (branchEl->GetType()==31) {
929  // This is inside a TClonesArray.
930 
931  if (!element) {
932  Warning("DefineVariable",
933  "Missing TStreamerElement in object in TClonesArray section");
934  return -2;
935  }
936  TFormLeafInfo* clonesinfo = new TFormLeafInfoClones(cl, 0, element, kTRUE);
937 
938  // The following code was commmented out because in THIS case
939  // the dimension are actually handled by parsing the title and name of the leaf
940  // and branch (see a little further)
941  // The dimension needs to be handled!
942  // numberOfVarDim += RegisterDimensions(code,clonesinfo);
943 
944  maininfo = clonesinfo;
945 
946  // We skip some cases because we can assume we have an object.
947  Int_t offset=0;
948  info->GetStreamerElement(element->GetName(),offset);
949  if (type == TStreamerInfo::kObjectp ||
950  type == TStreamerInfo::kObjectP ||
953  type == TStreamerInfo::kSTLp ||
954  type == TStreamerInfo::kAnyp ||
955  type == TStreamerInfo::kAnyP ||
959  previnfo = new TFormLeafInfoPointer(cl,offset+branchEl->GetOffset(),element);
960  } else {
961  previnfo = new TFormLeafInfo(cl,offset+branchEl->GetOffset(),element);
962  }
963  maininfo->fNext = previnfo;
964  unwindCollection = kTRUE;
965 
966  } else if (branchEl->GetType()==41) {
967 
968  // This is inside a Collection
969 
970  if (!element) {
971  Warning("DefineVariable","Missing TStreamerElement in object in Collection section");
972  return -2;
973  }
974  // First we need to recover the collection.
975  TBranchElement *count = branchEl->GetBranchCount();
976  TFormLeafInfo* collectioninfo;
977  if ( count->GetID() >= 0 ) {
978  TStreamerElement *collectionElement =
979  count->GetInfo()->GetElement(count->GetID());
980  TClass *collectionCl = collectionElement->GetClassPointer();
981 
982  collectioninfo =
983  new TFormLeafInfoCollection(collectionCl, 0, collectionElement, kTRUE);
984  } else {
985  TClass *collectionCl = TClass::GetClass(count->GetClassName());
986  collectioninfo =
987  new TFormLeafInfoCollection(collectionCl, 0, collectionCl, kTRUE);
988  }
989 
990  // The following code was commmented out because in THIS case
991  // the dimension are actually handled by parsing the title and name of the leaf
992  // and branch (see a little further)
993  // The dimension needs to be handled!
994  // numberOfVarDim += RegisterDimensions(code,clonesinfo);
995 
996  maininfo = collectioninfo;
997 
998  // We skip some cases because we can assume we have an object.
999  Int_t offset=0;
1000  info->GetStreamerElement(element->GetName(),offset);
1001  if (type == TStreamerInfo::kObjectp ||
1002  type == TStreamerInfo::kObjectP ||
1005  type == TStreamerInfo::kSTLp ||
1006  type == TStreamerInfo::kAnyp ||
1007  type == TStreamerInfo::kAnyP ||
1011  previnfo = new TFormLeafInfoPointer(cl,offset+branchEl->GetOffset(),element);
1012  } else {
1013  previnfo = new TFormLeafInfo(cl,offset+branchEl->GetOffset(),element);
1014  }
1015  maininfo->fNext = previnfo;
1016  unwindCollection = kTRUE;
1017  }
1018  } else if ( branchEl->GetType()==3) {
1019  TFormLeafInfo* clonesinfo;
1020  if (useLeafCollectionObject) {
1021  clonesinfo = new TFormLeafInfoCollectionObject(cl);
1022  } else {
1023  clonesinfo = new TFormLeafInfoClones(cl, 0, kTRUE);
1024  // The dimension needs to be handled!
1025  numberOfVarDim += RegisterDimensions(code,clonesinfo,maininfo,useLeafCollectionObject);
1026 
1027  }
1028  maininfo = clonesinfo;
1029  previnfo = maininfo;
1030 
1031  } else if (!useLeafCollectionObject && branchEl->GetType()==4) {
1032 
1033  TFormLeafInfo* collectioninfo;
1034  if (useLeafCollectionObject) {
1035  collectioninfo = new TFormLeafInfoCollectionObject(cl);
1036  } else {
1037  collectioninfo = new TFormLeafInfoCollection(cl, 0, cl, kTRUE);
1038  // The dimension needs to be handled!
1039  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,useLeafCollectionObject);
1040  }
1041 
1042  maininfo = collectioninfo;
1043  previnfo = maininfo;
1044 
1045  } else if (branchEl->GetStreamerType()==-1 && cl && cl->GetCollectionProxy()) {
1046 
1047  if (useLeafCollectionObject) {
1048 
1049  TFormLeafInfo *collectioninfo = new TFormLeafInfoCollectionObject(cl);
1050  maininfo = collectioninfo;
1051  previnfo = collectioninfo;
1052 
1053  } else {
1054  TFormLeafInfo *collectioninfo = new TFormLeafInfoCollection(cl, 0, cl, kTRUE);
1055  // The dimension needs to be handled!
1056  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,kFALSE);
1057 
1058  maininfo = collectioninfo;
1059  previnfo = collectioninfo;
1060 
1061  if (cl->GetCollectionProxy()->GetValueClass()!=0 &&
1063 
1065  cl->GetCollectionProxy()->GetValueClass(),collectioninfo);
1066 
1067  fHasMultipleVarDim[code] = kTRUE;
1068  numberOfVarDim += RegisterDimensions(code,multi,maininfo,kFALSE);
1069  previnfo->fNext = multi;
1070  cl = cl->GetCollectionProxy()->GetValueClass();
1071  multi->fNext = new TFormLeafInfoCollection(cl, 0, cl, false);
1072  previnfo = multi->fNext;
1073 
1074  }
1075  if (cl->GetCollectionProxy()->GetValueClass()==0 &&
1076  cl->GetCollectionProxy()->GetType()>0) {
1077 
1078  previnfo->fNext =
1080  previnfo = previnfo->fNext;
1081  } else {
1082  // nothing to do
1083  }
1084  }
1085 
1086  } else if (strlen(right)==0 && cl && element && final) {
1087 
1088  TClass *elemCl = element->GetClassPointer();
1089  if (!useLeafCollectionObject
1090  && elemCl && elemCl->GetCollectionProxy()
1091  && elemCl->GetCollectionProxy()->GetValueClass()
1093 
1094  TFormLeafInfo *collectioninfo =
1095  new TFormLeafInfoCollection(cl, 0, elemCl);
1096 
1097  // The dimension needs to be handled!
1098  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,kFALSE);
1099 
1100  maininfo = collectioninfo;
1101  previnfo = collectioninfo;
1102 
1103  TFormLeafInfo *multi =
1104  new TFormLeafInfoMultiVarDimCollection(elemCl, 0,
1105  elemCl->GetCollectionProxy()->GetValueClass(),
1106  collectioninfo);
1107 
1108  fHasMultipleVarDim[code] = kTRUE;
1109  numberOfVarDim += RegisterDimensions(code,multi,maininfo,kFALSE);
1110  previnfo->fNext = multi;
1111  cl = elemCl->GetCollectionProxy()->GetValueClass();
1112  multi->fNext = new TFormLeafInfoCollection(cl, 0, cl, false);
1113  previnfo = multi->fNext;
1114 
1115  if (cl->GetCollectionProxy()->GetValueClass()==0 &&
1116  cl->GetCollectionProxy()->GetType()>0) {
1117 
1118  previnfo->fNext =
1120  previnfo = previnfo->fNext;
1121  }
1122 
1123  } else if (!useLeafCollectionObject
1124  && elemCl && elemCl->GetCollectionProxy()
1125  && elemCl->GetCollectionProxy()->GetValueClass()==0
1126  && elemCl->GetCollectionProxy()->GetType()>0) {
1127 
1128  // At this point we have an element which is inside a class (which is not
1129  // a collection) and this element of a collection of numerical type.
1130  // (Note: it is not possible to have more than one variable dimension
1131  // unless we were supporting variable size C-style array of collection).
1132 
1133  TFormLeafInfo* collectioninfo =
1134  new TFormLeafInfoCollection(cl, 0, elemCl);
1135 
1136  // The dimension needs to be handled!
1137  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,kFALSE);
1138 
1139  collectioninfo->fNext =
1141 
1142  maininfo = collectioninfo;
1143  previnfo = maininfo->fNext;
1144 
1145  } else if (!useLeafCollectionObject
1146  && elemCl && elemCl->GetCollectionProxy()) {
1147  if (elemCl->GetCollectionProxy()->GetValueClass()==TString::Class()) {
1148  right = "Data()";
1149  } else if (elemCl->GetCollectionProxy()->GetValueClass()==stdStringClass) {
1150  right = "c_str()";
1151  }
1152 
1153  } else if (!element->IsaPointer()) {
1154 
1155  maininfo = new TFormLeafInfoDirect(branchEl);
1156  previnfo = maininfo;
1157 
1158  }
1159  }
1160  else if ( cl && cl->GetReferenceProxy() ) {
1161  if ( useLeafCollectionObject || fullExpression[0] == '@' || fullExpression[strlen(scratch)] == '@' ) {
1162  useLeafReferenceObject = true;
1163  }
1164  else {
1165  if ( !maininfo ) {
1166  maininfo = previnfo = new TFormLeafInfoReference(cl, element, 0);
1167  numberOfVarDim += RegisterDimensions(code,maininfo,maininfo,kFALSE);
1168  }
1169  TVirtualRefProxy *refproxy = cl->GetReferenceProxy();
1170  for(Long64_t i=0; i<leaf->GetBranch()->GetEntries()-readentry; ++i) {
1171  R__LoadBranch(leaf->GetBranch(), readentry+i, fQuickLoad);
1172  void *refobj = maininfo->GetValuePointer(leaf,0);
1173  if (refobj) {
1174  cl = refproxy->GetValueClass(refobj);
1175  }
1176  if ( cl ) break;
1177  }
1178  if ( !cl ) {
1179  Error("DefinedVariable","Failed to access class type of reference target (%s)",element->GetName());
1180  return -1;
1181  }
1182  }
1183  }
1184  }
1185 
1186  // Treat the dimension information in the leaf name, title and 2nd branch count
1187  if (leaf) numberOfVarDim += RegisterDimensions(code,leaf);
1188 
1189  if (cl) {
1190  if (unwindCollection) {
1191  // So far we should get here only if we encounter a split collection of a class that contains
1192  // directly a collection.
1193  R__ASSERT(numberOfVarDim==1 && maininfo);
1194 
1195  if (!useLeafCollectionObject && cl && cl->GetCollectionProxy()) {
1196  TFormLeafInfo *multi =
1197  new TFormLeafInfoMultiVarDimCollection(cl, 0, cl, maininfo);
1198  fHasMultipleVarDim[code] = kTRUE;
1199  numberOfVarDim += RegisterDimensions(code,multi,maininfo,kFALSE);
1200  previnfo->fNext = multi;
1201 
1202  multi->fNext = new TFormLeafInfoCollection(cl, 0, cl, false);
1203  previnfo = multi->fNext;
1204 
1205  if (cl->GetCollectionProxy()->GetValueClass()==0 &&
1206  cl->GetCollectionProxy()->GetType()>0) {
1207 
1208  previnfo->fNext =
1210  previnfo = previnfo->fNext;
1211  }
1212  } else if (!useLeafCollectionObject && cl == TClonesArray::Class()) {
1213 
1214  TFormLeafInfo *multi =
1215  new TFormLeafInfoMultiVarDimClones(cl, 0, cl, maininfo);
1216  fHasMultipleVarDim[code] = kTRUE;
1217  numberOfVarDim += RegisterDimensions(code,multi,maininfo,kFALSE);
1218  previnfo->fNext = multi;
1219 
1220  multi->fNext = new TFormLeafInfoClones(cl, 0, false);
1221  previnfo = multi->fNext;
1222  }
1223  }
1224  Int_t offset=0;
1225  if (cl == TString::Class() && strcmp(right,"fData")==0) {
1226  // For backward compatibility replace TString::fData which no longer exist
1227  // by a call to TString::Data()
1228  right = "Data()";
1229  }
1230  Int_t nchname = strlen(right);
1231  TFormLeafInfo *leafinfo = 0;
1232  TStreamerElement* element = 0;
1233 
1234  // Let see if the leaf was attempted to be casted.
1235  // Since there would have been something like
1236  // ((cast_class*)leafname)->.... we need to use
1237  // paran_level+1
1238  // Also we disable this functionality in case of TClonesArray
1239  // because it is not yet allowed to have 'inheritance' (or virtuality)
1240  // in play in a TClonesArray.
1241  {
1242  TClass * casted = (TClass*) castqueue.At(paran_level+1);
1243  if (casted && cl != TClonesArray::Class()) {
1244  if ( ! casted->InheritsFrom(cl) ) {
1245  Error("DefinedVariable","%s does not inherit from %s. Casting not possible!",
1246  casted->GetName(),cl->GetName());
1247  return -2;
1248  }
1249  leafinfo = new TFormLeafInfoCast(cl,casted);
1250  fHasCast = kTRUE;
1251  if (maininfo==0) {
1252  maininfo = leafinfo;
1253  }
1254  if (previnfo==0) {
1255  previnfo = leafinfo;
1256  } else {
1257  previnfo->fNext = leafinfo;
1258  previnfo = leafinfo;
1259  }
1260  leafinfo = 0;
1261 
1262  cl = casted;
1263  castqueue.AddAt(0,paran_level);
1264  }
1265  }
1266  Int_t i;
1267  Bool_t prevUseCollectionObject = useLeafCollectionObject;
1268  Bool_t useCollectionObject = useLeafCollectionObject;
1269  Bool_t useReferenceObject = useLeafReferenceObject;
1270  Bool_t prevUseReferenceObject = useLeafReferenceObject;
1271  for (i=0, current = &(work[0]); i<=nchname;i++ ) {
1272  // We will treated the terminator as a token.
1273  if (right[i] == '(') {
1274  // Right now we do not allow nested paranthesis
1275  do {
1276  *current++ = right[i++];
1277  } while(right[i]!=')' && right[i]);
1278  *current++ = right[i];
1279  *current='\0';
1280  char *params = strchr(work,'(');
1281  if (params) {
1282  *params = 0; params++;
1283  } else params = (char *) ")";
1284  if (cl==0) {
1285  Error("DefinedVariable","Can not call '%s' with a class",work);
1286  return -1;
1287  }
1288  if (!cl->HasDataMemberInfo() && !cl->GetCollectionProxy()) {
1289  Error("DefinedVariable","Class probably unavailable:%s",cl->GetName());
1290  return -2;
1291  }
1292  if (!useCollectionObject && cl == TClonesArray::Class()) {
1293  // We are not interested in the ClonesArray object but only
1294  // in its contents.
1295  // We need to retrieve the class of its content.
1296 
1297  TBranch *clbranch = leaf->GetBranch();
1298  R__LoadBranch(clbranch,readentry,fQuickLoad);
1299  TClonesArray * clones;
1300  if (previnfo) clones = (TClonesArray*)previnfo->GetLocalValuePointer(leaf,0);
1301  else {
1302  Bool_t top = (clbranch==((TBranchElement*)clbranch)->GetMother()
1303  || !leaf->IsOnTerminalBranch());
1304  TClass *mother_cl;
1305  if (leaf->IsA()==TLeafObject::Class()) {
1306  // in this case mother_cl is not really used
1307  mother_cl = cl;
1308  } else {
1309  mother_cl = ((TBranchElement*)clbranch)->GetInfo()->GetClass();
1310  }
1311  TFormLeafInfo* clonesinfo = new TFormLeafInfoClones(mother_cl, 0, top);
1312 
1313  // The dimension needs to be handled!
1314  numberOfVarDim += RegisterDimensions(code,clonesinfo,maininfo,kFALSE);
1315 
1316  previnfo = clonesinfo;
1317  maininfo = clonesinfo;
1318 
1319  clones = (TClonesArray*)clonesinfo->GetLocalValuePointer(leaf,0);
1320  }
1321  TClass * inside_cl = clones->GetClass();
1322  cl = inside_cl;
1323 
1324  }
1325  else if (!useCollectionObject && cl && cl->GetCollectionProxy() ) {
1326 
1327  // We are NEVER (for now!) interested in the ClonesArray object but only
1328  // in its contents.
1329  // We need to retrieve the class of its content.
1330 
1331  if (previnfo==0) {
1332 
1333  Bool_t top = (branch==((TBranchElement*)branch)->GetMother()
1334  || !leaf->IsOnTerminalBranch());
1335 
1336  TClass *mother_cl;
1337  if (leaf->IsA()==TLeafObject::Class()) {
1338  // in this case mother_cl is not really used
1339  mother_cl = cl;
1340  } else {
1341  mother_cl = ((TBranchElement*)branch)->GetInfo()->GetClass();
1342  }
1343 
1344  TFormLeafInfo* collectioninfo =
1345  new TFormLeafInfoCollection(mother_cl, 0,cl,top);
1346  // The dimension needs to be handled!
1347  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,kFALSE);
1348 
1349  previnfo = collectioninfo;
1350  maininfo = collectioninfo;
1351 
1352  }
1353 
1354  TClass * inside_cl = cl->GetCollectionProxy()->GetValueClass();
1355  if (inside_cl) cl = inside_cl;
1356  else if (cl->GetCollectionProxy()->GetType()>0) {
1357  Warning("DefinedVariable","Can not call method on content of %s in %s\n",
1358  cl->GetName(),name.Data());
1359  return -2;
1360  }
1361  }
1362  TMethodCall *method = 0;
1363  if (cl==0) {
1364  Error("DefinedVariable",
1365  "Could not discover the TClass corresponding to (%s)!",
1366  right);
1367  return -2;
1368  } else if (cl==TClonesArray::Class() && strcmp(work,"size")==0) {
1369  method = new TMethodCall(cl, "GetEntriesFast", "");
1370  } else if (cl->GetCollectionProxy() && strcmp(work,"size")==0) {
1371  if (maininfo==0) {
1372  TFormLeafInfo* collectioninfo=0;
1373  if (useLeafCollectionObject) {
1374 
1375  Bool_t top = (branch==((TBranchElement*)branch)->GetMother()
1376  || !leaf->IsOnTerminalBranch());
1377  collectioninfo = new TFormLeafInfoCollectionObject(cl,top);
1378  }
1379  maininfo=previnfo=collectioninfo;
1380  }
1381  leafinfo = new TFormLeafInfoCollectionSize(cl);
1382  cl = 0;
1383  } else {
1384  if (!cl->HasDataMemberInfo()) {
1385  Error("DefinedVariable",
1386  "Can not call method %s on class without dictionary (%s)!",
1387  right,cl->GetName());
1388  return -2;
1389  }
1390  method = new TMethodCall(cl, work, params);
1391  }
1392  if (method) {
1393  if (!method->GetMethod()) {
1394  Error("DefinedVariable","Unknown method:%s in %s",right,cl->GetName());
1395  return -1;
1396  }
1397  switch(method->ReturnType()) {
1398  case TMethodCall::kLong:
1399  leafinfo = new TFormLeafInfoMethod(cl,method);
1400  cl = 0;
1401  break;
1402  case TMethodCall::kDouble:
1403  leafinfo = new TFormLeafInfoMethod(cl,method);
1404  cl = 0;
1405  break;
1406  case TMethodCall::kString:
1407  leafinfo = new TFormLeafInfoMethod(cl,method);
1408  // 1 will be replaced by -1 when we know how to use strlen
1409  numberOfVarDim += RegisterDimensions(code,1); //NOTE: changed from 0
1410  cl = 0;
1411  break;
1412  case TMethodCall::kOther:
1413  {
1414  TString return_type =
1415  gInterpreter->TypeName(method->GetMethod()->GetReturnTypeName());
1416  leafinfo = new TFormLeafInfoMethod(cl,method);
1417  cl = (return_type == "void") ? 0 : TClass::GetClass(return_type.Data());
1418  }
1419  break;
1420  default:
1421  Error("DefineVariable","Method %s from %s has an impossible return type %d",
1422  work,cl->GetName(), (Int_t)method->ReturnType());
1423  return -2;
1424  }
1425  }
1426  if (maininfo==0) {
1427  maininfo = leafinfo;
1428  }
1429  if (previnfo==0) {
1430  previnfo = leafinfo;
1431  } else {
1432  previnfo->fNext = leafinfo;
1433  previnfo = leafinfo;
1434  }
1435  leafinfo = 0;
1436  current = &(work[0]);
1437  *current = 0;
1438  prevUseCollectionObject = kFALSE;
1439  prevUseReferenceObject = kFALSE;
1440  useCollectionObject = kFALSE;
1441 
1442  if (cl && cl->GetCollectionProxy()) {
1443  if (numberOfVarDim>1) {
1444  Warning("DefinedVariable","TTreeFormula support only 2 level of variables size collections. Assuming '@' notation for the collection %s.",
1445  cl->GetName());
1446  leafinfo = new TFormLeafInfo(cl,0,0);
1447  useCollectionObject = kTRUE;
1448  } else if (numberOfVarDim==0) {
1449  R__ASSERT(maininfo);
1450  leafinfo = new TFormLeafInfoCollection(cl,0,cl);
1451  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,kFALSE);
1452  } else if (numberOfVarDim==1) {
1453  R__ASSERT(maininfo);
1454  leafinfo =
1456  (TStreamerElement*)0,maininfo);
1457  previnfo->fNext = leafinfo;
1458  previnfo = leafinfo;
1459  leafinfo = new TFormLeafInfoCollection(cl,0,cl);
1460 
1461  fHasMultipleVarDim[code] = kTRUE;
1462  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,kFALSE);
1463  }
1464  previnfo->fNext = leafinfo;
1465  previnfo = leafinfo;
1466  leafinfo = 0;
1467  }
1468  continue;
1469  } else if (right[i] == ')') {
1470  // We should have the end of a cast operator. Let's introduce a TFormLeafCast
1471  // in the chain.
1472  TClass * casted = (TClass*) ((int(--paran_level)>=0) ? castqueue.At(paran_level) : 0);
1473  if (casted) {
1474  leafinfo = new TFormLeafInfoCast(cl,casted);
1475  fHasCast = kTRUE;
1476 
1477  if (maininfo==0) {
1478  maininfo = leafinfo;
1479  }
1480  if (previnfo==0) {
1481  previnfo = leafinfo;
1482  } else {
1483  previnfo->fNext = leafinfo;
1484  previnfo = leafinfo;
1485  }
1486  leafinfo = 0;
1487  current = &(work[0]);
1488  *current = 0;
1489 
1490  cl = casted;
1491  continue;
1492 
1493  }
1494  } else if (i > 0 && (right[i] == '.' || right[i] == '[' || right[i] == '\0') ) {
1495  // A delimiter happened let's see if what we have seen
1496  // so far does point to a data member.
1497  Bool_t needClass = kTRUE;
1498  *current = '\0';
1499 
1500  // skip it all if there is nothing to look at
1501  if (strlen(work)==0) continue;
1502 
1503  prevUseCollectionObject = useCollectionObject;
1504  prevUseReferenceObject = useReferenceObject;
1505  if (work[0]=='@') {
1506  useReferenceObject = kTRUE;
1507  useCollectionObject = kTRUE;
1508  Int_t l = 0;
1509  for(l=0;work[l+1]!=0;++l) work[l] = work[l+1];
1510  work[l] = '\0';
1511  } else if (work[strlen(work)-1]=='@') {
1512  useReferenceObject = kTRUE;
1513  useCollectionObject = kTRUE;
1514  work[strlen(work)-1] = '\0';
1515  } else {
1516  useReferenceObject = kFALSE;
1517  useCollectionObject = kFALSE;
1518  }
1519 
1520  Bool_t mustderef = kFALSE;
1521  if ( !prevUseReferenceObject && cl && cl->GetReferenceProxy() ) {
1522  R__LoadBranch(leaf->GetBranch(), readentry, fQuickLoad);
1523  if ( !maininfo ) {
1524  maininfo = previnfo = new TFormLeafInfoReference(cl, element, offset);
1525  if ( cl->GetReferenceProxy()->HasCounter() ) {
1526  numberOfVarDim += RegisterDimensions(code,-1);
1527  }
1528  prevUseReferenceObject = kFALSE;
1529  } else {
1530  previnfo->fNext = new TFormLeafInfoReference(cl, element, offset);
1531  previnfo = previnfo->fNext;
1532  }
1533  TVirtualRefProxy *refproxy = cl->GetReferenceProxy();
1534  cl = 0;
1535  for(Long64_t entry=0; entry<leaf->GetBranch()->GetEntries()-readentry; ++entry) {
1536  R__LoadBranch(leaf->GetBranch(), readentry+i, fQuickLoad);
1537  void *refobj = maininfo->GetValuePointer(leaf,0);
1538  if (refobj) {
1539  cl = refproxy->GetValueClass(refobj);
1540  }
1541  if ( cl ) break;
1542  }
1543  needClass = kFALSE;
1544  mustderef = kTRUE;
1545  }
1546  else if (!prevUseCollectionObject && cl == TClonesArray::Class()) {
1547  // We are not interested in the ClonesArray object but only
1548  // in its contents.
1549  // We need to retrieve the class of its content.
1550 
1551  TBranch *clbranch = leaf->GetBranch();
1552  R__LoadBranch(clbranch,readentry,fQuickLoad);
1553  TClonesArray * clones;
1554  if (maininfo) {
1555  clones = (TClonesArray*)maininfo->GetValuePointer(leaf,0);
1556  } else {
1557  // we have a unsplit TClonesArray leaves
1558  // or we did not yet match any of the sub-branches!
1559 
1560  TClass *mother_cl;
1561  if (leaf->IsA()==TLeafObject::Class()) {
1562  // in this case mother_cl is not really used
1563  mother_cl = cl;
1564  } else {
1565  mother_cl = ((TBranchElement*)clbranch)->GetInfo()->GetClass();
1566  }
1567 
1568  TFormLeafInfo* clonesinfo = new TFormLeafInfoClones(mother_cl, 0);
1569  // The dimension needs to be handled!
1570  numberOfVarDim += RegisterDimensions(code,clonesinfo,maininfo,kFALSE);
1571 
1572  mustderef = kTRUE;
1573  previnfo = clonesinfo;
1574  maininfo = clonesinfo;
1575 
1576  if (clbranch->GetListOfBranches()->GetLast()>=0) {
1577  if (clbranch->IsA() != TBranchElement::Class()) {
1578  Error("DefinedVariable","Unimplemented usage of ClonesArray");
1579  return -2;
1580  }
1581  //clbranch = ((TBranchElement*)clbranch)->GetMother();
1582  clones = (TClonesArray*)((TBranchElement*)clbranch)->GetObject();
1583  } else
1584  clones = (TClonesArray*)clonesinfo->GetLocalValuePointer(leaf,0);
1585  }
1586  // NOTE clones can be zero!
1587  if (clones==0) {
1588  Warning("DefinedVariable",
1589  "TClonesArray object was not retrievable for %s!",
1590  name.Data());
1591  return -1;
1592  }
1593  TClass * inside_cl = clones->GetClass();
1594 #if 1
1595  cl = inside_cl;
1596 #else
1597 /* Maybe we should make those test lead to warning messages */
1598  if (1 || inside_cl) cl = inside_cl;
1599  // if inside_cl is nul ... we have a problem of inconsistency :(
1600  if (0 && strlen(work)==0) {
1601  // However in this case we have NO content :(
1602  // so let get the number of objects
1603  //strcpy(work,"fLast");
1604  }
1605 #endif
1606  } else if (!prevUseCollectionObject && cl && cl->GetCollectionProxy() ) {
1607 
1608  // We are NEVER interested in the Collection object but only
1609  // in its contents.
1610  // We need to retrieve the class of its content.
1611 
1612  TBranch *clbranch = leaf->GetBranch();
1613  R__LoadBranch(clbranch,readentry,fQuickLoad);
1614 
1615  if (maininfo==0) {
1616 
1617  // we have a unsplit Collection leaf
1618  // or we did not yet match any of the sub-branches!
1619 
1620  TClass *mother_cl;
1621  if (leaf->IsA()==TLeafObject::Class()) {
1622  // in this case mother_cl is not really used
1623  mother_cl = cl;
1624  } else {
1625  mother_cl = ((TBranchElement*)clbranch)->GetInfo()->GetClass();
1626  }
1627 
1628  TFormLeafInfo* collectioninfo =
1629  new TFormLeafInfoCollection(mother_cl, 0, cl);
1630  // The dimension needs to be handled!
1631  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,kFALSE);
1632 
1633  mustderef = kTRUE;
1634  previnfo = collectioninfo;
1635  maininfo = collectioninfo;
1636 
1637  } //else if (clbranch->GetStreamerType()==0) {
1638 
1639  //}
1640 
1641  TClass * inside_cl = cl->GetCollectionProxy()->GetValueClass();
1642 
1643  if (!inside_cl && cl->GetCollectionProxy()->GetType() > 0) {
1644  Warning("DefinedVariable","No data member in content of %s in %s\n",
1645  cl->GetName(),name.Data());
1646  }
1647  cl = inside_cl;
1648  // if inside_cl is nul ... we have a problem of inconsistency.
1649  }
1650 
1651  if (!cl) {
1652  Warning("DefinedVariable","Missing class for %s!",name.Data());
1653  } else {
1654  element = ((TStreamerInfo*)cl->GetStreamerInfo())->GetStreamerElement(work,offset);
1655  }
1656 
1657  if (!element && !prevUseCollectionObject) {
1658  // We allow for looking for a data member inside a class inside
1659  // a TClonesArray without mentioning the TClonesArrays variable name
1660  TIter next( cl->GetStreamerInfo()->GetElements() );
1661  TStreamerElement * curelem;
1662  while ((curelem = (TStreamerElement*)next())) {
1663  if (curelem->GetClassPointer() == TClonesArray::Class()) {
1664  Int_t clones_offset = 0;
1665  ((TStreamerInfo*)cl->GetStreamerInfo())->GetStreamerElement(curelem->GetName(),clones_offset);
1666  TFormLeafInfo* clonesinfo =
1667  new TFormLeafInfo(cl, clones_offset, curelem);
1668  TClonesArray * clones;
1669  R__LoadBranch(leaf->GetBranch(),readentry,fQuickLoad);
1670 
1671  if (previnfo) {
1672  previnfo->fNext = clonesinfo;
1673  clones = (TClonesArray*)maininfo->GetValuePointer(leaf,0);
1674  previnfo->fNext = 0;
1675  } else {
1676  clones = (TClonesArray*)clonesinfo->GetLocalValuePointer(leaf,0);
1677  }
1678 
1679  TClass *sub_cl = clones->GetClass();
1680  if (sub_cl) element = ((TStreamerInfo*)sub_cl->GetStreamerInfo())->GetStreamerElement(work,offset);
1681  delete clonesinfo;
1682 
1683  if (element) {
1684  leafinfo = new TFormLeafInfoClones(cl,clones_offset,curelem);
1685  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,kFALSE);
1686  if (maininfo==0) maininfo = leafinfo;
1687  if (previnfo==0) previnfo = leafinfo;
1688  else {
1689  previnfo->fNext = leafinfo;
1690  previnfo = leafinfo;
1691  }
1692  leafinfo = 0;
1693  cl = sub_cl;
1694  break;
1695  }
1696  } else if (curelem->GetClassPointer() && curelem->GetClassPointer()->GetCollectionProxy()) {
1697 
1698  Int_t coll_offset = 0;
1699  ((TStreamerInfo*)cl->GetStreamerInfo())->GetStreamerElement(curelem->GetName(),coll_offset);
1700 
1701  TClass *sub_cl =
1702  curelem->GetClassPointer()->GetCollectionProxy()->GetValueClass();
1703  if (sub_cl) {
1704  element = ((TStreamerInfo*)sub_cl->GetStreamerInfo())->GetStreamerElement(work,offset);
1705  }
1706  if (element) {
1707  if (numberOfVarDim>1) {
1708  Warning("DefinedVariable","TTreeFormula support only 2 level of variables size collections. Assuming '@' notation for the collection %s.",
1709  curelem->GetName());
1710  leafinfo = new TFormLeafInfo(cl,coll_offset,curelem);
1711  useCollectionObject = kTRUE;
1712  } else if (numberOfVarDim==1) {
1713  R__ASSERT(maininfo);
1714  leafinfo =
1715  new TFormLeafInfoMultiVarDimCollection(cl,coll_offset,
1716  curelem,maininfo);
1717  fHasMultipleVarDim[code] = kTRUE;
1718  leafinfo->fNext = new TFormLeafInfoCollection(cl,coll_offset,curelem);
1719  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,kFALSE);
1720  } else {
1721  leafinfo = new TFormLeafInfoCollection(cl,coll_offset,curelem);
1722  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,kFALSE);
1723  }
1724  if (maininfo==0) maininfo = leafinfo;
1725  if (previnfo==0) previnfo = leafinfo;
1726  else {
1727  previnfo->fNext = leafinfo;
1728  previnfo = leafinfo;
1729  }
1730  if (leafinfo->fNext) {
1731  previnfo = leafinfo->fNext;
1732  }
1733  leafinfo = 0;
1734  cl = sub_cl;
1735  break;
1736  }
1737  }
1738  }
1739 
1740  }
1741 
1742  if (element) {
1743  Int_t type = element->GetNewType();
1744  if (type<60 && type!=0) {
1745  // This is a basic type ...
1746  if (numberOfVarDim>=1 && type>40) {
1747  // We have a variable array within a variable array!
1748  leafinfo = new TFormLeafInfoMultiVarDim(cl,offset,element,maininfo);
1749  fHasMultipleVarDim[code] = kTRUE;
1750  } else {
1751  if (leafinfo && type<=40 ) {
1752  leafinfo->AddOffset(offset,element);
1753  } else {
1754  leafinfo = new TFormLeafInfo(cl,offset,element);
1755  }
1756  }
1757  } else {
1758  Bool_t object = kFALSE;
1759  Bool_t pointer = kFALSE;
1760  Bool_t objarr = kFALSE;
1761  switch(type) {
1764  case TStreamerInfo::kSTLp:
1765  case TStreamerInfo::kAnyp:
1766  case TStreamerInfo::kAnyP:
1772  pointer = kTRUE;
1773  break;
1774  case TStreamerInfo::kBase:
1775  case TStreamerInfo::kAny :
1776  case TStreamerInfo::kSTL:
1781  object = kTRUE;
1782  break;
1786  objarr = kTRUE;
1787  break;
1790  // Unsupported case.
1791  Error("DefinedVariable",
1792  "%s is a datamember of %s BUT is not yet of a supported type (%d)",
1793  right,cl ? cl->GetName() : "unknown class",type);
1794  return -2;
1795  default:
1796  // Unknown and Unsupported case.
1797  Error("DefinedVariable",
1798  "%s is a datamember of %s BUT is not of a unknown type (%d)",
1799  right,cl ? cl->GetName() : "unknown class",type);
1800  return -2;
1801  }
1802 
1803  if (object && !useCollectionObject &&
1804  ( element->GetClassPointer() == TClonesArray::Class()
1805  || element->GetClassPointer()->GetCollectionProxy() ) )
1806  {
1807  object = kFALSE;
1808  }
1809  if (object && leafinfo) {
1810  leafinfo->AddOffset(offset,element);
1811  } else if (objarr) {
1812  // This is an embedded array of objects. We can not increase the offset.
1813  leafinfo = new TFormLeafInfo(cl,offset,element);
1814  mustderef = kTRUE;
1815  } else {
1816 
1817  if (!useCollectionObject && element->GetClassPointer() == TClonesArray::Class()) {
1818 
1819  leafinfo = new TFormLeafInfoClones(cl,offset,element);
1820  mustderef = kTRUE;
1821 
1822  } else if (!useCollectionObject && element->GetClassPointer()
1823  && element->GetClassPointer()->GetCollectionProxy()) {
1824 
1825  mustderef = kTRUE;
1826  if (numberOfVarDim>1) {
1827  Warning("DefinedVariable","TTreeFormula support only 2 level of variables size collections. Assuming '@' notation for the collection %s.",
1828  element->GetName());
1829  leafinfo = new TFormLeafInfo(cl,offset,element);
1830  useCollectionObject = kTRUE;
1831  } else if (numberOfVarDim==1) {
1832  R__ASSERT(maininfo);
1833  leafinfo =
1834  new TFormLeafInfoMultiVarDimCollection(cl,offset,element,maininfo);
1835 
1836  fHasMultipleVarDim[code] = kTRUE;
1837  //numberOfVarDim += RegisterDimensions(code,leafinfo);
1838  //cl = cl->GetCollectionProxy()->GetValueClass();
1839 
1840  //if (maininfo==0) maininfo = leafinfo;
1841  //if (previnfo==0) previnfo = leafinfo;
1842  //else {
1843  // previnfo->fNext = leafinfo;
1844  // previnfo = leafinfo;
1845  //}
1846  leafinfo->fNext = new TFormLeafInfoCollection(cl, offset, element);
1847  if (element->GetClassPointer()->GetCollectionProxy()->GetValueClass()==0) {
1849  element->GetClassPointer()->GetCollectionProxy());
1850  if (leafinfo->fNext) leafinfo->fNext->fNext = info;
1851  else leafinfo->fNext = info;
1852  }
1853  } else {
1854  leafinfo = new TFormLeafInfoCollection(cl, offset, element);
1855 
1856  TClass *elemCl = element->GetClassPointer();
1857  TClass *valueCl = elemCl->GetCollectionProxy()->GetValueClass();
1858  if (!maininfo) maininfo = leafinfo;
1859 
1860  if (valueCl!=0 && valueCl->GetCollectionProxy()!=0) {
1861 
1862  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,kFALSE);
1863  if (previnfo==0) previnfo = leafinfo;
1864  else {
1865  previnfo->fNext = leafinfo;
1866  previnfo = leafinfo;
1867  }
1868  leafinfo = new TFormLeafInfoMultiVarDimCollection(elemCl,0,
1869  elemCl->GetCollectionProxy()->GetValueClass(),maininfo);
1870  //numberOfVarDim += RegisterDimensions(code,previnfo->fNext);
1871  fHasMultipleVarDim[code] = kTRUE;
1872  //previnfo = previnfo->fNext;
1873  leafinfo->fNext = new TFormLeafInfoCollection(elemCl,0,
1874  valueCl);
1875  elemCl = valueCl;
1876  }
1877  if (elemCl->GetCollectionProxy() &&
1878  elemCl->GetCollectionProxy()->GetValueClass()==0) {
1880  if (leafinfo->fNext) leafinfo->fNext->fNext = info;
1881  else leafinfo->fNext = info;
1882  }
1883  }
1884  } else if ( (object || pointer) && !useReferenceObject && element->GetClassPointer()->GetReferenceProxy() ) {
1885  TClass* c = element->GetClassPointer();
1886  R__LoadBranch(leaf->GetBranch(),readentry,fQuickLoad);
1887  if ( object ) {
1888  leafinfo = new TFormLeafInfoReference(c, element, offset);
1889  }
1890  else {
1891  leafinfo = new TFormLeafInfoPointer(cl,offset,element);
1892  leafinfo->fNext = new TFormLeafInfoReference(c, element, 0);
1893  }
1894  //if ( c->GetReferenceProxy()->HasCounter() ) {
1895  // numberOfVarDim += RegisterDimensions(code,-1);
1896  //}
1897  prevUseReferenceObject = kFALSE;
1898  needClass = kFALSE;
1899  mustderef = kTRUE;
1900  } else if (pointer) {
1901  // this is a pointer to be followed.
1902  leafinfo = new TFormLeafInfoPointer(cl,offset,element);
1903  mustderef = kTRUE;
1904  } else {
1905  // this is an embedded object.
1906  R__ASSERT(object);
1907  leafinfo = new TFormLeafInfo(cl,offset,element);
1908  }
1909  }
1910  }
1911  } else {
1912  if (cl) Error("DefinedVariable","%s is not a datamember of %s",work,cl->GetName());
1913  // no else, we warned earlier that the class was missing.
1914  return -1;
1915  }
1916 
1917  numberOfVarDim += RegisterDimensions(code,leafinfo,maininfo,useCollectionObject); // Note or useCollectionObject||prevUseColectionObject
1918  if (maininfo==0) {
1919  maininfo = leafinfo;
1920  }
1921  if (previnfo==0) {
1922  previnfo = leafinfo;
1923  } else if (previnfo!=leafinfo) {
1924  previnfo->fNext = leafinfo;
1925  previnfo = leafinfo;
1926  }
1927  while (previnfo->fNext) previnfo = previnfo->fNext;
1928 
1929  if ( right[i] != '\0' ) {
1930  if ( !needClass && mustderef ) {
1931  maininfo->SetBranch(leaf->GetBranch());
1932  char *ptr = (char*)maininfo->GetValuePointer(leaf,0);
1933  TFormLeafInfoReference* refInfo = 0;
1934  if ( !maininfo->IsReference() ) {
1935  for( TFormLeafInfo* inf = maininfo->fNext; inf; inf = inf->fNext ) {
1936  if ( inf->IsReference() ) {
1937  refInfo = (TFormLeafInfoReference*)inf;
1938  }
1939  }
1940  }
1941  else {
1942  refInfo = (TFormLeafInfoReference*)maininfo;
1943  }
1944  if ( refInfo ) {
1945  cl = refInfo->GetValueClass(ptr);
1946  if ( !cl ) {
1947  Error("DefinedVariable","Failed to access class type of reference target (%s)",element->GetName());
1948  return -1;
1949  }
1950  element = ((TStreamerInfo*)cl->GetStreamerInfo())->GetStreamerElement(work,offset);
1951  }
1952  else {
1953  Error("DefinedVariable","Failed to access class type of reference target (%s)",element->GetName());
1954  return -1;
1955  }
1956  }
1957  else if ( needClass ) {
1958  cl = element->GetClassPointer();
1959  }
1960  }
1961  if (mustderef) leafinfo = 0;
1962  current = &(work[0]);
1963  *current = 0;
1964  R__ASSERT(right[i] != '['); // We are supposed to have removed all dimensions already!
1965 
1966  if (cl == TString::Class() && strcmp(right+i+1,"fData") == 0) {
1967  // For backward compatibility replace TString::fData which no longer exist
1968  // by a call to TString::Data()
1969  right = ".Data()";
1970  i = 0;
1971  nchname = strlen(right);
1972  }
1973 
1974  } else
1975  *current++ = right[i];
1976  }
1977  if (maininfo) {
1978  fDataMembers.AddAtAndExpand(maininfo,code);
1979  if (leaf) fLookupType[code] = kDataMember;
1980  else fLookupType[code] = kTreeMember;
1981  }
1982  }
1983 
1984  if (strlen(work)!=0) {
1985  // We have something left to analyze. Let's make this an error case!
1986  return -1;
1987  }
1988 
1989  TClass *objClass = EvalClass(code);
1990  if (objClass && !useLeafCollectionObject && objClass->GetCollectionProxy() && objClass->GetCollectionProxy()->GetValueClass()) {
1991  TFormLeafInfo *last = 0;
1992  if ( SwitchToFormLeafInfo(code) ) {
1993 
1994  last = (TFormLeafInfo*)fDataMembers.At(code);
1995 
1996  if (!last) return action;
1997  while (last->fNext) { last = last->fNext; }
1998 
1999  }
2000  if (last && last->GetClass() != objClass) {
2001  TClass *mother_cl;
2002  if (leaf->IsA()==TLeafObject::Class()) {
2003  // in this case mother_cl is not really used
2004  mother_cl = cl;
2005  } else {
2006  mother_cl = ((TBranchElement*)branch)->GetInfo()->GetClass();
2007  }
2008 
2009  TFormLeafInfo* collectioninfo = new TFormLeafInfoCollection(mother_cl, 0, objClass, kFALSE);
2010  // The dimension needs to be handled!
2011  numberOfVarDim += RegisterDimensions(code,collectioninfo,maininfo,kFALSE);
2012  last->fNext = collectioninfo;
2013  }
2014  numberOfVarDim += RegisterDimensions(code,1); //NOTE: changed from 0
2015  objClass = objClass->GetCollectionProxy()->GetValueClass();
2016  }
2017  if (IsLeafString(code) || objClass == TString::Class() || objClass == stdStringClass) {
2018 
2019  TFormLeafInfo *last = 0;
2020  if ( SwitchToFormLeafInfo(code) ) {
2021 
2022  last = (TFormLeafInfo*)fDataMembers.At(code);
2023 
2024  if (!last) return action;
2025  while (last->fNext) { last = last->fNext; }
2026 
2027  }
2028  const char *funcname = 0;
2029  if (objClass == TString::Class()) {
2030  funcname = "Data";
2031  //tobetested: numberOfVarDim += RegisterDimensions(code,1,0); // Register the dim of the implied char*
2032  } else if (objClass == stdStringClass) {
2033  funcname = "c_str";
2034  //tobetested: numberOfVarDim += RegisterDimensions(code,1,0); // Register the dim of the implied char*
2035  }
2036  if (funcname) {
2037  TMethodCall *method = new TMethodCall(objClass, funcname, "");
2038  if (last) {
2039  last->fNext = new TFormLeafInfoMethod(objClass,method);
2040  } else {
2041  fDataMembers.AddAtAndExpand(new TFormLeafInfoMethod(objClass,method),code);
2042  if (leaf) fLookupType[code] = kDataMember;
2043  else fLookupType[code] = kTreeMember;
2044  }
2045  }
2046  return kDefinedString;
2047  }
2048 
2049  if (objClass) {
2050  TMethodCall *method = new TMethodCall(objClass, "AsDouble", "");
2051  if (method->IsValid()
2052  && (method->ReturnType() == TMethodCall::kLong || method->ReturnType() == TMethodCall::kDouble)) {
2053 
2054  TFormLeafInfo *last = 0;
2055  if (SwitchToFormLeafInfo(code)) {
2056  last = (TFormLeafInfo*)fDataMembers.At(code);
2057  // Improbable case
2058  if (!last) {
2059  delete method;
2060  return action;
2061  }
2062  while (last->fNext) { last = last->fNext; }
2063  }
2064  if (last) {
2065  last->fNext = new TFormLeafInfoMethod(objClass,method);
2066  } else {
2067  fDataMembers.AddAtAndExpand(new TFormLeafInfoMethod(objClass,method),code);
2068  if (leaf) fLookupType[code] = kDataMember;
2069  else fLookupType[code] = kTreeMember;
2070  }
2071 
2072  return kDefinedVariable;
2073  }
2074  delete method;
2075  method = new TMethodCall(objClass, "AsString", "");
2076  if (method->IsValid()
2077  && method->ReturnType() == TMethodCall::kString) {
2078 
2079  TFormLeafInfo *last = 0;
2080  if (SwitchToFormLeafInfo(code)) {
2081  last = (TFormLeafInfo*)fDataMembers.At(code);
2082  // Improbable case
2083  if (!last) {
2084  delete method;
2085  return action;
2086  }
2087  while (last->fNext) { last = last->fNext; }
2088  }
2089  if (last) {
2090  last->fNext = new TFormLeafInfoMethod(objClass,method);
2091  } else {
2092  fDataMembers.AddAtAndExpand(new TFormLeafInfoMethod(objClass,method),code);
2093  if (leaf) fLookupType[code] = kDataMember;
2094  else fLookupType[code] = kTreeMember;
2095  }
2096 
2097  //tobetested: numberOfVarDim += RegisterDimensions(code,1,0); // Register the dim of the implied char*
2098  return kDefinedString;
2099  }
2100  if (method->IsValid()
2101  && method->ReturnType() == TMethodCall::kOther) {
2102 
2103  TClass *rcl = 0;
2104  TFunction *f = method->GetMethod();
2105  if (f) rcl = TClass::GetClass(gInterpreter->TypeName(f->GetReturnTypeName()));
2106  if ((rcl == TString::Class() || rcl == stdStringClass) ) {
2107 
2108  TFormLeafInfo *last = 0;
2109  if (SwitchToFormLeafInfo(code)) {
2110  last = (TFormLeafInfo*)fDataMembers.At(code);
2111  // Improbable case
2112  if (!last) {
2113  delete method;
2114  return action;
2115  }
2116  while (last->fNext) { last = last->fNext; }
2117  }
2118  if (last) {
2119  last->fNext = new TFormLeafInfoMethod(objClass,method);
2120  last = last->fNext;
2121  } else {
2122  last = new TFormLeafInfoMethod(objClass,method);
2123  fDataMembers.AddAtAndExpand(last,code);
2124  if (leaf) fLookupType[code] = kDataMember;
2125  else fLookupType[code] = kTreeMember;
2126  }
2127 
2128  objClass = rcl;
2129 
2130  const char *funcname = 0;
2131  if (objClass == TString::Class()) {
2132  funcname = "Data";
2133  } else if (objClass == stdStringClass) {
2134  funcname = "c_str";
2135  }
2136  if (funcname) {
2137  method = new TMethodCall(objClass, funcname, "");
2138  last->fNext = new TFormLeafInfoMethod(objClass,method);
2139  }
2140  return kDefinedString;
2141  }
2142  }
2143  delete method;
2144  }
2145 
2146  return action;
2147 }
2148 
2149 ////////////////////////////////////////////////////////////////////////////////
2150 /// Look for the leaf corresponding to the start of expression.
2151 /// It returns the corresponding leaf if any.
2152 /// It also modify the following arguments:
2153 ///
2154 /// - leftover: contain from expression that was not used to determine the leaf
2155 /// - final:
2156 /// * paran_level: number of un-matched open parenthesis
2157 /// * cast_queue: list of cast to be done
2158 /// * aliases: list of aliases used
2159 /// - Return <0 in case of failure
2160 ///
2161 /// - Return 0 if a leaf has been found
2162 /// - Return 2 if info about the TTree itself has been requested.
2163 
2164 Int_t TTreeFormula::FindLeafForExpression(const char* expression, TLeaf*& leaf, TString& leftover, Bool_t& final, UInt_t& paran_level, TObjArray& castqueue, std::vector<std::string>& aliasUsed, Bool_t& useLeafCollectionObject, const char* fullExpression)
2165 {
2166  // Later on we will need to read one entry, let's make sure
2167  // it is a real entry.
2168  if (fTree->GetTree()==0) {
2169  fTree->LoadTree(0);
2170  if (fTree->GetTree()==0) return -1;
2171  }
2172  Long64_t readentry = fTree->GetTree()->GetReadEntry();
2173  if (readentry < 0) readentry=0;
2174  const char *cname = expression;
2175  char first[kMaxLen]; first[0] = '\0';
2176  char second[kMaxLen]; second[0] = '\0';
2177  char right[kMaxLen]; right[0] = '\0';
2178  char work[kMaxLen]; work[0] = '\0';
2179  char left[kMaxLen]; left[0] = '\0';
2180  char scratch[kMaxLen];
2181  char scratch2[kMaxLen];
2182  std::string currentname;
2183  Int_t previousdot = 0;
2184  char *current;
2185  TLeaf *tmp_leaf=0;
2186  TBranch *branch=0, *tmp_branch=0;
2187  Int_t nchname = strlen(cname);
2188  Int_t i;
2189  Bool_t foundAtSign = kFALSE;
2190  Bool_t startWithParan = kFALSE;
2191 
2192  for (i=0, current = &(work[0]); i<=nchname && !final;i++ ) {
2193  // We will treated the terminator as a token.
2194  *current++ = cname[i];
2195 
2196  if (cname[i] == '(') {
2197  ++paran_level;
2198 
2199  if (current==work+1) {
2200  // If the expression starts with a paranthesis, we are likely
2201  // to have a cast operator inside.
2202  startWithParan = kTRUE;
2203  current--;
2204  }
2205  continue;
2206  //i++;
2207  //while( cname[i]!=')' && cname[i] ) {
2208  // *current++ = cname[i++];
2209  //}
2210  //*current++ = cname[i];
2211  ////*current = 0;
2212  //continue;
2213  }
2214  if (cname[i] == ')') {
2215  if (paran_level==0) {
2216  Error("DefinedVariable","Unmatched paranthesis in %s",fullExpression);
2217  return -1;
2218  }
2219  paran_level--;
2220 
2221  if (startWithParan) {
2222  startWithParan = kFALSE; // the next match wont be against the starting paranthesis.
2223 
2224  // Let's see if work is a classname and thus we have a cast.
2225  *(--current) = 0;
2226  TString cast_name = gInterpreter->TypeName(work);
2227  TClass *cast_cl = TClass::GetClass(cast_name);
2228  if (cast_cl) {
2229  // We must have a cast
2230  castqueue.AddAtAndExpand(cast_cl,paran_level);
2231  current = &(work[0]);
2232  *current = 0;
2233  // Warning("DefinedVariable","Found cast to %s",cast_fullExpression);
2234  continue;
2235  } else if (gROOT->GetType(cast_name)) {
2236  // We reset work
2237  current = &(work[0]);
2238  *current = 0;
2239  Warning("DefinedVariable",
2240  "Casting to primary types like \"%s\" is not supported yet",cast_name.Data());
2241  continue;
2242  }
2243  *(current++)=')';
2244  }
2245 
2246  *current='\0';
2247  char *params = strchr(work,'(');
2248  if (params) {
2249  *params = 0; params++;
2250 
2251  if (branch && !leaf) {
2252  // We have a branch but not a leaf. We are likely to have found
2253  // the top of split branch.
2254  if (BranchHasMethod(0, branch, work, params, readentry)) {
2255  //fprintf(stderr, "Does have a method %s for %s.\n", work, branch->GetName());
2256  }
2257  }
2258 
2259  // What we have so far might be a member function of one of the
2260  // leaves that are not split (for example "GetNtrack" for the Event class).
2261  TIter next(fTree->GetIteratorOnAllLeaves());
2262  TLeaf* leafcur = 0;
2263  while (!leaf && (leafcur = (TLeaf*) next())) {
2264  TBranch* br = leafcur->GetBranch();
2265  Bool_t yes = BranchHasMethod(leafcur, br, work, params, readentry);
2266  if (yes) {
2267  leaf = leafcur;
2268  //fprintf(stderr, "Does have a method %s for %s found in leafcur %s.\n", work, leafcur->GetBranch()->GetName(), leafcur->GetName());
2269  }
2270  }
2271  if (!leaf) {
2272  // Check for an alias.
2273  if (strlen(left) && left[strlen(left)-1]=='.') left[strlen(left)-1]=0;
2274  const char *aliasValue = fTree->GetAlias(left);
2275  if (aliasValue && strcspn(aliasValue,"+*/-%&!=<>|")==strlen(aliasValue)) {
2276  // First check whether we are using this alias recursively (this would
2277  // lead to an infinite recursion.
2278  if (find(aliasUsed.begin(),
2279  aliasUsed.end(),
2280  left) != aliasUsed.end()) {
2281  Error("DefinedVariable",
2282  "The substitution of the branch alias \"%s\" by \"%s\" in \"%s\" failed\n"\
2283  "\tbecause \"%s\" is used [recursively] in its own definition!",
2284  left,aliasValue,fullExpression,left);
2285  return -3;
2286  }
2287  aliasUsed.push_back(left);
2288  TString newExpression = aliasValue;
2289  newExpression += (cname+strlen(left));
2290  Int_t res = FindLeafForExpression(newExpression, leaf, leftover, final, paran_level,
2291  castqueue, aliasUsed, useLeafCollectionObject, fullExpression);
2292  if (res<0) {
2293  Error("DefinedVariable",
2294  "The substitution of the alias \"%s\" by \"%s\" failed.",left,aliasValue);
2295  return -3;
2296  }
2297  return res;
2298  }
2299 
2300  // This is actually not really any error, we probably received something
2301  // like "abs(some_val)", let ROOT::v5::TFormula decompose it first.
2302  return -1;
2303  }
2304  // if (!leaf->InheritsFrom(TLeafObject::Class()) ) {
2305  // If the leaf that we found so far is not a TLeafObject then there is
2306  // nothing we would be able to do.
2307  // Error("DefinedVariable","Need a TLeafObject to call a function!");
2308  // return -1;
2309  //}
2310  // We need to recover the info not used.
2311  strlcpy(right,work,kMaxLen);
2312  strncat(right,"(",kMaxLen-1-strlen(right));
2313  strncat(right,params,kMaxLen-1-strlen(right));
2314  final = kTRUE;
2315 
2316  // Record in 'i' what we consumed
2317  i += strlen(params);
2318 
2319  // we reset work
2320  current = &(work[0]);
2321  *current = 0;
2322  break;
2323  }
2324  }
2325  if (cname[i] == '.' || cname[i] == '\0' || cname[i] == ')') {
2326  // A delimiter happened let's see if what we have seen
2327  // so far does point to a leaf.
2328  *current = '\0';
2329 
2330  Int_t len = strlen(work);
2331  if (work[0]=='@') {
2332  foundAtSign = kTRUE;
2333  Int_t l = 0;
2334  for(l=0;work[l+1]!=0;++l) work[l] = work[l+1];
2335  work[l] = '\0';
2336  --current;
2337  } else if (len>=2 && work[len-2]=='@') {
2338  foundAtSign = kTRUE;
2339  work[len-2] = cname[i];
2340  work[len-1] = '\0';
2341  --current;
2342  } else {
2343  foundAtSign = kFALSE;
2344  }
2345 
2346  if (left[0]==0) strlcpy(left,work,kMaxLen);
2347  if (!leaf && !branch) {
2348  // So far, we have not found a matching leaf or branch.
2349  strlcpy(first,work,kMaxLen);
2350 
2351  std::string treename(first);
2352  if (treename.size() && treename[treename.size()-1]=='.') {
2353  treename.erase(treename.size()-1);
2354  }
2355  if (treename== "This" /* || treename == fTree->GetName() */ ) {
2356  // Request info about the TTree object itself,
2357  TNamed *named = new TNamed(fTree->GetName(),fTree->GetName());
2358  fLeafNames.AddAtAndExpand(named,fNcodes);
2359  fLeaves.AddAtAndExpand(fTree,fNcodes);
2360  if (cname[i]) leftover = &(cname[i+1]);
2361  return 2;
2362  }
2363  // The following would allow to access the friend by name
2364  // however, it would also prevent the access of the leaves
2365  // within the friend. We could use the '@' notation here
2366  // however this would not be aesthetically pleasing :(
2367  // What we need to do, is add the ability to look ahead to
2368  // the next 'token' to decide whether we to access the tree
2369  // or its leaf.
2370  //} else {
2371  // TTree *tfriend = fTree->GetFriend(treename.c_str());
2372  // TTree *realtree = fTree->GetTree();
2373  // if (!tfriend && realtree != fTree){
2374  // // If it is a chain and we did not find a friend,
2375  // // let's try with the internal tree.
2376  // tfriend = realtree->GetFriend(treename.c_str());
2377  // }
2378  // if (tfriend) {
2379  // TNamed *named = new TNamed(treename.c_str(),tfriend->GetName());
2380  // fLeafNames.AddAtAndExpand(named,fNcodes);
2381  // fLeaves.AddAtAndExpand(tfriend,fNcodes);
2382  // if (cname[i]) leftover = &(cname[i+1]);
2383  // return 2;
2384  // }
2385  //}
2386 
2387  branch = fTree->FindBranch(first);
2388  leaf = fTree->FindLeaf(first);
2389 
2390  // Now look with the delimiter removed (we looked with it first
2391  // because a dot is allowed at the end of some branches).
2392  if (cname[i]) first[strlen(first)-1]='\0';
2393  if (!branch) branch = fTree->FindBranch(first);
2394  if (!leaf) leaf = fTree->FindLeaf(first);
2395  TClass* cl = 0;
2396  if ( branch && branch->InheritsFrom(TBranchElement::Class()) ) {
2397  int offset=0;
2398  TBranchElement* bElt = (TBranchElement*)branch;
2399  TStreamerInfo* info = bElt->GetInfo();
2400  TStreamerElement* element = info ? info->GetStreamerElement(first,offset) : 0;
2401  if (element) cl = element->GetClassPointer();
2402  if ( cl && !cl->GetReferenceProxy() ) cl = 0;
2403  }
2404  if ( cl ) { // We have a reference class here....
2405  final = kTRUE;
2406  useLeafCollectionObject = foundAtSign;
2407  // we reset work
2408  current = &(work[0]);
2409  *current = 0;
2410  }
2411  else if (branch && (foundAtSign || cname[i] != 0) ) {
2412  // Since we found a branch and there is more information in the name,
2413  // we do NOT look at the 'IsOnTerminalBranch' status of the leaf
2414  // we found ... yet!
2415 
2416  if (leaf==0) {
2417  // Note we do not know (yet?) what (if anything) to do
2418  // for a TBranchObject branch.
2419  if (branch->InheritsFrom(TBranchElement::Class()) ) {
2420  Int_t type = ((TBranchElement*)branch)->GetType();
2421  if ( type == 3 || type ==4) {
2422  // We have a Collection branch.
2423  leaf = (TLeaf*)branch->GetListOfLeaves()->At(0);
2424  if (foundAtSign) {
2425  useLeafCollectionObject = foundAtSign;
2426  foundAtSign = kFALSE;
2427  current = &(work[0]);
2428  *current = 0;
2429  ++i;
2430  break;
2431  }
2432  }
2433  }
2434  }
2435 
2436  // we reset work
2437  useLeafCollectionObject = foundAtSign;
2438  foundAtSign = kFALSE;
2439  current = &(work[0]);
2440  *current = 0;
2441  } else if (leaf || branch) {
2442  if (leaf && branch) {
2443  // We found both a leaf and branch matching the request name
2444  // let's see which one is the proper one to use! (On annoying case
2445  // is that where the same name is repeated ( varname.varname )
2446 
2447  // We always give priority to the branch
2448  // leaf = 0;
2449  }
2450  if (leaf && leaf->IsOnTerminalBranch()) {
2451  // This is a non-object leaf, it should NOT be specified more except for
2452  // dimensions.
2453  final = kTRUE;
2454  }
2455  // we reset work
2456  current = &(work[0]);
2457  *current = 0;
2458  } else {
2459  // What we have so far might be a data member of one of the
2460  // leaves that are not split (for example "fNtrack" for the Event class.
2461  TLeaf *leafcur = GetLeafWithDatamember(first,work,readentry);
2462  if (leafcur) {
2463  leaf = leafcur;
2464  branch = leaf->GetBranch();
2465  if (leaf->IsOnTerminalBranch()) {
2466  final = kTRUE;
2467  strlcpy(right,first,kMaxLen);
2468  //We need to put the delimiter back!
2469  if (foundAtSign) strncat(right,"@",kMaxLen-1-strlen(right));
2470  if (cname[i]=='.') strncat(right,".",kMaxLen-1-strlen(right));
2471 
2472  // We reset work
2473  current = &(work[0]);
2474  *current = 0;
2475  };
2476  } else if (cname[i] == '.') {
2477  // If we have a branch that match a name preceded by a dot
2478  // then we assume we are trying to drill down the branch
2479  // Let look if one of the top level branch has a branch with the name
2480  // we are looking for.
2481  TBranch *branchcur;
2482  TIter next( fTree->GetListOfBranches() );
2483  while(!branch && (branchcur=(TBranch*)next()) ) {
2484  branch = branchcur->FindBranch(first);
2485  }
2486  if (branch) {
2487  // We reset work
2488  current = &(work[0]);
2489  *current = 0;
2490  }
2491  }
2492  }
2493  } else { // correspond to if (leaf || branch)
2494  if (final) {
2495  Error("DefinedVariable", "Unexpected control flow!");
2496  return -1;
2497  }
2498 
2499  // No dot is allowed in subbranches and leaves, so
2500  // we always remove it in the present case.
2501  if (cname[i]) work[strlen(work)-1] = '\0';
2502  snprintf(scratch,sizeof(scratch),"%s.%s",first,work);
2503  snprintf(scratch2,sizeof(scratch2),"%s.%s.%s",first,second,work);
2504 
2505  if (previousdot) {
2506  currentname = &(work[previousdot+1]);
2507  }
2508 
2509  // First look for the current 'word' in the list of
2510  // leaf of the
2511  if (branch) {
2512  tmp_leaf = branch->FindLeaf(work);
2513  if (!tmp_leaf) tmp_leaf = branch->FindLeaf(scratch);
2514  if (!tmp_leaf) tmp_leaf = branch->FindLeaf(scratch2);
2515  if (!tmp_leaf) tmp_leaf = branch->FindLeaf(currentname.c_str());
2516  }
2517  if (tmp_leaf && tmp_leaf->IsOnTerminalBranch() ) {
2518  // This is a non-object leaf, it should NOT be specified more except for
2519  // dimensions.
2520  final = kTRUE;
2521  }
2522 
2523  if (branch) {
2524  tmp_branch = branch->FindBranch(work);
2525  if (!tmp_branch) tmp_branch = branch->FindBranch(scratch);
2526  if (!tmp_branch) tmp_branch = branch->FindBranch(scratch2);
2527  if (!tmp_branch) tmp_branch = branch->FindBranch(currentname.c_str());
2528  }
2529  if (tmp_branch) {
2530  branch=tmp_branch;
2531 
2532  // NOTE: Should we look for a leaf within here?
2533  if (!final) {
2534  tmp_leaf = branch->FindLeaf(work);
2535  if (!tmp_leaf) tmp_leaf = branch->FindLeaf(scratch);
2536  if (!tmp_leaf) tmp_leaf = branch->FindLeaf(scratch2);
2537  if (!tmp_leaf) tmp_leaf = branch->FindLeaf(currentname.c_str());
2538  if (tmp_leaf && tmp_leaf->IsOnTerminalBranch() ) {
2539  // This is a non-object leaf, it should NOT be specified
2540  // more except for dimensions.
2541  final = kTRUE;
2542  leaf = tmp_leaf;
2543  }
2544  }
2545  }
2546  if (tmp_leaf) {
2547  // Something was found.
2548  if (second[0]) strncat(second,".",kMaxLen-1-strlen(second));
2549  strncat(second,work,kMaxLen-1-strlen(second));
2550  leaf = tmp_leaf;
2551  useLeafCollectionObject = foundAtSign;
2552  foundAtSign = kFALSE;
2553 
2554  // we reset work
2555  current = &(work[0]);
2556  *current = 0;
2557  } else {
2558  //We need to put the delimiter back!
2559  if (strlen(work)) {
2560  if (foundAtSign) {
2561  Int_t where = strlen(work);
2562  work[where] = '@';
2563  work[where+1] = cname[i];
2564  ++current;
2565  previousdot = where+1;
2566  } else {
2567  previousdot = strlen(work);
2568  work[strlen(work)] = cname[i];
2569  }
2570  } else --current;
2571  }
2572  }
2573  }
2574  }
2575 
2576  // Copy the left over for later use.
2577  if (strlen(work)) {
2578  strncat(right,work,kMaxLen-1-strlen(right));
2579  }
2580 
2581  if (i<nchname) {
2582  if (strlen(right) && right[strlen(right)-1]!='.' && cname[i]!='.') {
2583  // In some cases we remove a little to fast the period, we add
2584  // it back if we need. It is assumed that 'right' and the rest of
2585  // the name was cut by a delimiter, so this should be safe.
2586  strncat(right,".",kMaxLen-1-strlen(right));
2587  }
2588  strncat(right,&cname[i],kMaxLen-1-strlen(right));
2589  }
2590 
2591  if (!final && branch) {
2592  if (!leaf) {
2593  leaf = (TLeaf*)branch->GetListOfLeaves()->UncheckedAt(0);
2594  if (!leaf) return -1;
2595  }
2596  final = leaf->IsOnTerminalBranch();
2597  }
2598 
2599  if (leaf && leaf->InheritsFrom(TLeafObject::Class()) ) {
2600  if (strlen(right)==0) strlcpy(right,work,kMaxLen);
2601  }
2602 
2603  if (leaf==0 && left[0]!=0) {
2604  if (left[strlen(left)-1]=='.') left[strlen(left)-1]=0;
2605 
2606  // Check for an alias.
2607  const char *aliasValue = fTree->GetAlias(left);
2608  if (aliasValue && strcspn(aliasValue,"[]+*/-%&!=<>|")==strlen(aliasValue)) {
2609  // First check whether we are using this alias recursively (this would
2610  // lead to an infinite recursion).
2611  if (find(aliasUsed.begin(),
2612  aliasUsed.end(),
2613  left) != aliasUsed.end()) {
2614  Error("DefinedVariable",
2615  "The substitution of the branch alias \"%s\" by \"%s\" in \"%s\" failed\n"\
2616  "\tbecause \"%s\" is used [recursively] in its own definition!",
2617  left,aliasValue,fullExpression,left);
2618  return -3;
2619  }
2620  aliasUsed.push_back(left);
2621  TString newExpression = aliasValue;
2622  newExpression += (cname+strlen(left));
2623  Int_t res = FindLeafForExpression(newExpression, leaf, leftover, final, paran_level,
2624  castqueue, aliasUsed, useLeafCollectionObject, fullExpression);
2625  if (res<0) {
2626  Error("DefinedVariable",
2627  "The substitution of the alias \"%s\" by \"%s\" failed.",left,aliasValue);
2628  return -3;
2629  }
2630  return res;
2631  }
2632  }
2633  leftover = right;
2634 
2635  return 0;
2636 }
2637 
2638 ////////////////////////////////////////////////////////////////////////////////
2639 /// Check if name is in the list of Tree/Branch leaves.
2640 ///
2641 /// This member function redefines the function in ROOT::v5::TFormula
2642 /// If a leaf has a name corresponding to the argument name, then
2643 /// returns a new code.
2644 ///
2645 /// A TTreeFormula may contain more than one variable.
2646 /// For each variable referenced, the pointers to the corresponding
2647 /// branch and leaf is stored in the object arrays fBranches and fLeaves.
2648 ///
2649 /// name can be :
2650 /// - Leaf_Name (simple variable or data member of a ClonesArray)
2651 /// - Branch_Name.Leaf_Name
2652 /// - Branch_Name.Method_Name
2653 /// - Leaf_Name[index]
2654 /// - Branch_Name.Leaf_Name[index]
2655 /// - Branch_Name.Leaf_Name[index1]
2656 /// - Branch_Name.Leaf_Name[][index2]
2657 /// - Branch_Name.Leaf_Name[index1][index2]
2658 ///
2659 /// New additions:
2660 /// - Branch_Name.Leaf_Name[OtherLeaf_Name]
2661 /// - Branch_Name.Datamember_Name
2662 /// - '.' can be replaced by '->'
2663 ///
2664 /// and
2665 /// - Branch_Name[index1].Leaf_Name[index2]
2666 /// - Leaf_name[index].Action().OtherAction(param)
2667 /// - Leaf_name[index].Action()[val].OtherAction(param)
2668 ///
2669 /// The expected returns values are
2670 /// - -2 : the name has been recognized but won't be usable
2671 /// - -1 : the name has not been recognized
2672 /// - >=0 : the name has been recognized, return the internal code for this name.
2673 
2675 {
2676 
2677  action = kDefinedVariable;
2678  if (!fTree) return -1;
2679 
2680  fNpar = 0;
2681  if (name.Length() > kMaxLen) return -1;
2682  Int_t i,k;
2683 
2684  if (name == "Entry$") {
2685  Int_t code = fNcodes++;
2686  fCodes[code] = 0;
2687  fLookupType[code] = kIndexOfEntry;
2688  return code;
2689  }
2690  if (name == "LocalEntry$") {
2691  Int_t code = fNcodes++;
2692  fCodes[code] = 0;
2694  return code;
2695  }
2696  if (name == "Entries$") {
2697  Int_t code = fNcodes++;
2698  fCodes[code] = 0;
2699  fLookupType[code] = kEntries;
2702  return code;
2703  }
2704  if (name == "Iteration$") {
2705  Int_t code = fNcodes++;
2706  fCodes[code] = 0;
2707  fLookupType[code] = kIteration;
2708  return code;
2709  }
2710  if (name == "Length$") {
2711  Int_t code = fNcodes++;
2712  fCodes[code] = 0;
2713  fLookupType[code] = kLength;
2714  return code;
2715  }
2716  static const char *lenfunc = "Length$(";
2717  if (strncmp(name.Data(),"Length$(",strlen(lenfunc))==0
2718  && name[name.Length()-1]==')') {
2719 
2720  TString subform = name.Data()+strlen(lenfunc);
2721  subform.Remove( subform.Length() - 1 );
2722  TTreeFormula *lengthForm = new TTreeFormula("lengthForm",subform,fTree);
2723  fAliases.AddAtAndExpand(lengthForm,fNoper);
2724  Int_t code = fNcodes++;
2725  fCodes[code] = 0;
2726  fLookupType[code] = kLengthFunc;
2727  return code;
2728  }
2729  static const char *minfunc = "Min$(";
2730  if (strncmp(name.Data(),"Min$(",strlen(minfunc))==0
2731  && name[name.Length()-1]==')') {
2732 
2733  TString subform = name.Data()+strlen(minfunc);
2734  subform.Remove( subform.Length() - 1 );
2735  TTreeFormula *minForm = new TTreeFormula("minForm",subform,fTree);
2736  fAliases.AddAtAndExpand(minForm,fNoper);
2737  Int_t code = fNcodes++;
2738  fCodes[code] = 0;
2739  fLookupType[code] = kMin;
2740  return code;
2741  }
2742  static const char *maxfunc = "Max$(";
2743  if (strncmp(name.Data(),"Max$(",strlen(maxfunc))==0
2744  && name[name.Length()-1]==')') {
2745 
2746  TString subform = name.Data()+strlen(maxfunc);
2747  subform.Remove( subform.Length() - 1 );
2748  TTreeFormula *maxForm = new TTreeFormula("maxForm",subform,fTree);
2749  fAliases.AddAtAndExpand(maxForm,fNoper);
2750  Int_t code = fNcodes++;
2751  fCodes[code] = 0;
2752  fLookupType[code] = kMax;
2753  return code;
2754  }
2755  static const char *sumfunc = "Sum$(";
2756  if (strncmp(name.Data(),"Sum$(",strlen(sumfunc))==0
2757  && name[name.Length()-1]==')') {
2758 
2759  TString subform = name.Data()+strlen(sumfunc);
2760  subform.Remove( subform.Length() - 1 );
2761  TTreeFormula *sumForm = new TTreeFormula("sumForm",subform,fTree);
2762  fAliases.AddAtAndExpand(sumForm,fNoper);
2763  Int_t code = fNcodes++;
2764  fCodes[code] = 0;
2765  fLookupType[code] = kSum;
2766  return code;
2767  }
2768 
2769 
2770 
2771  // Check for $Alt(expression1,expression2)
2772  Int_t res = DefineAlternate(name.Data());
2773  if (res!=0) {
2774  // There was either a syntax error or we found $Alt
2775  if (res<0) return res;
2776  action = res;
2777  return 0;
2778  }
2779 
2780  // Find the top level leaf and deal with dimensions
2781 
2782  char cname[kMaxLen]; strlcpy(cname,name.Data(),kMaxLen);
2783  char dims[kMaxLen]; dims[0] = '\0';
2784 
2785  Bool_t final = kFALSE;
2786 
2787  UInt_t paran_level = 0;
2788  TObjArray castqueue;
2789 
2790  // First, it is easier to remove all dimensions information from 'cname'
2791  Int_t cnamelen = strlen(cname);
2792  for(i=0,k=0; i<cnamelen; ++i, ++k) {
2793  if (cname[i] == '[') {
2794  int bracket = i;
2795  int bracket_level = 1;
2796  int j;
2797  for (j=++i; j<cnamelen && (bracket_level>0 || cname[j]=='['); j++, i++) {
2798  if (cname[j]=='[') bracket_level++;
2799  else if (cname[j]==']') bracket_level--;
2800  }
2801  if (bracket_level != 0) {
2802  //Error("DefinedVariable","Bracket unbalanced");
2803  return -1;
2804  }
2805  strncat(dims,&cname[bracket],j-bracket);
2806  //k += j-bracket;
2807  }
2808  if (i!=k) cname[k] = cname[i];
2809  }
2810  cname[k]='\0';
2811 
2812  Bool_t useLeafCollectionObject = kFALSE;
2813  TString leftover;
2814  TLeaf *leaf = 0;
2815  {
2816  std::vector<std::string> aliasSofar = fAliasesUsed;
2817  res = FindLeafForExpression(cname, leaf, leftover, final, paran_level, castqueue, aliasSofar, useLeafCollectionObject, name);
2818  }
2819  if (res<0) return res;
2820 
2821  if (!leaf && res!=2) {
2822  // Check for an alias.
2823  const char *aliasValue = fTree->GetAlias(cname);
2824  if (aliasValue) {
2825  // First check whether we are using this alias recursively (this would
2826  // lead to an infinite recursion.
2827  if (find(fAliasesUsed.begin(),
2828  fAliasesUsed.end(),
2829  cname) != fAliasesUsed.end()) {
2830  Error("DefinedVariable",
2831  "The substitution of the alias \"%s\" by \"%s\" failed\n"\
2832  "\tbecause \"%s\" is recursively used in its own definition!",
2833  cname,aliasValue,cname);
2834  return -3;
2835  }
2836 
2837 
2838  if (strcspn(aliasValue,"+*/-%&!=<>|")!=strlen(aliasValue)) {
2839  // If the alias contains an operator, we need to use a nested formula
2840  // (since DefinedVariable must only add one entry to the operation's list).
2841 
2842  // Need to check the aliases used so far
2843  std::vector<std::string> aliasSofar = fAliasesUsed;
2844  aliasSofar.push_back( cname );
2845 
2846  TString subValue( aliasValue );
2847  if (dims[0]) {
2848  subValue += dims;
2849  }
2850 
2851  TTreeFormula *subform = new TTreeFormula(cname,subValue,fTree,aliasSofar); // Need to pass the aliases used so far.
2852 
2853  if (subform->GetNdim()==0) {
2854  delete subform;
2855  Error("DefinedVariable",
2856  "The substitution of the alias \"%s\" by \"%s\" failed.",cname,aliasValue);
2857  return -3;
2858  }
2859 
2860  fManager->Add(subform);
2861  fAliases.AddAtAndExpand(subform,fNoper);
2862 
2863  if (subform->IsString()) {
2864  action = kAliasString;
2865  return 0;
2866  } else {
2867  action = kAlias;
2868  return 0;
2869  }
2870  } else { /* assumes strcspn(aliasValue,"[]")!=strlen(aliasValue) */
2871  TString thisAlias( aliasValue );
2872  thisAlias += dims;
2873  Int_t aliasRes = DefinedVariable(thisAlias,action);
2874  if (aliasRes<0) {
2875  // We failed but DefinedVariable has not printed why yet.
2876  // and because we want thoses to be printed _before_ the notice
2877  // of the failure of the substitution, we need to print them here.
2878  if (aliasRes==-1) {
2879  Error("Compile", " Bad numerical expression : \"%s\"",thisAlias.Data());
2880  } else if (aliasRes==-2) {
2881  Error("Compile", " Part of the Variable \"%s\" exists but some of it is not accessible or useable",thisAlias.Data());
2882 
2883  }
2884  Error("DefinedVariable",
2885  "The substitution of the alias \"%s\" by \"%s\" failed.",cname,aliasValue);
2886  return -3;
2887  }
2888  return aliasRes;
2889  }
2890  }
2891  }
2892 
2893 
2894  if (leaf || res==2) {
2895 
2896  if (leaf && leaf->GetBranch() && leaf->GetBranch()->TestBit(kDoNotProcess)) {
2897  Error("DefinedVariable","the branch \"%s\" has to be enabled to be used",leaf->GetBranch()->GetName());
2898  return -2;
2899  }
2900 
2901  Int_t code = fNcodes++;
2902 
2903  // If needed will now parse the indexes specified for
2904  // arrays.
2905  if (dims[0]) {
2906  char *current = &( dims[0] );
2907  Int_t dim = 0;
2908  TString varindex;
2909  Int_t index;
2910  Int_t scanindex ;
2911  while (current) {
2912  current++;
2913  if (current[0] == ']') {
2914  fIndexes[code][dim] = -1; // Loop over all elements;
2915  } else {
2916  scanindex = sscanf(current,"%d",&index);
2917  if (scanindex) {
2918  fIndexes[code][dim] = index;
2919  } else {
2920  fIndexes[code][dim] = -2; // Index is calculated via a variable.
2921  varindex = current;
2922  char *end = (char*)(varindex.Data());
2923  for(char bracket_level = 0;*end!=0;end++) {
2924  if (*end=='[') bracket_level++;
2925  if (bracket_level==0 && *end==']') break;
2926  if (*end==']') bracket_level--;
2927  }
2928  *end = '\0';
2929  fVarIndexes[code][dim] = new TTreeFormula("index_var",
2930  varindex,
2931  fTree);
2932  current += strlen(varindex)+1; // move to the end of the index array
2933  }
2934  }
2935  dim ++;
2936  if (dim >= kMAXFORMDIM) {
2937  // NOTE: test that dim this is NOT too big!!
2938  break;
2939  }
2940  current = (char*)strstr( current, "[" );
2941  }
2942  }
2943 
2944  // Now that we have cleaned-up the expression, let's compare it to the content
2945  // of the leaf!
2946 
2947  res = ParseWithLeaf(leaf,leftover,final,paran_level,castqueue,useLeafCollectionObject,name);
2948  if (res<0) return res;
2949  if (res>0) action = res;
2950  return code;
2951  }
2952 
2953 //*-*- May be a graphical cut ?
2954  TCutG *gcut = (TCutG*)gROOT->GetListOfSpecials()->FindObject(name.Data());
2955  if (gcut) {
2956  if (gcut->GetObjectX()) {
2957  if(!gcut->GetObjectX()->InheritsFrom(TTreeFormula::Class())) {
2958  delete gcut->GetObjectX(); gcut->SetObjectX(0);
2959  }
2960  }
2961  if (gcut->GetObjectY()) {
2962  if(!gcut->GetObjectY()->InheritsFrom(TTreeFormula::Class())) {
2963  delete gcut->GetObjectY(); gcut->SetObjectY(0);
2964  }
2965  }
2966 
2967  Int_t code = fNcodes;
2968 
2969  if (strlen(gcut->GetVarX()) && strlen(gcut->GetVarY()) ) {
2970 
2971  TTreeFormula *fx = new TTreeFormula("f_x",gcut->GetVarX(),fTree);
2972  gcut->SetObjectX(fx);
2973 
2974  TTreeFormula *fy = new TTreeFormula("f_y",gcut->GetVarY(),fTree);
2975  gcut->SetObjectY(fy);
2976 
2977  fCodes[code] = -2;
2978 
2979  } else if (strlen(gcut->GetVarX())) {
2980 
2981  // Let's build the equivalent formula:
2982  // min(gcut->X) <= VarX <= max(gcut->Y)
2983  Double_t min = 0;
2984  Double_t max = 0;
2985  Int_t n = gcut->GetN();
2986  Double_t *x = gcut->GetX();
2987  min = max = x[0];
2988  for(Int_t i2 = 1; i2<n; i2++) {
2989  if (x[i2] < min) min = x[i2];
2990  if (x[i2] > max) max = x[i2];
2991  }
2992  TString formula = "(";
2993  formula += min;
2994  formula += "<=";
2995  formula += gcut->GetVarX();
2996  formula += " && ";
2997  formula += gcut->GetVarX();
2998  formula += "<=";
2999  formula += max;
3000  formula += ")";
3001 
3002  TTreeFormula *fx = new TTreeFormula("f_x",formula.Data(),fTree);
3003  gcut->SetObjectX(fx);
3004 
3005  fCodes[code] = -1;
3006 
3007  } else {
3008 
3009  Error("DefinedVariable","Found a TCutG without leaf information (%s)",
3010  gcut->GetName());
3011  return -1;
3012 
3013  }
3014 
3015  fExternalCuts.AddAtAndExpand(gcut,code);
3016  fNcodes++;
3017  fLookupType[code] = -1;
3018  return code;
3019  }
3020 
3021  //may be an entrylist
3022  TEntryList *elist = dynamic_cast<TEntryList*> (gDirectory->Get(name.Data()));
3023  if (elist) {
3024  Int_t code = fNcodes;
3025  fCodes[code] = 0;
3026  fExternalCuts.AddAtAndExpand(elist, code);
3027  fNcodes++;
3028  fLookupType[code] = kEntryList;
3029  return code;
3030 
3031  }
3032 
3033  return -1;
3034 }
3035 
3036 ////////////////////////////////////////////////////////////////////////////////
3037 /// Return the leaf (if any) which contains an object containing
3038 /// a data member which has the name provided in the arguments.
3039 
3040 TLeaf* TTreeFormula::GetLeafWithDatamember(const char* topchoice, const char* nextchoice, Long64_t readentry) const
3041 {
3042  TClass * cl = 0;
3043  TIter nextleaf (fTree->GetIteratorOnAllLeaves());
3044  TFormLeafInfo* clonesinfo = 0;
3045  TLeaf *leafcur;
3046  while ((leafcur = (TLeaf*)nextleaf())) {
3047  // The following code is used somewhere else, we need to factor it out.
3048 
3049  // Here since we are interested in data member, we want to consider only
3050  // 'terminal' branch and leaf.
3051  cl = 0;
3052  if (leafcur->InheritsFrom(TLeafObject::Class()) &&
3053  leafcur->GetBranch()->GetListOfBranches()->Last()==0) {
3054  TLeafObject *lobj = (TLeafObject*)leafcur;
3055  cl = lobj->GetClass();
3056  } else if (leafcur->InheritsFrom(TLeafElement::Class()) && leafcur->IsOnTerminalBranch()) {
3057  TLeafElement * lElem = (TLeafElement*) leafcur;
3058  if (lElem->IsOnTerminalBranch()) {
3059  TBranchElement *branchEl = (TBranchElement *)leafcur->GetBranch();
3060  Int_t type = branchEl->GetStreamerType();
3061  if (type==-1) {
3062  cl = branchEl->GetInfo() ? branchEl->GetInfo()->GetClass() : 0;
3063  } else if (type>60 || type==0) {
3064  // Case of an object data member. Here we allow for the
3065  // variable name to be ommitted. Eg, for Event.root with split
3066  // level 1 or above Draw("GetXaxis") is the same as Draw("fH.GetXaxis()")
3067  TStreamerElement* element = branchEl->GetInfo()->GetElement(branchEl->GetID());
3068  if (element) cl = element->GetClassPointer();
3069  else cl = 0;
3070  }
3071  }
3072 
3073  }
3074  if (clonesinfo) { delete clonesinfo; clonesinfo = 0; }
3075  if (cl == TClonesArray::Class()) {
3076  // We have a unsplit TClonesArray leaves
3077  // In this case we assume that cl is the class in which the TClonesArray
3078  // belongs.
3079  R__LoadBranch(leafcur->GetBranch(),readentry,fQuickLoad);
3080  TClonesArray * clones;
3081 
3082  TBranch *branch = leafcur->GetBranch();
3083  if ( branch->IsA()==TBranchElement::Class()
3084  && ((TBranchElement*)branch)->GetType()==31) {
3085 
3086  // We have an unsplit TClonesArray as part of a split TClonesArray!
3087 
3088  // Let's not dig any further. If the user really wants a data member
3089  // inside the nested TClonesArray, it has to specify it explicitly.
3090 
3091  continue;
3092 
3093  } else {
3094  Bool_t toplevel = (branch == branch->GetMother());
3095  clonesinfo = new TFormLeafInfoClones(cl, 0, toplevel);
3096  clones = (TClonesArray*)clonesinfo->GetLocalValuePointer(leafcur,0);
3097  }
3098  if (clones) cl = clones->GetClass();
3099  } else if (cl && cl->GetCollectionProxy()) {
3100 
3101  // We have a unsplit Collection leaves
3102  // In this case we assume that cl is the class in which the TClonesArray
3103  // belongs.
3104 
3105  TBranch *branch = leafcur->GetBranch();
3106  if ( branch->IsA()==TBranchElement::Class()
3107  && ((TBranchElement*)branch)->GetType()==41) {
3108 
3109  // We have an unsplit Collection as part of a split Collection!
3110 
3111  // Let's not dig any further. If the user really wants a data member
3112  // inside the nested Collection, it has to specify it explicitly.
3113 
3114  continue;
3115 
3116  } else {
3117  clonesinfo = new TFormLeafInfoCollection(cl, 0);
3118  }
3119  cl = cl->GetCollectionProxy()->GetValueClass();
3120  }
3121  if (cl) {
3122  // Now that we have the class, let's check if the topchoice is of its datamember
3123  // or if the nextchoice is a datamember of one of its datamember.
3124  Int_t offset;
3126  TStreamerElement* element = info?info->GetStreamerElement(topchoice,offset):0;
3127  if (!element) {
3128  TIter nextel( cl->GetStreamerInfo()->GetElements() );
3129  TStreamerElement * curelem;
3130  while ((curelem = (TStreamerElement*)nextel())) {
3131 
3132  if (curelem->GetClassPointer() == TClonesArray::Class()) {
3133  // In case of a TClonesArray we need to load the data and read the
3134  // clonesArray object before being able to look into the class inside.
3135  // We need to do that because we are never interested in the TClonesArray
3136  // itself but only in the object inside.
3137  TBranch *branch = leafcur->GetBranch();
3138  TFormLeafInfo *leafinfo = 0;
3139  if (clonesinfo) {
3140  leafinfo = clonesinfo;
3141  } else if (branch->IsA()==TBranchElement::Class()
3142  && ((TBranchElement*)branch)->GetType()==31) {
3143  // Case of a sub branch of a TClonesArray
3144  TBranchElement *branchEl = (TBranchElement*)branch;
3145  TStreamerInfo *bel_info = branchEl->GetInfo();
3146  TClass * mother_cl = ((TBranchElement*)branch)->GetInfo()->GetClass();
3147  TStreamerElement *bel_element =
3148  bel_info->GetElement(branchEl->GetID());
3149  leafinfo = new TFormLeafInfoClones(mother_cl, 0, bel_element, kTRUE);
3150  }
3151 
3152  Int_t clones_offset = 0;
3153  ((TStreamerInfo*)cl->GetStreamerInfo())->GetStreamerElement(curelem->GetName(),clones_offset);
3154  TFormLeafInfo* sub_clonesinfo = new TFormLeafInfo(cl, clones_offset, curelem);
3155  if (leafinfo)
3156  if (leafinfo->fNext) leafinfo->fNext->fNext = sub_clonesinfo;
3157  else leafinfo->fNext = sub_clonesinfo;
3158  else leafinfo = sub_clonesinfo;
3159 
3160  R__LoadBranch(branch,readentry,fQuickLoad);
3161 
3162  TClonesArray * clones = (TClonesArray*)leafinfo->GetValuePointer(leafcur,0);
3163 
3164  delete leafinfo; clonesinfo = 0;
3165  // If TClonesArray object does not exist we have no information, so let go
3166  // on. This is a weakish test since the TClonesArray object might exist in
3167  // the next entry ... In other word, we ONLY rely on the information available
3168  // in entry #0.
3169  if (!clones) continue;
3170  TClass *sub_cl = clones->GetClass();
3171 
3172  // Now that we finally have the inside class, let's query it.
3173  element = ((TStreamerInfo*)sub_cl->GetStreamerInfo())->GetStreamerElement(nextchoice,offset);
3174  if (element) break;
3175  } // if clones array
3176  else if (curelem->GetClassPointer() && curelem->GetClassPointer()->GetCollectionProxy()) {
3177 
3178  TClass *sub_cl = curelem->GetClassPointer()->GetCollectionProxy()->GetValueClass();
3179 
3180  while(sub_cl && sub_cl->GetCollectionProxy())
3181  sub_cl = sub_cl->GetCollectionProxy()->GetValueClass();
3182 
3183  // Now that we finally have the inside class, let's query it.
3184  if (sub_cl) element = ((TStreamerInfo*)sub_cl->GetStreamerInfo())->GetStreamerElement(nextchoice,offset);
3185  if (element) break;
3186 
3187  }
3188  } // loop on elements
3189  }
3190  if (element) break;
3191  else cl = 0;
3192  }
3193  }
3194  delete clonesinfo;
3195  if (cl) {
3196  return leafcur;
3197  } else {
3198  return 0;
3199  }
3200 }
3201 
3202 ////////////////////////////////////////////////////////////////////////////////
3203 /// Return the leaf (if any) of the tree with contains an object of a class
3204 /// having a method which has the name provided in the argument.
3205 
3206 Bool_t TTreeFormula::BranchHasMethod(TLeaf* leafcur, TBranch* branch, const char* method, const char* params, Long64_t readentry) const
3207 {
3208  TClass *cl = 0;
3209  TLeafObject* lobj = 0;
3210 
3211  // Since the user does not want this branch to be loaded anyway, we just
3212  // skip it. This prevents us from warning the user that the method might
3213  // be on a disabled branch. However, and more usefully, this allows the
3214  // user to avoid error messages from branches that cannot be currently
3215  // read without warnings/errors.
3216 
3217  if (branch->TestBit(kDoNotProcess)) {
3218  return kFALSE;
3219  }
3220 
3221  // FIXME: The following code is used somewhere else, we need to factor it out.
3222  if (branch->InheritsFrom(TBranchObject::Class())) {
3223  lobj = (TLeafObject*) branch->GetListOfLeaves()->At(0);
3224  cl = lobj->GetClass();
3225  } else if (branch->InheritsFrom(TBranchElement::Class())) {
3226  TBranchElement* branchEl = (TBranchElement*) branch;
3227  Int_t type = branchEl->GetStreamerType();
3228  if (type == -1) {
3229  cl = branchEl->GetInfo() ? branchEl->GetInfo()->GetClass() : 0;
3230  } else if (type > 60) {
3231  // Case of an object data member. Here we allow for the
3232  // variable name to be ommitted. Eg, for Event.root with split
3233  // level 1 or above Draw("GetXaxis") is the same as Draw("fH.GetXaxis()")
3234  TStreamerElement* element = branchEl->GetInfo()->GetElement(branchEl->GetID());
3235  if (element) {
3236  cl = element->GetClassPointer();
3237  } else {
3238  cl = 0;
3239  }
3240  if ((cl == TClonesArray::Class()) && (branchEl->GetType() == 31)) {
3241  // we have a TClonesArray inside a split TClonesArray,
3242  // Let's not dig any further. If the user really wants a data member
3243  // inside the nested TClonesArray, it has to specify it explicitly.
3244  cl = 0;
3245  }
3246  // NOTE do we need code for Collection here?
3247  }
3248  }
3249 
3250  if (cl == TClonesArray::Class()) {
3251  // We might be try to call a method of the top class inside a
3252  // TClonesArray.
3253  // Since the leaf was not terminal, we might have a split or
3254  // unsplit and/or top leaf/branch.
3255  TClonesArray* clones = 0;
3256  R__LoadBranch(branch, readentry, fQuickLoad);
3257  if (branch->InheritsFrom(TBranchObject::Class())) {
3258  clones = (TClonesArray*) lobj->GetObject();
3259  } else if (branch->InheritsFrom(TBranchElement::Class())) {
3260  // We do not know exactly where the leaf of the TClonesArray is
3261  // in the hierachy but we still need to get the correct class
3262  // holder.
3263  TBranchElement* bc = (TBranchElement*) branch;
3264  if (bc == bc->GetMother()) {
3265  // Top level branch
3266  //clones = *((TClonesArray**) bc->GetAddress());
3267  clones = (TClonesArray*) bc->GetObject();
3268  } else if (!leafcur || !leafcur->IsOnTerminalBranch()) {
3269  TStreamerElement* element = bc->GetInfo()->GetElement(bc->GetID());
3270  if (element->IsaPointer()) {
3271  clones = *((TClonesArray**) bc->GetAddress());
3272  //clones = *((TClonesArray**) bc->GetObject());
3273  } else {
3274  //clones = (TClonesArray*) bc->GetAddress();
3275  clones = (TClonesArray*) bc->GetObject();
3276  }
3277  }
3278  if (!clones) {
3279  R__LoadBranch(bc, readentry, fQuickLoad);
3280  TClass* mother_cl;
3281  mother_cl = bc->GetInfo()->GetClass();
3282  TFormLeafInfo* clonesinfo = new TFormLeafInfoClones(mother_cl, 0);
3283  // if (!leafcur) { leafcur = (TLeaf*) branch->GetListOfLeaves()->At(0); }
3284  clones = (TClonesArray*) clonesinfo->GetLocalValuePointer(leafcur, 0);
3285  // cl = clones->GetClass();
3286  delete clonesinfo;
3287  }
3288  } else {
3289  Error("BranchHasMethod","A TClonesArray was stored in a branch type no yet support (i.e. neither TBranchObject nor TBranchElement): %s",branch->IsA()->GetName());
3290  return kFALSE;
3291  }
3292  cl = clones ? clones->GetClass() : 0;
3293  } else if (cl && cl->GetCollectionProxy()) {
3294  cl = cl->GetCollectionProxy()->GetValueClass();
3295  }
3296 
3297  if (cl) {
3298  if (cl->GetClassInfo()) {
3299  if (cl->GetMethodAllAny(method)) {
3300  // Let's try to see if the function we found belongs to the current
3301  // class. Note that this implementation currently can not work if
3302  // one the argument is another leaf or data member of the object.
3303  // (Anyway we do NOT support this case).
3304  TMethodCall* methodcall = new TMethodCall(cl, method, params);
3305  if (methodcall->GetMethod()) {
3306  // We have a method that works.
3307  // We will use it.
3308  return kTRUE;
3309  }
3310  delete methodcall;
3311  }
3312  }
3313  }
3314 
3315  return kFALSE;
3316 }
3317 
3318 ////////////////////////////////////////////////////////////////////////////////
3319 /// Now let calculate what physical instance we really need.
3320 /// Some redundant code is used to speed up the cases where
3321 /// they are no dimensions.
3322 ///
3323 /// We know that instance is less that fCumulUsedSize[0] so
3324 /// we can skip the modulo when virt_dim is 0.
3325 
3327  Int_t real_instance = 0;
3328  Int_t virt_dim;
3329 
3330  Bool_t check = kFALSE;
3331  if (codeindex<0) {
3332  codeindex = 0;
3333  check = kTRUE;
3334  }
3335 
3336  TFormLeafInfo * info = 0;
3337  Int_t max_dim = fNdimensions[codeindex];
3338  if ( max_dim ) {
3339  virt_dim = 0;
3340  max_dim--;
3341 
3342  if (!fManager->fMultiVarDim) {
3343  if (fIndexes[codeindex][0]>=0) {
3344  real_instance = fIndexes[codeindex][0] * fCumulSizes[codeindex][1];
3345  } else {
3346  Int_t local_index;
3347  local_index = ( instance / fManager->fCumulUsedSizes[virt_dim+1]);
3348  if (fIndexes[codeindex][0]==-2) {
3349  // NOTE: Should we check that this is a valid index?
3350  if (check) {
3351  Int_t index_real_instance = fVarIndexes[codeindex][0]->GetRealInstance(local_index,-1);
3352  if (index_real_instance >= fVarIndexes[codeindex][0]->fNdata[0]) {
3353  // out of bounds
3354  return fNdata[0]+1;
3355  }
3356  }
3357  if (fDidBooleanOptimization && local_index!=0) {
3358  // Force the loading of the index.
3359  fVarIndexes[codeindex][0]->LoadBranches();
3360  }
3361  local_index = (Int_t)fVarIndexes[codeindex][0]->EvalInstance(local_index);
3362  if (local_index<0) {
3363  Error("EvalInstance","Index %s is out of bound (%d) in formula %s",
3364  fVarIndexes[codeindex][0]->GetTitle(),
3365  local_index,
3366  GetTitle());
3367  return fNdata[0]+1;
3368  }
3369  }
3370  real_instance = local_index * fCumulSizes[codeindex][1];
3371  virt_dim ++;
3372  }
3373  } else {
3374  // NOTE: We assume that ONLY the first dimension of a leaf can have a variable
3375  // size AND contain the index for the size of yet another sub-dimension.
3376  // I.e. a variable size array inside a variable size array can only have its
3377  // size vary with the VERY FIRST physical dimension of the leaf.
3378  // Thus once the index of the first dimension is found, all other dimensions
3379  // are fixed!
3380 
3381  // NOTE: We could unroll some of this loops to avoid a few tests.
3382  if (fHasMultipleVarDim[codeindex]) {
3383  info = (TFormLeafInfo *)(fDataMembers.At(codeindex));
3384  // if (info && info->GetVarDim()==-1) info = 0;
3385  }
3386  Int_t local_index;
3387 
3388  switch (fIndexes[codeindex][0]) {
3389  case -2:
3390  if (fDidBooleanOptimization && instance!=0) {
3391  // Force the loading of the index.
3392  fVarIndexes[codeindex][0]->LoadBranches();
3393  }
3394  local_index = (Int_t)fVarIndexes[codeindex][0]->EvalInstance(instance);
3395  if (local_index<0) {
3396  Error("EvalInstance","Index %s is out of bound (%d) in formula %s",
3397  fVarIndexes[codeindex][0]->GetTitle(),
3398  local_index,
3399  GetTitle());
3400  local_index = 0;
3401  }
3402  break;
3403  case -1: {
3404  local_index = 0;
3405  Int_t virt_accum = 0;
3406  Int_t maxloop = fManager->fCumulUsedVarDims->GetSize();
3407  if (maxloop == 0) {
3408  local_index--;
3409  instance = fNdata[0]+1; // out of bounds.
3410  if (check) return fNdata[0]+1;
3411  } else {
3412  do {
3413  virt_accum += fManager->fCumulUsedVarDims->GetArray()[local_index];
3414  local_index++;
3415  } while( instance >= virt_accum && local_index<maxloop);
3416  if (local_index==maxloop && (instance >= virt_accum)) {
3417  local_index--;
3418  instance = fNdata[0]+1; // out of bounds.
3419  if (check) return fNdata[0]+1;
3420  } else {
3421  local_index--;
3422  if (fManager->fCumulUsedVarDims->At(local_index)) {
3423  instance -= (virt_accum - fManager->fCumulUsedVarDims->At(local_index));
3424  } else {
3425  instance = fNdata[0]+1; // out of bounds.
3426  if (check) return fNdata[0]+1;
3427  }
3428  }
3429  }
3430  virt_dim ++;
3431  }
3432  break;
3433  default:
3434  local_index = fIndexes[codeindex][0];
3435  }
3436 
3437  // Inform the (appropriate) MultiVarLeafInfo that the clones array index is
3438  // local_index.
3439 
3440  if (fManager->fVarDims[kMAXFORMDIM]) {
3442  } else {
3444  }
3445  for(Int_t d = kMAXFORMDIM-1; d>0; d--) {
3446  if (fManager->fVarDims[d]) {
3448  } else {
3450  }
3451  }
3452  if (info) {
3453  // When we have multiple variable dimensions, the LeafInfo only expect
3454  // the instance after the primary index has been set.
3455  info->SetPrimaryIndex(local_index);
3456  real_instance = 0;
3457 
3458  // Let's update fCumulSizes for the rest of the code.
3459  Int_t vdim = info->GetVarDim();
3460  Int_t isize = info->GetSize(local_index);
3461  if (fIndexes[codeindex][vdim]>=0) {
3462  info->SetSecondaryIndex(fIndexes[codeindex][vdim]);
3463  }
3464  if (isize!=1 && fIndexes[codeindex][vdim]>isize) {
3465  // We are out of bounds!
3466  return fNdata[0]+1;
3467  }
3468  fCumulSizes[codeindex][vdim] = isize*fCumulSizes[codeindex][vdim+1];
3469  for(Int_t k=vdim -1; k>0; --k) {
3470  fCumulSizes[codeindex][k] = fCumulSizes[codeindex][k+1]*fFixedSizes[codeindex][k];
3471  }
3472  } else {
3473  real_instance = local_index * fCumulSizes[codeindex][1];
3474  }
3475  }
3476  if (max_dim>0) {
3477  for (Int_t dim = 1; dim < max_dim; dim++) {
3478  if (fIndexes[codeindex][dim]>=0) {
3479  real_instance += fIndexes[codeindex][dim] * fCumulSizes[codeindex][dim+1];
3480  } else {
3481  Int_t local_index;
3482  if (virt_dim && fManager->fCumulUsedSizes[virt_dim]>1) {
3483  local_index = ( ( instance % fManager->fCumulUsedSizes[virt_dim] )
3484  / fManager->fCumulUsedSizes[virt_dim+1]);
3485  } else {
3486  local_index = ( instance / fManager->fCumulUsedSizes[virt_dim+1]);
3487  }
3488  if (fIndexes[codeindex][dim]==-2) {
3489  // NOTE: Should we check that this is a valid index?
3490  if (fDidBooleanOptimization && local_index!=0) {
3491  // Force the loading of the index.
3492  fVarIndexes[codeindex][dim]->LoadBranches();
3493  }
3494  local_index = (Int_t)fVarIndexes[codeindex][dim]->EvalInstance(local_index);
3495  if (local_index<0 ||
3496  local_index>=(fCumulSizes[codeindex][dim]/fCumulSizes[codeindex][dim+1])) {
3497  Error("EvalInstance","Index %s is out of bound (%d/%d) in formula %s",
3498  fVarIndexes[codeindex][dim]->GetTitle(),
3499  local_index,
3500  (fCumulSizes[codeindex][dim]/fCumulSizes[codeindex][dim+1]),
3501  GetTitle());
3502  local_index = (fCumulSizes[codeindex][dim]/fCumulSizes[codeindex][dim+1])-1;
3503  }
3504  }
3505  real_instance += local_index * fCumulSizes[codeindex][dim+1];
3506  virt_dim ++;
3507  }
3508  }
3509  if (fIndexes[codeindex][max_dim]>=0) {
3510  if (!info) real_instance += fIndexes[codeindex][max_dim];
3511  } else {
3512  Int_t local_index;
3513  if (virt_dim && fManager->fCumulUsedSizes[virt_dim]>1) {
3514  local_index = instance % fManager->fCumulUsedSizes[virt_dim];
3515  } else {
3516  local_index = instance;
3517  }
3518  if (info && local_index>=fCumulSizes[codeindex][max_dim]) {
3519  // We are out of bounds! [Multiple var dims, See same message a few line above]
3520  return fNdata[0]+1;
3521  }
3522  if (fIndexes[codeindex][max_dim]==-2) {
3523  if (fDidBooleanOptimization && local_index!=0) {
3524  // Force the loading of the index.
3525  fVarIndexes[codeindex][max_dim]->LoadBranches();
3526  }
3527  local_index = (Int_t)fVarIndexes[codeindex][max_dim]->EvalInstance(local_index);
3528  if (local_index<0 ||
3529  local_index>=fCumulSizes[codeindex][max_dim]) {
3530  Error("EvalInstance","Index %s is of out bound (%d/%d) in formula %s",
3531  fVarIndexes[codeindex][max_dim]->GetTitle(),
3532  local_index,
3533  fCumulSizes[codeindex][max_dim],
3534  GetTitle());
3535  local_index = fCumulSizes[codeindex][max_dim]-1;
3536  }
3537  }
3538  real_instance += local_index;
3539  }
3540  } // if (max_dim-1>0)
3541  } // if (max_dim)
3542 
3543  return real_instance;
3544 }
3545 
3546 ////////////////////////////////////////////////////////////////////////////////
3547 /// Evaluate the class of this treeformula.
3548 ///
3549 /// If the 'value' of this formula is a simple pointer to an object,
3550 /// this function returns the TClass corresponding to its type.
3551 
3553 {
3554  if (fNoper != 1 || fNcodes <=0 ) return 0;
3555 
3556  return EvalClass(0);
3557 }
3558 
3559 ////////////////////////////////////////////////////////////////////////////////
3560 /// Evaluate the class of the operation oper.
3561 ///
3562 /// If the 'value' in the requested operation is a simple pointer to an object,
3563 /// this function returns the TClass corresponding to its type.
3564 
3566 {
3567  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(oper);
3568  switch(fLookupType[oper]) {
3569  case kDirect: {
3570  if (leaf->IsA()==TLeafObject::Class()) {
3571  return ((TLeafObject*)leaf)->GetClass();
3572  } else if ( leaf->IsA()==TLeafElement::Class()) {
3573  TBranchElement * branch = (TBranchElement*)((TLeafElement*)leaf)->GetBranch();
3574  TStreamerInfo * info = branch->GetInfo();
3575  Int_t id = branch->GetID();
3576  if (id>=0) {
3577  if (info==0 || !info->IsCompiled()) {
3578  // we probably do not have a way to know the class of the object.
3579  return 0;
3580  }
3581  TStreamerElement* elem = (TStreamerElement*)info->GetElement(id);
3582  if (elem==0) {
3583  // we probably do not have a way to know the class of the object.
3584  return 0;
3585  } else {
3586  return elem->GetClass();
3587  }
3588  } else return TClass::GetClass( branch->GetClassName() );
3589  } else {
3590  return 0;
3591  }
3592  }
3593  case kMethod: return 0; // kMethod is deprecated so let's no waste time implementing this.
3594  case kTreeMember:
3595  case kDataMember: {
3597  if (!obj) return 0;
3598  return ((TFormLeafInfo*)obj)->GetClass();
3599  }
3600 
3601  default: return 0;
3602  }
3603 
3604 
3605 }
3606 
3607 ////////////////////////////////////////////////////////////////////////////////
3608 /// Evaluate this treeformula.
3609 ///
3610 /// Return the address of the object pointed to by the formula.
3611 /// Return 0 if the formula is not a single object
3612 /// The object type can be retrieved using by call EvalClass();
3613 
3614 void* TTreeFormula::EvalObject(int instance)
3615 {
3616  if (fNoper != 1 || fNcodes <=0 ) return 0;
3617 
3618 
3619  switch (fLookupType[0]) {
3620  case kIndexOfEntry:
3621  case kIndexOfLocalEntry:
3622  case kEntries:
3623  case kLength:
3624  case kLengthFunc:
3625  case kIteration:
3626  case kEntryList:
3627  return 0;
3628  }
3629 
3630  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(0);
3631 
3632  Int_t real_instance = GetRealInstance(instance,0);
3633 
3634  if (instance==0 || fNeedLoading) {
3635  fNeedLoading = kFALSE;
3636  R__LoadBranch(leaf->GetBranch(),
3637  leaf->GetBranch()->GetTree()->GetReadEntry(),
3638  fQuickLoad);
3639  }
3640  else if (real_instance>=fNdata[0]) return 0;
3641  if (fAxis) {
3642  return 0;
3643  }
3644  switch(fLookupType[0]) {
3645  case kDirect: {
3646  if (real_instance) {
3647  Warning("EvalObject","Not yet implement for kDirect and arrays (for %s).\nPlease contact the developers",GetName());
3648  }
3649  return leaf->GetValuePointer();
3650  }
3651  case kMethod: return GetValuePointerFromMethod(0,leaf);
3652  case kTreeMember:
3653  case kDataMember: return ((TFormLeafInfo*)fDataMembers.UncheckedAt(0))->GetValuePointer(leaf,real_instance);
3654  default: return 0;
3655  }
3656 
3657 
3658 }
3659 
3660 
3661 ////////////////////////////////////////////////////////////////////////////////
3662 /// Eval the instance as a string.
3663 
3665 {
3666  const Int_t kMAXSTRINGFOUND = 10;
3667  const char *stringStack[kMAXSTRINGFOUND];
3668 
3669  if (fNoper==1 && fNcodes>0 && IsString()) {
3670  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(0);
3671 
3672  Int_t real_instance = GetRealInstance(instance,0);
3673 
3674  if (instance==0 || fNeedLoading) {
3675  fNeedLoading = kFALSE;
3676  TBranch *branch = leaf->GetBranch();
3677  R__LoadBranch(branch,branch->GetTree()->GetReadEntry(),fQuickLoad);
3678  } else if (real_instance>=fNdata[0]) {
3679  return 0;
3680  }
3681 
3682  if (fLookupType[0]==kDirect) {
3683  return (char*)leaf->GetValuePointer();
3684  } else {
3685  return (char*)GetLeafInfo(0)->GetValuePointer(leaf,real_instance);
3686  }
3687  }
3688 
3689  EvalInstance(instance,stringStack);
3690 
3691  return stringStack[0];
3692 }
3693 
3694 #define TT_EVAL_INIT \
3695  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(0); \
3696  \
3697  const Int_t real_instance = GetRealInstance(instance,0); \
3698  \
3699  if (instance==0) fNeedLoading = kTRUE; \
3700  if (real_instance>=fNdata[0]) return 0; \
3701  \
3702  /* Since the only operation in this formula is reading this branch, \
3703  we are guaranteed that this function is first called with instance==0 and \
3704  hence we are guaranteed that the branch is always properly read */ \
3705  \
3706  if (fNeedLoading) { \
3707  fNeedLoading = kFALSE; \
3708  TBranch *br = leaf->GetBranch(); \
3709  Long64_t tentry = br->GetTree()->GetReadEntry(); \
3710  R__LoadBranch(br,tentry,fQuickLoad); \
3711  } \
3712  \
3713  if (fAxis) { \
3714  char * label; \
3715  /* This portion is a duplicate (for speed reason) of the code \
3716  located in the main for loop at "a tree string" (and in EvalStringInstance) */ \
3717  if (fLookupType[0]==kDirect) { \
3718  label = (char*)leaf->GetValuePointer(); \
3719  } else { \
3720  label = (char*)GetLeafInfo(0)->GetValuePointer(leaf,instance); \
3721  } \
3722  Int_t bin = fAxis->FindBin(label); \
3723  return bin-0.5; \
3724  }
3725 
3726 #define TREE_EVAL_INIT \
3727  const Int_t real_instance = GetRealInstance(instance,0); \
3728  \
3729  if (real_instance>=fNdata[0]) return 0; \
3730  \
3731  if (fAxis) { \
3732  char * label; \
3733  /* This portion is a duplicate (for speed reason) of the code \
3734  located in the main for loop at "a tree string" (and in EvalStringInstance) */ \
3735  label = (char*)GetLeafInfo(0)->GetValuePointer((TLeaf*)0x0,instance); \
3736  Int_t bin = fAxis->FindBin(label); \
3737  return bin-0.5; \
3738  }
3739 
3740 #define TT_EVAL_INIT_LOOP \
3741  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(code); \
3742  \
3743  /* Now let calculate what physical instance we really need. */ \
3744  const Int_t real_instance = GetRealInstance(instance,code); \
3745  \
3746  if (willLoad) { \
3747  TBranch *branch = (TBranch*)fBranches.UncheckedAt(code); \
3748  if (branch) { \
3749  Long64_t treeEntry = branch->GetTree()->GetReadEntry(); \
3750  R__LoadBranch(branch,treeEntry,fQuickLoad); \
3751  } else if (fDidBooleanOptimization) { \
3752  branch = leaf->GetBranch(); \
3753  Long64_t treeEntry = branch->GetTree()->GetReadEntry(); \
3754  if (branch->GetReadEntry() != treeEntry) branch->GetEntry( treeEntry ); \
3755  } \
3756  } else { \
3757  /* In the cases where we are behind (i.e. right of) a potential boolean optimization \
3758  this tree variable reading may have not been executed with instance==0 which would \
3759  result in the branch being potentially not read in. */ \
3760  if (fDidBooleanOptimization) { \
3761  TBranch *br = leaf->GetBranch(); \
3762  Long64_t treeEntry = br->GetTree()->GetReadEntry(); \
3763  if (br->GetReadEntry() != treeEntry) br->GetEntry( treeEntry ); \
3764  } \
3765  } \
3766  if (real_instance>=fNdata[code]) return 0;
3767 
3768 #define TREE_EVAL_INIT_LOOP \
3769  /* Now let calculate what physical instance we really need. */ \
3770  const Int_t real_instance = GetRealInstance(instance,code); \
3771  \
3772  if (real_instance>=fNdata[code]) return 0;
3773 
3774 
3775 template<typename T> T Summing(TTreeFormula *sum) {
3776  Int_t len = sum->GetNdata();
3777  T res = 0;
3778  for (int i=0; i<len; ++i) res += sum->EvalInstance<T>(i);
3779  return res;
3780 }
3781 
3782 template<typename T> T FindMin(TTreeFormula *arr) {
3783  Int_t len = arr->GetNdata();
3784  T res = 0;
3785  if (len) {
3786  res = arr->EvalInstance<T>(0);
3787  for (int i=1; i<len; ++i) {
3788  T val = arr->EvalInstance<T>(i);
3789  if (val < res) {
3790  res = val;
3791  }
3792  }
3793  }
3794  return res;
3795 }
3796 
3797 template<typename T> T FindMax(TTreeFormula *arr) {
3798  Int_t len = arr->GetNdata();
3799  T res = 0;
3800  if (len) {
3801  res = arr->EvalInstance<T>(0);
3802  for (int i=1; i<len; ++i) {
3803  T val = arr->EvalInstance(i);
3804  if (val > res) {
3805  res = val;
3806  }
3807  }
3808  }
3809  return res;
3810 }
3811 
3812 template<typename T> T FindMin(TTreeFormula *arr, TTreeFormula *condition) {
3813  Int_t len = arr->GetNdata();
3814  T res = 0;
3815  if (len) {
3816  int i = 0;
3817  T condval;
3818  do {
3819  condval = condition->EvalInstance<T>(i);
3820  ++i;
3821  } while (!condval && i<len);
3822  if (i==len) {
3823  return 0;
3824  }
3825  if (i!=1) {
3826  // Insure the loading of the branch.
3827  arr->EvalInstance<T>(0);
3828  }
3829  // Now we know that i>0 && i<len and cond==true
3830  res = arr->EvalInstance<T>(i-1);
3831  for (; i<len; ++i) {
3832  condval = condition->EvalInstance<T>(i);
3833  if (condval) {
3834  T val = arr->EvalInstance<T>(i);
3835  if (val < res) {
3836  res = val;
3837  }
3838  }
3839  }
3840  }
3841  return res;
3842 }
3843 
3844 template<typename T> T FindMax(TTreeFormula *arr, TTreeFormula *condition) {
3845  Int_t len = arr->GetNdata();
3846  T res = 0;
3847  if (len) {
3848  int i = 0;
3849  T condval;
3850  do {
3851  condval = condition->EvalInstance<T>(i);
3852  ++i;
3853  } while (!condval && i<len);
3854  if (i==len) {
3855  return 0;
3856  }
3857  if (i!=1) {
3858  // Insure the loading of the branch.
3859  arr->EvalInstance<T>(0);
3860  }
3861  // Now we know that i>0 && i<len and cond==true
3862  res = arr->EvalInstance<T>(i-1);
3863  for (; i<len; ++i) {
3864  condval = condition->EvalInstance<T>(i);
3865  if (condval) {
3866  T val = arr->EvalInstance<T>(i);
3867  if (val > res) {
3868  res = val;
3869  }
3870  }
3871  }
3872  }
3873  return res;
3874 }
3875 
3876 namespace {
3877 
3878 template <typename T> T fmod_local(T x, T y) { return fmod(x,y); }
3879 template <> Long64_t fmod_local(Long64_t x, Long64_t y) { return fmod((LongDouble_t)x,(LongDouble_t)y); }
3880 
3881 template<typename T> inline void SetMethodParam(TMethodCall *method, T p) { method->SetParam(p); }
3882 template<> void SetMethodParam(TMethodCall *method, LongDouble_t p) { method->SetParam((Double_t)p); }
3883 
3884 }
3885 
3886 template<typename T> inline T TTreeFormula::GetConstant(Int_t k) { return fConst[k]; }
3888  if( !fConstLD ) {
3889  // create LD version of the constants list by rescanning all literals used in the expression
3890  fConstLD = new LongDouble_t[fNconst];
3891  for (Int_t op=0; op<fNoper ; ++op) {
3892  const Int_t oper = GetOper()[op];
3893  if( (oper >> kTFOperShift) == kConstant ) {
3894  int i = (oper & kTFOperMask);
3895  if( !strncmp(fExpr[op], "0x", 2) || !strncmp(fExpr[op], "0X", 2) ) {
3896  ULong64_t val;
3897  sscanf( fExpr[op], "%llx", &val );
3898  fConstLD[i] = (LongDouble_t)val;
3899  } else {
3900  sscanf( fExpr[op], "%Lg", &fConstLD[i] );
3901  }
3902  }
3903  }
3904  }
3905  return fConstLD[k];
3906 }
3907 template<> inline Long64_t TTreeFormula::GetConstant(Int_t k) { return (Long64_t)GetConstant<LongDouble_t>(k); }
3908 
3909 ////////////////////////////////////////////////////////////////////////////////
3910 /// Evaluate this treeformula.
3911 
3912 template<typename T>
3913 T TTreeFormula::EvalInstance(Int_t instance, const char *stringStackArg[])
3914 {
3915 // Note that the redundance and structure in this code is tailored to improve
3916 // efficiencies.
3917  if (TestBit(kMissingLeaf)) return 0;
3918  if (fNoper == 1 && fNcodes > 0) {
3919 
3920  switch (fLookupType[0]) {
3921  case kDirect: {
3922  TT_EVAL_INIT;
3923  return leaf->GetTypedValue<T>(real_instance);
3924  }
3925  case kMethod: {
3926  TT_EVAL_INIT;
3927  ((TFormLeafInfo*)fDataMembers.UncheckedAt(0))->SetBranch(leaf->GetBranch());
3928  return GetValueFromMethod(0,leaf);
3929  }
3930  case kDataMember: {
3931  TT_EVAL_INIT;
3932  ((TFormLeafInfo*)fDataMembers.UncheckedAt(0))->SetBranch(leaf->GetBranch());
3933  return ((TFormLeafInfo*)fDataMembers.UncheckedAt(0))->GetTypedValue<T>(leaf,real_instance);
3934  }
3935  case kTreeMember: {
3937  return ((TFormLeafInfo*)fDataMembers.UncheckedAt(0))->GetTypedValue<T>((TLeaf*)0x0,real_instance);
3938  }
3939  case kIndexOfEntry: return (T)fTree->GetReadEntry();
3940  case kIndexOfLocalEntry: return (T)fTree->GetTree()->GetReadEntry();
3941  case kEntries: return (T)fTree->GetEntries();
3942  case kLength: return fManager->fNdata;
3943  case kLengthFunc: return ((TTreeFormula*)fAliases.UncheckedAt(0))->GetNdata();
3944  case kIteration: return instance;
3945  case kSum: return Summing<T>((TTreeFormula*)fAliases.UncheckedAt(0));
3946  case kMin: return FindMin<T>((TTreeFormula*)fAliases.UncheckedAt(0));
3947  case kMax: return FindMax<T>((TTreeFormula*)fAliases.UncheckedAt(0));
3948  case kEntryList: {
3949  TEntryList *elist = (TEntryList*)fExternalCuts.At(0);
3950  return elist->Contains(fTree->GetTree()->GetReadEntry());
3951  }
3952  case -1: break;
3953  }
3954  switch (fCodes[0]) {
3955  case -2: {
3956  TCutG *gcut = (TCutG*)fExternalCuts.At(0);
3957  TTreeFormula *fx = (TTreeFormula *)gcut->GetObjectX();
3958  TTreeFormula *fy = (TTreeFormula *)gcut->GetObjectY();
3959  T xcut = fx->EvalInstance<T>(instance);
3960  T ycut = fy->EvalInstance<T>(instance);
3961  return gcut->IsInside(xcut,ycut);
3962  }
3963  case -1: {
3964  TCutG *gcut = (TCutG*)fExternalCuts.At(0);
3965  TTreeFormula *fx = (TTreeFormula *)gcut->GetObjectX();
3966  return fx->EvalInstance<T>(instance);
3967  }
3968  default: return 0;
3969  }
3970  }
3971 
3972  T tab[kMAXFOUND];
3973  const Int_t kMAXSTRINGFOUND = 10;
3974  const char *stringStackLocal[kMAXSTRINGFOUND];
3975  const char **stringStack = stringStackArg?stringStackArg:stringStackLocal;
3976 
3977  const Bool_t willLoad = (instance==0 || fNeedLoading); fNeedLoading = kFALSE;
3978  if (willLoad) fDidBooleanOptimization = kFALSE;
3979 
3980  Int_t pos = 0;
3981  Int_t pos2 = 0;
3982  for (Int_t i=0; i<fNoper ; ++i) {
3983 
3984  const Int_t oper = GetOper()[i];
3985  const Int_t newaction = oper >> kTFOperShift;
3986 
3987  if (newaction<kDefinedVariable) {
3988  // ROOT::v5::TFormula operands.
3989 
3990  // one of the most used cases
3991  if (newaction==kConstant) { pos++; tab[pos-1] = GetConstant<T>(oper & kTFOperMask); continue; }
3992 
3993  switch(newaction) {
3994 
3995  case kEnd : return tab[0];
3996  case kAdd : pos--; tab[pos-1] += tab[pos]; continue;
3997  case kSubstract : pos--; tab[pos-1] -= tab[pos]; continue;
3998  case kMultiply : pos--; tab[pos-1] *= tab[pos]; continue;
3999  case kDivide : pos--; if (tab[pos] == 0) tab[pos-1] = 0; // division by 0
4000  else tab[pos-1] /= tab[pos];
4001  continue;
4002  case kModulo : {pos--;
4003  Long64_t int1((Long64_t)tab[pos-1]);
4004  Long64_t int2((Long64_t)tab[pos]);
4005  tab[pos-1] = T(int1 % int2);
4006  continue;}
4007 
4008  case kcos : tab[pos-1] = TMath::Cos(tab[pos-1]); continue;
4009  case ksin : tab[pos-1] = TMath::Sin(tab[pos-1]); continue;
4010  case ktan : if (TMath::Cos(tab[pos-1]) == 0) {tab[pos-1] = 0;} // { tangente indeterminee }
4011  else tab[pos-1] = TMath::Tan(tab[pos-1]);
4012  continue;
4013  case kacos : if (TMath::Abs(tab[pos-1]) > 1) {tab[pos-1] = 0;} // indetermination
4014  else tab[pos-1] = TMath::ACos(tab[pos-1]);
4015  continue;
4016  case kasin : if (TMath::Abs(tab[pos-1]) > 1) {tab[pos-1] = 0;} // indetermination
4017  else tab[pos-1] = TMath::ASin(tab[pos-1]);
4018  continue;
4019  case katan : tab[pos-1] = TMath::ATan(tab[pos-1]); continue;
4020  case kcosh : tab[pos-1] = TMath::CosH(tab[pos-1]); continue;
4021  case ksinh : tab[pos-1] = TMath::SinH(tab[pos-1]); continue;
4022  case ktanh : if (TMath::CosH(tab[pos-1]) == 0) {tab[pos-1] = 0;} // { tangente indeterminee }
4023  else tab[pos-1] = TMath::TanH(tab[pos-1]);
4024  continue;
4025  case kacosh: if (tab[pos-1] < 1) {tab[pos-1] = 0;} // indetermination
4026  else tab[pos-1] = TMath::ACosH(tab[pos-1]);
4027  continue;
4028  case kasinh: tab[pos-1] = TMath::ASinH(tab[pos-1]); continue;
4029  case katanh: if (TMath::Abs(tab[pos-1]) > 1) {tab[pos-1] = 0;} // indetermination
4030  else tab[pos-1] = TMath::ATanH(tab[pos-1]); continue;
4031  case katan2: pos--; tab[pos-1] = TMath::ATan2(tab[pos-1],tab[pos]); continue;
4032 
4033  case kfmod : pos--; tab[pos-1] = fmod_local(tab[pos-1],tab[pos]); continue;
4034  case kpow : pos--; tab[pos-1] = TMath::Power(tab[pos-1],tab[pos]); continue;
4035  case ksq : tab[pos-1] = tab[pos-1]*tab[pos-1]; continue;
4036  case ksqrt : tab[pos-1] = TMath::Sqrt(TMath::Abs(tab[pos-1])); continue;
4037 
4038  case kstrstr : pos2 -= 2; pos++;if (strstr(stringStack[pos2],stringStack[pos2+1])) tab[pos-1]=1;
4039  else tab[pos-1]=0; continue;
4040 
4041  case kmin : pos--; tab[pos-1] = std::min(tab[pos-1],tab[pos]); continue;
4042  case kmax : pos--; tab[pos-1] = std::max(tab[pos-1],tab[pos]); continue;
4043 
4044  case klog : if (tab[pos-1] > 0) tab[pos-1] = TMath::Log(tab[pos-1]);
4045  else {tab[pos-1] = 0;} //{indetermination }
4046  continue;
4047  case kexp : { Double_t dexp = tab[pos-1];
4048  if (dexp < -700) {tab[pos-1] = 0; continue;}
4049  if (dexp > 700) {tab[pos-1] = TMath::Exp(700); continue;}
4050  tab[pos-1] = TMath::Exp(dexp); continue;
4051  }
4052  case klog10: if (tab[pos-1] > 0) tab[pos-1] = TMath::Log10(tab[pos-1]);
4053  else {tab[pos-1] = 0;} //{indetermination }
4054  continue;
4055 
4056  case kpi : pos++; tab[pos-1] = TMath::ACos(-1); continue;
4057 
4058  case kabs : tab[pos-1] = TMath::Abs(tab[pos-1]); continue;
4059  case ksign : if (tab[pos-1] < 0) tab[pos-1] = -1; else tab[pos-1] = 1; continue;
4060  case kint : tab[pos-1] = T(Long64_t(tab[pos-1])); continue;
4061  case kSignInv: tab[pos-1] = -1 * tab[pos-1]; continue;
4062  case krndm : pos++; tab[pos-1] = gRandom->Rndm(1); continue;
4063 
4064  case kAnd : pos--; if (tab[pos-1]!=0 && tab[pos]!=0) tab[pos-1]=1;
4065  else tab[pos-1]=0; continue;
4066  case kOr : pos--; if (tab[pos-1]!=0 || tab[pos]!=0) tab[pos-1]=1;
4067  else tab[pos-1]=0; continue;
4068 
4069  case kEqual : pos--; tab[pos-1] = (tab[pos-1] == tab[pos]) ? 1 : 0; continue;
4070  case kNotEqual : pos--; tab[pos-1] = (tab[pos-1] != tab[pos]) ? 1 : 0; continue;
4071  case kLess : pos--; tab[pos-1] = (tab[pos-1] < tab[pos]) ? 1 : 0; continue;
4072  case kGreater : pos--; tab[pos-1] = (tab[pos-1] > tab[pos]) ? 1 : 0; continue;
4073  case kLessThan : pos--; tab[pos-1] = (tab[pos-1] <= tab[pos]) ? 1 : 0; continue;
4074  case kGreaterThan: pos--; tab[pos-1] = (tab[pos-1] >= tab[pos]) ? 1 : 0; continue;
4075  case kNot : tab[pos-1] = (tab[pos-1] != 0) ? 0 : 1; continue;
4076 
4077  case kStringEqual : pos2 -= 2; pos++; if (!strcmp(stringStack[pos2+1],stringStack[pos2])) tab[pos-1]=1;
4078  else tab[pos-1]=0; continue;
4079  case kStringNotEqual: pos2 -= 2; pos++;if (strcmp(stringStack[pos2+1],stringStack[pos2])) tab[pos-1]=1;
4080  else tab[pos-1]=0; continue;
4081 
4082  case kBitAnd : pos--; tab[pos-1]= ((ULong64_t) tab[pos-1]) & ((ULong64_t) tab[pos]); continue;
4083  case kBitOr : pos--; tab[pos-1]= ((ULong64_t) tab[pos-1]) | ((ULong64_t) tab[pos]); continue;
4084  case kLeftShift : pos--; tab[pos-1]= ((ULong64_t) tab[pos-1]) <<((ULong64_t) tab[pos]); continue;
4085  case kRightShift: pos--; tab[pos-1]= ((ULong64_t) tab[pos-1]) >>((ULong64_t) tab[pos]); continue;
4086 
4087  case kJump : i = (oper & kTFOperMask); continue;
4088  case kJumpIf : {
4089  pos--;
4090  if (!tab[pos]) {
4091  i = (oper & kTFOperMask);
4092  // If we skip the left (true) side of the if statement we may,
4093  // skip some of the branch loading (since we remove duplicate branch
4094  // request (in TTreeFormula constructor) and so we need to force the
4095  // loading here.
4096  if (willLoad) fDidBooleanOptimization = kTRUE;
4097  }
4098  continue;
4099  }
4100 
4101  case kStringConst: {
4102  // String
4103  pos2++; stringStack[pos2-1] = (char*)fExpr[i].Data();
4104  if (fAxis) {
4105  // See TT_EVAL_INIT
4106  Int_t bin = fAxis->FindBin(stringStack[pos2-1]);
4107  return bin;
4108  }
4109  continue;
4110  }
4111 
4112  case kBoolOptimize: {
4113  // boolean operation optimizer
4114 
4115  int param = (oper & kTFOperMask);
4116  Bool_t skip = kFALSE;
4117  int op = param % 10; // 1 is && , 2 is ||
4118 
4119  if (op == 1 && (!tab[pos-1]) ) {
4120  // &&: skip the right part if the left part is already false
4121 
4122  skip = kTRUE;
4123 
4124  // Preserve the existing behavior (i.e. the result of a&&b is
4125  // either 0 or 1)
4126  tab[pos-1] = 0;
4127 
4128  } else if (op == 2 && tab[pos-1] ) {
4129  // ||: skip the right part if the left part is already true
4130 
4131  skip = kTRUE;
4132 
4133  // Preserve the existing behavior (i.e. the result of a||b is
4134  // either 0 or 1)
4135  tab[pos-1] = 1;
4136  }
4137 
4138  if (skip) {
4139  int toskip = param / 10;
4140  i += toskip;
4141  if (willLoad) fDidBooleanOptimization = kTRUE;
4142  }
4143  continue;
4144  }
4145 
4146  case kFunctionCall: {
4147  // an external function call
4148 
4149  int param = (oper & kTFOperMask);
4150  int fno = param / 1000;
4151  int nargs = param % 1000;
4152 
4153  // Retrieve the function
4154  TMethodCall *method = (TMethodCall*)fFunctions.At(fno);
4155 
4156  // Set the arguments
4157  method->ResetParam();
4158  if (nargs) {
4159  UInt_t argloc = pos-nargs;
4160  for(Int_t j=0;j<nargs;j++,argloc++,pos--) {
4161  SetMethodParam(method, tab[argloc]);
4162  }
4163  }
4164  pos++;
4165  Double_t ret = 0;
4166  method->Execute(ret);
4167  tab[pos-1] = ret; // check for the correct conversion!
4168 
4169  continue;
4170  }
4171 
4172 // case kParameter: { pos++; tab[pos-1] = fParams[(oper & kTFOperMask)]; continue; }
4173  }
4174 
4175  } else {
4176  // TTreeFormula operands.
4177 
4178  // a tree variable (the most used case).
4179 
4180  if (newaction == kDefinedVariable) {
4181 
4182  const Int_t code = (oper & kTFOperMask);
4183  const Int_t lookupType = fLookupType[code];
4184  switch (lookupType) {
4185  case kIndexOfEntry: tab[pos++] = (T)fTree->GetReadEntry(); continue;
4186  case kIndexOfLocalEntry: tab[pos++] = (T)fTree->GetTree()->GetReadEntry(); continue;
4187  case kEntries: tab[pos++] = (T)fTree->GetEntries(); continue;
4188  case kLength: tab[pos++] = fManager->fNdata; continue;
4189  case kLengthFunc: tab[pos++] = ((TTreeFormula*)fAliases.UncheckedAt(i))->GetNdata(); continue;
4190  case kIteration: tab[pos++] = instance; continue;
4191  case kSum: tab[pos++] = Summing<T>((TTreeFormula*)fAliases.UncheckedAt(i)); continue;
4192  case kMin: tab[pos++] = FindMin<T>((TTreeFormula*)fAliases.UncheckedAt(i)); continue;
4193  case kMax: tab[pos++] = FindMax<T>((TTreeFormula*)fAliases.UncheckedAt(i)); continue;
4194 
4195  case kDirect: { TT_EVAL_INIT_LOOP; tab[pos++] = leaf->GetTypedValue<T>(real_instance); continue; }
4196  case kMethod: { TT_EVAL_INIT_LOOP; tab[pos++] = GetValueFromMethod(code,leaf); continue; }
4197  case kDataMember: { TT_EVAL_INIT_LOOP; tab[pos++] = ((TFormLeafInfo*)fDataMembers.UncheckedAt(code))->
4198  GetTypedValue<T>(leaf,real_instance); continue; }
4199  case kTreeMember: { TREE_EVAL_INIT_LOOP; tab[pos++] = ((TFormLeafInfo*)fDataMembers.UncheckedAt(code))->
4200  GetTypedValue<T>((TLeaf*)0x0,real_instance); continue; }
4201  case kEntryList: { TEntryList *elist = (TEntryList*)fExternalCuts.At(code);
4202  tab[pos++] = elist->Contains(fTree->GetReadEntry());
4203  continue;}
4204  case -1: break;
4205  default: tab[pos++] = 0; continue;
4206  }
4207  switch (fCodes[code]) {
4208  case -2: {
4209  TCutG *gcut = (TCutG*)fExternalCuts.At(code);
4210  TTreeFormula *fx = (TTreeFormula *)gcut->GetObjectX();
4211  TTreeFormula *fy = (TTreeFormula *)gcut->GetObjectY();
4212  T xcut = fx->EvalInstance<T>(instance);
4213  T ycut = fy->EvalInstance<T>(instance);
4214  tab[pos++] = gcut->IsInside(xcut,ycut);
4215  continue;
4216  }
4217  case -1: {
4218  TCutG *gcut = (TCutG*)fExternalCuts.At(code);
4219  TTreeFormula *fx = (TTreeFormula *)gcut->GetObjectX();
4220  tab[pos++] = fx->EvalInstance<T>(instance);
4221  continue;
4222  }
4223  default: {
4224  tab[pos++] = 0;
4225  continue;
4226  }
4227  }
4228  }
4229  switch(newaction) {
4230 
4231  // a TTree Variable Alias (i.e. a sub-TTreeFormula)
4232  case kAlias: {
4233  int aliasN = i;
4234  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(aliasN));
4235  R__ASSERT(subform);
4236 
4238  T param = subform->EvalInstance<T>(instance);
4239 
4240  tab[pos] = param; pos++;
4241  continue;
4242  }
4243  // a TTree Variable Alias String (i.e. a sub-TTreeFormula)
4244  case kAliasString: {
4245  int aliasN = i;
4246  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(aliasN));
4247  R__ASSERT(subform);
4248 
4249  pos2++;
4251  stringStack[pos2-1] = subform->EvalStringInstance(instance);
4252  continue;
4253  }
4254  case kMinIf: {
4255  int alternateN = i;
4256  TTreeFormula *primary = static_cast<TTreeFormula*>(fAliases.UncheckedAt(alternateN));
4257  TTreeFormula *condition = static_cast<TTreeFormula*>(fAliases.UncheckedAt(alternateN+1));
4258  T param = FindMin<T>(primary,condition);
4259  ++i; // skip the place holder for the condition
4260  tab[pos] = param; pos++;
4261  continue;
4262  }
4263  case kMaxIf: {
4264  int alternateN = i;
4265  TTreeFormula *primary = static_cast<TTreeFormula*>(fAliases.UncheckedAt(alternateN));
4266  TTreeFormula *condition = static_cast<TTreeFormula*>(fAliases.UncheckedAt(alternateN+1));
4267  T param = FindMax<T>(primary,condition);
4268  ++i; // skip the place holder for the condition
4269  tab[pos] = param; pos++;
4270  continue;
4271  }
4272 
4273  // a TTree Variable Alternate (i.e. a sub-TTreeFormula)
4274  case kAlternate: {
4275  int alternateN = i;
4276  TTreeFormula *primary = static_cast<TTreeFormula*>(fAliases.UncheckedAt(alternateN));
4277 
4278  // First check whether we are in range for the primary formula
4279  if (instance < primary->GetNdata()) {
4280 
4281  T param = primary->EvalInstance<T>(instance);
4282 
4283  ++i; // skip the alternate value.
4284 
4285  tab[pos] = param; pos++;
4286  } else {
4287  // The primary is not in rancge, we will calculate the alternate value
4288  // via the next operation (which will be a intentional).
4289 
4290  // kAlias no operations
4291  }
4292  continue;
4293  }
4294  case kAlternateString: {
4295  int alternateN = i;
4296  TTreeFormula *primary = static_cast<TTreeFormula*>(fAliases.UncheckedAt(alternateN));
4297 
4298  // First check whether we are in range for the primary formula
4299  if (instance < primary->GetNdata()) {
4300 
4301  pos2++;
4302  stringStack[pos2-1] = primary->EvalStringInstance(instance);
4303 
4304  ++i; // skip the alternate value.
4305 
4306  } else {
4307  // The primary is not in rancge, we will calculate the alternate value
4308  // via the next operation (which will be a kAlias).
4309 
4310  // intentional no operations
4311  }
4312  continue;
4313  }
4314 
4315  // a tree string
4316  case kDefinedString: {
4317  Int_t string_code = (oper & kTFOperMask);
4318  TLeaf *leafc = (TLeaf*)fLeaves.UncheckedAt(string_code);
4319 
4320  // Now let calculate what physical instance we really need.
4321  const Int_t real_instance = GetRealInstance(instance,string_code);
4322 
4323  if (instance==0 || fNeedLoading) {
4324  fNeedLoading = kFALSE;
4325  TBranch *branch = leafc->GetBranch();
4326  Long64_t readentry = branch->GetTree()->GetReadEntry();
4327  R__LoadBranch(branch,readentry,fQuickLoad);
4328  } else {
4329  // In the cases where we are behind (i.e. right of) a potential boolean optimization
4330  // this tree variable reading may have not been executed with instance==0 which would
4331  // result in the branch being potentially not read in.
4333  TBranch *br = leafc->GetBranch();
4334  Long64_t treeEntry = br->GetTree()->GetReadEntry();
4335  R__LoadBranch(br,treeEntry,kTRUE);
4336  }
4337  if (real_instance>=fNdata[string_code]) return 0;
4338  }
4339  pos2++;
4340  if (fLookupType[string_code]==kDirect) {
4341  stringStack[pos2-1] = (char*)leafc->GetValuePointer();
4342  } else {
4343  stringStack[pos2-1] = (char*)GetLeafInfo(string_code)->GetValuePointer(leafc,real_instance);
4344  }
4345  continue;
4346  }
4347 
4348  }
4349  }
4350 
4351  R__ASSERT(i<fNoper);
4352  }
4353 
4354  //std::cout << __PRETTY_FUNCTION__ << " returning " << tab[0] << std::endl;
4355  return tab[0];
4356 }
4357 
4358 // Template instantiations
4359 template double TTreeFormula::EvalInstance<double> (int, char const**);
4360 template long double TTreeFormula::EvalInstance<long double> (int, char const**);
4361 template long long TTreeFormula::EvalInstance<long long> (int, char const**);
4362 
4363 ////////////////////////////////////////////////////////////////////////////////
4364 /// Return DataMember corresponding to code.
4365 ///
4366 /// function called by TLeafObject::GetValue
4367 /// with the value of fLookupType computed in TTreeFormula::DefinedVariable
4368 
4370 {
4371  return (TFormLeafInfo *)fDataMembers.UncheckedAt(code);
4372 
4373 }
4374 
4375 ////////////////////////////////////////////////////////////////////////////////
4376 /// Return leaf corresponding to serial number n.
4377 
4379 {
4380  return (TLeaf*)fLeaves.UncheckedAt(n);
4381 }
4382 
4383 ////////////////////////////////////////////////////////////////////////////////
4384 /// Return methodcall corresponding to code.
4385 ///
4386 /// function called by TLeafObject::GetValue
4387 /// with the value of fLookupType computed in TTreeFormula::DefinedVariable
4388 
4390 {
4391  return (TMethodCall *)fMethods.UncheckedAt(code);
4392 
4393 }
4394 
4395 ////////////////////////////////////////////////////////////////////////////////
4396 /// Return number of available instances in the formula.
4397 
4399 {
4400  return fManager->GetNdata();
4401 }
4402 
4403 ////////////////////////////////////////////////////////////////////////////////
4404 /// Return result of a leafobject method.
4405 
4407 {
4408  TMethodCall* m = GetMethodCall(i);
4409 
4410  if (!m) {
4411  return 0.0;
4412  }
4413 
4414  void* thisobj = 0;
4415  if (leaf->InheritsFrom(TLeafObject::Class())) {
4416  thisobj = ((TLeafObject*) leaf)->GetObject();
4417  } else {
4418  TBranchElement* branch = (TBranchElement*) ((TLeafElement*) leaf)->GetBranch();
4419  Int_t id = branch->GetID();
4420  // FIXME: This is wrong for a top-level branch.
4421  Int_t offset = 0;
4422  if (id > -1) {
4423  TStreamerInfo* info = branch->GetInfo();
4424  if (info) {
4425  offset = info->GetElementOffset(id);
4426  } else {
4427  Warning("GetValueFromMethod", "No streamer info for branch %s.", branch->GetName());
4428  }
4429  }
4430  if (id < 0) {
4431  char* address = branch->GetObject();
4432  thisobj = address;
4433  } else {
4434  //char* address = branch->GetAddress();
4435  char* address = branch->GetObject();
4436  if (address) {
4437  thisobj = *((char**) (address + offset));
4438  } else {
4439  // FIXME: If the address is not set, the object won't be either!
4440  thisobj = branch->GetObject();
4441  }
4442  }
4443  }
4444 
4446 
4447  if (r == TMethodCall::kLong) {
4448  Long_t l = 0;
4449  m->Execute(thisobj, l);
4450  return (Double_t) l;
4451  }
4452 
4453  if (r == TMethodCall::kDouble) {
4454  Double_t d = 0.0;
4455  m->Execute(thisobj, d);
4456  return d;
4457  }
4458 
4459  m->Execute(thisobj);
4460 
4461  return 0;
4462 }
4463 
4464 ////////////////////////////////////////////////////////////////////////////////
4465 /// Return result of a leafobject method.
4466 
4468 {
4469  TMethodCall* m = GetMethodCall(i);
4470 
4471  if (!m) {
4472  return 0;
4473  }
4474 
4475  void* thisobj;
4476  if (leaf->InheritsFrom(TLeafObject::Class())) {
4477  thisobj = ((TLeafObject*) leaf)->GetObject();
4478  } else {
4479  TBranchElement* branch = (TBranchElement*) ((TLeafElement*) leaf)->GetBranch();
4480  Int_t id = branch->GetID();
4481  Int_t offset = 0;
4482  if (id > -1) {
4483  TStreamerInfo* info = branch->GetInfo();
4484  if (info) {
4485  offset = info->GetElementOffset(id);
4486  } else {
4487  Warning("GetValuePointerFromMethod", "No streamer info for branch %s.", branch->GetName());
4488  }
4489  }
4490  if (id < 0) {
4491  char* address = branch->GetObject();
4492  thisobj = address;
4493  } else {
4494  //char* address = branch->GetAddress();
4495  char* address = branch->GetObject();
4496  if (address) {
4497  thisobj = *((char**) (address + offset));
4498  } else {
4499  // FIXME: If the address is not set, the object won't be either!
4500  thisobj = branch->GetObject();
4501  }
4502  }
4503  }
4504 
4506 
4507  if (r == TMethodCall::kLong) {
4508  Long_t l = 0;
4509  m->Execute(thisobj, l);
4510  return 0;
4511  }
4512 
4513  if (r == TMethodCall::kDouble) {
4514  Double_t d = 0.0;
4515  m->Execute(thisobj, d);
4516  return 0;
4517  }
4518 
4519  if (r == TMethodCall::kOther) {
4520  char* c = 0;
4521  m->Execute(thisobj, &c);
4522  return c;
4523  }
4524 
4525  m->Execute(thisobj);
4526 
4527  return 0;
4528 }
4529 
4530 ////////////////////////////////////////////////////////////////////////////////
4531 /// Return TRUE if the formula corresponds to one single Tree leaf
4532 /// and this leaf is short, int or unsigned short, int
4533 /// When a leaf is of type integer or string, the generated histogram is forced
4534 /// to have an integer bin width
4535 
4537 {
4538  if (fast) {
4539  if (TestBit(kIsInteger)) return kTRUE;
4540  else return kFALSE;
4541  }
4542 
4543  if (fNoper==2 && GetAction(0)==kAlternate) {
4544  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(0));
4545  R__ASSERT(subform);
4546  return subform->IsInteger(kFALSE);
4547  }
4548 
4549  if (GetAction(0)==kMinIf || GetAction(0)==kMaxIf) {
4550  return kFALSE;
4551  }
4552 
4553  if (fNoper > 1) return kFALSE;
4554 
4555  if (GetAction(0)==kAlias) {
4556  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(0));
4557  R__ASSERT(subform);
4558  return subform->IsInteger(kFALSE);
4559  }
4560 
4561  if (fLeaves.GetEntries() != 1) {
4562  switch (fLookupType[0]) {
4563  case kIndexOfEntry:
4564  case kIndexOfLocalEntry:
4565  case kEntries:
4566  case kLength:
4567  case kLengthFunc:
4568  case kIteration:
4569  return kTRUE;
4570  case kSum:
4571  case kMin:
4572  case kMax:
4573  case kEntryList:
4574  default:
4575  return kFALSE;
4576  }
4577  }
4578 
4579  if (EvalClass()==TBits::Class()) return kTRUE;
4580 
4581  if (IsLeafInteger(0) || IsLeafString(0)) return kTRUE;
4582  return kFALSE;
4583 }
4584 
4585 ////////////////////////////////////////////////////////////////////////////////
4586 /// Return TRUE if the leaf corresponding to code is short, int or unsigned
4587 /// short, int When a leaf is of type integer, the generated histogram is
4588 /// forced to have an integer bin width
4589 
4591 {
4592  TLeaf *leaf = (TLeaf*)fLeaves.At(code);
4593  if (!leaf) {
4594  switch (fLookupType[code]) {
4595  case kIndexOfEntry:
4596  case kIndexOfLocalEntry:
4597  case kEntries:
4598  case kLength:
4599  case kLengthFunc:
4600  case kIteration:
4601  return kTRUE;
4602  case kSum:
4603  case kMin:
4604  case kMax:
4605  case kEntryList:
4606  default:
4607  return kFALSE;
4608  }
4609  }
4610  if (fAxis) return kTRUE;
4611  TFormLeafInfo * info;
4612  switch (fLookupType[code]) {
4613  case kMethod:
4614  case kTreeMember:
4615  case kDataMember:
4616  info = GetLeafInfo(code);
4617  return info->IsInteger();
4618  case kDirect:
4619  break;
4620  }
4621  if (!strcmp(leaf->GetTypeName(),"Int_t")) return kTRUE;
4622  if (!strcmp(leaf->GetTypeName(),"Short_t")) return kTRUE;
4623  if (!strcmp(leaf->GetTypeName(),"UInt_t")) return kTRUE;
4624  if (!strcmp(leaf->GetTypeName(),"UShort_t")) return kTRUE;
4625  if (!strcmp(leaf->GetTypeName(),"Bool_t")) return kTRUE;
4626  if (!strcmp(leaf->GetTypeName(),"Char_t")) return kTRUE;
4627  if (!strcmp(leaf->GetTypeName(),"UChar_t")) return kTRUE;
4628  if (!strcmp(leaf->GetTypeName(),"Long64_t")) return kTRUE;
4629  if (!strcmp(leaf->GetTypeName(),"ULong64_t")) return kTRUE;
4630  if (!strcmp(leaf->GetTypeName(),"string")) return kTRUE;
4631  return kFALSE;
4632 }
4633 
4634 ////////////////////////////////////////////////////////////////////////////////
4635 /// Return TRUE if the formula is a string
4636 
4638 {
4639  // See TTreeFormula::Init for the setting of kIsCharacter.
4640  return TestBit(kIsCharacter);
4641 }
4642 
4643 ////////////////////////////////////////////////////////////////////////////////
4644 /// Return true if the expression at the index 'oper' is to be treated as
4645 /// as string.
4646 
4648 {
4649  if (ROOT::v5::TFormula::IsString(oper)) return kTRUE;
4650  if (GetAction(oper)==kDefinedString) return kTRUE;
4651  if (GetAction(oper)==kAliasString) return kTRUE;
4652  if (GetAction(oper)==kAlternateString) return kTRUE;
4653  return kFALSE;
4654 }
4655 
4656 ////////////////////////////////////////////////////////////////////////////////
4657 /// Return TRUE if the leaf or data member corresponding to code is a string
4658 
4660 {
4661  TLeaf *leaf = (TLeaf*)fLeaves.At(code);
4662  TFormLeafInfo * info;
4663  if (fLookupType[code]==kTreeMember) {
4664  info = GetLeafInfo(code);
4665  return info->IsString();
4666  }
4667 
4668  switch(fLookupType[code]) {
4669  case kDirect:
4670  if ( !leaf->IsUnsigned() && (leaf->InheritsFrom(TLeafC::Class()) || leaf->InheritsFrom(TLeafB::Class()) ) ) {
4671  // Need to find out if it is an 'array' or a pointer.
4672  if (leaf->GetLenStatic() > 1) return kTRUE;
4673 
4674  // Now we need to differantiate between a variable length array and
4675  // a TClonesArray.
4676  if (leaf->GetLeafCount()) {
4677  const char* indexname = leaf->GetLeafCount()->GetName();
4678  if (indexname[strlen(indexname)-1] == '_' ) {
4679  // This in a clones array
4680  return kFALSE;
4681  } else {
4682  // this is a variable length char array
4683  return kTRUE;
4684  }
4685  }
4686  return kFALSE;
4687  } else if (leaf->InheritsFrom(TLeafElement::Class())) {
4688  TBranchElement * br = (TBranchElement*)leaf->GetBranch();
4689  Int_t bid = br->GetID();
4690  if (bid < 0) return kFALSE;
4691  if (br->GetInfo()==0 || !br->GetInfo()->IsCompiled()) {
4692  // Case where the file is corrupted is some ways.
4693  // We can not get to the actual type of the data
4694  // let's assume it is NOT a string.
4695  return kFALSE;
4696  }
4697  TStreamerElement * elem = (TStreamerElement*) br->GetInfo()->GetElement(bid);
4698  if (!elem) {
4699  // Case where the file is corrupted is some ways.
4700  // We can not get to the actual type of the data
4701  // let's assume it is NOT a string.
4702  return kFALSE;
4703  }
4704  if (elem->GetNewType()== TStreamerInfo::kOffsetL +kChar_t) {
4705  // Check whether a specific element of the string is specified!
4706  if (fIndexes[code][fNdimensions[code]-1] != -1) return kFALSE;
4707  return kTRUE;
4708  }
4709  if ( elem->GetNewType()== TStreamerInfo::kCharStar) {
4710  // Check whether a specific element of the string is specified!
4711  if (fNdimensions[code] && fIndexes[code][fNdimensions[code]-1] != -1) return kFALSE;
4712  return kTRUE;
4713  }
4714  return kFALSE;
4715  } else {
4716  return kFALSE;
4717  }
4718  case kMethod:
4719  //TMethodCall *m = GetMethodCall(code);
4720  //TMethodCall::EReturnType r = m->ReturnType();
4721  return kFALSE;
4722  case kDataMember:
4723  info = GetLeafInfo(code);
4724  return info->IsString();
4725  default:
4726  return kFALSE;
4727  }
4728 }
4729 
4730 ////////////////////////////////////////////////////////////////////////////////
4731 /// Return value of variable as a string
4732 ///
4733 /// - mode = -2 : Print line with ***
4734 /// - mode = -1 : Print column names
4735 /// - mode = 0 : Print column values
4736 
4738 {
4739  return PrintValue(mode,0);
4740 }
4741 
4742 ////////////////////////////////////////////////////////////////////////////////
4743 /// Return value of variable as a string
4744 ///
4745 /// - mode = -2 : Print line with ***
4746 /// - mode = -1 : Print column names
4747 /// - mode = 0 : Print column values
4748 ///
4749 /// decform contains the requested format (with the same convention as printf).
4750 
4751 char *TTreeFormula::PrintValue(Int_t mode, Int_t instance, const char *decform) const
4752 {
4753  const int kMAXLENGTH = 1024;
4754  static char value[kMAXLENGTH];
4755 
4756  if (mode == -2) {
4757  for (int i = 0; i < kMAXLENGTH-1; i++)
4758  value[i] = '*';
4759  value[kMAXLENGTH-1] = 0;
4760  } else if (mode == -1) {
4761  snprintf(value, kMAXLENGTH-1, "%s", GetTitle());
4762  } else if (mode == 0) {
4763  if ( (fNstring && fNval==0 && fNoper==1) || IsString() )
4764  {
4765  const char * val = 0;
4766  if (GetAction(0)==kStringConst) {
4767  val = fExpr[0].Data();
4768  } else if (instance<fNdata[0]) {
4769  if (fNoper == 1) {
4770  if (fLookupType[0]==kTreeMember) {
4771  val = (char*)GetLeafInfo(0)->GetValuePointer((TLeaf*)0x0,instance);
4772  } else {
4773  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(0);
4774  TBranch *branch = leaf->GetBranch();
4775  Long64_t readentry = branch->GetTree()->GetReadEntry();
4776  R__LoadBranch(branch,readentry,fQuickLoad);
4777  if (fLookupType[0]==kDirect && fNoper==1) {
4778  val = (const char*)leaf->GetValuePointer();
4779  } else {
4780  val = ((TTreeFormula*)this)->EvalStringInstance(instance);
4781  }
4782  }
4783  } else {
4784  val = ((TTreeFormula*)this)->EvalStringInstance(instance);
4785  }
4786  }
4787  if (val) {
4788  strlcpy(value, val, kMAXLENGTH);
4789  } else {
4790  value[0] = '\0';
4791  }
4792  value[kMAXLENGTH-1] = 0;
4793  } else {
4794  //NOTE: This is terrible form ... but is forced upon us by the fact that we can not
4795  //use the mutable keyword AND we should keep PrintValue const.
4796  Int_t real_instance = ((TTreeFormula*)this)->GetRealInstance(instance,-1);
4797  if (real_instance<fNdata[0]) {
4798  Ssiz_t len = strlen(decform);
4799  Char_t outputSizeLevel = 1;
4800  char *expo = 0;
4801  if (len>2) {
4802  switch (decform[len-2]) {
4803  case 'l':
4804  case 'L': {
4805  outputSizeLevel = 2;
4806  if (len>3 && tolower(decform[len-3])=='l') {
4807  outputSizeLevel = 3;
4808  }
4809  break;
4810  }
4811  case 'h': outputSizeLevel = 0; break;
4812  }
4813  }
4814  switch(decform[len-1]) {
4815  case 'c':
4816  case 'd':
4817  case 'i':
4818  {
4819  switch (outputSizeLevel) {
4820  case 0: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(Short_t)((TTreeFormula*)this)->EvalInstance(instance)); break;
4821  case 2: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(Long_t)((TTreeFormula*)this)->EvalInstance(instance)); break;
4822  case 3: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(Long64_t)((TTreeFormula*)this)->EvalInstance<LongDouble_t>(instance)); break;
4823  case 1:
4824  default: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(Int_t)((TTreeFormula*)this)->EvalInstance(instance)); break;
4825  }
4826  break;
4827  }
4828  case 'o':
4829  case 'x':
4830  case 'X':
4831  case 'u':
4832  {
4833  switch (outputSizeLevel) {
4834  case 0: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(UShort_t)((TTreeFormula*)this)->EvalInstance(instance)); break;
4835  case 2: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(ULong_t)((TTreeFormula*)this)->EvalInstance(instance)); break;
4836  case 3: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(ULong64_t)((TTreeFormula*)this)->EvalInstance<LongDouble_t>(instance)); break;
4837  case 1:
4838  default: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(UInt_t)((TTreeFormula*)this)->EvalInstance(instance)); break;
4839  }
4840  break;
4841  }
4842  case 'f':
4843  case 'e':
4844  case 'E':
4845  case 'g':
4846  case 'G':
4847  {
4848  switch (outputSizeLevel) {
4849  case 2: snprintf(value,kMAXLENGTH,Form("%%%s",decform),(LongDouble_t)((TTreeFormula*)this)->EvalInstance<LongDouble_t>(instance)); break;
4850  case 1:
4851  default: snprintf(value,kMAXLENGTH,Form("%%%s",decform),((TTreeFormula*)this)->EvalInstance(instance)); break;
4852  }
4853  expo = strchr(value,'e');
4854  break;
4855  }
4856  default:
4857  snprintf(value,kMAXLENGTH,Form("%%%sg",decform),((TTreeFormula*)this)->EvalInstance(instance));
4858  expo = strchr(value,'e');
4859  }
4860  if (expo) {
4861  // If there is an exponent we may be longer than planned.
4862  // so let's trim off the excess precission!
4863  UInt_t declen = atoi(decform);
4864  if (strlen(value)>declen) {
4865  UInt_t off = strlen(value)-declen;
4866  char *start = expo - off;
4867  UInt_t vlen = strlen(expo);
4868  for(UInt_t z=0;z<=vlen;++z) {
4869  start[z] = expo[z];
4870  }
4871  //strcpy(expo-off,expo);
4872  }
4873  }
4874  } else {
4875  if (isalpha(decform[strlen(decform)-1])) {
4876  TString short_decform(decform);
4877  short_decform.Remove(short_decform.Length()-1);
4878  snprintf(value,kMAXLENGTH,Form(" %%%sc",short_decform.Data()),' ');
4879  } else {
4880  snprintf(value,kMAXLENGTH,Form(" %%%sc",decform),' ');
4881  }
4882 
4883  }
4884  }
4885  }
4886  return &value[0];
4887 }
4888 
4889 ////////////////////////////////////////////////////////////////////////////////
4890 /// Tell the formula that we are going to request a new entry.
4891 
4893 {
4894  fNeedLoading = kTRUE;
4896 
4897  for(Int_t i=0; i<fNcodes; ++i) {
4898  UInt_t max_dim = fNdimensions[i];
4899  for(UInt_t dim=0; dim<max_dim ;++dim) {
4900  if (fVarIndexes[i][dim]) {
4901  fVarIndexes[i][dim]->ResetLoading();
4902  }
4903  }
4904  }
4905  Int_t n = fAliases.GetLast();
4906  if ( fNoper < n ) {
4907  n = fNoper;
4908  }
4909  for(Int_t k=0; k <= n; ++k) {
4910  TTreeFormula *f = static_cast<TTreeFormula*>(fAliases.UncheckedAt(k));
4911  if (f) {
4912  f->ResetLoading();
4913  }
4914  }
4915 }
4916 
4917 ////////////////////////////////////////////////////////////////////////////////
4918 /// Set the axis (in particular get the type).
4919 
4921 {
4922  if (!axis) {fAxis = 0; return;}
4923  if (IsString()) {
4924  fAxis = axis;
4925  if (fNoper==1 && GetAction(0)==kAliasString){
4926  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(0));
4927  R__ASSERT(subform);
4928  subform->SetAxis(axis);
4929  } else if (fNoper==2 && GetAction(0)==kAlternateString){
4930  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(0));
4931  R__ASSERT(subform);
4932  subform->SetAxis(axis);
4933  }
4934  // Since the bin are corresponding to 'string', we currently must also set
4935  // the axis to align the bins exactly on integer boundaries.
4936  axis->SetBit(TAxis::kIsInteger);
4937  } else if (IsInteger()) {
4938  axis->SetBit(TAxis::kIsInteger);
4939  }
4940 }
4941 
4942 ////////////////////////////////////////////////////////////////////////////////
4943 /// Stream an object of class TTreeFormula.
4944 
4945 void TTreeFormula::Streamer(TBuffer &R__b)
4946 {
4947  if (R__b.IsReading()) {
4948  UInt_t R__s, R__c;
4949  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
4950  if (R__v > 2) {
4951  R__b.ReadClassBuffer(TTreeFormula::Class(), this, R__v, R__s, R__c);
4952  return;
4953  }
4954  //====process old versions before automatic schema evolution
4956  R__b >> fTree;
4957  R__b >> fNcodes;
4958  R__b.ReadFastArray(fCodes, fNcodes);
4959  R__b >> fMultiplicity;
4960  Int_t instance;
4961  R__b >> instance; //data member removed
4962  R__b >> fNindex;
4963  if (fNindex) {
4964  fLookupType = new Int_t[fNindex];
4965  R__b.ReadFastArray(fLookupType, fNindex);
4966  }
4967  fMethods.Streamer(R__b);
4968  //====end of old versions
4969 
4970  } else {
4972  }
4973 }
4974 
4975 ////////////////////////////////////////////////////////////////////////////////
4976 /// Try to 'demote' a string into an array bytes. If this is not possible,
4977 /// return false.
4978 
4980 {
4981  Int_t code = GetActionParam(oper);
4982  if (GetAction(oper)==kDefinedString && fLookupType[code]==kDirect) {
4983  if (oper>0 && GetAction(oper-1)==kJump) {
4984  // We are the second hand of a ternary operator, let's not do the fixing.
4985  return kFALSE;
4986  }
4987  TLeaf *leaf = (TLeaf*)fLeaves.At(code);
4988  if (leaf && (leaf->InheritsFrom(TLeafC::Class()) || leaf->InheritsFrom(TLeafB::Class()) ) ) {
4989  SetAction(oper, kDefinedVariable, code );
4990  fNval++;
4991  fNstring--;
4992  return kTRUE;
4993  }
4994  }
4995  return kFALSE;
4996 }
4997 
4998 
4999 ////////////////////////////////////////////////////////////////////////////////
5000 /// This function is called TTreePlayer::UpdateFormulaLeaves, itself
5001 /// called by TChain::LoadTree when a new Tree is loaded.
5002 /// Because Trees in a TChain may have a different list of leaves, one
5003 /// must update the leaves numbers in the TTreeFormula used by the TreePlayer.
5004 ///
5005 /// A safer alternative would be to recompile the whole thing .... However
5006 /// currently compile HAS TO be called from the constructor!
5007 
5009 {
5010  Int_t nleaves = fLeafNames.GetEntriesFast();
5012  for (Int_t i=0;i<nleaves;i++) {
5013  if (!fTree) break;
5014  if (!fLeafNames[i]) continue;
5015 
5016  TLeaf *leaf = fTree->GetLeaf(fLeafNames[i]->GetTitle(),fLeafNames[i]->GetName());
5017  fLeaves[i] = leaf;
5018  if (fBranches[i] && leaf) {
5019  fBranches[i] = leaf->GetBranch();
5020  // Since sometimes we might no read all the branches for all the entries, we
5021  // might sometimes only read the branch count and thus reset the colleciton
5022  // but might not read the data branches, to insure that a subsequent read
5023  // from TTreeFormula will properly load the data branches even if fQuickLoad is true,
5024  // we reset the entry of all branches in the TTree.
5025  ((TBranch*)fBranches[i])->ResetReadEntry();
5026  }
5027  if (leaf==0) SetBit( kMissingLeaf );
5028  }
5029  for (Int_t j=0; j<kMAXCODES; j++) {
5030  for (Int_t k = 0; k<kMAXFORMDIM; k++) {
5031  if (fVarIndexes[j][k]) {
5033  }
5034  }
5036  if (j<fNval && fCodes[j]<0) {
5037  TCutG *gcut = (TCutG*)fExternalCuts.At(j);
5038  if (gcut) {
5039  TTreeFormula *fx = (TTreeFormula *)gcut->GetObjectX();
5040  TTreeFormula *fy = (TTreeFormula *)gcut->GetObjectY();
5041  if (fx) fx->UpdateFormulaLeaves();
5042  if (fy) fy->UpdateFormulaLeaves();
5043  }
5044  }
5045  }
5046  for(Int_t k=0;k<fNoper;k++) {
5047  const Int_t oper = GetOper()[k];
5048  switch(oper >> kTFOperShift) {
5049  case kAlias:
5050  case kAliasString:
5051  case kAlternate:
5052  case kAlternateString:
5053  case kMinIf:
5054  case kMaxIf:
5055  {
5056  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(k));
5057  R__ASSERT(subform);
5058  subform->UpdateFormulaLeaves();
5059  break;
5060  }
5061  case kDefinedVariable:
5062  {
5063  Int_t code = GetActionParam(k);
5064  if (fCodes[code]==0) switch(fLookupType[code]) {
5065  case kLengthFunc:
5066  case kSum:
5067  case kMin:
5068  case kMax:
5069  {
5070  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(k));
5071  R__ASSERT(subform);
5072  subform->UpdateFormulaLeaves();
5073  break;
5074  }
5075  default:
5076  break;
5077  }
5078  }
5079  default:
5080  break;
5081  }
5082  }
5083 }
5084 
5085 ////////////////////////////////////////////////////////////////////////////////
5086 /// Populate the TTreeFormulaManager with the dimension information.
5087 
5089  Int_t i,k;
5090 
5091  // Now that we saw all the expressions and variables AND that
5092  // we know whether arrays of chars are treated as string or
5093  // not, we can properly setup the dimensions.
5095  Int_t last_code = -1;
5096  Int_t virt_dim = 0;
5097  for(TDimensionInfo * info; (info = (TDimensionInfo*)next()); ) {
5098  if (last_code!=info->fCode) {
5099  // We know that the list is ordered by code number then by
5100  // dimension. Thus a different code means that we need to
5101  // restart at the lowest dimensions.
5102  virt_dim = 0;
5103  last_code = info->fCode;
5104  fNdimensions[last_code] = 0;
5105  }
5106 
5107  if (GetAction(info->fOper)==kDefinedString) {
5108 
5109  // We have a string used as a string (and not an array of number)
5110  // We need to determine which is the last dimension and skip it.
5111  TDimensionInfo *nextinfo = (TDimensionInfo*)next();
5112  while(nextinfo && nextinfo->fCode==info->fCode) {
5113  DefineDimensions(info->fCode,info->fSize, info->fMultiDim, virt_dim);
5114  nextinfo = (TDimensionInfo*)next();
5115  }
5116  if (!nextinfo) break;
5117 
5118  info = nextinfo;
5119  virt_dim = 0;
5120  last_code = info->fCode;
5121  fNdimensions[last_code] = 0;
5122 
5123  info->fSize = 1; // Maybe this should actually do nothing!
5124  }
5125 
5126 
5127  DefineDimensions(info->fCode,info->fSize, info->fMultiDim, virt_dim);
5128  }
5129 
5130  fMultiplicity = 0;
5131  for(i=0;i<fNoper;i++) {
5132  Int_t action = GetAction(i);
5133 
5134  if (action==kMinIf || action==kMaxIf) {
5135  // Skip/Ignore the 2nd args
5136  ++i;
5137  continue;
5138  }
5139  if (action==kAlias || action==kAliasString) {
5140  TTreeFormula *subform = static_cast<TTreeFormula*>(fAliases.UncheckedAt(i));
5141  R__ASSERT(subform);
5142  switch(subform->GetMultiplicity()) {
5143  case 0: break;
5144  case 1: fMultiplicity = 1; break;
5145  case 2: if (fMultiplicity!=1) fMultiplicity = 2; break;
5146  }
5147  fManager->Add(subform);
5148  // since we are addint to this manager 'subform->ResetDimensions();'
5149  // will be called a little latter
5150  continue;
5151  }
5152  if (action==kDefinedString) {
5153  //if (fOper[i] >= 105000 && fOper[i]<110000) {
5154  // We have a string used as a string
5155 
5156  // This dormant portion of code would be used if (when?) we allow the histogramming
5157  // of the integral content (as opposed to the string content) of strings
5158  // held in a variable size container delimited by a null (as opposed to
5159  // a fixed size container or variable size container whose size is controlled
5160  // by a variable). In GetNdata, we will then use strlen to grab the current length.
5161  //fCumulSizes[i][fNdimensions[i]-1] = 1;
5162  //fUsedSizes[fNdimensions[i]-1] = -TMath::Abs(fUsedSizes[fNdimensions[i]-1]);
5163  //fUsedSizes[0] = - TMath::Abs( fUsedSizes[0]);
5164 
5165  //continue;
5166  }
5167  }
5168 
5169  for (i=0;i<fNcodes;i++) {
5170  if (fCodes[i] < 0) {
5171  TCutG *gcut = (TCutG*)fExternalCuts.At(i);
5172  if (!gcut) continue;
5173  TTreeFormula *fx = (TTreeFormula *)gcut->GetObjectX();
5174  TTreeFormula *fy = (TTreeFormula *)gcut->GetObjectY();
5175 
5176  if (fx) {
5177  switch(fx->GetMultiplicity()) {
5178  case 0: break;
5179  case 1: fMultiplicity = 1; break;
5180  case 2: if (fMultiplicity!=1) fMultiplicity = 2; break;
5181  }
5182  fManager->Add(fx);
5183  }
5184  if (fy) {
5185  switch(fy->GetMultiplicity()) {
5186  case 0: break;
5187  case 1: fMultiplicity = 1; break;
5188  case 2: if (fMultiplicity!=1) fMultiplicity = 2; break;
5189  }
5190  fManager->Add(fy);
5191  }
5192 
5193  continue;
5194  }
5195 
5196  if (fLookupType[i]==kIteration) {
5197  fMultiplicity = 1;
5198  continue;
5199  }
5200 
5201  TLeaf *leaf = i <= fLeaves.GetLast() ? (TLeaf*)fLeaves.UncheckedAt(i) : 0;
5202  if (!leaf) continue;
5203 
5204  // Reminder of the meaning of fMultiplicity:
5205  // -1: Only one or 0 element per entry but contains variable length
5206  // -array! (Only used for TTreeFormulaManager)
5207  // 0: Only one element per entry, no variable length array
5208  // 1: loop over the elements of a variable length array
5209  // 2: loop over elements of fixed length array (nData is the same for all entry)
5210 
5211  if (leaf->GetLeafCount()) {
5212  // We assume only one possible variable length dimension (the left most)
5213  fMultiplicity = 1;
5214  } else if (fLookupType[i]==kDataMember) {
5215  TFormLeafInfo * leafinfo = GetLeafInfo(i);
5216  TStreamerElement * elem = leafinfo->fElement;
5217  if (fMultiplicity!=1) {
5218  if (leafinfo->HasCounter() ) fMultiplicity = 1;
5219  else if (elem && elem->GetArrayDim()>0) fMultiplicity = 2;
5220  else if (leaf->GetLenStatic()>1) fMultiplicity = 2;
5221  }
5222  } else {
5223  if (leaf->GetLenStatic()>1 && fMultiplicity!=1) fMultiplicity = 2;
5224  }
5225  if (fMultiplicity!=1) {
5226  // If the leaf belongs to a friend tree which has an index, we might
5227  // be in the case where some entry do not exist.
5228 
5229  TTree *realtree = fTree ? fTree->GetTree() : 0;
5230  TTree *tleaf = leaf->GetBranch()->GetTree();
5231  if (tleaf && tleaf != realtree && tleaf->GetTreeIndex()) {
5232  // Reset the multiplicity if we have a friend tree with an index.
5233  fMultiplicity = 1;
5234  }
5235  }
5236 
5237  Int_t virt_dim2 = 0;
5238  for (k = 0; k < fNdimensions[i]; k++) {
5239  // At this point fCumulSizes[i][k] actually contain the physical
5240  // dimension of the k-th dimensions.
5241  if ( (fCumulSizes[i][k]>=0) && (fIndexes[i][k] >= fCumulSizes[i][k]) ) {
5242  // unreacheable element requested:
5243  fManager->CancelDimension(virt_dim2); // fCumulUsedSizes[virt_dim2] = 0;
5244  }
5245  if ( fIndexes[i][k] < 0 ) virt_dim2++;
5246  fFixedSizes[i][k] = fCumulSizes[i][k];
5247  }
5248 
5249  // Add up the cumulative size
5250  for (k = fNdimensions[i]; (k > 0); k--) {
5251  // NOTE: When support for inside variable dimension is added this
5252  // will become inacurate (since one of the value in the middle of the chain
5253  // is unknown until GetNdata is called.
5254  fCumulSizes[i][k-1] *= TMath::Abs(fCumulSizes[i][k]);
5255  }
5256  // NOTE: We assume that the inside variable dimensions are dictated by the
5257  // first index.
5258  if (fCumulSizes[i][0]>0) fNdata[i] = fCumulSizes[i][0];
5259 
5260  //for (k = 0; k<kMAXFORMDIM; k++) {
5261  // if (fVarIndexes[i][k]) fManager->Add(fVarIndexes[i][k]);
5262  //}
5263 
5264  }
5265 }
5266 
5267 ////////////////////////////////////////////////////////////////////////////////
5268 /// Make sure that all the branches have been loaded properly.
5269 
5271 {
5272  Int_t i;
5273  for (i=0; i<fNoper ; ++i) {
5274  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(i);
5275  if (leaf==0) continue;
5276 
5277  TBranch *br = leaf->GetBranch();
5278  Long64_t treeEntry = br->GetTree()->GetReadEntry();
5279  R__LoadBranch(br,treeEntry,kTRUE);
5280 
5282  if (alias) alias->LoadBranches();
5283 
5284  Int_t max_dim = fNdimensions[i];
5285  for (Int_t dim = 0; dim < max_dim; ++dim) {
5286  if (fVarIndexes[i][dim]) fVarIndexes[i][dim]->LoadBranches();
5287  }
5288  }
5289 }
5290 
5291 ////////////////////////////////////////////////////////////////////////////////
5292 /// Calculate the actual dimension for the current entry.
5293 
5295  Int_t size;
5296  Bool_t outofbounds = kFALSE;
5297 
5298  for (Int_t i=0;i<fNcodes;i++) {
5299  if (fCodes[i] < 0) continue;
5300 
5301  // NOTE: Currently only the leafcount can indicates a dimension that
5302  // is physically variable. So only the left-most dimension is variable.
5303  // When an API is introduced to be able to determine a variable inside dimensions
5304  // one would need to add a way to recalculate the values of fCumulSizes for this
5305  // leaf. This would probably require the addition of a new data member
5306  // fSizes[kMAXCODES][kMAXFORMDIM];
5307  // Also note that EvalInstance expect all the values (but the very first one)
5308  // of fCumulSizes to be positive. So indicating that a physical dimension is
5309  // variable (expected for the first one) can NOT be done via negative values of
5310  // fCumulSizes.
5311 
5312  TLeaf *leaf = (TLeaf*)fLeaves.UncheckedAt(i);
5313  if (!leaf) {
5314  switch(fLookupType[i]) {
5315  case kDirect:
5316  case kMethod:
5317  case kTreeMember:
5318  case kDataMember:
5319  fNdata[i] = 0;
5320  outofbounds = kTRUE;
5321  }
5322  continue;
5323  }
5324 
5325  TTree *realtree = fTree->GetTree();
5326  TTree *tleaf = leaf->GetBranch()->GetTree();
5327  if (tleaf && tleaf != realtree && tleaf->GetTreeIndex()) {
5328  if (tleaf->GetReadEntry() < 0) {
5329  fNdata[i] = 0;
5330  outofbounds = kTRUE;
5331  continue;
5332  } else {
5333  fNdata[i] = fCumulSizes[i][0];
5334  }
5335  }
5336  Bool_t hasBranchCount2 = kFALSE;
5337  if (leaf->GetLeafCount()) {
5338  TLeaf* leafcount = leaf->GetLeafCount();
5339  TBranch *branchcount = leafcount->GetBranch();
5340  TFormLeafInfo * info = 0;
5341  if (leaf->IsA() == TLeafElement::Class()) {
5342  //if branchcount address not yet set, GetEntry will set the address
5343  // read branchcount value
5344  Long64_t readentry = leaf->GetBranch()->GetTree()->GetReadEntry();
5345  if (readentry < 0) readentry=0;
5346  if (!branchcount->GetAddress()) {
5347  R__LoadBranch(branchcount, readentry, fQuickLoad);
5348  } else {
5349  // Since we do not read the full branch let's reset the read entry number
5350  // so that a subsequent read from TTreeFormula will properly load the full
5351  // object even if fQuickLoad is true.
5352  branchcount->TBranch::GetEntry(readentry);
5353  branchcount->ResetReadEntry();
5354  }
5355 
5356  size = ((TBranchElement*)branchcount)->GetNdata();
5357  // Reading the size as above is correct only when the branchcount
5358  // is of streamer type kCounter which require the underlying data
5359  // member to be signed integral type.
5360 
5361  TBranchElement* branch = (TBranchElement*) leaf->GetBranch();
5362  if (branch->GetAddress() == 0) {
5363  // Humm there is no space reserve to write the data,
5364  // the data member is likely 'removed' from the class
5365  // layout, so rather than crashing by accessing
5366  // random memory, make it clear we can't read it.
5367  size = 0;
5368  }
5369 
5370  // NOTE: could be sped up
5371  if (fHasMultipleVarDim[i]) {// info && info->GetVarDim()>=0) {
5372  info = (TFormLeafInfo* )fDataMembers.At(i);
5373  if (branch->GetBranchCount2()) R__LoadBranch(branch->GetBranchCount2(),readentry,fQuickLoad);
5374  else R__LoadBranch(branch,readentry,fQuickLoad);
5375 
5376  // Here we need to add the code to take in consideration the
5377  // double variable length
5378  // We fill up the array of sizes in the TLeafInfo:
5379  info->LoadSizes(branch);
5380  hasBranchCount2 = kTRUE;
5381  if (info->GetVirtVarDim()>=0) info->UpdateSizes(fManager->fVarDims[info->GetVirtVarDim()]);
5382 
5383  // Refresh the fCumulSizes[i] to have '1' for the
5384  // double variable dimensions
5385  Int_t vdim = info->GetVarDim();
5386  fCumulSizes[i][vdim] = fCumulSizes[i][vdim+1];
5387  for(Int_t k=vdim -1; k>=0; k--) {
5388  fCumulSizes[i][k] = fCumulSizes[i][k+1]*fFixedSizes[i][k];
5389  }
5390  // Update fCumulUsedSizes
5391  // UpdateMultiVarSizes(vdim,info,i)
5392  //Int_t fixed = fCumulSizes[i][vdim+1];
5393  //for(Int_t k=vdim - 1; k>=0; k++) {
5394  // Int_t fixed *= fFixedSizes[i][k];
5395  // for(Int_t l=0;l<size; l++) {
5396  // fCumulSizes[i][k] += info->GetSize(l) * fixed;
5397  //}
5398  }
5399  } else {
5400  Long64_t readentry = leaf->GetBranch()->GetTree()->GetReadEntry();
5401  if (readentry < 0) readentry=0;
5402  R__LoadBranch(branchcount,readentry,fQuickLoad);
5403  size = leaf->GetLen() / leaf->GetLenStatic();
5404  }
5405  if (hasBranchCount2) {
5406  // We assume that fCumulSizes[i][1] contains the product of the fixed sizes
5407  fNdata[i] = fCumulSizes[i][1] * ((TFormLeafInfo *)fDataMembers.At(i))->GetSumOfSizes();
5408  } else {
5409  fNdata[i] = size * fCumulSizes[i][1];
5410  }
5411  if (fIndexes[i][0]==-1) {
5412  // Case where the index is not specified AND the 1st dimension has a variable
5413  // size.
5414  if (fManager->fUsedSizes[0]==1 || (size<fManager->fUsedSizes[0]) ) fManager->fUsedSizes[0] = size;
5415  if (info && fIndexes[i][info->GetVarDim()]>=0) {
5416  for(Int_t j=0; j<size; j++) {
5417  if (fIndexes[i][info->GetVarDim()] >= info->GetSize(j)) {
5418  info->SetSize(j,0);
5421  } else if (fIndexes[i][info->GetVarDim()]>=0) {
5422  // There is an index and it is not too large
5423  info->SetSize(j,1);
5426  }
5427  }
5428  }
5429  } else if (fIndexes[i][0] >= size) {
5430  // unreacheable element requested:
5431  fManager->fUsedSizes[0] = 0;
5432  fNdata[i] = 0;
5433  outofbounds = kTRUE;
5434  } else if (hasBranchCount2) {
5435  TFormLeafInfo *info2;
5436  info2 = (TFormLeafInfo *)fDataMembers.At(i);
5437  if (fIndexes[i][0]<0
5438  || fIndexes[i][info2->GetVarDim()] >= info2->GetSize(fIndexes[i][0])) {
5439  // unreacheable element requested:
5440  fManager->fUsedSizes[0] = 0;
5441  fNdata[i] = 0;
5442  outofbounds = kTRUE;
5443  }
5444  }
5445  } else if (fLookupType[i]==kDataMember) {
5447  if (leafinfo->HasCounter()) {
5448  TBranch *branch = leaf->GetBranch();
5449  Long64_t readentry = branch->GetTree()->GetReadEntry();
5450  if (readentry < 0) readentry=0;
5451  R__LoadBranch(branch,readentry,fQuickLoad);
5452  size = (Int_t) leafinfo->GetCounterValue(leaf);
5453  if (fIndexes[i][0]==-1) {
5454  // Case where the index is not specified AND the 1st dimension has a variable
5455  // size.
5456  if (fManager->fUsedSizes[0]==1 || (size<fManager->fUsedSizes[0]) ) {
5457  fManager->fUsedSizes[0] = size;
5458  }
5459  } else if (fIndexes[i][0] >= size) {
5460  // unreacheable element requested:
5461  fManager->fUsedSizes[0] = 0;
5462  fNdata[i] = 0;
5463  outofbounds = kTRUE;
5464  } else {
5465  fNdata[i] = size*fCumulSizes[i][1];
5466  }
5467  Int_t vdim = leafinfo->GetVarDim();
5468  if (vdim>=0) {
5469  // Here we need to add the code to take in consideration the
5470  // double variable length
5471  // We fill up the array of sizes in the TLeafInfo:
5472  // here we can assume that branch is a TBranch element because the other style does NOT support this type
5473  // of complexity.
5474  leafinfo->LoadSizes(branch);
5475  hasBranchCount2 = kTRUE;
5476  if (fIndexes[i][0]==-1&&fIndexes[i][vdim] >= 0) {
5477  for(int z=0; z<size; ++z) {
5478  if (fIndexes[i][vdim] >= leafinfo->GetSize(z)) {
5479  leafinfo->SetSize(z,0);
5480  // --fManager->fUsedSizes[0];
5481  } else if (fIndexes[i][vdim] >= 0 ) {
5482  leafinfo->SetSize(z,1);
5483  }
5484  }
5485  }
5486  leafinfo->UpdateSizes(fManager->fVarDims[vdim]);
5487 
5488  // Refresh the fCumulSizes[i] to have '1' for the
5489  // double variable dimensions
5490  fCumulSizes[i][vdim] = fCumulSizes[i][vdim+1];
5491  for(Int_t k=vdim -1; k>=0; k--) {
5492  fCumulSizes[i][k] = fCumulSizes[i][k+1]*fFixedSizes[i][k];
5493  }
5494  fNdata[i] = fCumulSizes[i][1] * leafinfo->GetSumOfSizes();
5495  } else {
5496  fNdata[i] = size * fCumulSizes[i][1];
5497  }
5498  } else if (leafinfo->GetMultiplicity()==-1) {
5499  TBranch *branch = leaf->GetBranch();
5500  Long64_t readentry = branch->GetTree()->GetReadEntry();
5501  if (readentry < 0) readentry=0;
5502  R__LoadBranch(branch,readentry,fQuickLoad);
5503  if (leafinfo->GetNdata(leaf)==0) {
5504  outofbounds = kTRUE;
5505  }
5506  }
5507  }
5508  // However we allow several dimensions that virtually vary via the size of their
5509  // index variables. So we have code to recalculate fCumulUsedSizes.
5510  Int_t index;
5511  TFormLeafInfo * info = 0;
5512  if (fLookupType[i]!=kDirect) {
5513  info = (TFormLeafInfo *)fDataMembers.At(i);
5514  }
5515  for(Int_t k=0, virt_dim=0; k < fNdimensions[i]; k++) {
5516  if (fIndexes[i][k]<0) {
5517  if (fIndexes[i][k]==-2 && fManager->fVirtUsedSizes[virt_dim]<0) {
5518 
5519  // if fVirtUsedSize[virt_dim] is positive then VarIndexes[i][k]->GetNdata()
5520  // is always the same and has already been factored in fUsedSize[virt_dim]
5521  index = fVarIndexes[i][k]->GetNdata();
5522  if (index==1) {
5523  // We could either have a variable size array which is currently of size one
5524  // or a single element that might or not might not be present (and is currently present!)
5525  if (fVarIndexes[i][k]->GetManager()->GetMultiplicity()==1) {
5526  if (index<fManager->fUsedSizes[virt_dim]) fManager->fUsedSizes[virt_dim] = index;
5527  }
5528 
5529  } else if (fManager->fUsedSizes[virt_dim]==-fManager->fVirtUsedSizes[virt_dim] ||
5530  index<fManager->fUsedSizes[virt_dim]) {
5531  fManager->fUsedSizes[virt_dim] = index;
5532  }
5533 
5534  } else if (hasBranchCount2 && info && k==info->GetVarDim()) {
5535  // NOTE: We assume the indexing of variable sizes on the first index!
5536  if (fIndexes[i][0]>=0) {
5537  index = info->GetSize(fIndexes[i][0]);
5538  if (fManager->fUsedSizes[virt_dim]==1 || (index!=1 && index<fManager->fUsedSizes[virt_dim]) )
5539  fManager->fUsedSizes[virt_dim] = index;
5540  }
5541  }
5542  virt_dim++;
5543  } else if (hasBranchCount2 && info && k==info->GetVarDim()) {
5544 
5545  // nothing to do, at some point I thought this might be useful:
5546  // if (fIndexes[i][k]>=0) {
5547  // index = info->GetSize(fIndexes[i][k]);
5548  // if (fManager->fUsedSizes[virt_dim]==1 || (index!=1 && index<fManager->fUsedSizes[virt_dim]) )
5549  // fManager->fUsedSizes[virt_dim] = index;
5550  // virt_dim++;
5551  // }
5552 
5553  }
5554  }
5555  }
5556  return ! outofbounds;
5557 
5558 
5559 
5560 }
5561 
5563 {
5564  // Convert the fOper of a TTTreeFormula version fromVersion to the current in memory version
5565 
5566  enum { kOldAlias = /*ROOT::v5::TFormula::kVariable*/ 100000+10000+1,
5567  kOldAliasString = kOldAlias+1,
5568  kOldAlternate = kOldAlias+2,
5569  kOldAlternateString = kOldAliasString+2
5570  };
5571 
5572  for (int k=0; k<fNoper; k++) {
5573  // First hide from ROOT::v5::TFormula convertion
5574 
5575  Int_t action = GetOper()[k];
5576 
5577  switch (action) {
5578 
5579  case kOldAlias: GetOper()[k] = -kOldAlias; break;
5580  case kOldAliasString: GetOper()[k] = -kOldAliasString; break;
5581  case kOldAlternate: GetOper()[k] = -kOldAlternate; break;
5582  case kOldAlternateString: GetOper()[k] = -kOldAlternateString; break;
5583  }
5584  }
5585 
5586  ROOT::v5::TFormula::Convert(oldversion);
5587 
5588  for (int i=0,offset=0; i<fNoper; i++) {
5589  Int_t action = GetOper()[i+offset];
5590 
5591  switch (action) {
5592  case -kOldAlias: SetAction(i, kAlias, 0); break;
5593  case -kOldAliasString: SetAction(i, kAliasString, 0); break;
5594  case -kOldAlternate: SetAction(i, kAlternate, 0); break;
5595  case -kOldAlternateString: SetAction(i, kAlternateString, 0); break;
5596  }
5597  }
5598 
5599 }
5600 
5601 ////////////////////////////////////////////////////////////////////////////////
5602 /// Convert the underlying lookup method from the direct technique
5603 /// (dereferencing the address held by the branch) to the method using
5604 /// TFormLeafInfo. This is in particular usefull in the case where we
5605 /// need to append an additional TFormLeafInfo (for example to call a
5606 /// method).
5607 /// Return false if the switch was unsuccessfull (basically in the
5608 /// case of an old style split tree).
5609 
5611 {
5612  TFormLeafInfo *last = 0;
5613  TLeaf *leaf = (TLeaf*)fLeaves.At(code);
5614  if (!leaf) return kFALSE;
5615 
5616  if (fLookupType[code]==kDirect) {
5617  if (leaf->InheritsFrom(TLeafElement::Class())) {
5618  TBranchElement * br = (TBranchElement*)leaf->GetBranch();
5619  if (br->GetType()==31) {
5620  // sub branch of a TClonesArray
5621  TStreamerInfo *info = br->GetInfo();
5622  TClass* cl = info->GetClass();
5623  TStreamerElement *element = (TStreamerElement *)info->GetElement(br->GetID());
5624  TFormLeafInfo* clonesinfo = new TFormLeafInfoClones(cl, 0, element, kTRUE);
5625  Int_t offset;
5626  info->GetStreamerElement(element->GetName(),offset);
5627  clonesinfo->fNext = new TFormLeafInfo(cl,offset+br->GetOffset(),element);
5628  last = clonesinfo->fNext;
5629  fDataMembers.AddAtAndExpand(clonesinfo,code);
5630  fLookupType[code]=kDataMember;
5631 
5632  } else if (br->GetType()==41) {
5633  // sub branch of a Collection
5634 
5635  TBranchElement *count = br->GetBranchCount();
5636  TFormLeafInfo* collectioninfo;
5637  if ( count->GetID() >= 0 ) {
5638  TStreamerElement *collectionElement =
5639  (TStreamerElement *)count->GetInfo()->GetElement(count->GetID());
5640  TClass *collectionCl = collectionElement->GetClassPointer();
5641 
5642  collectioninfo =
5643  new TFormLeafInfoCollection(collectionCl, 0, collectionElement, kTRUE);
5644  } else {
5645  TClass *collectionCl = TClass::GetClass(count->GetClassName());
5646  collectioninfo =
5647  new TFormLeafInfoCollection(collectionCl, 0, collectionCl, kTRUE);
5648  }
5649 
5650  TStreamerInfo *info = br->GetInfo();
5651  TClass* cl = info->GetClass();
5652  TStreamerElement *element = (TStreamerElement *)info->GetElement(br->GetID());
5653  Int_t offset;
5654  info->GetStreamerElement(element->GetName(),offset);
5655  collectioninfo->fNext = new TFormLeafInfo(cl,offset+br->GetOffset(),element);
5656  last = collectioninfo->fNext;
5657  fDataMembers.AddAtAndExpand(collectioninfo,code);
5658  fLookupType[code]=kDataMember;
5659 
5660  } else if (br->GetID()<0) {
5661  return kFALSE;
5662  } else {
5663  last = new TFormLeafInfoDirect(br);
5664  fDataMembers.AddAtAndExpand(last,code);
5665  fLookupType[code]=kDataMember;
5666  }
5667  } else {
5668  //last = new TFormLeafInfoDirect(br);
5669  //fDataMembers.AddAtAndExpand(last,code);
5670  //fLookupType[code]=kDataMember;
5671  return kFALSE;
5672  }
5673  }
5674  return kTRUE;
5675 }
virtual Int_t GetLen() const
Return the number of effective elements of this leaf.
Definition: TLeaf.cxx:276
Describe Streamer information for one class version.
Definition: TStreamerInfo.h:47
Double_t ACosH(Double_t)
Definition: TMath.cxx:80
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
Int_t fCumulUsedSizes[kMAXFORMDIM+1]
Last value calculated by GetNdata.
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
virtual Bool_t IsReference() const
TVirtualRefProxy * GetReferenceProxy() const
Definition: TClass.h:437
const Int_t kMAXFORMDIM
Definition: TTreeFormula.h:49
An array of TObjects.
Definition: TObjArray.h:39
A small helper class to implement the following of reference objects stored in a TTree.
virtual Bool_t IsLeafInteger(Int_t code) const
Return TRUE if the leaf corresponding to code is short, int or unsigned short, int When a leaf is of ...
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void Remove(TTreeFormula *)
Remove a formula from this manager.
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:405
virtual void SetObjectX(TObject *obj)
Set the X object (and delete the previous one if any).
Definition: TCutG.cxx:381
A small helper class to implement reading a data member on a TClonesArray object stored in a TTree...
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
virtual Bool_t IsOnTerminalBranch() const
Return true if this leaf is does not have any sub-branch/leaf.
Double_t TanH(Double_t)
Definition: TMath.h:436
virtual const char * GetFriendAlias(TTree *) const
If the 'tree' is a friend, this method returns its alias name.
Definition: TTree.cxx:5371
virtual void AddOffset(Int_t offset, TStreamerElement *element)
Increase the offset of this element.
long long Long64_t
Definition: RtypesCore.h:69
Int_t GetID() const
virtual Bool_t IsUnsigned() const
Definition: TLeaf.h:91
Bool_t IsReading() const
Definition: TBuffer.h:83
Double_t Log(Double_t x)
Definition: TMath.h:526
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:487
short Version_t
Definition: RtypesCore.h:61
Int_t GetNdata(TLeaf *leaf)
A Branch for the case of an object.
Definition: TBranchObject.h:28
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
Ssiz_t Length() const
Definition: TString.h:390
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:528
TObject * GetObjectY() const
Definition: TCutG.h:49
static const EReturnType kOther
Definition: TMethodCall.h:50
TObjArray fBranches
Definition: TTreeFormula.h:106
return c
const char * current
Definition: demos.C:12
virtual Bool_t Sync()
Synchronize all the formulae.
virtual TClass * GetValueClass() const =0
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:329
tuple offset
Definition: tree.py:93
virtual Long64_t GetReadEntry() const
Definition: TTree.h:428
A small helper class to implement reading a data member on an object stored in a TTree.
TTreeFormulaManager * GetManager() const
Definition: TTreeFormula.h:184
virtual void * GetValuePointer(TLeaf *leaf, Int_t instance=0)
returns the address of the value pointed to by the serie of TFormLeafInfo.
TBranchElement * GetBranchCount2() const
virtual Int_t DefinedVariable(TString &variable, Int_t &action)
Check if name is in the list of Tree/Branch leaves.
A TLeaf for a general object derived from TObject.
Definition: TLeafObject.h:35
char * GetObject() const
Return a pointer to our object.
Int_t GetActionParam(Int_t code) const
Definition: TFormula.h:111
virtual Int_t IsInside(Double_t x, Double_t y) const
Return 1 if the point (x,y) is inside the polygon defined by the graph vertices 0 otherwise...
Definition: TGraph.cxx:1771
#define lenfunc
Definition: PyROOT.h:163
virtual TLeaf * GetLeaf(Int_t n) const
Return leaf corresponding to serial number n.
unsigned short UShort_t
Definition: RtypesCore.h:36
const char * GetReturnTypeName() const
Get full type description of function return type, e,g.: "class TDirectory*".
Definition: TFunction.cxx:140
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
virtual Int_t Compile(const char *expression="")
virtual void EnableMultiVarDims()
Set the manager as handling a formula with multiple variable dimensions.
virtual TLeaf * FindLeaf(const char *name)
Find the leaf corresponding to the name 'searchname'.
Definition: TBranch.cxx:940
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
#define R__ASSERT(e)
Definition: TError.h:98
#define gROOT
Definition: TROOT.h:344
TArrayI * fVarDims[kMAXFORMDIM+1]
void DefineDimensions(Int_t code, Int_t size, TFormLeafInfoMultiVarDim *info, Int_t &virt_dim)
This method is used internally to decode the dimensions of the variables.
static const EReturnType kLong
Definition: TMethodCall.h:47
A small helper class to implement casting an object to a different type (equivalent to dynamic_cast) ...
Basic string class.
Definition: TString.h:137
Int_t fNdimensions[kMAXCODES]
If true, the current entry has not been loaded yet.
Definition: TTreeFormula.h:110
TFormLeafInfo * fNext
Definition: TFormLeafInfo.h:73
virtual Int_t GetNdim() const
Definition: TFormula.h:243
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Bool_t IsString() const
Return TRUE if the formula is a string.
const Bool_t kFALSE
Definition: Rtypes.h:92
#define gInterpreter
Definition: TInterpreter.h:502
TBranch * GetBranch() const
Definition: TLeaf.h:70
const Int_t kTFOperMask
Definition: TFormula.h:38
virtual Bool_t Update()
We reloading all cached information in case the underlying class information has changed (for example...
T GetConstant(Int_t k)
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
Int_t fMultiplicity
Definition: TTreeFormula.h:97
virtual void * EvalObject(Int_t i=0)
Evaluate this treeformula.
virtual TVirtualIndex * GetTreeIndex() const
Definition: TTree.h:437
void SetAction(Int_t code, Int_t value, Int_t param=0)
Definition: TFormula.h:113
Int_t GetN() const
Definition: TGraph.h:132
virtual EDataType GetType() const =0
virtual void Add(TTreeFormula *)
Add a new formula to the list of formulas managed The manager of the formula will be changed and the ...
TFormLeafInfo * GetLeafInfo(Int_t code) const
Return DataMember corresponding to code.
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition: TClass.cxx:4037
Int_t fNdata[kMAXCODES]
Definition: TTreeFormula.h:94
virtual void * GetValuePointerFromMethod(Int_t i, TLeaf *leaf) const
Return result of a leafobject method.
TFile * f
A helper class to implement reading a data member on a variable size array inside a TClonesArray obje...
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:501
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
Int_t fVirtUsedSizes[kMAXFORMDIM+1]
void Init(const char *name, const char *formula)
Initialiation called from the constructors.
Int_t GetElementOffset(Int_t id) const
virtual Int_t GetMultiplicity() const
static const EReturnType kString
Definition: TMethodCall.h:49
virtual TClass * GetClass() const
Get the class of the underlying data.
TBranchElement * GetBranchCount() const
TTree * T
const char * Data() const
Definition: TString.h:349
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:396
Bool_t LoadCurrentDim()
Calculate the actual dimension for the current entry.
virtual Bool_t IsString() const
Return true if the underlying data is a string.
virtual void CancelDimension(Int_t virt_dim)
Cancel a dimension.
TClass * GetClass(T *)
Definition: TClass.h:554
TMethodCall * GetMethodCall(Int_t code) const
Return methodcall corresponding to code.
void Convert(UInt_t fromVersion)
Graphical cut class.
Definition: TCutG.h:29
TObjArray fLeafNames
List of TTreeFormula for each alias used.
Definition: TTreeFormula.h:105
TObjArray fFunctions
Definition: TFormula.h:91
virtual Bool_t SwitchToFormLeafInfo(Int_t code)
Convert the underlying lookup method from the direct technique (dereferencing the address held by the...
Int_t GetOffset() const
Definition: TBranch.h:167
virtual Bool_t IsOnTerminalBranch() const
Definition: TLeaf.h:89
TObjArray fExternalCuts
List of leaf method calls.
Definition: TTreeFormula.h:103
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
void Class()
Definition: Class.C:29
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
void ResetDimensions()
Populate the TTreeFormulaManager with the dimension information.
Int_t RegisterDimensions(Int_t code, Int_t size, TFormLeafInfoMultiVarDim *multidim=0)
This method stores the dimension information for later usage.
int d
Definition: tornado.py:11
Double_t ASinH(Double_t)
Definition: TMath.cxx:67
Double_t Log10(Double_t x)
Definition: TMath.h:529
virtual TBranch * FindBranch(const char *name)
Find the immediate sub-branch with passed name.
Definition: TBranch.cxx:894
virtual Bool_t IsaPointer() const
#define TT_EVAL_INIT
const Int_t kDoNotProcess
Definition: TBranch.h:52
TString & Append(const char *cs)
Definition: TString.h:492
std::vector< std::vector< double > > Data
static const EReturnType kDouble
Definition: TMethodCall.h:48
virtual Double_t GetValueFromMethod(Int_t i, TLeaf *leaf) const
Return result of a leafobject method.
Long64_t GetEntries() const
Definition: TBranch.h:182
virtual void LoadSizes(TBranch *branch)
Load the current array sizes.
TObject * GetObjectX() const
Definition: TCutG.h:48
TObjArray * GetListOfBranches()
Definition: TBranch.h:177
virtual Bool_t IsInteger() const
Return true if the underlying data is an integral value.
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:454
const Int_t kMAXFOUND
Definition: TFormula.h:37
TClass * GetClass() const
virtual TClass * EvalClass() const
Evaluate the class of this treeformula.
Method or function calling interface.
Definition: TMethodCall.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
Float_t z[5]
Definition: Ifit.C:16
void Set(Int_t n)
Set size of this array to n ints.
Definition: TArrayI.cxx:105
void LoadBranches()
Make sure that all the branches have been loaded properly.
void AddAt(Int_t c, Int_t i)
Add Int_t c at position i. Check for out of bounds.
Definition: TArrayI.cxx:93
virtual const char * EvalStringInstance(Int_t i=0)
Eval the instance as a string.
Int_t GetArrayDim() const
#define TREE_EVAL_INIT_LOOP
Used to pass a selection expression to the Tree drawing routine.
Definition: TTreeFormula.h:64
A doubly linked list.
Definition: TList.h:47
static const char * what
Definition: stlLoader.cc:6
virtual void AddVarDims(Int_t virt_dim)
Add a variable dimension.
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:91
virtual Bool_t HasCounter() const =0
UChar_t fHasMultipleVarDim[kMAXCODES]
Definition: TTreeFormula.h:112
virtual char * GetAddress() const
Definition: TBranch.h:146
A small helper class to implement reading a data member on a variable size array inside a TClonesArra...
Used to return the size of a collection.
virtual Bool_t StringToNumber(Int_t code)
Try to 'demote' a string into an array bytes.
virtual const char * GetTypeName() const
Definition: TLeaf.h:81
Double_t * GetX() const
Definition: TGraph.h:139
TObjArray fDataMembers
List of leaf used in this formula.
Definition: TTreeFormula.h:101
TClass * fClass
Definition: TFormLeafInfo.h:66
virtual void SetSecondaryIndex(Int_t index)
Set the primary index value.
virtual Bool_t IsString(Int_t oper) const
Return true if the expression at the index 'oper' is to be treated as as string.
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TObjArray.cxx:222
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void Convert(UInt_t fromVersion)
Class to manage histogram axis.
Definition: TAxis.h:36
virtual void SetupAddresses()
If the branch address is not set, we set all addresses starting with the top level parent branch...
const Int_t kMAXCODES
Definition: TTreeFormula.h:48
A small helper class to implement reading a data member on a variable size array inside a TClonesArra...
TVirtualStreamerInfo * GetStreamerInfo(Int_t version=0) const
returns a pointer to the TVirtualStreamerInfo object for version If the object does not exist...
Definition: TClass.cxx:4256
TStreamerElement * fElement
Offset of the data pointed inside the class fClass.
Definition: TFormLeafInfo.h:69
A small helper class to implement reading a data member by following a pointer inside a branch of TTr...
Int_t ParseWithLeaf(TLeaf *leaf, const char *expression, Bool_t final, UInt_t paran_level, TObjArray &castqueue, Bool_t useLeafCollectionObject, const char *fullExpression)
Decompose 'expression' as pointing to something inside the leaf Returns:
Int_t GetMaxIndex(Int_t i) const
ClassInfo_t * GetClassInfo() const
Definition: TClass.h:390
The F O R M U L A class.
Definition: TFormula.h:89
virtual void SetSize(Int_t index, Int_t val)
Set the current size of the arrays.
Double_t * fConst
Definition: TFormula.h:88
virtual void SetObjectY(TObject *obj)
Set the Y object (and delete the previous one if any).
Definition: TCutG.cxx:390
TBranch * GetMother() const
Get our top-level parent branch in the tree.
Definition: TBranch.cxx:1533
Int_t fNcodes
This caches the physical number of element in the leaf or datamember.
Definition: TTreeFormula.h:95
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
TMarker * m
Definition: textangle.C:8
TObjArray fAliases
List of TCutG and TEntryList used in the formula.
Definition: TTreeFormula.h:104
char * Form(const char *fmt,...)
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
Definition: TBranchObject.h:46
virtual void UpdateFormulaLeaves()
This function is called TTreePlayer::UpdateFormulaLeaves, itself called by TChain::LoadTree when a ne...
Int_t fCumulSizes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:115
bool first
Definition: line3Dfit.C:48
short Short_t
Definition: RtypesCore.h:35
TLine * l
Definition: textangle.C:4
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
long double LongDouble_t
Definition: RtypesCore.h:57
virtual Int_t GetSize(Int_t index)
For the current entry, and the value 'index' for the main array, return the size of the secondary var...
Double_t ACos(Double_t)
Definition: TMath.h:445
virtual void UpdateSizes(TArrayI *garr)
Set the current sizes of the arrays.
const UChar_t kTFOperShift
Definition: TFormula.h:39
std::vector< std::string > fAliasesUsed
list of dimension setups, for delayed creation of the dimension information.
Definition: TTreeFormula.h:125
virtual void SetPrimaryIndex(Int_t index)
Set the primary index value.
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Long64_t entry
virtual Int_t Contains(Long64_t entry, TTree *tree=0)
Definition: TEntryList.cxx:517
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
Short_t GetAction(Int_t code) const
Definition: TFormula.h:110
virtual void AddAt(TObject *obj, Int_t idx)
Add object at position ids.
Definition: TObjArray.cxx:239
T FindMax(TTreeFormula *arr)
TTree * GetTree() const
Definition: TBranch.h:183
A TLeaf for the general case when using the branches created via a TStreamerInfo (i.e.
Definition: TLeafElement.h:34
virtual TLeaf * GetLeafCount() const
Definition: TLeaf.h:71
A Branch for the case of an object.
Double_t Cos(Double_t)
Definition: TMath.h:424
Int_t GetSize() const
Definition: TArray.h:49
TAxis * fAxis
Definition: TTreeFormula.h:119
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:264
Bool_t fHasCast
Definition: TTreeFormula.h:96
TString & Remove(Ssiz_t pos)
Definition: TString.h:616
long Long_t
Definition: RtypesCore.h:50
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
int Ssiz_t
Definition: RtypesCore.h:63
virtual Int_t GetNdata()
Return number of available instances in the formula.
Int_t GetRealInstance(Int_t instance, Int_t codeindex)
Now let calculate what physical instance we really need.
virtual TFormLeafInfo * DeepCopy() const
Make a complete copy of this FormLeafInfo and all its content.
virtual Int_t GetVirtVarDim()
Return the virtual index (for this expression) of the dimension which varies for each elements of an ...
Int_t fUsedSizes[kMAXFORMDIM+1]
const char * GetCountName() const
TFunction * GetMethod()
Returns the TMethod describing the method to be executed.
Bool_t BranchHasMethod(TLeaf *leaf, TBranch *branch, const char *method, const char *params, Long64_t readentry) const
Return the leaf (if any) of the tree with contains an object of a class having a method which has the...
A small helper class to implement reading from the containing TTree object itself.
Double_t Exp(Double_t x)
Definition: TMath.h:495
tuple tree
Definition: tree.py:24
TStreamerElement * GetStreamerElement(const char *datamember, Int_t &offset) const
Return the StreamerElement of "datamember" inside our class or any of its base classes.
LongDouble_t * fConstLD
List of aliases used during the parsing of the expression.
Definition: TTreeFormula.h:127
virtual Bool_t IsLeafString(Int_t code) const
Return TRUE if the leaf or data member corresponding to code is a string.
virtual void SetBranch(TBranch *br)
void ResetParam()
Reset parameter list. To be used before the first call the SetParam().
const Int_t * GetArray() const
Definition: TArrayI.h:45
TObjArray fLeaves
Definition: TTreeFormula.h:100
double Double_t
Definition: RtypesCore.h:55
Int_t DefineAlternate(const char *expression)
This method check for treat the case where expression contains $Atl and load up both fAliases and fEx...
virtual TObjArray * GetElements() const =0
virtual TClass * GetClassPointer() const
Returns a pointer to the TClass of this element.
Bool_t fDidBooleanOptimization
pointer to histogram axis if this is a string
Definition: TTreeFormula.h:120
Int_t fIndexes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:116
TClass * GetClass() const
Definition: TClonesArray.h:57
Describe directory structure in memory.
Definition: TDirectory.h:44
A small helper class to implement reading a numerical value inside a collection.
Int_t At(Int_t i) const
Definition: TArrayI.h:81
TClass * GetClass() const
Definition: TLeafObject.h:50
This class is a small helper class to implement reading a data member on an object stored in a TTree...
Definition: TFormLeafInfo.h:53
int type
Definition: TGX11.cxx:120
unsigned long long ULong64_t
Definition: RtypesCore.h:70
Int_t GetMultiplicity()
Reminder of the meaning of fMultiplicity:
TNamed()
Definition: TNamed.h:40
unsigned long ULong_t
Definition: RtypesCore.h:51
Int_t GetNewType() const
TObject * GetObject() const
Definition: TLeafObject.h:52
T FindMin(TTreeFormula *arr)
virtual TTree * GetTree() const
Definition: TTree.h:436
virtual TClass * GetValueClass(TLeaf *from)
Access to the value class of the reference proxy.
TObjArray * GetListOfLeaves()
Definition: TBranch.h:178
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any. ...
T EvalInstance(Int_t i=0, const char *stringStack[]=0)
Evaluate this treeformula.
ClassImp(TTreeFormula) inline static void R__LoadBranch(TBranch *br
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2801
virtual Int_t GetSumOfSizes()
Total all the elements that are available for the current entry for the secondary variable dimension...
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
TFormLeafInfo * fCounter2
TVirtualCollectionProxy * GetCollectionProxy() const
Return the proxy describing the collection (if any).
Definition: TClass.cxx:2730
#define name(a, b)
Definition: linkTestLib0.cpp:5
TClass * GetClass() const
T Summing(TTreeFormula *sum)
Mother of all ROOT objects.
Definition: TObject.h:58
virtual Bool_t IsString(Int_t oper) const
TTree * fTree
Definition: TTreeFormula.h:92
Global functions class (global functions are obtained from CINT).
Definition: TFunction.h:30
Bool_t fQuickLoad
List of branches to read. Similar to fLeaces but duplicates are zeroed out.
Definition: TTreeFormula.h:107
TClassRef is used to implement a permanent reference to a TClass object.
Definition: TClassRef.h:33
char Char_t
Definition: RtypesCore.h:29
TTreeFormula * fVarIndexes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:117
TObject * Last() const
Return the object in the last filled slot. Returns 0 if no entries.
Definition: TObjArray.cxx:479
An array of clone (identical) objects.
Definition: TClonesArray.h:32
TTreeFormulaManager * fManager
True if we executed one boolean optimization since the last time instance number 0 was evaluated...
Definition: TTreeFormula.h:121
Bool_t axis
Definition: geodemo.C:37
Int_t GetType() const
virtual Bool_t HasCounter() const
Return true if any of underlying data has a array size counter.
virtual TClass * GetValueClass(void *data) const =0
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:433
void Streamer(TBuffer &b, const TClass *onfile_class)
virtual ~TTreeFormula()
Tree Formula default destructor.
virtual char * GetAddress() const
Get the branch address.
virtual void * GetValuePointer() const
Definition: TLeaf.h:80
virtual void Add(TObject *obj)
Definition: TList.h:81
Int_t * fLookupType
Definition: TTreeFormula.h:99
void Execute(const char *, const char *, int *=0)
Execute method on this object with the given parameter string, e.g.
Definition: TMethodCall.h:68
friend class TTreeFormulaManager
Definition: TTreeFormula.h:66
virtual Int_t GetLenStatic() const
Definition: TLeaf.h:74
#define TREE_EVAL_INIT
Double_t Sin(Double_t)
Definition: TMath.h:421
const char * GetVarY() const
Definition: TCutG.h:51
TList * fDimensionSetup
The dimension coordinator.
Definition: TTreeFormula.h:124
void SetParam(Long_t l)
Add a long method parameter.
Double_t ASin(Double_t)
Definition: TMath.h:439
void * GetObject() const
TStreamerElement * GetElement(Int_t id) const
const Int_t kMaxLen
TObjArray fMethods
List of leaf data members.
Definition: TTreeFormula.h:102
A small helper class to implement reading a data member on a generic collection object stored in a TT...
TString * fExpr
Definition: TFormula.h:84
Int_t * GetOper() const
Definition: TFormula.h:109
virtual char * PrintValue(Int_t mode=0) const
Return value of variable as a string.
TLeaf * GetLeafWithDatamember(const char *topchoice, const char *nextchice, Long64_t readentry) const
Return the leaf (if any) which contains an object containing a data member which has the name provide...
A TTree object has a header with a name and a title.
Definition: TTree.h:98
#define gDirectory
Definition: TDirectory.h:221
Asmall helper class to implement executing a method of an object stored in a TTree.
Double_t SinH(Double_t)
Definition: TMath.h:430
virtual void * GetLocalValuePointer(TLeaf *leaf, Int_t instance=0)
returns the address of the value pointed to by the TFormLeafInfo.
void ResetBit(UInt_t f)
Definition: TObject.h:172
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4498
virtual Int_t GetNdata(Bool_t forceLoadDim=kFALSE)
Return number of available instances in the formulas.
Bool_t HasDataMemberInfo() const
Definition: TClass.h:370
TFormLeafInfo * fCounter
Descriptor of the data pointed to.
Definition: TFormLeafInfo.h:72
virtual void ResetReadEntry()
Definition: TBranch.h:198
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
A TTree is a list of TBranches.
Definition: TBranch.h:58
A small helper class to implement reading a data member on a TClonesArray object stored in a TTree...
#define TT_EVAL_INIT_LOOP
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual Int_t GetMultiplicity() const
Definition: TTreeFormula.h:186
TObject * obj
A small helper class to implement reading a data member on a variable size array inside a TClonesArra...
virtual Int_t GetCounterValue(TLeaf *leaf)
Return the size of the underlying array for the current entry in the TTree.
float value
Definition: math.cpp:443
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:27
virtual Int_t GetVarDim()
Return the index of the dimension which varies for each elements of an enclosing array (typically a T...
static char * skip(char **buf, const char *delimiters)
Definition: civetweb.c:1014
Int_t FindLeafForExpression(const char *expression, TLeaf *&leaf, TString &leftover, Bool_t &final, UInt_t &paran_level, TObjArray &castqueue, std::vector< std::string > &aliasUsed, Bool_t &useLeafCollectionObject, const char *fullExpression)
Look for the leaf corresponding to the start of expression.
const Int_t n
Definition: legend1.C:16
Double_t ATanH(Double_t)
Definition: TMath.cxx:93
Double_t CosH(Double_t)
Definition: TMath.h:433
Int_t fFixedSizes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:111
Bool_t fNeedLoading
If true, branch GetEntry is only called when the entry number changes.
Definition: TTreeFormula.h:108
Double_t Tan(Double_t)
Definition: TMath.h:427
Bool_t IsValid() const
Return true if the method call has been properly initialized and is usable.
Int_t GetStreamerType() const
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition: TString.cxx:453
virtual void ResetLoading()
Tell the formula that we are going to request a new entry.
EReturnType ReturnType()
Returns the return type of the method.
virtual void UpdateUsedSize(Int_t &virt_dim, Int_t vsize)
Reload the array sizes.
Int_t fCodes[kMAXCODES]
pointer to Tree
Definition: TTreeFormula.h:93
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
const char * GetVarX() const
Definition: TCutG.h:50
virtual Bool_t IsInteger(Bool_t fast=kTRUE) const
Return TRUE if the formula corresponds to one single Tree leaf and this leaf is short, int or unsigned short, int When a leaf is of type integer or string, the generated histogram is forced to have an integer bin width.
virtual void SetAxis(TAxis *axis=0)
Set the axis (in particular get the type).
Double_t ATan(Double_t)
Definition: TMath.h:451
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904