20#include "gsl/gsl_errno.h"
58 virtual double DataElement(
const double *
x,
unsigned i,
double *
g =
nullptr,
double * =
nullptr,
bool =
false)
const {
60 const double * xExt =
fTransform->Transformation(
x);
61 if (
g == 0)
return fFunc.DataElement( xExt, i );
63 double val =
fFunc.DataElement( xExt, i, &
fGrad[0]);
76 virtual unsigned int NDim()
const {
80 unsigned int NTot()
const {
105 virtual double DoEval(
const double *
x)
const {
111 throw std::runtime_error(
"FitTransformFunction::DoDerivative");
169 unsigned int NDim()
const override {
return fChi2->NDim(); }
171 void Gradient(
const double *
x,
double *
g)
const override {
176 void FdF (
const double *
x,
double &
f,
double *
g)
const override {
189 double DoEval (
const double *
x)
const override {
195 throw std::runtime_error(
"LSRESidualFunc::DoDerivative");
209 const gsl_multifit_fdfsolver_type * gsl_type = 0;
210 if (
type == 1) gsl_type = gsl_multifit_fdfsolver_lmsder;
211 if (
type == 2) gsl_type = gsl_multifit_fdfsolver_lmder;
219 if (niter <= 0) niter = 100;
256 MATH_ERROR_MSG(
"GSLNLSMinimizer::Minimize",
"Function has not been set");
262 if (fitFunc ==
nullptr && fitGradFunc ==
nullptr) {
263 if (
PrintLevel() > 0) std::cout <<
"GSLNLSMinimizer: Invalid function set - only FitMethodFunction types are supported" << std::endl;
268 return DoMinimize<ROOT::Math::FitMethodGradFunction>(*fitGradFunc);
270 return DoMinimize<ROOT::Math::FitMethodFunction>(*fitFunc);
276 unsigned int size = fitFunc.NPoints();
279 std::vector<LSResidualFunc<Func>> residualFuncs;
280 residualFuncs.reserve(
size);
287 unsigned int npar =
NPar();
288 unsigned int ndim =
NDim();
289 if (npar == 0 || npar < ndim) {
290 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Wrong number of parameters",npar);
295 std::vector<double> startValues;
298 std::unique_ptr<MultiNumGradFunction> gradFunction;
299 std::unique_ptr<MinimTransformFunction> trFuncRaw;
301 gradFunction = std::make_unique<MultiNumGradFunction>(fitFunc);
309 std::unique_ptr<FitTransformFunction<Func>> trFunc;
312 trFunc = std::make_unique<FitTransformFunction<Func>>(fitFunc, std::move(trFuncRaw));
313 assert(npar == trFunc->NTot() );
314 for (
unsigned int ires = 0; ires <
size; ++ires) {
318 for (
unsigned int ires = 0; ires <
size; ++ires) {
323 if (debugLevel >=1 ) std::cout <<
"Minimize using GSLNLSMinimizer " << std::endl;
329 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Error setting the residual functions ",iret);
333 if (debugLevel >=1 ) std::cout <<
"GSLNLSMinimizer: " <<
fGSLMultiFit->
Name() <<
" - start iterating......... " << std::endl;
336 unsigned int iter = 0;
338 bool minFound =
false;
342 if (debugLevel >=1) {
343 std::cout <<
"----------> Iteration " << iter <<
" / " <<
MaxIterations() <<
" status " << gsl_strerror(status) << std::endl;
345 if (trFunc)
x = trFunc->Transformation(
x);
346 int pr = std::cout.precision(18);
347 std::cout <<
" FVAL = " << (fitFunc)(
x) << std::endl;
348 std::cout.precision(pr);
349 std::cout <<
" X Values : ";
350 for (
unsigned int i = 0; i <
NDim(); ++i)
352 std::cout << std::endl;
375 if (debugLevel >=1) {
376 std::cout <<
" after Gradient and Delta tests: " << gsl_strerror(status);
377 if (
fEdm > 0) std::cout <<
", edm is: " <<
fEdm;
378 std::cout << std::endl;
394 if (
x == 0)
return false;
397 if (trFunc)
x = trFunc->Transformation(
x);
418 for (
unsigned int i = 0; i < ndim; ++i)
424 if (debugLevel >=1 ) {
425 std::cout <<
"GSLNLSMinimizer: Minimum Found" << std::endl;
426 int pr = std::cout.precision(18);
427 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
428 std::cout <<
"Edm = " <<
fEdm << std::endl;
429 std::cout.precision(pr);
430 std::cout <<
"NIterations = " << iter << std::endl;
431 std::cout <<
"NFuncCalls = " << fitFunc.NCalls() << std::endl;
432 for (
unsigned int i = 0; i <
NDim(); ++i)
433 std::cout << std::setw(12) <<
VariableName(i) <<
" = " << std::setw(12) <<
X()[i] <<
" +/- " << std::setw(12) <<
fErrors[i] << std::endl;
439 if (debugLevel >=0 ) {
440 std::cout <<
"GSLNLSMinimizer: Minimization did not converge: " << std::endl;
441 if (status == GSL_ENOPROG)
442 std::cout <<
"\t iteration is not making progress towards solution" << std::endl;
444 std::cout <<
"\t failed with status " << status << std::endl;
446 if (debugLevel >=1 ) {
447 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
449 std::cout <<
"Niterations = " << iter << std::endl;
464 unsigned int ndim =
NDim();
466 if (i > ndim || j > ndim)
return 0;
#define MATH_ERROR_MSGVAL(loc, txt, x)
#define MATH_ERROR_MSG(loc, str)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
FitMethodFunction class Interface for objective functions (like chi2 and likelihood used in the fit) ...
virtual unsigned int NPar() const
total number of parameter defined
unsigned int NDim() const override
number of dimensions
void SetMinValue(double val)
void SetFinalValues(const double *x, const MinimTransformFunction *func=nullptr)
double MinValue() const override
return minimum function value
MinimTransformFunction * CreateTransformation(std::vector< double > &startValues, const ROOT::Math::IMultiGradFunction *func=nullptr)
void SetFunction(const ROOT::Math::IMultiGenFunction &func) override
set the function to minimize
const ROOT::Math::IMultiGenFunction * ObjFunction() const
return pointer to used objective function
const double * X() const override
return pointer to X values at the minimum
std::string VariableName(unsigned int ivar) const override
get name of variables (override if minimizer support storing of variable names)
GSLMultiFit, internal class for implementing GSL non linear least square GSL fitting.
int TestGradient(double absTol) const
test gradient (ask from solver gradient vector)
int TestDelta(double absTol, double relTol) const
test using abs and relative tolerance |dx| < absTol + relTol*|x| for every component
const double * Gradient() const
gradient value at the minimum
const double * CovarMatrix() const
return covariance matrix of the parameters
const double * X() const
parameter values at the minimum
int Set(const std::vector< Func > &funcVec, const double *x)
set the solver parameters
double CovMatrix(unsigned int, unsigned int) const override
return covariance matrices elements if the variable is fixed the matrix is zero The ordering of the v...
int CovMatrixStatus() const override
return covariance matrix status
void SetFunction(const ROOT::Math::IMultiGenFunction &func) override
set the function to minimize
std::vector< double > fErrors
std::vector< double > fCovMatrix
~GSLNLSMinimizer() override
Destructor (no operations)
bool DoMinimize(const Func &f)
Internal method to perform minimization template on the type of method function.
bool Minimize() override
method to perform the minimization
GSLNLSMinimizer(int type=0)
Default constructor.
const double * MinGradient() const override
return pointer to gradient values at the minimum
ROOT::Math::GSLMultiFit * fGSLMultiFit
Documentation for the abstract class IBaseFunctionMultiDim.
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
LSResidualFunc class description.
double DoEval(const double *x) const override
LSResidualFunc< Func > & operator=(const LSResidualFunc< Func > &rhs)
void FdF(const double *x, double &f, double *g) const override
LSResidualFunc(const LSResidualFunc< Func > &rhs)
void Gradient(const double *x, double *g) const override
double DoDerivative(const double *, unsigned int) const override
LSResidualFunc(const Func &func, unsigned int i)
IMultiGenFunction * Clone() const override
Clone a function.
unsigned int NDim() const override
Retrieve the dimension of the function.
static int DefaultPrintLevel()
static double DefaultTolerance()
static int DefaultMaxIterations()
double Tolerance() const
absolute tolerance
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
int fStatus
status of minimizer
unsigned int MaxIterations() const
max iterations
void SetPrintLevel(int level)
set print level
int PrintLevel() const
minimizer configuration parameters
Namespace for new Math classes and functions.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.