84 std::string algoname(
type);
85 std::transform(algoname.begin(), algoname.end(), algoname.begin(), (
int(*)(
int)) tolower );
119 if (
this == &rhs)
return *
this;
148 if (
gROOT->GetListOfSpecials()->FindObject(
fgMinuit) ==
nullptr) {
172 else if (
fgMinuit->GetNumPars() !=
int(dim) ) {
200 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
230 fMinuit->mnexcm(
"SET GRAD",arglist,1,ierr);
233 fMinuit->mnexcm(
"SET NOGrad",arglist,0,ierr);
249 assert(gFunc !=
nullptr);
250 f = gFunc->operator()(
x);
268 int iret =
fMinuit->DefineParameter(ivar ,
name.c_str(), val, step, 0., 0. );
284 int iret =
fMinuit->DefineParameter(ivar,
name.c_str(), val, step, lower, upper );
291 Warning(
"TMinuitMinimizer::SetLowerLimitedVariable",
"not implemented - use as upper limit 1.E7 instead of +inf");
298 Warning(
"TMinuitMinimizer::SetUpperLimitedVariable",
"not implemented - use as lower limit -1.E7 instead of -inf");
306 Error(
"TMinuitMinimizer::CheckMinuitInstance",
"Invalid TMinuit pointer. Need to call first SetFunction");
314 if ((
int) ivar >=
fMinuit->fNu ) {
315 Error(
"TMinuitMinimizer::CheckVarIndex",
"Invalid parameter index");
335 double step = ( val != 0) ? 0.1 * std::abs(val) : 0.1;
336 int iret =
fMinuit->DefineParameter(ivar,
name.c_str(), val, step, 0., 0. );
337 if (iret == 0) iret =
fMinuit->FixParameter(ivar);
351 fMinuit->mnexcm(
"SET PAR",arglist,2,ierr);
361 double curval,err, lowlim, uplim;
364 fMinuit->mnpout(ivar,
name, curval, err, lowlim, uplim,iuint);
365 if (iuint == -1)
return false;
366 int iret =
fMinuit->DefineParameter(ivar,
name, curval, step, lowlim, uplim );
374 Warning(
"TMinuitMinimizer::SetVariableLowerLimit",
"not implemented - use as upper limit 1.E30 instead of +inf");
380 Warning(
"TMinuitMinimizer::SetVariableUpperLimit",
"not implemented - - use as lower limit -1.E30 instead of +inf");
391 double curval,err, lowlim, uplim;
394 fMinuit->mnpout(ivar,
name, curval, err, lowlim, uplim,iuint);
395 if (iuint == -1)
return false;
396 int iret =
fMinuit->DefineParameter(ivar,
name, curval, err, lower, upper );
405 int iret =
fMinuit->FixParameter(ivar);
413 int iret =
fMinuit->Release(ivar);
421 return (
fMinuit->fNiofex[ivar] == 0 );
428 double curval,err, lowlim, uplim;
431 fMinuit->mnpout(ivar,
name, curval, err, lowlim, uplim,iuint);
432 if (iuint == -1)
return false;
433 var.
Set(
name.Data(), curval, err, lowlim, uplim);
444 return fMinuit->fCpnam[ivar].Data();
449 Error(
"TMinuitMinimizer::VariableIndex",
" find index of a variable from its name is not implemented in TMinuit");
463 Error(
"TMinuitMinimizer::Minimize",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
470 Error(
"TMinuitMinimizer::Minimize",
"The total number of defined parameters is different than the function dimension, npar = %d, dim = %d",
fMinuit->fNu,
fDim);
481 if (printlevel > 0)
Info(
"TMinuitMinimizer::Minimize",
"There are no free parameter - just compute the function value");
492 fMinuit->mnexcm(
"SET Err",arglist,1,ierr);
494 arglist[0] = printlevel - 1;
495 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
498 if (printlevel == 0)
fMinuit->mnexcm(
"SET NOW",arglist,0,ierr);
503 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
508 if (strategy >=0 && strategy <=2 ) {
509 arglist[0] = strategy;
510 fMinuit->mnexcm(
"SET STR",arglist,1,ierr);
521 fMinuit->mnexcm(
"MIGRAD",arglist,nargs,ierr);
525 fMinuit->mnexcm(
"MINIMIZE",arglist,nargs,ierr);
529 fMinuit->mnexcm(
"SIMPLEX",arglist,nargs,ierr);
534 fMinuit->mnexcm(
"SCAN",arglist,nargs,ierr);
540 if (arglist[1] >= 1.) nargs = 2;
541 fMinuit->mnexcm(
"SEEK",arglist,nargs,ierr);
545 fMinuit->mnexcm(
"MIGRAD",arglist,nargs,ierr);
553 int minErrStatus = ierr;
555 if (printlevel>2)
Info(
"TMinuitMinimizer::Minimize",
"Finished to run MIGRAD - status %d",ierr);
559 fMinuit->mnexcm(
"IMPROVE",arglist,1,ierr);
561 if (printlevel>2)
Info(
"TMinuitMinimizer::Minimize",
"Finished to run IMPROVE - status %d",ierr);
571 fMinuit->mnexcm(
"HESSE",arglist,1,ierr);
578 if (printlevel>2)
Info(
"TMinuitMinimizer::Minimize",
"Finished to run HESSE - status %d",ierr);
584 if (minErrStatus == 0) {
609 for (
unsigned int i = 0;
i <
fDim; ++
i) {
620 unsigned int nfree =
NFree();
629 std::vector<double> tmpMat(nfree*nfree);
630 fMinuit->mnemat(&tmpMat.front(), nfree);
633 for (
unsigned int i = 0;
i <
fDim; ++
i) {
637 for (
unsigned int j = 0; j <=
i; ++j) {
638 if (
fMinuit->fNiofex[j] > 0 ) {
653 if (
fMinuit ==
nullptr)
return 0;
662 double minval =
fMinuit->fAmin;
663 if (minval ==
fMinuit->fUndefi)
return 0;
679 if (
fMinuit->fNpar < 0)
return 0;
687 Error(
"TMinuitMinimizer::GetHessianMatrix",
"Hessian matrix has not been computed - status %d",covStatus);
701 Error(
"TMinuitMinimizer::GetHessianMatrix",
"Hessian matrix has not been computed - status %d",covStatus);
705 unsigned int nfree =
NFree();
713 for (
unsigned int i = 0;
i <
fDim; ++
i) {
716 for (
unsigned int j = 0; j <=
i; ++j) {
717 if (
fMinuit->fNiofex[j] > 0 ) {
750 if (!
fMinuit->fGlobcc)
return 0;
751 if (
int(
i) >=
fMinuit->fNu)
return 0;
755 if (iin < 1)
return 0;
756 return fMinuit->fGlobcc[iin-1];
763 Error(
"TMinuitMinimizer::GetMinosError",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
769 if (
PrintLevel() > 0)
Info(
"TMinuitMinimizer::GetMinosError",
"Parameter %s is fixed. There are no Minos error to calculate. Ignored.",
VariableName(
i).c_str());
770 errLow = 0; errUp = 0;
781 fMinuit->mnexcm(
"SET Err",arglist,1,ierr);
786 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
792 fMinuit->mnexcm(
"SET STR",arglist,1,ierr);
797 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
807 fMinuit->mnexcm(
"MINOS",arglist,nargs,ierr);
808 bool isValid = (ierr == 0);
810 if (isValid &&
fMinuit->fCstatu !=
"SUCCESSFUL") {
811 if (
fMinuit->fCstatu ==
"FAILURE" ) {
816 if (
fMinuit->fCstatu ==
"PROBLEMS") ierr = 6;
826 if (
fMinuit->fCstatu ==
"SUCCESSFUL")
832 fMinuit->mnerrs(
i,errUp,errLow, errParab, gcor);
858 if (
fMinuit ==
nullptr)
return;
859 if (
fMinuit->GetNumFixedPars() == 0)
return;
861 if (
int(ivar) >=
fMinuit->GetNumPars() )
return;
865 if (
fMinuit->fIpfix[
i] == ivar+1 ) {
877 if (
fMinuit ==
nullptr)
return;
891 fMinuit->mnexcm(
"SET NOW",&arglist,0,ierr);
893 fMinuit->mnexcm(
"SET WAR",&arglist,0,ierr);
901 Error(
"TMinuitMinimizer::Contour",
" invalid TMinuit instance");
909 fMinuit->mnexcm(
"SET Err",arglist,1,ierr);
912 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
920 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
925 Error(
"TMinuitMinimizer::Contour",
"Cannot make contour with so few points");
930 fMinuit->mncont( ipar,jpar,npoints,
x,
y,npfound);
933 Error(
"TMinuitMinimizer::Contour",
"Cannot find more than 4 points");
936 if (npfound!=(
int)npoints) {
938 Warning(
"TMinuitMinimizer::Contour",
"Returning only %d points ",npfound);
955 Error(
"TMinuitMinimizer::Scan",
" invalid TMinuit instance");
960 if (
xmin >=
xmax && (
int) ipar < fMinuit->GetNumPars() ) {
961 double val = 0;
double err = 0;
963 double xlow = 0;
double xup = 0 ;
966 fMinuit->mnpout( ipar,
name, val, err, xlow, xup, iuint);
968 if (iuint > 0 && err > 0) {
970 xmax = val + 2 * err;
978 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
985 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
988 if (nstep == 0)
return false;
997 fMinuit->mnexcm(
"SCAN",arglist,nargs,ierr);
999 Error(
"TMinuitMinimizer::Scan",
" Error executing command SCAN");
1005 Error(
"TMinuitMinimizer::Scan",
" Error in returned graph object");
1008 nstep = std::min(
gr->GetN(), (
int) nstep);
1011 std::copy(
gr->GetX(),
gr->GetX()+nstep,
x);
1012 std::copy(
gr->GetY(),
gr->GetY()+nstep,
y);
1021 Error(
"TMinuitMinimizer::Hesse",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
1031 fMinuit->mnexcm(
"SET ERR",arglist,1,ierr);
1034 arglist[0] = printlevel - 1;
1035 fMinuit->mnexcm(
"SET PRINT",arglist,1,ierr);
1038 if (printlevel == 0)
fMinuit->mnexcm(
"SET NOW",arglist,0,ierr);
1043 fMinuit->mnexcm(
"SET EPS",arglist,1,ierr);
1048 fMinuit->mnexcm(
"HESSE",arglist,1,ierr);
1051 if (ierr != 0)
return false;
1066 Error(
"TMinuitMinimizer::SetDebug",
"invalid TMinuit pointer. Need to call first SetFunction and SetVariable");
1073 fMinuit->mnexcm(
"SET DEBUG",arglist,1,ierr);
1075 fMinuit->mnexcm(
"SET NODEBUG",arglist,1,ierr);
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
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
TMatrixTSym< Double_t > TMatrixDSym
static ROOT::Math::IMultiGenFunction *& GetGlobalFuncPtr()
R__EXTERN TMinuit * gMinuit
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 unsigned int NDim() const =0
Retrieve the dimension of the function.
virtual void Gradient(const T *x, T *grad) const
Evaluate all the vector of function derivatives (gradient) at a point x.
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 ( a value <=0 corresponds to the l...
int fStatus
status of minimizer
Minimizer()
Default constructor.
int Strategy() const
strategy
double ErrorDef() const
return the statistical scale used for calculate the error is typically 1 for Chi2 and 0....
bool IsValidError() const
return true if Minimizer has performed a detailed error validation (e.g. run Hesse for Minuit)
int PrintLevel() const
minimizer configuration parameters
A TGraph is an object made of two arrays X and Y with npoints each.
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
TMinuitMinimizer class: ROOT::Math::Minimizer implementation based on TMinuit.
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
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
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const ...
unsigned int NCalls() const override
number of function calls to reach the minimum
TMinuitMinimizer & operator=(const TMinuitMinimizer &rhs)
Assignment operator.
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)
double GlobalCC(unsigned int) const override
global correlation coefficient for variable i
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