Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TFormula.h
Go to the documentation of this file.
1 
2 // @(#)root/hist:$Id$
3 // Author: Maciej Zimnoch 30/09/2013
4 
5 /*************************************************************************
6  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 // ---------------------------------- TFormula.h
13 #ifndef ROOT_TFormula
14 #define ROOT_TFormula
15 
16 
17 #include "TNamed.h"
18  #include "TBits.h"
19 #include "TObjArray.h"
20 #include "TMethodCall.h"
21 #include "TInterpreter.h"
22 #include <vector>
23 #include <list>
24 #include <map>
25 
26 class TFormulaFunction
27 {
28 public:
29  TString fName;
30  TString fBody;
31  Int_t fNargs;
32  Bool_t fFound;
34  const char * GetName() const { return fName.Data(); }
35  const char * GetBody() const { return fBody.Data(); }
36  Int_t GetNargs() const { return fNargs;}
37  Bool_t IsFuncCall() const { return fFuncCall;}
39  TFormulaFunction(const TString &name, const TString &body, int numArgs)
40  : fName(name),fBody(body),fNargs(numArgs),fFound(false),fFuncCall(true) {}
42  : fName(name),fBody(""),fNargs(0),fFound(false),fFuncCall(false){}
43  Bool_t operator<(const TFormulaFunction &rhv) const
44  {
45  // order by length - first the longer ones to avoid replacing wrong functions
46  if ( fName.Length() > rhv.fName.Length() )
47  return true;
48  else if ( fName.Length() > rhv.fName.Length() )
49  return false;
50  // case of equal length
51  return fName < rhv.fName && fBody < rhv.fBody;
52  }
53  Bool_t operator==(const TFormulaFunction &rhv) const
54  {
55  return fName == rhv.fName && fBody == rhv.fBody && fNargs == rhv.fNargs;
56  }
57 };
58 
59 class TFormulaVariable
60 {
61 public:
62  TString fName;
65  Bool_t fFound;
66  const char * GetName() const { return fName.Data(); }
67  Double_t GetInitialValue() const { return fValue; }
68  Int_t GetArrayPos() const { return fArrayPos; }
69  TFormulaVariable():fName(""),fValue(-1),fArrayPos(-1),fFound(false){}
70  TFormulaVariable(const TString &name, Double_t value, Int_t pos)
71  : fName(name), fValue(value), fArrayPos(pos),fFound(false) {}
72  Bool_t operator<(const TFormulaVariable &rhv) const
73  {
74  return fName < rhv.fName;
75  }
76 };
77 
78 struct TFormulaParamOrder {
79  bool operator() (const TString& a, const TString& b) const;
80 };
81 
82 
83 class TFormula : public TNamed
84 {
85 private:
86 
87  // All data members are transient apart from the string defining the formula and the parameter values
88 
89  TString fClingInput; //! input function passed to Cling
90  std::vector<Double_t> fClingVariables; //! cached variables
91  std::vector<Double_t> fClingParameters; // parameter values
92  Bool_t fReadyToExecute; //! trasient to force initialization
93  Bool_t fClingInitialized; //! transient to force re-initialization
94  Bool_t fAllParametersSetted; // flag to control if all parameters are setted
95  TMethodCall* fMethod; //! pointer to methocall
96  TString fClingName; //! unique name passed to Cling to define the function ( double clingName(double*x, double*p) )
97 
99  void * fLambdaPtr; //! pointer to the lambda function
100 
101  void InputFormulaIntoCling();
103  void FillDefaults();
104  void HandlePolN(TString &formula);
105  void HandleParametrizedFunctions(TString &formula);
106  void HandleExponentiation(TString &formula);
107  void HandleLinear(TString &formula);
108  Bool_t InitLambdaExpression(const char * formula);
109  static Bool_t IsDefaultVariableName(const TString &name);
110 protected:
111 
112  std::list<TFormulaFunction> fFuncs; //!
113  std::map<TString,TFormulaVariable> fVars; //! list of variable names
114  std::map<TString,Int_t,TFormulaParamOrder> fParams; // list of parameter names
115  std::map<TString,Double_t> fConsts; //!
116  std::map<TString,TString> fFunctionsShortcuts; //!
118  Int_t fNdim; //!
119  Int_t fNpar; //!
120  Int_t fNumber; //!
121  std::vector<TObject*> fLinearParts; // vector of linear functions
122 
123  static Bool_t IsOperator(const char c);
124  static Bool_t IsBracket(const char c);
125  static Bool_t IsFunctionNameChar(const char c);
126  static Bool_t IsScientificNotation(const TString & formula, int ipos);
127  static Bool_t IsHexadecimal(const TString & formula, int ipos);
128  static Bool_t IsAParameterName(const TString & formula, int ipos);
129  void ExtractFunctors(TString &formula);
130  void PreProcessFormula(TString &formula);
131  void ProcessFormula(TString &formula);
132  Bool_t PrepareFormula(TString &formula);
133  void ReplaceParamName(TString &formula, const TString & oldname, const TString & name);
134  void DoAddParameter(const TString &name, Double_t value, bool processFormula);
135  void DoSetParameters(const Double_t * p, Int_t size);
136  void SetPredefinedParamNames();
137 
138  Double_t DoEval(const Double_t * x, const Double_t * p = nullptr) const;
139 
140 public:
141 
142  enum {
143  kNotGlobal = BIT(10), // don't store in gROOT->GetListOfFunction (it should be protected)
144  kNormalized = BIT(14), // set to true if the TFormula (ex gausn) is normalized
145  kLinear = BIT(16), //set to true if the TFormula is for linear fitting
146  kLambda = BIT(17) // set to true if TFormula has been build with a lambda
147  };
148  TFormula();
149  virtual ~TFormula();
150  TFormula& operator=(const TFormula &rhs);
151  TFormula(const char *name, const char * formula = "", bool addToGlobList = true);
152  TFormula(const char *name, const char * formula, int ndim, int npar, bool addToGlobList = true);
153  TFormula(const TFormula &formula);
154  // TFormula(const char *name, Int_t nparams, Int_t ndims);
155 
156  void AddParameter(const TString &name, Double_t value = 0) { DoAddParameter(name,value,true); }
157  void AddVariable(const TString &name, Double_t value = 0);
158  void AddVariables(const TString *vars, const Int_t size);
159  Int_t Compile(const char *expression="");
160  virtual void Copy(TObject &f1) const;
161  virtual void Clear(Option_t * option="");
162  Double_t Eval(Double_t x) const;
163  Double_t Eval(Double_t x, Double_t y) const;
166  Double_t EvalPar(const Double_t *x, const Double_t *params=0) const;
167  TString GetExpFormula(Option_t *option="") const;
168  const TObject *GetLinearPart(Int_t i) const;
169  Int_t GetNdim() const {return fNdim;}
170  Int_t GetNpar() const {return fNpar;}
171  Int_t GetNumber() const { return fNumber; }
172  const char * GetParName(Int_t ipar) const;
173  Int_t GetParNumber(const char * name) const;
174  Double_t GetParameter(const char * name) const;
175  Double_t GetParameter(Int_t param) const;
176  Double_t* GetParameters() const;
177  void GetParameters(Double_t *params) const;
178  Double_t GetVariable(const char *name) const;
179  Int_t GetVarNumber(const char *name) const;
180  TString GetVarName(Int_t ivar) const;
181  Bool_t IsValid() const { return fReadyToExecute && fClingInitialized; }
182  Bool_t IsLinear() const { return TestBit(kLinear); }
183  void Print(Option_t *option = "") const;
184  void SetName(const char* name);
185  void SetParameter(const char* name, Double_t value);
186  void SetParameter(Int_t param, Double_t value);
187  void SetParameters(const Double_t *params);
188  //void SetParameters(const pair<TString,Double_t> *params, const Int_t size);
190  Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0,
191  Double_t p9=0,Double_t p10=0); // *MENU*
192  void SetParName(Int_t ipar, const char *name);
193  void SetParNames(const char *name0="p0",const char *name1="p1",const char
194  *name2="p2",const char *name3="p3",const char
195  *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char
196  *name8="p8",const char *name9="p9",const char *name10="p10"); // *MENU*
197  void SetVariable(const TString &name, Double_t value);
198  void SetVariables(const std::pair<TString,Double_t> *vars, const Int_t size);
199 
200  ClassDef(TFormula,10)
201 };
202 #endif
Bool_t IsFuncCall() const
Definition: TFormula.h:37
virtual void Clear(Option_t *option="")
Clear the formula setting expression to empty and reset the variables and parameters containers...
Definition: TFormula.cxx:621
static Bool_t IsBracket(const char c)
Definition: TFormula.cxx:153
Bool_t operator==(const TFormulaFunction &rhv) const
Definition: TFormula.h:53
void HandleExponentiation(TString &formula)
Handling exponentiation Can handle multiple carets, eg.
Definition: TFormula.cxx:1199
std::map< TString, Double_t > fConsts
Definition: TFormula.h:115
Int_t fNpar
Definition: TFormula.h:119
std::vector< TObject * > fLinearParts
Definition: TFormula.h:121
static double p3(double t, double a, double b, double c, double d)
Double_t Eval(Double_t x) const
Sets first variable (e.g. x) and evaluate formula.
Definition: TFormula.cxx:2642
Int_t GetVarNumber(const char *name) const
Returns variable number (positon in array) given its name.
Definition: TFormula.cxx:2204
Ssiz_t Length() const
Definition: TString.h:385
void AddVariables(const TString *vars, const Int_t size)
Adds multiple variables.
Definition: TFormula.cxx:2098
TMethodCall * fMethod
Definition: TFormula.h:95
const char Option_t
Definition: RtypesCore.h:62
void DoSetParameters(const Double_t *p, Int_t size)
Definition: TFormula.cxx:2467
void HandleLinear(TString &formula)
Handle linear functions defined with the operator ++.
Definition: TFormula.cxx:1296
void HandlePolN(TString &formula)
Handling polN If before &#39;pol&#39; exist any name, this name will be treated as variable used in polynomia...
Definition: TFormula.cxx:828
TString fName
Definition: TFormula.h:29
void SetParameters(const Double_t *params)
Set a vector of parameters value.
Definition: TFormula.cxx:2489
#define BIT(n)
Definition: Rtypes.h:75
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")
Definition: TFormula.cxx:2531
void SetVariables(const std::pair< TString, Double_t > *vars, const Int_t size)
Sets multiple variables.
Definition: TFormula.cxx:2170
Helper class for TFormula.
Definition: TFormula.h:26
Basic string class.
Definition: TString.h:129
Functor defining the parameter order.
Definition: TFormula.h:78
void AddVariable(const TString &name, Double_t value=0)
Adds variable to known variables, and reprocess formula.
Definition: TFormula.cxx:2059
void InputFormulaIntoCling()
pointer to the lambda function
Definition: TFormula.cxx:713
void DoAddParameter(const TString &name, Double_t value, bool processFormula)
Adds parameter to known parameters.
Definition: TFormula.cxx:2250
int Int_t
Definition: RtypesCore.h:41
virtual void Copy(TObject &f1) const
Copy this to obj.
Definition: TFormula.cxx:544
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
Bool_t fFound
Definition: TFormula.h:32
Bool_t fAllParametersSetted
transient to force re-initialization
Definition: TFormula.h:94
Int_t GetParNumber(const char *name) const
Return parameter index given a name (return -1 for not existing parameters) non need to print an erro...
Definition: TFormula.cxx:2331
const TObject * GetLinearPart(Int_t i) const
Return linear part.
Definition: TFormula.cxx:2043
void SetPredefinedParamNames()
Set parameter names only in case of pre-defined functions.
Definition: TFormula.cxx:1971
void Print(Option_t *option="") const
Print the formula and its attributes.
Definition: TFormula.cxx:2785
Bool_t PrepareFormula(TString &formula)
prepare the formula to be executed normally is called with fFormula
Definition: TFormula.cxx:1371
TString fBody
Definition: TFormula.h:30
const char * Data() const
Definition: TString.h:344
void ReplaceParamName(TString &formula, const TString &oldname, const TString &name)
Replace in Formula expression the parameter name.
Definition: TFormula.cxx:2579
virtual ~TFormula()
Definition: TFormula.cxx:292
Double_t x[n]
Definition: legend1.C:17
Double_t EvalPar(const Double_t *x, const Double_t *params=0) const
Definition: TFormula.cxx:2607
Bool_t operator<(const TFormulaVariable &rhv) const
Definition: TFormula.h:72
#define ClassDef(name, id)
Definition: Rtypes.h:297
Bool_t fFuncCall
Definition: TFormula.h:33
Int_t fArrayPos
Definition: TFormula.h:64
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
void SetParameter(const char *name, Double_t value)
Sets parameter value.
Definition: TFormula.cxx:2405
TString GetVarName(Int_t ivar) const
Returns variable name given its position in the array.
Definition: TFormula.cxx:2218
static double p2(double t, double a, double b, double c)
static Bool_t IsScientificNotation(const TString &formula, int ipos)
Definition: TFormula.cxx:176
TInterpreter::CallFuncIFacePtr_t::Generic_t fFuncPtr
unique name passed to Cling to define the function ( double clingName(double*x, double*p) ) ...
Definition: TFormula.h:98
TFormula & operator=(const TFormula &rhs)
= operator.
Definition: TFormula.cxx:459
TString GetExpFormula(Option_t *option="") const
Return the expression formula.
Definition: TFormula.cxx:2712
static Bool_t IsAParameterName(const TString &formula, int ipos)
Definition: TFormula.cxx:211
Double_t fValue
Definition: TFormula.h:63
void * fLambdaPtr
function pointer
Definition: TFormula.h:99
Method or function calling interface.
Definition: TMethodCall.h:37
Int_t GetNdim() const
Definition: TFormula.h:169
void SetParName(Int_t ipar, const char *name)
Definition: TFormula.cxx:2549
Int_t fNdim
Definition: TFormula.h:118
static Bool_t IsHexadecimal(const TString &formula, int ipos)
Definition: TFormula.cxx:188
std::vector< Double_t > fClingParameters
cached variables
Definition: TFormula.h:91
Int_t GetNpar() const
Definition: TFormula.h:170
void FillDefaults()
Fill structures with default variables, constants and function shortcuts.
Definition: TFormula.cxx:726
void HandleParametrizedFunctions(TString &formula)
Handling parametrized functions Function can be normalized, and have different variable then x...
Definition: TFormula.cxx:940
static Bool_t IsOperator(const char c)
Definition: TFormula.cxx:141
void SetName(const char *name)
Set the name of the formula.
Definition: TFormula.cxx:2142
The Formula class.
Definition: TFormula.h:83
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:159
static Bool_t IsDefaultVariableName(const TString &name)
Definition: TFormula.cxx:170
bool operator()(const TString &a, const TString &b) const
Definition: TFormula.cxx:233
static Bool_t IsFunctionNameChar(const char c)
Definition: TFormula.cxx:164
static double p1(double t, double a, double b)
Double_t DoEval(const Double_t *x, const Double_t *p=nullptr) const
Evaluate formula.
Definition: TFormula.cxx:2655
Double_t GetVariable(const char *name) const
Returns variable value.
Definition: TFormula.cxx:2190
Bool_t PrepareEvalMethod()
Sets TMethodCall to function inside Cling environment.
Definition: TFormula.cxx:658
TString fFormula
Definition: TFormula.h:117
Bool_t fFound
Definition: TFormula.h:65
TString fClingName
pointer to methocall
Definition: TFormula.h:96
std::vector< Double_t > fClingVariables
input function passed to Cling
Definition: TFormula.h:90
Int_t GetArrayPos() const
Definition: TFormula.h:68
std::map< TString, TString > fFunctionsShortcuts
Definition: TFormula.h:116
Double_t * GetParameters() const
Definition: TFormula.cxx:2384
double Double_t
Definition: RtypesCore.h:55
Int_t GetNumber() const
Definition: TFormula.h:171
Bool_t IsLinear() const
Definition: TFormula.h:182
void AddParameter(const TString &name, Double_t value=0)
Definition: TFormula.h:156
Double_t y[n]
Definition: legend1.C:17
Int_t fNumber
Definition: TFormula.h:120
Int_t Compile(const char *expression="")
Compile the given expression with Cling backward compatibility method to be used in combination with ...
Definition: TFormula.cxx:510
Int_t GetNargs() const
Definition: TFormula.h:36
void SetVariable(const TString &name, Double_t value)
Sets variable value.
Definition: TFormula.cxx:2234
const char * GetParName(Int_t ipar) const
Return parameter name given by integer.
Definition: TFormula.cxx:2370
Mother of all ROOT objects.
Definition: TObject.h:37
Bool_t InitLambdaExpression(const char *formula)
Definition: TFormula.cxx:469
TString fName
Definition: TFormula.h:62
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
void(* Generic_t)(void *, int, void **, void *)
Definition: TInterpreter.h:74
Bool_t IsValid() const
Definition: TFormula.h:181
void ProcessFormula(TString &formula)
Iterates through funtors in fFuncs and performs the appropriate action.
Definition: TFormula.cxx:1637
Bool_t operator<(const TFormulaFunction &rhv) const
Definition: TFormula.h:43
TF1 * f1
Definition: legend1.C:11
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
const char * GetName() const
Definition: TFormula.h:66
Bool_t fReadyToExecute
Definition: TFormula.h:92
const char * GetBody() const
Definition: TFormula.h:35
void ExtractFunctors(TString &formula)
Extracts functors from formula, and put them in fFuncs.
Definition: TFormula.cxx:1410
TString fClingInput
Definition: TFormula.h:89
std::map< TString, TFormulaVariable > fVars
Definition: TFormula.h:113
Double_t GetInitialValue() const
Definition: TFormula.h:67
Bool_t fClingInitialized
trasient to force initialization
Definition: TFormula.h:93
std::map< TString, Int_t, TFormulaParamOrder > fParams
list of variable names
Definition: TFormula.h:114
Double_t GetParameter(const char *name) const
Returns parameter value given by string.
Definition: TFormula.cxx:2344
char name[80]
Definition: TGX11.cxx:109
std::list< TFormulaFunction > fFuncs
Definition: TFormula.h:112
Another helper class for TFormula.
Definition: TFormula.h:59
void PreProcessFormula(TString &formula)
Preprocessing of formula Replace all ** by ^, and removes spaces.
Definition: TFormula.cxx:1351
const char * GetName() const
Definition: TFormula.h:34