 ROOT   Reference Guide 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.

### Example of valid expressions:

• sin(x)/x
• *sin(x) + *exp(-*x)
• x + y**2
• x^2 + y^2
• *pow(,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 *exp(-0.5*((x-)/)**2) and (0) means start numbering parameters at 0
• gausn(0) is a substitute for *exp(-0.5*((x-)/)**2)/(sqrt(2*pi)*)) and (0) means start numbering parameters at 0
• expo(3) is a substitute for exp(+*x)
• pol3(5) is a substitute for par+par*x+par*x**2+par*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(+*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(, )
• 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&lt;0.5 || x&gt;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,x,x,x,....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:

TFormula f("f", "x* - y*");
Double_t p[] = {40, 30};
Double_t x[] = {1, 2};
f.SetParameters(p);
#define f(i)
Definition: RSha256.hxx:104
double Double_t
Definition: RtypesCore.h:59
The Formula class.
Definition: TFormula.h:87
Definition: TFormula.h:184
Double_t x[n]
Definition: legend1.C:17

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:

f.HessianPar(x, hess);

### List of predefined functions

The list of available predefined functions which can be used as shortcuts is the following:

1. One Dimensional functions:
• 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.
2. Two Dimensional functions:
• 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.
3. Three Dimensional functions:
• 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))

### An expanded note on variables and parameters

In a TFormula, a variable is a defined by a name x, y, z or t or an index like x, x, x; that is x[N] where N is an integer.

TFormula("", "x * x + 10")

Parameters are similar and can take any name. It is specified using brackets e.g. [expected_mass] or .

TFormula("", "exp([expected_mass])-1")

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.

TFormula tf ("", "*exp(-*t)");
tf.SetParameter(0, 1);
tf.SetParameter(1, 0.5);
for (auto & event : events) {
tf.Eval(event.t);
}

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.

Int_t params = {1, 2}; // {vel_x, vel_y}
TFormula tf ("", "[vel_x]/sqrt(([vel_x + vel_y])**2)");
tf.EvalPar(nullptr, params);
int Int_t
Definition: RtypesCore.h:45

### A note on operators

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 root -l -q -e TFormula("", "x+(10%3)").Eval(0) but not root -l -q -e TFormula("", "x%10").Eval(0).

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 [n]*x + [n+1]*2 where n is the first previously unused parameter number.

Definition at line 86 of file TFormula.h.

## Public Types

using CladStorage = std::vector< Double_t >

enum  EStatusBits { kNotGlobal = BIT(10) , kNormalized = BIT(14) , kLinear = BIT(16) , kLambda = BIT(17) } Public Types inherited from TObject
enum  {
kIsOnHeap = 0x01000000 , kNotDeleted = 0x02000000 , kZombie = 0x04000000 , kInconsistent = 0x08000000 ,
}

enum  { kSingleKey = BIT(0) , kOverwrite = BIT(1) , kWriteDelete = BIT(2) }

enum  EDeprecatedStatusBits { kObjInCanvas = BIT(3) }

enum  EStatusBits {
kCanDelete = BIT(0) , kMustCleanup = BIT(3) , kIsReferenced = BIT(4) , kHasUUID = BIT(5) ,
kCannotPick = BIT(6) , kNoContextMenu = BIT(8) , kInvalidObject = BIT(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. More...

TFormula (const char *name, const char *formula="", bool addToGlobList=true, bool vectorize=false)

TFormula (const TFormula &formula)

virtual ~TFormula ()

void AddParameter (const TString &name, Double_t value=0)

void AddVariable (const TString &name, Double_t value=0)

void AddVariables (const TString *vars, const Int_t size)

virtual void Clear (Option_t *option="")
Clear the formula setting expression to empty and reset the variables and parameters containers. More...

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. More...

virtual void Copy (TObject &f1) const
Copy this to obj. More...

Double_t Eval (Double_t x) const

Double_t Eval (Double_t x, Double_t y) const

Double_t Eval (Double_t x, Double_t y, Double_t z) const

Double_t Eval (Double_t x, Double_t y, Double_t z, Double_t t) const

Double_t EvalPar (const Double_t *x, const Double_t *params=0) const

template<class T >
EvalPar (const T *x, const Double_t *params=0) const

Generate gradient computation routine with respect to the parameters. More...

bool GenerateHessianPar ()
Generate hessian computation routine with respect to the parameters. More...

TString GetExpFormula (Option_t *option="") const

TString GetHessianFormula () const

const TObjectGetLinearPart (Int_t i) const

Int_t GetNdim () const

Int_t GetNpar () const

Int_t GetNumber () const

Double_t GetParameter (const char *name) const

Double_t GetParameter (Int_t param) const

Double_tGetParameters () const

void GetParameters (Double_t *params) const

const char * GetParName (Int_t ipar) const

Int_t GetParNumber (const char *name) const

Double_t GetVariable (const char *name) const

TString GetVarName (Int_t ivar) const

Int_t GetVarNumber (const char *name) const

void GradientPar (const Double_t *x, Double_t *result)

Compute the gradient employing automatic differentiation. More...

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. More...

Bool_t IsLinear () const

Bool_t IsValid () const

Bool_t IsVectorized () const

TFormulaoperator= (const TFormula &rhs)
= operator. More...

void Print (Option_t *option="") const
Print TNamed name and title. More...

void SetName (const char *name)
Set the name of the TNamed. More...

void SetParameter (const char *name, Double_t value)

void SetParameter (Int_t param, Double_t value)

void SetParameters (const Double_t *params)

void SetParameters (Double_t p0, Double_t p1, Double_t p2=0, Double_t p3=0, Double_t p4=0, Double_t p5=0, Double_t p6=0, Double_t p7=0, Double_t p8=0, Double_t p9=0, Double_t p10=0)

void SetParName (Int_t ipar, const char *name)

void SetParNames (const char *name0="p0", const char *name1="p1", const char *name2="p2", const char *name3="p3", const char *name4="p4", const char *name5="p5", const char *name6="p6", const char *name7="p7", const char *name8="p8", const char *name9="p9", const char *name10="p10")

void SetVariable (const TString &name, Double_t value)

void SetVariables (const std::pair< TString, Double_t > *vars, const Int_t size)

void SetVectorized (Bool_t vectorized) Public Member Functions inherited from TNamed
TNamed ()

TNamed (const char *name, const char *title)

TNamed (const TNamed &named)
TNamed copy ctor. More...

TNamed (const TString &name, const TString &title)

virtual ~TNamed ()
TNamed destructor. More...

virtual void Clear (Option_t *option="")
Set name and title to empty strings (""). More...

virtual TObjectClone (const char *newname="") const
Make a clone of an object using the Streamer facility. More...

virtual Int_t Compare (const TObject *obj) const
Compare two TNamed objects. More...

virtual void Copy (TObject &named) const
Copy this to obj. More...

virtual void FillBuffer (char *&buffer)
Encode TNamed into output buffer. More...

virtual const char * GetName () const
Returns name of object. More...

virtual const char * GetTitle () const
Returns title of object. More...

virtual ULong_t Hash () const
Return hash value for this object. More...

virtual Bool_t IsSortable () const

virtual void ls (Option_t *option="") const
List TNamed name and title. More...

TNamedoperator= (const TNamed &rhs)
TNamed assignment operator. More...

virtual void Print (Option_t *option="") const
Print TNamed name and title. More...

virtual void SetName (const char *name)
Set the name of the TNamed. More...

virtual void SetNameTitle (const char *name, const char *title)
Set all the TNamed parameters (name and title). More...

virtual void SetTitle (const char *title="")
Set the title of the TNamed. More...

virtual Int_t Sizeof () const
Return size of the TNamed part of the TObject. More... Public Member Functions inherited from TObject
TObject ()
TObject constructor. More...

TObject (const TObject &object)
TObject copy ctor. More...

virtual ~TObject ()
TObject destructor. More...

void AbstractMethod (const char *method) const
Use this method to implement an "abstract" method that you don't want to leave purely abstract. More...

Append graphics object to current pad. More...

virtual void Browse (TBrowser *b)
Browse object. May be overridden for another default action. More...

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. More...

virtual const char * ClassName () const
Returns name of class to which the object belongs. More...

virtual void Clear (Option_t *="")

virtual TObjectClone (const char *newname="") const
Make a clone of an object using the Streamer facility. More...

virtual Int_t Compare (const TObject *obj) const
Compare abstract method. More...

virtual void Copy (TObject &object) const
Copy this to obj. More...

virtual void Delete (Option_t *option="")
Delete this object. More...

virtual Int_t DistancetoPrimitive (Int_t px, Int_t py)
Computes distance from point (px,py) to the object. More...

virtual void Draw (Option_t *option="")
Default Draw method for all objects. More...

virtual void DrawClass () const
Draw class inheritance tree of the class to which this object belongs. More...

virtual TObjectDrawClone (Option_t *option="") const
Draw a clone of this object in the current selected pad for instance with: gROOT->SetSelectedPad(gPad). More...

virtual void Dump () const
Dump contents of object on stdout. More...

virtual void Error (const char *method, const char *msgfmt,...) const
Issue error message. More...

virtual void Execute (const char *method, const char *params, Int_t *error=0)
Execute method on this object with the given parameter string, e.g. More...

virtual void Execute (TMethod *method, TObjArray *params, Int_t *error=0)
Execute method on this object with parameters stored in the TObjArray. More...

virtual void ExecuteEvent (Int_t event, Int_t px, Int_t py)
Execute action corresponding to an event at (px,py). More...

virtual void Fatal (const char *method, const char *msgfmt,...) const
Issue fatal error message. More...

virtual TObjectFindObject (const char *name) const
Must be redefined in derived classes. More...

virtual TObjectFindObject (const TObject *obj) const
Must be redefined in derived classes. More...

virtual Option_tGetDrawOption () const
Get option used by the graphics system to draw this object. More...

virtual const char * GetIconName () const
Returns mime type name of object. More...

virtual const char * GetName () const
Returns name of object. More...

virtual char * GetObjectInfo (Int_t px, Int_t py) const
Returns string containing info about the object at position (px,py). More...

virtual Option_tGetOption () const

virtual const char * GetTitle () const
Returns title of object. More...

virtual UInt_t GetUniqueID () const
Return the unique object id. More...

virtual Bool_t HandleTimer (TTimer *timer)
Execute action in response of a timer timing out. More...

virtual ULong_t Hash () const
Return hash value for this object. More...

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. More...

virtual void Info (const char *method, const char *msgfmt,...) const
Issue info message. More...

virtual Bool_t InheritsFrom (const char *classname) const
Returns kTRUE if object inherits from class "classname". More...

virtual Bool_t InheritsFrom (const TClass *cl) const
Returns kTRUE if object inherits from TClass cl. More...

virtual void Inspect () const
Dump contents of this object in a graphics canvas. More...

void InvertBit (UInt_t f)

virtual Bool_t IsEqual (const TObject *obj) const
Default equal comparison (objects are equal if they have the same address in memory). More...

virtual Bool_t IsFolder () const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects). More...

R__ALWAYS_INLINE Bool_t IsOnHeap () const

virtual Bool_t IsSortable () const

R__ALWAYS_INLINE Bool_t IsZombie () const

virtual void ls (Option_t *option="") const
The ls function lists the contents of a class on stdout. More...

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). More...

virtual Bool_t Notify ()
This method must be overridden to handle object notification. More...

void Obsolete (const char *method, const char *asOfVers, const char *removedFromVers) const
Use this method to declare a method obsolete. More...

void operator delete (void *ptr)
Operator delete. More...

void operator delete[] (void *ptr)
Operator delete []. More...

voidoperator new (size_t sz)

voidoperator new (size_t sz, void *vp)

voidoperator new[] (size_t sz)

voidoperator new[] (size_t sz, void *vp)

TObjectoperator= (const TObject &rhs)
TObject assignment operator. More...

virtual void Paint (Option_t *option="")
This method must be overridden if a class wants to paint itself. More...

virtual void Pop ()
Pop on object drawn in a pad to the top of the display list. More...

virtual void Print (Option_t *option="") const
This method must be overridden when a class wants to print itself. More...

virtual Int_t Read (const char *name)
Read contents of object with specified name from the current directory. More...

virtual void RecursiveRemove (TObject *obj)
Recursively remove this object from a list. More...

void ResetBit (UInt_t f)

virtual void SaveAs (const char *filename="", Option_t *option="") const
Save this object in the file specified by filename. More...

virtual void SavePrimitive (std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out". More...

void SetBit (UInt_t f)

void SetBit (UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f. More...

virtual void SetDrawOption (Option_t *option="")
Set drawing option for object. More...

virtual void SetUniqueID (UInt_t uid)
Set the unique object id. More...

virtual void SysError (const char *method, const char *msgfmt,...) const
Issue system error message. More...

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. More...

virtual void Warning (const char *method, const char *msgfmt,...) const
Issue warning message. More...

virtual Int_t Write (const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory. More...

virtual Int_t Write (const char *name=0, Int_t option=0, Int_t bufsize=0) const
Write this object to the current directory. More...

## Protected Member Functions

void DoAddParameter (const TString &name, Double_t value, bool processFormula)

Double_t DoEval (const Double_t *x, const Double_t *p=nullptr) const

void DoSetParameters (const Double_t *p, Int_t size)

void ExtractFunctors (TString &formula)

Bool_t PrepareFormula (TString &formula)

void PreProcessFormula (TString &formula)

void ProcessFormula (TString &formula)

void ReplaceParamName (TString &formula, const TString &oldname, const TString &name)

void SetPredefinedParamNames () 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). More...

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_tfConsts
! More...

TString fFormula
String representing the formula expression. More...

std::list< TFormulaFunctionfFuncs
! More...

std::map< TString, TStringfFunctionsShortcuts
! More...

std::vector< TObject * > fLinearParts
Vector of linear functions. More...

Int_t fNdim
Dimension - needed for lambda expressions. More...

Int_t fNpar
! Number of parameter (transient since we save the vector) More...

Int_t fNumber
! More...

std::map< TString, Int_t, TFormulaParamOrderfParams
|| List of parameter names More...

std::map< TString, TFormulaVariablefVars
! List of variable names More...

Bool_t fVectorized = false
Whether we should use vectorized or regular variables. More... 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. More...

void FillParametrizedFunctions (std::map< std::pair< TString, Int_t >, std::pair< TString, TString > > &functions)

void FillVecFunctionsShurtCuts ()
Fill the shortcuts for vectorized functions We will replace for example sin with vecCore::Mat::Sin. More...

std::string GetHessianFuncName () const

void HandleExponentiation (TString &formula)

void HandleFunctionArguments (TString &formula)

void HandleLinear (TString &formula)

void HandleParametrizedFunctions (TString &formula)
Handling parametrized functions Function can be normalized, and have different variable then x. More...

void HandleParamRanges (TString &formula)
Handling parameter ranges, in the form of [1..5]. More...

void HandlePolN (TString &formula)
Handling polN If before 'pol' exist any name, this name will be treated as variable used in polynomial eg. More...

bool HasHessianGenerationFailed () const

Bool_t InitLambdaExpression (const char *formula)

void InputFormulaIntoCling ()
Inputs formula, transfered to C++ code into Cling. More...

Bool_t PrepareEvalMethod ()
Sets TMethodCall to function inside Cling environment. More...

void ReInitializeEvalMethod ()

void ReplaceAllNames (TString &formula, std::map< TString, TString > &substitutions)

## Static Private Member Functions

static Bool_t IsDefaultVariableName (const TString &name)

## Private Attributes

Flag to control if all parameters are setted. More...

std::atomic< Bool_tfClingInitialized
! Transient to force re-initialization More...

TString fClingInput
! Input function passed to Cling More...

TString fClingName
! Unique name passed to Cling to define the function ( double clingName(double*x, double*p) ) More...

std::vector< Double_tfClingParameters
Parameter values. More...

std::vector< Double_tfClingVariables
! Cached variables More...

CallFuncSignature fFuncPtr = nullptr
! Function pointer, owned by the JIT. More...

! Function pointer, owned by the JIT. More...

CallFuncSignature fHessFuncPtr = nullptr
! Function pointer, owned by the JIT. More...

std::string fHessGenerationInput
! Input query to clad to generate a hessian More...

voidfLambdaPtr = nullptr
! Pointer to the lambda function More...

Bool_t fLazyInitialization = kFALSE
! Transient flag to control lazy initialization (needed for reading from files) More...

std::unique_ptr< TMethodCallfMethod
! Pointer to methodcall More...

! Transient to force initialization More...

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) More...

## Static Private Attributes Static Public Member Functions inherited from TObject
static Longptr_t GetDtorOnly ()
Return destructor only flag. More...

static Bool_t GetObjectStat ()
Get status of object stat flag. More...

static void SetDtorOnly (void *obj)
Set destructor only flag. More...

static void SetObjectStat (Bool_t stat)
Turn on/off tracking of objects in the TObjectTable. More... Protected Types inherited from TObject
enum  { kOnlyPrepStep = BIT(3) }

#include "inc/TFormula.h"

Inheritance diagram for TFormula:
[legend]

## ◆ CallFuncSignature

 private

Definition at line 102 of file TFormula.h.

Definition at line 184 of file TFormula.h.

## ◆ EStatusBits

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() [1/4]

 TFormula::TFormula ( )

Definition at line 439 of file TFormula.cxx.

## ◆ ~TFormula()

 TFormula::~TFormula ( )
virtual

Definition at line 465 of file TFormula.cxx.

## ◆ TFormula() [2/4]

 TFormula::TFormula ( const char * name, const char * formula = "", bool addToGlobList = true, bool vectorize = false )

Definition at line 482 of file TFormula.cxx.

## ◆ TFormula() [3/4]

 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() [4/4]

 TFormula::TFormula ( const TFormula & formula )

Definition at line 575 of file TFormula.cxx.

## Member Function Documentation

 void TFormula::AddParameter ( const TString & name, Double_t value = 0 )
inline

Definition at line 194 of file TFormula.h.

 void TFormula::AddVariable ( const TString & name, Double_t value = 0 )

 void TFormula::AddVariables ( const TString * vars, const Int_t size )

## ◆ Clear()

 void TFormula::Clear ( Option_t * option = "" )
virtual

Clear the formula setting expression to empty and reset the variables and parameters containers.

Reimplemented from TNamed.

Definition at line 769 of file TFormula.cxx.

## ◆ Compile()

 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 652 of file TFormula.cxx.

## ◆ Copy()

 void TFormula::Copy ( TObject & named ) const
virtual

Copy this to obj.

Reimplemented from TNamed.

Definition at line 686 of file TFormula.cxx.

 void TFormula::DoAddParameter ( const TString & name, Double_t value, bool processFormula )
protected

## ◆ DoEval()

 Double_t TFormula::DoEval ( const Double_t * x, const Double_t * p = nullptr ) const
protected

## ◆ DoSetParameters()

 void TFormula::DoSetParameters ( const Double_t * p, Int_t size )
protected

## ◆ Eval() [1/4]

 Double_t TFormula::Eval ( Double_t x ) const

## ◆ Eval() [2/4]

 Double_t TFormula::Eval ( Double_t x, Double_t y ) const

## ◆ Eval() [3/4]

 Double_t TFormula::Eval ( Double_t x, Double_t y, Double_t z ) const

## ◆ Eval() [4/4]

 Double_t TFormula::Eval ( Double_t x, Double_t y, Double_t z, Double_t t ) const

## ◆ EvalPar() [1/2]

 Double_t TFormula::EvalPar ( const Double_t * x, const Double_t * params = 0 ) const

## ◆ EvalPar() [2/2]

template<class T >
 T TFormula::EvalPar ( const T * x, const Double_t * params = 0 ) const
inline

Definition at line 246 of file TFormula.h.

## ◆ ExtractFunctors()

 void TFormula::ExtractFunctors ( TString & formula )
protected

## ◆ FillDefaults()

 void TFormula::FillDefaults ( )
private

Fill structures with default variables, constants and function shortcuts.

Definition at line 904 of file TFormula.cxx.

## ◆ FillParametrizedFunctions()

 void TFormula::FillParametrizedFunctions ( std::map< std::pair< TString, Int_t >, std::pair< TString, TString > > & functions )
private

## ◆ FillVecFunctionsShurtCuts()

 void TFormula::FillVecFunctionsShurtCuts ( )
private

Fill the shortcuts for vectorized functions We will replace for example sin with vecCore::Mat::Sin.

Definition at line 972 of file TFormula.cxx.

Generate gradient computation routine with respect to the parameters.

Returns

## ◆ GenerateHessianPar()

 bool TFormula::GenerateHessianPar ( )

Generate hessian computation routine with respect to the parameters.

Returns
true if a hessian was generated and HessianPar can be called.

## ◆ GetExpFormula()

 TString TFormula::GetExpFormula ( Option_t * option = "" ) const

inlineprivate

Definition at line 126 of file TFormula.h.

## ◆ GetHessianFormula()

 TString TFormula::GetHessianFormula ( ) const

## ◆ GetHessianFuncName()

 std::string TFormula::GetHessianFuncName ( ) const
inlineprivate

Definition at line 130 of file TFormula.h.

## ◆ GetLinearPart()

 const TObject * TFormula::GetLinearPart ( Int_t i ) const

## ◆ GetNdim()

 Int_t TFormula::GetNdim ( ) const
inline

Definition at line 256 of file TFormula.h.

## ◆ GetNpar()

 Int_t TFormula::GetNpar ( ) const
inline

Definition at line 257 of file TFormula.h.

## ◆ GetNumber()

 Int_t TFormula::GetNumber ( ) const
inline

Definition at line 258 of file TFormula.h.

## ◆ GetParameter() [1/2]

 Double_t TFormula::GetParameter ( const char * name ) const

## ◆ GetParameter() [2/2]

 Double_t TFormula::GetParameter ( Int_t param ) const

## ◆ GetParameters() [1/2]

 Double_t * TFormula::GetParameters ( ) const

## ◆ GetParameters() [2/2]

 void TFormula::GetParameters ( Double_t * params ) const

## ◆ GetParName()

 const char * TFormula::GetParName ( Int_t ipar ) const

## ◆ GetParNumber()

 Int_t TFormula::GetParNumber ( const char * name ) const

## ◆ GetVariable()

 Double_t TFormula::GetVariable ( const char * name ) const

## ◆ GetVarName()

 TString TFormula::GetVarName ( Int_t ivar ) const

## ◆ GetVarNumber()

 Int_t TFormula::GetVarNumber ( const char * name ) const

 void TFormula::GradientPar ( const Double_t * x, Double_t * result )

Compute the gradient employing automatic differentiation.

Parameters
 [in] x - The given variables, if nullptr the already stored variables are used. [out] result - The result of the computation wrt each direction.

## ◆ HandleExponentiation()

 void TFormula::HandleExponentiation ( TString & formula )
private

## ◆ HandleFunctionArguments()

 void TFormula::HandleFunctionArguments ( TString & formula )
private

## ◆ HandleLinear()

 void TFormula::HandleLinear ( TString & formula )
private

## ◆ HandleParametrizedFunctions()

 void TFormula::HandleParametrizedFunctions ( TString & formula )
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 *exp(-0.5*((var-)/)^2)/(sqrt(2*pi)*)

Adding function is easy, just follow these rules, and add to TFormula::FillParametrizedFunctions defined further below:

• Key for function map is pair of name and dimension of function
• value of key is a pair function body and normalized function body
• {Vn} is a place where variable appear, n represents n-th variable from variable list. Count starts from 0.
• [num] stands for parameter number. If user pass to function argument 5, num will stand for (5 + num) parameter.

Definition at line 1105 of file TFormula.cxx.

## ◆ HandleParamRanges()

 void TFormula::HandleParamRanges ( TString & formula )
private

Handling parameter ranges, in the form of [1..5].

Definition at line 1322 of file TFormula.cxx.

## ◆ HandlePolN()

 void TFormula::HandlePolN ( TString & formula )
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:  + *var + *var^2 Empty name is treated like variable x.

Definition at line 1004 of file TFormula.cxx.

inline

Definition at line 234 of file TFormula.h.

## ◆ HasGeneratedHessian()

 bool TFormula::HasGeneratedHessian ( ) const
inline

Definition at line 239 of file TFormula.h.

inlineprivate

Definition at line 134 of file TFormula.h.

## ◆ HasHessianGenerationFailed()

 bool TFormula::HasHessianGenerationFailed ( ) const
inlineprivate

Definition at line 137 of file TFormula.h.

## ◆ HessianPar() [1/2]

 void TFormula::HessianPar ( const Double_t * x, Double_t * result )

## ◆ HessianPar() [2/2]

 void TFormula::HessianPar ( const Double_t * x, TFormula::CladStorage & result )

Compute the gradient employing automatic differentiation.

Parameters
 [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.

## ◆ InitLambdaExpression()

 Bool_t TFormula::InitLambdaExpression ( const char * formula )
private

Definition at line 607 of file TFormula.cxx.

## ◆ InputFormulaIntoCling()

 void TFormula::InputFormulaIntoCling ( )
private

Inputs formula, transfered to C++ code into Cling.

Definition at line 878 of file TFormula.cxx.

## ◆ IsAParameterName()

 Bool_t TFormula::IsAParameterName ( const TString & formula, int ipos )
staticprotected

Definition at line 354 of file TFormula.cxx.

## ◆ IsBracket()

 Bool_t TFormula::IsBracket ( const char c )
staticprotected

Definition at line 295 of file TFormula.cxx.

## ◆ IsDefaultVariableName()

 Bool_t TFormula::IsDefaultVariableName ( const TString & name )
staticprivate

Definition at line 313 of file TFormula.cxx.

## ◆ IsFunctionNameChar()

 Bool_t TFormula::IsFunctionNameChar ( const char c )
staticprotected

Definition at line 307 of file TFormula.cxx.

 Bool_t TFormula::IsHexadecimal ( const TString & formula, int ipos )
staticprotected

Definition at line 331 of file TFormula.cxx.

## ◆ IsLinear()

 Bool_t TFormula::IsLinear ( ) const
inline

Definition at line 270 of file TFormula.h.

## ◆ IsOperator()

 Bool_t TFormula::IsOperator ( const char c )
staticprotected

Definition at line 287 of file TFormula.cxx.

## ◆ IsScientificNotation()

 Bool_t TFormula::IsScientificNotation ( const TString & formula, int ipos )
staticprotected

Definition at line 319 of file TFormula.cxx.

## ◆ IsValid()

 Bool_t TFormula::IsValid ( ) const
inline

Definition at line 268 of file TFormula.h.

## ◆ IsVectorized()

 Bool_t TFormula::IsVectorized ( ) const
inline

Definition at line 269 of file TFormula.h.

## ◆ operator=()

 TFormula & TFormula::operator= ( const TFormula & rhs )

= operator.

Definition at line 597 of file TFormula.cxx.

## ◆ PrepareEvalMethod()

 bool TFormula::PrepareEvalMethod ( )
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 863 of file TFormula.cxx.

## ◆ PrepareFormula()

 Bool_t TFormula::PrepareFormula ( TString & formula )
protected

## ◆ PreProcessFormula()

 void TFormula::PreProcessFormula ( TString & formula )
protected

## ◆ Print()

 void TFormula::Print ( Option_t * option = "" ) const
virtual

Print TNamed name and title.

Reimplemented from TNamed.

## ◆ ProcessFormula()

 void TFormula::ProcessFormula ( TString & formula )
protected

## ◆ ReInitializeEvalMethod()

 void TFormula::ReInitializeEvalMethod ( )
private

## ◆ ReplaceAllNames()

 void TFormula::ReplaceAllNames ( TString & formula, std::map< TString, TString > & substitutions )
private

Definition at line 406 of file TFormula.cxx.

## ◆ ReplaceParamName()

 void TFormula::ReplaceParamName ( TString & formula, const TString & oldname, const TString & name )
protected

## ◆ SetName()

 void TFormula::SetName ( const char * name )
virtual

Set the name of the TNamed.

WARNING: if the object is a member of a THashTable or THashList container the container must be Rehash()'ed after SetName(). For example the list of objects in the current directory is a THashList.

Reimplemented from TNamed.

## ◆ SetParameter() [1/2]

 void TFormula::SetParameter ( const char * name, Double_t value )

## ◆ SetParameter() [2/2]

 void TFormula::SetParameter ( Int_t param, Double_t value )

## ◆ SetParameters() [1/2]

 void TFormula::SetParameters ( const Double_t * params )

## ◆ SetParameters() [2/2]

 void TFormula::SetParameters ( Double_t p0, Double_t p1, Double_t p2 = 0, Double_t p3 = 0, Double_t p4 = 0, Double_t p5 = 0, Double_t p6 = 0, Double_t p7 = 0, Double_t p8 = 0, Double_t p9 = 0, Double_t p10 = 0 )

## ◆ SetParName()

 void TFormula::SetParName ( Int_t ipar, const char * name )

## ◆ SetParNames()

 void TFormula::SetParNames ( const char * name0 = "p0", const char * name1 = "p1", const char * name2 = "p2", const char * name3 = "p3", const char * name4 = "p4", const char * name5 = "p5", const char * name6 = "p6", const char * name7 = "p7", const char * name8 = "p8", const char * name9 = "p9", const char * name10 = "p10" )

## ◆ SetPredefinedParamNames()

 void TFormula::SetPredefinedParamNames ( )
protected

## ◆ SetVariable()

 void TFormula::SetVariable ( const TString & name, Double_t value )

## ◆ SetVariables()

 void TFormula::SetVariables ( const std::pair< TString, Double_t > * vars, const Int_t size )

## ◆ SetVectorized()

 void TFormula::SetVectorized ( Bool_t vectorized )

## Member Data Documentation

private

Flag to control if all parameters are setted.

Definition at line 96 of file TFormula.h.

## ◆ fClingInitialized

 std::atomic TFormula::fClingInitialized
private

! Transient to force re-initialization

Definition at line 95 of file TFormula.h.

## ◆ fClingInput

 TString TFormula::fClingInput
private

! Input function passed to Cling

Definition at line 91 of file TFormula.h.

## ◆ fClingName

 TString TFormula::fClingName
private

! Unique name passed to Cling to define the function ( double clingName(double*x, double*p) )

Definition at line 99 of file TFormula.h.

## ◆ fClingParameters

 std::vector TFormula::fClingParameters
private

Parameter values.

Definition at line 93 of file TFormula.h.

## ◆ fClingVariables

 std::vector TFormula::fClingVariables
private

! Cached variables

Definition at line 92 of file TFormula.h.

## ◆ fConsts

 std::map TFormula::fConsts
protected

!

Definition at line 146 of file TFormula.h.

## ◆ fFormula

 TString TFormula::fFormula
protected

String representing the formula expression.

Definition at line 148 of file TFormula.h.

## ◆ fFuncPtr

 CallFuncSignature TFormula::fFuncPtr = nullptr
private

! Function pointer, owned by the JIT.

Definition at line 105 of file TFormula.h.

## ◆ fFuncs

 std::list TFormula::fFuncs
protected

!

Definition at line 143 of file TFormula.h.

## ◆ fFunctionsShortcuts

 std::map TFormula::fFunctionsShortcuts
protected

!

Definition at line 147 of file TFormula.h.

private

! Function pointer, owned by the JIT.

Definition at line 106 of file TFormula.h.

private

Definition at line 103 of file TFormula.h.

## ◆ fHessFuncPtr

 CallFuncSignature TFormula::fHessFuncPtr = nullptr
private

! Function pointer, owned by the JIT.

Definition at line 107 of file TFormula.h.

## ◆ fHessGenerationInput

 std::string TFormula::fHessGenerationInput
private

! Input query to clad to generate a hessian

Definition at line 104 of file TFormula.h.

staticprivate

Definition at line 109 of file TFormula.h.

## ◆ fLambdaPtr

 void* TFormula::fLambdaPtr = nullptr
private

! Pointer to the lambda function

Definition at line 108 of file TFormula.h.

## ◆ fLazyInitialization

 Bool_t TFormula::fLazyInitialization = kFALSE
private

! Transient flag to control lazy initialization (needed for reading from files)

Definition at line 97 of file TFormula.h.

## ◆ fLinearParts

 std::vector TFormula::fLinearParts
protected

Vector of linear functions.

Definition at line 152 of file TFormula.h.

## ◆ fMethod

 std::unique_ptr TFormula::fMethod
private

! Pointer to methodcall

Definition at line 98 of file TFormula.h.

## ◆ fNdim

 Int_t TFormula::fNdim
protected

Dimension - needed for lambda expressions.

Definition at line 149 of file TFormula.h.

## ◆ fNpar

 Int_t TFormula::fNpar
protected

! Number of parameter (transient since we save the vector)

Definition at line 150 of file TFormula.h.

## ◆ fNumber

 Int_t TFormula::fNumber
protected

!

Definition at line 151 of file TFormula.h.

## ◆ fParams

 std::map TFormula::fParams
protected

|| List of parameter names

Definition at line 145 of file TFormula.h.

private

! Transient to force initialization

Definition at line 94 of file TFormula.h.

## ◆ fSavedInputFormula

 std::string TFormula::fSavedInputFormula
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 100 of file TFormula.h.

## ◆ fVars

 std::map TFormula::fVars
protected

! List of variable names

Definition at line 144 of file TFormula.h.

## ◆ fVectorized

 Bool_t TFormula::fVectorized = false
protected

Whether we should use vectorized or regular variables.

Definition at line 153 of file TFormula.h.

Libraries for TFormula: [legend]

The documentation for this class was generated from the following files: