Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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#include "v5/TFormula.h"
27
28#include "TLeaf.h"
29
30#include "TObjArray.h"
31
32#include <string>
33#include <vector>
34
35#ifdef R__OLDHPACC
36namespace std {
37 using ::string;
38 using ::vector;
39}
40#endif
41
42const Int_t kMAXCODES = kMAXFOUND; // must be the same as kMAXFOUND in TFormulaOld
43const Int_t kMAXFORMDIM = 5; // Maximum number of array dimensions support in TTreeFormula
44
45class TTree;
46class TArrayI;
47class TMethodCall;
48class TLeafObject;
49class TDataMember;
52class TFormLeafInfo;
53class TBranchElement;
54class TAxis;
56
57
59
61
62protected:
65 kMissingLeaf = BIT(15), // true if some of the needed leaves are missing in the current TTree
66 kIsInteger = BIT(17), // true if the branch contains an integer variable
67 kNeedEntries = BIT(18) // true if the formula uses Entries$
68 };
69 enum {
76
77 };
78 enum {
79 kAlias = 200,
83 kMinIf = 204,
84 kMaxIf = 205
85 };
86
87 // Helper struct to hold a cache
88 // that can accelerate calculation of the RealIndex.
94
95 TTree *fTree; ///<! Pointer to Tree
96 Int_t fCodes[kMAXCODES]; ///< List of leaf numbers referenced in formula
97 Int_t fNdata[kMAXCODES]; ///<! This caches the physical number of element in the leaf or data member.
98 Int_t fNcodes; ///< Number of leaves referenced in formula
99 bool fHasCast; ///< Record whether the formula contain a cast operation or not
100 Int_t fMultiplicity; ///< Indicator of the variability of the formula
101 Int_t fNindex; ///< Size of fIndex
102 Int_t *fLookupType; ///<[fNindex] Array indicating how each leaf should be looked-up
103 TObjArray fLeaves; ///<! List of leaf used in this formula.
104 TObjArray fDataMembers; ///<! List of leaf data members
105 TObjArray fMethods; ///<! List of leaf method calls
106 TObjArray fExternalCuts; ///<! List of TCutG and TEntryList used in the formula
107 TObjArray fAliases; ///<! List of TTreeFormula for each alias used.
108 TObjArray fLeafNames; ///< List of TNamed describing leaves
109 TObjArray fBranches; ///<! List of branches to read. Similar to fLeaves but duplicates are zeroed out.
110 bool fQuickLoad; ///<! If true, branch GetEntry is only called when the entry number changes.
111 bool fNeedLoading; ///<! If true, the current entry has not been loaded yet.
112
113 Int_t fNdimensions[kMAXCODES]; ///< Number of array dimensions in each leaf
114 Int_t fFixedSizes[kMAXCODES][kMAXFORMDIM]; ///< Physical sizes of lower dimensions for each leaf
115 UChar_t fHasMultipleVarDim[kMAXCODES]; ///< True if the corresponding variable is an array with more than one variable dimension.
116
117 //the next line should have a mutable in front. See GetNdata()
118 Int_t fCumulSizes[kMAXCODES][kMAXFORMDIM]; ///< Accumulated sizes of lower dimensions for each leaf after variable dimensions has been calculated
119 Int_t fIndexes[kMAXCODES][kMAXFORMDIM]; ///< Index of array selected by user for each leaf
120 TTreeFormula *fVarIndexes[kMAXCODES][kMAXFORMDIM]; ///< Pointer to a variable index.
121
122 TAxis *fAxis; ///<! pointer to histogram axis if this is a string
123 bool fDidBooleanOptimization; ///<! True if we executed one boolean optimization since the last time instance number 0 was evaluated
124 TTreeFormulaManager *fManager; ///<! The dimension coordinator.
125
126 // Helper members and function used during the construction and parsing
127 TList *fDimensionSetup; ///<! list of dimension setups, for delayed creation of the dimension information.
128 std::vector<std::string> fAliasesUsed; ///<! List of aliases used during the parsing of the expression.
129
130 LongDouble_t* fConstLD; ///<! local version of fConsts able to store bigger numbers
131
132 RealInstanceCache fRealInstanceCache; ///<! Cache accelerating the GetRealInstance function
133
134 TTreeFormula(const char *name, const char *formula, TTree *tree, const std::vector<std::string>& aliases);
135 void Init(const char *name, const char *formula);
136 bool BranchHasMethod(TLeaf* leaf, TBranch* branch, const char* method,const char* params, Long64_t readentry) const;
137 Int_t DefineAlternate(const char* expression);
138 void DefineDimensions(Int_t code, Int_t size, TFormLeafInfoMultiVarDim * info, Int_t& virt_dim);
139 Int_t FindLeafForExpression(const char* expression, TLeaf *&leaf, TString &leftover, bool &final, UInt_t &paran_level, TObjArray &castqueue, std::vector<std::string>& aliasUsed, bool &useLeafCollectionObject, const char *fullExpression);
140 TLeaf* GetLeafWithDatamember(const char* topchoice, const char* nextchice, Long64_t readentry) const;
141 Int_t ParseWithLeaf(TLeaf *leaf, const char *expression, bool final, UInt_t paran_level, TObjArray &castqueue, bool useLeafCollectionObject, const char *fullExpression);
144 Int_t RegisterDimensions(Int_t code, TFormLeafInfo *info, TFormLeafInfo *maininfo, bool useCollectionObject);
145 Int_t RegisterDimensions(Int_t code, TLeaf *leaf);
146 Int_t RegisterDimensions(const char *size, Int_t code);
147
148 virtual Double_t GetValueFromMethod(Int_t i, TLeaf *leaf) const;
149 virtual void* GetValuePointerFromMethod(Int_t i, TLeaf *leaf) const;
151
152 void LoadBranches();
153 bool LoadCurrentDim();
154 void ResetDimensions();
155
156 virtual TClass* EvalClass(Int_t oper) const;
157 virtual bool IsLeafInteger(Int_t code) const;
158 bool IsString(Int_t oper) const override;
159 virtual bool IsLeafString(Int_t code) const;
160 virtual bool SwitchToFormLeafInfo(Int_t code);
161 bool StringToNumber(Int_t code) override;
162
163 void Convert(UInt_t fromVersion) override;
164 Bool_t AnalyzePrimitive(TString &chain, TObjArray &args, Int_t &err, Int_t offset) override;
165 void Optimize() override;
166
167private:
168 // Not implemented yet
169 TTreeFormula(const TTreeFormula&) = delete;
171
172 template<typename T> T GetConstant(Int_t k);
173
174public:
175 TTreeFormula() = delete;
177 TTreeFormula(const char *name,const char *formula, TTree *tree);
178 ~TTreeFormula() override;
179
180 Int_t DefinedVariable(TString &variable, Int_t &action) override;
181 virtual TClass* EvalClass() const;
182
183 template<typename T> T EvalInstance(Int_t i=0, const char *stringStack[] = nullptr);
184 /// \brief Double_t template specialization of EvalInstance
185 virtual Double_t EvalInstance(Int_t i=0, const char *stringStack[] = nullptr) {return EvalInstance<Double_t>(i, stringStack); }
186 /// \brief Long64_t template specialization of EvalInstance
187 virtual Long64_t EvalInstance64(Int_t i=0, const char *stringStack[] = nullptr) {return EvalInstance<Long64_t>(i, stringStack); }
188 /// \brief LongDouble_t template specialization of EvalInstance
189 virtual LongDouble_t EvalInstanceLD(Int_t i=0, const char *stringStack[] = nullptr) {return EvalInstance<LongDouble_t>(i, stringStack); }
190
191 virtual const char *EvalStringInstance(Int_t i=0);
192 virtual void* EvalObject(Int_t i=0);
193 // EvalInstance should be const. See comment on GetNdata()
194 TFormLeafInfo *GetLeafInfo(Int_t code) const;
196 TMethodCall *GetMethodCall(Int_t code) const;
197 virtual Int_t GetMultiplicity() const {return fMultiplicity;}
198 virtual TLeaf *GetLeaf(Int_t n) const;
199 virtual Int_t GetNcodes() const {return fNcodes;}
200 virtual Int_t GetNdata();
201 //GetNdata should probably be const. However it need to cache some information about the actual dimension
202 //of arrays, so if GetNdata is const, the variables fUsedSizes and fCumulUsedSizes need to be declared
203 //mutable. We will be able to do that only when all the compilers supported for ROOT actually implemented
204 //the mutable keyword.
205 //NOTE: Also modify the code in PrintValue which current goes around this limitation :(
206 virtual bool IsInteger(bool fast=true) const;
207 bool IsQuickLoad() const { return fQuickLoad; }
208 virtual bool IsString() const;
209 bool Notify() override { UpdateFormulaLeaves(); return true; }
210 virtual char *PrintValue(Int_t mode=0) const;
211 virtual char *PrintValue(Int_t mode, Int_t instance, const char *decform = "9.9") const;
212 virtual void SetAxis(TAxis *axis = nullptr);
213 void SetQuickLoad(bool quick) { fQuickLoad = quick; }
214 virtual void SetTree(TTree *tree) {fTree = tree;}
215 virtual void ResetLoading();
216 virtual TTree* GetTree() const {return fTree;}
217 virtual void UpdateFormulaLeaves();
218
219 ClassDefOverride(TTreeFormula, 10); //The Tree formula
220};
221
222#endif
static Roo_reg_AGKInteg1D instance
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char).
Definition RtypesCore.h:52
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long double LongDouble_t
Long Double (not portable).
Definition RtypesCore.h:75
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
#define BIT(n)
Definition Rtypes.h:91
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
char name[80]
Definition TGX11.cxx:148
Double_t err
const Int_t kMAXCODES
const Int_t kMAXFORMDIM
The FORMULA class (ROOT version 5).
Definition TFormula.h:65
Array of integers (32 bits per element).
Definition TArrayI.h:27
Class to manage histogram axis.
Definition TAxis.h:32
A Branch for the case of an object.
A TTree is a list of TBranches.
Definition TBranch.h:93
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
A helper class to implement reading a data member on a variable size array inside a TClonesArray obje...
This class is a small helper class to implement reading a data member on an object stored in a TTree.
A TLeaf for a general object derived from TObject.
Definition TLeafObject.h:31
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
A doubly linked list.
Definition TList.h:38
Method or function calling interface.
Definition TMethodCall.h:37
An array of TObjects.
Definition TObjArray.h:31
Describe one element (data member) to be Streamed.
Basic string class.
Definition TString.h:138
Used to coordinate one or more TTreeFormula objects.
bool LoadCurrentDim()
Calculate the actual dimension for the current entry.
virtual void ResetLoading()
Tell the formula that we are going to request a new entry.
virtual bool IsInteger(bool fast=true) const
Return TRUE if the formula corresponds to one single Tree leaf and this leaf is short,...
bool fHasCast
Record whether the formula contain a cast operation or not.
UChar_t fHasMultipleVarDim[kMAXCODES]
True if the corresponding variable is an array with more than one variable dimension.
Int_t fMultiplicity
Indicator of the variability of the formula.
Int_t FindLeafForExpression(const char *expression, TLeaf *&leaf, TString &leftover, bool &final, UInt_t &paran_level, TObjArray &castqueue, std::vector< std::string > &aliasUsed, bool &useLeafCollectionObject, const char *fullExpression)
Look for the leaf corresponding to the start of expression.
TTreeFormula()=delete
Int_t DefineAlternate(const char *expression)
This method check for treat the case where expression contains Alt$( and load up both fAliases and fE...
TTreeFormulaManager * GetManager() const
virtual TLeaf * GetLeaf(Int_t n) const
Return leaf corresponding to serial number n.
TAxis * fAxis
! pointer to histogram axis if this is a string
Bool_t AnalyzePrimitive(TString &chain, TObjArray &args, Int_t &err, Int_t offset) override
Check if the given string matches a defined function primitive.
Int_t fNindex
Size of fIndex.
Int_t fNcodes
Number of leaves referenced in formula.
Int_t RegisterDimensions(Int_t code, Int_t size, TFormLeafInfoMultiVarDim *multidim=nullptr)
This method stores the dimension information for later usage.
TObjArray fExternalCuts
! List of TCutG and TEntryList used in the formula
virtual Long64_t EvalInstance64(Int_t i=0, const char *stringStack[]=nullptr)
Long64_t template specialization of EvalInstance.
virtual Int_t GetNcodes() const
Int_t DefinedVariable(TString &variable, Int_t &action) override
Check if name is in the list of Tree/Branch leaves.
virtual Double_t GetValueFromMethod(Int_t i, TLeaf *leaf) const
Return result of a leafobject method.
virtual const char * EvalStringInstance(Int_t i=0)
Eval the instance as a string.
void Init(const char *name, const char *formula)
Initialization called from the constructors.
Int_t * fLookupType
[fNindex] Array indicating how each leaf should be looked-up
bool fQuickLoad
! If true, branch GetEntry is only called when the entry number changes.
Int_t ParseWithLeaf(TLeaf *leaf, const char *expression, bool final, UInt_t paran_level, TObjArray &castqueue, bool useLeafCollectionObject, const char *fullExpression)
Decompose 'expression' as pointing to something inside the leaf Returns:
virtual Double_t EvalInstance(Int_t i=0, const char *stringStack[]=nullptr)
Double_t template specialization of EvalInstance.
bool Notify() override
This method must be overridden to handle object notification (the base implementation is no-op).
virtual void SetTree(TTree *tree)
virtual bool SwitchToFormLeafInfo(Int_t code)
Convert the underlying lookup method from the direct technique (dereferencing the address held by the...
std::vector< std::string > fAliasesUsed
! List of aliases used during the parsing of the expression.
Int_t fCodes[kMAXCODES]
List of leaf numbers referenced in formula.
virtual char * PrintValue(Int_t mode=0) const
Return value of variable as a string.
bool fNeedLoading
! If true, the current entry has not been loaded yet.
Int_t fIndexes[kMAXCODES][kMAXFORMDIM]
Index of array selected by user for each leaf.
virtual Int_t GetMultiplicity() const
Int_t fCumulSizes[kMAXCODES][kMAXFORMDIM]
Accumulated sizes of lower dimensions for each leaf after variable dimensions has been calculated.
virtual bool IsString() const
Return TRUE if the formula is a string.
TList * fDimensionSetup
! list of dimension setups, for delayed creation of the dimension information.
T EvalInstance(Int_t i=0, const char *stringStack[]=nullptr)
Evaluate this treeformula.
void ResetDimensions()
Populate the TTreeFormulaManager with the dimension information.
Int_t fNdimensions[kMAXCODES]
Number of array dimensions in each leaf.
TTree * fTree
! Pointer to Tree
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...
void SetQuickLoad(bool quick)
TMethodCall * GetMethodCall(Int_t code) const
Return methodcall corresponding to code.
virtual void UpdateFormulaLeaves()
This function is called TTreePlayer::UpdateFormulaLeaves, itself called by TChain::LoadTree when a ne...
~TTreeFormula() override
Tree Formula default destructor.
virtual TTree * GetTree() const
TObjArray fAliases
! List of TTreeFormula for each alias used.
T GetConstant(Int_t k)
virtual void * EvalObject(Int_t i=0)
Evaluate this treeformula.
TObjArray fLeafNames
List of TNamed describing leaves.
bool StringToNumber(Int_t code) override
Try to 'demote' a string into an array bytes.
virtual bool 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 ...
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.
virtual bool IsLeafString(Int_t code) const
Return TRUE if the leaf or data member corresponding to code is a string.
void LoadBranches()
Make sure that all the branches have been loaded properly.
virtual TClass * EvalClass() const
Evaluate the class of this treeformula.
TTreeFormula(const char *name, const char *formula, TTree *tree, const std::vector< std::string > &aliases)
Constructor used during the expansion of an alias.
Int_t GetRealInstance(Int_t instance, Int_t codeindex)
Now let calculate what physical instance we really need.
virtual void SetAxis(TAxis *axis=nullptr)
Set the axis (in particular get the type).
virtual LongDouble_t EvalInstanceLD(Int_t i=0, const char *stringStack[]=nullptr)
LongDouble_t template specialization of EvalInstance.
TTreeFormula & operator=(const TTreeFormula &)=delete
TFormLeafInfo * GetLeafInfo(Int_t code) const
Return DataMember corresponding to code.
TObjArray fDataMembers
! List of leaf data members
bool 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...
TTreeFormula(const TTreeFormula &)=delete
TTreeFormula * fVarIndexes[kMAXCODES][kMAXFORMDIM]
Pointer to a variable index.
LongDouble_t * fConstLD
! local version of fConsts able to store bigger numbers
Int_t fFixedSizes[kMAXCODES][kMAXFORMDIM]
Physical sizes of lower dimensions for each leaf.
virtual Int_t GetNdata()
Return number of available instances in the formula.
RealInstanceCache fRealInstanceCache
! Cache accelerating the GetRealInstance function
TObjArray fMethods
! List of leaf method calls
Int_t fNdata[kMAXCODES]
! This caches the physical number of element in the leaf or data member.
bool IsQuickLoad() const
void Optimize() override
MI include.
TObjArray fLeaves
! List of leaf used in this formula.
bool fDidBooleanOptimization
! True if we executed one boolean optimization since the last time instance number 0 was evaluated
friend class TTreeFormulaManager
virtual void * GetValuePointerFromMethod(Int_t i, TLeaf *leaf) const
Return result of a leafobject method.
TObjArray fBranches
! List of branches to read. Similar to fLeaves but duplicates are zeroed out.
TTreeFormulaManager * fManager
! The dimension coordinator.
void Convert(UInt_t fromVersion) override
A TTree represents a columnar dataset.
Definition TTree.h:89
const Int_t n
Definition legend1.C:16
const Int_t kMAXFOUND
Definition TFormula.h:31