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 ==
nullptr)
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 =
nullptr;
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;
250 MATH_ERROR_MSG(
"GSLNLSMinimizer::Minimize",
"Function has not been set");
256 if (fitFunc ==
nullptr && fitGradFunc ==
nullptr) {
257 if (
PrintLevel() > 0) std::cout <<
"GSLNLSMinimizer: Invalid function set - only FitMethodFunction types are supported" << std::endl;
262 return DoMinimize<ROOT::Math::FitMethodGradFunction>(*fitGradFunc);
264 return DoMinimize<ROOT::Math::FitMethodFunction>(*fitFunc);
270 unsigned int size = fitFunc.NPoints();
273 std::vector<LSResidualFunc<Func>> residualFuncs;
274 residualFuncs.reserve(
size);
281 unsigned int npar =
NPar();
282 unsigned int ndim =
NDim();
283 if (npar == 0 || npar < ndim) {
284 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Wrong number of parameters",npar);
289 std::vector<double> startValues;
292 std::unique_ptr<MultiNumGradFunction> gradFunction;
293 std::unique_ptr<MinimTransformFunction> trFuncRaw;
295 gradFunction = std::make_unique<MultiNumGradFunction>(fitFunc);
303 std::unique_ptr<FitTransformFunction<Func>> trFunc;
306 trFunc = std::make_unique<FitTransformFunction<Func>>(fitFunc, std::move(trFuncRaw));
307 assert(npar == trFunc->NTot() );
308 for (
unsigned int ires = 0; ires <
size; ++ires) {
312 for (
unsigned int ires = 0; ires <
size; ++ires) {
317 if (debugLevel >=1 ) std::cout <<
"Minimize using GSLNLSMinimizer " << std::endl;
323 MATH_ERROR_MSGVAL(
"GSLNLSMinimizer::Minimize",
"Error setting the residual functions ",iret);
327 if (debugLevel >=1 ) std::cout <<
"GSLNLSMinimizer: " <<
fGSLMultiFit->
Name() <<
" - start iterating......... " << std::endl;
330 unsigned int iter = 0;
332 bool minFound =
false;
336 if (debugLevel >=1) {
337 std::cout <<
"----------> Iteration " << iter <<
" / " <<
MaxIterations() <<
" status " << gsl_strerror(status) << std::endl;
339 if (trFunc)
x = trFunc->Transformation(
x);
340 int pr = std::cout.precision(18);
341 std::cout <<
" FVAL = " << (fitFunc)(
x) << std::endl;
342 std::cout.precision(pr);
343 std::cout <<
" X Values : ";
344 for (
unsigned int i = 0; i <
NDim(); ++i)
346 std::cout << std::endl;
369 if (debugLevel >=1) {
370 std::cout <<
" after Gradient and Delta tests: " << gsl_strerror(status);
371 if (
fEdm > 0) std::cout <<
", edm is: " <<
fEdm;
372 std::cout << std::endl;
388 if (
x ==
nullptr)
return false;
391 if (trFunc)
x = trFunc->Transformation(
x);
412 for (
unsigned int i = 0; i < ndim; ++i)
418 if (debugLevel >=1 ) {
419 std::cout <<
"GSLNLSMinimizer: Minimum Found" << std::endl;
420 int pr = std::cout.precision(18);
421 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
422 std::cout <<
"Edm = " <<
fEdm << std::endl;
423 std::cout.precision(pr);
424 std::cout <<
"NIterations = " << iter << std::endl;
425 std::cout <<
"NFuncCalls = " << fitFunc.NCalls() << std::endl;
426 for (
unsigned int i = 0; i <
NDim(); ++i)
427 std::cout << std::setw(12) <<
VariableName(i) <<
" = " << std::setw(12) <<
X()[i] <<
" +/- " << std::setw(12) <<
fErrors[i] << std::endl;
433 if (debugLevel >=0 ) {
434 std::cout <<
"GSLNLSMinimizer: Minimization did not converge: " << std::endl;
435 if (status == GSL_ENOPROG)
436 std::cout <<
"\t iteration is not making progress towards solution" << std::endl;
438 std::cout <<
"\t failed with status " << status << std::endl;
440 if (debugLevel >=1 ) {
441 std::cout <<
"FVAL = " <<
MinValue() << std::endl;
443 std::cout <<
"Niterations = " << iter << std::endl;
458 unsigned int ndim =
NDim();
460 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.
virtual bool HasGradient() const
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...