Logo ROOT  
Reference Guide
RooMinimizer.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
9 * PB, Patrick Bos, NL eScience Center, p.bos@esciencecenter.nl *
10 * *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17#ifndef ROO_MINIMIZER
18#define ROO_MINIMIZER
19
20#include <memory> // shared_ptr, unique_ptr
21
22#include "TObject.h"
23#include "TStopwatch.h"
24#include <fstream>
25#include <vector>
26#include <string>
27#include <utility>
28#include "TMatrixDSymfwd.h"
29
30#include "RooArgList.h" // cannot just use forward decl due to default argument in lastMinuitFit
31
34
35#include "RooSentinel.h"
36#include "RooMsgService.h"
37
38#include "Fit/Fitter.h"
39#include <stdexcept> // logic_error
40
41class RooAbsReal ;
42class RooFitResult ;
43class RooArgList ;
44class RooRealVar ;
45class RooArgSet ;
46class TH2F ;
47class RooPlot ;
48namespace RooFit {
49namespace TestStatistics {
50class LikelihoodSerial;
51//class LikelihoodGradientSerial;
52}
53} // namespace RooFit
54
55class RooMinimizer : public TObject {
56public:
58
60 static std::unique_ptr<RooMinimizer> create(RooAbsReal &function, FcnMode fcnMode = FcnMode::classic);
61 template <typename LikelihoodWrapperT /*= RooFit::TestStatistics::LikelihoodSerial*/,
62 typename LikelihoodGradientWrapperT /*= RooFit::TestStatistics::LikelihoodGradientSerial*/>
63 static std::unique_ptr<RooMinimizer> create(std::shared_ptr<RooFit::TestStatistics::RooAbsL> likelihood);
64
65 ~RooMinimizer() override;
66
67 enum Strategy { Speed=0, Balance=1, Robustness=2 } ;
69 void setStrategy(Int_t strat) ;
70 void setErrorLevel(Double_t level) ;
71 void setEps(Double_t eps) ;
72 void optimizeConst(Int_t flag) ;
74 /// \copydoc RooMinimizerFcn::SetRecoverFromNaNStrength()
75 void setRecoverFromNaNStrength(double strength) { fitterFcn()->SetRecoverFromNaNStrength(strength); }
76 void setOffsetting(Bool_t flag) ;
79
80 RooFitResult* fit(const char* options) ;
81
82 Int_t migrad() ;
83 Int_t hesse() ;
84 Int_t minos() ;
85 Int_t minos(const RooArgSet& minosParamList) ;
86 Int_t seek() ;
87 Int_t simplex() ;
88 Int_t improve() ;
89
90 Int_t minimize(const char* type, const char* alg=0) ;
91
92 RooFitResult* save(const char* name=0, const char* title=0) ;
93 RooPlot* contour(RooRealVar& var1, RooRealVar& var2,
94 Double_t n1=1, Double_t n2=2, Double_t n3=0,
95 Double_t n4=0, Double_t n5=0, Double_t n6=0, unsigned int npoints = 50) ;
96
97 Int_t setPrintLevel(Int_t newLevel) ;
98 void setPrintEvalErrors(Int_t numEvalErrors) { fitterFcn()->SetPrintEvalErrors(numEvalErrors); }
99 void setVerbose(Bool_t flag=kTRUE) { _verbose = flag ; fitterFcn()->SetVerbose(flag); }
100 void setProfile(Bool_t flag=kTRUE) { _profile = flag ; }
101 Bool_t setLogFile(const char* logf=0) { return fitterFcn()->SetLogFile(logf); }
102
103 Int_t getPrintLevel() const;
104
105 void setMinimizerType(const char* type) ;
106
107 static void cleanup() ;
108 static RooFitResult* lastMinuitFit(const RooArgList& varList=RooArgList()) ;
109
110 void saveStatus(const char* label, Int_t status) { _statusHistory.push_back(std::pair<std::string,int>(label,status)) ; }
111
112 Int_t evalCounter() const { return fitterFcn()->evalCounter() ; }
114
116 const ROOT::Fit::Fitter* fitter() const ;
117
120
121protected:
122
123 friend class RooAbsPdf ;
125
126 void profileStart() ;
127 void profileStop() ;
128
129 inline Int_t getNPar() const { return fitterFcn()->getNDim() ; }
130 inline std::ofstream* logfile() { return fitterFcn()->GetLogFile(); }
131 inline Double_t& maxFCN() { return fitterFcn()->GetMaxFCN() ; }
132
133 const RooAbsMinimizerFcn *fitterFcn() const;
135
136 bool fitFcn() const;
137
138private:
139 template <typename LikelihoodWrapperT /*= RooFit::TestStatistics::LikelihoodSerial*/, typename LikelihoodGradientWrapperT /*= RooFit::TestStatistics::LikelihoodGradientSerial*/>
140 RooMinimizer(std::shared_ptr<RooFit::TestStatistics::RooAbsL> likelihood,
141 LikelihoodWrapperT* /* used only for template deduction = static_cast<RooFit::TestStatistics::LikelihoodSerial*>(nullptr) */,
142 LikelihoodGradientWrapperT* /* used only for template deduction = static_cast<RooFit::TestStatistics::LikelihoodGradientSerial*>(nullptr) */);
143
147
152
154
156 std::string _minimizerType = "Minuit";
158
160
161 std::vector<std::pair<std::string,int> > _statusHistory ;
162
164
165 ClassDefOverride(RooMinimizer,0) // RooFit interface to ROOT::Fit::Fitter
166} ;
167
168
169template <typename LikelihoodWrapperT, typename LikelihoodGradientWrapperT>
170RooMinimizer::RooMinimizer(std::shared_ptr<RooFit::TestStatistics::RooAbsL> likelihood, LikelihoodWrapperT* /* value unused */,
171 LikelihoodGradientWrapperT* /* value unused */) :
172 _fcnMode(FcnMode::generic_wrapper)
173{
175
176 if (_theFitter)
177 delete _theFitter;
180 setEps(1.0); // default tolerance
181
182 _fcn = RooFit::TestStatistics::MinuitFcnGrad::create<LikelihoodWrapperT, LikelihoodGradientWrapperT>(likelihood, this, _theFitter->Config().ParamsSettings(), _verbose);
183
184 // default max number of calls
187
188 // Shut up for now
189 setPrintLevel(-1);
190
191 // Use +0.5 for 1-sigma errors
192 setErrorLevel(likelihood->defaultErrorLevel());
193
194 // Declare our parameters to MINUIT
196
197 // Now set default verbosity
198 if (RooMsgService::instance().silentMode()) {
199 setPrintLevel(-1);
200 } else {
201 setPrintLevel(1);
202 }
203}
204
205// static function
206template <typename LikelihoodWrapperT, typename LikelihoodGradientWrapperT>
207std::unique_ptr<RooMinimizer> RooMinimizer::create(std::shared_ptr<RooFit::TestStatistics::RooAbsL> likelihood) {
208 return std::make_unique<RooMinimizer>(likelihood, static_cast<LikelihoodWrapperT*>(nullptr),
209 static_cast<LikelihoodGradientWrapperT*>(nullptr));
210}
211
212#endif
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassDefOverride(name, id)
Definition: Rtypes.h:329
char name[80]
Definition: TGX11.cxx:110
int type
Definition: TGX11.cxx:121
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
Definition: FitConfig.h:181
const std::vector< ROOT::Fit::ParameterSettings > & ParamsSettings() const
get the vector of parameter settings (const method)
Definition: FitConfig.h:86
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
Definition: FitConfig.h:167
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
const FitConfig & Config() const
access to the fit configuration (const method)
Definition: Fitter.h:412
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
std::ofstream * GetLogFile()
virtual Bool_t Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameters, Bool_t optConst, Bool_t verbose)
Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector o...
void SetPrintEvalErrors(Int_t numEvalErrors)
void SetEvalErrorWall(Bool_t flag)
void SetVerbose(Bool_t flag=kTRUE)
unsigned int getNDim() const
Bool_t SetLogFile(const char *inLogfile)
Change the file name for logging of a RooMinimizer of all MINUIT steppings through the parameter spac...
void SetRecoverFromNaNStrength(double strength)
Try to recover from invalid function values.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:35
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
Definition: RooMinimizer.h:55
void setRecoverFromNaNStrength(double strength)
Try to recover from invalid function values.
Definition: RooMinimizer.h:75
Int_t _printLevel
Definition: RooMinimizer.h:144
std::vector< std::pair< std::string, int > > _statusHistory
Definition: RooMinimizer.h:161
std::ofstream * logfile()
Definition: RooMinimizer.h:130
RooMinimizer(RooAbsReal &function, FcnMode fcnMode=FcnMode::classic)
Construct MINUIT interface to given function.
Int_t hesse()
Execute HESSE.
Double_t & maxFCN()
Definition: RooMinimizer.h:131
void setEvalErrorWall(Bool_t flag)
Definition: RooMinimizer.h:73
void setMaxIterations(Int_t n)
Change maximum number of MINUIT iterations (RooMinimizer default 500 * #parameters)
void zeroEvalCount()
Definition: RooMinimizer.h:113
RooFitResult * save(const char *name=0, const char *title=0)
Save and return a RooFitResult snapshot of current minimizer status.
void setMinimizerType(const char *type)
Choose the minimizer algorithm.
Bool_t _verbose
Definition: RooMinimizer.h:148
void saveStatus(const char *label, Int_t status)
Definition: RooMinimizer.h:110
Int_t improve()
Execute IMPROVE.
FcnMode _fcnMode
Definition: RooMinimizer.h:157
void profileStart()
Start profiling timer.
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, unsigned int npoints=50)
Create and draw a TH2 with the error contours in the parameters var1 and var2.
Int_t migrad()
Execute MIGRAD.
Int_t minimize(const char *type, const char *alg=0)
Minimise the function passed in the constructor.
void profileStop()
Stop profiling timer and report results of last session.
ROOT::Math::IMultiGenFunction * getFitterMultiGenFcn() const
static RooFitResult * lastMinuitFit(const RooArgList &varList=RooArgList())
const RooAbsMinimizerFcn * fitterFcn() const
RooFitResult * fit(const char *options)
Parse traditional RooAbsPdf::fitTo driver options.
static std::unique_ptr< RooMinimizer > create(RooAbsReal &function, FcnMode fcnMode=FcnMode::classic)
void setOffsetting(Bool_t flag)
Enable internal likelihood offsetting for enhanced numeric precision.
TMatrixDSym * _extV
Definition: RooMinimizer.h:153
Int_t seek()
Execute SEEK.
Bool_t setLogFile(const char *logf=0)
Definition: RooMinimizer.h:101
void setProfile(Bool_t flag=kTRUE)
Definition: RooMinimizer.h:100
void setEps(Double_t eps)
Change MINUIT epsilon.
void setErrorLevel(Double_t level)
Set the level for MINUIT error analysis to the given value.
RooMinimizer(const RooMinimizer &)
static ROOT::Fit::Fitter * _theFitter
Definition: RooMinimizer.h:159
static void cleanup()
Cleanup method called by atexit handler installed by RooSentinel to delete all global heap objects wh...
Int_t setPrintLevel(Int_t newLevel)
Change the MINUIT internal printing level.
TStopwatch _timer
Definition: RooMinimizer.h:149
void setMaxFunctionCalls(Int_t n)
Change maximum number of likelihood function calss from MINUIT (RooMinimizer default 500 * #parameter...
Int_t evalCounter() const
Definition: RooMinimizer.h:112
Int_t minos()
Execute MINOS.
void applyCovarianceMatrix(TMatrixDSym &V)
Apply results of given external covariance matrix.
bool fitFcn() const
~RooMinimizer() override
Destructor.
void optimizeConst(Int_t flag)
If flag is true, perform constant term optimization on function being minimized.
ROOT::Math::IMultiGenFunction * getMultiGenFcn() const
Int_t simplex()
Execute SIMPLEX.
Bool_t _profileStart
Definition: RooMinimizer.h:151
Int_t getPrintLevel() const
ROOT::Fit::Fitter * fitter()
Return underlying ROOT fitter object.
void setStrategy(Int_t strat)
Change MINUIT strategy to istrat.
std::string _minimizerType
Definition: RooMinimizer.h:156
void setPrintEvalErrors(Int_t numEvalErrors)
Definition: RooMinimizer.h:98
TStopwatch _cumulTimer
Definition: RooMinimizer.h:150
Bool_t _profile
Definition: RooMinimizer.h:146
RooAbsMinimizerFcn * _fcn
Definition: RooMinimizer.h:155
void setVerbose(Bool_t flag=kTRUE)
Definition: RooMinimizer.h:99
Int_t getNPar() const
Definition: RooMinimizer.h:129
static RooMsgService & instance()
Return reference to singleton instance.
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:55
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
Mother of all ROOT objects.
Definition: TObject.h:37
Stopwatch class.
Definition: TStopwatch.h:28
const Int_t n
Definition: legend1.C:16
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:150
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...