WrappedMultiTF1.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: WrappedMultiTF1.h 32265 2010-02-08 10:10:44Z 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 WrappedTFunction
00012 
00013 #ifndef ROOT_Math_WrappedMultiTF1
00014 #define ROOT_Math_WrappedMultiTF1
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 IParamMultiFunction interface
00032    of multi-dimensions to be used in the ROOT::Math numerical algorithm
00033    The parameter are stored in this wrapper class, so the TF1 parameter values are not used for evaluating the function. 
00034    This allows for the copy of the wrapper function without the need to copy the TF1. 
00035    This wrapper class does not own the TF1 pointer, so it assumes it exists during the wrapper lifetime. 
00036 
00037    @ingroup CppFunctions
00038 */ 
00039 class WrappedMultiTF1 : public ROOT::Math::IParamMultiGradFunction {
00040 
00041 public: 
00042 
00043    typedef  ROOT::Math::IParamMultiGradFunction        BaseParamFunc; 
00044    typedef  ROOT::Math::IParamMultiFunction::BaseFunc  BaseFunc; 
00045  
00046 
00047    /** 
00048       constructor from a function pointer to a TF1 
00049       If dim = 0 dimension is taken from TF1::GetNdim(). 
00050       IN case of multi-dimensional function created using directly TF1 object the dimension 
00051       returned by TF1::GetNdim is always 1. The user must then pass the correct value of dim
00052    */ 
00053    WrappedMultiTF1 (TF1 & f, unsigned int dim = 0 );  
00054 
00055    /** 
00056       Destructor (no operations). Function pointer is not owned
00057    */ 
00058    virtual ~WrappedMultiTF1 () {}
00059 
00060    /** 
00061       Copy constructor
00062    */ 
00063    WrappedMultiTF1(const WrappedMultiTF1 & rhs); 
00064 
00065    /** 
00066       Assignment operator
00067    */ 
00068    WrappedMultiTF1 & operator = (const WrappedMultiTF1 & rhs); 
00069 
00070 
00071    /** @name interface inherited from IFunction */
00072 
00073    /** 
00074        Clone the wrapper but not the original function
00075    */
00076    IMultiGenFunction * Clone() const { 
00077       return new WrappedMultiTF1(*this); 
00078    } 
00079 
00080    /// function dimension
00081    unsigned int NDim() const { 
00082       return fDim;
00083    }
00084 
00085 
00086    /** @name interface inherited from IParamFunction */     
00087 
00088    /// get the parameter values (return values cached inside, those inside TF1 might be different) 
00089    const double * Parameters() const {
00090       return  (fParams.size() > 0) ? &fParams.front() : 0;  
00091    }
00092 
00093    /// set parameter values (only the cached one in this class,leave unchanges those of TF1)
00094    void SetParameters(const double * p) { 
00095       std::copy(p,p+fParams.size(),fParams.begin());
00096    } 
00097 
00098    /// return number of parameters 
00099    unsigned int NPar() const {
00100       return fParams.size();
00101    }
00102 
00103    /// return parameter name (from TF1)
00104    std::string ParameterName(unsigned int i) const { 
00105       return std::string(fFunc->GetParName(i)); 
00106    } 
00107 
00108    /// evaluate the derivative of the function with respect to the parameters
00109    void  ParameterGradient(const double * x, const double * par, double * grad ) const;
00110 
00111    /// precision value used for calculating the derivative step-size 
00112    /// h = eps * |x|. The default is 0.001, give a smaller in case function changes rapidly
00113    static void SetDerivPrecision(double eps); 
00114 
00115    /// get precision value used for calculating the derivative step-size 
00116    static double GetDerivPrecision();
00117 
00118 
00119 private: 
00120 
00121    /// evaluate function passing coordinates x and vector of parameters
00122    double DoEvalPar (const double * x, const double * p ) const { 
00123       if (fFunc->GetMethodCall() )  fFunc->InitArgs(x,p);  // needed for interpreted functions 
00124       return fFunc->EvalPar(x,p); 
00125    }
00126 
00127    /// evaluate the partial derivative with respect to the parameter
00128    double DoParameterDerivative(const double * x, const double * p, unsigned int ipar) const; 
00129 
00130 
00131    bool fLinear;                 // flag for linear functions 
00132    TF1 * fFunc;                   // pointer to ROOT function
00133    unsigned int fDim;             // cached value of dimension
00134    std::vector<double> fParams;   // cached vector with parameter values
00135 
00136    static double fgEps;          // epsilon used in derivative calculation h ~ eps |p|
00137 }; 
00138 
00139    } // end namespace Fit
00140 
00141 } // end namespace ROOT
00142 
00143 
00144 #endif /* ROOT_Fit_WrappedMultiTF1 */

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