TMinuitMinimizer.h

Go to the documentation of this file.
00001 // @(#)root/minuit:$Id: TMinuitMinimizer.h 35643 2010-09-23 12:48:32Z moneta $
00002 // Author: L. Moneta Wed Oct 25 16:28:55 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class TMinuitMinimizer
00012 
00013 #ifndef ROOT_TMinuitMinimizer
00014 #define ROOT_TMinuitMinimizer
00015 
00016 #ifndef ROOT_Math_Minimizer
00017 #include "Math/Minimizer.h"
00018 #endif
00019 
00020 #ifndef ROOT_Rtypes
00021 #include "Rtypes.h"
00022 #endif
00023 
00024 class TMinuit; 
00025 
00026 
00027 
00028 namespace ROOT { 
00029 
00030    namespace Minuit { 
00031 
00032 
00033       // enumeration specifying the type of TMinuit minimizers
00034       enum EMinimizerType { 
00035          kMigrad, 
00036          kSimplex, 
00037          kCombined, 
00038          kMigradImproved, 
00039          kScan, 
00040          kSeek
00041       };
00042 
00043    }
00044 }
00045 
00046  
00047 
00048 /** 
00049    TMinuitMinimizer class: minimizer implementation based on TMinuit.
00050 */ 
00051 class TMinuitMinimizer  : public ROOT::Math::Minimizer {
00052 
00053 public: 
00054 
00055    /** 
00056       Default constructor
00057    */ 
00058    TMinuitMinimizer ( ROOT::Minuit::EMinimizerType type = ROOT::Minuit::kMigrad, unsigned int ndim = 0); 
00059 
00060    /** 
00061       Constructor from a char * (used by PM)
00062    */ 
00063    TMinuitMinimizer ( const char * type , unsigned int ndim = 0); 
00064 
00065    /** 
00066       Destructor (no operations)
00067    */ 
00068    ~TMinuitMinimizer (); 
00069 
00070 private:
00071    // usually copying is non trivial, so we make this unaccessible
00072 
00073    /** 
00074       Copy constructor
00075    */ 
00076    TMinuitMinimizer(const TMinuitMinimizer &); 
00077 
00078    /** 
00079       Assignment operator
00080    */ 
00081    TMinuitMinimizer & operator = (const TMinuitMinimizer & rhs); 
00082 
00083 public: 
00084 
00085    /// set the function to minimize
00086    virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func); 
00087 
00088    /// set the function to minimize
00089    virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func); 
00090 
00091    /// set free variable 
00092    virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step); 
00093 
00094    /// set upper/lower limited variable (override if minimizer supports them )
00095    virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double /* lower */, double /* upper */); 
00096 
00097 #ifdef LATER
00098    /// set lower limit variable  (override if minimizer supports them )
00099    virtual bool SetLowerLimitedVariable(unsigned int  ivar , const std::string & name , double val , double step , double lower );
00100    /// set upper limit variable (override if minimizer supports them )
00101    virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper );
00102 #endif
00103 
00104    /// set fixed variable (override if minimizer supports them )
00105    virtual bool SetFixedVariable(unsigned int /* ivar */, const std::string & /* name */, double /* val */);  
00106 
00107    /// set the value of an existing variable 
00108    virtual bool SetVariableValue(unsigned int , double );
00109  
00110    /// method to perform the minimization
00111    virtual  bool Minimize(); 
00112 
00113    /// return minimum function value
00114    virtual double MinValue() const;  
00115 
00116    /// return expected distance reached from the minimum
00117    virtual double Edm() const; 
00118 
00119    /// return  pointer to X values at the minimum 
00120    virtual const double *  X() const { return &fParams.front(); }
00121 
00122    /// return pointer to gradient values at the minimum 
00123    virtual const double *  MinGradient() const { return 0; } // not available in Minuit2 
00124 
00125    /// number of function calls to reach the minimum 
00126    virtual unsigned int NCalls() const;
00127 
00128    /// this is <= Function().NDim() which is the total 
00129    /// number of variables (free+ constrained ones) 
00130    virtual unsigned int NDim() const { return fDim; }   
00131 
00132    /// number of free variables (real dimension of the problem) 
00133    /// this is <= Function().NDim() which is the total 
00134    virtual unsigned int NFree() const; 
00135 
00136    /// minimizer provides error and error matrix
00137    virtual bool ProvidesError() const { return true; } 
00138 
00139    /// return errors at the minimum 
00140    virtual const double * Errors() const { return  &fErrors.front(); }
00141 
00142    /** return covariance matrices elements 
00143        if the variable is fixed the matrix is zero
00144        The ordering of the variables is the same as in errors
00145    */ 
00146    virtual double CovMatrix(unsigned int i, unsigned int j) const { 
00147       return ( fCovar.size() > (i + fDim* j) ) ? fCovar[i + fDim* j] : 0; 
00148  }
00149 
00150    ///return status of covariance matrix 
00151    virtual int CovMatrixStatus() const; 
00152 
00153    ///global correlation coefficient for variable i
00154    virtual double GlobalCC(unsigned int ) const; 
00155 
00156    /// minos error for variable i, return false if Minos failed
00157    virtual bool GetMinosError(unsigned int i, double & errLow, double & errUp, int = 0); 
00158 
00159    /**
00160       perform a full calculation of the Hessian matrix for error calculation
00161     */
00162    virtual bool Hesse(); 
00163 
00164    /**
00165       scan a parameter i around the minimum. A minimization must have been done before, 
00166       return false if it is not the case
00167     */
00168    virtual bool Scan(unsigned int i, unsigned int &nstep, double * x, double * y, double xmin = 0, double xmax = 0); 
00169 
00170    /**
00171       find the contour points (xi,xj) of the function for parameter i and j around the minimum
00172       The contour will be find for value of the function = Min + ErrorUp();
00173     */
00174    virtual bool Contour(unsigned int i, unsigned int j, unsigned int & npoints, double *xi, double *xj); 
00175 
00176 
00177    virtual void PrintResults();
00178 
00179    /// return reference to the objective function
00180    ///virtual const ROOT::Math::IGenFunction & Function() const; 
00181 
00182    /// get name of variables (override if minimizer support storing of variable names)
00183    virtual std::string VariableName(unsigned int ivar) const;
00184 
00185    /// get index of variable given a variable given a name
00186    /// return always -1 . (It is Not implemented)
00187    virtual int VariableIndex(const std::string & name) const;
00188 
00189    /// static function to switch on/off usage of static global TMinuit instance (gMinuit)
00190    /// By default it is used (i.e. is on). Method returns the previous state
00191    bool static UseStaticMinuit(bool on = true); 
00192    
00193 
00194 protected: 
00195 
00196    /// implementation of FCN for Minuit
00197    static void Fcn( int &, double * , double & f, double * , int);
00198    /// implementation of FCN for Minuit when user provided gradient is used
00199    static void FcnGrad( int &, double * g, double & f, double * , int);
00200 
00201    /// initialize the TMinuit instance
00202    void InitTMinuit(int ndim); 
00203 
00204    /// reset 
00205    void DoClear(); 
00206 
00207    ///release a parameter that is fixed  when it is redefined
00208    void DoReleaseFixParameter( int ivar); 
00209 
00210    /// retrieve minimum parameters and errors from TMinuit
00211    void RetrieveParams(); 
00212 
00213    /// retrieve error matrix from TMinuit
00214    void RetrieveErrorMatrix(); 
00215 
00216 private: 
00217 
00218    bool fUsed;
00219    bool fMinosRun; 
00220    unsigned int fDim; 
00221    unsigned int fStrategy;
00222    std::vector<double> fParams;
00223    std::vector<double> fErrors;
00224    std::vector<double> fCovar; 
00225 
00226    ROOT::Minuit::EMinimizerType fType; 
00227    TMinuit * fMinuit; 
00228 
00229    // need to have a static copy of the function 
00230    //NOTE: This is NOT thread safe.
00231    static ROOT::Math::IMultiGenFunction * fgFunc;
00232 
00233    static TMinuit * fgMinuit; 
00234 
00235    static bool fgUsed;  // flag to control if static instance has done minimization
00236    static bool fgUseStaticMinuit; // flag to control if using global TMInuit instance (gMinuit)
00237 
00238    ClassDef(TMinuitMinimizer,1)  //Implementation of Minimizer interface using TMinuit 
00239 
00240 }; 
00241 
00242 
00243 
00244 #endif /* ROOT_TMinuitMinimizer */

Generated on Tue Jul 5 14:27:17 2011 for ROOT_528-00b_version by  doxygen 1.5.1