ROOT  6.06/09
Reference Guide
TTreeFormula.h
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 // ---------------------------------- TreeFormula.h
12 
13 #ifndef ROOT_TTreeFormula
14 #define ROOT_TTreeFormula
15 
16 
17 
18 //////////////////////////////////////////////////////////////////////////
19 // //
20 // TTreeFormula //
21 // //
22 // The Tree formula class //
23 // //
24 //////////////////////////////////////////////////////////////////////////
25 
26 #ifndef ROOT_v5_TFormula
27 #include "v5/TFormula.h"
28 #endif
29 
30 #ifndef ROOT_TLeaf
31 #include "TLeaf.h"
32 #endif
33 
34 #ifndef ROOT_TObjArray
35 #include "TObjArray.h"
36 #endif
37 
38 #include <string>
39 #include <vector>
40 
41 #ifdef R__OLDHPACC
42 namespace std {
43  using ::string;
44  using ::vector;
45 }
46 #endif
47 
48 const Int_t kMAXCODES = kMAXFOUND; // must be the same as kMAXFOUND in TFormulaOld
49 const Int_t kMAXFORMDIM = 5; // Maximum number of array dimensions support in TTreeFormula
50 
51 class TTree;
52 class TArrayI;
53 class TMethodCall;
54 class TLeafObject;
55 class TDataMember;
56 class TStreamerElement;
58 class TFormLeafInfo;
59 class TBranchElement;
60 class TAxis;
62 
63 
65 
66 friend class TTreeFormulaManager;
67 
68 protected:
69  enum {
71  kMissingLeaf = BIT(15), // true if some of the needed leaves are missing in the current TTree
72  kIsInteger = BIT(17), // true if the branch contains an integer variable
73  kNeedEntries = BIT(18) // true if the formula uses Entries$
74  };
75  enum {
82 
83  };
84  enum {
85  kAlias = 200,
86  kAliasString = 201,
87  kAlternate = 202,
89  kMinIf = 204,
90  kMaxIf = 205
91  };
92 
93  TTree *fTree; //! pointer to Tree
94  Int_t fCodes[kMAXCODES]; // List of leaf numbers referenced in formula
95  Int_t fNdata[kMAXCODES]; //! This caches the physical number of element in the leaf or datamember.
96  Int_t fNcodes; // Number of leaves referenced in formula
97  Bool_t fHasCast; // Record whether the formula contain a cast operation or not
98  Int_t fMultiplicity; // Indicator of the variability of the formula
99  Int_t fNindex; // Size of fIndex
100  Int_t *fLookupType; //[fNindex] array indicating how each leaf should be looked-up
101  TObjArray fLeaves; //! List of leaf used in this formula.
102  TObjArray fDataMembers; //! List of leaf data members
103  TObjArray fMethods; //! List of leaf method calls
104  TObjArray fExternalCuts; //! List of TCutG and TEntryList used in the formula
105  TObjArray fAliases; //! List of TTreeFormula for each alias used.
106  TObjArray fLeafNames; // List of TNamed describing leaves
107  TObjArray fBranches; //! List of branches to read. Similar to fLeaces but duplicates are zeroed out.
108  Bool_t fQuickLoad; //! If true, branch GetEntry is only called when the entry number changes.
109  Bool_t fNeedLoading; //! If true, the current entry has not been loaded yet.
110 
111  Int_t fNdimensions[kMAXCODES]; //Number of array dimensions in each leaf
112  Int_t fFixedSizes[kMAXCODES][kMAXFORMDIM]; //Physical sizes of lower dimensions for each leaf
113  UChar_t fHasMultipleVarDim[kMAXCODES]; //True if the corresponding variable is an array with more than one variable dimension.
114 
115  //the next line should have a mutable in front. See GetNdata()
116  Int_t fCumulSizes[kMAXCODES][kMAXFORMDIM]; //Accumulated sizes of lower dimensions for each leaf after variable dimensions has been calculated
117  Int_t fIndexes[kMAXCODES][kMAXFORMDIM]; //Index of array selected by user for each leaf
118  TTreeFormula *fVarIndexes[kMAXCODES][kMAXFORMDIM]; //Pointer to a variable index.
119 
120  TAxis *fAxis; //! pointer to histogram axis if this is a string
121  Bool_t fDidBooleanOptimization; //! True if we executed one boolean optimization since the last time instance number 0 was evaluated
122  TTreeFormulaManager *fManager; //! The dimension coordinator.
123 
124  // Helper members and function used during the construction and parsing
125  TList *fDimensionSetup; //! list of dimension setups, for delayed creation of the dimension information.
126  std::vector<std::string> fAliasesUsed; //! List of aliases used during the parsing of the expression.
127 
128  LongDouble_t* fConstLD; // local version of fConsts able to store bigger numbers
129 
130  TTreeFormula(const char *name, const char *formula, TTree *tree, const std::vector<std::string>& aliases);
131  void Init(const char *name, const char *formula);
132  Bool_t BranchHasMethod(TLeaf* leaf, TBranch* branch, const char* method,const char* params, Long64_t readentry) const;
133  Int_t DefineAlternate(const char* expression);
134  void DefineDimensions(Int_t code, Int_t size, TFormLeafInfoMultiVarDim * info, Int_t& virt_dim);
135  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);
136  TLeaf* GetLeafWithDatamember(const char* topchoice, const char* nextchice, Long64_t readentry) const;
137  Int_t ParseWithLeaf(TLeaf *leaf, const char *expression, Bool_t final, UInt_t paran_level, TObjArray &castqueue, Bool_t useLeafCollectionObject, const char *fullExpression);
138  Int_t RegisterDimensions(Int_t code, Int_t size, TFormLeafInfoMultiVarDim * multidim = 0);
140  Int_t RegisterDimensions(Int_t code, TFormLeafInfo *info, TFormLeafInfo *maininfo, Bool_t useCollectionObject);
141  Int_t RegisterDimensions(Int_t code, TLeaf *leaf);
142  Int_t RegisterDimensions(const char *size, Int_t code);
143 
144  virtual Double_t GetValueFromMethod(Int_t i, TLeaf *leaf) const;
145  virtual void* GetValuePointerFromMethod(Int_t i, TLeaf *leaf) const;
146  Int_t GetRealInstance(Int_t instance, Int_t codeindex);
147 
148  void LoadBranches();
150  void ResetDimensions();
151 
152  virtual TClass* EvalClass(Int_t oper) const;
153  virtual Bool_t IsLeafInteger(Int_t code) const;
154  virtual Bool_t IsString(Int_t oper) const;
155  virtual Bool_t IsLeafString(Int_t code) const;
156  virtual Bool_t SwitchToFormLeafInfo(Int_t code);
157  virtual Bool_t StringToNumber(Int_t code);
158 
159  void Convert(UInt_t fromVersion);
160 
161 private:
162  // Not implemented yet
163  TTreeFormula(const TTreeFormula&);
165 
166  template<typename T> T GetConstant(Int_t k);
167 
168 public:
169  TTreeFormula();
170  TTreeFormula(const char *name,const char *formula, TTree *tree);
171  virtual ~TTreeFormula();
172 
173  virtual Int_t DefinedVariable(TString &variable, Int_t &action);
174  virtual TClass* EvalClass() const;
175 
176  template<typename T> T EvalInstance(Int_t i=0, const char *stringStack[]=0);
177  virtual Double_t EvalInstance(Int_t i=0, const char *stringStack[]=0) {return EvalInstance<Double_t>(i, stringStack); }
178  virtual Long64_t EvalInstance64(Int_t i=0, const char *stringStack[]=0) {return EvalInstance<Long64_t>(i, stringStack); }
179  virtual LongDouble_t EvalInstanceLD(Int_t i=0, const char *stringStack[]=0) {return EvalInstance<LongDouble_t>(i, stringStack); }
180 
181  virtual const char *EvalStringInstance(Int_t i=0);
182  virtual void* EvalObject(Int_t i=0);
183  // EvalInstance should be const. See comment on GetNdata()
184  TFormLeafInfo *GetLeafInfo(Int_t code) const;
186  TMethodCall *GetMethodCall(Int_t code) const;
187  virtual Int_t GetMultiplicity() const {return fMultiplicity;}
188  virtual TLeaf *GetLeaf(Int_t n) const;
189  virtual Int_t GetNcodes() const {return fNcodes;}
190  virtual Int_t GetNdata();
191  //GetNdata should probably be const. However it need to cache some information about the actual dimension
192  //of arrays, so if GetNdata is const, the variables fUsedSizes and fCumulUsedSizes need to be declared
193  //mutable. We will be able to do that only when all the compilers supported for ROOT actually implemented
194  //the mutable keyword.
195  //NOTE: Also modify the code in PrintValue which current goes around this limitation :(
196  virtual Bool_t IsInteger(Bool_t fast=kTRUE) const;
197  Bool_t IsQuickLoad() const { return fQuickLoad; }
198  virtual Bool_t IsString() const;
199  virtual Bool_t Notify() { UpdateFormulaLeaves(); return kTRUE; }
200  virtual char *PrintValue(Int_t mode=0) const;
201  virtual char *PrintValue(Int_t mode, Int_t instance, const char *decform = "9.9") const;
202  virtual void SetAxis(TAxis *axis=0);
203  void SetQuickLoad(Bool_t quick) { fQuickLoad = quick; }
204  virtual void SetTree(TTree *tree) {fTree = tree;}
205  virtual void ResetLoading();
206  virtual TTree* GetTree() const {return fTree;}
207  virtual void UpdateFormulaLeaves();
208 
209  ClassDef(TTreeFormula,9) //The Tree formula
210 };
211 
212 #endif
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
const Int_t kMAXFORMDIM
Definition: TTreeFormula.h:49
An array of TObjects.
Definition: TObjArray.h:39
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 ...
long long Long64_t
Definition: RtypesCore.h:69
TObjArray fBranches
Definition: TTreeFormula.h:107
virtual Double_t EvalInstance(Int_t i=0, const char *stringStack[]=0)
Definition: TTreeFormula.h:177
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:33
double T(double x)
Definition: ChebyshevPol.h:34
TTreeFormulaManager * GetManager() const
Definition: TTreeFormula.h:185
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
virtual TLeaf * GetLeaf(Int_t n) const
Return leaf corresponding to serial number n.
#define BIT(n)
Definition: Rtypes.h:120
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.
Basic string class.
Definition: TString.h:137
Int_t fNdimensions[kMAXCODES]
If true, the current entry has not been loaded yet.
Definition: TTreeFormula.h:111
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.
T GetConstant(Int_t k)
STL namespace.
Int_t fMultiplicity
Definition: TTreeFormula.h:98
virtual void SetTree(TTree *tree)
Definition: TTreeFormula.h:204
virtual void * EvalObject(Int_t i=0)
Evaluate this treeformula.
TFormLeafInfo * GetLeafInfo(Int_t code) const
Return DataMember corresponding to code.
Int_t fNdata[kMAXCODES]
Definition: TTreeFormula.h:95
TTreeFormula & operator=(const TTreeFormula &)
virtual void * GetValuePointerFromMethod(Int_t i, TLeaf *leaf) const
Return result of a leafobject method.
A helper class to implement reading a data member on a variable size array inside a TClonesArray obje...
Array of integers (32 bits per element).
Definition: TArrayI.h:29
void Init(const char *name, const char *formula)
Initialiation called from the constructors.
virtual Bool_t Notify()
This method must be overridden to handle object notification.
Definition: TTreeFormula.h:199
Bool_t LoadCurrentDim()
Calculate the actual dimension for the current entry.
TMethodCall * GetMethodCall(Int_t code) const
Return methodcall corresponding to code.
void Convert(UInt_t fromVersion)
TObjArray fLeafNames
List of TTreeFormula for each alias used.
Definition: TTreeFormula.h:106
Used to coordinate one or more TTreeFormula objects.
virtual Bool_t SwitchToFormLeafInfo(Int_t code)
Convert the underlying lookup method from the direct technique (dereferencing the address held by the...
#define ClassDef(name, id)
Definition: Rtypes.h:254
TObjArray fExternalCuts
List of leaf method calls.
Definition: TTreeFormula.h:104
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.
virtual Double_t GetValueFromMethod(Int_t i, TLeaf *leaf) const
Return result of a leafobject method.
const Int_t kMAXFOUND
Definition: TFormula.h:37
virtual TClass * EvalClass() const
Evaluate the class of this treeformula.
Method or function calling interface.
Definition: TMethodCall.h:41
void LoadBranches()
Make sure that all the branches have been loaded properly.
virtual const char * EvalStringInstance(Int_t i=0)
Eval the instance as a string.
Used to pass a selection expression to the Tree drawing routine.
Definition: TTreeFormula.h:64
A doubly linked list.
Definition: TList.h:47
UChar_t fHasMultipleVarDim[kMAXCODES]
Definition: TTreeFormula.h:113
virtual Bool_t StringToNumber(Int_t code)
Try to 'demote' a string into an array bytes.
TObjArray fDataMembers
List of leaf used in this formula.
Definition: TTreeFormula.h:102
Class to manage histogram axis.
Definition: TAxis.h:36
const Int_t kMAXCODES
Definition: TTreeFormula.h:48
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 fNcodes
This caches the physical number of element in the leaf or datamember.
Definition: TTreeFormula.h:96
unsigned int UInt_t
Definition: RtypesCore.h:42
TObjArray fAliases
List of TCutG and TEntryList used in the formula.
Definition: TTreeFormula.h:105
virtual Int_t GetNcodes() const
Definition: TTreeFormula.h:189
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:116
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
long double LongDouble_t
Definition: RtypesCore.h:57
std::vector< std::string > fAliasesUsed
list of dimension setups, for delayed creation of the dimension information.
Definition: TTreeFormula.h:126
A Branch for the case of an object.
virtual TTree * GetTree() const
Definition: TTreeFormula.h:206
TAxis * fAxis
Definition: TTreeFormula.h:120
Bool_t fHasCast
Definition: TTreeFormula.h:97
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.
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...
LongDouble_t * fConstLD
List of aliases used during the parsing of the expression.
Definition: TTreeFormula.h:128
virtual Bool_t IsLeafString(Int_t code) const
Return TRUE if the leaf or data member corresponding to code is a string.
TObjArray fLeaves
Definition: TTreeFormula.h:101
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...
Bool_t fDidBooleanOptimization
pointer to histogram axis if this is a string
Definition: TTreeFormula.h:121
Int_t fIndexes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:117
This class is a small helper class to implement reading a data member on an object stored in a TTree...
Definition: TFormLeafInfo.h:53
T EvalInstance(Int_t i=0, const char *stringStack[]=0)
Evaluate this treeformula.
#define name(a, b)
Definition: linkTestLib0.cpp:5
TTree * fTree
Definition: TTreeFormula.h:93
Bool_t fQuickLoad
List of branches to read. Similar to fLeaces but duplicates are zeroed out.
Definition: TTreeFormula.h:108
TTreeFormula * fVarIndexes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:118
TTreeFormulaManager * fManager
True if we executed one boolean optimization since the last time instance number 0 was evaluated...
Definition: TTreeFormula.h:122
virtual ~TTreeFormula()
Tree Formula default destructor.
Int_t * fLookupType
Definition: TTreeFormula.h:100
TList * fDimensionSetup
The dimension coordinator.
Definition: TTreeFormula.h:125
virtual Long64_t EvalInstance64(Int_t i=0, const char *stringStack[]=0)
Definition: TTreeFormula.h:178
Bool_t IsQuickLoad() const
Definition: TTreeFormula.h:197
TObjArray fMethods
List of leaf data members.
Definition: TTreeFormula.h:103
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:94
virtual LongDouble_t EvalInstanceLD(Int_t i=0, const char *stringStack[]=0)
Definition: TTreeFormula.h:179
void SetQuickLoad(Bool_t quick)
Definition: TTreeFormula.h:203
unsigned char UChar_t
Definition: RtypesCore.h:34
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual Int_t GetMultiplicity() const
Definition: TTreeFormula.h:187
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
Int_t fFixedSizes[kMAXCODES][kMAXFORMDIM]
Definition: TTreeFormula.h:112
Bool_t fNeedLoading
If true, branch GetEntry is only called when the entry number changes.
Definition: TTreeFormula.h:109
virtual void ResetLoading()
Tell the formula that we are going to request a new entry.
Int_t fCodes[kMAXCODES]
pointer to Tree
Definition: TTreeFormula.h:94
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).