Logo ROOT  
Reference Guide
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 */
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 virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
87
88 /// set the function to minimize
89 virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
90
91 /// set free variable
92 virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step);
93
94 /// set upper/lower limited variable (override if minimizer supports them )
95 virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double /* lower */, double /* upper */);
96
97 /// set lower limit variable (override if minimizer supports them )
98 virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower );
99
100 /// set upper limit variable (override if minimizer supports them )
101 virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );
102
103 /// set fixed variable (override if minimizer supports them )
104 virtual bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */);
105
106 /// set the value of an existing variable
107 virtual bool SetVariableValue(unsigned int , double );
108
109 /// set the step size of an existing variable
110 virtual bool SetVariableStepSize(unsigned int , double );
111 /// set the lower-limit of an existing variable
112 virtual bool SetVariableLowerLimit(unsigned int , double );
113 /// set the upper-limit of an existing variable
114 virtual bool SetVariableUpperLimit(unsigned int , double );
115 /// set the limits of an existing variable
116 virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper);
117 /// fix an existing variable
118 virtual bool FixVariable(unsigned int);
119 /// release an existing variable
120 virtual bool ReleaseVariable(unsigned int);
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 virtual bool IsFixedVariable(unsigned int) const;
124 /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
125 virtual bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings & ) const;
126
127
128 /// method to perform the minimization
129 virtual bool Minimize();
130
131 /// return minimum function value
132 virtual double MinValue() const;
133
134 /// return expected distance reached from the minimum
135 virtual double Edm() const;
136
137 /// return pointer to X values at the minimum
138 virtual const double * X() const { return &fParams.front(); }
139
140 /// return pointer to gradient values at the minimum
141 virtual const double * MinGradient() const { return 0; } // not available in Minuit2
142
143 /// number of function calls to reach the minimum
144 virtual unsigned int NCalls() const;
145
146 /// this is <= Function().NDim() which is the total
147 /// number of variables (free+ constrained ones)
148 virtual unsigned int NDim() const { return fDim; }
149
150 /// number of free variables (real dimension of the problem)
151 /// this is <= Function().NDim() which is the total
152 virtual unsigned int NFree() const;
153
154 /// minimizer provides error and error matrix
155 virtual bool ProvidesError() const { return true; }
156
157 /// return errors at the minimum
158 virtual const double * Errors() const { 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 virtual double CovMatrix(unsigned int i, unsigned int j) const {
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 virtual bool GetCovMatrix(double * cov) const;
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 virtual bool GetHessianMatrix(double * h) const;
186
187 ///return status of covariance matrix
188 virtual int CovMatrixStatus() const;
189
190 ///global correlation coefficient for variable i
191 virtual double GlobalCC(unsigned int ) const;
192
193 /// minos error for variable i, return false if Minos failed
194 virtual bool GetMinosError(unsigned int i, double & errLow, double & errUp, int = 0);
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 virtual int MinosStatus() const { return fMinosStatus; }
201
202 /**
203 perform a full calculation of the Hessian matrix for error calculation
204 */
205 virtual bool Hesse();
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 virtual bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0);
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 virtual bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj);
218
219
220 virtual void PrintResults();
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 virtual std::string VariableName(unsigned int ivar) const;
227
228 /// get index of variable given a variable given a name
229 /// return always -1 . (It is Not implemented)
230 virtual int VariableIndex(const std::string & name) const;
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:325
char name[80]
Definition: TGX11.cxx:110
int type
Definition: TGX11.cxx:121
float xmin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
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:75
TMinuitMinimizer class: ROOT::Math::Minimizer implementation based on TMinuit.
virtual bool SetLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double, double)
set upper/lower limited variable (override if minimizer supports them )
TMinuitMinimizer(ROOT::Minuit::EMinimizerType type=ROOT::Minuit::kMigrad, unsigned int ndim=0)
Default constructor.
virtual bool GetMinosError(unsigned int i, double &errLow, double &errUp, int=0)
minos error for variable i, return false if Minos failed
virtual std::string VariableName(unsigned int ivar) const
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const;
virtual const double * X() const
return pointer to X values at the minimum
virtual const double * Errors() const
return errors at the minimum
virtual bool SetVariableStepSize(unsigned int, double)
set the step size of an existing variable
void RetrieveErrorMatrix()
retrieve error matrix from TMinuit
static TMinuit * fgMinuit
virtual bool Scan(unsigned int i, unsigned int &nstep, double *x, double *y, double xmin=0, double xmax=0)
scan a parameter i around the minimum.
static void Fcn(int &, double *, double &f, double *, int)
implementation of FCN for Minuit
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
virtual bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings &) const
get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
virtual bool Minimize()
method to perform the minimization
virtual bool SetVariableLowerLimit(unsigned int, double)
set the lower-limit of an existing variable
virtual unsigned int NCalls() const
number of function calls to reach the minimum
void RetrieveParams()
retrieve minimum parameters and errors from TMinuit
virtual bool GetHessianMatrix(double *h) const
Fill the passed array with the Hessian matrix elements The Hessian matrix is the matrix of the second...
std::vector< double > fErrors
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)
set free variable
virtual bool GetCovMatrix(double *cov) const
Fill the passed array with the covariance matrix elements if the variable is fixed or const the value...
bool CheckMinuitInstance() const
check TMinuit instance
virtual bool SetLowerLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double lower)
set lower limit variable (override if minimizer supports them )
~TMinuitMinimizer()
Destructor (no operations)
static bool fgUseStaticMinuit
virtual int CovMatrixStatus() const
return status of covariance matrix
std::vector< double > fCovar
virtual bool ReleaseVariable(unsigned int)
release an existing variable
bool CheckVarIndex(unsigned int ivar) const
check parameter
virtual int MinosStatus() const
minos status code of last Minos run minos status = -1 : Minos is not run = 0 : last MINOS run was suc...
virtual bool SetFixedVariable(unsigned int, const std::string &, double)
set fixed variable (override if minimizer supports them )
virtual double CovMatrix(unsigned int i, unsigned int j) const
return covariance matrices elements if the variable is fixed the matrix is zero The ordering of the v...
void SuppressMinuitWarnings(bool nowarn=true)
suppress the minuit warnings (if called with false will enable them) By default they are suppressed o...
virtual bool ProvidesError() const
minimizer provides error and error matrix
virtual void PrintResults()
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const ...
static bool UseStaticMinuit(bool on=true)
static function to switch on/off usage of static global TMinuit instance (gMinuit) By default it is u...
virtual bool IsFixedVariable(unsigned int) const
query if an existing variable is fixed (i.e.
virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper)
set the limits of an existing variable
virtual double GlobalCC(unsigned int) const
global correlation coefficient for variable i
virtual bool Contour(unsigned int i, unsigned int j, unsigned int &npoints, double *xi, double *xj)
find the contour points (xi,xj) of the function for parameter i and j around the minimum The contour ...
TMinuitMinimizer & operator=(const TMinuitMinimizer &rhs)
Assignment operator.
virtual double Edm() const
return expected distance reached from the minimum
virtual bool SetVariableValue(unsigned int, double)
set the value of an existing variable
void DoReleaseFixParameter(int ivar)
release a parameter that is fixed when it is redefined
virtual bool SetVariableUpperLimit(unsigned int, double)
set the upper-limit of an existing variable
virtual unsigned int NFree() const
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
virtual bool Hesse()
perform a full calculation of the Hessian matrix for error calculation
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
virtual double MinValue() const
return minimum function value
virtual bool FixVariable(unsigned int)
fix an existing variable
std::vector< double > fParams
void InitTMinuit(int ndim)
initialize the TMinuit instance
bool SetDebug(bool on=true)
set debug mode. Return true if setting was successfull
virtual bool SetUpperLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double upper)
set upper limit variable (override if minimizer supports them )
virtual int VariableIndex(const std::string &name) const
get index of variable given a variable given a name return always -1 .
virtual unsigned int NDim() const
this is <= Function().NDim() which is the total number of variables (free+ constrained ones)
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.