Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
23
24#include <Fit/Fitter.h>
25#include <TStopwatch.h>
26#include <TMatrixDSymfwd.h>
27
28#include <fstream>
29#include <memory> // shared_ptr, unique_ptr
30#include <string>
31#include <utility>
32#include <vector>
33
35class RooAbsReal;
36class RooFitResult;
37class RooArgList;
38class RooRealVar;
39class RooArgSet;
40class RooPlot;
41class RooDataSet;
42namespace RooFit {
43namespace TestStatistics {
44class LikelihoodGradientJob;
45}
46} // namespace RooFit
47
48class RooMinimizer : public TObject {
49public:
50 /// Config argument to RooMinimizer constructor.
51 struct Config {
52
53 Config() {}
54
55 bool useGradient = true; // Use the gradient provided by the RooAbsReal, if there is one.
56
57 double recoverFromNaN = 10.; // RooAbsMinimizerFcn config
58 int printEvalErrors = 10; // RooAbsMinimizerFcn config
59 int doEEWall = 1; // RooAbsMinimizerFcn config
60 int offsetting = -1; // RooAbsMinimizerFcn config
61 const char *logf = nullptr; // RooAbsMinimizerFcn config
62
63 // RooAbsMinimizerFcn config that can only be set in constructor, 0 means no parallelization (default),
64 // -1 is parallelization with the number of workers controlled by RooFit::MultiProcess which
65 // defaults to the number of available processors, n means parallelization with n CPU's
66 int parallelize = 0;
67
68 // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
69 // argument is ignored when parallelize is 0
71
72 // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
73 // argument is ignored when parallelize is 0
75
76 bool verbose = false; // local config
77 bool profile = false; // local config
78 bool timingAnalysis = false; // local config
79 std::string minimizerType; // local config
80 private:
82 };
83
84 explicit RooMinimizer(RooAbsReal &function, Config const &cfg = {});
85
86 ~RooMinimizer() override;
87
88 enum Strategy { Speed = 0, Balance = 1, Robustness = 2 };
89 enum PrintLevel { None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3 };
90
91 // Setters on _theFitter
92 void setStrategy(int istrat);
93 void setErrorLevel(double level);
94 void setEps(double eps);
95 void setMaxIterations(int n);
96 void setMaxFunctionCalls(int n);
97 void setPrintLevel(int newLevel);
98
99 // Setters on _fcn
100 void optimizeConst(int flag);
101 void setEvalErrorWall(bool flag) { _cfg.doEEWall = flag; }
102 void setRecoverFromNaNStrength(double strength);
103 void setOffsetting(bool flag);
104 void setPrintEvalErrors(int numEvalErrors) { _cfg.printEvalErrors = numEvalErrors; }
105 void setVerbose(bool flag = true) { _cfg.verbose = flag; }
106 bool setLogFile(const char *logf = nullptr);
107
108 int migrad();
109 int hesse();
110 int minos();
111 int minos(const RooArgSet &minosParamList);
112 int seek();
113 int simplex();
114 int improve();
115
116 int minimize(const char *type, const char *alg = nullptr);
117
118 RooFit::OwningPtr<RooFitResult> save(const char *name = nullptr, const char *title = nullptr);
119 RooPlot *contour(RooRealVar &var1, RooRealVar &var2, double n1 = 1.0, double n2 = 2.0, double n3 = 0.0,
120 double n4 = 0.0, double n5 = 0.0, double n6 = 0.0, unsigned int npoints = 50);
121
122 void setProfile(bool flag = true) { _cfg.profile = flag; }
123 /// Enable or disable the logging of function evaluations to a RooDataSet.
124 /// \see RooMinimizer::getLogDataSet().
125 /// param[in] flag Boolean flag to disable or enable the functionality.
126 void setLoggingToDataSet(bool flag = true) { _loggingToDataSet = flag; }
127
128 /// If logging of function evaluations to a RooDataSet is enabled, returns a
129 /// pointer to a dataset with one row per evaluation of the RooAbsReal passed
130 /// to the minimizer. As columns, there are all floating parameters and the
131 /// values they had for that evaluation.
132 /// \see RooMinimizer::setLoggingToDataSet(bool).
134
135 static int getPrintLevel();
136
137 void setMinimizerType(std::string const &type);
138 std::string const &minimizerType() const { return _cfg.minimizerType; }
139
140 static void cleanup();
143
144 void saveStatus(const char *label, int status) { _statusHistory.emplace_back(label, status); }
145
146 /// Clears the Minuit status history.
148
149 int evalCounter() const;
150 void zeroEvalCount();
151
153 const ROOT::Fit::Fitter *fitter() const;
154
156
157 int getNPar() const;
158
159 void applyCovarianceMatrix(TMatrixDSym const &V);
160
161private:
162 friend class RooAbsMinimizerFcn;
163 friend class RooMinimizerFcn;
165
166 std::unique_ptr<RooAbsReal::EvalErrorContext> makeEvalErrorContext() const;
167
169
170 void profileStart();
171 void profileStop();
172
173 std::ofstream *logfile();
174 double &maxFCN();
175 double &fcnOffset() const;
176
177 bool fitFcn() const;
178
179 // constructor helper functions
182
183 void determineStatus(bool fitterReturnValue);
184
185 int _status = -99;
186 bool _profileStart = false;
187 bool _loggingToDataSet = false;
188
191
192 std::unique_ptr<TMatrixDSym> _extV;
193
194 std::unique_ptr<RooAbsMinimizerFcn> _fcn;
195
196 static std::unique_ptr<ROOT::Fit::Fitter> _theFitter;
197
198 std::vector<std::pair<std::string, int>> _statusHistory;
199
200 std::unique_ptr<RooDataSet> _logDataSet;
201
202 RooMinimizer::Config _cfg; // local config object
203
204 ClassDefOverride(RooMinimizer, 0) // RooFit interface to ROOT::Fit::Fitter
205};
206
207#endif
RooAbsReal & function()
double defaultErrorLevel() const override
Definition RooChi2Var.h:17
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
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
char name[80]
Definition TGX11.cxx:110
Fitter class, entry point for performing all type of fits.
Definition Fitter.h:77
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:61
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
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:55
Container class to hold unbinned data.
Definition RooDataSet.h:57
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
RooMinimizerFcn is an interface to the ROOT::Math::IBaseFunctionMultiDim, a function that ROOT's mini...
Wrapper class around ROOT::Fit:Fitter that provides a seamless interface between the minimizer functi...
void setRecoverFromNaNStrength(double strength)
Try to recover from invalid function values.
static int getPrintLevel()
Get the MINUIT internal printing level.
void optimizeConst(int flag)
If flag is true, perform constant term optimization on function being minimized.
void initMinimizerFirstPart()
Initialize the part of the minimizer that is independent of the function to be minimized.
std::ofstream * logfile()
int simplex()
Execute SIMPLEX.
std::unique_ptr< TMatrixDSym > _extV
void setMinimizerType(std::string const &type)
Choose the minimizer algorithm.
RooFit::OwningPtr< RooFitResult > save(const char *name=nullptr, const char *title=nullptr)
Save and return a RooFitResult snapshot of current minimizer status.
std::vector< std::pair< std::string, int > > _statusHistory
std::unique_ptr< RooDataSet > _logDataSet
void profileStart()
Start profiling timer.
void setProfile(bool flag=true)
RooPlot * contour(RooRealVar &var1, RooRealVar &var2, double n1=1.0, double n2=2.0, double n3=0.0, double n4=0.0, double n5=0.0, double n6=0.0, unsigned int npoints=50)
Create and draw a TH2 with the error contours in the parameters var1 and var2.
bool setLogFile(const char *logf=nullptr)
void initMinimizerFcnDependentPart(double defaultErrorLevel)
Initialize the part of the minimizer that is dependent on the function to be minimized.
double & fcnOffset() const
void setLoggingToDataSet(bool flag=true)
Enable or disable the logging of function evaluations to a RooDataSet.
void profileStop()
Stop profiling timer and report results of last session.
int minos()
Execute MINOS.
double & maxFCN()
int hesse()
Execute HESSE.
void setErrorLevel(double level)
Set the level for MINUIT error analysis to the given value.
static std::unique_ptr< ROOT::Fit::Fitter > _theFitter
void determineStatus(bool fitterReturnValue)
void setEvalErrorWall(bool flag)
int migrad()
Execute MIGRAD.
int seek()
Execute SEEK.
void setEps(double eps)
Change MINUIT epsilon.
void setPrintLevel(int newLevel)
Change the MINUIT internal printing level.
std::string const & minimizerType() const
int improve()
Execute IMPROVE.
bool _loggingToDataSet
static void cleanup()
Cleanup method called by atexit handler installed by RooSentinel to delete all global heap objects wh...
void setOffsetting(bool flag)
Enable internal likelihood offsetting for enhanced numeric precision.
TStopwatch _timer
RooMinimizer::Config _cfg
RooDataSet * getLogDataSet() const
If logging of function evaluations to a RooDataSet is enabled, returns a pointer to a dataset with on...
static RooFit::OwningPtr< RooFitResult > lastMinuitFit()
bool fitFcn() const
void saveStatus(const char *label, int status)
~RooMinimizer() override
Destructor.
int minimize(const char *type, const char *alg=nullptr)
Minimise the function passed in the constructor.
void clearStatusHistory()
Clears the Minuit status history.
ROOT::Math::IMultiGenFunction * getMultiGenFcn() const
std::unique_ptr< RooAbsReal::EvalErrorContext > makeEvalErrorContext() const
void setVerbose(bool flag=true)
void setPrintEvalErrors(int numEvalErrors)
ROOT::Fit::Fitter * fitter()
Return underlying ROOT fitter object.
void setMaxFunctionCalls(int n)
Change maximum number of likelihood function class from MINUIT (RooMinimizer default 500 * #parameter...
void setStrategy(int istrat)
Change MINUIT strategy to istrat.
int evalCounter() const
TStopwatch _cumulTimer
int getNPar() const
void setMaxIterations(int n)
Change maximum number of MINUIT iterations (RooMinimizer default 500 * #parameters)
void addParamsToProcessTimer()
Add parameters in metadata field to process timer.
std::unique_ptr< RooAbsMinimizerFcn > _fcn
void applyCovarianceMatrix(TMatrixDSym const &V)
Apply results of given external covariance matrix.
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:45
Variable that can be changed from the outside.
Definition RooRealVar.h:37
Mother of all ROOT objects.
Definition TObject.h:41
Stopwatch class.
Definition TStopwatch.h:28
const Int_t n
Definition legend1.C:16
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
T * OwningPtr
An alias for raw pointers for indicating that the return type of a RooFit function is an owning point...
Definition Config.h:35
Config argument to RooMinimizer constructor.
std::string minimizerType