Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 }
48 void MinimizerOptions::SetDefaultErrorDef(double up) {
49  // set the default error definition
50  Minim::gDefaultErrorDef = up;
51 }
52 void MinimizerOptions::SetDefaultTolerance(double tol) {
53  // set the default tolerance
54  Minim::gDefaultTolerance = tol;
55 }
56 void MinimizerOptions::SetDefaultPrecision(double prec) {
57  // set the default precision
58  Minim::gDefaultPrecision = prec;
59 }
60 void MinimizerOptions::SetDefaultMaxFunctionCalls(int maxcall) {
61  // set the default maximum number of function calls
62  Minim::gDefaultMaxCalls = maxcall;
63 }
64 void MinimizerOptions::SetDefaultMaxIterations(int maxiter) {
65  // set the default maximum number of iterations
66  Minim::gDefaultMaxIter = maxiter;
67 }
68 void MinimizerOptions::SetDefaultStrategy(int stra) {
69  // set the default minimization strategy
70  Minim::gDefaultStrategy = stra;
71 }
72 void MinimizerOptions::SetDefaultPrintLevel(int level) {
73  // set the default printing level
74  Minim::gDefaultPrintLevel = level;
75 }
76 void MinimizerOptions::SetDefaultExtraOptions(const IOptions * extraoptions) {
77  // set the pointer to default extra options
78  delete Minim::gDefaultExtraOptions;
79  Minim::gDefaultExtraOptions = (extraoptions) ? extraoptions->Clone() : 0;
80 }
81 
82 const std::string & MinimizerOptions::DefaultMinimizerAlgo() { return Minim::gDefaultMinimAlgo; }
83 double MinimizerOptions::DefaultErrorDef() { return Minim::gDefaultErrorDef; }
84 double MinimizerOptions::DefaultTolerance() { return Minim::gDefaultTolerance; }
85 double MinimizerOptions::DefaultPrecision() { return Minim::gDefaultPrecision; }
86 int MinimizerOptions::DefaultMaxFunctionCalls() { return Minim::gDefaultMaxCalls; }
87 int MinimizerOptions::DefaultMaxIterations() { return Minim::gDefaultMaxIter; }
88 int MinimizerOptions::DefaultStrategy() { return Minim::gDefaultStrategy; }
89 int MinimizerOptions::DefaultPrintLevel() { return Minim::gDefaultPrintLevel; }
90 IOptions * MinimizerOptions::DefaultExtraOptions() { return Minim::gDefaultExtraOptions; }
91 
92 const std::string & MinimizerOptions::DefaultMinimizerType()
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)
99  return Minim::gDefaultMinimizer;
100 
101  Minim::gDefaultMinimizer = "Minuit2"; // in case no PM exists
102 
103 #else
104  R__READ_LOCKGUARD(ROOT::gCoreMutex);
105 
106  if (Minim::gDefaultMinimizer.size() != 0)
107  return Minim::gDefaultMinimizer;
108 
109  R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
110 
111  // Another thread also waiting for the write lock might have
112  // done the assignment
113  if (Minim::gDefaultMinimizer.size() != 0)
114  return Minim::gDefaultMinimizer;
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 
121  return Minim::gDefaultMinimizer;
122 }
123 
124 
125 MinimizerOptions::MinimizerOptions():
126  fExtraOptions(0)
127 {
128  // constructor using the default options
129 
130  ResetToDefaultOptions();
131 }
132 
133 
134 MinimizerOptions::MinimizerOptions(const MinimizerOptions & opt) : fExtraOptions(0) {
135  // copy constructor
136  (*this) = opt;
137 }
138 
139 MinimizerOptions & MinimizerOptions::operator=(const MinimizerOptions & opt) {
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 
158 MinimizerOptions::~MinimizerOptions() {
159  delete fExtraOptions;
160 }
161 
162 void MinimizerOptions::ResetToDefaultOptions() {
163  fLevel = Minim::gDefaultPrintLevel;
164  fMaxCalls = Minim::gDefaultMaxCalls;
165  fMaxIter = Minim::gDefaultMaxIter;
166  fStrategy = Minim::gDefaultStrategy;
167  fErrorDef = Minim::gDefaultErrorDef;
168  fTolerance = Minim::gDefaultTolerance;
169  fPrecision = Minim::gDefaultPrecision;
170 
171  fMinimType = MinimizerOptions::DefaultMinimizerType();
172 
173  fAlgoType = Minim::gDefaultMinimAlgo;
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)
187  if (Minim::gDefaultExtraOptions)
188  fExtraOptions = Minim::gDefaultExtraOptions->Clone();
189  else {
190  IOptions * gopts = FindDefault( fMinimType.c_str() );
191  if (gopts) fExtraOptions = gopts->Clone();
192  }
193 }
194 
195 void MinimizerOptions::SetExtraOptions(const IOptions & opt) {
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 
219 IOptions & MinimizerOptions::Default(const char * name) {
220  // create default extra options for the given algorithm type
221  return GenAlgoOptions::Default(name);
222 }
223 
224 IOptions * MinimizerOptions::FindDefault(const char * name) {
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