Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
FitConfig.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Thu Sep 21 16:21:29 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Implementation file for class FitConfig
12 
13 #include "Fit/FitConfig.h"
14 
15 #include "Fit/FitResult.h"
16 
17 #include "Math/IParamFunction.h"
18 #include "Math/Util.h"
19 
20 #include "Math/Minimizer.h"
21 #include "Math/Factory.h"
22 
23 #include <cmath>
24 
25 #include <string>
26 #include <sstream>
27 
28 #include "Math/Error.h"
29 
30 //#define DEBUG
31 #ifdef DEBUG
32 #endif
33 #include <iostream>
34 
35 namespace ROOT {
36 
37 namespace Fit {
38 
39 
40 
41 FitConfig::FitConfig(unsigned int npar) :
42  fNormErrors(false),
43  fParabErrors(false), // ensure that in any case correct parabolic errors are estimated
44  fMinosErrors(false), // do full Minos error analysis for all parameters
45  fUpdateAfterFit(true), // update after fit
46  fWeightCorr(false),
47  fSettings(std::vector<ParameterSettings>(npar) )
48 {
49  // constructor implementation
50 }
51 
52 
53 FitConfig::~FitConfig()
54 {
55  // destructor implementation. No Operations
56 }
57 
58 FitConfig::FitConfig(const FitConfig &rhs) {
59  // Implementation of copy constructor
60  (*this) = rhs;
61 }
62 
63 FitConfig & FitConfig::operator = (const FitConfig &rhs) {
64  // Implementation of assignment operator.
65  if (this == &rhs) return *this; // time saving self-test
66 
67  fNormErrors = rhs.fNormErrors;
68  fParabErrors = rhs.fParabErrors;
69  fMinosErrors = rhs.fMinosErrors;
70  fUpdateAfterFit = rhs.fUpdateAfterFit;
71  fWeightCorr = rhs.fWeightCorr;
72 
73  fSettings = rhs.fSettings;
74  fMinosParams = rhs.fMinosParams;
75 
76  fMinimizerOpts = rhs.fMinimizerOpts;
77 
78  return *this;
79 }
80 
81 void FitConfig::SetFromFitResult(const FitResult &result) {
82  // Implementation of setting of parameters from the result of the fit
83  // all the other options will stay the same.
84  // If the size of parameters do not match they will be re-created
85  // but in that case the bound on the parameter will be lost
86 
87  unsigned int npar = result.NPar();
88  if (fSettings.size() != npar) {
89  fSettings.clear();
90  fSettings.resize(npar);
91  }
92  // fill the parameter settings
93  for (unsigned int i = 0; i < npar; ++i) {
94  if (result.IsParameterFixed(i) )
95  fSettings[i].Set(result.ParName(i), result.Value(i) );
96  else {
97  fSettings[i].Set( result.ParName(i), result.Value(i), result.Error(i) );
98  // check if parameter is bound
99  double lower = 0;
100  double upper = 0;
101  if (result.ParameterBounds(i,lower,upper) ) {
102  if (lower == -std::numeric_limits<double>::infinity()) fSettings[i].SetUpperLimit(upper);
103  else if (upper == std::numeric_limits<double>::infinity()) fSettings[i].SetLowerLimit(lower);
104  else fSettings[i].SetLimits(lower,upper);
105  }
106 
107  // query if parameter needs to run Minos
108  if (result.HasMinosError(i) ) {
109  if (fMinosParams.size() == 0) {
110  fMinosErrors = true;
111  fMinosParams.reserve(npar-i);
112  }
113  fMinosParams.push_back(i);
114  }
115  }
116  }
117 
118  // set information about errors
119  SetNormErrors( result.NormalizedErrors() );
120 
121  // set also minimizer type
122  // algorithm is after " / "
123  const std::string & minname = result.MinimizerType();
124  size_t pos = minname.find(" / ");
125  if (pos != std::string::npos) {
126  std::string minimType = minname.substr(0,pos);
127  std::string algoType = minname.substr(pos+3,minname.length() );
128  SetMinimizer(minimType.c_str(), algoType.c_str() );
129  }
130  else {
131  SetMinimizer(minname.c_str());
132  }
133 }
134 
135 
136 void FitConfig::SetParamsSettings(unsigned int npar, const double *params, const double * vstep ) {
137  // initialize FitConfig from given parameter values and step sizes
138  // if npar different than existing one - clear old one and create new ones
139  if (params == 0) {
140  fSettings = std::vector<ParameterSettings>(npar);
141  return;
142  }
143  // if a vector of parameters is given and parameters are not existing or are of different size
144  bool createNew = false;
145  if (npar != fSettings.size() ) {
146  fSettings.clear();
147  fSettings.reserve(npar);
148  createNew = true;
149  }
150  unsigned int i = 0;
151  const double * end = params+npar;
152  for (const double * ipar = params; ipar != end; ++ipar) {
153  double val = *ipar;
154  double step = 0;
155  if (vstep == 0) {
156  step = 0.3*std::fabs(val); // step size is 30% of par value
157  //double step = 2.0*std::fabs(val); // step size is 30% of par value
158  if (val == 0) step = 0.3;
159  }
160  else
161  step = vstep[i];
162 
163  if (createNew)
164  fSettings.push_back( ParameterSettings("Par_" + ROOT::Math::Util::ToString(i), val, step ) );
165  else {
166  fSettings[i].SetValue(val);
167  fSettings[i].SetStepSize(step);
168  }
169 
170  i++;
171  }
172 }
173 
174 ROOT::Math::Minimizer * FitConfig::CreateMinimizer() {
175  // create minimizer according to the chosen configuration using the
176  // plug-in manager
177 
178  // in case of empty string usesd default values
179  if (fMinimizerOpts.MinimizerType().empty())
180  fMinimizerOpts.SetMinimizerType(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str());
181  if (fMinimizerOpts.MinimizerAlgorithm().empty())
182  fMinimizerOpts.SetMinimizerAlgorithm(ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
183 
184  const std::string &minimType = fMinimizerOpts.MinimizerType();
185  const std::string & algoType = fMinimizerOpts.MinimizerAlgorithm();
186 
187  std::string defaultMinim = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
188 
189  ROOT::Math::Minimizer * min = ROOT::Math::Factory::CreateMinimizer(minimType, algoType);
190  // check if a different minimizer is used (in case a default value is passed, then set correctly in FitConfig)
191  const std::string & minim_newDefault = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
192  if (defaultMinim != minim_newDefault ) fMinimizerOpts.SetMinimizerType(minim_newDefault.c_str());
193 
194  if (min == 0) {
195  // if creation of minimizer failed force the use by default of Minuit
196  std::string minim2 = "Minuit";
197  if (minimType == "Minuit") minim2 = "Minuit2";
198  if (minimType != minim2 ) {
199  std::string msg = "Could not create the " + minimType + " minimizer. Try using the minimizer " + minim2;
200  MATH_WARN_MSG("FitConfig::CreateMinimizer",msg.c_str());
201  min = ROOT::Math::Factory::CreateMinimizer(minim2,"Migrad");
202  if (min == 0) {
203  MATH_ERROR_MSG("FitConfig::CreateMinimizer","Could not create the Minuit2 minimizer");
204  return 0;
205  }
206  SetMinimizer( minim2.c_str(),"Migrad");
207  }
208  else {
209  std::string msg = "Could not create the Minimizer " + minimType;
210  MATH_ERROR_MSG("FitConfig::CreateMinimizer",msg.c_str());
211  return 0;
212  }
213  }
214 
215  // set default max of function calls according to the number of parameters
216  // formula from Minuit2 (adapted)
217  if (fMinimizerOpts.MaxFunctionCalls() == 0) {
218  unsigned int npar = fSettings.size();
219  int maxfcn = 1000 + 100*npar + 5*npar*npar;
220  fMinimizerOpts.SetMaxFunctionCalls(maxfcn);
221  }
222 
223 
224  // set default minimizer control parameters
225  min->SetPrintLevel( fMinimizerOpts.PrintLevel() );
226  min->SetMaxFunctionCalls( fMinimizerOpts.MaxFunctionCalls() );
227  min->SetMaxIterations( fMinimizerOpts.MaxIterations() );
228  min->SetTolerance( fMinimizerOpts.Tolerance() );
229  min->SetPrecision( fMinimizerOpts.Precision() );
230  min->SetValidError( fParabErrors );
231  min->SetStrategy( fMinimizerOpts.Strategy() );
232  min->SetErrorDef( fMinimizerOpts.ErrorDef() );
233 
234 
235  return min;
236 }
237 
238 std::string FitConfig::MinimizerName() const
239 {
240  // set minimizer type
241  std::string name = MinimizerType();
242 
243  // append algorithm name for minimizer that support it
244  if ((name.find("Fumili") == std::string::npos) && (name.find("GSLMultiFit") == std::string::npos)) {
245  if (MinimizerAlgoType() != "")
246  name += " / " + MinimizerAlgoType();
247  }
248  return name;
249 }
250 
251 void FitConfig::SetDefaultMinimizer(const char * type, const char *algo ) {
252  // set the default minimizer type and algorithms
253  ROOT::Math::MinimizerOptions::SetDefaultMinimizer(type, algo);
254 }
255 
256 void FitConfig::SetMinimizerOptions(const ROOT::Math::MinimizerOptions & minopt) {
257  // set all the minimizer options
258  fMinimizerOpts = minopt;
259 }
260 
261 std::vector<double> FitConfig::ParamsValues() const {
262 
263  std::vector<double> params(NPar() );
264  for (unsigned int i = 0; i < params.size(); ++i) {
265  params[i] = fSettings[i].Value();
266  }
267  return params;
268 }
269  } // end namespace Fit
270 
271 } // end namespace ROOT