Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 class TMinuit;
21 
22 
23 
24 namespace ROOT {
25 
26  namespace Minuit {
27 
28 
29  // enumeration specifying the type of TMinuit minimizers
30  enum EMinimizerType {
31  kMigrad,
32  kSimplex,
33  kCombined,
34  kMigradImproved,
35  kScan,
36  kSeek
37  };
38 
39  }
40 }
41 
42 
43 
44 /**
45  TMinuitMinimizer class:
46  ROOT::Math::Minimizer implementation based on TMinuit
47 
48  @ingroup TMinuit
49 */
50 class TMinuitMinimizer : public ROOT::Math::Minimizer {
51 
52 public:
53 
54  /**
55  Default constructor
56  */
57  TMinuitMinimizer ( ROOT::Minuit::EMinimizerType type = ROOT::Minuit::kMigrad, unsigned int ndim = 0);
58 
59  /**
60  Constructor from a char * (used by PM)
61  */
62  TMinuitMinimizer ( const char * type , unsigned int ndim = 0);
63 
64  /**
65  Destructor (no operations)
66  */
67  ~TMinuitMinimizer ();
68 
69 private:
70  // usually copying is non trivial, so we make this unaccessible
71 
72  /**
73  Copy constructor
74  */
75  TMinuitMinimizer(const TMinuitMinimizer &);
76 
77  /**
78  Assignment operator
79  */
80  TMinuitMinimizer & operator = (const TMinuitMinimizer & rhs);
81 
82 public:
83 
84  /// set the function to minimize
85  virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func);
86 
87  /// set the function to minimize
88  virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func);
89 
90  /// set free variable
91  virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step);
92 
93  /// set upper/lower limited variable (override if minimizer supports them )
94  virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double /* lower */, double /* upper */);
95 
96  /// set lower limit variable (override if minimizer supports them )
97  virtual bool SetLowerLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower );
98 
99  /// set upper limit variable (override if minimizer supports them )
100  virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );
101 
102  /// set fixed variable (override if minimizer supports them )
103  virtual bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */);
104 
105  /// set the value of an existing variable
106  virtual bool SetVariableValue(unsigned int , double );
107 
108  /// set the step size of an existing variable
109  virtual bool SetVariableStepSize(unsigned int , double );
110  /// set the lower-limit of an existing variable
111  virtual bool SetVariableLowerLimit(unsigned int , double );
112  /// set the upper-limit of an existing variable
113  virtual bool SetVariableUpperLimit(unsigned int , double );
114  /// set the limits of an existing variable
115  virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper);
116  /// fix an existing variable
117  virtual bool FixVariable(unsigned int);
118  /// release an existing variable
119  virtual bool ReleaseVariable(unsigned int);
120  /// query if an existing variable is fixed (i.e. considered constant in the minimization)
121  /// note that by default all variables are not fixed
122  virtual bool IsFixedVariable(unsigned int) const;
123  /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
124  virtual bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings & ) const;
125 
126 
127  /// method to perform the minimization
128  virtual bool Minimize();
129 
130  /// return minimum function value
131  virtual double MinValue() const;
132 
133  /// return expected distance reached from the minimum
134  virtual double Edm() const;
135 
136  /// return pointer to X values at the minimum
137  virtual const double * X() const { return &fParams.front(); }
138 
139  /// return pointer to gradient values at the minimum
140  virtual const double * MinGradient() const { return 0; } // not available in Minuit2
141 
142  /// number of function calls to reach the minimum
143  virtual unsigned int NCalls() const;
144 
145  /// this is <= Function().NDim() which is the total
146  /// number of variables (free+ constrained ones)
147  virtual unsigned int NDim() const { return fDim; }
148 
149  /// number of free variables (real dimension of the problem)
150  /// this is <= Function().NDim() which is the total
151  virtual unsigned int NFree() const;
152 
153  /// minimizer provides error and error matrix
154  virtual bool ProvidesError() const { return true; }
155 
156  /// return errors at the minimum
157  virtual const double * Errors() const { return &fErrors.front(); }
158 
159  /** return covariance matrices elements
160  if the variable is fixed the matrix is zero
161  The ordering of the variables is the same as in errors
162  */
163  virtual double CovMatrix(unsigned int i, unsigned int j) const {
164  return ( fCovar.size() > (i + fDim* j) ) ? fCovar[i + fDim* j] : 0;
165  }
166 
167  /**
168  Fill the passed array with the covariance matrix elements
169  if the variable is fixed or const the value is zero.
170  The array will be filled as cov[i *ndim + j]
171  The ordering of the variables is the same as in errors and parameter value.
172  This is different from the direct interface of Minuit2 or TMinuit where the
173  values were obtained only to variable parameters
174  */
175  virtual bool GetCovMatrix(double * cov) const;
176 
177  /**
178  Fill the passed array with the Hessian matrix elements
179  The Hessian matrix is the matrix of the second derivatives
180  and is the inverse of the covariance matrix
181  If the variable is fixed or const the values for that variables are zero.
182  The array will be filled as h[i *ndim + j]
183  */
184  virtual bool GetHessianMatrix(double * h) const;
185 
186  ///return status of covariance matrix
187  virtual int CovMatrixStatus() const;
188 
189  ///global correlation coefficient for variable i
190  virtual double GlobalCC(unsigned int ) const;
191 
192  /// minos error for variable i, return false if Minos failed
193  virtual bool GetMinosError(unsigned int i, double & errLow, double & errUp, int = 0);
194 
195  /**
196  perform a full calculation of the Hessian matrix for error calculation
197  */
198  virtual bool Hesse();
199 
200  /**
201  scan a parameter i around the minimum. A minimization must have been done before,
202  return false if it is not the case
203  */
204  virtual bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0);
205 
206  /**
207  find the contour points (xi,xj) of the function for parameter i and j around the minimum
208  The contour will be find for value of the function = Min + ErrorUp();
209  */
210  virtual bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj);
211 
212 
213  virtual void PrintResults();
214 
215  /// return reference to the objective function
216  ///virtual const ROOT::Math::IGenFunction & Function() const;
217 
218  /// get name of variables (override if minimizer support storing of variable names)
219  virtual std::string VariableName(unsigned int ivar) const;
220 
221  /// get index of variable given a variable given a name
222  /// return always -1 . (It is Not implemented)
223  virtual int VariableIndex(const std::string & name) const;
224 
225  /// static function to switch on/off usage of static global TMinuit instance (gMinuit)
226  /// By default it is used (i.e. is on). Method returns the previous state
227  bool static UseStaticMinuit(bool on = true);
228 
229  /// suppress the minuit warnings (if called with false will enable them)
230  /// By default they are suppressed only when the printlevel is <= 0
231  void SuppressMinuitWarnings(bool nowarn=true);
232 
233  /// set debug mode. Return true if setting was successfull
234  bool SetDebug(bool on = true);
235 
236 protected:
237 
238  /// implementation of FCN for Minuit
239  static void Fcn( int &, double * , double & f, double * , int);
240  /// implementation of FCN for Minuit when user provided gradient is used
241  static void FcnGrad( int &, double * g, double & f, double * , int);
242 
243  /// initialize the TMinuit instance
244  void InitTMinuit(int ndim);
245 
246  /// reset
247  void DoClear();
248 
249  ///release a parameter that is fixed when it is redefined
250  void DoReleaseFixParameter( int ivar);
251 
252  /// retrieve minimum parameters and errors from TMinuit
253  void RetrieveParams();
254 
255  /// retrieve error matrix from TMinuit
256  void RetrieveErrorMatrix();
257 
258  /// check TMinuit instance
259  bool CheckMinuitInstance() const;
260 
261  ///check parameter
262  bool CheckVarIndex(unsigned int ivar) const;
263 
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 
274  ROOT::Minuit::EMinimizerType fType;
275  TMinuit * fMinuit;
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 */