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
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;
42
43class RooMinimizer : public TObject {
44public:
45 /// Config argument to RooMinimizer ctor
46 struct Config {
47 Config() {}
48 double recoverFromNaN = 10.; // RooAbsMinimizerFcn config
49 int printEvalErrors = 10; // RooAbsMinimizerFcn config
50 int doEEWall = 1; // RooAbsMinimizerFcn config
51 int offsetting = -1; // RooAbsMinimizerFcn config
52 const char *logf = nullptr; // RooAbsMinimizerFcn config
53
54 // RooAbsMinimizerFcn config that can only be set in ctor, 0 means no parallelization (default),
55 // -1 is parallelization with the number of workers controlled by RooFit::MultiProcess which
56 // defaults to the number of available processors, n means parallelization with n CPU's
57 int parallelize = 0;
58
59 // Experimental: RooAbsMinimizerFcn config that can only be set in ctor
60 // argument is ignored when parallelize is 0
62
63 // Experimental: RooAbsMinimizerFcn config that can only be set in ctor
64 // argument is ignored when parallelize is 0
66
67 bool verbose = false; // local config
68 bool profile = false; // local config
69 bool timingAnalysis = false; // local config
70 std::string minimizerType = ""; // local config
71 private:
73 };
74
75 explicit RooMinimizer(RooAbsReal &function, Config const &cfg = {});
76
77 ~RooMinimizer() override;
78
79 enum Strategy { Speed = 0, Balance = 1, Robustness = 2 };
80 enum PrintLevel { None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3 };
81
82 // Setters on _theFitter
83 void setStrategy(int strat);
84 void setErrorLevel(double level);
85 void setEps(double eps);
86 void setMaxIterations(int n);
87 void setMaxFunctionCalls(int n);
88 void setPrintLevel(int newLevel);
89
90 // Setters on _fcn
91 void optimizeConst(int flag);
92 void setEvalErrorWall(bool flag) { _cfg.doEEWall = flag; }
93 void setRecoverFromNaNStrength(double strength);
94 void setOffsetting(bool flag);
95 void setPrintEvalErrors(int numEvalErrors) { _cfg.printEvalErrors = numEvalErrors; }
96 void setVerbose(bool flag = true) { _cfg.verbose = flag; }
97 bool setLogFile(const char *logf = nullptr);
98
99 int migrad();
100 int hesse();
101 int minos();
102 int minos(const RooArgSet &minosParamList);
103 int seek();
104 int simplex();
105 int improve();
106
107 int minimize(const char *type, const char *alg = nullptr);
108
109 RooFitResult *save(const char *name = nullptr, const char *title = nullptr);
110 RooPlot *contour(RooRealVar &var1, RooRealVar &var2, double n1 = 1.0, double n2 = 2.0, double n3 = 0.0,
111 double n4 = 0.0, double n5 = 0.0, double n6 = 0.0, unsigned int npoints = 50);
112
113 void setProfile(bool flag = true) { _cfg.profile = flag; }
114 /// Enable or disable the logging of function evaluations to a RooDataSet.
115 /// \see RooMinimizer::getLogDataSet().
116 /// param[in] flag Boolean flag to disable or enable the functionality.
117 void setLoggingToDataSet(bool flag = true) { _loggingToDataSet = flag; }
118
119 /// If logging of function evaluations to a RooDataSet is enabled, returns a
120 /// pointer to a dataset with one row per evaluation of the RooAbsReal passed
121 /// to the minimizer. As columns, there are all floating parameters and the
122 /// values they had for that evaluation.
123 /// \see RooMinimizer::setLoggingToDataSet(bool).
124 RooDataSet *getLogDataSet() const { return _logDataSet.get(); }
125
126 static int getPrintLevel();
127
128 void setMinimizerType(std::string const &type);
129 std::string const &minimizerType() const { return _cfg.minimizerType; }
130
131 static void cleanup();
132 static RooFitResult *lastMinuitFit();
133 static RooFitResult *lastMinuitFit(const RooArgList &varList);
134
135 void saveStatus(const char *label, int status)
136 {
137 _statusHistory.push_back(std::pair<std::string, int>(label, status));
138 }
139
140 int evalCounter() const;
141 void zeroEvalCount();
142
144 const ROOT::Fit::Fitter *fitter() const;
145
147
148 int getNPar() const;
149
150 void applyCovarianceMatrix(TMatrixDSym const &V);
151
152private:
153 friend class RooAbsMinimizerFcn;
154
156
157 void profileStart();
158 void profileStop();
159
160 std::ofstream *logfile();
161 double &maxFCN();
162
163 bool fitFcn() const;
164
165 // constructor helper functions
167 void initMinimizerFcnDependentPart(double defaultErrorLevel);
168
169 int _status = -99;
170 bool _profileStart = false;
171 bool _loggingToDataSet = false;
172
175
176 std::unique_ptr<TMatrixDSym> _extV;
177
178 std::unique_ptr<RooAbsMinimizerFcn> _fcn;
179
180 static std::unique_ptr<ROOT::Fit::Fitter> _theFitter;
181
182 std::vector<std::pair<std::string, int>> _statusHistory;
183
184 std::unique_ptr<RooDataSet> _logDataSet;
185
186 RooMinimizer::Config _cfg; // local config object
187
188 ClassDefOverride(RooMinimizer, 0) // RooFit interface to ROOT::Fit::Fitter
189};
190
191#endif
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
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:62
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:60
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:56
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
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:43
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
Definition: RooMinimizer.h:176
void zeroEvalCount()
void setMinimizerType(std::string const &type)
Choose the minimizer algorithm.
std::vector< std::pair< std::string, int > > _statusHistory
Definition: RooMinimizer.h:182
std::unique_ptr< RooDataSet > _logDataSet
Definition: RooMinimizer.h:184
void profileStart()
Start profiling timer.
void setProfile(bool flag=true)
Definition: RooMinimizer.h:113
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.
void setLoggingToDataSet(bool flag=true)
Enable or disable the logging of function evaluations to a RooDataSet.
Definition: RooMinimizer.h:117
void profileStop()
Stop profiling timer and report results of last session.
int minos()
Execute MINOS.
double & maxFCN()
int hesse()
Execute HESSE.
RooFitResult * save(const char *name=nullptr, const char *title=nullptr)
Save and return a RooFitResult snapshot of current minimizer status.
void setErrorLevel(double level)
Set the level for MINUIT error analysis to the given value.
static std::unique_ptr< ROOT::Fit::Fitter > _theFitter
Definition: RooMinimizer.h:180
void setEvalErrorWall(bool flag)
Definition: RooMinimizer.h:92
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
Definition: RooMinimizer.h:129
static RooFitResult * lastMinuitFit()
int improve()
Execute IMPROVE.
bool _loggingToDataSet
Definition: RooMinimizer.h:171
static void cleanup()
Cleanup method called by atexit handler installed by RooSentinel to delete all global heap objects wh...
bool _profileStart
Definition: RooMinimizer.h:170
void setOffsetting(bool flag)
Enable internal likelihood offsetting for enhanced numeric precision.
TStopwatch _timer
Definition: RooMinimizer.h:173
RooMinimizer::Config _cfg
Definition: RooMinimizer.h:186
RooDataSet * getLogDataSet() const
If logging of function evaluations to a RooDataSet is enabled, returns a pointer to a dataset with on...
Definition: RooMinimizer.h:124
bool fitFcn() const
void saveStatus(const char *label, int status)
Definition: RooMinimizer.h:135
~RooMinimizer() override
Destructor.
int minimize(const char *type, const char *alg=nullptr)
Minimise the function passed in the constructor.
ROOT::Math::IMultiGenFunction * getMultiGenFcn() const
void setStrategy(int strat)
Change MINUIT strategy to istrat.
void setVerbose(bool flag=true)
Definition: RooMinimizer.h:96
void setPrintEvalErrors(int numEvalErrors)
Definition: RooMinimizer.h:95
RooMinimizer(RooAbsReal &function, Config const &cfg={})
Construct MINUIT interface to given function.
ROOT::Fit::Fitter * fitter()
Return underlying ROOT fitter object.
void setMaxFunctionCalls(int n)
Change maximum number of likelihood function calss from MINUIT (RooMinimizer default 500 * #parameter...
int evalCounter() const
TStopwatch _cumulTimer
Definition: RooMinimizer.h:174
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
Definition: RooMinimizer.h:178
void applyCovarianceMatrix(TMatrixDSym const &V)
Apply results of given external covariance matrix.
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:43
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
Mother of all ROOT objects.
Definition: TObject.h:41
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:167
Config argument to RooMinimizer ctor.
Definition: RooMinimizer.h:46
const char * logf
Definition: RooMinimizer.h:52
std::string minimizerType
Definition: RooMinimizer.h:70