Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Minuit2Minimizer.h
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Author: L. Moneta Wed Oct 18 11:48:00 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for class Minuit2Minimizer
12
13#ifndef ROOT_Minuit2_Minuit2Minimizer
14#define ROOT_Minuit2_Minuit2Minimizer
15
16#include "Math/Minimizer.h"
17
19
20#include "Math/IFunctionfwd.h"
21
22#include <vector>
23#include <string>
24#include <functional>
25
26namespace ROOT {
27
28namespace Minuit2 {
29
30class ModularFunctionMinimizer;
31class FCNBase;
32class FunctionMinimum;
33class MnTraceObject;
34
35// enumeration specifying the type of Minuit2 minimizers
37
38} // namespace Minuit2
39
40namespace Minuit2 {
41//_____________________________________________________________________________________________________
42/**
43 Minuit2Minimizer class implementing the ROOT::Math::Minimizer interface for
44 Minuit2 minimization algorithm.
45 In ROOT it can be instantiated using the plug-in manager (plug-in "Minuit2")
46 Using a string (used by the plugin manager) or via an enumeration
47 an one can set all the possible minimization algorithms (Migrad, Simplex, Combined, Scan and Fumili).
48
49 Refer to the [guide](https://root.cern.ch/doc/master/Minuit2Page.html) for an introduction how Minuit2
50 works.
51
52 \note Some gradient calculations, such as `Numerical2PGradientCalculator` support parallelization
53 via OpenMP. To profit from this acceleration, one needs to build ROOT using `minuit2_omp=ON`
54 and later call GradientCalculator::SetParallelOMP()
55
56 @ingroup Minuit
57*/
59
60public:
61 /**
62 Default constructor
63 */
65
66 /**
67 Constructor with a char (used by PM)
68 */
69 Minuit2Minimizer(const char *type);
70
71 /**
72 Destructor (no operations)
73 */
74 ~Minuit2Minimizer() override;
75
76 // clear resources (parameters) for consecutives minimizations
77 void Clear() override;
78
79 /// set the function to minimize
80 void SetFunction(const ROOT::Math::IMultiGenFunction &func) override;
81
82 /// set the function implementing Hessian computation
83 void SetHessianFunction(std::function<bool(std::span<const double>, double *)> hfunc) override;
84
85 /// set free variable
86 bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override;
87
88 /// set lower limit variable (override if minimizer supports them )
89 bool
90 SetLowerLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double lower) override;
91 /// set upper limit variable (override if minimizer supports them )
92 bool
93 SetUpperLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double upper) override;
94 /// set upper/lower limited variable (override if minimizer supports them )
95 bool SetLimitedVariable(unsigned int ivar, const std::string &name, double val, double step,
96 double /* lower */, double /* upper */) override;
97 /// set fixed variable (override if minimizer supports them )
98 bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */) override;
99 /// set variable
100 bool SetVariableValue(unsigned int ivar, double val) override;
101 // set variable values
102 bool SetVariableValues(const double *val) override;
103 /// set the step size of an already existing variable
104 bool SetVariableStepSize(unsigned int ivar, double step) override;
105 /// set the lower-limit of an already existing variable
106 bool SetVariableLowerLimit(unsigned int ivar, double lower) override;
107 /// set the upper-limit of an already existing variable
108 bool SetVariableUpperLimit(unsigned int ivar, double upper) override;
109 /// set the limits of an already existing variable
110 bool SetVariableLimits(unsigned int ivar, double lower, double upper) override;
111 /// fix an existing variable
112 bool FixVariable(unsigned int ivar) override;
113 /// release an existing variable
114 bool ReleaseVariable(unsigned int ivar) override;
115 /// query if an existing variable is fixed (i.e. considered constant in the minimization)
116 /// note that by default all variables are not fixed
117 bool IsFixedVariable(unsigned int ivar) const override;
118 /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
119 bool GetVariableSettings(unsigned int ivar, ROOT::Fit::ParameterSettings &varObj) const override;
120 /// get name of variables (override if minimizer support storing of variable names)
121 std::string VariableName(unsigned int ivar) const override;
122 /// get index of variable given a variable given a name
123 /// return -1 if variable is not found
124 int VariableIndex(const std::string &name) const override;
125
126 /**
127 method to perform the minimization.
128 Return false in case the minimization did not converge. In this case a
129 status code different than zero is set
130 (retrieved by the derived method Minimizer::Status() )"
131
132 status = 1 : Covariance was made pos defined
133 status = 2 : Hesse is invalid
134 status = 3 : Edm is above max
135 status = 4 : Reached call limit
136 status = 5 : Any other failure
137 */
138 bool Minimize() override;
139
140 /// return minimum function value
141 double MinValue() const override { return fState.Fval(); }
142
143 /// return expected distance reached from the minimum
144 double Edm() const override { return fState.Edm(); }
145
146 /// return pointer to X values at the minimum
147 const double *X() const override { return &fValues.front(); }
148
149 /// return pointer to gradient values at the minimum
150 const double *MinGradient() const override { return nullptr; } // not available in Minuit2
151
152 /// number of function calls to reach the minimum
153 unsigned int NCalls() const override { return fState.NFcn(); }
154
155 /// this is <= Function().NDim() which is the total
156 /// number of variables (free+ constrained ones)
157 unsigned int NDim() const override { return fDim; }
158
159 /// number of free variables (real dimension of the problem)
160 /// this is <= Function().NDim() which is the total
161 unsigned int NFree() const override { return fState.VariableParameters(); }
162
163 /// minimizer provides error and error matrix
164 bool ProvidesError() const override { return true; }
165
166 /// return errors at the minimum
167 const double *Errors() const override;
168
169 /**
170 return covariance matrix elements
171 if the variable is fixed or const the value is zero
172 The ordering of the variables is the same as in errors and parameter value.
173 This is different from the direct interface of Minuit2 or TMinuit where the
174 values were obtained only to variable parameters
175 */
176 double CovMatrix(unsigned int i, unsigned int j) const override;
177
178 /**
179 Fill the passed array with the covariance matrix elements
180 if the variable is fixed or const the value is zero.
181 The array will be filled as cov[i *ndim + j]
182 The ordering of the variables is the same as in errors and parameter value.
183 This is different from the direct interface of Minuit2 or TMinuit where the
184 values were obtained only to variable parameters
185 */
186 bool GetCovMatrix(double *cov) const override;
187
188 /**
189 Fill the passed array with the Hessian matrix elements
190 The Hessian matrix is the matrix of the second derivatives
191 and is the inverse of the covariance matrix
192 If the variable is fixed or const the values for that variables are zero.
193 The array will be filled as h[i *ndim + j]
194 */
195 bool GetHessianMatrix(double *h) const override;
196
197 /**
198 return the status of the covariance matrix
199 status = -1 : not available (inversion failed or Hesse failed)
200 status = 0 : available but not positive defined
201 status = 1 : covariance only approximate
202 status = 2 : full matrix but forced pos def
203 status = 3 : full accurate matrix
204
205 */
206 int CovMatrixStatus() const override;
207 /**
208 return correlation coefficient between variable i and j.
209 If the variable is fixed or const the return value is zero
210 */
211 double Correlation(unsigned int i, unsigned int j) const override;
212
213 /**
214 get global correlation coefficient for the variable i. This is a number between zero and one which gives
215 the correlation between the i-th variable and that linear combination of all other variables which
216 is most strongly correlated with i.
217 If the variable is fixed or const the return value is zero
218 */
219 double GlobalCC(unsigned int i) const override;
220
221 /**
222 get the minos error for parameter i, return false if Minos failed
223 A minimizaiton must be performed befre, return false if no minimization has been done
224 In case of Minos failed the status error is updated as following
225 status += 10 * minosStatus.
226 The Minos status of last Minos run can also be retrieved by calling MinosStatus()
227 */
228 bool GetMinosError(unsigned int i, double &errLow, double &errUp, int = 0) override;
229
230 /**
231 MINOS status code of last Minos run
232 `status & 1 > 0` : invalid lower error
233 `status & 2 > 0` : invalid upper error
234 `status & 4 > 0` : invalid because maximum number of function calls exceeded
235 `status & 8 > 0` : a new minimum has been found
236 `status & 16 > 0` : error is truncated because parameter is at lower/upper limit
237 */
238 int MinosStatus() const override { return fMinosStatus; }
239
240 /**
241 scan a parameter i around the minimum. A minimization must have been done before,
242 return false if it is not the case
243 */
244 bool Scan(unsigned int i, unsigned int &nstep, double *x, double *y, double xmin = 0, double xmax = 0) override;
245
246 /**
247 find the contour points (xi,xj) of the function for parameter i and j around the minimum
248 The contour will be find for value of the function = Min + ErrorUp();
249 */
250 bool Contour(unsigned int i, unsigned int j, unsigned int &npoints, double *xi, double *xj) override;
251
252 /**
253 perform a full calculation of the Hessian matrix for error calculation
254 If a valid minimum exists the calculation is done on the minimum point otherwise is performed
255 in the current set values of parameters
256 Status code of minimizer is updated according to the following convention (in case Hesse failed)
257 status += 100*hesseStatus where hesse status is:
258 status = 1 : hesse failed
259 status = 2 : matrix inversion failed
260 status = 3 : matrix is not pos defined
261 */
262 bool Hesse() override;
263
264 /// return reference to the objective function
265 /// virtual const ROOT::Math::IGenFunction & Function() const;
266
267 /// print result of minimization
268 void PrintResults() override;
269
270 /// set an object to trace operation for each iteration
271 /// The object must be a (or inherit from) ROOT::Minuit2::MnTraceObject and implement operator() (int, const
272 /// MinimumState & state)
273 void SetTraceObject(MnTraceObject &obj);
274
275 /// set storage level = 1 : store all iteration states (default)
276 /// = 0 : store only first and last state to save memory
277 void SetStorageLevel(int level);
278
279 /// return the minimizer state (containing values, step size , etc..)
281
282protected:
283 // protected function for accessing the internal Minuit2 object. Needed for derived classes
284
286
288
290
291 virtual const ROOT::Minuit2::FCNBase *GetFCN() const { return fMinuitFCN; }
292
293 /// examine the minimum result
295
296 // internal function to compute Minos errors
297 int RunMinosError(unsigned int i, double &errLow, double &errUp, int runopt);
298
299 /// set initial second derivatives
300 virtual bool SetCovarianceDiag(std::span<const double> d2, unsigned int n) override;
301
302 /// set initial covariance matrix
303 bool SetCovariance(std::span<const double> cov, unsigned int nrow) override;
304
305private:
306 unsigned int fDim; // dimension of the function to be minimized
308 int fMinosStatus = -1; // Minos status code
309
311 // std::vector<ROOT::Minuit2::MinosError> fMinosErrors;
315 mutable std::vector<double> fValues;
316 mutable std::vector<double> fErrors;
317};
318
319} // namespace Minuit2
320
321} // end namespace ROOT
322
323#endif /* ROOT_Minuit2_Minuit2Minimizer */
#define h(i)
Definition RSha256.hxx:106
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
float xmin
float xmax
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:63
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition Minimizer.h:124
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
Definition FCNBase.h:49
class holding the full result of the minimization; both internal and external (MnUserParameterState) ...
Minuit2Minimizer class implementing the ROOT::Math::Minimizer interface for Minuit2 minimization algo...
bool ExamineMinimum(const ROOT::Minuit2::FunctionMinimum &min)
examine the minimum result
Minuit2Minimizer(ROOT::Minuit2::EMinimizerType type=ROOT::Minuit2::kMigrad)
Default constructor.
const ROOT::Minuit2::MnUserParameterState & State()
return the minimizer state (containing values, step size , etc..)
void SetStorageLevel(int level)
set storage level = 1 : store all iteration states (default) = 0 : store only first and last state to...
bool SetCovariance(std::span< const double > cov, unsigned int nrow) override
set initial covariance matrix
bool SetLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double, double) override
set upper/lower limited variable (override if minimizer supports them )
bool Contour(unsigned int i, unsigned int j, unsigned int &npoints, double *xi, double *xj) override
find the contour points (xi,xj) of the function for parameter i and j around the minimum The contour ...
virtual bool SetCovarianceDiag(std::span< const double > d2, unsigned int n) override
set initial second derivatives
bool IsFixedVariable(unsigned int ivar) const override
query if an existing variable is fixed (i.e.
bool SetVariableUpperLimit(unsigned int ivar, double upper) override
set the upper-limit of an already existing variable
unsigned int NCalls() const override
number of function calls to reach the minimum
double GlobalCC(unsigned int i) const override
get global correlation coefficient for the variable i.
bool SetVariableValues(const double *val) override
Set the values of all existing variables (array must be dimensioned to the size of the existing param...
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
virtual const ROOT::Minuit2::FCNBase * GetFCN() const
const double * X() const override
return pointer to X values at the minimum
const double * Errors() const override
return errors at the minimum
void SetFunction(const ROOT::Math::IMultiGenFunction &func) override
set the function to minimize
double MinValue() const override
return minimum function value
bool SetVariableStepSize(unsigned int ivar, double step) override
set the step size of an already existing variable
bool GetCovMatrix(double *cov) const override
Fill the passed array with the covariance matrix elements if the variable is fixed or const the value...
bool ReleaseVariable(unsigned int ivar) override
release an existing variable
bool GetVariableSettings(unsigned int ivar, ROOT::Fit::ParameterSettings &varObj) const override
get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
bool Hesse() override
perform a full calculation of the Hessian matrix for error calculation If a valid minimum exists the ...
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
bool GetMinosError(unsigned int i, double &errLow, double &errUp, int=0) override
get the minos error for parameter i, return false if Minos failed A minimizaiton must be performed be...
std::string VariableName(unsigned int ivar) const override
get name of variables (override if minimizer support storing of variable names)
int RunMinosError(unsigned int i, double &errLow, double &errUp, int runopt)
bool SetVariableLimits(unsigned int ivar, double lower, double upper) override
set the limits of an already existing variable
bool ProvidesError() const override
minimizer provides error and error matrix
double Correlation(unsigned int i, unsigned int j) const override
return correlation coefficient between variable i and j.
bool SetLowerLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double lower) override
set lower limit variable (override if minimizer supports them )
void SetTraceObject(MnTraceObject &obj)
set an object to trace operation for each iteration The object must be a (or inherit from) ROOT::Minu...
virtual const ROOT::Minuit2::ModularFunctionMinimizer * GetMinimizer() const
unsigned int NFree() const override
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
double CovMatrix(unsigned int i, unsigned int j) const override
return covariance matrix elements if the variable is fixed or const the value is zero The ordering of...
void SetMinimizerType(ROOT::Minuit2::EMinimizerType type)
bool SetVariableValue(unsigned int ivar, double val) override
set variable
bool Scan(unsigned int i, unsigned int &nstep, double *x, double *y, double xmin=0, double xmax=0) override
scan a parameter i around the minimum.
int VariableIndex(const std::string &name) const override
get index of variable given a variable given a name return -1 if variable is not found
int MinosStatus() const override
MINOS status code of last Minos run status & 1 > 0 : invalid lower error status & 2 > 0 : invalid upp...
bool SetUpperLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double upper) override
set upper limit variable (override if minimizer supports them )
ROOT::Minuit2::MnUserParameterState fState
unsigned int NDim() const override
this is <= Function().NDim() which is the total number of variables (free+ constrained ones)
bool SetVariableLowerLimit(unsigned int ivar, double lower) override
set the lower-limit of an already existing variable
bool FixVariable(unsigned int ivar) override
fix an existing variable
virtual void SetMinimizer(ROOT::Minuit2::ModularFunctionMinimizer *m)
double Edm() const override
return expected distance reached from the minimum
void SetHessianFunction(std::function< bool(std::span< const double >, double *)> hfunc) override
set the function implementing Hessian computation
bool Minimize() override
method to perform the minimization.
ROOT::Minuit2::FunctionMinimum * fMinimum
bool SetFixedVariable(unsigned int, const std::string &, double) override
set fixed variable (override if minimizer supports them )
bool GetHessianMatrix(double *h) const override
Fill the passed array with the Hessian matrix elements The Hessian matrix is the matrix of the second...
ROOT::Minuit2::FCNBase * fMinuitFCN
const double * MinGradient() const override
return pointer to gradient values at the minimum
void PrintResults() override
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const;
void Clear() override
Reset for consecutive minimization - implement if needed.
~Minuit2Minimizer() override
Destructor (no operations)
int CovMatrixStatus() const override
return the status of the covariance matrix status = -1 : not available (inversion failed or Hesse fai...
class which holds the external user and/or internal Minuit representation of the parameters and error...
Base common class providing the API for all the minimizer Various Minimize methods are provided varyi...
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Namespace for new ROOT classes and functions.
TMarker m
Definition textangle.C:8