ROOT  6.06/09
Reference Guide
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 #ifndef ROOT_TNamed
18 #include "TNamed.h"
19 #endif
20  #ifndef ROOT_TBits
21 #include "TBits.h"
22 #endif
23 #ifndef ROOT_TObjArray
24 #include "TObjArray.h"
25 #endif
26 #include "TMethodCall.h"
27 #include "TInterpreter.h"
28 #include <vector>
29 #include <list>
30 #include <map>
31 
32 class TFormulaFunction
33 {
34 public:
35  TString fName;
36  TString fBody;
37  Int_t fNargs;
38  Bool_t fFound;
40  const char * GetName() const { return fName.Data(); }
41  const char * GetBody() const { return fBody.Data(); }
42  Int_t GetNargs() const { return fNargs;}
43  Bool_t IsFuncCall() const { return fFuncCall;}
45  TFormulaFunction(const TString &name, const TString &body, int numArgs)
46  : fName(name),fBody(body),fNargs(numArgs),fFound(false),fFuncCall(true) {}
48  : fName(name),fBody(""),fNargs(0),fFound(false),fFuncCall(false){}
49  Bool_t operator<(const TFormulaFunction &rhv) const
50  {
51  // order by length - first the longer ones to avoid replacing wrong functions
52  if ( fName.Length() > rhv.fName.Length() )
53  return true;
54  else if ( fName.Length() > rhv.fName.Length() )
55  return false;
56  // case of equal length
57  return fName < rhv.fName && fBody < rhv.fBody;
58  }
59  Bool_t operator==(const TFormulaFunction &rhv) const
60  {
61  return fName == rhv.fName && fBody == rhv.fBody && fNargs == rhv.fNargs;
62  }
63 };
64 
65 class TFormulaVariable
66 {
67 public:
68  TString fName;
71  Bool_t fFound;
72  const char * GetName() const { return fName.Data(); }
73  Double_t GetInitialValue() const { return fValue; }
74  Int_t GetArrayPos() const { return fArrayPos; }
75  TFormulaVariable():fName(""),fValue(-1),fArrayPos(-1),fFound(false){}
77  : fName(name), fValue(value), fArrayPos(pos),fFound(false) {}
78  Bool_t operator<(const TFormulaVariable &rhv) const
79  {
80  return fName < rhv.fName;
81  }
82 };
83 
84 struct TFormulaParamOrder {
85  bool operator() (const TString& a, const TString& b) const;
86 };
87 
88 
89 class TFormula : public TNamed
90 {
91 private:
92 
93  // All data members are transient apart from the string defining the formula and the parameter values
94 
95  TString fClingInput; //! input function passed to Cling
96  std::vector<Double_t> fClingVariables; //! cached variables
97  std::vector<Double_t> fClingParameters; // parameter values
98  Bool_t fReadyToExecute; //! trasient to force initialization
99  Bool_t fClingInitialized; //! transient to force re-initialization
100  Bool_t fAllParametersSetted; // flag to control if all parameters are setted
101  TMethodCall* fMethod; //! pointer to methocall
102  TString fClingName; //! unique name passed to Cling to define the function ( double clingName(double*x, double*p) )
103 
105  void * fLambdaPtr; //! pointer to the lambda function
106 
107  void InputFormulaIntoCling();
109  void FillDefaults();
110  void HandlePolN(TString &formula);
111  void HandleParametrizedFunctions(TString &formula);
112  void HandleExponentiation(TString &formula);
113  void HandleLinear(TString &formula);
114  Bool_t InitLambdaExpression(const char * formula);
115  static Bool_t IsDefaultVariableName(const TString &name);
116 protected:
117 
118  std::list<TFormulaFunction> fFuncs; //!
119  std::map<TString,TFormulaVariable> fVars; //! list of variable names
120  std::map<TString,Int_t,TFormulaParamOrder> fParams; // list of parameter names
121  std::map<TString,Double_t> fConsts; //!
122  std::map<TString,TString> fFunctionsShortcuts; //!
124  Int_t fNdim; //!
125  Int_t fNpar; //!
126  Int_t fNumber; //!
127  std::vector<TObject*> fLinearParts; // vector of linear functions
128 
129  static Bool_t IsOperator(const char c);
130  static Bool_t IsBracket(const char c);
131  static Bool_t IsFunctionNameChar(const char c);
132  static Bool_t IsScientificNotation(const TString & formula, int ipos);
133  static Bool_t IsHexadecimal(const TString & formula, int ipos);
134  void ExtractFunctors(TString &formula);
135  void PreProcessFormula(TString &formula);
136  void ProcessFormula(TString &formula);
137  Bool_t PrepareFormula(TString &formula);
138  void ReplaceParamName(TString &formula, const TString & oldname, const TString & name);
139  void DoAddParameter(const TString &name, Double_t value, bool processFormula);
140  void DoSetParameters(const Double_t * p, Int_t size);
141  void SetPredefinedParamNames();
142 
143  Double_t DoEval(const Double_t * x, const Double_t * p = nullptr) const;
144 
145 public:
146 
147  enum {
148  kNotGlobal = BIT(10), // don't store in gROOT->GetListOfFunction (it should be protected)
149  kNormalized = BIT(14), // set to true if the TFormula (ex gausn) is normalized
150  kLinear = BIT(16), //set to true if the TFormula is for linear fitting
151  kLambda = BIT(17) // set to true if TFormula has been build with a lambda
152  };
153  TFormula();
154  virtual ~TFormula();
155  TFormula& operator=(const TFormula &rhs);
156  TFormula(const char *name, const char * formula = "", bool addToGlobList = true);
157  TFormula(const char *name, const char * formula, int ndim, int npar, bool addToGlobList = true);
158  TFormula(const TFormula &formula);
159  // TFormula(const char *name, Int_t nparams, Int_t ndims);
160 
161  void AddParameter(const TString &name, Double_t value = 0) { DoAddParameter(name,value,true); }
162  void AddVariable(const TString &name, Double_t value = 0);
163  void AddVariables(const TString *vars, const Int_t size);
164  Int_t Compile(const char *expression="");
165  virtual void Copy(TObject &f1) const;
166  virtual void Clear(Option_t * option="");
167  Double_t Eval(Double_t x) const;
168  Double_t Eval(Double_t x, Double_t y) const;
169  Double_t Eval(Double_t x, Double_t y , Double_t z) const;
170  Double_t Eval(Double_t x, Double_t y , Double_t z , Double_t t ) const;
171  Double_t EvalPar(const Double_t *x, const Double_t *params=0) const;
172  TString GetExpFormula(Option_t *option="") const;
173  const TObject *GetLinearPart(Int_t i) const;
174  Int_t GetNdim() const {return fNdim;}
175  Int_t GetNpar() const {return fNpar;}
176  Int_t GetNumber() const { return fNumber; }
177  const char * GetParName(Int_t ipar) const;
178  Int_t GetParNumber(const char * name) const;
179  Double_t GetParameter(const char * name) const;
180  Double_t GetParameter(Int_t param) const;
181  Double_t* GetParameters() const;
182  void GetParameters(Double_t *params) const;
183  Double_t GetVariable(const char *name) const;
184  Int_t GetVarNumber(const char *name) const;
185  TString GetVarName(Int_t ivar) const;
186  Bool_t IsValid() const { return fReadyToExecute && fClingInitialized; }
187  Bool_t IsLinear() const { return TestBit(kLinear); }
188  void Print(Option_t *option = "") const;
189  void SetName(const char* name);
190  void SetParameter(const char* name, Double_t value);
191  void SetParameter(Int_t param, Double_t value);
192  void SetParameters(const Double_t *params);
193  //void SetParameters(const pair<TString,Double_t> *params, const Int_t size);
195  Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0,
196  Double_t p9=0,Double_t p10=0); // *MENU*
197  void SetParName(Int_t ipar, const char *name);
198  void SetParNames(const char *name0="p0",const char *name1="p1",const char
199  *name2="p2",const char *name3="p3",const char
200  *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char
201  *name8="p8",const char *name9="p9",const char *name10="p10"); // *MENU*
202  void SetVariable(const TString &name, Double_t value);
203  void SetVariables(const std::pair<TString,Double_t> *vars, const Int_t size);
204 
205  ClassDef(TFormula,10)
206 };
207 #endif
Bool_t IsFuncCall() const
Definition: TFormula.h:43
virtual void Clear(Option_t *option="")
Set name and title to empty strings ("").
Definition: TFormula.cxx:591
static Bool_t IsBracket(const char c)
Definition: TFormula.cxx:162
Bool_t operator==(const TFormulaFunction &rhv) const
Definition: TFormula.h:59
void HandleExponentiation(TString &formula)
Definition: TFormula.cxx:1145
std::map< TString, Double_t > fConsts
Definition: TFormula.h:121
Int_t fNpar
Definition: TFormula.h:125
std::vector< TObject * > fLinearParts
Definition: TFormula.h:127
static double p3(double t, double a, double b, double c, double d)
Double_t Eval(Double_t x) const
Definition: TFormula.cxx:2538
Int_t GetVarNumber(const char *name) const
Definition: TFormula.cxx:2124
Ssiz_t Length() const
Definition: TString.h:390
void AddVariables(const TString *vars, const Int_t size)
Definition: TFormula.cxx:2017
TMethodCall * fMethod
Definition: TFormula.h:101
const char Option_t
Definition: RtypesCore.h:62
void DoSetParameters(const Double_t *p, Int_t size)
Definition: TFormula.cxx:2384
void HandleLinear(TString &formula)
Definition: TFormula.cxx:1245
void HandlePolN(TString &formula)
Definition: TFormula.cxx:788
TString fName
Definition: TFormula.h:35
void SetParameters(const Double_t *params)
Definition: TFormula.cxx:2401
#define BIT(n)
Definition: Rtypes.h:120
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:2438
void SetVariables(const std::pair< TString, Double_t > *vars, const Int_t size)
Definition: TFormula.cxx:2087
Helper class for TFormula.
Definition: TFormula.h:32
Basic string class.
Definition: TString.h:137
Functor defining the parameter order.
Definition: TFormula.h:84
void AddVariable(const TString &name, Double_t value=0)
Definition: TFormula.cxx:1982
void InputFormulaIntoCling()
pointer to the lambda function
Definition: TFormula.cxx:679
void DoAddParameter(const TString &name, Double_t value, bool processFormula)
Definition: TFormula.cxx:2169
int Int_t
Definition: RtypesCore.h:41
virtual void Copy(TObject &f1) const
Copy this to obj.
Definition: TFormula.cxx:518
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
Bool_t fFound
Definition: TFormula.h:38
Bool_t fAllParametersSetted
transient to force re-initialization
Definition: TFormula.h:100
Int_t GetParNumber(const char *name) const
Definition: TFormula.cxx:2251
const TObject * GetLinearPart(Int_t i) const
Definition: TFormula.cxx:1968
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
Definition: TIterator.cxx:20
void SetPredefinedParamNames()
Definition: TFormula.cxx:1899
void Print(Option_t *option="") const
print the formula and its attributes
Definition: TFormula.cxx:2682
Bool_t PrepareFormula(TString &formula)
Definition: TFormula.cxx:1315
TString fBody
Definition: TFormula.h:36
const char * Data() const
Definition: TString.h:349
void ReplaceParamName(TString &formula, const TString &oldname, const TString &name)
Definition: TFormula.cxx:2481
virtual ~TFormula()
Definition: TFormula.cxx:274
Double_t x[n]
Definition: legend1.C:17
Double_t EvalPar(const Double_t *x, const Double_t *params=0) const
Definition: TFormula.cxx:2509
Bool_t operator<(const TFormulaVariable &rhv) const
Definition: TFormula.h:78
#define ClassDef(name, id)
Definition: Rtypes.h:254
Bool_t fFuncCall
Definition: TFormula.h:39
Int_t fArrayPos
Definition: TFormula.h:70
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
void SetParameter(const char *name, Double_t value)
Definition: TFormula.cxx:2321
TString GetVarName(Int_t ivar) const
Definition: TFormula.cxx:2138
static double p2(double t, double a, double b, double c)
void(* Generic_t)(void *, int, void **, void *)
Definition: TInterpreter.h:77
static Bool_t IsScientificNotation(const TString &formula, int ipos)
Definition: TFormula.cxx:183
TInterpreter::CallFuncIFacePtr_t::Generic_t fFuncPtr
unique name passed to Cling to define the function ( double clingName(double*x, double*p) ) ...
Definition: TFormula.h:104
TFormula & operator=(const TFormula &rhs)
Definition: TFormula.cxx:430
TString GetExpFormula(Option_t *option="") const
return the expression formula If option = "P" replace the parameter names with their values If option...
Definition: TFormula.cxx:2610
Double_t fValue
Definition: TFormula.h:69
void * fLambdaPtr
function pointer
Definition: TFormula.h:105
Method or function calling interface.
Definition: TMethodCall.h:41
Int_t GetNdim() const
Definition: TFormula.h:174
void SetParName(Int_t ipar, const char *name)
Definition: TFormula.cxx:2454
Int_t fNdim
Definition: TFormula.h:124
static Bool_t IsHexadecimal(const TString &formula, int ipos)
Definition: TFormula.cxx:194
std::vector< Double_t > fClingParameters
cached variables
Definition: TFormula.h:97
Int_t GetNpar() const
Definition: TFormula.h:175
void FillDefaults()
Definition: TFormula.cxx:690
void HandleParametrizedFunctions(TString &formula)
Definition: TFormula.cxx:881
static Bool_t IsOperator(const char c)
Definition: TFormula.cxx:151
void SetName(const char *name)
Change (i.e.
Definition: TFormula.cxx:2065
The F O R M U L A class.
Definition: TFormula.h:89
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
static Bool_t IsDefaultVariableName(const TString &name)
Definition: TFormula.cxx:177
bool operator()(const TString &a, const TString &b) const
Definition: TFormula.cxx:215
static Bool_t IsFunctionNameChar(const char c)
Definition: TFormula.cxx:172
static double p1(double t, double a, double b)
Double_t DoEval(const Double_t *x, const Double_t *p=nullptr) const
Definition: TFormula.cxx:2547
Double_t GetVariable(const char *name) const
Definition: TFormula.cxx:2111
Bool_t PrepareEvalMethod()
Definition: TFormula.cxx:623
TString fFormula
Definition: TFormula.h:123
Bool_t fFound
Definition: TFormula.h:71
TString fClingName
pointer to methocall
Definition: TFormula.h:102
std::vector< Double_t > fClingVariables
input function passed to Cling
Definition: TFormula.h:96
Int_t GetArrayPos() const
Definition: TFormula.h:74
std::map< TString, TString > fFunctionsShortcuts
Definition: TFormula.h:122
Double_t * GetParameters() const
Definition: TFormula.cxx:2303
double Double_t
Definition: RtypesCore.h:55
Int_t GetNumber() const
Definition: TFormula.h:176
Bool_t IsLinear() const
Definition: TFormula.h:187
void AddParameter(const TString &name, Double_t value=0)
Definition: TFormula.h:161
Double_t y[n]
Definition: legend1.C:17
Int_t fNumber
Definition: TFormula.h:126
Int_t Compile(const char *expression="")
Definition: TFormula.cxx:479
Int_t GetNargs() const
Definition: TFormula.h:42
#define name(a, b)
Definition: linkTestLib0.cpp:5
void SetVariable(const TString &name, Double_t value)
Definition: TFormula.cxx:2155
const char * GetParName(Int_t ipar) const
Definition: TFormula.cxx:2288
Mother of all ROOT objects.
Definition: TObject.h:58
Bool_t InitLambdaExpression(const char *formula)
Definition: TFormula.cxx:442
TString fName
Definition: TFormula.h:68
Bool_t IsValid() const
Definition: TFormula.h:186
void ProcessFormula(TString &formula)
Definition: TFormula.cxx:1549
Bool_t operator<(const TFormulaFunction &rhv) const
Definition: TFormula.h:49
TF1 * f1
Definition: legend1.C:11
const char * GetName() const
Definition: TFormula.h:72
Bool_t fReadyToExecute
Definition: TFormula.h:98
const char * GetBody() const
Definition: TFormula.h:41
void ExtractFunctors(TString &formula)
Definition: TFormula.cxx:1346
TString fClingInput
Definition: TFormula.h:95
float value
Definition: math.cpp:443
std::map< TString, TFormulaVariable > fVars
Definition: TFormula.h:119
Double_t GetInitialValue() const
Definition: TFormula.h:73
Bool_t fClingInitialized
trasient to force initialization
Definition: TFormula.h:99
std::map< TString, Int_t, TFormulaParamOrder > fParams
list of variable names
Definition: TFormula.h:120
Double_t GetParameter(const char *name) const
Definition: TFormula.cxx:2263
std::list< TFormulaFunction > fFuncs
Definition: TFormula.h:118
Another helper class for TFormula.
Definition: TFormula.h:65
void PreProcessFormula(TString &formula)
Definition: TFormula.cxx:1293
const char * GetName() const
Definition: TFormula.h:40