Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:23

0001 // @(#)root/mathmore:$Id$
0002 // Author: L. Moneta Wed Sep  6 09:52:26 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for class WrappedTF1
0012 
0013 #ifndef ROOT_Math_WrappedTF1
0014 #define ROOT_Math_WrappedTF1
0015 
0016 
0017 #include "Math/IParamFunction.h"
0018 
0019 #include "TF1.h"
0020 #include <string>
0021 #include <vector>
0022 
0023 namespace ROOT {
0024 
0025    namespace Math {
0026 
0027 
0028       /**
0029          Class to Wrap a ROOT Function class (like TF1)  in a IParamFunction interface
0030          of one dimensions to be used in the ROOT::Math numerical algorithms
0031          The wrapper does not own bby default the TF1 pointer, so it assumes it exists during the wrapper lifetime
0032 
0033          The class from ROOT version 6.03  does not contain anymore a copy of the parameters. The parameters are
0034          stored in the TF1 class.
0035 
0036 
0037          @ingroup CppFunctions
0038       */
0039       class WrappedTF1 : public ROOT::Math::IParamGradFunction, public ROOT::Math::IGradientFunctionOneDim {
0040 
0041       public:
0042 
0043          typedef  ROOT::Math::IGradientFunctionOneDim     IGrad;
0044          typedef  ROOT::Math::IParamGradFunction  BaseGradFunc;
0045          typedef  ROOT::Math::IParamGradFunction::BaseFunc BaseFunc;
0046 
0047 
0048          /**
0049             constructor from a TF1 function pointer.
0050          */
0051          WrappedTF1(TF1 &f);
0052 
0053          /**
0054             Destructor (no operations). TF1 Function pointer is not owned
0055          */
0056          ~WrappedTF1() override {}
0057 
0058          /**
0059             Copy constructor
0060          */
0061          WrappedTF1(const WrappedTF1 &rhs);
0062 
0063          /**
0064             Assignment operator
0065          */
0066          WrappedTF1 &operator = (const WrappedTF1 &rhs);
0067 
0068          /** @name interface inherited from IFunction */
0069 
0070          /**
0071              Clone the wrapper but not the original function
0072          */
0073          ROOT::Math::IGenFunction *Clone() const override
0074          {
0075             return  new WrappedTF1(*this);
0076          }
0077 
0078 
0079          /** @name interface inherited from IParamFunction */
0080 
0081          /// get the parameter values (return values cachen inside, those inside TF1 might be different)
0082          const double *Parameters() const override
0083          {
0084             //return  (fParams.size() > 0) ? &fParams.front() : 0;
0085             return fFunc->GetParameters();
0086          }
0087 
0088          /// set parameter values
0089          /// need to call also SetParameters in TF1 in ace some other operations (re-normalizations) are needed
0090          void SetParameters(const double *p) override
0091          {
0092             //std::copy(p,p+fParams.size(),fParams.begin());
0093             fFunc->SetParameters(p);
0094          }
0095 
0096          /// return number of parameters
0097          unsigned int NPar() const override
0098          {
0099             //return fParams.size();
0100             return fFunc->GetNpar();
0101          }
0102 
0103          /// return parameter name (this is stored in TF1)
0104          std::string ParameterName(unsigned int i) const override
0105          {
0106             return std::string(fFunc->GetParName(i));
0107          }
0108 
0109 
0110          using BaseGradFunc::operator();
0111 
0112          /// evaluate the derivative of the function with respect to the parameters
0113          void  ParameterGradient(double x, const double *par, double *grad) const override;
0114 
0115          /// calculate function and derivative at same time (required by IGradient interface)
0116          void FdF(double x, double &f, double &deriv) const override
0117          {
0118             f = DoEval(x);
0119             deriv = DoDerivative(x);
0120          }
0121 
0122          /// precision value used for calculating the derivative step-size
0123          /// h = eps * |x|. The default is 0.001, give a smaller in case function changes rapidly
0124          static void SetDerivPrecision(double eps);
0125 
0126          /// get precision value used for calculating the derivative step-size
0127          static double GetDerivPrecision();
0128 
0129       private:
0130 
0131 
0132          /// evaluate function passing coordinates x and vector of parameters
0133          double DoEvalPar(double x, const double *p) const override
0134          {
0135             fX[0] = x;
0136             if (fFunc->GetMethodCall()) fFunc->InitArgs(fX, p);  // needed for interpreted functions
0137             return fFunc->EvalPar(fX, p);
0138          }
0139 
0140          /// evaluate function using the cached parameter values (of TF1)
0141          /// re-implement for better efficiency
0142          double DoEval(double x) const override
0143          {
0144             // no need to call InitArg for interpreted functions (done in ctor)
0145             // use EvalPar since it is much more efficient than Eval
0146             fX[0] = x;
0147             //const double * p = (fParams.size() > 0) ? &fParams.front() : 0;
0148             return fFunc->EvalPar(fX, nullptr);
0149          }
0150 
0151          /// return the function derivatives w.r.t. x
0152          double DoDerivative(double  x) const override;
0153 
0154          /// evaluate the derivative of the function with respect to the parameters
0155          double  DoParameterDerivative(double x, const double *p, unsigned int ipar) const override;
0156 
0157          bool fLinear;                 // flag for linear functions
0158          bool fPolynomial;             // flag for polynomial functions
0159          TF1 *fFunc;                   // pointer to ROOT function
0160          mutable double fX[1];         //! cached vector for x value (needed for TF1::EvalPar signature)
0161          //std::vector<double> fParams;  //  cached vector with parameter values
0162 
0163       };
0164 
0165    } // end namespace Fit
0166 
0167 } // end namespace ROOT
0168 
0169 
0170 #endif /* ROOT_Fit_WrappedTF1 */