Logo ROOT   6.08/07
Reference Guide
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 #ifndef ROOT_Math_IParamFunction
18 #include "Math/IParamFunction.h"
19 #endif
20 
21 #ifndef ROOT_TF1
22 #include "TF1.h"
23 #endif
24 
25 namespace ROOT {
26 
27  namespace Math {
28 
29 
30 /**
31  Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface
32  of one dimensions to be used in the ROOT::Math numerical algorithms
33  The wrapper does not own bby default the TF1 pointer, so it assumes it exists during the wrapper lifetime
34 
35  The class from ROOT version 6.03 does not contain anymore a copy of the parameters. The parameters are
36  stored in the TF1 class.
37 
38 
39  @ingroup CppFunctions
40 */
42 
43 public:
44 
48 
49 
50  /**
51  constructor from a TF1 function pointer.
52  */
53  WrappedTF1 ( TF1 & f );
54 
55  /**
56  Destructor (no operations). TF1 Function pointer is not owned
57  */
58  virtual ~WrappedTF1 () {}
59 
60  /**
61  Copy constructor
62  */
63  WrappedTF1(const WrappedTF1 & rhs);
64 
65  /**
66  Assignment operator
67  */
68  WrappedTF1 & operator = (const WrappedTF1 & rhs);
69 
70  /** @name interface inherited from IFunction */
71 
72  /**
73  Clone the wrapper but not the original function
74  */
76  return new WrappedTF1(*this);
77  }
78 
79 
80  /** @name interface inherited from IParamFunction */
81 
82  /// get the parameter values (return values cachen inside, those inside TF1 might be different)
83  const double * Parameters() const {
84  //return (fParams.size() > 0) ? &fParams.front() : 0;
85  return fFunc->GetParameters();
86  }
87 
88  /// set parameter values
89  /// need to call also SetParameters in TF1 in ace some other operations (re-normalizations) are needed
90  void SetParameters(const double * p) {
91  //std::copy(p,p+fParams.size(),fParams.begin());
92  fFunc->SetParameters(p);
93  }
94 
95  /// return number of parameters
96  unsigned int NPar() const {
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  return std::string(fFunc->GetParName(i));
104  }
105 
106 
107  using BaseGradFunc::operator();
108 
109  /// evaluate the derivative of the function with respect to the parameters
110  void ParameterGradient(double x, const double * par, double * grad ) const;
111 
112  /// calculate function and derivative at same time (required by IGradient interface)
113  void FdF(double x, double & f, double & deriv) const {
114  f = DoEval(x);
115  deriv = DoDerivative(x);
116  }
117 
118  /// precision value used for calculating the derivative step-size
119  /// h = eps * |x|. The default is 0.001, give a smaller in case function changes rapidly
120  static void SetDerivPrecision(double eps);
121 
122  /// get precision value used for calculating the derivative step-size
123  static double GetDerivPrecision();
124 
125 private:
126 
127 
128  /// evaluate function passing coordinates x and vector of parameters
129  double DoEvalPar (double x, const double * p ) const {
130  fX[0] = x;
131  if (fFunc->GetMethodCall() ) fFunc->InitArgs(fX,p); // needed for interpreted functions
132  return fFunc->EvalPar(fX,p);
133  }
134 
135  /// evaluate function using the cached parameter values (of TF1)
136  /// re-implement for better efficiency
137  double DoEval (double x) const {
138  // no need to call InitArg for interpreted functions (done in ctor)
139  // use EvalPar since it is much more efficient than Eval
140  fX[0] = x;
141  //const double * p = (fParams.size() > 0) ? &fParams.front() : 0;
142  return fFunc->EvalPar(fX, 0 );
143  }
144 
145  /// return the function derivatives w.r.t. x
146  double DoDerivative( double x ) const;
147 
148  /// evaluate the derivative of the function with respect to the parameters
149  double DoParameterDerivative(double x, const double * p, unsigned int ipar ) const;
150 
151  bool fLinear; // flag for linear functions
152  bool fPolynomial; // flag for polynomial functions
153  TF1 * fFunc; // pointer to ROOT function
154  mutable double fX[1]; //! cached vector for x value (needed for TF1::EvalPar signature)
155  //std::vector<double> fParams; // cached vector with parameter values
156 
157  static double fgEps; // epsilon used in derivative calculation h ~ eps |x|
158 };
159 
160  } // end namespace Fit
161 
162 } // end namespace ROOT
163 
164 
165 #endif /* ROOT_Fit_WrappedTF1 */
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:439
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
double DoParameterDerivative(double x, const double *p, unsigned int ipar) const
evaluate the derivative of the function with respect to the parameters
Definition: WrappedTF1.cxx:105
static double GetDerivPrecision()
get precision value used for calculating the derivative step-size
Definition: WrappedTF1.cxx:131
TMethodCall * GetMethodCall() const
Definition: TF1.h:353
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
Definition: WrappedTF1.h:41
static void SetDerivPrecision(double eps)
precision value used for calculating the derivative step-size h = eps * |x|.
Definition: WrappedTF1.cxx:129
double DoDerivative(double x) const
return the function derivatives w.r.t. x
Definition: WrappedTF1.cxx:97
virtual ~WrappedTF1()
Destructor (no operations).
Definition: WrappedTF1.h:58
Specialized Gradient interface(abstract class) for one dimensional functions It provides a method to ...
Definition: IFunction.h:247
void SetParameters(const double *p)
set parameter values
Definition: WrappedTF1.h:90
Double_t x[n]
Definition: legend1.C:17
ROOT::Math::IGradientOneDim IGrad
Definition: WrappedTF1.h:45
void ParameterGradient(double x, const double *par, double *grad) const
evaluate the derivative of the function with respect to the parameters
Definition: WrappedTF1.cxx:82
virtual const char * GetParName(Int_t ipar) const
Definition: TF1.h:370
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
double DoEval(double x) const
evaluate function using the cached parameter values (of TF1) re-implement for better efficiency ...
Definition: WrappedTF1.h:137
double f(double x)
ROOT::Math::IGenFunction * Clone() const
Clone the wrapper but not the original function.
Definition: WrappedTF1.h:75
unsigned int NPar() const
return number of parameters
Definition: WrappedTF1.h:96
double DoEvalPar(double x, const double *p) const
evaluate function passing coordinates x and vector of parameters
Definition: WrappedTF1.h:129
WrappedTF1(TF1 &f)
constructor from a TF1 function pointer.
Definition: WrappedTF1.cxx:29
WrappedTF1 & operator=(const WrappedTF1 &rhs)
Assignment operator.
Definition: WrappedTF1.cxx:71
std::string ParameterName(unsigned int i) const
return parameter name (this is stored in TF1)
Definition: WrappedTF1.h:102
Namespace for new Math classes and functions.
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition: TF1.cxx:2234
virtual Int_t GetNpar() const
Definition: TF1.h:349
ROOT::Math::IParamGradFunction BaseGradFunc
Definition: WrappedTF1.h:46
1-Dim function class
Definition: TF1.h:149
ROOT::Math::IParamGradFunction::BaseFunc BaseFunc
Definition: WrappedTF1.h:47
virtual Double_t * GetParameters() const
Definition: TF1.h:365
const double * Parameters() const
get the parameter values (return values cachen inside, those inside TF1 might be different) ...
Definition: WrappedTF1.h:83
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=0)
Evaluate function with given coordinates and parameters.
Definition: TF1.cxx:1225
void FdF(double x, double &f, double &deriv) const
calculate function and derivative at same time (required by IGradient interface)
Definition: WrappedTF1.h:113
static double fgEps
cached vector for x value (needed for TF1::EvalPar signature)
Definition: WrappedTF1.h:157