Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
WrappedTF1.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta Wed Sep 6 09:52:26 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class WrappedTF1
12 
13 #ifndef ROOT_Math_WrappedTF1
14 #define ROOT_Math_WrappedTF1
15 
16 
17 #include "Math/IParamFunction.h"
18 
19 #include "TF1.h"
20 
21 namespace ROOT {
22 
23  namespace Math {
24 
25 
26  /**
27  Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface
28  of one dimensions to be used in the ROOT::Math numerical algorithms
29  The wrapper does not own bby default the TF1 pointer, so it assumes it exists during the wrapper lifetime
30 
31  The class from ROOT version 6.03 does not contain anymore a copy of the parameters. The parameters are
32  stored in the TF1 class.
33 
34 
35  @ingroup CppFunctions
36  */
37  class WrappedTF1 : public ROOT::Math::IParamGradFunction, public ROOT::Math::IGradientOneDim {
38 
39  public:
40 
41  typedef ROOT::Math::IGradientOneDim IGrad;
42  typedef ROOT::Math::IParamGradFunction BaseGradFunc;
43  typedef ROOT::Math::IParamGradFunction::BaseFunc BaseFunc;
44 
45 
46  /**
47  constructor from a TF1 function pointer.
48  */
49  WrappedTF1(TF1 &f);
50 
51  /**
52  Destructor (no operations). TF1 Function pointer is not owned
53  */
54  virtual ~WrappedTF1() {}
55 
56  /**
57  Copy constructor
58  */
59  WrappedTF1(const WrappedTF1 &rhs);
60 
61  /**
62  Assignment operator
63  */
64  WrappedTF1 &operator = (const WrappedTF1 &rhs);
65 
66  /** @name interface inherited from IFunction */
67 
68  /**
69  Clone the wrapper but not the original function
70  */
71  ROOT::Math::IGenFunction *Clone() const
72  {
73  return new WrappedTF1(*this);
74  }
75 
76 
77  /** @name interface inherited from IParamFunction */
78 
79  /// get the parameter values (return values cachen inside, those inside TF1 might be different)
80  const double *Parameters() const
81  {
82  //return (fParams.size() > 0) ? &fParams.front() : 0;
83  return fFunc->GetParameters();
84  }
85 
86  /// set parameter values
87  /// need to call also SetParameters in TF1 in ace some other operations (re-normalizations) are needed
88  void SetParameters(const double *p)
89  {
90  //std::copy(p,p+fParams.size(),fParams.begin());
91  fFunc->SetParameters(p);
92  }
93 
94  /// return number of parameters
95  unsigned int NPar() const
96  {
97  //return fParams.size();
98  return fFunc->GetNpar();
99  }
100 
101  /// return parameter name (this is stored in TF1)
102  std::string ParameterName(unsigned int i) const
103  {
104  return std::string(fFunc->GetParName(i));
105  }
106 
107 
108  using BaseGradFunc::operator();
109 
110  /// evaluate the derivative of the function with respect to the parameters
111  void ParameterGradient(double x, const double *par, double *grad) const;
112 
113  /// calculate function and derivative at same time (required by IGradient interface)
114  void FdF(double x, double &f, double &deriv) const
115  {
116  f = DoEval(x);
117  deriv = DoDerivative(x);
118  }
119 
120  /// precision value used for calculating the derivative step-size
121  /// h = eps * |x|. The default is 0.001, give a smaller in case function changes rapidly
122  static void SetDerivPrecision(double eps);
123 
124  /// get precision value used for calculating the derivative step-size
125  static double GetDerivPrecision();
126 
127  private:
128 
129 
130  /// evaluate function passing coordinates x and vector of parameters
131  double DoEvalPar(double x, const double *p) const
132  {
133  fX[0] = x;
134  if (fFunc->GetMethodCall()) fFunc->InitArgs(fX, p); // needed for interpreted functions
135  return fFunc->EvalPar(fX, p);
136  }
137 
138  /// evaluate function using the cached parameter values (of TF1)
139  /// re-implement for better efficiency
140  double DoEval(double x) const
141  {
142  // no need to call InitArg for interpreted functions (done in ctor)
143  // use EvalPar since it is much more efficient than Eval
144  fX[0] = x;
145  //const double * p = (fParams.size() > 0) ? &fParams.front() : 0;
146  return fFunc->EvalPar(fX, 0);
147  }
148 
149  /// return the function derivatives w.r.t. x
150  double DoDerivative(double x) const;
151 
152  /// evaluate the derivative of the function with respect to the parameters
153  double DoParameterDerivative(double x, const double *p, unsigned int ipar) const;
154 
155  bool fLinear; // flag for linear functions
156  bool fPolynomial; // flag for polynomial functions
157  TF1 *fFunc; // pointer to ROOT function
158  mutable double fX[1]; //! cached vector for x value (needed for TF1::EvalPar signature)
159  //std::vector<double> fParams; // cached vector with parameter values
160 
161  };
162 
163  } // end namespace Fit
164 
165 } // end namespace ROOT
166 
167 
168 #endif /* ROOT_Fit_WrappedTF1 */