42 #ifndef __ROOFIT_NOROOMINIMIZER
57 #include "TDirectory.h"
76 #if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
127 _minimizerType =
"Minuit";
129 if (_theFitter)
delete _theFitter ;
132 _theFitter->Config().SetMinimizer(_minimizerType.c_str());
135 _theFitter->Config().MinimizerOptions().SetMaxIterations(500*_fcn->NDim());
136 _theFitter->Config().MinimizerOptions().SetMaxFunctionCalls(500*_fcn->NDim());
142 setErrorLevel(_func->defaultErrorLevel()) ;
145 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
146 _optConst,_verbose) ;
183 _theFitter->Config().MinimizerOptions().SetStrategy(istrat);
195 _theFitter->Config().MinimizerOptions().SetMaxIterations(n);
207 _theFitter->Config().MinimizerOptions().SetMaxFunctionCalls(n);
221 _theFitter->Config().MinimizerOptions().SetErrorDef(level);
232 _theFitter->Config().MinimizerOptions().SetTolerance(eps);
242 _func->enableOffsetting(flag) ;
253 _minimizerType =
type;
295 if (opts.
Contains(
"v")) setVerbose(1) ;
296 if (opts.
Contains(
"t")) setProfile(1) ;
297 if (opts.
Contains(
"l")) setLogFile(
Form(
"%s.log",_func->GetName())) ;
298 if (opts.
Contains(
"c")) optimizeConst(1) ;
301 if (opts.
Contains(
"0")) setStrategy(0) ;
303 if (opts.
Contains(
"0")) setStrategy(1) ;
307 return (opts.
Contains(
"r")) ? save() : 0 ;
317 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
318 _optConst,_verbose) ;
320 _theFitter->Config().SetMinimizer(type,alg);
326 bool ret = _theFitter->FitFCN(*_fcn);
327 _status = ((ret) ? _theFitter->Result().Status() : -1);
331 _fcn->BackProp(_theFitter->Result());
333 saveStatus(
"MINIMIZE",_status) ;
348 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
349 _optConst,_verbose) ;
354 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),
"migrad");
355 bool ret = _theFitter->FitFCN(*_fcn);
356 _status = ((ret) ? _theFitter->Result().Status() : -1);
360 _fcn->BackProp(_theFitter->Result());
362 saveStatus(
"MIGRAD",_status) ;
377 if (_theFitter->GetMinimizer()==0) {
384 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
385 _optConst,_verbose) ;
390 _theFitter->Config().SetMinimizer(_minimizerType.c_str());
391 bool ret = _theFitter->CalculateHessErrors();
392 _status = ((ret) ? _theFitter->Result().Status() : -1);
396 _fcn->BackProp(_theFitter->Result());
398 saveStatus(
"HESSE",_status) ;
414 if (_theFitter->GetMinimizer()==0) {
421 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
422 _optConst,_verbose) ;
427 _theFitter->Config().SetMinimizer(_minimizerType.c_str());
428 bool ret = _theFitter->CalculateMinosErrors();
429 _status = ((ret) ? _theFitter->Result().Status() : -1);
433 _fcn->BackProp(_theFitter->Result());
435 saveStatus(
"MINOS",_status) ;
452 if (_theFitter->GetMinimizer()==0) {
457 else if (minosParamList.
getSize()>0) {
459 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
460 _optConst,_verbose) ;
468 std::vector<unsigned int> paramInd;
472 Int_t index = _fcn->GetFloatParamList()->index(par);
473 paramInd.push_back(index);
478 if (paramInd.size()) {
480 _theFitter->Config().SetMinosErrors(paramInd);
482 _theFitter->Config().SetMinimizer(_minimizerType.c_str());
483 bool ret = _theFitter->CalculateMinosErrors();
484 _status = ((ret) ? _theFitter->Result().Status() : -1);
486 _theFitter->Config().SetMinosErrors(
kFALSE);
492 _fcn->BackProp(_theFitter->Result());
494 saveStatus(
"MINOS",_status) ;
511 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
512 _optConst,_verbose) ;
517 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),
"seek");
518 bool ret = _theFitter->FitFCN(*_fcn);
519 _status = ((ret) ? _theFitter->Result().Status() : -1);
523 _fcn->BackProp(_theFitter->Result());
525 saveStatus(
"SEEK",_status) ;
540 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
541 _optConst,_verbose) ;
546 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),
"simplex");
547 bool ret = _theFitter->FitFCN(*_fcn);
548 _status = ((ret) ? _theFitter->Result().Status() : -1);
552 _fcn->BackProp(_theFitter->Result());
554 saveStatus(
"SEEK",_status) ;
569 _fcn->Synchronize(_theFitter->Config().ParamsSettings(),
570 _optConst,_verbose) ;
575 _theFitter->Config().SetMinimizer(_minimizerType.c_str(),
"migradimproved");
576 bool ret = _theFitter->FitFCN(*_fcn);
577 _status = ((ret) ? _theFitter->Result().Status() : -1);
581 _fcn->BackProp(_theFitter->Result());
583 saveStatus(
"IMPROVE",_status) ;
595 Int_t ret = _printLevel ;
596 _theFitter->Config().MinimizerOptions().SetPrintLevel(newLevel+1);
597 _printLevel = newLevel+1 ;
609 if (_optConst && !flag){
610 if (_printLevel>-1)
coutI(
Minimization) <<
"RooMinimizer::optimizeConst: deactivating const optimization" << endl ;
613 }
else if (!_optConst && flag) {
614 if (_printLevel>-1)
coutI(
Minimization) <<
"RooMinimizer::optimizeConst: activating const optimization" << endl ;
617 }
else if (_optConst && flag) {
618 if (_printLevel>-1)
coutI(
Minimization) <<
"RooMinimizer::optimizeConst: const optimization already active" << endl ;
620 if (_printLevel>-1)
coutI(
Minimization) <<
"RooMinimizer::optimizeConst: const optimization wasn't active" << endl ;
639 if (_theFitter->GetMinimizer()==0) {
646 name = userName ? userName :
Form(
"%s", _func->GetName()) ;
647 title = userTitle ? userTitle :
Form(
"%s", _func->GetTitle()) ;
652 RooArgList saveConstList(*(_fcn->GetConstParamList())) ;
653 RooArgList saveFloatInitList(*(_fcn->GetInitFloatParamList())) ;
654 RooArgList saveFloatFinalList(*(_fcn->GetFloatParamList())) ;
655 for (i=0 ; i<_fcn->GetFloatParamList()->getSize() ; i++) {
659 saveFloatFinalList.
remove(*par) ;
660 saveConstList.
add(*par) ;
663 saveConstList.
sort() ;
665 fitRes->setConstParList(saveConstList) ;
666 fitRes->setInitParList(saveFloatInitList) ;
668 fitRes->setStatus(_status) ;
669 fitRes->setCovQual(_theFitter->GetMinimizer()->CovMatrixStatus()) ;
670 fitRes->setMinNLL(_theFitter->Result().MinFcnValue()) ;
671 fitRes->setNumInvalidNLL(_fcn->GetNumInvalidNLL()) ;
672 fitRes->setEDM(_theFitter->Result().Edm()) ;
673 fitRes->setFinalParList(saveFloatFinalList) ;
675 std::vector<double> globalCC;
676 TMatrixDSym corrs(_theFitter->Result().Parameters().size()) ;
677 TMatrixDSym covs(_theFitter->Result().Parameters().size()) ;
678 for (
UInt_t ic=0; ic<_theFitter->Result().Parameters().size(); ic++) {
679 globalCC.push_back(_theFitter->Result().GlobalCC(ic));
680 for (
UInt_t ii=0; ii<_theFitter->Result().Parameters().size(); ii++) {
681 corrs(ic,ii) = _theFitter->Result().Correlation(ic,ii);
682 covs(ic,ii) = _theFitter->Result().CovMatrix(ic,ii);
685 fitRes->fillCorrMatrix(globalCC,corrs,covs) ;
687 fitRes->setCovarianceMatrix(*_extV) ;
690 fitRes->setStatusHistory(_statusHistory) ;
706 RooArgList* params = _fcn->GetFloatParamList() ;
710 Int_t index1= _fcn->GetFloatParamList()->index(&var1);
713 <<
") ERROR: " << var1.
GetName()
714 <<
" is not a floating parameter of "
715 << _func->GetName() << endl ;
719 Int_t index2= _fcn->GetFloatParamList()->index(&var2);
722 <<
") ERROR: " << var2.
GetName()
723 <<
" is not a floating parameter of PDF "
724 << _func->GetName() << endl ;
737 if (_theFitter->GetMinimizer()==0) {
738 coutW(
Minimization) <<
"RooMinimizer::contour: Error, run Migrad before contours!"
745 Double_t errdef= _theFitter->GetMinimizer()->ErrorDef();
748 n[0] = n1 ; n[1] = n2 ; n[2] = n3 ; n[3] = n4 ; n[4] = n5 ; n[5] = n6 ;
749 unsigned int npoints(50);
751 for (
Int_t ic = 0 ; ic<6 ; ic++) {
755 _theFitter->GetMinimizer()->SetErrorDef(n[ic]*n[ic]*errdef);
760 bool ret = _theFitter->GetMinimizer()->Contour(index1,index2,npoints,xcoor,ycoor);
765 <<
") ERROR: MINUIT did not return a contour graph for n="
768 xcoor[npoints] = xcoor[0];
769 ycoor[npoints] = ycoor[0];
772 graph->
SetName(
Form(
"contour_%s_n%f",_func->GetName(),n[ic])) ;
786 _theFitter->Config().MinimizerOptions().SetErrorDef(errdef);
789 *params = *paramSave ;
805 _profileStart =
kTRUE ;
835 _fcn->ApplyCovarianceMatrix(*_extV);
846 if (_theFitter==0 || _theFitter->GetMinimizer()==0) {
853 if (varList.
getSize()>0 && varList.
getSize()!=
Int_t(_theFitter->Result().NTotalParameters())) {
855 <<
"RooMinimizer::lastMinuitFit: ERROR: supplied variable list must be either empty " << endl
856 <<
" or match the number of variables of the last fit ("
857 << _theFitter->Result().NTotalParameters() <<
")" << endl ;
866 if (!dynamic_cast<RooRealVar*>(arg)) {
868 << arg->
GetName() <<
"' is not of type RooRealVar" << endl ;
882 for (i = 0; i < _theFitter->Result().NTotalParameters(); ++i) {
884 TString varName(_theFitter->Result().GetParameterName(i));
885 Bool_t isConst(_theFitter->Result().IsParameterFixed(i)) ;
887 Double_t xlo = _theFitter->Config().ParSettings(i).LowerLimit();
888 Double_t xhi = _theFitter->Config().ParSettings(i).UpperLimit();
889 Double_t xerr = _theFitter->Result().Error(i);
890 Double_t xval = _theFitter->Result().Value(i);
895 if ((xlo<xhi) && !isConst) {
896 var =
new RooRealVar(varName,varName,xval,xlo,xhi) ;
910 if (varName.CompareTo(var->
GetName())) {
911 oocoutI((
TObject*)0,
Eval) <<
"RooMinimizer::lastMinuitFit: fit parameter '" << varName
912 <<
"' stored in variable '" << var->
GetName() <<
"'" << endl ;
928 res->
setMinNLL(_theFitter->Result().MinFcnValue()) ;
929 res->
setEDM(_theFitter->Result().Edm()) ;
930 res->
setCovQual(_theFitter->GetMinimizer()->CovMatrixStatus()) ;
931 res->
setStatus(_theFitter->Result().Status()) ;
932 std::vector<double> globalCC;
933 TMatrixDSym corrs(_theFitter->Result().Parameters().size()) ;
934 TMatrixDSym covs(_theFitter->Result().Parameters().size()) ;
935 for (
UInt_t ic=0; ic<_theFitter->Result().Parameters().size(); ic++) {
936 globalCC.push_back(_theFitter->Result().GlobalCC(ic));
937 for (
UInt_t ii=0; ii<_theFitter->Result().Parameters().size(); ii++) {
938 corrs(ic,ii) = _theFitter->Result().Correlation(ic,ii);
939 covs(ic,ii) = _theFitter->Result().CovMatrix(ic,ii);
virtual void SetLineWidth(Width_t lwidth)
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
void sort(Bool_t reverse=kFALSE)
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
void optimizeConst(Int_t flag)
If flag is true, perform constant term optimization on function being minimized.
Int_t seek()
Execute SEEK.
void applyCovarianceMatrix(TMatrixDSym &V)
Apply results of given external covariance matrix.
virtual ~RooMinimizer()
Destructor.
void addObject(TObject *obj, Option_t *drawOptions="", Bool_t invisible=kFALSE)
Add a generic object to this plot.
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Int_t improve()
Execute IMPROVE.
virtual void SetName(const char *name)
Change (i.e.
void setStatus(Int_t val)
static void clearEvalErrorLog()
Clear the stack of evaluation error messages.
void ToLower()
Change string to lower-case.
ROOT::Fit::Fitter * fitter()
Return underlying ROOT fitter object.
static RooMsgService & instance()
Return reference to singleton instance.
void setInitParList(const RooArgList &list)
Fill the list of initial values of the floating parameters.
Iterator abstract base class.
void setMinimizerType(const char *type)
Choose the minimzer algorithm.
void setEps(Double_t eps)
Change MINUIT epsilon.
void setStrategy(Int_t strat)
Change MINUIT strategy to istrat.
void setEDM(Double_t val)
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Int_t simplex()
Execute SIMPLEX.
static void setEvalErrorLoggingMode(ErrorLoggingMode m)
Set evaluation error logging mode.
std::map< std::string, std::string >::const_iterator iter
TIterator * createIterator(Bool_t dir=kIterForward) const
RooMinimizer(RooAbsReal &function)
Construct MINUIT interface to given function.
Double_t getVal(const RooArgSet *set=0) const
void setMaxIterations(Int_t n)
Change maximum number of MINUIT iterations (RooMinimizer default 500 * #parameters) ...
TString operator+(const TString &s1, const TString &s2)
Use the special concatenation constructor.
void setMinNLL(Double_t val)
virtual void setVal(Double_t value)
Set value of variable to 'value'.
virtual void SetLineColor(Color_t lcolor)
void setMaxFunctionCalls(Int_t n)
Change maximum number of likelihood function calss from MINUIT (RooMinimizer default 500 * #parameter...
void function(const char *name_, T fun, const char *docstring=0)
RooAbsArg * find(const char *name) const
Find object with given name in list.
void setConstant(Bool_t value=kTRUE)
RooFitResult * save(const char *name=0, const char *title=0)
Save and return a RooFitResult snaphot of current minimizer status.
Fitter class, entry point for performing all type of fits.
char * Form(const char *fmt,...)
void setFinalParList(const RooArgList &list)
Fill the list of final values of the floating parameters.
Int_t migrad()
Execute MIGRAD.
virtual const char * GetName() const
Returns name of object.
Bool_t isConstant() const
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
void setConstParList(const RooArgList &list)
Fill the list of constant parameters.
RooAbsArg * at(Int_t idx) const
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Int_t minos()
Execute MINOS.
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
void setErrorLevel(Double_t level)
Set the level for MINUIT error analysis to the given value.
Int_t minimize(const char *type, const char *alg=0)
virtual void SetLineStyle(Style_t lstyle)
void profileStop()
Stop profiling timer and report results of last session.
void setRange(const char *name, Double_t min, Double_t max)
Set range named 'name to [min,max].
Mother of all ROOT objects.
Int_t setPrintLevel(Int_t newLevel)
Change the MINUIT internal printing level.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
Int_t hesse()
Execute HESSE.
RooPlot * contour(RooRealVar &var1, RooRealVar &var2, Double_t n1=1, Double_t n2=2, Double_t n3=0, Double_t n4=0, Double_t n5=0, Double_t n6=0)
Create and draw a TH2 with the error contours in parameters var1 and v2 at up to 6 'sigma' settings w...
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
virtual TObject * Next()=0
A Graph is a graphics object made of two arrays X and Y with npoints each.
void setCovQual(Int_t val)
static void cleanup()
Cleanup method called by atexit handler installed by RooSentinel to delete all global heap objects wh...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
void fillCorrMatrix()
Internal utility method to extract the correlation matrix and the global correlation coefficients fro...
void setOffsetting(Bool_t flag)
Enable internal likelihood offsetting for enhanced numeric precision.
RooFitResult * fit(const char *options)
Parse traditional RooAbsPdf::fitTo driver options.
static RooFitResult * lastMinuitFit(const RooArgList &varList=RooArgList())
void profileStart()
Start profiling timer.
void setError(Double_t value)
Bool_t silentMode() const