The Formula class.
This is a new version of the TFormula class based on Cling. This class is not 100% backward compatible with the old TFormula class, which is still available in ROOT as ROOT::v5::TFormula
. Some of the TFormula member functions available in version 5, such as Analyze
and AnalyzeFunction
are not available in the new TFormula. On the other hand formula expressions which were valid in version 5 are still valid in TFormula version 6
This class has been implemented during Google Summer of Code 2013 by Maciej Zimnoch.
sin(x)/x
[0]*sin(x) + [1]*exp(-[2]*x)
x + y**2
x^2 + y^2
[0]*pow([1],4)
2*pi*sqrt(x/y)
gaus(0)*expo(3) + ypol3(5)*x
gausn(0)*expo(3) + ypol3(5)*x
gaus(x, [0..2]) + expo(y, [3..4])
In the last examples above:
gaus(0)
is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2)
and (0) means start numbering parameters at 0gausn(0)
is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2)/(sqrt(2*pi)*[2]))
and (0) means start numbering parameters at 0expo(3)
is a substitute for exp([3]+[4]*x)
pol3(5)
is a substitute for par[5]+par[6]*x+par[7]*x**2+par[8]*x**3
(PolN
stands for Polynomial of degree N)gaus(x, [0..2])
is a more explicit way of writing gaus(0)
expo(y, [3..4])
is a substitute for exp([3]+[4]*y)
See below the full list of predefined functions which can be used as shortcuts in TFormula.
TMath
functions can be part of the expression, eg:
TMath::Landau(x)*sin(x)
TMath::Erf(x)
Formula may contain constants, eg:
sqrt2
e
pi
ln10
infinity
and more.
Formulas may also contain other user-defined ROOT functions defined with a TFormula, eg, where f1
is defined on one x-dimension and 2 parameters:
f1(x, [omega], [phi])
f1([0..1])
f1([1], [0])
f1(y)
To replace only parameter names, the dimension variable can be dropped. Alternatively, to change only the dimension variable, the parameters can be dropped. Note that if a parameter is dropped or keeps its old name, its old value will be copied to the new function. The syntax used in the examples above also applies to the predefined parametrized functions like gaus
and expo
.
Comparisons operators are also supported (&&, ||, ==, <=, >=, !)
Examples:
sin(x*(x<0.5 || x>1))
If the result of a comparison is TRUE, the result is 1, otherwise 0.
Already predefined names can be given. For example, if the formula
TFormula old("old",sin(x*(x<0.5 || x>1)))
one can assign a name to the formula. By default the name of the object = title = formula itself.
TFormula new("new","x*old")
is equivalent to:
TFormula new("new","x*sin(x*(x<0.5 || x>1))")
The class supports unlimited number of variables and parameters. By default the names which can be used for the variables are x,y,z,t
or x[0],x[1],x[2],x[3],....x[N]
for N-dimensional formulas.
This class is not anymore the base class for the function classes TF1
, but it has now a data member of TF1 which can be accessed via TF1::GetFormula
.
TFormula supports gradient and hessian calculations through clad. To calculate the gradient one needs to first declare a CladStorage
of the same size as the number of parameters and then pass the variables and the created CladStorage
:
The process is similar for hessians, except that the size of the created CladStorage should be the square of the number of parameters because HessianPar
returns a flattened matrix:
The list of available predefined functions which can be used as shortcuts is the following:
gaus
is a substitute for [Constant]*exp(-0.5*((x-[Mean])/[Sigma])*((x-[Mean])/[Sigma]))
landau
is a substitute for [Constant]*TMath::Landau (x,[MPV],[Sigma],false)
expo
is a substitute for exp([Constant]+[Slope]*x)
crystalball
is substitute for [Constant]*ROOT::Math::crystalball_function (x,[Alpha],[N],[Sigma],[Mean])
breitwigner
is a substitute for [p0]*ROOT::Math::breitwigner_pdf (x,[p2],[p1])
pol0,1,2,...N
is a substitute for a polynomial of degree N
: ([p0]+[p1]*x+[p2]*pow(x,2)+....[pN]*pow(x,N)
cheb0,1,2,...N
is a substitute for a Chebyshev polynomial of degree N
: ROOT::Math::Chebyshev10(x,[p0],[p1],[p2],...[pN])
. Note the maximum N allowed here is 10.xygaus
is a substitute for [Constant]*exp(-0.5*pow(((x-[MeanX])/[SigmaX]),2 )- 0.5*pow(((y-[MeanY])/[SigmaY]),2))
, a 2d Gaussian without correlation.bigaus
is a substitute for [Constant]*ROOT::Math::bigaussian_pdf (x,y,[SigmaX],[SigmaY],[Rho],[MeanX],[MeanY])
, a 2d gaussian including a correlation parameter.xyzgaus
is for a 3d Gaussians without correlations: [Constant]*exp(-0.5*pow(((x-[MeanX])/[SigmaX]),2 )- 0.5*pow(((y-[MeanY])/[SigmaY]),2 )- 0.5*pow(((z-[MeanZ])/[SigmaZ]),2))
In a TFormula, a variable is a defined by a name x
, y
, z
or t
or an index like x[0]
, x[1]
, x[2]
; that is x[N]
where N is an integer.
Parameters are similar and can take any name. It is specified using brackets e.g. [expected_mass]
or [0]
.
Variables and parameters can be combined in the same TFormula. Here we consider a very simple case where we have an exponential decay after some time t and a number of events with timestamps for which we want to evaluate this function.
The distinction between variables and parameters arose from the TFormula's application in fitting. There parameters are fitted to the data provided through variables. In other applications this distinction can go away.
Parameter values can be provided dynamically using TFormula::EvalPar
instead of TFormula::Eval
. In this way parameters can be used identically to variables. See below for an example that uses only parameters to model a function.
All operators of C/C++ are allowed in a TFormula with a few caveats.
The operators |
, &
, %
can be used but will raise an error if used in conjunction with a variable or a parameter. Variables and parameters are treated as doubles internally for which these operators are not defined. This means the following command will run successfully
but not
.
The operator ^
is defined to mean exponentiation instead of the C/C++ interpretation xor. **
is added, also meaning exponentiation.
The operators ++
and @
are added, and are shorthand for the a linear function. That means the expression x@2
will be expanded to
where n is the first previously unused parameter number.
Definition at line 88 of file TFormula.h.
Public Types | |
using | CladStorage = std::vector< Double_t > |
enum | EStatusBits { kNotGlobal = (1ULL << ( 10 )) , kNormalized = (1ULL << ( 14 )) , kLinear = (1ULL << ( 16 )) , kLambda = (1ULL << ( 17 )) } |
Public Types inherited from TObject | |
enum | { kIsOnHeap = 0x01000000 , kNotDeleted = 0x02000000 , kZombie = 0x04000000 , kInconsistent = 0x08000000 , kBitMask = 0x00ffffff } |
enum | { kSingleKey = (1ULL << ( 0 )) , kOverwrite = (1ULL << ( 1 )) , kWriteDelete = (1ULL << ( 2 )) } |
enum | EDeprecatedStatusBits { kObjInCanvas = (1ULL << ( 3 )) } |
enum | EStatusBits { kCanDelete = (1ULL << ( 0 )) , kMustCleanup = (1ULL << ( 3 )) , kIsReferenced = (1ULL << ( 4 )) , kHasUUID = (1ULL << ( 5 )) , kCannotPick = (1ULL << ( 6 )) , kNoContextMenu = (1ULL << ( 8 )) , kInvalidObject = (1ULL << ( 13 )) } |
Public Member Functions | |
TFormula () | |
TFormula (const char *name, const char *formula, int ndim, int npar, bool addToGlobList=true) | |
Constructor from a full compile-able C++ expression. | |
TFormula (const char *name, const char *formula="", bool addToGlobList=true, bool vectorize=false) | |
TFormula (const TFormula &formula) | |
~TFormula () override | |
void | AddParameter (const TString &name, Double_t value=0) |
void | AddVariable (const TString &name, Double_t value=0) |
Adds variable to known variables, and reprocess formula. | |
void | AddVariables (const TString *vars, const Int_t size) |
Adds multiple variables. | |
void | Clear (Option_t *option="") override |
Clear the formula setting expression to empty and reset the variables and parameters containers. | |
Int_t | Compile (const char *expression="") |
Compile the given expression with Cling backward compatibility method to be used in combination with the empty constructor if no expression is given , the current stored formula (retrieved with GetExpFormula()) or the title is used. | |
void | Copy (TObject &f1) const override |
Copy this to obj. | |
template<typename... Args> | |
Double_t | Eval (Args... args) const |
Set first 1, 2, 3 or 4 variables (e.g. | |
Double_t | EvalPar (const Double_t *x, const Double_t *params=nullptr) const |
template<class T > | |
T | EvalPar (const T *x, const Double_t *params=nullptr) const |
bool | GenerateGradientPar () |
Generate gradient computation routine with respect to the parameters. | |
bool | GenerateHessianPar () |
Generate hessian computation routine with respect to the parameters. | |
TString | GetExpFormula (Option_t *option="") const |
Return the expression formula. | |
TString | GetGradientFormula () const |
TString | GetHessianFormula () const |
const TObject * | GetLinearPart (Int_t i) const |
Return linear part. | |
Int_t | GetNdim () const |
Int_t | GetNpar () const |
Int_t | GetNumber () const |
Double_t | GetParameter (const char *name) const |
Returns parameter value given by string. | |
Double_t | GetParameter (Int_t param) const |
Return parameter value given by integer. | |
Double_t * | GetParameters () const |
void | GetParameters (Double_t *params) const |
const char * | GetParName (Int_t ipar) const |
Return parameter name given by integer. | |
Int_t | GetParNumber (const char *name) const |
Return parameter index given a name (return -1 for not existing parameters) non need to print an error. | |
TString | GetUniqueFuncName () const |
Double_t | GetVariable (const char *name) const |
Returns variable value. | |
TString | GetVarName (Int_t ivar) const |
Returns variable name given its position in the array. | |
Int_t | GetVarNumber (const char *name) const |
Returns variable number (positon in array) given its name. | |
void | GradientPar (const Double_t *x, Double_t *result) |
Compute the gradient with respect to the parameter passing a buffer with a size at least equal to the number of parameters. | |
void | GradientPar (const Double_t *x, TFormula::CladStorage &result) |
Compute the gradient employing automatic differentiation. | |
bool | HasGeneratedGradient () const |
bool | HasGeneratedHessian () const |
void | HessianPar (const Double_t *x, Double_t *result) |
void | HessianPar (const Double_t *x, TFormula::CladStorage &result) |
Compute the gradient employing automatic differentiation. | |
TClass * | IsA () const override |
Bool_t | IsLinear () const |
Bool_t | IsValid () const |
Bool_t | IsVectorized () const |
TFormula & | operator= (const TFormula &rhs) |
= operator. | |
void | Print (Option_t *option="") const override |
Print the formula and its attributes. | |
void | SetName (const char *name) override |
Set the name of the formula. | |
void | SetParameter (const char *name, Double_t value) |
Sets parameter value. | |
void | SetParameter (Int_t param, Double_t value) |
Set a parameter given a parameter index. | |
void | SetParameters (const Double_t *params) |
Set a vector of parameters value. | |
template<typename... Args> | |
void | SetParameters (Double_t arg1, Args &&... args) |
Set a list of parameters. | |
void | SetParName (Int_t ipar, const char *name) |
template<typename... Args> | |
void | SetParNames (Args &&... args) |
Set parameter names. | |
void | SetVariable (const TString &name, Double_t value) |
Sets variable value. | |
void | SetVariables (const std::pair< TString, Double_t > *vars, const Int_t size) |
Sets multiple variables. | |
void | SetVectorized (Bool_t vectorized) |
void | Streamer (TBuffer &) override |
Stream a class object. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
Public Member Functions inherited from TNamed | |
TNamed () | |
TNamed (const char *name, const char *title) | |
TNamed (const TNamed &named) | |
TNamed copy ctor. | |
TNamed (const TString &name, const TString &title) | |
virtual | ~TNamed () |
TNamed destructor. | |
void | Clear (Option_t *option="") override |
Set name and title to empty strings (""). | |
TObject * | Clone (const char *newname="") const override |
Make a clone of an object using the Streamer facility. | |
Int_t | Compare (const TObject *obj) const override |
Compare two TNamed objects. | |
void | Copy (TObject &named) const override |
Copy this to obj. | |
virtual void | FillBuffer (char *&buffer) |
Encode TNamed into output buffer. | |
const char * | GetName () const override |
Returns name of object. | |
const char * | GetTitle () const override |
Returns title of object. | |
ULong_t | Hash () const override |
Return hash value for this object. | |
TClass * | IsA () const override |
Bool_t | IsSortable () const override |
void | ls (Option_t *option="") const override |
List TNamed name and title. | |
TNamed & | operator= (const TNamed &rhs) |
TNamed assignment operator. | |
void | Print (Option_t *option="") const override |
Print TNamed name and title. | |
virtual void | SetNameTitle (const char *name, const char *title) |
Set all the TNamed parameters (name and title). | |
virtual void | SetTitle (const char *title="") |
Set the title of the TNamed. | |
virtual Int_t | Sizeof () const |
Return size of the TNamed part of the TObject. | |
void | Streamer (TBuffer &) override |
Stream an object of class TObject. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
Public Member Functions inherited from TObject | |
TObject () | |
TObject constructor. | |
TObject (const TObject &object) | |
TObject copy ctor. | |
virtual | ~TObject () |
TObject destructor. | |
void | AbstractMethod (const char *method) const |
Use this method to implement an "abstract" method that you don't want to leave purely abstract. | |
virtual void | AppendPad (Option_t *option="") |
Append graphics object to current pad. | |
virtual void | Browse (TBrowser *b) |
Browse object. May be overridden for another default action. | |
ULong_t | CheckedHash () |
Check and record whether this class has a consistent Hash/RecursiveRemove setup (*) and then return the regular Hash value for this object. | |
virtual const char * | ClassName () const |
Returns name of class to which the object belongs. | |
virtual void | Delete (Option_t *option="") |
Delete this object. | |
virtual Int_t | DistancetoPrimitive (Int_t px, Int_t py) |
Computes distance from point (px,py) to the object. | |
virtual void | Draw (Option_t *option="") |
Default Draw method for all objects. | |
virtual void | DrawClass () const |
Draw class inheritance tree of the class to which this object belongs. | |
virtual TObject * | DrawClone (Option_t *option="") const |
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1) . | |
virtual void | Dump () const |
Dump contents of object on stdout. | |
virtual void | Error (const char *method, const char *msgfmt,...) const |
Issue error message. | |
virtual void | Execute (const char *method, const char *params, Int_t *error=nullptr) |
Execute method on this object with the given parameter string, e.g. | |
virtual void | Execute (TMethod *method, TObjArray *params, Int_t *error=nullptr) |
Execute method on this object with parameters stored in the TObjArray. | |
virtual void | ExecuteEvent (Int_t event, Int_t px, Int_t py) |
Execute action corresponding to an event at (px,py). | |
virtual void | Fatal (const char *method, const char *msgfmt,...) const |
Issue fatal error message. | |
virtual TObject * | FindObject (const char *name) const |
Must be redefined in derived classes. | |
virtual TObject * | FindObject (const TObject *obj) const |
Must be redefined in derived classes. | |
virtual Option_t * | GetDrawOption () const |
Get option used by the graphics system to draw this object. | |
virtual const char * | GetIconName () const |
Returns mime type name of object. | |
virtual char * | GetObjectInfo (Int_t px, Int_t py) const |
Returns string containing info about the object at position (px,py). | |
virtual Option_t * | GetOption () const |
virtual UInt_t | GetUniqueID () const |
Return the unique object id. | |
virtual Bool_t | HandleTimer (TTimer *timer) |
Execute action in response of a timer timing out. | |
Bool_t | HasInconsistentHash () const |
Return true is the type of this object is known to have an inconsistent setup for Hash and RecursiveRemove (i.e. | |
virtual void | Info (const char *method, const char *msgfmt,...) const |
Issue info message. | |
virtual Bool_t | InheritsFrom (const char *classname) const |
Returns kTRUE if object inherits from class "classname". | |
virtual Bool_t | InheritsFrom (const TClass *cl) const |
Returns kTRUE if object inherits from TClass cl. | |
virtual void | Inspect () const |
Dump contents of this object in a graphics canvas. | |
void | InvertBit (UInt_t f) |
Bool_t | IsDestructed () const |
IsDestructed. | |
virtual Bool_t | IsEqual (const TObject *obj) const |
Default equal comparison (objects are equal if they have the same address in memory). | |
virtual Bool_t | IsFolder () const |
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects). | |
R__ALWAYS_INLINE Bool_t | IsOnHeap () const |
R__ALWAYS_INLINE Bool_t | IsZombie () const |
void | MayNotUse (const char *method) const |
Use this method to signal that a method (defined in a base class) may not be called in a derived class (in principle against good design since a child class should not provide less functionality than its parent, however, sometimes it is necessary). | |
virtual Bool_t | Notify () |
This method must be overridden to handle object notification (the base implementation is no-op). | |
void | Obsolete (const char *method, const char *asOfVers, const char *removedFromVers) const |
Use this method to declare a method obsolete. | |
void | operator delete (void *ptr) |
Operator delete. | |
void | operator delete (void *ptr, void *vp) |
Only called by placement new when throwing an exception. | |
void | operator delete[] (void *ptr) |
Operator delete []. | |
void | operator delete[] (void *ptr, void *vp) |
Only called by placement new[] when throwing an exception. | |
void * | operator new (size_t sz) |
void * | operator new (size_t sz, void *vp) |
void * | operator new[] (size_t sz) |
void * | operator new[] (size_t sz, void *vp) |
TObject & | operator= (const TObject &rhs) |
TObject assignment operator. | |
virtual void | Paint (Option_t *option="") |
This method must be overridden if a class wants to paint itself. | |
virtual void | Pop () |
Pop on object drawn in a pad to the top of the display list. | |
virtual Int_t | Read (const char *name) |
Read contents of object with specified name from the current directory. | |
virtual void | RecursiveRemove (TObject *obj) |
Recursively remove this object from a list. | |
void | ResetBit (UInt_t f) |
virtual void | SaveAs (const char *filename="", Option_t *option="") const |
Save this object in the file specified by filename. | |
virtual void | SavePrimitive (std::ostream &out, Option_t *option="") |
Save a primitive as a C++ statement(s) on output stream "out". | |
void | SetBit (UInt_t f) |
void | SetBit (UInt_t f, Bool_t set) |
Set or unset the user status bits as specified in f. | |
virtual void | SetDrawOption (Option_t *option="") |
Set drawing option for object. | |
virtual void | SetUniqueID (UInt_t uid) |
Set the unique object id. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
virtual void | SysError (const char *method, const char *msgfmt,...) const |
Issue system error message. | |
R__ALWAYS_INLINE Bool_t | TestBit (UInt_t f) const |
Int_t | TestBits (UInt_t f) const |
virtual void | UseCurrentStyle () |
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyle or TROOT::ForceStyle have been invoked. | |
virtual void | Warning (const char *method, const char *msgfmt,...) const |
Issue warning message. | |
virtual Int_t | Write (const char *name=nullptr, Int_t option=0, Int_t bufsize=0) |
Write this object to the current directory. | |
virtual Int_t | Write (const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const |
Write this object to the current directory. | |
Static Public Member Functions | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
Static Public Member Functions inherited from TNamed | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
Static Public Member Functions inherited from TObject | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
static Longptr_t | GetDtorOnly () |
Return destructor only flag. | |
static Bool_t | GetObjectStat () |
Get status of object stat flag. | |
static void | SetDtorOnly (void *obj) |
Set destructor only flag. | |
static void | SetObjectStat (Bool_t stat) |
Turn on/off tracking of objects in the TObjectTable. | |
Protected Member Functions | |
void | DoAddParameter (const TString &name, Double_t value, bool processFormula) |
Adds parameter to known parameters. | |
Double_t | DoEval (const Double_t *x, const Double_t *p=nullptr) const |
Evaluate formula. | |
void | DoSetParameters (const Double_t *p, Int_t size) |
void | ExtractFunctors (TString &formula) |
Extracts functors from formula, and put them in fFuncs. | |
Bool_t | PrepareFormula (TString &formula) |
prepare the formula to be executed normally is called with fFormula | |
void | PreProcessFormula (TString &formula) |
Preprocessing of formula Replace all ** by ^, and removes spaces. | |
void | ProcessFormula (TString &formula) |
Iterates through functors in fFuncs and performs the appropriate action. | |
void | ReplaceParamName (TString &formula, const TString &oldname, const TString &name) |
Replace in Formula expression the parameter name. | |
void | SetPredefinedParamNames () |
Set parameter names only in case of pre-defined functions. | |
Protected Member Functions inherited from TObject | |
virtual void | DoError (int level, const char *location, const char *fmt, va_list va) const |
Interface to ErrorHandler (protected). | |
void | MakeZombie () |
Static Protected Member Functions | |
static Bool_t | IsAParameterName (const TString &formula, int ipos) |
static Bool_t | IsBracket (const char c) |
static Bool_t | IsFunctionNameChar (const char c) |
static Bool_t | IsHexadecimal (const TString &formula, int ipos) |
static Bool_t | IsOperator (const char c) |
static Bool_t | IsScientificNotation (const TString &formula, int ipos) |
Protected Attributes | |
std::map< TString, Double_t > | fConsts |
! | |
TString | fFormula |
String representing the formula expression. | |
std::list< TFormulaFunction > | fFuncs |
! | |
std::map< TString, TString > | fFunctionsShortcuts |
! | |
std::vector< TObject * > | fLinearParts |
Vector of linear functions. | |
Int_t | fNdim |
Dimension - needed for lambda expressions. | |
Int_t | fNpar |
! Number of parameter (transient since we save the vector) | |
Int_t | fNumber |
Number used to identify pre-defined functions (gaus, expo,..) | |
std::map< TString, Int_t, TFormulaParamOrder > | fParams |
|| List of parameter names | |
std::map< TString, TFormulaVariable > | fVars |
! List of variable names | |
Bool_t | fVectorized = false |
Whether we should use vectorized or regular variables. | |
Protected Attributes inherited from TNamed | |
TString | fName |
TString | fTitle |
Private Types | |
using | CallFuncSignature = TInterpreter::CallFuncIFacePtr_t::Generic_t |
Private Member Functions | |
void | FillDefaults () |
Fill structures with default variables, constants and function shortcuts. | |
void | FillParametrizedFunctions (std::map< std::pair< TString, Int_t >, std::pair< TString, TString > > &functions) |
Fill map with parametrized functions. | |
void | FillVecFunctionsShurtCuts () |
Fill the shortcuts for vectorized functions We will replace for example sin with vecCore::Mat::Sin. | |
std::string | GetGradientFuncName () const |
std::string | GetHessianFuncName () const |
void | HandleExponentiation (TString &formula) |
Handling exponentiation Can handle multiple carets, eg. | |
void | HandleFunctionArguments (TString &formula) |
Handling user functions (and parametrized functions) to take variables and optionally parameters as arguments. | |
void | HandleLinear (TString &formula) |
Handle linear functions defined with the operator ++. | |
void | HandleParametrizedFunctions (TString &formula) |
Handling parametrized functions Function can be normalized, and have different variable then x. | |
void | HandleParamRanges (TString &formula) |
Handling parameter ranges, in the form of [1..5]. | |
void | HandlePolN (TString &formula) |
Handling polN If before 'pol' exist any name, this name will be treated as variable used in polynomial eg. | |
bool | HasGradientGenerationFailed () const |
bool | HasHessianGenerationFailed () const |
Bool_t | InitLambdaExpression (const char *formula) |
void | InputFormulaIntoCling () |
Inputs formula, transfered to C++ code into Cling. | |
Bool_t | PrepareEvalMethod () |
Sets TMethodCall to function inside Cling environment. | |
void | ReInitializeEvalMethod () |
Re-initialize eval method. | |
void | ReplaceAllNames (TString &formula, std::map< TString, TString > &substitutions) |
Static Private Member Functions | |
static Bool_t | IsDefaultVariableName (const TString &name) |
Private Attributes | |
Bool_t | fAllParametersSetted |
Flag to control if all parameters are setted. | |
std::atomic< Bool_t > | fClingInitialized |
! Transient to force re-initialization | |
TString | fClingInput |
! Input function passed to Cling | |
TString | fClingName |
! Unique name passed to Cling to define the function ( double clingName(double*x, double*p) ) | |
std::vector< Double_t > | fClingParameters |
Parameter values. | |
std::vector< Double_t > | fClingVariables |
! Cached variables | |
CallFuncSignature | fFuncPtr = nullptr |
! Function pointer, owned by the JIT. | |
CallFuncSignature | fGradFuncPtr = nullptr |
! Function pointer, owned by the JIT. | |
std::string | fGradGenerationInput |
! Input query to clad to generate a gradient | |
CallFuncSignature | fHessFuncPtr = nullptr |
! Function pointer, owned by the JIT. | |
std::string | fHessGenerationInput |
! Input query to clad to generate a hessian | |
void * | fLambdaPtr = nullptr |
! Pointer to the lambda function | |
Bool_t | fLazyInitialization = kFALSE |
! Transient flag to control lazy initialization (needed for reading from files) | |
std::unique_ptr< TMethodCall > | fMethod |
! Pointer to methodcall | |
Bool_t | fReadyToExecute |
! Transient to force initialization | |
std::string | fSavedInputFormula |
! Unique name used to defined the function and used in the global map (need to be saved in case of lazy initialization) | |
Static Private Attributes | |
static bool | fIsCladRuntimeIncluded = false |
Additional Inherited Members | |
Protected Types inherited from TObject | |
enum | { kOnlyPrepStep = (1ULL << ( 3 )) } |
#include "inc/TFormula.h"
Definition at line 104 of file TFormula.h.
using TFormula::CladStorage = std::vector<Double_t> |
Definition at line 184 of file TFormula.h.
Enumerator | |
---|---|
kNotGlobal | Don't store in gROOT->GetListOfFunction (it should be protected) |
kNormalized | Set to true if the TFormula (ex gausn) is normalized. |
kLinear | Set to true if the TFormula is for linear fitting. |
kLambda | Set to true if TFormula has been build with a lambda. |
Definition at line 178 of file TFormula.h.
TFormula::TFormula | ( | ) |
Definition at line 438 of file TFormula.cxx.
|
override |
Definition at line 465 of file TFormula.cxx.
TFormula::TFormula | ( | const char * | name, |
const char * | formula = "" , |
||
bool | addToGlobList = true , |
||
bool | vectorize = false |
||
) |
Definition at line 482 of file TFormula.cxx.
TFormula::TFormula | ( | const char * | name, |
const char * | formula, | ||
int | ndim, | ||
int | npar, | ||
bool | addToGlobList = true |
||
) |
Constructor from a full compile-able C++ expression.
Definition at line 528 of file TFormula.cxx.
TFormula::TFormula | ( | const TFormula & | formula | ) |
Definition at line 575 of file TFormula.cxx.
Definition at line 194 of file TFormula.h.
Adds variable to known variables, and reprocess formula.
Definition at line 2569 of file TFormula.cxx.
Adds multiple variables.
First argument is an array of pairs<TString,Double>, where first argument is name of variable, second argument represents value. size - number of variables passed in first argument
Definition at line 2602 of file TFormula.cxx.
|
static |
|
inlinestaticconstexpr |
Definition at line 289 of file TFormula.h.
|
overridevirtual |
Clear the formula setting expression to empty and reset the variables and parameters containers.
Reimplemented from TObject.
Definition at line 767 of file TFormula.cxx.
Int_t TFormula::Compile | ( | const char * | expression = "" | ) |
Compile the given expression with Cling backward compatibility method to be used in combination with the empty constructor if no expression is given , the current stored formula (retrieved with GetExpFormula()) or the title is used.
return 0 if the formula compilation is successful
Definition at line 650 of file TFormula.cxx.
|
overridevirtual |
|
inlinestatic |
Definition at line 289 of file TFormula.h.
|
protected |
Adds parameter to known parameters.
User should use SetParameter, because parameters are added during initialization part, and after that adding new will be pointless.
Definition at line 2743 of file TFormula.cxx.
Evaluate formula.
If formula is not ready to execute(missing parameters/variables), print these which are not known. If parameter has default value, and has not been set, appropriate warning is shown.
Definition at line 3359 of file TFormula.cxx.
Definition at line 2949 of file TFormula.cxx.
Double_t TFormula::Eval | ( | Args... | args | ) | const |
Set first 1, 2, 3 or 4 variables (e.g.
x, y, z and t) and evaluate formula.
Definition at line 324 of file TFormula.h.
Definition at line 3078 of file TFormula.cxx.
|
inline |
Definition at line 244 of file TFormula.h.
|
protected |
Extracts functors from formula, and put them in fFuncs.
Simple grammar:
<function>
:= name(arg1,arg2...)<variable>
:= name<parameter>
:= [number]<name>
:= String containing lower and upper letters, numbers, underscores<number>
:= Integer number Operators are omitted. Definition at line 1832 of file TFormula.cxx.
|
private |
Fill structures with default variables, constants and function shortcuts.
Definition at line 902 of file TFormula.cxx.
|
private |
Fill map with parametrized functions.
Definition at line 2417 of file TFormula.cxx.
|
private |
Fill the shortcuts for vectorized functions We will replace for example sin with vecCore::Mat::Sin.
Definition at line 970 of file TFormula.cxx.
bool TFormula::GenerateGradientPar | ( | ) |
Generate gradient computation routine with respect to the parameters.
returns true on success.
Definition at line 3192 of file TFormula.cxx.
bool TFormula::GenerateHessianPar | ( | ) |
Generate hessian computation routine with respect to the parameters.
returns true on success.
Definition at line 3256 of file TFormula.cxx.
Return the expression formula.
Definition at line 3524 of file TFormula.cxx.
TString TFormula::GetGradientFormula | ( | ) | const |
Definition at line 3594 of file TFormula.cxx.
|
inlineprivate |
Definition at line 128 of file TFormula.h.
TString TFormula::GetHessianFormula | ( | ) | const |
Definition at line 3602 of file TFormula.cxx.
|
inlineprivate |
Definition at line 131 of file TFormula.h.
Return linear part.
Definition at line 2553 of file TFormula.cxx.
|
inline |
Definition at line 259 of file TFormula.h.
|
inline |
Definition at line 260 of file TFormula.h.
|
inline |
Definition at line 261 of file TFormula.h.
Double_t TFormula::GetParameter | ( | const char * | name | ) | const |
Returns parameter value given by string.
Definition at line 2833 of file TFormula.cxx.
Return parameter value given by integer.
Definition at line 2847 of file TFormula.cxx.
Double_t * TFormula::GetParameters | ( | ) | const |
Definition at line 2873 of file TFormula.cxx.
void TFormula::GetParameters | ( | Double_t * | params | ) | const |
Definition at line 2880 of file TFormula.cxx.
const char * TFormula::GetParName | ( | Int_t | ipar | ) | const |
Return parameter name given by integer.
Definition at line 2859 of file TFormula.cxx.
Int_t TFormula::GetParNumber | ( | const char * | name | ) | const |
Return parameter index given a name (return -1 for not existing parameters) non need to print an error.
Definition at line 2821 of file TFormula.cxx.
|
inline |
Definition at line 253 of file TFormula.h.
Double_t TFormula::GetVariable | ( | const char * | name | ) | const |
Returns variable value.
Definition at line 2686 of file TFormula.cxx.
Returns variable name given its position in the array.
Definition at line 2712 of file TFormula.cxx.
Int_t TFormula::GetVarNumber | ( | const char * | name | ) | const |
Returns variable number (positon in array) given its name.
Definition at line 2699 of file TFormula.cxx.
Compute the gradient with respect to the parameter passing a buffer with a size at least equal to the number of parameters.
Note that the result buffer needs to be initialized to zero before passed to this function.
Definition at line 3249 of file TFormula.cxx.
void TFormula::GradientPar | ( | const Double_t * | x, |
TFormula::CladStorage & | result | ||
) |
Compute the gradient employing automatic differentiation.
a CladStorageObject, i.e.
[in] | x | - The given variables, if nullptr the already stored variables are used. |
[out] | result | - The result of the computation wrt each direction. |
a std::vector, which has the size as the nnumber of parameters. Note that the result buffer needs to be initialized to zero before passing it to this function.
Definition at line 3222 of file TFormula.cxx.
|
private |
Handling exponentiation Can handle multiple carets, eg.
2^3^4 will be treated like 2^(3^4)
Definition at line 1635 of file TFormula.cxx.
|
private |
Handling user functions (and parametrized functions) to take variables and optionally parameters as arguments.
Definition at line 1351 of file TFormula.cxx.
|
private |
Handle linear functions defined with the operator ++.
Definition at line 1735 of file TFormula.cxx.
|
private |
Handling parametrized functions Function can be normalized, and have different variable then x.
Variables should be placed in brackets after function name. No brackets are treated like [x]. Normalized function has char 'n' after name, eg. gausn[var](0) will be replaced with [0]*exp(-0.5*((var-[1])/[2])^2)/(sqrt(2*pi)*[2])
Adding function is easy, just follow these rules, and add to TFormula::FillParametrizedFunctions
defined further below:
Definition at line 1103 of file TFormula.cxx.
|
private |
Handling parameter ranges, in the form of [1..5].
Definition at line 1320 of file TFormula.cxx.
|
private |
Handling polN If before 'pol' exist any name, this name will be treated as variable used in polynomial eg.
varpol2(5) will be replaced with: [5] + [6]*var + [7]*var^2 Empty name is treated like variable x.
Definition at line 1002 of file TFormula.cxx.
|
inline |
Definition at line 232 of file TFormula.h.
|
inline |
Definition at line 237 of file TFormula.h.
|
inlineprivate |
Definition at line 134 of file TFormula.h.
|
inlineprivate |
Definition at line 137 of file TFormula.h.
Definition at line 3311 of file TFormula.cxx.
void TFormula::HessianPar | ( | const Double_t * | x, |
TFormula::CladStorage & | result | ||
) |
Compute the gradient employing automatic differentiation.
[in] | x | - The given variables, if nullptr the already stored variables are used. |
[out] | result | - The 2D hessian matrix flattened to form a vector in row-major order. |
Definition at line 3286 of file TFormula.cxx.
|
private |
Definition at line 605 of file TFormula.cxx.
|
private |
Inputs formula, transfered to C++ code into Cling.
Definition at line 876 of file TFormula.cxx.
|
inlineoverridevirtual |
Reimplemented from TObject.
Definition at line 289 of file TFormula.h.
Definition at line 353 of file TFormula.cxx.
|
staticprotected |
Definition at line 294 of file TFormula.cxx.
Definition at line 312 of file TFormula.cxx.
|
staticprotected |
Definition at line 306 of file TFormula.cxx.
Definition at line 330 of file TFormula.cxx.
|
inline |
Definition at line 273 of file TFormula.h.
|
staticprotected |
Definition at line 286 of file TFormula.cxx.
Definition at line 318 of file TFormula.cxx.
|
inline |
Definition at line 271 of file TFormula.h.
|
inline |
Definition at line 272 of file TFormula.h.
= operator.
Definition at line 597 of file TFormula.cxx.
|
private |
Sets TMethodCall to function inside Cling environment.
TFormula uses it to execute function. After call, TFormula should be ready to evaluate formula. Returns false on failure.
Definition at line 861 of file TFormula.cxx.
prepare the formula to be executed normally is called with fFormula
Definition at line 1793 of file TFormula.cxx.
|
protected |
Preprocessing of formula Replace all ** by ^, and removes spaces.
Handle also parametrized functions like polN,gaus,expo,landau and exponentiation. Similar functionality should be added here.
Definition at line 1771 of file TFormula.cxx.
|
overridevirtual |
Print the formula and its attributes.
Reimplemented from TObject.
Definition at line 3611 of file TFormula.cxx.
|
protected |
Iterates through functors in fFuncs and performs the appropriate action.
If functor has 0 arguments (has only name) can be:
Definition at line 2077 of file TFormula.cxx.
|
private |
Re-initialize eval method.
This function is called by DoEval and DoEvalVector in case of a previous failure or in case of reading from a file
Definition at line 3467 of file TFormula.cxx.
|
private |
Definition at line 405 of file TFormula.cxx.
|
protected |
Replace in Formula expression the parameter name.
Definition at line 3020 of file TFormula.cxx.
|
overridevirtual |
Set the name of the formula.
We need to allow the list of function to properly handle the hashes.
Reimplemented from TNamed.
Definition at line 2640 of file TFormula.cxx.
void TFormula::SetParameter | ( | const char * | name, |
Double_t | value | ||
) |
Sets parameter value.
Definition at line 2893 of file TFormula.cxx.
Set a parameter given a parameter index.
The parameter index is by default the alphabetic order given to the parameters, apart if the users has defined explicitly the parameter names.
Definition at line 2980 of file TFormula.cxx.
void TFormula::SetParameters | ( | const Double_t * | params | ) |
Set a vector of parameters value.
Order in the vector is by default the alphabetic order given to the parameters apart if the users has defined explicitly the parameter names
Definition at line 2970 of file TFormula.cxx.
void TFormula::SetParameters | ( | Double_t | arg1, |
Args &&... | args | ||
) |
Set a list of parameters.
The order is by default the alphabetic order given to the parameters, apart if the users has defined explicitly the parameter names. NaN values will be skipped, meaning that the corresponding parameters will not be changed.
Definition at line 299 of file TFormula.h.
void TFormula::SetParName | ( | Int_t | ipar, |
const char * | name | ||
) |
Definition at line 2990 of file TFormula.cxx.
void TFormula::SetParNames | ( | Args &&... | args | ) |
Set parameter names.
Empty strings will be skipped, meaning that the corresponding name will not be changed.
Definition at line 311 of file TFormula.h.
|
protected |
Set parameter names only in case of pre-defined functions.
Definition at line 2471 of file TFormula.cxx.
Sets variable value.
Definition at line 2728 of file TFormula.cxx.
Sets multiple variables.
First argument is an array of pairs<TString,Double>, where first argument is name of variable, second argument represents value. size - number of variables passed in first argument
Definition at line 2669 of file TFormula.cxx.
void TFormula::SetVectorized | ( | Bool_t | vectorized | ) |
Definition at line 3046 of file TFormula.cxx.
|
overridevirtual |
|
inline |
Definition at line 289 of file TFormula.h.
|
private |
Flag to control if all parameters are setted.
Definition at line 98 of file TFormula.h.
|
private |
! Transient to force re-initialization
Definition at line 97 of file TFormula.h.
|
private |
! Input function passed to Cling
Definition at line 93 of file TFormula.h.
|
private |
! Unique name passed to Cling to define the function ( double clingName(double*x, double*p) )
Definition at line 101 of file TFormula.h.
|
private |
Parameter values.
Definition at line 95 of file TFormula.h.
|
private |
! Cached variables
Definition at line 94 of file TFormula.h.
!
Definition at line 146 of file TFormula.h.
|
protected |
String representing the formula expression.
Definition at line 148 of file TFormula.h.
|
private |
! Function pointer, owned by the JIT.
Definition at line 107 of file TFormula.h.
|
protected |
!
Definition at line 143 of file TFormula.h.
!
Definition at line 147 of file TFormula.h.
|
private |
! Function pointer, owned by the JIT.
Definition at line 108 of file TFormula.h.
|
private |
! Input query to clad to generate a gradient
Definition at line 105 of file TFormula.h.
|
private |
! Function pointer, owned by the JIT.
Definition at line 109 of file TFormula.h.
|
private |
! Input query to clad to generate a hessian
Definition at line 106 of file TFormula.h.
|
staticprivate |
Definition at line 111 of file TFormula.h.
|
private |
! Pointer to the lambda function
Definition at line 110 of file TFormula.h.
! Transient flag to control lazy initialization (needed for reading from files)
Definition at line 99 of file TFormula.h.
|
protected |
Vector of linear functions.
Definition at line 152 of file TFormula.h.
|
private |
! Pointer to methodcall
Definition at line 100 of file TFormula.h.
|
protected |
Dimension - needed for lambda expressions.
Definition at line 149 of file TFormula.h.
|
protected |
! Number of parameter (transient since we save the vector)
Definition at line 150 of file TFormula.h.
|
protected |
Number used to identify pre-defined functions (gaus, expo,..)
Definition at line 151 of file TFormula.h.
|
protected |
|| List of parameter names
Definition at line 145 of file TFormula.h.
|
private |
! Transient to force initialization
Definition at line 96 of file TFormula.h.
|
private |
! Unique name used to defined the function and used in the global map (need to be saved in case of lazy initialization)
Definition at line 102 of file TFormula.h.
|
protected |
! List of variable names
Definition at line 144 of file TFormula.h.
|
protected |
Whether we should use vectorized or regular variables.
Definition at line 153 of file TFormula.h.