ROOT  6.06/09
Reference Guide
WrappedMultiTF1.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 WrappedTFunction
12 
13 #ifndef ROOT_Math_WrappedMultiTF1
14 #define ROOT_Math_WrappedMultiTF1
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 IParamMultiFunction interface
32  of multi-dimensions to be used in the ROOT::Math numerical algorithm
33  This wrapper class does not own the TF1 pointer, so it assumes it exists during the wrapper lifetime.
34  The class copy the TF1 pointer only when it owns it
35 
36  The class from ROOT version 6.03 does not contain anymore a copy of the parameters. The parameters are
37  stored in the TF1 class.
38 
39  @ingroup CppFunctions
40 */
41 
42 //LM note: are there any issues when cloning the class for the parameters that are not copied anymore ??
43 
45 
46 public:
47 
50 
51 
52  /**
53  constructor from a function pointer to a TF1
54  If dim = 0 dimension is taken from TF1::GetNdim().
55  IN case of multi-dimensional function created using directly TF1 object the dimension
56  returned by TF1::GetNdim is always 1. The user must then pass the correct value of dim
57  */
58  WrappedMultiTF1 (TF1 & f, unsigned int dim = 0 );
59 
60  /**
61  Destructor (no operations). Function pointer is not owned
62  */
63  virtual ~WrappedMultiTF1 () { if (fOwnFunc && fFunc) delete fFunc; }
64 
65  /**
66  Copy constructor
67  */
68  WrappedMultiTF1(const WrappedMultiTF1 & rhs);
69 
70  /**
71  Assignment operator
72  */
74 
75 
76  /** @name interface inherited from IFunction */
77 
78  /**
79  Clone the wrapper but not the original function
80  */
82  return new WrappedMultiTF1(*this);
83  }
84 
85  /// function dimension
86  unsigned int NDim() const {
87  return fDim;
88  }
89 
90 
91  /** @name interface inherited from IParamFunction */
92 
93  /// get the parameter values (return values from TF1)
94  const double * Parameters() const {
95  //return (fParams.size() > 0) ? &fParams.front() : 0;
96  return fFunc->GetParameters();
97  }
98 
99  /// set parameter values (only the cached one in this class,leave unchanges those of TF1)
100  void SetParameters(const double * p) {
101  //std::copy(p,p+fParams.size(),fParams.begin());
102  fFunc->SetParameters(p);
103  }
104 
105  /// return number of parameters
106  unsigned int NPar() const {
107  // return fParams.size();
108  return fFunc->GetNpar();
109  }
110 
111  /// return parameter name (from TF1)
112  std::string ParameterName(unsigned int i) const {
113  return std::string(fFunc->GetParName(i));
114  }
115 
116 
117  /// evaluate the derivative of the function with respect to the parameters
118  void ParameterGradient(const double * x, const double * par, double * grad ) const;
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  /// method to retrieve the internal function pointer
128  const TF1 * GetFunction() const { return fFunc; }
129 
130  /// method to set a new function pointer and copy it inside.
131  /// By calling this method the class manages now the passed TF1 pointer
132  void SetAndCopyFunction(const TF1 * f = 0);
133 
134 
135 
136 private:
137 
138  /// evaluate function passing coordinates x and vector of parameters
139  double DoEvalPar (const double * x, const double * p ) const {
140  if (fFunc->GetMethodCall() ) fFunc->InitArgs(x,p); // needed for interpreted functions
141  return fFunc->EvalPar(x,p);
142  }
143 
144  /// evaluate function using the cached parameter values (of TF1)
145  /// re-implement for better efficiency
146  double DoEval (const double* x) const {
147  // no need to call InitArg for interpreted functions (done in ctor)
148 
149  //const double * p = (fParams.size() > 0) ? &fParams.front() : 0;
150  return fFunc->EvalPar(x, 0 );
151  }
152 
153 
154  /// evaluate the partial derivative with respect to the parameter
155  double DoParameterDerivative(const double * x, const double * p, unsigned int ipar) const;
156 
157 
158  bool fLinear; // flag for linear functions
159  bool fPolynomial; // flag for polynomial functions
160  bool fOwnFunc; // flag to indicate we own the TF1 function pointer
161  TF1 * fFunc; // pointer to ROOT function
162  unsigned int fDim; // cached value of dimension
163  //std::vector<double> fParams; // cached vector with parameter values
164 
165  static double fgEps; // epsilon used in derivative calculation h ~ eps |p|
166 };
167 
168  } // end namespace Fit
169 
170 } // end namespace ROOT
171 
172 
173 #endif /* ROOT_Fit_WrappedMultiTF1 */
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:439
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
std::string ParameterName(unsigned int i) const
return parameter name (from TF1)
double DoParameterDerivative(const double *x, const double *p, unsigned int ipar) const
evaluate the partial derivative with respect to the parameter
Definition: WrappedTF1.cxx:216
WrappedMultiTF1 & operator=(const WrappedMultiTF1 &rhs)
Assignment operator.
Definition: WrappedTF1.cxx:185
Class to Wrap a ROOT Function class (like TF1) in a IParamMultiFunction interface of multi-dimensions...
WrappedMultiTF1(TF1 &f, unsigned int dim=0)
constructor from a function pointer to a TF1 If dim = 0 dimension is taken from TF1::GetNdim().
Definition: WrappedTF1.cxx:138
virtual const char * GetParName(Int_t ipar) const
Definition: TF1.h:370
virtual ~WrappedMultiTF1()
Destructor (no operations).
double DoEval(const double *x) const
evaluate function using the cached parameter values (of TF1) re-implement for better efficiency ...
TMethodCall * GetMethodCall() const
Definition: TF1.h:353
const double * Parameters() const
get the parameter values (return values from TF1)
ROOT::Math::IParamMultiFunction::BaseFunc BaseFunc
Double_t x[n]
Definition: legend1.C:17
void SetAndCopyFunction(const TF1 *f=0)
method to set a new function pointer and copy it inside.
Definition: WrappedTF1.cxx:242
ROOT::Math::IParamMultiGradFunction BaseParamFunc
static double GetDerivPrecision()
get precision value used for calculating the derivative step-size
Definition: WrappedTF1.cxx:240
IMultiGenFunction * Clone() const
Clone the wrapper but not the original function.
double DoEvalPar(const double *x, const double *p) const
evaluate function passing coordinates x and vector of parameters
void ParameterGradient(const double *x, const double *par, double *grad) const
evaluate the derivative of the function with respect to the parameters
Definition: WrappedTF1.cxx:197
void SetParameters(const double *p)
set parameter values (only the cached one in this class,leave unchanges those of TF1) ...
static void SetDerivPrecision(double eps)
precision value used for calculating the derivative step-size h = eps * |x|.
Definition: WrappedTF1.cxx:238
double f(double x)
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
const TF1 * GetFunction() const
method to retrieve the internal function pointer
Namespace for new Math classes and functions.
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition: TF1.cxx:2221
virtual Double_t * GetParameters() const
Definition: TF1.h:365
1-Dim function class
Definition: TF1.h:149
unsigned int NPar() const
return number of parameters
unsigned int NDim() const
function dimension
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=0)
Evaluate function with given coordinates and parameters.
Definition: TF1.cxx:1215
virtual Int_t GetNpar() const
Definition: TF1.h:349