83 std::string algoname(type);
84 std::transform(algoname.begin(), algoname.end(), algoname.begin(), (
int(*)(
int)) tolower );
134 if (
gROOT->GetListOfSpecials()->FindObject(
fgMinuit) ==
nullptr) {
158 else if (
fgMinuit->GetNumPars() !=
int(dim) ) {
186 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
216 fMinuit->mnexcm(
"SET GRAD",arglist,1,ierr);
219 fMinuit->mnexcm(
"SET NOGrad",arglist,0,ierr);
235 assert(gFunc !=
nullptr);
236 f = gFunc->operator()(
x);
254 int iret =
fMinuit->DefineParameter(ivar ,
name.c_str(), val, step, 0., 0. );
270 int iret =
fMinuit->DefineParameter(ivar,
name.c_str(), val, step, lower, upper );
277 Warning(
"TMinuitMinimizer::SetLowerLimitedVariable",
"not implemented - use as upper limit 1.E7 instead of +inf");
284 Warning(
"TMinuitMinimizer::SetUpperLimitedVariable",
"not implemented - use as lower limit -1.E7 instead of -inf");
292 Error(
"TMinuitMinimizer::CheckMinuitInstance",
"Invalid TMinuit pointer. Need to call first SetFunction");
300 if ((
int) ivar >=
fMinuit->fNu ) {
301 Error(
"TMinuitMinimizer::CheckVarIndex",
"Invalid parameter index");
321 double step = ( val != 0) ? 0.1 * std::abs(val) : 0.1;
322 int iret =
fMinuit->DefineParameter(ivar,
name.c_str(), val, step, 0., 0. );
323 if (iret == 0) iret =
fMinuit->FixParameter(ivar);
337 fMinuit->mnexcm(
"SET PAR",arglist,2,ierr);
347 double curval,
err, lowlim, uplim;
351 if (iuint == -1)
return false;
352 int iret =
fMinuit->DefineParameter(ivar,
name, curval, step, lowlim, uplim );
360 Warning(
"TMinuitMinimizer::SetVariableLowerLimit",
"not implemented - use as upper limit 1.E30 instead of +inf");
366 Warning(
"TMinuitMinimizer::SetVariableUpperLimit",
"not implemented - - use as lower limit -1.E30 instead of +inf");
377 double curval,
err, lowlim, uplim;
381 if (iuint == -1)
return false;
382 int iret =
fMinuit->DefineParameter(ivar,
name, curval,
err, lower, upper );
391 int iret =
fMinuit->FixParameter(ivar);
399 int iret =
fMinuit->Release(ivar);
407 return (
fMinuit->fNiofex[ivar] == 0 );
414 double curval,
err, lowlim, uplim;
418 if (iuint == -1)
return false;
419 var.
Set(
name.Data(), curval,
err, lowlim, uplim);
430 return fMinuit->fCpnam[ivar].Data();
435 Error(
"TMinuitMinimizer::VariableIndex",
" find index of a variable from its name is not implemented in TMinuit");
449 Error(
"TMinuitMinimizer::Minimize",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
456 Error(
"TMinuitMinimizer::Minimize",
"The total number of defined parameters is different than the function dimension, npar = %d, dim = %d",
fMinuit->fNu,
fDim);
467 if (printlevel > 0)
Info(
"TMinuitMinimizer::Minimize",
"There are no free parameter - just compute the function value");
478 fMinuit->mnexcm(
"SET Err",arglist,1,ierr);
480 arglist[0] = printlevel - 1;
481 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
484 if (printlevel == 0)
fMinuit->mnexcm(
"SET NOW",arglist,0,ierr);
489 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
494 if (strategy >=0 && strategy <=2 ) {
495 arglist[0] = strategy;
496 fMinuit->mnexcm(
"SET STR",arglist,1,ierr);
507 fMinuit->mnexcm(
"MIGRAD",arglist,nargs,ierr);
511 fMinuit->mnexcm(
"MINIMIZE",arglist,nargs,ierr);
515 fMinuit->mnexcm(
"SIMPLEX",arglist,nargs,ierr);
520 fMinuit->mnexcm(
"SCAN",arglist,nargs,ierr);
526 if (arglist[1] >= 1.) nargs = 2;
527 fMinuit->mnexcm(
"SEEK",arglist,nargs,ierr);
531 fMinuit->mnexcm(
"MIGRAD",arglist,nargs,ierr);
539 int minErrStatus = ierr;
541 if (printlevel>2)
Info(
"TMinuitMinimizer::Minimize",
"Finished to run MIGRAD - status %d",ierr);
545 fMinuit->mnexcm(
"IMPROVE",arglist,1,ierr);
547 if (printlevel>2)
Info(
"TMinuitMinimizer::Minimize",
"Finished to run IMPROVE - status %d",ierr);
557 fMinuit->mnexcm(
"HESSE",arglist,1,ierr);
564 if (printlevel>2)
Info(
"TMinuitMinimizer::Minimize",
"Finished to run HESSE - status %d",ierr);
570 if (minErrStatus == 0) {
595 for (
unsigned int i = 0; i <
fDim; ++i) {
606 unsigned int nfree =
NFree();
615 std::vector<double> tmpMat(nfree*nfree);
616 fMinuit->mnemat(&tmpMat.front(), nfree);
619 for (
unsigned int i = 0; i <
fDim; ++i) {
621 if (
fMinuit->fNiofex[i] > 0 ) {
623 for (
unsigned int j = 0; j <= i; ++j) {
624 if (
fMinuit->fNiofex[j] > 0 ) {
639 if (
fMinuit ==
nullptr)
return 0;
648 double minval =
fMinuit->fAmin;
649 if (minval ==
fMinuit->fUndefi)
return 0;
665 if (
fMinuit->fNpar < 0)
return 0;
673 Error(
"TMinuitMinimizer::GetHessianMatrix",
"Hessian matrix has not been computed - status %d",covStatus);
687 Error(
"TMinuitMinimizer::GetHessianMatrix",
"Hessian matrix has not been computed - status %d",covStatus);
691 unsigned int nfree =
NFree();
699 for (
unsigned int i = 0; i <
fDim; ++i) {
700 if (
fMinuit->fNiofex[i] > 0 ) {
702 for (
unsigned int j = 0; j <= i; ++j) {
703 if (
fMinuit->fNiofex[j] > 0 ) {
704 hes[i*
fDim + j] = mat(
l,
m);
735 std::vector<double> out;
742 for (
int i = 0; i <
fMinuit->fNu; ++i) {
750 out[i] =
fMinuit->fGlobcc[iin - 1];
759 Error(
"TMinuitMinimizer::GetMinosError",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
764 if (
fMinuit->fNiofex[i] == 0 ) {
765 if (
PrintLevel() > 0)
Info(
"TMinuitMinimizer::GetMinosError",
"Parameter %s is fixed. There are no Minos error to calculate. Ignored.",
VariableName(i).c_str());
766 errLow = 0; errUp = 0;
777 fMinuit->mnexcm(
"SET Err",arglist,1,ierr);
782 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
788 fMinuit->mnexcm(
"SET STR",arglist,1,ierr);
793 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
803 fMinuit->mnexcm(
"MINOS",arglist,nargs,ierr);
804 bool isValid = (ierr == 0);
806 if (isValid &&
fMinuit->fCstatu !=
"SUCCESSFUL") {
807 if (
fMinuit->fCstatu ==
"FAILURE" ) {
812 if (
fMinuit->fCstatu ==
"PROBLEMS") ierr = 6;
822 if (
fMinuit->fCstatu ==
"SUCCESSFUL")
828 fMinuit->mnerrs(i,errUp,errLow, errParab, gcor);
854 if (
fMinuit ==
nullptr)
return;
855 if (
fMinuit->GetNumFixedPars() == 0)
return;
857 if (
int(ivar) >=
fMinuit->GetNumPars() )
return;
860 for (
int i = 0; i <
fMinuit->fNpfix; ++i) {
861 if (
fMinuit->fIpfix[i] == ivar+1 ) {
873 if (
fMinuit ==
nullptr)
return;
887 fMinuit->mnexcm(
"SET NOW",&arglist,0,ierr);
889 fMinuit->mnexcm(
"SET WAR",&arglist,0,ierr);
897 Error(
"TMinuitMinimizer::Contour",
" invalid TMinuit instance");
905 fMinuit->mnexcm(
"SET Err",arglist,1,ierr);
908 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
916 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
921 Error(
"TMinuitMinimizer::Contour",
"Cannot make contour with so few points");
926 fMinuit->mncont( ipar,jpar,npoints,
x,
y,npfound);
929 Error(
"TMinuitMinimizer::Contour",
"Cannot find more than 4 points");
932 if (npfound!=(
int)npoints) {
934 Warning(
"TMinuitMinimizer::Contour",
"Returning only %d points ",npfound);
951 Error(
"TMinuitMinimizer::Scan",
" invalid TMinuit instance");
956 if (
xmin >=
xmax && (
int) ipar < fMinuit->GetNumPars() ) {
957 double val = 0;
double err = 0;
959 double xlow = 0;
double xup = 0 ;
964 if (iuint > 0 &&
err > 0) {
974 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
981 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
984 if (nstep == 0)
return false;
993 fMinuit->mnexcm(
"SCAN",arglist,nargs,ierr);
995 Error(
"TMinuitMinimizer::Scan",
" Error executing command SCAN");
1001 Error(
"TMinuitMinimizer::Scan",
" Error in returned graph object");
1004 nstep = std::min(
gr->GetN(), (
int) nstep);
1007 std::copy(
gr->GetX(),
gr->GetX()+nstep,
x);
1008 std::copy(
gr->GetY(),
gr->GetY()+nstep,
y);
1017 Error(
"TMinuitMinimizer::Hesse",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
1027 fMinuit->mnexcm(
"SET ERR",arglist,1,ierr);
1030 arglist[0] = printlevel - 1;
1031 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
1034 if (printlevel == 0)
fMinuit->mnexcm(
"SET NOW",arglist,0,ierr);
1039 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
1044 fMinuit->mnexcm(
"HESSE",arglist,1,ierr);
1047 if (ierr != 0)
return false;
1062 Error(
"TMinuitMinimizer::SetDebug",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
1069 fMinuit->mnexcm(
"SET DEBUG",arglist,1,ierr);
1071 fMinuit->mnexcm(
"SET NODEBUG",arglist,1,ierr);
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
TMatrixTSym< Double_t > TMatrixDSym
static ROOT::Math::IMultiGenFunction *& GetGlobalFuncPtr()
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
void Set(const std::string &name, double value, double step)
set value and name (unlimited parameter)
void Fix()
fix the parameter
virtual bool HasGradient() const
virtual void Gradient(const T *x, T *grad) const
Evaluate all the vector of function derivatives (gradient) at a point x.
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
double Tolerance() const
Absolute tolerance.
unsigned int MaxFunctionCalls() const
Max number of function calls.
double Precision() const
Precision of minimizer in the evaluation of the objective function.
int fStatus
status of minimizer
int Strategy() const
Strategy.
bool IsValidError() const
int PrintLevel() const
Set print level.
TMatrixTSym< Element > & Invert(Double_t *det=nullptr)
Invert the matrix and calculate its determinant Notice that the LU decomposition is used instead of B...
const Element * GetMatrixArray() const override
bool FixVariable(unsigned int) override
fix an existing variable
TMinuitMinimizer(ROOT::Minuit::EMinimizerType type=ROOT::Minuit::kMigrad, unsigned int ndim=0)
Default constructor.
void RetrieveErrorMatrix()
retrieve error matrix from TMinuit
bool SetFixedVariable(unsigned int, const std::string &, double) override
set fixed variable (override if minimizer supports them )
static TMinuit * fgMinuit
bool SetVariableLimits(unsigned int ivar, double lower, double upper) override
set the limits of an existing variable
static void Fcn(int &, double *, double &f, double *, int)
implementation of FCN for Minuit
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
bool ReleaseVariable(unsigned int) override
release an existing variable
static void FcnGrad(int &, double *g, double &f, double *, int)
implementation of FCN for Minuit when user provided gradient is used
ROOT::Minuit::EMinimizerType fType
bool SetVariableLowerLimit(unsigned int, double) override
set the lower-limit of an existing variable
int VariableIndex(const std::string &name) const override
get index of variable given a variable given a name return always -1 .
double MinValue() const override
return minimum function value
bool SetVariableStepSize(unsigned int, double) override
set the step size of an existing variable
void RetrieveParams()
retrieve minimum parameters and errors from TMinuit
~TMinuitMinimizer() override
Destructor (no operations).
std::vector< double > fErrors
void SetFunction(const ROOT::Math::IMultiGenFunction &func) override
set the function to minimize
bool CheckMinuitInstance() const
check TMinuit instance
static bool fgUseStaticMinuit
std::vector< double > fCovar
bool Hesse() override
perform a full calculation of the Hessian matrix for error calculation
std::vector< double > GlobalCC() const override
global correlation coefficient for variable i
bool CheckVarIndex(unsigned int ivar) const
check parameter
bool Minimize() override
method to perform the minimization
bool SetVariableValue(unsigned int, double) override
set the value of an existing variable
int CovMatrixStatus() const override
return status of covariance matrix
void SuppressMinuitWarnings(bool nowarn=true)
suppress the minuit warnings (if called with false will enable them) By default they are suppressed o...
bool SetVariableUpperLimit(unsigned int, double) override
set the upper-limit of an existing variable
double Edm() const override
return expected distance reached from the minimum
static bool UseStaticMinuit(bool on=true)
static function to switch on/off usage of static global TMinuit instance (gMinuit) By default it is u...
void PrintResults() override
Print the result according to set level (implemented for TMinuit for maintaining Minuit-style printin...
unsigned int NCalls() const override
number of function calls to reach the minimum
void DoReleaseFixParameter(int ivar)
release a parameter that is fixed when it is redefined
bool SetLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double, double) override
set upper/lower limited variable (override if minimizer supports them )
bool GetMinosError(unsigned int i, double &errLow, double &errUp, int=0) override
minos error for variable i, return false if Minos failed
bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings &) const override
get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
bool IsFixedVariable(unsigned int) const override
query if an existing variable is fixed (i.e.
std::string VariableName(unsigned int ivar) const override
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const;
bool SetUpperLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double upper) override
set upper limit variable (override if minimizer supports them )
bool SetLowerLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double lower) override
set lower limit variable (override if minimizer supports them )
unsigned int NFree() const override
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
std::vector< double > fParams
void InitTMinuit(int ndim)
initialize the TMinuit instance
bool GetHessianMatrix(double *h) const override
Fill the passed array with the Hessian matrix elements The Hessian matrix is the matrix of the second...
bool Scan(unsigned int i, unsigned int &nstep, double *x, double *y, double xmin=0, double xmax=0) override
scan a parameter i around the minimum.
bool SetDebug(bool on=true)
set debug mode. Return true if setting was successful
bool GetCovMatrix(double *cov) const override
Fill the passed array with the covariance matrix elements if the variable is fixed or const the value...
bool Contour(unsigned int i, unsigned int j, unsigned int &npoints, double *xi, double *xj) override
find the contour points (xi,xj) of the function for parameter i and j around the minimum The contour ...
IGradientFunctionMultiDim IMultiGradFunction
IMultiGenFunctionTempl< double > IMultiGenFunction