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