Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMinuitMinimizer.h
Go to the documentation of this file.
1// @(#)root/minuit:$Id$
2// Author: L. Moneta Wed Oct 25 16:28:55 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Header file for class TMinuitMinimizer
12
13#ifndef ROOT_TMinuitMinimizer
14#define ROOT_TMinuitMinimizer
15
16#include "Math/Minimizer.h"
17
18#include "Rtypes.h"
19
20#include <vector>
21#include <string>
22
23class TMinuit;
24
25namespace ROOT {
26
27 namespace Minuit {
28
29
30 // enumeration specifying the type of TMinuit minimizers
37 kSeek
38 };
39
40 }
41}
42
43
44
45/**
46 TMinuitMinimizer class:
47 ROOT::Math::Minimizer implementation based on TMinuit
48
49 @ingroup TMinuit
50*/
52
53public:
54
55 /**
56 Default constructor
57 */
59
60 /**
61 Constructor from a char * (used by PM)
62 */
63 TMinuitMinimizer ( const char * type , unsigned int ndim = 0);
64
65 /**
66 Destructor (no operations)
67 */
68 ~TMinuitMinimizer () override;
69
70private:
71 // usually copying is non trivial, so we make this unaccessible
72
73 /**
74 Copy constructor
75 */
77
78 /**
79 Assignment operator
80 */
82
83public:
84
85 /// set the function to minimize
86 void SetFunction(const ROOT::Math::IMultiGenFunction & func) override;
87
88 /// set the function to minimize
89 void SetFunction(const ROOT::Math::IMultiGradFunction & func) override;
90
91 /// set free variable
92 bool SetVariable(unsigned int ivar, const std::string & name, double val, double step) override;
93
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 , double /* lower */, double /* upper */) override;
96
97 /// set lower limit variable (override if minimizer supports them )
98 bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower ) override;
99
100 /// set upper limit variable (override if minimizer supports them )
101 bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper ) override;
102
103 /// set fixed variable (override if minimizer supports them )
104 bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */) override;
105
106 /// set the value of an existing variable
107 bool SetVariableValue(unsigned int , double ) override;
108
109 /// set the step size of an existing variable
110 bool SetVariableStepSize(unsigned int , double ) override;
111 /// set the lower-limit of an existing variable
112 bool SetVariableLowerLimit(unsigned int , double ) override;
113 /// set the upper-limit of an existing variable
114 bool SetVariableUpperLimit(unsigned int , double ) override;
115 /// set the limits of an existing variable
116 bool SetVariableLimits(unsigned int ivar, double lower, double upper) override;
117 /// fix an existing variable
118 bool FixVariable(unsigned int) override;
119 /// release an existing variable
120 bool ReleaseVariable(unsigned int) override;
121 /// query if an existing variable is fixed (i.e. considered constant in the minimization)
122 /// note that by default all variables are not fixed
123 bool IsFixedVariable(unsigned int) const override;
124 /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
125 bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings & ) const override;
126
127
128 /// method to perform the minimization
129 bool Minimize() override;
130
131 /// return minimum function value
132 double MinValue() const override;
133
134 /// return expected distance reached from the minimum
135 double Edm() const override;
136
137 /// return pointer to X values at the minimum
138 const double * X() const override { return &fParams.front(); }
139
140 /// return pointer to gradient values at the minimum
141 const double * MinGradient() const override { return nullptr; } // not available in Minuit2
142
143 /// number of function calls to reach the minimum
144 unsigned int NCalls() const override;
145
146 /// this is <= Function().NDim() which is the total
147 /// number of variables (free+ constrained ones)
148 unsigned int NDim() const override { return fDim; }
149
150 /// number of free variables (real dimension of the problem)
151 /// this is <= Function().NDim() which is the total
152 unsigned int NFree() const override;
153
154 /// minimizer provides error and error matrix
155 bool ProvidesError() const override { return true; }
156
157 /// return errors at the minimum
158 const double * Errors() const override { return &fErrors.front(); }
159
160 /** return covariance matrices elements
161 if the variable is fixed the matrix is zero
162 The ordering of the variables is the same as in errors
163 */
164 double CovMatrix(unsigned int i, unsigned int j) const override {
165 return ( fCovar.size() > (i + fDim* j) ) ? fCovar[i + fDim* j] : 0;
166 }
167
168 /**
169 Fill the passed array with the covariance matrix elements
170 if the variable is fixed or const the value is zero.
171 The array will be filled as cov[i *ndim + j]
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 bool GetCovMatrix(double * cov) const override;
177
178 /**
179 Fill the passed array with the Hessian matrix elements
180 The Hessian matrix is the matrix of the second derivatives
181 and is the inverse of the covariance matrix
182 If the variable is fixed or const the values for that variables are zero.
183 The array will be filled as h[i *ndim + j]
184 */
185 bool GetHessianMatrix(double * h) const override;
186
187 ///return status of covariance matrix
188 int CovMatrixStatus() const override;
189
190 ///global correlation coefficient for variable i
191 double GlobalCC(unsigned int ) const override;
192
193 /// minos error for variable i, return false if Minos failed
194 bool GetMinosError(unsigned int i, double & errLow, double & errUp, int = 0) override;
195
196 /// minos status code of last Minos run
197 /// minos status = -1 : Minos is not run
198 /// = 0 : last MINOS run was succesfull
199 /// > 0 : some problems encountered when running MINOS
200 int MinosStatus() const override { return fMinosStatus; }
201
202 /**
203 perform a full calculation of the Hessian matrix for error calculation
204 */
205 bool Hesse() override;
206
207 /**
208 scan a parameter i around the minimum. A minimization must have been done before,
209 return false if it is not the case
210 */
211 bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0) override;
212
213 /**
214 find the contour points (xi,xj) of the function for parameter i and j around the minimum
215 The contour will be find for value of the function = Min + ErrorUp();
216 */
217 bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj) override;
218
219
220 void PrintResults() override;
221
222 /// return reference to the objective function
223 ///virtual const ROOT::Math::IGenFunction & Function() const;
224
225 /// get name of variables (override if minimizer support storing of variable names)
226 std::string VariableName(unsigned int ivar) const override;
227
228 /// get index of variable given a variable given a name
229 /// return always -1 . (It is Not implemented)
230 int VariableIndex(const std::string & name) const override;
231
232 /// static function to switch on/off usage of static global TMinuit instance (gMinuit)
233 /// By default it is used (i.e. is on). Method returns the previous state
234 bool static UseStaticMinuit(bool on = true);
235
236 /// suppress the minuit warnings (if called with false will enable them)
237 /// By default they are suppressed only when the printlevel is <= 0
238 void SuppressMinuitWarnings(bool nowarn=true);
239
240 /// set debug mode. Return true if setting was successfull
241 bool SetDebug(bool on = true);
242
243protected:
244
245 /// implementation of FCN for Minuit
246 static void Fcn( int &, double * , double & f, double * , int);
247 /// implementation of FCN for Minuit when user provided gradient is used
248 static void FcnGrad( int &, double * g, double & f, double * , int);
249
250 /// initialize the TMinuit instance
251 void InitTMinuit(int ndim);
252
253 /// reset
254 void DoClear();
255
256 ///release a parameter that is fixed when it is redefined
257 void DoReleaseFixParameter( int ivar);
258
259 /// retrieve minimum parameters and errors from TMinuit
260 void RetrieveParams();
261
262 /// retrieve error matrix from TMinuit
263 void RetrieveErrorMatrix();
264
265 /// check TMinuit instance
266 bool CheckMinuitInstance() const;
267
268 ///check parameter
269 bool CheckVarIndex(unsigned int ivar) const;
270
271
272private:
273
274 bool fUsed;
276 unsigned int fDim;
277 int fMinosStatus = -1; // Minos status code
278 std::vector<double> fParams; // vector of output values
279 std::vector<double> fErrors; // vector of output errors
280 std::vector<double> fCovar; // vector storing the covariance matrix
281
284
286
287 static bool fgUsed; // flag to control if static instance has done minimization
288 static bool fgUseStaticMinuit; // flag to control if using global TMInuit instance (gMinuit)
289
290 ClassDef(TMinuitMinimizer,1) //Implementation of Minimizer interface using TMinuit
291
292};
293
294
295
296#endif /* ROOT_TMinuitMinimizer */
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
#define ClassDef(name, id)
Definition Rtypes.h:337
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
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:62
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition IFunction.h:343
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition Minimizer.h:78
TMinuitMinimizer class: ROOT::Math::Minimizer implementation based on TMinuit.
bool FixVariable(unsigned int) override
fix an existing variable
void RetrieveErrorMatrix()
retrieve error matrix from TMinuit
const double * Errors() const override
return errors at the minimum
bool SetFixedVariable(unsigned int, const std::string &, double) override
set fixed variable (override if minimizer supports them )
static TMinuit * fgMinuit
bool SetVariableLimits(unsigned int ivar, double lower, double upper) override
set the limits of an existing variable
static void Fcn(int &, double *, double &f, double *, int)
implementation of FCN for Minuit
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
bool ReleaseVariable(unsigned int) override
release an existing variable
static void FcnGrad(int &, double *g, double &f, double *, int)
implementation of FCN for Minuit when user provided gradient is used
ROOT::Minuit::EMinimizerType fType
double CovMatrix(unsigned int i, unsigned int j) const override
return covariance matrices elements if the variable is fixed the matrix is zero The ordering of the v...
bool SetVariableLowerLimit(unsigned int, double) override
set the lower-limit of an existing variable
int VariableIndex(const std::string &name) const override
get index of variable given a variable given a name return always -1 .
double MinValue() const override
return minimum function value
bool SetVariableStepSize(unsigned int, double) override
set the step size of an existing variable
void RetrieveParams()
retrieve minimum parameters and errors from TMinuit
~TMinuitMinimizer() override
Destructor (no operations)
const double * MinGradient() const override
return pointer to gradient values at the minimum
std::vector< double > fErrors
int MinosStatus() const override
minos status code of last Minos run minos status = -1 : Minos is not run = 0 : last MINOS run was suc...
void SetFunction(const ROOT::Math::IMultiGenFunction &func) override
set the function to minimize
bool CheckMinuitInstance() const
check TMinuit instance
bool ProvidesError() const override
minimizer provides error and error matrix
static bool fgUseStaticMinuit
std::vector< double > fCovar
bool Hesse() override
perform a full calculation of the Hessian matrix for error calculation
unsigned int NDim() const override
this is <= Function().NDim() which is the total number of variables (free+ constrained ones)
bool CheckVarIndex(unsigned int ivar) const
check parameter
bool Minimize() override
method to perform the minimization
bool SetVariableValue(unsigned int, double) override
set the value of an existing variable
int CovMatrixStatus() const override
return status of covariance matrix
void SuppressMinuitWarnings(bool nowarn=true)
suppress the minuit warnings (if called with false will enable them) By default they are suppressed o...
bool SetVariableUpperLimit(unsigned int, double) override
set the upper-limit of an existing variable
double Edm() const override
return expected distance reached from the minimum
static bool UseStaticMinuit(bool on=true)
static function to switch on/off usage of static global TMinuit instance (gMinuit) By default it is u...
void PrintResults() override
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const ...
unsigned int NCalls() const override
number of function calls to reach the minimum
TMinuitMinimizer & operator=(const TMinuitMinimizer &rhs)
Assignment operator.
void DoReleaseFixParameter(int ivar)
release a parameter that is fixed when it is redefined
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 GetMinosError(unsigned int i, double &errLow, double &errUp, int=0) override
minos error for variable i, return false if Minos failed
bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings &) const override
get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
double GlobalCC(unsigned int) const override
global correlation coefficient for variable i
const double * X() const override
return pointer to X values at the minimum
bool IsFixedVariable(unsigned int) const override
query if an existing variable is fixed (i.e.
std::string VariableName(unsigned int ivar) const override
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const;
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 )
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 )
unsigned int NFree() const override
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
std::vector< double > fParams
void InitTMinuit(int ndim)
initialize the TMinuit instance
bool GetHessianMatrix(double *h) const override
Fill the passed array with the Hessian matrix elements The Hessian matrix is the matrix of the second...
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.
bool SetDebug(bool on=true)
set debug mode. Return true if setting was successfull
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 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 ...
Implementation in C++ of the Minuit package written by Fred James.
Definition TMinuit.h:27
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.