Logo ROOT   6.14/05
Reference Guide
TFormula.h
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Maciej Zimnoch 30/09/2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 // ---------------------------------- TFormula.h
12 #ifndef ROOT_TFormula
13 #define ROOT_TFormula
14 
15 
16 #include "TNamed.h"
17  #include "TBits.h"
18 #include "TObjArray.h"
19 #include "TMethodCall.h"
20 #include "TInterpreter.h"
21 #include <vector>
22 #include <list>
23 #include <map>
24 #include <Math/Types.h>
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;
64  Int_t fArrayPos;
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  Bool_t fLazyInitialization = kFALSE; //! transient flag to control lazy initialization (needed for reading from files)
96  TMethodCall *fMethod; //! pointer to methodcall
97  TString fClingName; //! unique name passed to Cling to define the function ( double clingName(double*x, double*p) )
98  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)
99 
100  TInterpreter::CallFuncIFacePtr_t::Generic_t fFuncPtr; //! function pointer
101  void * fLambdaPtr; //! pointer to the lambda function
102 
103  void InputFormulaIntoCling();
104  Bool_t PrepareEvalMethod();
105  void FillDefaults();
106  void HandlePolN(TString &formula);
107  void HandleParametrizedFunctions(TString &formula);
108  void HandleParamRanges(TString &formula);
109  void HandleFunctionArguments(TString &formula);
110  void HandleExponentiation(TString &formula);
111  void HandleLinear(TString &formula);
112  Bool_t InitLambdaExpression(const char * formula);
113  static Bool_t IsDefaultVariableName(const TString &name);
114  void ReplaceAllNames(TString &formula, std::map<TString, TString> &substitutions);
115  void FillParametrizedFunctions(std::map<std::pair<TString, Int_t>, std::pair<TString, TString>> &functions);
116  void FillVecFunctionsShurtCuts();
117  void ReInitializeEvalMethod();
118 
119 protected:
120 
121  std::list<TFormulaFunction> fFuncs; //!
122  std::map<TString,TFormulaVariable> fVars; //! list of variable names
123  std::map<TString,Int_t,TFormulaParamOrder> fParams; // list of parameter names
124  std::map<TString,Double_t> fConsts; //!
125  std::map<TString,TString> fFunctionsShortcuts; //!
126  TString fFormula; // string representing the formula expression
127  Int_t fNdim; // Dimension - needed for lambda expressions
128  Int_t fNpar; //! Number of parameter (transient since we save the vector)
129  Int_t fNumber; //!
130  std::vector<TObject*> fLinearParts; // vector of linear functions
131  Bool_t fVectorized = false; // whether we should use vectorized or regular variables
132  // (we default to false since a lot of functions still cannot be expressed in vectorized form)
133 
134  static Bool_t IsOperator(const char c);
135  static Bool_t IsBracket(const char c);
136  static Bool_t IsFunctionNameChar(const char c);
137  static Bool_t IsScientificNotation(const TString & formula, int ipos);
138  static Bool_t IsHexadecimal(const TString & formula, int ipos);
139  static Bool_t IsAParameterName(const TString & formula, int ipos);
140  void ExtractFunctors(TString &formula);
141  void PreProcessFormula(TString &formula);
142  void ProcessFormula(TString &formula);
143  Bool_t PrepareFormula(TString &formula);
144  void ReplaceParamName(TString &formula, const TString & oldname, const TString & name);
145  void DoAddParameter(const TString &name, Double_t value, bool processFormula);
146  void DoSetParameters(const Double_t * p, Int_t size);
147  void SetPredefinedParamNames();
148 
149  Double_t DoEval(const Double_t * x, const Double_t * p = nullptr) const;
150 #ifdef R__HAS_VECCORE
151  ROOT::Double_v DoEvalVec(const ROOT::Double_v *x, const Double_t *p = nullptr) const;
152 #endif
153 
154 public:
155 
156  enum EStatusBits {
157  kNotGlobal = BIT(10), // don't store in gROOT->GetListOfFunction (it should be protected)
158  kNormalized = BIT(14), // set to true if the TFormula (ex gausn) is normalized
159  kLinear = BIT(16), //set to true if the TFormula is for linear fitting
160  kLambda = BIT(17) // set to true if TFormula has been build with a lambda
161  };
162  TFormula();
163  virtual ~TFormula();
164  TFormula& operator=(const TFormula &rhs);
165  TFormula(const char *name, const char * formula = "", bool addToGlobList = true, bool vectorize = false);
166  TFormula(const char *name, const char * formula, int ndim, int npar, bool addToGlobList = true);
167  TFormula(const TFormula &formula);
168  // TFormula(const char *name, Int_t nparams, Int_t ndims);
169 
170  void AddParameter(const TString &name, Double_t value = 0) { DoAddParameter(name,value,true); }
171  void AddVariable(const TString &name, Double_t value = 0);
172  void AddVariables(const TString *vars, const Int_t size);
173  Int_t Compile(const char *expression="");
174  virtual void Copy(TObject &f1) const;
175  virtual void Clear(Option_t * option="");
176  Double_t Eval(Double_t x) const;
177  Double_t Eval(Double_t x, Double_t y) const;
180  Double_t EvalPar(const Double_t *x, const Double_t *params=0) const;
181  // template <class T>
182  // T Eval(T x, T y = 0, T z = 0, T t = 0) const;
183  template <class T>
184  T EvalPar(const T *x, const Double_t *params = 0) const {
185  return EvalParVec(x, params);
186  }
187 #ifdef R__HAS_VECCORE
188  ROOT::Double_v EvalParVec(const ROOT::Double_v *x, const Double_t *params = 0) const;
189 #endif
190  TString GetExpFormula(Option_t *option="") const;
191  const TObject *GetLinearPart(Int_t i) const;
192  Int_t GetNdim() const {return fNdim;}
193  Int_t GetNpar() const {return fNpar;}
194  Int_t GetNumber() const { return fNumber; }
195  const char * GetParName(Int_t ipar) const;
196  Int_t GetParNumber(const char * name) const;
197  Double_t GetParameter(const char * name) const;
198  Double_t GetParameter(Int_t param) const;
199  Double_t* GetParameters() const;
200  void GetParameters(Double_t *params) const;
201  Double_t GetVariable(const char *name) const;
202  Int_t GetVarNumber(const char *name) const;
203  TString GetVarName(Int_t ivar) const;
204  Bool_t IsValid() const { return fReadyToExecute && fClingInitialized; }
205  Bool_t IsVectorized() const { return fVectorized; }
206  Bool_t IsLinear() const { return TestBit(kLinear); }
207  void Print(Option_t *option = "") const;
208  void SetName(const char* name);
209  void SetParameter(const char* name, Double_t value);
210  void SetParameter(Int_t param, Double_t value);
211  void SetParameters(const Double_t *params);
212  //void SetParameters(const pair<TString,Double_t> *params, const Int_t size);
214  Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0,
215  Double_t p9=0,Double_t p10=0); // *MENU*
216  void SetParName(Int_t ipar, const char *name);
217  void SetParNames(const char *name0="p0",const char *name1="p1",const char
218  *name2="p2",const char *name3="p3",const char
219  *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char
220  *name8="p8",const char *name9="p9",const char *name10="p10"); // *MENU*
221  void SetVariable(const TString &name, Double_t value);
222  void SetVariables(const std::pair<TString,Double_t> *vars, const Int_t size);
223  void SetVectorized(Bool_t vectorized);
224 
225  ClassDef(TFormula,12)
226 };
227 #endif
static double p3(double t, double a, double b, double c, double d)
const char Option_t
Definition: RtypesCore.h:62
double T(double x)
Definition: ChebyshevPol.h:34
TString fName
Definition: TFormula.h:29
#define BIT(n)
Definition: Rtypes.h:78
Helper class for TFormula.
Definition: TFormula.h:26
Basic string class.
Definition: TString.h:131
Functor defining the parameter order.
Definition: TFormula.h:78
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Bool_t fFound
Definition: TFormula.h:32
TRObject operator()(const T1 &t1) const
TString fBody
Definition: TFormula.h:30
void SetParameters(TFitEditor::FuncParams_t &pars, TF1 *func)
Restore the parameters from pars into the function.
Definition: TFitEditor.cxx:287
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:320
Bool_t IsFuncCall() const
Definition: TFormula.h:37
Bool_t fFuncCall
Definition: TFormula.h:33
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
static double p2(double t, double a, double b, double c)
void(* Generic_t)(void *, int, void **, void *)
Definition: TInterpreter.h:90
Bool_t operator==(const TFormulaFunction &rhv) const
Definition: TFormula.h:53
Method or function calling interface.
Definition: TMethodCall.h:37
Double_t Double_v
Definition: Types.h:50
Int_t GetNargs() const
Definition: TFormula.h:36
auto * a
Definition: textangle.C:12
PyObject * fValue
The Formula class.
Definition: TFormula.h:83
const char * GetName() const
Definition: TFormula.h:34
Ssiz_t Length() const
Definition: TString.h:405
void GetParameters(TFitEditor::FuncParams_t &pars, TF1 *func)
Stores the parameters of the given function into pars.
Definition: TFitEditor.cxx:270
static double p1(double t, double a, double b)
Bool_t operator<(const TFormulaFunction &rhv) const
Definition: TFormula.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:88
void Copy(void *source, void *dest)
void Print(std::ostream &os, const OptionType &opt)
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
Binding & operator=(OUT(*fun)(void))
Mother of all ROOT objects.
Definition: TObject.h:37
TString fName
Definition: TFormula.h:62
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
const char * GetBody() const
Definition: TFormula.h:35
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
#define c(i)
Definition: RSha256.hxx:101
gr SetName("gr")
char name[80]
Definition: TGX11.cxx:109
Another helper class for TFormula.
Definition: TFormula.h:59
const char * Data() const
Definition: TString.h:364