20 #include "gsl/gsl_errno.h"
43 FitTransformFunction(
const FitMethodFunction &
f,
const std::vector<EMinimVariableType> & types,
const std::vector<double> & values,
44 const std::map<
unsigned int, std::pair<double, double> > & bounds) :
46 fOwnTransformation(true),
48 fTransform(new MinimTransformFunction( new MultiNumGradFunction(f), types, values, bounds) ),
56 FitTransformFunction(
const FitMethodFunction &
f, MinimTransformFunction *transFunc ) :
58 fOwnTransformation(
false),
60 fTransform(transFunc),
66 ~FitTransformFunction() {
67 if (fOwnTransformation) {
74 virtual double DataElement(
const double * x,
unsigned i,
double *
g = 0)
const {
76 const double * xExt = fTransform->Transformation(x);
77 if (
g == 0)
return fFunc.DataElement( xExt, i );
79 double val = fFunc.DataElement( xExt, i, &fGrad[0]);
81 fTransform->GradientTransformation( x, &fGrad.front(),
g);
92 unsigned int NDim()
const {
93 return fTransform->
NDim();
96 unsigned int NTot()
const {
97 return fTransform->NTot();
101 const double * Transformation(
const double * x)
const {
return fTransform->Transformation(x); }
105 void InvTransformation(
const double * xext,
double * xint)
const { fTransform->InvTransformation(xext,xint); }
108 void InvStepTransformation(
const double * x,
const double * sext,
double * sint)
const { fTransform->InvStepTransformation(x,sext,sint); }
111 void GradientTransformation(
const double * x,
const double *gext,
double * gint)
const { fTransform->GradientTransformation(x,gext,gint); }
113 void MatrixTransformation(
const double * x,
const double *cint,
double * cext)
const { fTransform->MatrixTransformation(x,cint,cext); }
118 FitTransformFunction(
const FitTransformFunction& rhs);
119 FitTransformFunction&
operator=(
const FitTransformFunction& rhs);
121 double DoEval(
const double * x)
const {
122 return fFunc( fTransform->Transformation(x) );
125 bool fOwnTransformation;
127 MinimTransformFunction * fTransform;
128 mutable std::vector<double> fGrad;
143 const gsl_multifit_fdfsolver_type * gsl_type = 0;
144 if (type == 1) gsl_type = gsl_multifit_fdfsolver_lmsder;
145 if (type == 2) gsl_type = gsl_multifit_fdfsolver_lmder;
153 if (niter <= 0) niter = 100;
179 if (
PrintLevel() > 0) std::cout <<
"GSLNLSMinimizer: Invalid function set - only Chi2Func supported" << std::endl;
187 for (
unsigned int i = 0; i <
fSize; ++i) {
197 return SetFunction(static_cast<const ROOT::Math::IMultiGenFunction &>(func) );
208 MATH_ERROR_MSG(
"GSLNLSMinimizer::Minimize",
"Function has not been set");
212 unsigned int npar =
NPar();
213 unsigned int ndim =
NDim();
214 if (npar == 0 || npar < ndim) {
215 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Wrong number of parameters",npar);
220 std::vector<double> startValues;
226 std::auto_ptr<FitTransformFunction> trFunc;
228 trFunc.reset(
new FitTransformFunction(*
fChi2Func, trFuncRaw) );
230 for (
unsigned int ires = 0; ires <
fResiduals.size(); ++ires) {
234 assert(npar == trFunc->NTot() );
237 if (debugLevel >=1 ) std::cout <<
"Minimize using GSLNLSMinimizer " << std::endl;
247 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Error setting the residual functions ",iret);
251 if (debugLevel >=1 ) std::cout <<
"GSLNLSMinimizer: " <<
fGSLMultiFit->
Name() <<
" - start iterating......... " << std::endl;
254 unsigned int iter = 0;
256 bool minFound =
false;
260 if (debugLevel >=1) {
261 std::cout <<
"----------> Iteration " << iter <<
" / " <<
MaxIterations() <<
" status " << gsl_strerror(status) << std::endl;
263 if (trFunc.get()) x = trFunc->Transformation(x);
264 int pr = std::cout.precision(18);
265 std::cout <<
" FVAL = " << (*fChi2Func)(
x) << std::endl;
266 std::cout.precision(pr);
267 std::cout <<
" X Values : ";
268 for (
unsigned int i = 0; i <
NDim(); ++i)
270 std::cout << std::endl;
293 if (debugLevel >=1) {
294 std::cout <<
" after Gradient and Delta tests: " << gsl_strerror(status);
295 if (
fEdm > 0) std::cout <<
", edm is: " <<
fEdm;
296 std::cout << std::endl;
312 if (x == 0)
return false;
331 std::copy(cov, cov + ndim*ndim,
fCovMatrix.begin() );
334 for (
unsigned int i = 0; i < ndim; ++i)
340 if (debugLevel >=1 ) {
341 std::cout <<
"GSLNLSMinimizer: Minimum Found" << std::endl;
342 int pr = std::cout.precision(18);
343 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
344 std::cout <<
"Edm = " <<
fEdm << std::endl;
345 std::cout.precision(pr);
346 std::cout <<
"NIterations = " << iter << std::endl;
348 for (
unsigned int i = 0; i <
NDim(); ++i)
349 std::cout << std::setw(12) <<
VariableName(i) <<
" = " << std::setw(12) <<
X()[i] <<
" +/- " << std::setw(12) <<
fErrors[i] << std::endl;
355 if (debugLevel >=1 ) {
356 std::cout <<
"GSLNLSMinimizer: Minimization did not converge" << std::endl;
357 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
359 std::cout <<
"Niterations = " << iter << std::endl;
374 unsigned int ndim =
NDim();
376 if (i > ndim || j > ndim)
return 0;
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
~GSLNLSMinimizer()
Destructor (no operations)
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
int TestDelta(double absTol, double relTol) const
test using abs and relative tolerance |dx| < absTol + relTol*|x| for every component ...
std::vector< double > fErrors
Namespace for new ROOT classes and functions.
const ROOT::Math::FitMethodFunction * fChi2Func
GSLMultiFit, internal class for implementing GSL non linear least square GSL fitting.
const double * Gradient() const
gradient value at the minimum
unsigned int MaxIterations() const
max iterations
virtual unsigned int NPoints() const
return the number of data points used in evaluating the function
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=0)
ROOT::Math::GSLMultiFit * fGSLMultiFit
double Tolerance() const
absolute tolerance
int Set(const std::vector< Func > &funcVec, const double *x)
set the solver parameters
std::vector< double > fCovMatrix
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
#define MATH_ERROR_MSGVAL(loc, str, x)
const double * CovarMatrix() const
return covariance matrix of the parameters
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
const double * X() const
parameter values at the minimum
std::map< std::string, std::string >::const_iterator iter
void SetMinValue(double val)
virtual int CovMatrixStatus() const
return covariance matrix status
#define MATH_ERROR_MSG(loc, str)
virtual unsigned int NDim() const
number of dimensions
GSLNLSMinimizer(int type=0)
Default constructor.
void SetFinalValues(const double *x)
virtual double MinValue() const
return minimum function value
virtual std::string VariableName(unsigned int ivar) const
get name of variables (override if minimizer support storing of variable names)
virtual const double * X() const
return pointer to X values at the minimum
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
static int DefaultPrintLevel()
int PrintLevel() const
minimizer configuration parameters
virtual bool Minimize()
method to perform the minimization
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
MultiNumGradFunction class to wrap a normal function in a gradient function using numerical gradient ...
virtual unsigned int NPar() const
total number of parameter defined
int TestGradient(double absTol) const
test gradient (ask from solver gradient vector)
static int DefaultMaxIterations()
LSResidualFunc class description.
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
double func(double *x, double *p)
IBaseFunctionMultiDim IMultiGenFunction
virtual double CovMatrix(unsigned int, unsigned int) const
return covariance matrices elements if the variable is fixed the matrix is zero The ordering of the v...
std::vector< LSResidualFunc > fResiduals
Namespace for new Math classes and functions.
Binding & operator=(OUT(*fun)(void))
virtual unsigned int NCalls() const
return the total number of function calls (overrided if needed)
void SetPrintLevel(int level)
set print level
Documentation for the abstract class IBaseFunctionMultiDim.
static double DefaultTolerance()
virtual double DataElement(const double *x, unsigned int i, double *g=0) const =0
method returning the data i-th contribution to the fit objective function For example the residual fo...
BasicFitMethodFunction< ROOT::Math::IMultiGenFunction > FitMethodFunction