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 
23 class TMinuit;
24 
25 namespace ROOT {
26 
27  namespace Minuit {
28 
29 
30  // enumeration specifying the type of TMinuit minimizers
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 
53 public:
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 
70 private:
71  // usually copying is non trivial, so we make this unaccessible
72 
73  /**
74  Copy constructor
75  */
77 
78  /**
79  Assignment operator
80  */
82 
83 public:
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  /**
197  perform a full calculation of the Hessian matrix for error calculation
198  */
199  virtual bool Hesse();
200 
201  /**
202  scan a parameter i around the minimum. A minimization must have been done before,
203  return false if it is not the case
204  */
205  virtual bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0);
206 
207  /**
208  find the contour points (xi,xj) of the function for parameter i and j around the minimum
209  The contour will be find for value of the function = Min + ErrorUp();
210  */
211  virtual bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj);
212 
213 
214  virtual void PrintResults();
215 
216  /// return reference to the objective function
217  ///virtual const ROOT::Math::IGenFunction & Function() const;
218 
219  /// get name of variables (override if minimizer support storing of variable names)
220  virtual std::string VariableName(unsigned int ivar) const;
221 
222  /// get index of variable given a variable given a name
223  /// return always -1 . (It is Not implemented)
224  virtual int VariableIndex(const std::string & name) const;
225 
226  /// static function to switch on/off usage of static global TMinuit instance (gMinuit)
227  /// By default it is used (i.e. is on). Method returns the previous state
228  bool static UseStaticMinuit(bool on = true);
229 
230  /// suppress the minuit warnings (if called with false will enable them)
231  /// By default they are suppressed only when the printlevel is <= 0
232  void SuppressMinuitWarnings(bool nowarn=true);
233 
234  /// set debug mode. Return true if setting was successfull
235  bool SetDebug(bool on = true);
236 
237 protected:
238 
239  /// implementation of FCN for Minuit
240  static void Fcn( int &, double * , double & f, double * , int);
241  /// implementation of FCN for Minuit when user provided gradient is used
242  static void FcnGrad( int &, double * g, double & f, double * , int);
243 
244  /// initialize the TMinuit instance
245  void InitTMinuit(int ndim);
246 
247  /// reset
248  void DoClear();
249 
250  ///release a parameter that is fixed when it is redefined
251  void DoReleaseFixParameter( int ivar);
252 
253  /// retrieve minimum parameters and errors from TMinuit
254  void RetrieveParams();
255 
256  /// retrieve error matrix from TMinuit
257  void RetrieveErrorMatrix();
258 
259  /// check TMinuit instance
260  bool CheckMinuitInstance() const;
261 
262  ///check parameter
263  bool CheckVarIndex(unsigned int ivar) const;
264 
265 
266 private:
267 
268  bool fUsed;
269  bool fMinosRun;
270  unsigned int fDim;
271  std::vector<double> fParams; // vector of output values
272  std::vector<double> fErrors; // vector of output errors
273  std::vector<double> fCovar; // vector storing the covariance matrix
274 
277 
278  static TMinuit * fgMinuit;
279 
280  static bool fgUsed; // flag to control if static instance has done minimization
281  static bool fgUseStaticMinuit; // flag to control if using global TMInuit instance (gMinuit)
282 
283  ClassDef(TMinuitMinimizer,1) //Implementation of Minimizer interface using TMinuit
284 
285 };
286 
287 
288 
289 #endif /* ROOT_TMinuitMinimizer */
TMinuitMinimizer::CheckMinuitInstance
bool CheckMinuitInstance() const
check TMinuit instance
Definition: TMinuitMinimizer.cxx:316
TMinuitMinimizer::SetFunction
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
Definition: TMinuitMinimizer.cxx:205
Minimizer.h
TMinuitMinimizer::SuppressMinuitWarnings
void SuppressMinuitWarnings(bool nowarn=true)
suppress the minuit warnings (if called with false will enable them) By default they are suppressed o...
Definition: TMinuitMinimizer.cxx:891
TMinuitMinimizer::~TMinuitMinimizer
~TMinuitMinimizer()
Destructor (no operations)
Definition: TMinuitMinimizer.cxx:101
f
#define f(i)
Definition: RSha256.hxx:122
TMinuitMinimizer::PrintResults
virtual void PrintResults()
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const ...
Definition: TMinuitMinimizer.cxx:880
ROOT::Minuit::kSimplex
@ kSimplex
Definition: TMinuitMinimizer.h:33
TMinuitMinimizer::Edm
virtual double Edm() const
return expected distance reached from the minimum
Definition: TMinuitMinimizer.cxx:673
ROOT::Minuit::kCombined
@ kCombined
Definition: TMinuitMinimizer.h:34
ROOT::Minuit::kMigradImproved
@ kMigradImproved
Definition: TMinuitMinimizer.h:35
TMinuitMinimizer::DoClear
void DoClear()
reset
Definition: TMinuitMinimizer.cxx:844
xmax
float xmax
Definition: THbookFile.cxx:95
TMinuitMinimizer::ReleaseVariable
virtual bool ReleaseVariable(unsigned int)
release an existing variable
Definition: TMinuitMinimizer.cxx:422
ROOT::Minuit::kSeek
@ kSeek
Definition: TMinuitMinimizer.h:37
TMinuitMinimizer::Scan
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.
Definition: TMinuitMinimizer.cxx:950
TMinuitMinimizer::GetHessianMatrix
virtual bool GetHessianMatrix(double *h) const
Fill the passed array with the Hessian matrix elements The Hessian matrix is the matrix of the second...
Definition: TMinuitMinimizer.cxx:701
TMinuitMinimizer::Fcn
static void Fcn(int &, double *, double &f, double *, int)
implementation of FCN for Minuit
Definition: TMinuitMinimizer.cxx:250
TMinuitMinimizer::CovMatrixStatus
virtual int CovMatrixStatus() const
return status of covariance matrix
Definition: TMinuitMinimizer.cxx:740
TMinuitMinimizer::RetrieveErrorMatrix
void RetrieveErrorMatrix()
retrieve error matrix from TMinuit
Definition: TMinuitMinimizer.cxx:620
TMinuitMinimizer::fgUseStaticMinuit
static bool fgUseStaticMinuit
Definition: TMinuitMinimizer.h:281
x
Double_t x[n]
Definition: legend1.C:17
TMinuit
Definition: TMinuit.h:27
ROOT::Math::IGradientFunctionMultiDimTempl
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:326
TMinuitMinimizer::UseStaticMinuit
static bool UseStaticMinuit(bool on=true)
static function to switch on/off usage of static global TMinuit instance (gMinuit) By default it is u...
Definition: TMinuitMinimizer.cxx:123
TMinuitMinimizer::Contour
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 ...
Definition: TMinuitMinimizer.cxx:902
TMinuitMinimizer::SetVariableStepSize
virtual bool SetVariableStepSize(unsigned int, double)
set the step size of an existing variable
Definition: TMinuitMinimizer.cxx:368
TMinuitMinimizer::SetVariableValue
virtual bool SetVariableValue(unsigned int, double)
set the value of an existing variable
Definition: TMinuitMinimizer.cxx:354
TMinuitMinimizer::Errors
virtual const double * Errors() const
return errors at the minimum
Definition: TMinuitMinimizer.h:158
TMinuitMinimizer::SetVariableLowerLimit
virtual bool SetVariableLowerLimit(unsigned int, double)
set the lower-limit of an existing variable
Definition: TMinuitMinimizer.cxx:384
TMinuitMinimizer
TMinuitMinimizer class: ROOT::Math::Minimizer implementation based on TMinuit.
Definition: TMinuitMinimizer.h:51
TMinuitMinimizer::fType
ROOT::Minuit::EMinimizerType fType
Definition: TMinuitMinimizer.h:275
TMinuitMinimizer::GetMinosError
virtual bool GetMinosError(unsigned int i, double &errLow, double &errUp, int=0)
minos error for variable i, return false if Minos failed
Definition: TMinuitMinimizer.cxx:765
TMinuitMinimizer::fMinosRun
bool fMinosRun
Definition: TMinuitMinimizer.h:269
TMinuitMinimizer::SetVariableLimits
virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper)
set the limits of an existing variable
Definition: TMinuitMinimizer.cxx:397
TMinuitMinimizer::fgMinuit
static TMinuit * fgMinuit
Definition: TMinuitMinimizer.h:278
TMinuitMinimizer::SetLowerLimitedVariable
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 )
Definition: TMinuitMinimizer.cxx:301
TMinuitMinimizer::FcnGrad
static void FcnGrad(int &, double *g, double &f, double *, int)
implementation of FCN for Minuit when user provided gradient is used
Definition: TMinuitMinimizer.cxx:256
TMinuitMinimizer::MinValue
virtual double MinValue() const
return minimum function value
Definition: TMinuitMinimizer.cxx:663
TMinuitMinimizer::NDim
virtual unsigned int NDim() const
this is <= Function().NDim() which is the total number of variables (free+ constrained ones)
Definition: TMinuitMinimizer.h:148
TMinuitMinimizer::TMinuitMinimizer
TMinuitMinimizer(ROOT::Minuit::EMinimizerType type=ROOT::Minuit::kMigrad, unsigned int ndim=0)
Default constructor.
Definition: TMinuitMinimizer.cxx:57
TMinuitMinimizer::fParams
std::vector< double > fParams
Definition: TMinuitMinimizer.h:271
TMinuitMinimizer::fUsed
bool fUsed
Definition: TMinuitMinimizer.h:268
xmin
float xmin
Definition: THbookFile.cxx:95
h
#define h(i)
Definition: RSha256.hxx:124
TMinuitMinimizer::fCovar
std::vector< double > fCovar
Definition: TMinuitMinimizer.h:273
TMinuitMinimizer::CovMatrix
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...
Definition: TMinuitMinimizer.h:164
TMinuitMinimizer::NCalls
virtual unsigned int NCalls() const
number of function calls to reach the minimum
Definition: TMinuitMinimizer.cxx:657
TMinuitMinimizer::SetLimitedVariable
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 )
Definition: TMinuitMinimizer.cxx:285
TMinuitMinimizer::fErrors
std::vector< double > fErrors
Definition: TMinuitMinimizer.h:272
y
Double_t y[n]
Definition: legend1.C:17
TMinuitMinimizer::fDim
unsigned int fDim
Definition: TMinuitMinimizer.h:270
TMinuitMinimizer::SetVariable
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)
set free variable
Definition: TMinuitMinimizer.cxx:269
TMinuitMinimizer::MinGradient
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
Definition: TMinuitMinimizer.h:141
TMinuitMinimizer::fgUsed
static bool fgUsed
Definition: TMinuitMinimizer.h:280
TMinuitMinimizer::X
virtual const double * X() const
return pointer to X values at the minimum
Definition: TMinuitMinimizer.h:138
TMinuitMinimizer::Hesse
virtual bool Hesse()
perform a full calculation of the Hessian matrix for error calculation
Definition: TMinuitMinimizer.cxx:1022
TMinuitMinimizer::fMinuit
TMinuit * fMinuit
Definition: TMinuitMinimizer.h:276
TMinuitMinimizer::ProvidesError
virtual bool ProvidesError() const
minimizer provides error and error matrix
Definition: TMinuitMinimizer.h:155
ROOT::Minuit::EMinimizerType
EMinimizerType
Definition: TMinuitMinimizer.h:31
ROOT::Fit::ParameterSettings
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
Definition: ParameterSettings.h:46
TMinuitMinimizer::InitTMinuit
void InitTMinuit(int ndim)
initialize the TMinuit instance
Definition: TMinuitMinimizer.cxx:130
TMinuitMinimizer::GlobalCC
virtual double GlobalCC(unsigned int) const
global correlation coefficient for variable i
Definition: TMinuitMinimizer.cxx:753
ROOT::Minuit::kMigrad
@ kMigrad
Definition: TMinuitMinimizer.h:32
TMinuitMinimizer::VariableIndex
virtual int VariableIndex(const std::string &name) const
get index of variable given a variable given a name return always -1 .
Definition: TMinuitMinimizer.cxx:460
TMinuitMinimizer::SetUpperLimitedVariable
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 )
Definition: TMinuitMinimizer.cxx:308
ROOT::Minuit::kScan
@ kScan
Definition: TMinuitMinimizer.h:36
ROOT::Math::Minimizer
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition: Minimizer.h:80
TMinuitMinimizer::Minimize
virtual bool Minimize()
method to perform the minimization
Definition: TMinuitMinimizer.cxx:466
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TMinuitMinimizer::RetrieveParams
void RetrieveParams()
retrieve minimum parameters and errors from TMinuit
Definition: TMinuitMinimizer.cxx:606
name
char name[80]
Definition: TGX11.cxx:110
TMinuitMinimizer::IsFixedVariable
virtual bool IsFixedVariable(unsigned int) const
query if an existing variable is fixed (i.e.
Definition: TMinuitMinimizer.cxx:430
TMinuitMinimizer::operator=
TMinuitMinimizer & operator=(const TMinuitMinimizer &rhs)
Assignment operator.
Definition: TMinuitMinimizer.cxx:116
TMinuitMinimizer::VariableName
virtual std::string VariableName(unsigned int ivar) const
return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const;
Definition: TMinuitMinimizer.cxx:453
TMinuitMinimizer::FixVariable
virtual bool FixVariable(unsigned int)
fix an existing variable
Definition: TMinuitMinimizer.cxx:414
TMinuitMinimizer::SetVariableUpperLimit
virtual bool SetVariableUpperLimit(unsigned int, double)
set the upper-limit of an existing variable
Definition: TMinuitMinimizer.cxx:390
TMinuitMinimizer::GetVariableSettings
virtual bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings &) const
get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
Definition: TMinuitMinimizer.cxx:437
TMinuitMinimizer::DoReleaseFixParameter
void DoReleaseFixParameter(int ivar)
release a parameter that is fixed when it is redefined
Definition: TMinuitMinimizer.cxx:859
type
int type
Definition: TGX11.cxx:121
ROOT::Math::IBaseFunctionMultiDimTempl
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
TMinuitMinimizer::SetFixedVariable
virtual bool SetFixedVariable(unsigned int, const std::string &, double)
set fixed variable (override if minimizer supports them )
Definition: TMinuitMinimizer.cxx:335
TMinuitMinimizer::NFree
virtual unsigned int NFree() const
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
Definition: TMinuitMinimizer.cxx:682
TMinuitMinimizer::GetCovMatrix
virtual bool GetCovMatrix(double *cov) const
Fill the passed array with the covariance matrix elements if the variable is fixed or const the value...
Definition: TMinuitMinimizer.cxx:689
TMinuitMinimizer::CheckVarIndex
bool CheckVarIndex(unsigned int ivar) const
check parameter
Definition: TMinuitMinimizer.cxx:325
TMinuitMinimizer::SetDebug
bool SetDebug(bool on=true)
set debug mode. Return true if setting was successfull
Definition: TMinuitMinimizer.cxx:1067
Rtypes.h
ROOT
VSD Structures.
Definition: StringConv.hxx:21
g
#define g(i)
Definition: RSha256.hxx:123