Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
23namespace 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 */
40
41 public:
42
46
47
48 /**
49 constructor from a TF1 function pointer.
50 */
52
53 /**
54 Destructor (no operations). TF1 Function pointer is not owned
55 */
56 ~WrappedTF1() override {}
57
58 /**
59 Copy constructor
60 */
61 WrappedTF1(const WrappedTF1 &rhs);
62
63 /**
64 Assignment operator
65 */
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 override
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) override
91 {
92 //std::copy(p,p+fParams.size(),fParams.begin());
94 }
95
96 /// return number of parameters
97 unsigned int NPar() const override
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 override
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 override;
114
115 /// calculate function and derivative at same time (required by IGradient interface)
116 void FdF(double x, double &f, double &deriv) const override
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 override
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 override
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, nullptr);
149 }
150
151 /// return the function derivatives w.r.t. x
152 double DoDerivative(double x) const override;
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 override;
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 */
#define f(i)
Definition RSha256.hxx:104
winID h TVirtualViewer3D TVirtualGLPainter p
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Interface (abstract class) for one-dimensional functions providing a gradient calculation.
Definition IFunction.h:254
Interface (abstract class) for parametric one-dimensional gradient functions providing in addition to...
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
Definition WrappedTF1.h:39
double DoDerivative(double x) const override
return the function derivatives w.r.t. x
double DoEvalPar(double x, const double *p) const override
evaluate function passing coordinates x and vector of parameters
Definition WrappedTF1.h:133
double DoEval(double x) const override
evaluate function using the cached parameter values (of TF1) re-implement for better efficiency
Definition WrappedTF1.h:142
std::string ParameterName(unsigned int i) const override
return parameter name (this is stored in TF1)
Definition WrappedTF1.h:104
WrappedTF1 & operator=(const WrappedTF1 &rhs)
Assignment operator.
const double * Parameters() const override
get the parameter values (return values cachen inside, those inside TF1 might be different)
Definition WrappedTF1.h:82
double DoParameterDerivative(double x, const double *p, unsigned int ipar) const override
evaluate the derivative of the function with respect to the parameters
void SetParameters(const double *p) override
set parameter values need to call also SetParameters in TF1 in ace some other operations (re-normaliz...
Definition WrappedTF1.h:90
ROOT::Math::IGenFunction * Clone() const override
Clone the wrapper but not the original function.
Definition WrappedTF1.h:73
unsigned int NPar() const override
return number of parameters
Definition WrappedTF1.h:97
void FdF(double x, double &f, double &deriv) const override
calculate function and derivative at same time (required by IGradient interface)
Definition WrappedTF1.h:116
static void SetDerivPrecision(double eps)
precision value used for calculating the derivative step-size h = eps * |x|.
static double GetDerivPrecision()
get precision value used for calculating the derivative step-size
ROOT::Math::IParamGradFunction BaseGradFunc
Definition WrappedTF1.h:44
ROOT::Math::IGradientFunctionOneDim IGrad
Definition WrappedTF1.h:43
void ParameterGradient(double x, const double *par, double *grad) const override
evaluate the derivative of the function with respect to the parameters
~WrappedTF1() override
Destructor (no operations).
Definition WrappedTF1.h:56
ROOT::Math::IParamGradFunction::BaseFunc BaseFunc
Definition WrappedTF1.h:45
1-Dim function class
Definition TF1.h:214
virtual Int_t GetNpar() const
Definition TF1.h:487
virtual Double_t * GetParameters() const
Definition TF1.h:526
virtual void InitArgs(const Double_t *x, const Double_t *params)
Initialize parameters addresses.
Definition TF1.cxx:2482
TMethodCall * GetMethodCall() const
Definition TF1.h:500
virtual const char * GetParName(Int_t ipar) const
Definition TF1.h:535
virtual Double_t EvalPar(const Double_t *x, const Double_t *params=nullptr)
Evaluate function with given coordinates and parameters.
Definition TF1.cxx:1470
virtual void SetParameters(const Double_t *params)
Definition TF1.h:650
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.