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