WrappedTF1.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: WrappedTF1.h 34993 2010-08-25 10:41:19Z moneta $
00002 // Author: L. Moneta Wed Sep  6 09:52:26 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class WrappedTF1
00012 
00013 #ifndef ROOT_Math_WrappedTF1
00014 #define ROOT_Math_WrappedTF1
00015 
00016 
00017 #ifndef ROOT_Math_IParamFunction
00018 #include "Math/IParamFunction.h"
00019 #endif
00020 
00021 #ifndef ROOT_TF1
00022 #include "TF1.h"
00023 #endif
00024 
00025 namespace ROOT { 
00026 
00027    namespace Math { 
00028 
00029 
00030 /** 
00031    Class to Wrap a ROOT Function class (like TF1)  in a IParamFunction interface
00032    of one dimensions to be used in the ROOT::Math numerical algorithms
00033    The parameter are stored in this wrapper class, so  the TF1 parameter values are not used for evaluating the function. 
00034    We use TF1 only for the function evaluation. 
00035    This allows for the copy of the wrapper function without the need to copy the TF1. 
00036    The wrapper does not own the TF1 pointer, so it assumes it exists during the wrapper lifetime
00037 
00038    @ingroup CppFunctions
00039 */ 
00040 class WrappedTF1 : public ROOT::Math::IParamGradFunction, public ROOT::Math::IGradientOneDim {
00041 
00042 public: 
00043 
00044    typedef  ROOT::Math::IGradientOneDim     IGrad;
00045    typedef  ROOT::Math::IParamGradFunction  BaseGradFunc; 
00046    typedef  ROOT::Math::IParamGradFunction::BaseFunc BaseFunc; 
00047  
00048 
00049    /** 
00050       constructor from a TF1 function pointer. 
00051    */ 
00052    WrappedTF1 ( TF1 & f  ); 
00053 
00054    /** 
00055       Destructor (no operations). TF1 Function pointer is not owned
00056    */ 
00057    virtual ~WrappedTF1 () {}
00058 
00059    /** 
00060       Copy constructor
00061    */ 
00062    WrappedTF1(const WrappedTF1 & rhs);
00063 
00064    /** 
00065       Assignment operator
00066    */ 
00067    WrappedTF1 & operator = (const WrappedTF1 & rhs); 
00068 
00069    /** @name interface inherited from IFunction */
00070 
00071    /** 
00072        Clone the wrapper but not the original function
00073    */
00074    ROOT::Math::IGenFunction * Clone() const { 
00075       return  new WrappedTF1(*this); 
00076    } 
00077 
00078 
00079    /** @name interface inherited from IParamFunction */     
00080 
00081    /// get the parameter values (return values cachen inside, those inside TF1 might be different) 
00082    const double * Parameters() const {
00083       return  (fParams.size() > 0) ? &fParams.front() : 0;
00084    }
00085 
00086    /// set parameter values (only the cached one in this class,leave unchanges those of TF1)
00087    void SetParameters(const double * p) { 
00088       std::copy(p,p+fParams.size(),fParams.begin());
00089    } 
00090 
00091    /// return number of parameters 
00092    unsigned int NPar() const { 
00093       return fParams.size();
00094    }
00095 
00096    /// return parameter name (this is stored in TF1)
00097    std::string ParameterName(unsigned int i) const { 
00098       return std::string(fFunc->GetParName(i)); 
00099    } 
00100 
00101 
00102    using BaseGradFunc::operator();
00103 
00104    /// evaluate the derivative of the function with respect to the parameters
00105    void  ParameterGradient(double x, const double * par, double * grad ) const;
00106 
00107    /// calculate function and derivative at same time (required by IGradient interface)
00108    void FdF(double x, double & f, double & deriv) const { 
00109       f = DoEval(x); 
00110       deriv = DoDerivative(x);
00111    }      
00112 
00113    /// precision value used for calculating the derivative step-size 
00114    /// h = eps * |x|. The default is 0.001, give a smaller in case function changes rapidly
00115    static void SetDerivPrecision(double eps); 
00116 
00117    /// get precision value used for calculating the derivative step-size 
00118    static double GetDerivPrecision();
00119 
00120 private: 
00121 
00122 
00123    /// evaluate function passing coordinates x and vector of parameters
00124    double DoEvalPar (double x, const double * p ) const { 
00125       fX[0] = x;  
00126       if (fFunc->GetMethodCall() ) fFunc->InitArgs(fX,p);  // needed for interpreted functions 
00127       return fFunc->EvalPar(fX,p); 
00128    }
00129 
00130    /// evaluate function using the cached parameter values of this class (not of TF1)
00131    /// re-implement for better efficiency
00132    double DoEval (double x) const { 
00133       // no need to call InitArg for interpreted functions (done in ctor)
00134       // use EvalPar since it is much more efficient than Eval
00135       fX[0] = x;  
00136       const double * p = (fParams.size() > 0) ? &fParams.front() : 0;
00137       return fFunc->EvalPar(fX, p ); 
00138    }
00139 
00140    /// return the function derivatives w.r.t. x 
00141    double DoDerivative( double  x  ) const;
00142 
00143    /// evaluate the derivative of the function with respect to the parameters
00144    double  DoParameterDerivative(double x, const double * p, unsigned int ipar ) const; 
00145 
00146    bool fLinear;                 // flag for linear functions 
00147    bool fPolynomial;             // flag for polynomial functions 
00148    TF1 * fFunc;                  // pointer to ROOT function
00149    mutable double fX[1];         //! cached vector for x value (needed for TF1::EvalPar signature) 
00150    std::vector<double> fParams;  //  cached vector with parameter values
00151 
00152    static double fgEps;          // epsilon used in derivative calculation h ~ eps |x|
00153 }; 
00154 
00155    } // end namespace Fit
00156 
00157 } // end namespace ROOT
00158 
00159 
00160 #endif /* ROOT_Fit_WrappedTF1 */

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