ROOT » HIST » HIST » TFormula

class TFormula: public TNamed


The F O R M U L A 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 funtions 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
  • [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

In the last example above:

  • gaus(0) is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2) and (0) means start numbering parameters at 0
  • gausn(0) is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2)/(sqrt(2*pi)*[2])) and (0) means start numbering parameters at 0
  • expo(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)

TMath functions can be part of the expression, eg:

  • TMath::Landau(x)*sin(x)
  • TMath::Erf(x)

Formula may contain constans, eg:

  • sqrt2
  • e
  • pi
  • ln10
  • infinity

and more.

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 numer 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-dimensionals formula.

This class is not anymore the base class for the function classes TF1, but it has now adata member of TF1 which can be access via TF1::GetFormula.


Function Members (Methods)

public:
virtual~TFormula()
voidTObject::AbstractMethod(const char* method) const
voidAddParameter(const TString& name, Double_t value = 0)
voidAddVariable(const TString& name, Double_t value = 0)
voidAddVariables(const TString* vars, const Int_t size)
virtual voidTObject::AppendPad(Option_t* option = "")
virtual voidTObject::Browse(TBrowser* b)
static TClass*Class()
virtual const char*TObject::ClassName() const
virtual voidClear(Option_t* option = "")
virtual TObject*TNamed::Clone(const char* newname = "") const
virtual Int_tTNamed::Compare(const TObject* obj) const
Int_tCompile(const char* expression = "")
virtual voidCopy(TObject& f1) const
virtual voidTObject::Delete(Option_t* option = "")MENU
virtual Int_tTObject::DistancetoPrimitive(Int_t px, Int_t py)
virtual voidTObject::Draw(Option_t* option = "")
virtual voidTObject::DrawClass() constMENU
virtual TObject*TObject::DrawClone(Option_t* option = "") constMENU
virtual voidTObject::Dump() constMENU
virtual voidTObject::Error(const char* method, const char* msgfmt) const
Double_tEval(Double_t x) const
Double_tEval(Double_t x, Double_t y) const
Double_tEval(Double_t x, Double_t y, Double_t z) const
Double_tEval(Double_t x, Double_t y, Double_t z, Double_t t) const
Double_tEvalPar(const Double_t* x, const Double_t* params = 0) const
virtual voidTObject::Execute(const char* method, const char* params, Int_t* error = 0)
virtual voidTObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0)
virtual voidTObject::ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual voidTObject::Fatal(const char* method, const char* msgfmt) const
virtual voidTNamed::FillBuffer(char*& buffer)
virtual TObject*TObject::FindObject(const char* name) const
virtual TObject*TObject::FindObject(const TObject* obj) const
virtual Option_t*TObject::GetDrawOption() const
static Long_tTObject::GetDtorOnly()
TStringGetExpFormula(Option_t* option = "") const
virtual const char*TObject::GetIconName() const
const TObject*GetLinearPart(Int_t i) const
virtual const char*TNamed::GetName() const
Int_tGetNdim() const
Int_tGetNpar() const
Int_tGetNumber() const
virtual char*TObject::GetObjectInfo(Int_t px, Int_t py) const
static Bool_tTObject::GetObjectStat()
virtual Option_t*TObject::GetOption() const
Double_tGetParameter(const char* name) const
Double_tGetParameter(Int_t param) const
Double_t*GetParameters() const
voidGetParameters(Double_t* params) const
const char*GetParName(Int_t ipar) const
Int_tGetParNumber(const char* name) const
virtual const char*TNamed::GetTitle() const
virtual UInt_tTObject::GetUniqueID() const
Double_tGetVariable(const char* name) const
TStringGetVarName(Int_t ivar) const
Int_tGetVarNumber(const char* name) const
virtual Bool_tTObject::HandleTimer(TTimer* timer)
virtual ULong_tTNamed::Hash() const
virtual voidTObject::Info(const char* method, const char* msgfmt) const
virtual Bool_tTObject::InheritsFrom(const char* classname) const
virtual Bool_tTObject::InheritsFrom(const TClass* cl) const
virtual voidTObject::Inspect() constMENU
voidTObject::InvertBit(UInt_t f)
virtual TClass*IsA() const
virtual Bool_tTObject::IsEqual(const TObject* obj) const
virtual Bool_tTObject::IsFolder() const
Bool_tIsLinear() const
Bool_tTObject::IsOnHeap() const
virtual Bool_tTNamed::IsSortable() const
Bool_tIsValid() const
Bool_tTObject::IsZombie() const
virtual voidTNamed::ls(Option_t* option = "") const
voidTObject::MayNotUse(const char* method) const
virtual Bool_tTObject::Notify()
voidTObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const
voidTObject::operator delete(void* ptr)
voidTObject::operator delete(void* ptr, void* vp)
voidTObject::operator delete[](void* ptr)
voidTObject::operator delete[](void* ptr, void* vp)
void*TObject::operator new(size_t sz)
void*TObject::operator new(size_t sz, void* vp)
void*TObject::operator new[](size_t sz)
void*TObject::operator new[](size_t sz, void* vp)
TFormula&operator=(const TFormula& rhs)
virtual voidTObject::Paint(Option_t* option = "")
virtual voidTObject::Pop()
virtual voidPrint(Option_t* option = "") const
virtual Int_tTObject::Read(const char* name)
virtual voidTObject::RecursiveRemove(TObject* obj)
voidTObject::ResetBit(UInt_t f)
virtual voidTObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU
virtual voidTObject::SavePrimitive(ostream& out, Option_t* option = "")
voidTObject::SetBit(UInt_t f)
voidTObject::SetBit(UInt_t f, Bool_t set)
virtual voidTObject::SetDrawOption(Option_t* option = "")MENU
static voidTObject::SetDtorOnly(void* obj)
virtual voidSetName(const char* name)
virtual voidTNamed::SetNameTitle(const char* name, const char* title)
static voidTObject::SetObjectStat(Bool_t stat)
voidSetParameter(const char* name, Double_t value)
voidSetParameter(Int_t param, Double_t value)
voidSetParameters(const Double_t* params)
voidSetParameters(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)MENU
voidSetParName(Int_t ipar, const char* name)
voidSetParNames(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")MENU
virtual voidTNamed::SetTitle(const char* title = "")MENU
virtual voidTObject::SetUniqueID(UInt_t uid)
voidSetVariable(const TString& name, Double_t value)
voidSetVariables(const pair<TString,Double_t>* vars, const Int_t size)
virtual voidShowMembers(TMemberInspector& insp) const
virtual Int_tTNamed::Sizeof() const
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
virtual voidTObject::SysError(const char* method, const char* msgfmt) const
Bool_tTObject::TestBit(UInt_t f) const
Int_tTObject::TestBits(UInt_t f) const
TFormula()
TFormula(const TFormula& formula)
TFormula(const char* name, const char* formula = "", bool addToGlobList = true)
virtual voidTObject::UseCurrentStyle()
virtual voidTObject::Warning(const char* method, const char* msgfmt) const
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0)
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const
protected:
voidDoAddParameter(const TString& name, Double_t value, bool processFormula)
virtual voidTObject::DoError(int level, const char* location, const char* fmt, va_list va) const
Double_tDoEval(const Double_t* x, const Double_t* p = nullptr) const
voidDoSetParameters(const Double_t* p, Int_t size)
voidExtractFunctors(TString& formula)
static Bool_tIsBracket(const char c)
static Bool_tIsFunctionNameChar(const char c)
static Bool_tIsOperator(const char c)
static Bool_tIsScientificNotation(const TString& formula, int ipos)
voidTObject::MakeZombie()
Bool_tPrepareFormula(TString& formula)
voidPreProcessFormula(TString& formula)
voidProcessFormula(TString& formula)
voidReplaceParamName(TString& formula, const TString& oldname, const TString& name)
voidSetPredefinedParamNames()

Data Members

public:
static TObject::(anonymous)TObject::kBitMask
static TObject::EStatusBitsTObject::kCanDelete
static TObject::EStatusBitsTObject::kCannotPick
static TObject::EStatusBitsTObject::kHasUUID
static TObject::EStatusBitsTObject::kInvalidObject
static TObject::(anonymous)TObject::kIsOnHeap
static TObject::EStatusBitsTObject::kIsReferenced
static TFormula::(anonymous)kLinear
static TObject::EStatusBitsTObject::kMustCleanup
static TObject::EStatusBitsTObject::kNoContextMenu
static TFormula::(anonymous)kNormalized
static TObject::(anonymous)TObject::kNotDeleted
static TObject::EStatusBitsTObject::kObjInCanvas
static TObject::(anonymous)TObject::kOverwrite
static TObject::(anonymous)TObject::kSingleKey
static TObject::(anonymous)TObject::kWriteDelete
static TObject::(anonymous)TObject::kZombie
protected:
map<TString,Double_t>fConsts!
TStringfFormula
list<TFormulaFunction>fFuncs!
map<TString,TString>fFunctionsShortcuts!
vector<TObject*>fLinearPartsvector of linear functions
TStringTNamed::fNameobject identifier
Int_tfNdim!
Int_tfNpar!
Int_tfNumber!
map<TString,Int_t,TFormulaParamOrder>fParamslist of parameter names
TStringTNamed::fTitleobject title
map<TString,TFormulaVariable>fVars! list of variable names
static TFormula::(anonymous)kNotGlobal
private:
Bool_tfAllParametersSettedflag to control if all parameters are setted
Bool_tfClingInitialized! transient to force re-initialization
TStringfClingInput! input function passed to Cling
TStringfClingName! unique name passed to Cling to define the function ( double clingName(double*x, double*p) )
vector<Double_t>fClingParametersparameter values
vector<Double_t>fClingVariables! cached variables
TInterpreter::CallFuncIFacePtr_t::Generic_tfFuncPtr! function pointer
TMethodCall*fMethod! pointer to methocall
Bool_tfReadyToExecute!

Class Charts

Inheritance Inherited Members Includes Libraries
Class Charts

Function documentation

Bool_t IsOperator(const char c)
 operator ":" must be handled separatly
Bool_t IsBracket(const char c)
Bool_t IsFunctionNameChar(const char c)
Bool_t IsDefaultVariableName(const TString& name)
Bool_t IsScientificNotation(const TString& formula, int ipos)
 check if the character at position i  is part of a scientific notation
TFormula()
~TFormula()
TFormula(const char* name, const char* formula = "", bool addToGlobList = true)
-
*-*  Constructor
*-*  When TF1 is constructed using C++ function, TF1 need space to keep parameters values.
-
TFormula(const char* name, const char* formula = "", bool addToGlobList = true)
TFormula& operator=(const TFormula& rhs)
-
*-*  = Operator
-
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.
 return 0 if the formula compilation is successfull
void Copy(TObject& f1) const
void Clear(Option_t* option = "")
 clear the formula setting expression to empty and reset the variables and parameters containers
bool PrepareEvalMethod()
-
*-*    Sets TMethodCall to function inside Cling environment
*-*    TFormula uses it to execute function.
*-*    After call, TFormula should be ready to evaluate formula.
-
void InputFormulaIntoCling()
-
*-*    Inputs formula, transfered to C++ code into Cling
-
void FillDefaults()
-
*-*    Fill structures with default variables, constants and function shortcuts
-
#ifdef ROOT_CPLUSPLUS11
void HandlePolN(TString& formula)
-
*-*    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.
-
void HandleParametrizedFunctions(TString& formula)
-
*-*    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:
*-*    - 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.
-
void HandleExponentiation(TString& formula)
-
*-*    Handling exponentiation
*-*    Can handle multiple carets, eg.
*-*    2^3^4 will be treated like 2^(3^4)
-
void HandleLinear(TString& formula)
void PreProcessFormula(TString& formula)
-
*-*    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.
-
Bool_t PrepareFormula(TString& formula)
 prepare the formula to be executed
 normally is called with fFormula
void ExtractFunctors(TString& formula)
-
*-*    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.
-
void ProcessFormula(TString& formula)
-
*-*    Iterates through funtors in fFuncs and performs the appropriate action.
*-*    If functor has 0 arguments (has only name) can be:
*-*     - variable
*-*       * will be replaced with x[num], where x is an array containing value of this variable under num.
*-*     - pre-defined formula
*-*       * will be replaced with formulas body
*-*     - constant
*-*       * will be replaced with constant value
*-*     - parameter
*-*       * will be replaced with p[num], where p is an array containing value of this parameter under num.
*-*    If has arguments it can be :
*-*     - function shortcut, eg. sin
*-*       * will be replaced with fullname of function, eg. sin -> TMath::Sin
*-*     - function from cling environment, eg. TMath::BreitWigner(x,y,z)
*-*       * first check if function exists, and has same number of arguments, then accept it and set as found.
*-*    If all functors after iteration are matched with corresponding action,
*-*    it inputs C++ code of formula into cling, and sets flag that formula is ready to evaluate.
-
void SetPredefinedParamNames()
const TObject* GetLinearPart(Int_t i) const
 Return linear part.
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.
*-*    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
-
void SetName(const char* name)
 Set the name of the formula. We need to allow the list of function to
 properly handle the hashes.
void SetVariables(const pair<TString,Double_t>* vars, const Int_t size)
-
*-*    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
-
Double_t GetVariable(const char* name) const
-
*-*    Returns variable value.
-
Int_t GetVarNumber(const char* name) const
-
*-*    Returns variable number (positon in array) given its name
-
TString GetVarName(Int_t ivar) const
-
*-*    Returns variable name given its position in the array
-
void SetVariable(const TString& name, Double_t value)
-
*-*    Sets variable value.
-
void DoAddParameter(const TString& name, Double_t value, bool processFormula)
-
*-*    Adds parameter to known parameters.
*-*    User should use SetParameter, because parameters are added during initialization part,
*-*    and after that adding new will be pointless.
-
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
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.
-
-
TString name = TString::Format("%d",param);
const char * GetParName(Int_t ipar) const
-
*-*    Return parameter name given by integer.
-
Double_t* GetParameters() const
void GetParameters(Double_t* params) const
void SetParameter(const char* name, Double_t value)
-
*-*    Sets parameter value.
-
void SetParameters(const pair<TString,Double_t> *params,const Int_t size)
-
*-*    Set multiple parameters.
*-*    First argument is an array of pairs<TString,Double>, where
*-*    first argument is name of parameter,
*-*    second argument represents value.
*-*    size - number of params passed in first argument
-
void DoSetParameters(const Double_t* p, Int_t size)
void SetParameters(const Double_t* params)
 set a vector of parameters value
 Order in the vector is by default the aphabetic order given to the parameters
 apart if the users has defined explicitly the parameter names
void SetParameter(Int_t param, Double_t value)
 Set a parameter given a parameter index
 The parameter index is by default the aphabetic order given to the parameters
 apart if the users has defined explicitly the parameter names
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 SetParName(Int_t ipar, const char* name)
void ReplaceParamName(TString& formula, const TString& oldname, const TString& name)
 replace in Formula expression the parameter name
Double_t EvalPar(const Double_t* x, const Double_t* params = 0) const
Double_t Eval(Double_t x, Double_t y, Double_t z, Double_t t) const
-
*-*    Sets first 4  variables (e.g. x, y, z, t) and evaluate formula.
-
Double_t Eval(Double_t x, Double_t y, Double_t z) const
-
*-*    Sets first 3  variables (e.g. x, y, z) and evaluate formula.
-
Double_t Eval(Double_t x, Double_t y) const
-
*-*    Sets first 2  variables (e.g. x and y) and evaluate formula.
-
Double_t Eval(Double_t x) const
-
*-*    Sets first variable (e.g. x) and evaluate formula.
-
double xxx[1] = {x};
Double_t DoEval(const Double_t* x, const Double_t* p = nullptr) const
-
*-*    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 setted, appropriate warning is shown.
-
TString GetExpFormula(Option_t* option = "") const
 return the expression formula
 If option = "P" replace the parameter names with their values
 If option = "CLING" return the actual expression used to build the function  passed to cling
 If option = "CLINGP" replace in the CLING expression the parameter with their values
void Print(Option_t* option = "") const
 print the formula and its attributes
void Streamer(TBuffer& )
 Stream a class object.
Int_t GetNdim() const
{return fNdim;}
Int_t GetNpar() const
{return fNpar;}
Int_t GetNumber() const
{return fNumber;}
Bool_t IsLinear() const
{return TestBit(kLinear);}