TFormula.h

Go to the documentation of this file.
00001 // @(#)root/hist:$Id: TFormula.h 30176 2009-09-15 15:42:11Z pcanal $
00002 // Author: Nicolas Brun   19/08/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 // ---------------------------------- Formula.h
00012 
00013 #ifndef ROOT_TFormula
00014 #define ROOT_TFormula
00015 
00016 
00017 
00018 //////////////////////////////////////////////////////////////////////////
00019 //                                                                      //
00020 // TFormula                                                             //
00021 //                                                                      //
00022 // The formula base class  f(x,y,z,par)                                 //
00023 //                                                                      //
00024 //////////////////////////////////////////////////////////////////////////
00025 
00026 #ifndef ROOT_TNamed
00027 #include "TNamed.h"
00028 #endif
00029 #ifndef ROOT_TBits
00030 #include "TBits.h"
00031 #endif
00032 #ifndef ROOT_TObjArray
00033 #include "TObjArray.h"
00034 #endif
00035 
00036 class TFormulaPrimitive;
00037 
00038 const Int_t kMAXFOUND = 500;
00039 const Int_t kTFOperMask = 0x7fffff;
00040 const UChar_t kTFOperShift = 23;
00041 
00042 
00043 class  TOperOffset {
00044    friend class TFormula;
00045 public:
00046    enum {
00047       kVariable  = 0,
00048       kParameter = 1,
00049       kConstant  = 2 
00050    };
00051    TOperOffset();              
00052 protected:
00053    Short_t fType0;            // type     of operand  0
00054    Short_t fOffset0;          // offset   of operand  0
00055    Short_t fType1;            // type     of operand  1
00056    Short_t fOffset1;          // offset   of operand  1
00057    Short_t fType2;            // type     of operand  2
00058    Short_t fOffset2;          // offset   of operand  2
00059    Short_t fType3;            // type     of operand  3
00060    Short_t fOffset3;          // offset   of operand  3
00061    Short_t fToJump;           // where to jump in case of optimized boolen
00062    Short_t fOldAction;        // temporary variable used during optimization
00063 };
00064 
00065 class TFormula : public TNamed {
00066 
00067 protected:
00068 
00069    typedef Double_t (TObject::*TFuncG)(const Double_t*,const Double_t*) const;
00070 
00071    Int_t      fNdim;            //Dimension of function (1=1-Dim, 2=2-Dim,etc)
00072    Int_t      fNpar;            //Number of parameters
00073    Int_t      fNoper;           //Number of operators
00074    Int_t      fNconst;          //Number of constants
00075    Int_t      fNumber;          //formula number identifier
00076    Int_t      fNval;            //Number of different variables in expression
00077    Int_t      fNstring;         //Number of different constants character strings
00078    TString   *fExpr;            //[fNoper] List of expressions
00079 private:
00080    Int_t     *fOper;            //[fNoper] List of operators. (See documentation for changes made at version 7)
00081 protected:
00082    Double_t  *fConst;           //[fNconst] Array of fNconst formula constants
00083    Double_t  *fParams;          //[fNpar] Array of fNpar parameters
00084    TString   *fNames;           //[fNpar] Array of parameter names
00085    TObjArray  fFunctions;       //Array of function calls to make
00086    TObjArray  fLinearParts;     //Linear parts if the formula is linear (contains '|' or "++")
00087 
00088    TBits      fAlreadyFound;    //! cache for information
00089 
00090    // Optimized expression
00091    Int_t                fNOperOptimized; //!Number of operators after optimization
00092    TString             *fExprOptimized;  //![fNOperOptimized] List of expressions
00093    Int_t               *fOperOptimized;  //![fNOperOptimized] List of operators. (See documentation for changes made at version 7)
00094    TOperOffset         *fOperOffset;     //![fNOperOptimized]         Offsets of operrands
00095    TFormulaPrimitive  **fPredefined;      //![fNPar] predefined function  
00096    TFuncG               fOptimal; //!pointer to optimal function
00097 
00098    Int_t             PreCompile();
00099    virtual Bool_t    CheckOperands(Int_t operation, Int_t &err);
00100    virtual Bool_t    CheckOperands(Int_t leftoperand, Int_t rightoperartion, Int_t &err);
00101    virtual Bool_t    StringToNumber(Int_t code);
00102    void              MakePrimitive(const char *expr, Int_t pos);
00103    inline Int_t     *GetOper() const { return fOper; }
00104    inline Short_t    GetAction(Int_t code) const { return fOper[code] >> kTFOperShift; }
00105    inline Int_t      GetActionParam(Int_t code) const { return fOper[code] & kTFOperMask; }
00106 
00107    inline void       SetAction(Int_t code, Int_t value, Int_t param = 0) { 
00108       fOper[code]  = (value) << kTFOperShift; 
00109       fOper[code] += param;
00110    }
00111    inline Int_t     *GetOperOptimized() const { return fOperOptimized; }
00112    inline Short_t    GetActionOptimized(Int_t code) const { return fOperOptimized[code] >> kTFOperShift; }
00113    inline Int_t      GetActionParamOptimized(Int_t code) const { return fOperOptimized[code] & kTFOperMask; }
00114 
00115    inline void       SetActionOptimized(Int_t code, Int_t value, Int_t param = 0) { 
00116       fOperOptimized[code]  = (value) << kTFOperShift; 
00117       fOperOptimized[code] += param;
00118    }
00119 
00120    void            ClearFormula(Option_t *option="");
00121    virtual Bool_t  IsString(Int_t oper) const;
00122 
00123    virtual void    Convert(UInt_t fromVersion); 
00124    //
00125    // Functions  - used for formula evaluation
00126    Double_t        EvalParFast(const Double_t *x, const Double_t *params);
00127    Double_t        EvalPrimitive(const Double_t *x, const Double_t *params);
00128    Double_t        EvalPrimitive0(const Double_t *x, const Double_t *params);
00129    Double_t        EvalPrimitive1(const Double_t *x, const Double_t *params);
00130    Double_t        EvalPrimitive2(const Double_t *x, const Double_t *params);
00131    Double_t        EvalPrimitive3(const Double_t *x, const Double_t *params);
00132    Double_t        EvalPrimitive4(const Double_t *x, const Double_t *params);
00133 
00134    // Action code for Version 6 and above.
00135    enum {
00136       kEnd      = 0,
00137       kAdd      = 1, kSubstract = 2, 
00138       kMultiply = 3, kDivide    = 4,
00139       kModulo   = 5, 
00140 
00141       kcos      = 10, ksin  = 11 , ktan  = 12, 
00142       kacos     = 13, kasin = 14 , katan = 15, 
00143       katan2    = 16,
00144       kfmod     = 17, 
00145 
00146       kpow      = 20, ksq = 21, ksqrt     = 22, 
00147 
00148       kstrstr   = 23,
00149 
00150       kmin      = 24, kmax = 25,
00151 
00152       klog      = 30, kexp = 31, klog10 = 32,
00153       
00154       kpi     = 40,
00155 
00156       kabs    = 41 , ksign= 42, 
00157       kint    = 43 , 
00158       kSignInv= 44 ,
00159       krndm   = 50 ,
00160 
00161       kAnd      = 60, kOr          = 61,
00162       kEqual    = 62, kNotEqual    = 63,
00163       kLess     = 64, kGreater     = 65,
00164       kLessThan = 66, kGreaterThan = 67,
00165       kNot      = 68,
00166 
00167       kcosh   = 70 , ksinh  = 71, ktanh  = 72,
00168       kacosh  = 73 , kasinh = 74, katanh = 75,
00169 
00170       kStringEqual = 76, kStringNotEqual = 77,
00171 
00172       kBitAnd    = 78, kBitOr     = 79,
00173       kLeftShift = 80, kRightShift = 81,
00174 
00175       kJumpIf = 82, kJump = 83,
00176       
00177       kexpo   = 100 , kxexpo   = 100, kyexpo   = 101, kzexpo   = 102, kxyexpo   = 105,
00178       kgaus   = 110 , kxgaus   = 110, kygaus   = 111, kzgaus   = 112, kxygaus   = 115,
00179       klandau = 120 , kxlandau = 120, kylandau = 121, kzlandau = 122, kxylandau = 125,
00180       kpol    = 130 , kxpol    = 130, kypol    = 131, kzpol    = 132,
00181 
00182       kParameter       = 140,
00183       kConstant        = 141,
00184       kBoolOptimize    = 142,
00185       kStringConst     = 143,
00186       kVariable        = 144,
00187       kFunctionCall    = 145,
00188       kData            = 146,
00189       kUnary           = 147,
00190       kBinary          = 148,
00191       kThree           = 149,
00192       kDefinedVariable = 150,
00193       kDefinedString   = 151,
00194       //
00195       kPlusD           = 152,
00196       kPlusDD          = 153,
00197       kMultD           = 154,
00198       kMultDD          = 155,
00199       kBoolOptimizeOr  = 156,
00200       kBoolOptimizeAnd = 157,
00201       kBoolSet         = 158,
00202       kFDM             = 159,
00203       kFD0             = 160,
00204       kFD1             = 161,
00205       kFD2             = 162,
00206       kFD3             = 163
00207    };
00208 
00209 public:
00210    // TFormula status bits
00211    enum {
00212       kNotGlobal     = BIT(10),  // don't store in gROOT->GetListOfFunction
00213       kNormalized    = BIT(14),   // set to true if the function (ex gausn) is normalized
00214       kLinear        = BIT(16)    //set to true if the function is for linear fitting
00215    };
00216    
00217                TFormula();
00218                TFormula(const char *name,const char *formula);
00219                TFormula(const TFormula &formula);
00220    TFormula&   operator=(const TFormula &rhs);
00221    virtual    ~TFormula();
00222 
00223  public:
00224    void                Optimize();
00225    virtual void        Analyze(const char *schain, Int_t &err, Int_t offset=0);
00226    virtual Bool_t      AnalyzeFunction(TString &chaine, Int_t &err, Int_t offset=0);
00227    virtual Int_t       Compile(const char *expression="");
00228    virtual void        Copy(TObject &formula) const;
00229    virtual void        Clear(Option_t *option="");
00230    virtual char       *DefinedString(Int_t code);
00231    virtual Double_t    DefinedValue(Int_t code);
00232    virtual Int_t       DefinedVariable(TString &variable,Int_t &action);
00233    virtual Double_t    Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const;
00234    virtual Double_t    EvalParOld(const Double_t *x, const Double_t *params=0);
00235    virtual Double_t    EvalPar(const Double_t *x, const Double_t *params=0){return ((*this).*fOptimal)(x,params);};
00236    virtual const TObject *GetLinearPart(Int_t i);
00237    virtual Int_t       GetNdim() const {return fNdim;}
00238    virtual Int_t       GetNpar() const {return fNpar;}
00239    virtual Int_t       GetNumber() const {return fNumber;}
00240    virtual TString     GetExpFormula(Option_t *option="") const;
00241    Double_t            GetParameter(Int_t ipar) const;
00242    Double_t            GetParameter(const char *name) const;
00243    virtual Double_t   *GetParameters() const {return fParams;}
00244    virtual void        GetParameters(Double_t *params){for(Int_t i=0;i<fNpar;i++) params[i] = fParams[i];}
00245    virtual const char *GetParName(Int_t ipar) const;
00246    virtual Int_t       GetParNumber(const char *name) const;
00247    virtual Bool_t      IsLinear() {return TestBit(kLinear);}
00248    virtual Bool_t      IsNormalized() {return TestBit(kNormalized);}
00249    virtual void        Print(Option_t *option="") const; // *MENU*
00250    virtual void        ProcessLinear(TString &replaceformula);
00251    virtual void        SetNumber(Int_t number) {fNumber = number;}
00252    virtual void        SetParameter(const char *name, Double_t parvalue);
00253    virtual void        SetParameter(Int_t ipar, Double_t parvalue);
00254    virtual void        SetParameters(const Double_t *params);
00255    virtual void        SetParameters(Double_t p0,Double_t p1,Double_t p2=0,Double_t p3=0,Double_t p4=0,
00256                                      Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0,
00257                                      Double_t p9=0,Double_t p10=0); // *MENU*
00258    virtual void        SetParName(Int_t ipar, const char *name);
00259    virtual void        SetParNames(const char *name0="p0",const char *name1="p1",const char
00260                                    *name2="p2",const char *name3="p3",const char
00261                                    *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char
00262                                    *name8="p8",const char *name9="p9",const char *name10="p10"); // *MENU*
00263    virtual void        Update() {;}
00264 
00265    static  void        SetMaxima(Int_t maxop=1000, Int_t maxpar=1000, Int_t maxconst=1000);
00266    
00267    ClassDef(TFormula,8)  //The formula base class  f(x,y,z,par)
00268 };
00269 
00270 #endif

Generated on Tue Jul 5 14:22:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1