// RooMinuit is a wrapper class around TFitter/TMinuit that
// provides a seamless interface between the MINUIT functionality
// and the native RooFit interface.
// <p>
// RooMinuit can minimize any RooAbsReal function with respect to
// its parameters. Usual choices for minimization are RooNLLVar
// and RooChi2Var
// <p>
// RooMinuit has methods corresponding to MINUIT functions like
// hesse(), migrad(), minos() etc. In each of these function calls
// the state of the MINUIT engine is synchronized with the state
// of the RooFit variables: any change in variables, change
// in the constant status etc is forwarded to MINUIT prior to
// execution of the MINUIT call. Afterwards the RooFit objects
// are resynchronized with the output state of MINUIT: changes
// parameter values, errors are propagated.
// <p>
// Various methods are available to control verbosity, profiling,
// automatic PDF optimization.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "TClass.h"
#include <fstream>
#include <iomanip>
#include "TH1.h"
#include "TH2.h"
#include "TMarker.h"
#include "TGraph.h"
#include "TStopwatch.h"
#include "TFitter.h"
#include "TMinuit.h"
#include "TDirectory.h"
#include "TMatrixDSym.h"
#include "RooMinuit.h"
#include "RooArgSet.h"
#include "RooArgList.h"
#include "RooAbsReal.h"
#include "RooAbsRealLValue.h"
#include "RooRealVar.h"
#include "RooFitResult.h"
#include "RooAbsPdf.h"
#include "RooSentinel.h"
#include "RooMsgService.h"
#include "RooPlot.h"
#if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
char* operator+( streampos&, char* );
#endif
using namespace std;
ClassImp(RooMinuit)
;
TVirtualFitter *RooMinuit::_theFitter = 0 ;
void RooMinuit::cleanup()
{
if (_theFitter) {
delete _theFitter ;
_theFitter =0 ;
}
}
RooMinuit::RooMinuit(RooAbsReal& function)
{
RooSentinel::activate() ;
_evalCounter = 0 ;
_extV = 0 ;
_func = &function ;
_logfile = 0 ;
_optConst = kFALSE ;
_verbose = kFALSE ;
_profile = kFALSE ;
_handleLocalErrors = kTRUE ;
_printLevel = 1 ;
_printEvalErrors = 10 ;
_warnLevel = -999 ;
_maxEvalMult = 500 ;
_doEvalErrorWall = kTRUE ;
RooArgSet* paramSet = function.getParameters(RooArgSet()) ;
RooArgList paramList(*paramSet) ;
delete paramSet ;
_floatParamList = (RooArgList*) paramList.selectByAttrib("Constant",kFALSE) ;
if (_floatParamList->getSize()>1) {
_floatParamList->sort() ;
}
_floatParamList->setName("floatParamList") ;
_constParamList = (RooArgList*) paramList.selectByAttrib("Constant",kTRUE) ;
if (_constParamList->getSize()>1) {
_constParamList->sort() ;
}
_constParamList->setName("constParamList") ;
TIterator* pIter = _floatParamList->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)pIter->Next())) {
if (!arg->IsA()->InheritsFrom(RooAbsRealLValue::Class())) {
coutW(Minimization) << "RooMinuit::RooMinuit: removing parameter " << arg->GetName()
<< " from list because it is not of type RooRealVar" << endl ;
_floatParamList->remove(*arg) ;
}
}
_nPar = _floatParamList->getSize() ;
delete pIter ;
updateFloatVec() ;
_initFloatParamList = (RooArgList*) _floatParamList->snapshot(kFALSE) ;
_initConstParamList = (RooArgList*) _constParamList->snapshot(kFALSE) ;
Int_t nPar= _floatParamList->getSize() + _constParamList->getSize() ;
if (_theFitter) delete _theFitter ;
_theFitter = new TFitter(nPar*2+1) ;
_theFitter->SetObjectFit(this) ;
setPrintLevel(-1) ;
_theFitter->Clear();
_theFitter->SetFCN(RooMinuitGlue);
setErrorLevel(function.defaultErrorLevel()) ;
synchronize(kFALSE) ;
_maxFCN= -1e30 ;
_numBadNLL = 0 ;
if (RooMsgService::instance().silentMode()) {
setWarnLevel(-1) ;
setPrintLevel(-1) ;
} else {
setWarnLevel(1) ;
setPrintLevel(1) ;
}
}
RooMinuit::~RooMinuit()
{
delete _floatParamList ;
delete _initFloatParamList ;
delete _constParamList ;
delete _initConstParamList ;
if (_extV) {
delete _extV ;
}
}
void RooMinuit::setStrategy(Int_t istrat)
{
Double_t stratArg(istrat) ;
_theFitter->ExecuteCommand("SET STR",&stratArg,1) ;
}
void RooMinuit::setErrorLevel(Double_t level)
{
_theFitter->ExecuteCommand("SET ERR",&level,1);
}
void RooMinuit::setEps(Double_t eps)
{
_theFitter->ExecuteCommand("SET EPS",&eps,1) ;
}
void RooMinuit::setOffsetting(Bool_t flag)
{
_func->enableOffsetting(flag) ;
}
RooFitResult* RooMinuit::fit(const char* options)
{
if (_floatParamList->getSize()==0) {
return 0 ;
}
_theFitter->SetObjectFit(this) ;
TString opts(options) ;
opts.ToLower() ;
if (opts.Contains("v")) setVerbose(1) ;
if (opts.Contains("t")) setProfile(1) ;
if (opts.Contains("l")) setLogFile(Form("%s.log",_func->GetName())) ;
if (opts.Contains("c")) optimizeConst(1) ;
if (opts.Contains("s")) hesse() ;
if (opts.Contains("0")) setStrategy(0) ;
migrad() ;
if (opts.Contains("0")) setStrategy(1) ;
if (opts.Contains("h")||!opts.Contains("m")) hesse() ;
if (!opts.Contains("m")) minos() ;
return (opts.Contains("r")) ? save() : 0 ;
}
Int_t RooMinuit::migrad()
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Double_t arglist[2];
arglist[0]= _maxEvalMult*_nPar;
arglist[1]= 1.0;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("MIGRAD",arglist,2);
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
saveStatus("MIGRAD",_status) ;
return _status ;
}
Int_t RooMinuit::hesse()
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Double_t arglist[2];
arglist[0]= _maxEvalMult*_nPar;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("HESSE",arglist,1);
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
saveStatus("HESSE",_status) ;
return _status ;
}
Int_t RooMinuit::minos()
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Double_t arglist[2];
arglist[0]= _maxEvalMult*_nPar;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("MINOS",arglist,1);
if (_status == 0 && gMinuit->fCstatu != "SUCCESSFUL") {
if (gMinuit->fCstatu == "FAILURE" ||
gMinuit->fCstatu == "PROBLEMS") _status = 5;
_status = 6;
}
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
saveStatus("MINOS",_status) ;
return _status ;
}
Int_t RooMinuit::minos(const RooArgSet& minosParamList)
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Int_t nMinosPar(0) ;
Double_t* arglist = new Double_t[_nPar+1];
if (minosParamList.getSize()>0) {
TIterator* aIter = minosParamList.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)aIter->Next())) {
RooAbsArg* par = _floatParamList->find(arg->GetName());
if (par && !par->isConstant()) {
Int_t index = _floatParamList->index(par);
nMinosPar++;
arglist[nMinosPar]=index+1;
}
}
delete aIter ;
}
arglist[0]= _maxEvalMult*_nPar;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("MINOS",arglist,1+nMinosPar);
if (_status == 0 && gMinuit->fCstatu != "SUCCESSFUL") {
if (gMinuit->fCstatu == "FAILURE" ||
gMinuit->fCstatu == "PROBLEMS") _status = 5;
_status = 6;
}
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
delete[] arglist ;
saveStatus("MINOS",_status) ;
return _status ;
}
Int_t RooMinuit::seek()
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Double_t arglist[2];
arglist[0]= _maxEvalMult*_nPar;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("SEEK",arglist,1);
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
saveStatus("SEEK",_status) ;
return _status ;
}
Int_t RooMinuit::simplex()
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Double_t arglist[2];
arglist[0]= _maxEvalMult*_nPar;
arglist[1]= 1.0;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("SIMPLEX",arglist,2);
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
saveStatus("SIMPLEX",_status) ;
return _status ;
}
Int_t RooMinuit::improve()
{
if (_floatParamList->getSize()==0) {
return -1 ;
}
_theFitter->SetObjectFit(this) ;
Double_t arglist[2];
arglist[0]= _maxEvalMult*_nPar;
synchronize(_verbose) ;
profileStart() ;
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
RooAbsReal::clearEvalErrorLog() ;
_status= _theFitter->ExecuteCommand("IMPROVE",arglist,1);
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
profileStop() ;
backProp() ;
saveStatus("IMPROVE",_status) ;
return _status ;
}
Int_t RooMinuit::setPrintLevel(Int_t newLevel)
{
Int_t ret = _printLevel ;
Double_t arg(newLevel) ;
_theFitter->ExecuteCommand("SET PRINT",&arg,1);
_printLevel = newLevel ;
return ret ;
}
void RooMinuit::setNoWarn()
{
Double_t arg(0) ;
_theFitter->ExecuteCommand("SET NOWARNINGS",&arg,1);
_warnLevel = -1 ;
}
Int_t RooMinuit::setWarnLevel(Int_t newLevel)
{
if (newLevel==_warnLevel) {
return _warnLevel ;
}
Int_t ret = _warnLevel ;
Double_t arg(newLevel) ;
if (newLevel>=0) {
_theFitter->ExecuteCommand("SET WARNINGS",&arg,1);
} else {
Double_t arg2(0) ;
_theFitter->ExecuteCommand("SET NOWARNINGS",&arg2,1);
}
_warnLevel = newLevel ;
return ret ;
}
Bool_t RooMinuit::synchronize(Bool_t verbose)
{
Int_t oldPrint = setPrintLevel(-1) ;
gMinuit->fNwrmes[0] = 0;
Int_t oldWarn = setWarnLevel(-1) ;
Bool_t constValChange(kFALSE) ;
Bool_t constStatChange(kFALSE) ;
Int_t index(0) ;
for(index= 0; index < _constParamList->getSize() ; index++) {
RooRealVar *par= dynamic_cast<RooRealVar*>(_constParamList->at(index)) ;
if (!par) continue ;
RooRealVar *oldpar= dynamic_cast<RooRealVar*>(_initConstParamList->at(index)) ;
if (!oldpar) continue ;
if (!par->isConstant()) {
_constParamList->remove(*par) ;
_floatParamList->add(*par) ;
_initFloatParamList->addClone(*oldpar) ;
_initConstParamList->remove(*oldpar) ;
constStatChange=kTRUE ;
_nPar++ ;
if (verbose) {
coutI(Minimization) << "RooMinuit::synchronize: parameter " << par->GetName() << " is now floating." << endl ;
}
}
if (par->getVal()!= oldpar->getVal()) {
constValChange=kTRUE ;
if (verbose) {
coutI(Minimization) << "RooMinuit::synchronize: value of constant parameter " << par->GetName()
<< " changed from " << oldpar->getVal() << " to " << par->getVal() << endl ;
}
}
}
*_initConstParamList = *_constParamList ;
for(index= 0; index < _nPar; index++) {
RooRealVar *par= dynamic_cast<RooRealVar*>(_floatParamList->at(index)) ;
if (!par) continue ;
Double_t pstep(0) ;
Double_t pmin(0) ;
Double_t pmax(0) ;
if(!par->isConstant()) {
if (!par->IsA()->InheritsFrom(RooRealVar::Class())) {
coutW(Minimization) << "RooMinuit::fit: Error, non-constant parameter " << par->GetName()
<< " is not of type RooRealVar, skipping" << endl ;
continue ;
}
if (par->hasMin() && par->hasMax()) {
pmin = par->getMin();
pmax = par->getMax();
}
pstep= par->getError();
if(pstep <= 0) {
if (par->hasMin() && par->hasMax()) {
pstep= 0.1*(pmax-pmin);
if (pmax - par->getVal() < 2*pstep) {
pstep = (pmax - par->getVal())/2 ;
} else if (par->getVal() - pmin < 2*pstep) {
pstep = (par->getVal() - pmin )/2 ;
}
if (pstep==0) {
pstep= 0.1*(pmax-pmin);
}
} else {
pstep=1 ;
}
if(_verbose) {
coutW(Minimization) << "RooMinuit::synchronize: WARNING: no initial error estimate available for "
<< par->GetName() << ": using " << pstep << endl;
}
}
} else {
pmin = par->getVal() ;
pmax = par->getVal() ;
}
Double_t oldVar,oldVerr,oldVlo,oldVhi ;
char oldParname[100] ;
Int_t ierr = _theFitter->GetParameter(index,oldParname,oldVar,oldVerr,oldVlo,oldVhi) ;
Int_t ix ;
Bool_t oldFixed(kFALSE) ;
if (ierr>=0) {
for (ix = 1; ix <= gMinuit->fNpfix; ++ix) {
if (gMinuit->fIpfix[ix-1] == index+1) oldFixed=kTRUE ;
}
}
if (par->isConstant() && !oldFixed) {
if (oldVar!=par->getVal()) {
Double_t arglist[2] ;
arglist[0] = index+1 ;
arglist[1] = par->getVal() ;
_theFitter->ExecuteCommand("SET PAR",arglist,2) ;
if (verbose) {
coutI(Minimization) << "RooMinuit::synchronize: value of parameter " << par->GetName() << " changed from " << oldVar << " to " << par->getVal() << endl ;
}
}
_theFitter->FixParameter(index) ;
constStatChange=kTRUE ;
if (verbose) {
coutI(Minimization) << "RooMinuit::synchronize: parameter " << par->GetName() << " is now fixed." << endl ;
}
} else if (par->isConstant() && oldFixed) {
if (oldVar!=par->getVal()) {
Double_t arglist[2] ;
arglist[0] = index+1 ;
arglist[1] = par->getVal() ;
_theFitter->ExecuteCommand("SET PAR",arglist,2) ;
constValChange=kTRUE ;
if (verbose) {
coutI(Minimization) << "RooMinuit::synchronize: value of fixed parameter " << par->GetName() << " changed from " << oldVar << " to " << par->getVal() << endl ;
}
}
} else {
if (!par->isConstant() && oldFixed) {
_theFitter->ReleaseParameter(index) ;
constStatChange=kTRUE ;
if (verbose) {
coutI(Minimization) << "RooMinuit::synchronize: parameter " << par->GetName() << " is now floating." << endl ;
}
}
if (oldVar!=par->getVal() || oldVlo!=pmin || oldVhi != pmax || oldVerr!=pstep) {
_theFitter->SetParameter(index, par->GetName(), par->getVal(), pstep, pmin, pmax);
}
if (verbose && ierr>=0) {
if (oldVar!=par->getVal()) {
coutI(Minimization) << "RooMinuit::synchronize: value of parameter " << par->GetName() << " changed from " << oldVar << " to " << par->getVal() << endl ;
}
if (oldVlo!=pmin || oldVhi!=pmax) {
coutI(Minimization) << "RooMinuit::synchronize: limits of parameter " << par->GetName() << " changed from [" << oldVlo << "," << oldVhi
<< "] to [" << pmin << "," << pmax << "]" << endl ;
}
if (oldVerr!=pstep && oldVerr!=0) {
coutI(Minimization) << "RooMinuit::synchronize: error/step size of parameter " << par->GetName() << " changed from " << oldVerr << " to " << pstep << endl ;
}
}
}
}
gMinuit->fNwrmes[0] = 0;
oldWarn = setWarnLevel(oldWarn) ;
oldPrint = setPrintLevel(oldPrint) ;
if (_optConst) {
if (constStatChange) {
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
coutI(Minimization) << "RooMinuit::synchronize: set of constant parameters changed, rerunning const optimizer" << endl ;
_func->constOptimizeTestStatistic(RooAbsArg::ConfigChange) ;
} else if (constValChange) {
coutI(Minimization) << "RooMinuit::synchronize: constant parameter values changed, rerunning const optimizer" << endl ;
_func->constOptimizeTestStatistic(RooAbsArg::ValueChange) ;
}
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
}
updateFloatVec() ;
return 0 ;
}
void RooMinuit::optimizeConst(Int_t flag)
{
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::CollectErrors) ;
if (_optConst && !flag){
if (_printLevel>-1) coutI(Minimization) << "RooMinuit::optimizeConst: deactivating const optimization" << endl ;
_func->constOptimizeTestStatistic(RooAbsArg::DeActivate,flag>1) ;
_optConst = flag ;
} else if (!_optConst && flag) {
if (_printLevel>-1) coutI(Minimization) << "RooMinuit::optimizeConst: activating const optimization" << endl ;
_func->constOptimizeTestStatistic(RooAbsArg::Activate,flag>1) ;
_optConst = flag ;
} else if (_optConst && flag) {
if (_printLevel>-1) coutI(Minimization) << "RooMinuit::optimizeConst: const optimization already active" << endl ;
} else {
if (_printLevel>-1) coutI(Minimization) << "RooMinuit::optimizeConst: const optimization wasn't active" << endl ;
}
RooAbsReal::setEvalErrorLoggingMode(RooAbsReal::PrintErrors) ;
}
RooFitResult* RooMinuit::save(const char* userName, const char* userTitle)
{
TString name,title ;
name = userName ? userName : Form("%s", _func->GetName()) ;
title = userTitle ? userTitle : Form("%s", _func->GetTitle()) ;
if (_floatParamList->getSize()==0) {
RooFitResult* fitRes = new RooFitResult(name,title) ;
fitRes->setConstParList(*_constParamList) ;
fitRes->setInitParList(RooArgList()) ;
fitRes->setFinalParList(RooArgList()) ;
fitRes->setStatus(-999) ;
fitRes->setCovQual(-999) ;
fitRes->setMinNLL(_func->getVal()) ;
fitRes->setNumInvalidNLL(0) ;
fitRes->setEDM(-999) ;
return fitRes ;
}
RooFitResult* fitRes = new RooFitResult(name,title) ;
Int_t i ;
RooArgList saveConstList(*_constParamList) ;
RooArgList saveFloatInitList(*_initFloatParamList) ;
RooArgList saveFloatFinalList(*_floatParamList) ;
for (i=0 ; i<_floatParamList->getSize() ; i++) {
RooAbsArg* par = _floatParamList->at(i) ;
if (par->isConstant()) {
saveFloatInitList.remove(*saveFloatInitList.find(par->GetName()),kTRUE) ;
saveFloatFinalList.remove(*par) ;
saveConstList.add(*par) ;
}
}
saveConstList.sort() ;
fitRes->setConstParList(saveConstList) ;
fitRes->setInitParList(saveFloatInitList) ;
Double_t edm, errdef, minVal;
Int_t nvpar, nparx;
Int_t icode = _theFitter->GetStats(minVal, edm, errdef, nvpar, nparx);
fitRes->setStatus(_status) ;
fitRes->setCovQual(icode) ;
fitRes->setMinNLL(minVal) ;
fitRes->setNumInvalidNLL(_numBadNLL) ;
fitRes->setEDM(edm) ;
fitRes->setFinalParList(saveFloatFinalList) ;
if (!_extV) {
fitRes->fillCorrMatrix() ;
} else {
fitRes->setCovarianceMatrix(*_extV) ;
}
fitRes->setStatusHistory(_statusHistory) ;
return fitRes ;
}
RooPlot* RooMinuit::contour(RooRealVar& var1, RooRealVar& var2, Double_t n1, Double_t n2, Double_t n3, Double_t n4, Double_t n5, Double_t n6)
{
_theFitter->SetObjectFit(this) ;
RooArgList* paramSave = (RooArgList*) _floatParamList->snapshot() ;
Int_t index1= _floatParamList->index(&var1);
if(index1 < 0) {
coutE(Minimization) << "RooMinuit::contour(" << GetName()
<< ") ERROR: " << var1.GetName() << " is not a floating parameter of " << _func->GetName() << endl ;
return 0;
}
Int_t index2= _floatParamList->index(&var2);
if(index2 < 0) {
coutE(Minimization) << "RooMinuit::contour(" << GetName()
<< ") ERROR: " << var2.GetName() << " is not a floating parameter of PDF " << _func->GetName() << endl ;
return 0;
}
RooPlot* frame = new RooPlot(var1,var2) ;
TMarker *point= new TMarker(var1.getVal(), var2.getVal(), 8);
frame->addObject(point) ;
Double_t errdef= gMinuit->fUp;
Double_t n[6] ;
n[0] = n1 ; n[1] = n2 ; n[2] = n3 ; n[3] = n4 ; n[4] = n5 ; n[5] = n6 ;
for (Int_t ic = 0 ; ic<6 ; ic++) {
if(n[ic] > 0) {
gMinuit->SetErrorDef(n[ic]*n[ic]*errdef);
TGraph* graph= (TGraph*)gMinuit->Contour(50, index1, index2);
if (!graph) {
coutE(Minimization) << "RooMinuit::contour(" << GetName() << ") ERROR: MINUIT did not return a contour graph for n=" << n[ic] << endl ;
} else {
graph->SetName(Form("contour_%s_n%f",_func->GetName(),n[ic])) ;
graph->SetLineStyle(ic+1) ;
graph->SetLineWidth(2) ;
graph->SetLineColor(kBlue) ;
frame->addObject(graph,"L") ;
}
}
}
gMinuit->SetErrorDef(errdef);
*_floatParamList = *paramSave ;
delete paramSave ;
return frame ;
}
Bool_t RooMinuit::setLogFile(const char* inLogfile)
{
if (_logfile) {
coutI(Minimization) << "RooMinuit::setLogFile: closing previous log file" << endl ;
_logfile->close() ;
delete _logfile ;
_logfile = 0 ;
}
_logfile = new ofstream(inLogfile) ;
if (!_logfile->good()) {
coutI(Minimization) << "RooMinuit::setLogFile: cannot open file " << inLogfile << endl ;
_logfile->close() ;
delete _logfile ;
_logfile= 0;
}
return kFALSE ;
}
Double_t RooMinuit::getPdfParamVal(Int_t index)
{
return ((RooRealVar*)_floatParamList->at(index))->getVal() ;
}
Double_t RooMinuit::getPdfParamErr(Int_t index)
{
return ((RooRealVar*)_floatParamList->at(index))->getError() ;
}
Bool_t RooMinuit::setPdfParamVal(Int_t index, Double_t value, Bool_t verbose)
{
RooRealVar* par = (RooRealVar*)_floatParamVec[index] ;
if (par->getVal()!=value) {
if (verbose) cout << par->GetName() << "=" << value << ", " ;
par->setVal(value) ;
return kTRUE ;
}
return kFALSE ;
}
void RooMinuit::setPdfParamErr(Int_t index, Double_t value)
{
((RooRealVar*)_floatParamList->at(index))->setError(value) ;
}
void RooMinuit::clearPdfParamAsymErr(Int_t index)
{
((RooRealVar*)_floatParamList->at(index))->removeAsymError() ;
}
void RooMinuit::setPdfParamErr(Int_t index, Double_t loVal, Double_t hiVal)
{
((RooRealVar*)_floatParamList->at(index))->setAsymError(loVal,hiVal) ;
}
void RooMinuit::profileStart()
{
if (_profile) {
_timer.Start() ;
_cumulTimer.Start(kFALSE) ;
}
}
void RooMinuit::profileStop()
{
if (_profile) {
_timer.Stop() ;
_cumulTimer.Stop() ;
coutI(Minimization) << "Command timer: " ; _timer.Print() ;
coutI(Minimization) << "Session timer: " ; _cumulTimer.Print() ;
}
}
void RooMinuit::backProp()
{
Double_t val,err,vlo,vhi, eplus, eminus, eparab, globcc;
char buffer[10240];
Int_t index ;
for(index= 0; index < _nPar; index++) {
_theFitter->GetParameter(index, buffer, val, err, vlo, vhi);
setPdfParamVal(index, val);
_theFitter->GetErrors(index, eplus, eminus, eparab, globcc);
setPdfParamErr(index, err);
if(eplus > 0 || eminus < 0) {
setPdfParamErr(index, eminus,eplus);
} else {
clearPdfParamAsymErr(index) ;
}
}
}
void RooMinuit::updateFloatVec()
{
_floatParamVec.clear() ;
RooFIter iter = _floatParamList->fwdIterator() ;
RooAbsArg* arg ;
_floatParamVec.resize(_floatParamList->getSize()) ;
Int_t i(0) ;
while((arg=iter.next())) {
_floatParamVec[i++] = arg ;
}
}
void RooMinuit::applyCovarianceMatrix(TMatrixDSym& V)
{
_extV = (TMatrixDSym*) V.Clone() ;
for (Int_t i=0 ; i<getNPar() ; i++) {
if (_floatParamList->at(i)->isConstant()) {
continue ;
}
RooMinuit* context = (RooMinuit*) RooMinuit::_theFitter->GetObjectFit() ;
if (context && context->_verbose)
cout << "setting parameter " << i << " error to " << sqrt((*_extV)(i,i)) << endl ;
setPdfParamErr(i, sqrt((*_extV)(i,i))) ;
}
}
void RooMinuitGlue(Int_t& , Double_t* ,
Double_t &f, Double_t *par, Int_t )
{
RooMinuit* context = (RooMinuit*) RooMinuit::_theFitter->GetObjectFit() ;
ofstream* logf = context->logfile() ;
Double_t& maxFCN = context->maxFCN() ;
Bool_t verbose = context->_verbose ;
Int_t nPar= context->getNPar();
for(Int_t index= 0; index < nPar; index++) {
if (logf) (*logf) << par[index] << " " ;
context->setPdfParamVal(index, par[index],verbose);
}
RooAbsReal::setHideOffset(kFALSE) ;
f= context->_func->getVal() ;
RooAbsReal::setHideOffset(kTRUE) ;
context->_evalCounter++ ;
if ( RooAbsPdf::evalError() || RooAbsReal::numEvalErrors()>0 || f>1e30) {
if (context->_printEvalErrors>=0) {
if (context->_doEvalErrorWall) {
oocoutW(context,Minimization) << "RooMinuitGlue: Minimized function has error status." << endl
<< "Returning maximum FCN so far (" << maxFCN
<< ") to force MIGRAD to back out of this region. Error log follows" << endl ;
} else {
oocoutW(context,Minimization) << "RooMinuitGlue: Minimized function has error status but is ignored" << endl ;
}
TIterator* iter = context->_floatParamList->createIterator() ;
RooRealVar* var ;
Bool_t first(kTRUE) ;
ooccoutW(context,Minimization) << "Parameter values: " ;
while((var=(RooRealVar*)iter->Next())) {
if (first) { first = kFALSE ; } else ooccoutW(context,Minimization) << ", " ;
ooccoutW(context,Minimization) << var->GetName() << "=" << var->getVal() ;
}
delete iter ;
ooccoutW(context,Minimization) << endl ;
RooAbsReal::printEvalErrors(ooccoutW(context,Minimization),context->_printEvalErrors) ;
ooccoutW(context,Minimization) << endl ;
}
if (context->_doEvalErrorWall) {
f = maxFCN+1 ;
}
RooAbsPdf::clearEvalError() ;
RooAbsReal::clearEvalErrorLog() ;
context->_numBadNLL++ ;
} else if (f>maxFCN) {
maxFCN = f ;
}
if (logf) (*logf) << setprecision(15) << f << setprecision(4) << endl;
if (verbose) {
cout << "\nprevFCN" << (context->_func->isOffsetting()?"-offset":"") << " = " << setprecision(10) << f << setprecision(4) << " " ;
cout.flush() ;
}
}