Logo ROOT   6.16/01
Reference Guide
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
35namespace ROOT {
36
37namespace Fit {
38
39
40
41FitConfig::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
54{
55 // destructor implementation. No Operations
56}
57
59 // Implementation of copy constructor
60 (*this) = rhs;
61}
62
64 // Implementation of assignment operator.
65 if (this == &rhs) return *this; // time saving self-test
66
72
73 fSettings = rhs.fSettings;
75
77
78 return *this;
79}
80
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
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
136void 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
175 // create minimizer according to the chosen configuration using the
176 // plug-in manager
177
178 const std::string & minimType = fMinimizerOpts.MinimizerType();
179 const std::string & algoType = fMinimizerOpts.MinimizerAlgorithm();
180
181 std::string defaultMinim = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
183 // check if a different minimizer is used (in case a default value is passed, then set correctly in FitConfig)
184 const std::string & minim_newDefault = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
185 if (defaultMinim != minim_newDefault ) fMinimizerOpts.SetMinimizerType(minim_newDefault.c_str());
186
187 if (min == 0) {
188 // if creation of minimizer failed force the use by default of Minuit
189 std::string minim2 = "Minuit";
190 if (minimType == "Minuit") minim2 = "Minuit2";
191 if (minimType != minim2 ) {
192 std::string msg = "Could not create the " + minimType + " minimizer. Try using the minimizer " + minim2;
193 MATH_WARN_MSG("FitConfig::CreateMinimizer",msg.c_str());
194 min = ROOT::Math::Factory::CreateMinimizer(minim2,"Migrad");
195 if (min == 0) {
196 MATH_ERROR_MSG("FitConfig::CreateMinimizer","Could not create the Minuit2 minimizer");
197 return 0;
198 }
199 SetMinimizer( minim2.c_str(),"Migrad");
200 }
201 else {
202 std::string msg = "Could not create the Minimizer " + minimType;
203 MATH_ERROR_MSG("FitConfig::CreateMinimizer",msg.c_str());
204 return 0;
205 }
206 }
207
208 // set default max of function calls according to the number of parameters
209 // formula from Minuit2 (adapted)
210 if (fMinimizerOpts.MaxFunctionCalls() == 0) {
211 unsigned int npar = fSettings.size();
212 int maxfcn = 1000 + 100*npar + 5*npar*npar;
214 }
215
216
217 // set default minimizer control parameters
226
227
228 return min;
229}
230
231void FitConfig::SetDefaultMinimizer(const char * type, const char *algo ) {
232 // set the default minimizer type and algorithms
234}
235
237 // set all the minimizer options
238 fMinimizerOpts = minopt;
239}
240
241std::vector<double> FitConfig::ParamsValues() const {
242
243 std::vector<double> params(NPar() );
244 for (unsigned int i = 0; i < params.size(); ++i) {
245 params[i] = fSettings[i].Value();
246 }
247 return params;
248}
249 } // end namespace Fit
250
251} // end namespace ROOT
252
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:82
#define MATH_WARN_MSG(loc, str)
Definition: Error.h:79
int type
Definition: TGX11.cxx:120
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
Definition: FitConfig.h:46
void SetParamsSettings(unsigned int npar, const double *params, const double *vstep=0)
set the parameter settings from number of parameters and a vector of values and optionally step value...
Definition: FitConfig.cxx:136
std::vector< unsigned int > fMinosParams
Definition: FitConfig.h:263
FitConfig(unsigned int npar=0)
Default constructor.
Definition: FitConfig.cxx:41
std::vector< double > ParamsValues() const
return a vector of stored parameter values (i.e initial fit parameters)
Definition: FitConfig.cxx:241
void SetNormErrors(bool on=true)
set the option to normalize the error on the result according to chi2/ndf
Definition: FitConfig.h:220
void SetMinimizer(const char *type, const char *algo=0)
set minimizer type
Definition: FitConfig.h:180
void SetMinimizerOptions(const ROOT::Math::MinimizerOptions &minopt)
set all the minimizer options using class MinimizerOptions
Definition: FitConfig.cxx:236
unsigned int NPar() const
number of parameters settings
Definition: FitConfig.h:95
std::vector< ROOT::Fit::ParameterSettings > fSettings
Definition: FitConfig.h:262
ROOT::Math::MinimizerOptions fMinimizerOpts
Definition: FitConfig.h:265
~FitConfig()
Destructor.
Definition: FitConfig.cxx:53
ROOT::Math::Minimizer * CreateMinimizer()
create a new minimizer according to chosen configuration
Definition: FitConfig.cxx:174
void SetFromFitResult(const FitResult &rhs)
Definition: FitConfig.cxx:81
static void SetDefaultMinimizer(const char *type, const char *algo=0)
static function to control default minimizer type and algorithm
Definition: FitConfig.cxx:231
FitConfig & operator=(const FitConfig &rhs)
Definition: FitConfig.cxx:63
class containg the result of the fit and all the related information (fitted parameter values,...
Definition: FitResult.h:48
bool NormalizedErrors() const
flag to chek if errors are normalized
Definition: FitResult.h:301
bool IsParameterFixed(unsigned int ipar) const
query if a parameter is fixed
Definition: FitResult.cxx:422
double Error(unsigned int i) const
parameter error by index
Definition: FitResult.h:187
double Value(unsigned int i) const
parameter value by index
Definition: FitResult.h:180
const std::string & MinimizerType() const
minimization quantities
Definition: FitResult.h:103
bool ParameterBounds(unsigned int ipar, double &lower, double &upper) const
retrieve parameter bounds - return false if parameter is not bound
Definition: FitResult.cxx:426
bool HasMinosError(unsigned int i) const
query if parameter i has the Minos error
Definition: FitResult.cxx:382
unsigned int NPar() const
total number of parameters (abbreviation)
Definition: FitResult.h:132
std::string ParName(unsigned int i) const
name of the parameter
Definition: FitResult.cxx:439
Class, describing value, limits and step size of the parameters Provides functionality also to set/re...
static ROOT::Math::Minimizer * CreateMinimizer(const std::string &minimizerType="", const std::string &algoType="")
static method to create the corrisponding Minimizer given the string Supported Minimizers types are: ...
Definition: Factory.cxx:63
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
static void SetDefaultMinimizer(const char *type, const char *algo=0)
int Strategy() const
strategy
double Tolerance() const
absolute tolerance
double Precision() const
precision in the objective funciton calculation (value <=0 means left to default)
void SetMinimizerType(const char *type)
set minimizer type
const std::string & MinimizerAlgorithm() const
type of algorithm
double ErrorDef() const
error definition
static const std::string & DefaultMinimizerType()
const std::string & MinimizerType() const
type of minimizer
unsigned int MaxIterations() const
max iterations
unsigned int MaxFunctionCalls() const
max number of function calls
int PrintLevel() const
non-static methods for retrieving options
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition: Minimizer.h:78
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: Minimizer.h:451
void SetErrorDef(double up)
set scale for calculating the errors
Definition: Minimizer.h:464
void SetValidError(bool on)
flag to check if minimizer needs to perform accurate error analysis (e.g. run Hesse for Minuit)
Definition: Minimizer.h:467
void SetTolerance(double tol)
set the tolerance
Definition: Minimizer.h:454
void SetPrintLevel(int level)
set print level
Definition: Minimizer.h:445
void SetStrategy(int strategyLevel)
set the strategy
Definition: Minimizer.h:461
void SetPrecision(double prec)
set in the minimizer the objective function evaluation precision ( a value <=0 means the minimizer wi...
Definition: Minimizer.h:458
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition: Minimizer.h:448
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
std::string ToString(const T &val)
Utility function for conversion to strings.
Definition: Util.h:49
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
STL namespace.