ROOT  6.06/09
Reference Guide
TFormula.h
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Nicolas Brun 19/08/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 // ---------------------------------- Formula.h
12 
13 #ifndef ROOT_v5_TFormula
14 #define ROOT_v5_TFormula
15 
16 
17 
18 //////////////////////////////////////////////////////////////////////////
19 // //
20 // TFormula //
21 // //
22 // The old formula base class f(x,y,z,par) //
23 // mantained for backward compatibility and TTree usage //
24 //////////////////////////////////////////////////////////////////////////
25 
26 #ifndef ROOT_TNamed
27 #include "TNamed.h"
28 #endif
29 #ifndef ROOT_TBits
30 #include "TBits.h"
31 #endif
32 #ifndef ROOT_TObjArray
33 #include "TObjArray.h"
34 #endif
35 
36 
37 const Int_t kMAXFOUND = 500;
38 const Int_t kTFOperMask = 0x7fffff;
39 const UChar_t kTFOperShift = 23;
40 
41 
42 
43 namespace ROOT {
44  namespace v5 {
45 
46  class TFormulaPrimitive;
47 
48 class TOperOffset {
49  friend class TFormula;
50 public:
51  enum {
52  kVariable = 0,
55  };
56  TOperOffset();
57 protected:
58  Short_t fType0; // type of operand 0
59  Short_t fOffset0; // offset of operand 0
60  Short_t fType1; // type of operand 1
61  Short_t fOffset1; // offset of operand 1
62  Short_t fType2; // type of operand 2
63  Short_t fOffset2; // offset of operand 2
64  Short_t fType3; // type of operand 3
65  Short_t fOffset3; // offset of operand 3
66  Short_t fToJump; // where to jump in case of optimized boolen
67  Short_t fOldAction; // temporary variable used during optimization
68 };
69 
70 
71 class TFormula : public TNamed {
72 
73 protected:
74 
75  typedef Double_t (TObject::*TFuncG)(const Double_t*,const Double_t*) const;
76 
77  Int_t fNdim; //Dimension of function (1=1-Dim, 2=2-Dim,etc)
78  Int_t fNpar; //Number of parameters
79  Int_t fNoper; //Number of operators
80  Int_t fNconst; //Number of constants
81  Int_t fNumber; //formula number identifier
82  Int_t fNval; //Number of different variables in expression
83  Int_t fNstring; //Number of different constants character strings
84  TString *fExpr; //[fNoper] List of expressions
85 private:
86  Int_t *fOper; //[fNoper] List of operators. (See documentation for changes made at version 7)
87 protected:
88  Double_t *fConst; //[fNconst] Array of fNconst formula constants
89  Double_t *fParams; //[fNpar] Array of fNpar parameters
90  TString *fNames; //[fNpar] Array of parameter names
91  TObjArray fFunctions; //Array of function calls to make
92  TObjArray fLinearParts; //Linear parts if the formula is linear (contains '|' or "++")
93 
94  TBits fAlreadyFound; //! cache for information
95 
96  // Optimized expression
97  Int_t fNOperOptimized; //!Number of operators after optimization
98  TString *fExprOptimized; //![fNOperOptimized] List of expressions
99  Int_t *fOperOptimized; //![fNOperOptimized] List of operators. (See documentation for changes made at version 7)
100  TOperOffset *fOperOffset; //![fNOperOptimized] Offsets of operrands
101  TFormulaPrimitive **fPredefined; //![fNPar] predefined function
102  TFuncG fOptimal; //!pointer to optimal function
103 
105  virtual Bool_t CheckOperands(Int_t operation, Int_t &err);
106  virtual Bool_t CheckOperands(Int_t leftoperand, Int_t rightoperartion, Int_t &err);
107  virtual Bool_t StringToNumber(Int_t code);
108  void MakePrimitive(const char *expr, Int_t pos);
109  inline Int_t *GetOper() const { return fOper; }
110  inline Short_t GetAction(Int_t code) const { return fOper[code] >> kTFOperShift; }
111  inline Int_t GetActionParam(Int_t code) const { return fOper[code] & kTFOperMask; }
112 
113  inline void SetAction(Int_t code, Int_t value, Int_t param = 0) {
114  fOper[code] = (value) << kTFOperShift;
115  fOper[code] += param;
116  }
117  inline Int_t *GetOperOptimized() const { return fOperOptimized; }
118  inline Short_t GetActionOptimized(Int_t code) const { return fOperOptimized[code] >> kTFOperShift; }
119  inline Int_t GetActionParamOptimized(Int_t code) const { return fOperOptimized[code] & kTFOperMask; }
121  inline void SetActionOptimized(Int_t code, Int_t value, Int_t param = 0) {
122  fOperOptimized[code] = (value) << kTFOperShift;
123  fOperOptimized[code] += param;
124  }
126  void ClearFormula(Option_t *option="");
127  virtual Bool_t IsString(Int_t oper) const;
128 
129  virtual void Convert(UInt_t fromVersion);
130  //
131  // Functions - used for formula evaluation
132  Double_t EvalParFast(const Double_t *x, const Double_t *params);
133  Double_t EvalPrimitive(const Double_t *x, const Double_t *params);
134  Double_t EvalPrimitive0(const Double_t *x, const Double_t *params);
135  Double_t EvalPrimitive1(const Double_t *x, const Double_t *params);
136  Double_t EvalPrimitive2(const Double_t *x, const Double_t *params);
137  Double_t EvalPrimitive3(const Double_t *x, const Double_t *params);
138  Double_t EvalPrimitive4(const Double_t *x, const Double_t *params);
139 
140  // Action code for Version 6 and above.
141  enum {
142  kEnd = 0,
143  kAdd = 1, kSubstract = 2,
144  kMultiply = 3, kDivide = 4,
145  kModulo = 5,
146 
147  kcos = 10, ksin = 11 , ktan = 12,
148  kacos = 13, kasin = 14 , katan = 15,
149  katan2 = 16,
150  kfmod = 17,
152  kpow = 20, ksq = 21, ksqrt = 22,
153 
154  kstrstr = 23,
155 
156  kmin = 24, kmax = 25,
157 
158  klog = 30, kexp = 31, klog10 = 32,
159 
160  kpi = 40,
162  kabs = 41 , ksign= 42,
163  kint = 43 ,
164  kSignInv= 44 ,
165  krndm = 50 ,
166 
167  kAnd = 60, kOr = 61,
168  kEqual = 62, kNotEqual = 63,
169  kLess = 64, kGreater = 65,
171  kNot = 68,
172 
173  kcosh = 70 , ksinh = 71, ktanh = 72,
174  kacosh = 73 , kasinh = 74, katanh = 75,
177 
178  kBitAnd = 78, kBitOr = 79,
180 
181  kJumpIf = 82, kJump = 83,
182 
183  kexpo = 100 , kxexpo = 100, kyexpo = 101, kzexpo = 102, kxyexpo = 105,
184  kgaus = 110 , kxgaus = 110, kygaus = 111, kzgaus = 112, kxygaus = 115,
185  klandau = 120 , kxlandau = 120, kylandau = 121, kzlandau = 122, kxylandau = 125,
186  kpol = 130 , kxpol = 130, kypol = 131, kzpol = 132,
188  kParameter = 140,
189  kConstant = 141,
192  kVariable = 144,
194  kData = 146,
195  kUnary = 147,
196  kBinary = 148,
197  kThree = 149,
200  //
201  kPlusD = 152,
202  kPlusDD = 153,
203  kMultD = 154,
204  kMultDD = 155,
207  kBoolSet = 158,
208  kFDM = 159,
209  kFD0 = 160,
210  kFD1 = 161,
211  kFD2 = 162,
212  kFD3 = 163
213  };
214 
215 public:
216  // TFormula status bits
217  enum {
218  kNotGlobal = BIT(10), // don't store in gROOT->GetListOfFunction
219  kNormalized = BIT(14), // set to true if the function (ex gausn) is normalized
220  kLinear = BIT(16) //set to true if the function is for linear fitting
221  };
222 
223  TFormula();
224  TFormula(const char *name,const char *formula);
225  TFormula(const TFormula &formula);
226  TFormula& operator=(const TFormula &rhs);
227  virtual ~TFormula();
228 
229  public:
230  void Optimize();
231  virtual void Analyze(const char *schain, Int_t &err, Int_t offset=0);
232  virtual Bool_t AnalyzeFunction(TString &chaine, Int_t &err, Int_t offset=0);
233  virtual Int_t Compile(const char *expression="");
234  virtual void Copy(TObject &formula) const;
235  virtual void Clear(Option_t *option="");
236  virtual char *DefinedString(Int_t code);
237  virtual Double_t DefinedValue(Int_t code);
238  virtual Int_t DefinedVariable(TString &variable,Int_t &action);
239  virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const;
240  virtual Double_t EvalParOld(const Double_t *x, const Double_t *params=0);
241  virtual Double_t EvalPar(const Double_t *x, const Double_t *params=0){return ((*this).*fOptimal)(x,params);};
242  virtual const TObject *GetLinearPart(Int_t i);
243  virtual Int_t GetNdim() const {return fNdim;}
244  virtual Int_t GetNpar() const {return fNpar;}
245  virtual Int_t GetNumber() const {return fNumber;}
246  virtual TString GetExpFormula(Option_t *option="") const;
247  Double_t GetParameter(Int_t ipar) const;
248  Double_t GetParameter(const char *name) const;
249  virtual Double_t *GetParameters() const {return fParams;}
250  virtual void GetParameters(Double_t *params){for(Int_t i=0;i<fNpar;i++) params[i] = fParams[i];}
251  virtual const char *GetParName(Int_t ipar) const;
252  virtual Int_t GetParNumber(const char *name) const;
253  virtual Bool_t IsLinear() const {return TestBit(kLinear);}
254  virtual Bool_t IsNormalized() const {return TestBit(kNormalized);}
255  virtual void Print(Option_t *option="") const; // *MENU*
256  virtual void ProcessLinear(TString &replaceformula);
257  virtual void SetNumber(Int_t number) {fNumber = number;}
258  virtual void SetParameter(const char *name, Double_t parvalue);
259  virtual void SetParameter(Int_t ipar, Double_t parvalue);
260  virtual void SetParameters(const Double_t *params);
261  virtual void SetParameters(Double_t p0,Double_t p1,Double_t p2=0,Double_t p3=0,Double_t p4=0,
262  Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0,
263  Double_t p9=0,Double_t p10=0); // *MENU*
264  virtual void SetParName(Int_t ipar, const char *name);
265  virtual void SetParNames(const char *name0="p0",const char *name1="p1",const char
266  *name2="p2",const char *name3="p3",const char
267  *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char
268  *name8="p8",const char *name9="p9",const char *name10="p10"); // *MENU*
269  virtual void Update() {;}
270 
271  static void SetMaxima(Int_t maxop=1000, Int_t maxpar=1000, Int_t maxconst=1000);
272  static void GetMaxima(Int_t& maxop, Int_t& maxpar, Int_t& maxconst);
273 
274  void Streamer(TBuffer &b, const TClass *onfile_class);
275  void Streamer(TBuffer &b, Int_t version, UInt_t start, UInt_t count, const TClass *onfile_class = 0);
276 
277  ClassDef(ROOT::v5::TFormula,8) //The formula base class f(x,y,z,par)
278 };
279 
280  } // end namespace v5
281 
282 } // end namespace ROOT
283 
284 #endif
Int_t fNOperOptimized
cache for information
Definition: TFormula.h:97
virtual void Update()
Definition: TFormula.h:269
An array of TObjects.
Definition: TObjArray.h:39
virtual TString GetExpFormula(Option_t *option="") const
virtual void GetParameters(Double_t *params)
Definition: TFormula.h:250
Double_t(TObject::* TFuncG)(const Double_t *, const Double_t *) const
Definition: TFormula.h:75
static double p3(double t, double a, double b, double c, double d)
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
virtual Double_t DefinedValue(Int_t code)
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=0)
Definition: TFormula.h:241
virtual Bool_t StringToNumber(Int_t code)
Double_t GetParameter(Int_t ipar) const
const char Option_t
Definition: RtypesCore.h:62
Int_t GetActionParam(Int_t code) const
Definition: TFormula.h:111
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
#define BIT(n)
Definition: Rtypes.h:120
virtual Int_t Compile(const char *expression="")
static void SetMaxima(Int_t maxop=1000, Int_t maxpar=1000, Int_t maxconst=1000)
TBits fAlreadyFound
Definition: TFormula.h:94
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Basic string class.
Definition: TString.h:137
virtual void SetNumber(Int_t number)
Definition: TFormula.h:257
virtual Int_t GetNdim() const
Definition: TFormula.h:243
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Double_t EvalPrimitive2(const Double_t *x, const Double_t *params)
virtual Bool_t IsNormalized() const
Definition: TFormula.h:254
virtual const TObject * GetLinearPart(Int_t i)
const Int_t kTFOperMask
Definition: TFormula.h:38
Double_t * fParams
Definition: TFormula.h:89
virtual void SetParName(Int_t ipar, const char *name)
virtual Int_t GetNumber() const
Definition: TFormula.h:245
void SetAction(Int_t code, Int_t value, Int_t param=0)
Definition: TFormula.h:113
Double_t EvalPrimitive4(const Double_t *x, const Double_t *params)
static void GetMaxima(Int_t &maxop, Int_t &maxpar, Int_t &maxconst)
void SetActionOptimized(Int_t code, Int_t value, Int_t param=0)
Definition: TFormula.h:121
virtual char * DefinedString(Int_t code)
Double_t EvalPrimitive0(const Double_t *x, const Double_t *params)
Double_t x[n]
Definition: legend1.C:17
TObjArray fFunctions
Definition: TFormula.h:91
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual void SetParameters(const Double_t *params)
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
static double p2(double t, double a, double b, double c)
virtual void Clear(Option_t *option="")
Set name and title to empty strings ("").
virtual const char * GetParName(Int_t ipar) const
const Int_t kMAXFOUND
Definition: TFormula.h:37
Double_t EvalParFast(const Double_t *x, const Double_t *params)
virtual Int_t GetNpar() const
Definition: TFormula.h:244
Double_t EvalPrimitive3(const Double_t *x, const Double_t *params)
virtual void Convert(UInt_t fromVersion)
virtual Bool_t CheckOperands(Int_t operation, Int_t &err)
TFormulaPrimitive ** fPredefined
[fNOperOptimized] Offsets of operrands
Definition: TFormula.h:101
virtual Int_t GetParNumber(const char *name) const
Int_t GetActionParamOptimized(Int_t code) const
Definition: TFormula.h:119
Double_t EvalPrimitive(const Double_t *x, const Double_t *params)
The F O R M U L A class.
Definition: TFormula.h:89
Double_t * fConst
Definition: TFormula.h:88
Short_t GetActionOptimized(Int_t code) const
Definition: TFormula.h:118
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
virtual void Print(Option_t *option="") const
Print TNamed name and title.
short Short_t
Definition: RtypesCore.h:35
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
static double p1(double t, double a, double b)
virtual void ProcessLinear(TString &replaceformula)
const UChar_t kTFOperShift
Definition: TFormula.h:39
Short_t GetAction(Int_t code) const
Definition: TFormula.h:110
TString * fNames
Definition: TFormula.h:90
virtual Double_t * GetParameters() const
Definition: TFormula.h:249
virtual Double_t EvalParOld(const Double_t *x, const Double_t *params=0)
void ClearFormula(Option_t *option="")
virtual 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")
double Double_t
Definition: RtypesCore.h:55
virtual void Copy(TObject &formula) const
Copy this to obj.
Int_t PreCompile()
pointer to optimal function
virtual Bool_t AnalyzeFunction(TString &chaine, Int_t &err, Int_t offset=0)
Double_t EvalPrimitive1(const Double_t *x, const Double_t *params)
Double_t y[n]
Definition: legend1.C:17
virtual Bool_t IsLinear() const
Definition: TFormula.h:253
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
virtual Bool_t IsString(Int_t oper) const
Container of bits.
Definition: TBits.h:33
void Streamer(TBuffer &b, const TClass *onfile_class)
TOperOffset * fOperOffset
[fNOperOptimized] List of operators. (See documentation for changes made at version 7) ...
Definition: TFormula.h:100
TObjArray fLinearParts
Definition: TFormula.h:92
void MakePrimitive(const char *expr, Int_t pos)
TFuncG fOptimal
[fNPar] predefined function
Definition: TFormula.h:102
TString * fExpr
Definition: TFormula.h:84
Int_t * GetOper() const
Definition: TFormula.h:109
unsigned char UChar_t
Definition: RtypesCore.h:34
TFormula & operator=(const TFormula &rhs)
virtual Int_t DefinedVariable(TString &variable, Int_t &action)
float value
Definition: math.cpp:443
virtual void Analyze(const char *schain, Int_t &err, Int_t offset=0)
virtual void SetParameter(const char *name, Double_t parvalue)
Int_t * fOperOptimized
[fNOperOptimized] List of expressions
Definition: TFormula.h:99
TString * fExprOptimized
Number of operators after optimization.
Definition: TFormula.h:98
Int_t * GetOperOptimized() const
Definition: TFormula.h:117