Logo ROOT   6.12/07
Reference Guide
MinimizerOptions.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Fri Aug 15 2008
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2008 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 #include "Math/MinimizerOptions.h"
12 
13 #include "Math/GenAlgoOptions.h"
14 
15 // case of using ROOT plug-in manager
16 #ifndef MATH_NO_PLUGIN_MANAGER
17 #include "TEnv.h"
18 #include "TVirtualRWMutex.h"
19 #endif
20 
21 
22 #include <iomanip>
23 
24 namespace ROOT {
25 
26 
27 namespace Math {
28 
29  namespace Minim {
30  static std::string gDefaultMinimizer = ""; // take from /etc/system.rootrc in ROOT Fitter
31  static std::string gDefaultMinimAlgo = "Migrad";
32  static double gDefaultErrorDef = 1.;
33  static double gDefaultTolerance = 1.E-2;
34  static double gDefaultPrecision = -1; // value <= 0 means left to minimizer
35  static int gDefaultMaxCalls = 0; // 0 means leave default values Deaf
36  static int gDefaultMaxIter = 0;
37  static int gDefaultStrategy = 1;
38  static int gDefaultPrintLevel = 0;
39  static IOptions * gDefaultExtraOptions = 0; // pointer to default extra options
40  }
41 
42 
43 void MinimizerOptions::SetDefaultMinimizer(const char * type, const char * algo) {
44  // set the default minimizer type and algorithm
45  if (type) Minim::gDefaultMinimizer = std::string(type);
46  if (algo) Minim::gDefaultMinimAlgo = std::string(algo);
47 }
49  // set the default error definition
51 }
53  // set the default tolerance
55 }
57  // set the default precision
59 }
61  // set the default maximum number of function calls
62  Minim::gDefaultMaxCalls = maxcall;
63 }
65  // set the default maximum number of iterations
66  Minim::gDefaultMaxIter = maxiter;
67 }
69  // set the default minimization strategy
71 }
73  // set the default printing level
75 }
77  // set the pointer to default extra options
79  Minim::gDefaultExtraOptions = (extraoptions) ? extraoptions->Clone() : 0;
80 }
81 
91 
93 {
94  // return default minimizer
95  // if is "" (no default is set) read from etc/system.rootrc
96 
97 #ifdef MATH_NO_PLUGIN_MANAGER
98  if (Minim::gDefaultMinimizer.size() != 0)
100 
101  Minim::gDefaultMinimizer = "Minuit2"; // in case no PM exists
102 
103 #else
105 
106  if (Minim::gDefaultMinimizer.size() != 0)
108 
110 
111  // Another thread also waiting for the write lock might have
112  // done the assignment
113  if (Minim::gDefaultMinimizer.size() != 0)
115 
116  // use value defined in etc/system.rootrc (if not found Minuit is used)
117  if (gEnv)
118  Minim::gDefaultMinimizer = gEnv->GetValue("Root.Fitter","Minuit");
119 #endif
120 
122 }
123 
124 
126  fExtraOptions(0)
127 {
128  // constructor using the default options
129 
131 }
132 
133 
135  // copy constructor
136  (*this) = opt;
137 }
138 
140  // assignment operator
141  if (this == &opt) return *this; // self assignment
142  fLevel = opt.fLevel;
143  fMaxCalls = opt.fMaxCalls;
144  fMaxIter = opt.fMaxIter;
145  fStrategy = opt.fStrategy;
146  fErrorDef = opt.fErrorDef;
147  fTolerance = opt.fTolerance;
148  fPrecision = opt.fPrecision;
149  fMinimType = opt.fMinimType;
150  fAlgoType = opt.fAlgoType;
151 
152  delete fExtraOptions;
153  fExtraOptions = (opt.fExtraOptions) ? (opt.fExtraOptions)->Clone() : 0;
154 
155  return *this;
156 }
157 
159  delete fExtraOptions;
160 }
161 
170 
172 
174 
175  // case of Fumili2 and TMinuit
176  if (fMinimType == "TMinuit") fMinimType = "Minuit";
177  else if (fMinimType == "Fumili2") {
178  fMinimType = "Minuit2";
179  fAlgoType = "Fumili";
180  }
181  else if (fMinimType == "GSLMultiMin" && fAlgoType == "Migrad")
182  fAlgoType = "BFGS2";
183 
184  delete fExtraOptions;
185  fExtraOptions = 0;
186  // check if extra options exists (copy them if needed)
189  else {
190  IOptions * gopts = FindDefault( fMinimType.c_str() );
191  if (gopts) fExtraOptions = gopts->Clone();
192  }
193 }
194 
196  // set extra options (clone the passed one)
197  delete fExtraOptions;
198  fExtraOptions = opt.Clone();
199 }
200 
201 void MinimizerOptions::Print(std::ostream & os) const {
202  //print all the options
203  os << std::setw(25) << "Minimizer Type" << " : " << std::setw(15) << fMinimType << std::endl;
204  os << std::setw(25) << "Minimizer Algorithm" << " : " << std::setw(15) << fAlgoType << std::endl;
205  os << std::setw(25) << "Strategy" << " : " << std::setw(15) << fStrategy << std::endl;
206  os << std::setw(25) << "Tolerance" << " : " << std::setw(15) << fTolerance << std::endl;
207  os << std::setw(25) << "Max func calls" << " : " << std::setw(15) << fMaxCalls << std::endl;
208  os << std::setw(25) << "Max iterations" << " : " << std::setw(15) << fMaxIter << std::endl;
209  os << std::setw(25) << "Func Precision" << " : " << std::setw(15) << fPrecision << std::endl;
210  os << std::setw(25) << "Error definition" << " : " << std::setw(15) << fErrorDef << std::endl;
211  os << std::setw(25) << "Print Level" << " : " << std::setw(15) << fLevel << std::endl;
212 
213  if (ExtraOptions()) {
214  os << fMinimType << " specific options :" << std::endl;
215  ExtraOptions()->Print(os);
216  }
217 }
218 
220  // create default extra options for the given algorithm type
221  return GenAlgoOptions::Default(name);
222 }
223 
225  // find extra options for the given algorithm type
226  return GenAlgoOptions::FindDefault(name);
227 }
228 
229 void MinimizerOptions::PrintDefault(const char * name, std::ostream & os) {
230  //print default options
231  MinimizerOptions tmp;
232  tmp.Print(os);
233  if (!tmp.ExtraOptions() ) {
234  IOptions * opt = FindDefault(name);
235  os << "Specific options for " << name << std::endl;
236  if (opt) opt->Print(os);
237  }
238 }
239 
240 
241 
242 
243 } // end namespace Math
244 
245 } // end namespace ROOT
246 
static IOptions * gDefaultExtraOptions
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void Print(std::ostream &os=std::cout) const
print all the options
static IOptions * DefaultExtraOptions()
virtual void Print(std::ostream &=std::cout) const
print options
Definition: IOptions.h:98
static int gDefaultMaxCalls
const IOptions * ExtraOptions() const
return extra options (NULL pointer if they are not present)
static void SetDefaultExtraOptions(const IOptions *extraoptions)
static double gDefaultPrecision
static void SetDefaultMaxIterations(int maxiter)
static ROOT::Math::IOptions & Default(const char *name)
retrieve extra options - if not existing create a IOptions
void ResetToDefaultOptions()
non-static methods for setting options
static std::string gDefaultMinimAlgo
static void SetDefaultPrintLevel(int level)
MinimizerOptions & operator=(const MinimizerOptions &opt)
assignment operators
#define R__WRITE_LOCKGUARD(mutex)
static int gDefaultStrategy
R__EXTERN TVirtualRWMutex * gCoreMutex
void SetExtraOptions(const IOptions &opt)
set extra options (in this case pointer is cloned)
static void SetDefaultErrorDef(double up)
static void SetDefaultStrategy(int strat)
#define R__READ_LOCKGUARD(mutex)
static void SetDefaultMaxFunctionCalls(int maxcall)
static ROOT::Math::IOptions * FindDefault(const char *name)
static const std::string & DefaultMinimizerType()
static void PrintDefault(const char *name, std::ostream &os=std::cout)
print all the default options for the name given
ROOT::Math::IOptions * fExtraOptions
static double gDefaultTolerance
virtual IOptions * Clone() const =0
static int gDefaultPrintLevel
static std::string gDefaultMinimizer
static const std::string & DefaultMinimizerAlgo()
static double gDefaultErrorDef
static void SetDefaultTolerance(double tol)
int type
Definition: TGX11.cxx:120
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
static void SetDefaultPrecision(double prec)
Namespace for new Math classes and functions.
static IOptions & Default(const char *algoname)
Generic interface for defining configuration options of a numerical algorithm.
Definition: IOptions.h:30
static int gDefaultMaxIter
static IOptions * FindDefault(const char *algoname)
static void SetDefaultMinimizer(const char *type, const char *algo=0)
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:491
char name[80]
Definition: TGX11.cxx:109