// @(#)root/minuit:$Id$
// Author: L. Moneta Wed Oct 25 16:28:55 2006

/**********************************************************************
 *                                                                    *
 * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
 *                                                                    *
 *                                                                    *
 **********************************************************************/

// Header file for class TMinuitMinimizer

#ifndef ROOT_TMinuitMinimizer
#define ROOT_TMinuitMinimizer

#ifndef ROOT_Math_Minimizer
#include "Math/Minimizer.h"
#endif

#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif

class TMinuit; 



namespace ROOT { 

   namespace Minuit { 


      // enumeration specifying the type of TMinuit minimizers
      enum EMinimizerType { 
         kMigrad, 
         kSimplex, 
         kCombined, 
         kMigradImproved, 
         kScan, 
         kSeek
      };

   }
}

 

/** 
   TMinuitMinimizer class: minimizer implementation based on TMinuit.
*/ 
class TMinuitMinimizer  : public ROOT::Math::Minimizer {

public: 

   /** 
      Default constructor
   */ 
   TMinuitMinimizer ( ROOT::Minuit::EMinimizerType type = ROOT::Minuit::kMigrad, unsigned int ndim = 0); 

   /** 
      Constructor from a char * (used by PM)
   */ 
   TMinuitMinimizer ( const char * type , unsigned int ndim = 0); 

   /** 
      Destructor (no operations)
   */ 
   ~TMinuitMinimizer (); 

private:
   // usually copying is non trivial, so we make this unaccessible

   /** 
      Copy constructor
   */ 
   TMinuitMinimizer(const TMinuitMinimizer &); 

   /** 
      Assignment operator
   */ 
   TMinuitMinimizer & operator = (const TMinuitMinimizer & rhs); 

public: 

   /// set the function to minimize
   virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func); 

   /// set the function to minimize
   virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func); 

   /// set free variable 
   virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step); 

   /// set upper/lower limited variable (override if minimizer supports them )
   virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double /* lower */, double /* upper */); 

   /// set lower limit variable  (override if minimizer supports them )
   virtual bool SetLowerLimitedVariable(unsigned int  ivar , const std::string & name , double val , double step , double lower );

   /// set upper limit variable (override if minimizer supports them )
   virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );

   /// set fixed variable (override if minimizer supports them )
   virtual bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */);  

   /// set the value of an existing variable 
   virtual bool SetVariableValue(unsigned int , double );

   /// set the step size of an existing variable
   virtual bool SetVariableStepSize(unsigned int , double );
   /// set the lower-limit of an existing variable 
   virtual bool SetVariableLowerLimit(unsigned int , double );
   /// set the upper-limit of an existing variable 
   virtual bool SetVariableUpperLimit(unsigned int , double );
   /// set the limits of an existing variable 
   virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper);
   /// fix an existing variable
   virtual bool FixVariable(unsigned int);
   /// release an existing variable
   virtual bool ReleaseVariable(unsigned int);
   /// query if an existing variable is fixed (i.e. considered constant in the minimization)
   /// note that by default all variables are not fixed 
   virtual bool IsFixedVariable(unsigned int) const;
   /// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
   virtual bool GetVariableSettings(unsigned int, ROOT::Fit::ParameterSettings & ) const;

 
   /// method to perform the minimization
   virtual  bool Minimize(); 

   /// return minimum function value
   virtual double MinValue() const;  

   /// return expected distance reached from the minimum
   virtual double Edm() const; 

   /// return  pointer to X values at the minimum 
   virtual const double *  X() const { return &fParams.front(); }

   /// return pointer to gradient values at the minimum 
   virtual const double *  MinGradient() const { return 0; } // not available in Minuit2 

   /// number of function calls to reach the minimum 
   virtual unsigned int NCalls() const;

   /// this is <= Function().NDim() which is the total 
   /// number of variables (free+ constrained ones) 
   virtual unsigned int NDim() const { return fDim; }   

   /// number of free variables (real dimension of the problem) 
   /// this is <= Function().NDim() which is the total 
   virtual unsigned int NFree() const; 

   /// minimizer provides error and error matrix
   virtual bool ProvidesError() const { return true; } 

   /// return errors at the minimum 
   virtual const double * Errors() const { return  &fErrors.front(); }

   /** return covariance matrices elements 
       if the variable is fixed the matrix is zero
       The ordering of the variables is the same as in errors
   */ 
   virtual double CovMatrix(unsigned int i, unsigned int j) const { 
      return ( fCovar.size() > (i + fDim* j) ) ? fCovar[i + fDim* j] : 0; 
   }

   /** 
       Fill the passed array with the  covariance matrix elements 
       if the variable is fixed or const the value is zero. 
       The array will be filled as cov[i *ndim + j]
       The ordering of the variables is the same as in errors and parameter value. 
       This is different from the direct interface of Minuit2 or TMinuit where the 
       values were obtained only to variable parameters
   */ 
   virtual bool GetCovMatrix(double * cov) const;  

   /** 
       Fill the passed array with the Hessian matrix elements 
       The Hessian matrix is the matrix of the second derivatives 
       and is the inverse of the covariance matrix
       If the variable is fixed or const the values for that variables are zero. 
       The array will be filled as h[i *ndim + j]
   */ 
   virtual bool GetHessianMatrix(double * h) const;  

   ///return status of covariance matrix 
   virtual int CovMatrixStatus() const; 

   ///global correlation coefficient for variable i
   virtual double GlobalCC(unsigned int ) const; 

   /// minos error for variable i, return false if Minos failed
   virtual bool GetMinosError(unsigned int i, double & errLow, double & errUp, int = 0); 

   /**
      perform a full calculation of the Hessian matrix for error calculation
    */
   virtual bool Hesse(); 

   /**
      scan a parameter i around the minimum. A minimization must have been done before, 
      return false if it is not the case
    */
   virtual bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0); 

   /**
      find the contour points (xi,xj) of the function for parameter i and j around the minimum
      The contour will be find for value of the function = Min + ErrorUp();
    */
   virtual bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj); 


   virtual void PrintResults();

   /// return reference to the objective function
   ///virtual const ROOT::Math::IGenFunction & Function() const; 

   /// get name of variables (override if minimizer support storing of variable names)
   virtual std::string VariableName(unsigned int ivar) const;

   /// get index of variable given a variable given a name
   /// return always -1 . (It is Not implemented)
   virtual int VariableIndex(const std::string & name) const;

   /// static function to switch on/off usage of static global TMinuit instance (gMinuit)
   /// By default it is used (i.e. is on). Method returns the previous state
   bool static UseStaticMinuit(bool on = true); 

   /// suppress the minuit warnings (if called with false will enable them)
   /// By default they are suppressed only when the printlevel is <= 0
   void SuppressMinuitWarnings(bool nowarn=true);

protected: 

   /// implementation of FCN for Minuit
   static void Fcn( int &, double * , double & f, double * , int);
   /// implementation of FCN for Minuit when user provided gradient is used
   static void FcnGrad( int &, double * g, double & f, double * , int);

   /// initialize the TMinuit instance
   void InitTMinuit(int ndim); 

   /// reset 
   void DoClear(); 

   ///release a parameter that is fixed  when it is redefined
   void DoReleaseFixParameter( int ivar); 

   /// retrieve minimum parameters and errors from TMinuit
   void RetrieveParams(); 

   /// retrieve error matrix from TMinuit
   void RetrieveErrorMatrix(); 

   /// check TMinuit instance
   bool CheckMinuitInstance() const; 

   ///check parameter
   bool CheckVarIndex(unsigned int ivar) const; 

private: 

   bool fUsed;
   bool fMinosRun; 
   unsigned int fDim; 
   std::vector<double> fParams;   // vector of output values
   std::vector<double> fErrors;   // vector of output errors
   std::vector<double> fCovar;    // vector storing the covariance matrix

   ROOT::Minuit::EMinimizerType fType; 
   TMinuit * fMinuit; 

   // need to have a static copy of the function 
   //NOTE: This is NOT thread safe.
   static ROOT::Math::IMultiGenFunction * fgFunc;

   static TMinuit * fgMinuit; 

   static bool fgUsed;  // flag to control if static instance has done minimization
   static bool fgUseStaticMinuit; // flag to control if using global TMInuit instance (gMinuit)

   ClassDef(TMinuitMinimizer,1)  //Implementation of Minimizer interface using TMinuit 

}; 



#endif /* ROOT_TMinuitMinimizer */
 TMinuitMinimizer.h:1
 TMinuitMinimizer.h:2
 TMinuitMinimizer.h:3
 TMinuitMinimizer.h:4
 TMinuitMinimizer.h:5
 TMinuitMinimizer.h:6
 TMinuitMinimizer.h:7
 TMinuitMinimizer.h:8
 TMinuitMinimizer.h:9
 TMinuitMinimizer.h:10
 TMinuitMinimizer.h:11
 TMinuitMinimizer.h:12
 TMinuitMinimizer.h:13
 TMinuitMinimizer.h:14
 TMinuitMinimizer.h:15
 TMinuitMinimizer.h:16
 TMinuitMinimizer.h:17
 TMinuitMinimizer.h:18
 TMinuitMinimizer.h:19
 TMinuitMinimizer.h:20
 TMinuitMinimizer.h:21
 TMinuitMinimizer.h:22
 TMinuitMinimizer.h:23
 TMinuitMinimizer.h:24
 TMinuitMinimizer.h:25
 TMinuitMinimizer.h:26
 TMinuitMinimizer.h:27
 TMinuitMinimizer.h:28
 TMinuitMinimizer.h:29
 TMinuitMinimizer.h:30
 TMinuitMinimizer.h:31
 TMinuitMinimizer.h:32
 TMinuitMinimizer.h:33
 TMinuitMinimizer.h:34
 TMinuitMinimizer.h:35
 TMinuitMinimizer.h:36
 TMinuitMinimizer.h:37
 TMinuitMinimizer.h:38
 TMinuitMinimizer.h:39
 TMinuitMinimizer.h:40
 TMinuitMinimizer.h:41
 TMinuitMinimizer.h:42
 TMinuitMinimizer.h:43
 TMinuitMinimizer.h:44
 TMinuitMinimizer.h:45
 TMinuitMinimizer.h:46
 TMinuitMinimizer.h:47
 TMinuitMinimizer.h:48
 TMinuitMinimizer.h:49
 TMinuitMinimizer.h:50
 TMinuitMinimizer.h:51
 TMinuitMinimizer.h:52
 TMinuitMinimizer.h:53
 TMinuitMinimizer.h:54
 TMinuitMinimizer.h:55
 TMinuitMinimizer.h:56
 TMinuitMinimizer.h:57
 TMinuitMinimizer.h:58
 TMinuitMinimizer.h:59
 TMinuitMinimizer.h:60
 TMinuitMinimizer.h:61
 TMinuitMinimizer.h:62
 TMinuitMinimizer.h:63
 TMinuitMinimizer.h:64
 TMinuitMinimizer.h:65
 TMinuitMinimizer.h:66
 TMinuitMinimizer.h:67
 TMinuitMinimizer.h:68
 TMinuitMinimizer.h:69
 TMinuitMinimizer.h:70
 TMinuitMinimizer.h:71
 TMinuitMinimizer.h:72
 TMinuitMinimizer.h:73
 TMinuitMinimizer.h:74
 TMinuitMinimizer.h:75
 TMinuitMinimizer.h:76
 TMinuitMinimizer.h:77
 TMinuitMinimizer.h:78
 TMinuitMinimizer.h:79
 TMinuitMinimizer.h:80
 TMinuitMinimizer.h:81
 TMinuitMinimizer.h:82
 TMinuitMinimizer.h:83
 TMinuitMinimizer.h:84
 TMinuitMinimizer.h:85
 TMinuitMinimizer.h:86
 TMinuitMinimizer.h:87
 TMinuitMinimizer.h:88
 TMinuitMinimizer.h:89
 TMinuitMinimizer.h:90
 TMinuitMinimizer.h:91
 TMinuitMinimizer.h:92
 TMinuitMinimizer.h:93
 TMinuitMinimizer.h:94
 TMinuitMinimizer.h:95
 TMinuitMinimizer.h:96
 TMinuitMinimizer.h:97
 TMinuitMinimizer.h:98
 TMinuitMinimizer.h:99
 TMinuitMinimizer.h:100
 TMinuitMinimizer.h:101
 TMinuitMinimizer.h:102
 TMinuitMinimizer.h:103
 TMinuitMinimizer.h:104
 TMinuitMinimizer.h:105
 TMinuitMinimizer.h:106
 TMinuitMinimizer.h:107
 TMinuitMinimizer.h:108
 TMinuitMinimizer.h:109
 TMinuitMinimizer.h:110
 TMinuitMinimizer.h:111
 TMinuitMinimizer.h:112
 TMinuitMinimizer.h:113
 TMinuitMinimizer.h:114
 TMinuitMinimizer.h:115
 TMinuitMinimizer.h:116
 TMinuitMinimizer.h:117
 TMinuitMinimizer.h:118
 TMinuitMinimizer.h:119
 TMinuitMinimizer.h:120
 TMinuitMinimizer.h:121
 TMinuitMinimizer.h:122
 TMinuitMinimizer.h:123
 TMinuitMinimizer.h:124
 TMinuitMinimizer.h:125
 TMinuitMinimizer.h:126
 TMinuitMinimizer.h:127
 TMinuitMinimizer.h:128
 TMinuitMinimizer.h:129
 TMinuitMinimizer.h:130
 TMinuitMinimizer.h:131
 TMinuitMinimizer.h:132
 TMinuitMinimizer.h:133
 TMinuitMinimizer.h:134
 TMinuitMinimizer.h:135
 TMinuitMinimizer.h:136
 TMinuitMinimizer.h:137
 TMinuitMinimizer.h:138
 TMinuitMinimizer.h:139
 TMinuitMinimizer.h:140
 TMinuitMinimizer.h:141
 TMinuitMinimizer.h:142
 TMinuitMinimizer.h:143
 TMinuitMinimizer.h:144
 TMinuitMinimizer.h:145
 TMinuitMinimizer.h:146
 TMinuitMinimizer.h:147
 TMinuitMinimizer.h:148
 TMinuitMinimizer.h:149
 TMinuitMinimizer.h:150
 TMinuitMinimizer.h:151
 TMinuitMinimizer.h:152
 TMinuitMinimizer.h:153
 TMinuitMinimizer.h:154
 TMinuitMinimizer.h:155
 TMinuitMinimizer.h:156
 TMinuitMinimizer.h:157
 TMinuitMinimizer.h:158
 TMinuitMinimizer.h:159
 TMinuitMinimizer.h:160
 TMinuitMinimizer.h:161
 TMinuitMinimizer.h:162
 TMinuitMinimizer.h:163
 TMinuitMinimizer.h:164
 TMinuitMinimizer.h:165
 TMinuitMinimizer.h:166
 TMinuitMinimizer.h:167
 TMinuitMinimizer.h:168
 TMinuitMinimizer.h:169
 TMinuitMinimizer.h:170
 TMinuitMinimizer.h:171
 TMinuitMinimizer.h:172
 TMinuitMinimizer.h:173
 TMinuitMinimizer.h:174
 TMinuitMinimizer.h:175
 TMinuitMinimizer.h:176
 TMinuitMinimizer.h:177
 TMinuitMinimizer.h:178
 TMinuitMinimizer.h:179
 TMinuitMinimizer.h:180
 TMinuitMinimizer.h:181
 TMinuitMinimizer.h:182
 TMinuitMinimizer.h:183
 TMinuitMinimizer.h:184
 TMinuitMinimizer.h:185
 TMinuitMinimizer.h:186
 TMinuitMinimizer.h:187
 TMinuitMinimizer.h:188
 TMinuitMinimizer.h:189
 TMinuitMinimizer.h:190
 TMinuitMinimizer.h:191
 TMinuitMinimizer.h:192
 TMinuitMinimizer.h:193
 TMinuitMinimizer.h:194
 TMinuitMinimizer.h:195
 TMinuitMinimizer.h:196
 TMinuitMinimizer.h:197
 TMinuitMinimizer.h:198
 TMinuitMinimizer.h:199
 TMinuitMinimizer.h:200
 TMinuitMinimizer.h:201
 TMinuitMinimizer.h:202
 TMinuitMinimizer.h:203
 TMinuitMinimizer.h:204
 TMinuitMinimizer.h:205
 TMinuitMinimizer.h:206
 TMinuitMinimizer.h:207
 TMinuitMinimizer.h:208
 TMinuitMinimizer.h:209
 TMinuitMinimizer.h:210
 TMinuitMinimizer.h:211
 TMinuitMinimizer.h:212
 TMinuitMinimizer.h:213
 TMinuitMinimizer.h:214
 TMinuitMinimizer.h:215
 TMinuitMinimizer.h:216
 TMinuitMinimizer.h:217
 TMinuitMinimizer.h:218
 TMinuitMinimizer.h:219
 TMinuitMinimizer.h:220
 TMinuitMinimizer.h:221
 TMinuitMinimizer.h:222
 TMinuitMinimizer.h:223
 TMinuitMinimizer.h:224
 TMinuitMinimizer.h:225
 TMinuitMinimizer.h:226
 TMinuitMinimizer.h:227
 TMinuitMinimizer.h:228
 TMinuitMinimizer.h:229
 TMinuitMinimizer.h:230
 TMinuitMinimizer.h:231
 TMinuitMinimizer.h:232
 TMinuitMinimizer.h:233
 TMinuitMinimizer.h:234
 TMinuitMinimizer.h:235
 TMinuitMinimizer.h:236
 TMinuitMinimizer.h:237
 TMinuitMinimizer.h:238
 TMinuitMinimizer.h:239
 TMinuitMinimizer.h:240
 TMinuitMinimizer.h:241
 TMinuitMinimizer.h:242
 TMinuitMinimizer.h:243
 TMinuitMinimizer.h:244
 TMinuitMinimizer.h:245
 TMinuitMinimizer.h:246
 TMinuitMinimizer.h:247
 TMinuitMinimizer.h:248
 TMinuitMinimizer.h:249
 TMinuitMinimizer.h:250
 TMinuitMinimizer.h:251
 TMinuitMinimizer.h:252
 TMinuitMinimizer.h:253
 TMinuitMinimizer.h:254
 TMinuitMinimizer.h:255
 TMinuitMinimizer.h:256
 TMinuitMinimizer.h:257
 TMinuitMinimizer.h:258
 TMinuitMinimizer.h:259
 TMinuitMinimizer.h:260
 TMinuitMinimizer.h:261
 TMinuitMinimizer.h:262
 TMinuitMinimizer.h:263
 TMinuitMinimizer.h:264
 TMinuitMinimizer.h:265
 TMinuitMinimizer.h:266
 TMinuitMinimizer.h:267
 TMinuitMinimizer.h:268
 TMinuitMinimizer.h:269
 TMinuitMinimizer.h:270
 TMinuitMinimizer.h:271
 TMinuitMinimizer.h:272
 TMinuitMinimizer.h:273
 TMinuitMinimizer.h:274
 TMinuitMinimizer.h:275
 TMinuitMinimizer.h:276
 TMinuitMinimizer.h:277
 TMinuitMinimizer.h:278
 TMinuitMinimizer.h:279
 TMinuitMinimizer.h:280
 TMinuitMinimizer.h:281
 TMinuitMinimizer.h:282
 TMinuitMinimizer.h:283
 TMinuitMinimizer.h:284
 TMinuitMinimizer.h:285
 TMinuitMinimizer.h:286
 TMinuitMinimizer.h:287
 TMinuitMinimizer.h:288
 TMinuitMinimizer.h:289