Logo ROOT   6.08/07
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 #ifndef ROOT_Math_Minimizer
17 #include "Math/Minimizer.h"
18 #endif
19 
20 #ifndef ROOT_Rtypes
21 #include "Rtypes.h"
22 #endif
23 
24 class TMinuit;
25 
26 
27 
28 namespace ROOT {
29 
30  namespace Minuit {
31 
32 
33  // enumeration specifying the type of TMinuit minimizers
41  };
42 
43  }
44 }
45 
46 
47 
48 /**
49  TMinuitMinimizer class:
50  ROOT::Math::Minimizer implementation based on TMinuit
51 
52  @ingroup TMinuit
53 */
55 
56 public:
57 
58  /**
59  Default constructor
60  */
62 
63  /**
64  Constructor from a char * (used by PM)
65  */
66  TMinuitMinimizer ( const char * type , unsigned int ndim = 0);
67 
68  /**
69  Destructor (no operations)
70  */
71  ~TMinuitMinimizer ();
72 
73 private:
74  // usually copying is non trivial, so we make this unaccessible
75 
76  /**
77  Copy constructor
78  */
80 
81  /**
82  Assignment operator
83  */
84  TMinuitMinimizer & operator = (const TMinuitMinimizer & rhs);
85 
86 public:
87 
88  /// set the function to minimize
89  virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
90 
91  /// set the function to minimize
92  virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
93 
94  /// set free variable
95  virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step);
96 
97  /// set upper/lower limited variable (override if minimizer supports them )
98  virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double /* lower */, double /* upper */);
99 
100  /// set lower limit variable (override if minimizer supports them )
101  virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower );
102 
103  /// set upper limit variable (override if minimizer supports them )
104  virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );
105 
106  /// set fixed variable (override if minimizer supports them )
107  virtual bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */);
108 
109  /// set the value of an existing variable
110  virtual bool SetVariableValue(unsigned int , double );
111 
112  /// set the step size of an existing variable
113  virtual bool SetVariableStepSize(unsigned int , double );
114  /// set the lower-limit of an existing variable
115  virtual bool SetVariableLowerLimit(unsigned int , double );
116  /// set the upper-limit of an existing variable
117  virtual bool SetVariableUpperLimit(unsigned int , double );
118  /// set the limits of an existing variable
119  virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper);
120  /// fix an existing variable
121  virtual bool FixVariable(unsigned int);
122  /// release an existing variable
123  virtual bool ReleaseVariable(unsigned int);
124  /// query if an existing variable is fixed (i.e. considered constant in the minimization)
125  /// note that by default all variables are not fixed
126  virtual bool IsFixedVariable(unsigned int) const;
127  /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
128  virtual bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings & ) const;
129 
130 
131  /// method to perform the minimization
132  virtual bool Minimize();
133 
134  /// return minimum function value
135  virtual double MinValue() const;
136 
137  /// return expected distance reached from the minimum
138  virtual double Edm() const;
139 
140  /// return pointer to X values at the minimum
141  virtual const double * X() const { return &fParams.front(); }
142 
143  /// return pointer to gradient values at the minimum
144  virtual const double * MinGradient() const { return 0; } // not available in Minuit2
145 
146  /// number of function calls to reach the minimum
147  virtual unsigned int NCalls() const;
148 
149  /// this is <= Function().NDim() which is the total
150  /// number of variables (free+ constrained ones)
151  virtual unsigned int NDim() const { return fDim; }
152 
153  /// number of free variables (real dimension of the problem)
154  /// this is <= Function().NDim() which is the total
155  virtual unsigned int NFree() const;
156 
157  /// minimizer provides error and error matrix
158  virtual bool ProvidesError() const { return true; }
159 
160  /// return errors at the minimum
161  virtual const double * Errors() const { return &fErrors.front(); }
162 
163  /** return covariance matrices elements
164  if the variable is fixed the matrix is zero
165  The ordering of the variables is the same as in errors
166  */
167  virtual double CovMatrix(unsigned int i, unsigned int j) const {
168  return ( fCovar.size() > (i + fDim* j) ) ? fCovar[i + fDim* j] : 0;
169  }
170 
171  /**
172  Fill the passed array with the covariance matrix elements
173  if the variable is fixed or const the value is zero.
174  The array will be filled as cov[i *ndim + j]
175  The ordering of the variables is the same as in errors and parameter value.
176  This is different from the direct interface of Minuit2 or TMinuit where the
177  values were obtained only to variable parameters
178  */
179  virtual bool GetCovMatrix(double * cov) const;
180 
181  /**
182  Fill the passed array with the Hessian matrix elements
183  The Hessian matrix is the matrix of the second derivatives
184  and is the inverse of the covariance matrix
185  If the variable is fixed or const the values for that variables are zero.
186  The array will be filled as h[i *ndim + j]
187  */
188  virtual bool GetHessianMatrix(double * h) const;
189 
190  ///return status of covariance matrix
191  virtual int CovMatrixStatus() const;
192 
193  ///global correlation coefficient for variable i
194  virtual double GlobalCC(unsigned int ) const;
195 
196  /// minos error for variable i, return false if Minos failed
197  virtual bool GetMinosError(unsigned int i, double & errLow, double & errUp, int = 0);
198 
199  /**
200  perform a full calculation of the Hessian matrix for error calculation
201  */
202  virtual bool Hesse();
203 
204  /**
205  scan a parameter i around the minimum. A minimization must have been done before,
206  return false if it is not the case
207  */
208  virtual bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0);
209 
210  /**
211  find the contour points (xi,xj) of the function for parameter i and j around the minimum
212  The contour will be find for value of the function = Min + ErrorUp();
213  */
214  virtual bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj);
215 
216 
217  virtual void PrintResults();
218 
219  /// return reference to the objective function
220  ///virtual const ROOT::Math::IGenFunction & Function() const;
221 
222  /// get name of variables (override if minimizer support storing of variable names)
223  virtual std::string VariableName(unsigned int ivar) const;
224 
225  /// get index of variable given a variable given a name
226  /// return always -1 . (It is Not implemented)
227  virtual int VariableIndex(const std::string & name) const;
228 
229  /// static function to switch on/off usage of static global TMinuit instance (gMinuit)
230  /// By default it is used (i.e. is on). Method returns the previous state
231  bool static UseStaticMinuit(bool on = true);
232 
233  /// suppress the minuit warnings (if called with false will enable them)
234  /// By default they are suppressed only when the printlevel is <= 0
235  void SuppressMinuitWarnings(bool nowarn=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 private:
266 
267  bool fUsed;
268  bool fMinosRun;
269  unsigned int fDim;
270  std::vector<double> fParams; // vector of output values
271  std::vector<double> fErrors; // vector of output errors
272  std::vector<double> fCovar; // vector storing the covariance matrix
273 
276 
277  static TMinuit * fgMinuit;
278 
279  static bool fgUsed; // flag to control if static instance has done minimization
280  static bool fgUseStaticMinuit; // flag to control if using global TMInuit instance (gMinuit)
281 
282  ClassDef(TMinuitMinimizer,1) //Implementation of Minimizer interface using TMinuit
283 
284 };
285 
286 
287 
288 #endif /* ROOT_TMinuitMinimizer */
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:322
float xmin
Definition: THbookFile.cxx:93
virtual const double * Errors() const
return errors at the minimum
static TMinuit * fgMinuit
Implementation in C++ of the Minuit package written by Fred James.
Definition: TMinuit.h:31
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
std::vector< double > fParams
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
TH1 * h
Definition: legend2.C:5
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...
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
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:86
virtual const double * X() const
return pointer to X values at the minimum
static bool fgUseStaticMinuit
float xmax
Definition: THbookFile.cxx:93
RooCmdArg Hesse(Bool_t flag=kTRUE)
ROOT::Minuit::EMinimizerType fType
double f(double x)
int type
Definition: TGX11.cxx:120
Double_t y[n]
Definition: legend1.C:17
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual unsigned int NDim() const
this is <= Function().NDim() which is the total number of variables (free+ constrained ones) ...
TMinuitMinimizer class: ROOT::Math::Minimizer implementation based on TMinuit.
std::vector< double > fCovar
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
virtual bool ProvidesError() const
minimizer provides error and error matrix
char name[80]
Definition: TGX11.cxx:109
std::vector< double > fErrors