MinimizerOptions.cxx

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: MinimizerOptions.cxx 36905 2010-11-24 15:44:34Z moneta $ 
00002 // Author: L. Moneta Fri Aug 15 2008
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2008  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 #include "Math/MinimizerOptions.h"
00012 
00013 #include "Math/GenAlgoOptions.h"
00014 
00015 // case of using ROOT plug-in manager
00016 #ifndef MATH_NO_PLUGIN_MANAGER
00017 #include "TEnv.h"
00018 #endif 
00019 
00020 #include <iomanip>
00021 
00022 namespace ROOT { 
00023    
00024 
00025 namespace Math { 
00026 
00027    namespace Minim { 
00028       static std::string gDefaultMinimizer = ""; // take from /etc/system.rootrc in ROOT Fitter
00029       static std::string gDefaultMinimAlgo = "Migrad";
00030       static double gDefaultErrorDef = 1.;
00031       static double gDefaultTolerance = 1.E-4; 
00032       static double gDefaultPrecision = -1; // value <= 0 means left to minimizer
00033       static int  gDefaultMaxCalls = 0; // 0 means leave default values Deaf
00034       static int  gDefaultMaxIter  = 0; 
00035       static int  gDefaultStrategy  = 1; 
00036       static int  gDefaultPrintLevel  = 0; 
00037    }
00038 
00039 
00040 void MinimizerOptions::SetDefaultMinimizer(const char * type, const char * algo ) {   
00041    // set the default minimizer type and algorithm
00042    if (type) Minim::gDefaultMinimizer = std::string(type); 
00043    if (algo) Minim::gDefaultMinimAlgo = std::string(algo);
00044 }
00045 void MinimizerOptions::SetDefaultErrorDef(double up) {
00046    // set the default error definition 
00047    Minim::gDefaultErrorDef = up; 
00048 }
00049 void MinimizerOptions::SetDefaultTolerance(double tol) {
00050    // set the defult tolerance
00051    Minim::gDefaultTolerance = tol; 
00052 }
00053 void MinimizerOptions::SetDefaultPrecision(double prec) {
00054    // set the defult precision
00055    Minim::gDefaultPrecision = prec; 
00056 }
00057 void MinimizerOptions::SetDefaultMaxFunctionCalls(int maxcall) {
00058    // set the default maximum number of function calls
00059    Minim::gDefaultMaxCalls = maxcall; 
00060 }
00061 void MinimizerOptions::SetDefaultMaxIterations(int maxiter) {
00062    // set the default maximum number of iterations
00063    Minim::gDefaultMaxIter = maxiter; 
00064 }
00065 void MinimizerOptions::SetDefaultStrategy(int stra) {
00066    // set the default minimization strategy
00067    Minim::gDefaultStrategy = stra; 
00068 }
00069 void MinimizerOptions::SetDefaultPrintLevel(int level) {
00070    // set the default printing level 
00071    Minim::gDefaultPrintLevel = level; 
00072 }
00073 
00074 const std::string & MinimizerOptions::DefaultMinimizerAlgo() { return Minim::gDefaultMinimAlgo; }
00075 double MinimizerOptions::DefaultErrorDef()         { return Minim::gDefaultErrorDef; }
00076 double MinimizerOptions::DefaultTolerance()        { return Minim::gDefaultTolerance; }
00077 double MinimizerOptions::DefaultPrecision()        { return Minim::gDefaultPrecision; }
00078 int    MinimizerOptions::DefaultMaxFunctionCalls() { return Minim::gDefaultMaxCalls; }
00079 int    MinimizerOptions::DefaultMaxIterations()    { return Minim::gDefaultMaxIter; }
00080 int    MinimizerOptions::DefaultStrategy()         { return Minim::gDefaultStrategy; }
00081 int    MinimizerOptions::DefaultPrintLevel()       { return Minim::gDefaultPrintLevel; }
00082 
00083 const std::string & MinimizerOptions::DefaultMinimizerType() 
00084 { 
00085    // return default minimizer
00086    // if is "" (no default is set) read from etc/system.rootrc
00087 
00088    if (Minim::gDefaultMinimizer.size() == 0) { 
00089 #ifndef MATH_NO_PLUGIN_MANAGER
00090    // use value defined in etc/system.rootrc  (if not found Minuit is used) 
00091       if (gEnv) 
00092          Minim::gDefaultMinimizer = gEnv->GetValue("Root.Fitter","Minuit");   
00093 #else
00094       Minim::gDefaultMinimizer = "Minuit2";  // in case no PM exists 
00095 #endif
00096    }
00097 
00098    return Minim::gDefaultMinimizer; 
00099 }
00100 
00101 
00102 MinimizerOptions::MinimizerOptions(IOptions * extraOpts): 
00103    fLevel( Minim::gDefaultPrintLevel),
00104    fMaxCalls( Minim::gDefaultMaxCalls ), 
00105    fMaxIter( Minim::gDefaultMaxIter ), 
00106    fStrategy( Minim::gDefaultStrategy ), 
00107    fErrorDef(  Minim::gDefaultErrorDef ), 
00108    fTolerance( Minim::gDefaultTolerance ),
00109    fPrecision( Minim::gDefaultPrecision ),
00110    fExtraOptions(extraOpts)
00111 {
00112    // constructor using  the default options
00113 
00114    fMinimType = MinimizerOptions::DefaultMinimizerType();
00115 
00116    fAlgoType =  Minim::gDefaultMinimAlgo;
00117 
00118    // case of Fumili2 and TMinuit
00119    if (fMinimType == "TMinuit") fMinimType = "Minuit";
00120    else if (fMinimType == "Fumili2") { 
00121       fMinimType = "Minuit2";
00122       fAlgoType = "Fumili";
00123    }   
00124    else if (fMinimType == "GSLMultiMin" && fAlgoType == "Migrad") 
00125       fAlgoType = "BFGS2";
00126 
00127    // check if extra options exists (copy them if needed)
00128    if (!fExtraOptions) { 
00129       IOptions * gopts = FindDefault( fMinimType.c_str() );
00130       if (gopts) fExtraOptions = gopts->Clone();
00131    }
00132 }
00133 
00134 
00135 MinimizerOptions::MinimizerOptions(const MinimizerOptions & opt) : fExtraOptions(0) {  
00136    // copy constructor 
00137    (*this) = opt; 
00138 }
00139 
00140 MinimizerOptions & MinimizerOptions::operator=(const MinimizerOptions & opt) {  
00141    // assignment operator 
00142    if (this == &opt) return *this; // self assignment
00143    fLevel = opt.fLevel;
00144    fMaxCalls = opt.fMaxCalls; 
00145    fMaxIter = opt.fMaxIter; 
00146    fStrategy = opt.fStrategy; 
00147    fErrorDef = opt.fErrorDef;
00148    fTolerance = opt.fTolerance;
00149    fPrecision = opt.fPrecision; 
00150    fMinimType = opt.fMinimType; 
00151    fAlgoType = opt.fAlgoType; 
00152 
00153    if (fExtraOptions) delete fExtraOptions; 
00154    fExtraOptions = 0; 
00155    if (opt.fExtraOptions)  fExtraOptions =  (opt.fExtraOptions)->Clone();
00156    return *this;
00157 }
00158 
00159 MinimizerOptions::~MinimizerOptions() { 
00160    if (fExtraOptions) delete fExtraOptions; 
00161 }
00162 
00163 void MinimizerOptions::SetExtraOptions(const IOptions & opt) {  
00164    // set extra options (clone the passed one)
00165    if (fExtraOptions) delete fExtraOptions; 
00166    fExtraOptions = opt.Clone(); 
00167 }
00168 
00169 void MinimizerOptions::Print(std::ostream & os) const {
00170    //print all the options
00171    os << std::setw(25) << "Minimizer Type"        << " : " << std::setw(15) << fMinimType << std::endl;
00172    os << std::setw(25) << "Minimizer Algorithm"   << " : " << std::setw(15) << fAlgoType << std::endl;
00173    os << std::setw(25) << "Strategy"              << " : " << std::setw(15) << fStrategy << std::endl;
00174    os << std::setw(25) << "Tolerance"              << " : " << std::setw(15) << fTolerance << std::endl;
00175    os << std::setw(25) << "Max func calls"         << " : " << std::setw(15) << fMaxCalls << std::endl;
00176    os << std::setw(25) << "Max iterations"         << " : " << std::setw(15) << fMaxIter << std::endl;
00177    os << std::setw(25) << "Func Precision"         << " : " << std::setw(15) << fPrecision << std::endl;
00178    os << std::setw(25) << "Error definition"       << " : " << std::setw(15) << fErrorDef << std::endl;
00179    os << std::setw(25) << "Print Level"            << " : " << std::setw(15) << fLevel << std::endl;
00180    
00181    if (ExtraOptions()) { 
00182       os << fMinimType << " specific options :"  << std::endl;
00183       ExtraOptions()->Print(os);
00184    }
00185 }
00186 
00187 IOptions & MinimizerOptions::Default(const char * name) { 
00188    // create default extra options for the given algorithm type 
00189    return GenAlgoOptions::Default(name);
00190 }
00191 
00192 IOptions * MinimizerOptions::FindDefault(const char * name) { 
00193    // find extra options for the given algorithm type 
00194    return GenAlgoOptions::FindDefault(name);
00195 }
00196 
00197 void MinimizerOptions::PrintDefault(const char * name, std::ostream & os) {
00198    //print default options
00199    MinimizerOptions tmp;
00200    tmp.Print(os);
00201    if (!tmp.ExtraOptions() ) {
00202       IOptions * opt = FindDefault(name);
00203       os << "Specific options for "  << name << std::endl;
00204       if (opt) opt->Print(os);
00205    }
00206 }
00207 
00208 
00209 
00210 
00211 } // end namespace Math
00212 
00213 } // end namespace ROOT
00214 

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