Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
34namespace ROOT {
35
36namespace Fit {
37
38
39
40FitConfig::FitConfig(unsigned int npar) :
41 fNormErrors(false),
42 fParabErrors(false), // ensure that in any case correct parabolic errors are estimated
43 fMinosErrors(false), // do full Minos error analysis for all parameters
44 fUpdateAfterFit(true), // update after fit
45 fWeightCorr(false),
46 fSettings(std::vector<ParameterSettings>(npar) )
47{
48 // constructor implementation
49}
50
51
53{
54 // destructor implementation. No Operations
55}
56
58 // Implementation of copy constructor
59 (*this) = rhs;
60}
61
63 // Implementation of assignment operator.
64 if (this == &rhs) return *this; // time saving self-test
65
71
72 fSettings = rhs.fSettings;
74
76
77 return *this;
78}
79
81 // Implementation of setting of parameters from the result of the fit
82 // all the other options will stay the same.
83 // If the size of parameters do not match they will be re-created
84 // but in that case the bound on the parameter will be lost
85
86 unsigned int npar = result.NPar();
87 if (fSettings.size() != npar) {
88 fSettings.clear();
89 fSettings.resize(npar);
90 }
91 // fill the parameter settings
92 for (unsigned int i = 0; i < npar; ++i) {
93 if (result.IsParameterFixed(i) )
94 fSettings[i].Set(result.ParName(i), result.Value(i) );
95 else {
96 fSettings[i].Set( result.ParName(i), result.Value(i), result.Error(i) );
97 // check if parameter is bound
98 double lower = 0;
99 double upper = 0;
100 if (result.ParameterBounds(i,lower,upper) ) {
101 if (lower == -std::numeric_limits<double>::infinity()) fSettings[i].SetUpperLimit(upper);
102 else if (upper == std::numeric_limits<double>::infinity()) fSettings[i].SetLowerLimit(lower);
103 else fSettings[i].SetLimits(lower,upper);
104 }
105
106 // query if parameter needs to run Minos
107 if (result.HasMinosError(i) ) {
108 if (fMinosParams.empty()) {
109 fMinosErrors = true;
110 fMinosParams.reserve(npar-i);
111 }
112 fMinosParams.push_back(i);
113 }
114 }
115 }
116
117 // set information about errors
118 SetNormErrors( result.NormalizedErrors() );
119
120 // set also minimizer type
121 // algorithm is after " / "
122 const std::string & minname = result.MinimizerType();
123 size_t pos = minname.find(" / ");
124 if (pos != std::string::npos) {
125 std::string minimType = minname.substr(0,pos);
126 std::string algoType = minname.substr(pos+3,minname.length() );
127 SetMinimizer(minimType.c_str(), algoType.c_str() );
128 }
129 else {
130 SetMinimizer(minname.c_str());
131 }
132}
133
134
135void FitConfig::SetParamsSettings(unsigned int npar, const double *params, const double * vstep ) {
136 // initialize FitConfig from given parameter values and step sizes
137 // if npar different than existing one - clear old one and create new ones
138 if (params == nullptr) {
139 fSettings = std::vector<ParameterSettings>(npar);
140 return;
141 }
142 // if a vector of parameters is given and parameters are not existing or are of different size
143 bool createNew = false;
144 if (npar != fSettings.size() ) {
145 fSettings.clear();
146 fSettings.reserve(npar);
147 createNew = true;
148 }
149 unsigned int i = 0;
150 const double * end = params+npar;
151 for (const double * ipar = params; ipar != end; ++ipar) {
152 double val = *ipar;
153 double step = 0;
154 if (vstep == nullptr) {
155 step = 0.3*std::fabs(val); // step size is 30% of par value
156 //double step = 2.0*std::fabs(val); // step size is 30% of par value
157 if (val == 0) step = 0.3;
158 }
159 else
160 step = vstep[i];
161
162 if (createNew)
163 fSettings.push_back( ParameterSettings("Par_" + ROOT::Math::Util::ToString(i), val, step ) );
164 else {
165 fSettings[i].SetValue(val);
166 fSettings[i].SetStepSize(step);
167 }
168
169 i++;
170 }
171}
172
174 // create minimizer according to the chosen configuration using the
175 // plug-in manager
176
177 // in case of empty string usesd default values
178 if (fMinimizerOpts.MinimizerType().empty())
182
183 const std::string &minimType = fMinimizerOpts.MinimizerType();
184 const std::string & algoType = fMinimizerOpts.MinimizerAlgorithm();
185
186 std::string defaultMinim = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
187
189 // check if a different minimizer is used (in case a default value is passed, then set correctly in FitConfig)
190 const std::string & minim_newDefault = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
191 if (defaultMinim != minim_newDefault ) fMinimizerOpts.SetMinimizerType(minim_newDefault.c_str());
192
193 if (min == nullptr) {
194 // if creation of minimizer failed force the use by default of Minuit
195 std::string minim2 = "Minuit";
196 if (minimType == "Minuit") minim2 = "Minuit2";
197 if (minimType != minim2 ) {
198 std::string msg = "Could not create the " + minimType + " minimizer. Try using the minimizer " + minim2;
199 MATH_WARN_MSG("FitConfig::CreateMinimizer",msg.c_str());
200 min = ROOT::Math::Factory::CreateMinimizer(minim2,"Migrad");
201 if (min == nullptr) {
202 MATH_ERROR_MSG("FitConfig::CreateMinimizer","Could not create the Minuit2 minimizer");
203 return nullptr;
204 }
205 SetMinimizer( minim2.c_str(),"Migrad");
206 }
207 else {
208 std::string msg = "Could not create the Minimizer " + minimType;
209 MATH_ERROR_MSG("FitConfig::CreateMinimizer",msg.c_str());
210 return nullptr;
211 }
212 }
213
214 // set default max of function calls according to the number of parameters
215 // formula from Minuit2 (adapted)
216 if (fMinimizerOpts.MaxFunctionCalls() == 0) {
217 unsigned int npar = fSettings.size();
218 int maxfcn = 1000 + 100*npar + 5*npar*npar;
220 }
221
222
223 // set default minimizer control parameters
232 // set extra options if existing
235
236 return min;
237}
238
239std::string FitConfig::MinimizerName() const
240{
241 // set minimizer type
242 std::string name = MinimizerType();
243
244 // append algorithm name for minimizer that support it
245 if ((name.find("Fumili") == std::string::npos) && (name.find("GSLMultiFit") == std::string::npos)) {
246 if (!MinimizerAlgoType().empty())
247 name += " / " + MinimizerAlgoType();
248 }
249 return name;
250}
251
252void FitConfig::SetDefaultMinimizer(const char * type, const char *algo ) {
253 // set the default minimizer type and algorithms
255}
256
258 // set all the minimizer options
259 fMinimizerOpts = minopt;
260}
261
262std::vector<double> FitConfig::ParamsValues() const {
263
264 std::vector<double> params(NPar() );
265 for (unsigned int i = 0; i < params.size(); ++i) {
266 params[i] = fSettings[i].Value();
267 }
268 return params;
269}
270 } // end namespace Fit
271
272} // end namespace ROOT
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define MATH_WARN_MSG(loc, str)
Definition Error.h:80
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
Class describing the configuration of the fit, options and parameter settings using the ROOT::Fit::Pa...
Definition FitConfig.h:47
std::vector< unsigned int > fMinosParams
vector with the parameter indices for running Minos
Definition FitConfig.h:266
FitConfig(unsigned int npar=0)
Default constructor.
Definition FitConfig.cxx:40
void SetMinimizer(const char *type, const char *algo=nullptr)
set minimizer type
Definition FitConfig.h:179
bool fNormErrors
flag for error normalization
Definition FitConfig.h:259
std::vector< double > ParamsValues() const
return a vector of stored parameter values (i.e initial fit parameters)
const std::string & MinimizerAlgoType() const
return type of minimizer algorithms
Definition FitConfig.h:192
void SetNormErrors(bool on=true)
set the option to normalize the error on the result according to chi2/ndf
Definition FitConfig.h:223
void SetMinimizerOptions(const ROOT::Math::MinimizerOptions &minopt)
set all the minimizer options using class MinimizerOptions
unsigned int NPar() const
number of parameters settings
Definition FitConfig.h:96
void SetParamsSettings(unsigned int npar, const double *params, const double *vstep=nullptr)
set the parameter settings from number of parameters and a vector of values and optionally step value...
std::vector< ROOT::Fit::ParameterSettings > fSettings
vector with the parameter settings
Definition FitConfig.h:265
std::string MinimizerName() const
return Minimizer full name (type / algorithm)
bool fParabErrors
get correct parabolic errors estimate (call Hesse after minimizing)
Definition FitConfig.h:260
ROOT::Math::MinimizerOptions fMinimizerOpts
minimizer control parameters including name and algo type
Definition FitConfig.h:268
~FitConfig()
Destructor.
Definition FitConfig.cxx:52
ROOT::Math::Minimizer * CreateMinimizer()
create a new minimizer according to chosen configuration
bool fMinosErrors
do full error analysis using Minos
Definition FitConfig.h:261
static void SetDefaultMinimizer(const char *type, const char *algo=nullptr)
static function to control default minimizer type and algorithm
void SetFromFitResult(const FitResult &rhs)
Definition FitConfig.cxx:80
const std::string & MinimizerType() const
return type of minimizer package
Definition FitConfig.h:187
bool fWeightCorr
apply correction to errors for weights fits
Definition FitConfig.h:263
bool fUpdateAfterFit
update the configuration after a fit using the result
Definition FitConfig.h:262
FitConfig & operator=(const FitConfig &rhs)
Definition FitConfig.cxx:62
class containing the result of the fit and all the related information (fitted parameter values,...
Definition FitResult.h:47
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 corresponding Minimizer given the string Supported Minimizers types are: ...
Definition Factory.cxx:63
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
const IOptions * ExtraOptions() const
return extra options (NULL pointer if they are not present)
double Tolerance() const
absolute tolerance
double Precision() const
precision in the objective function 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 void SetDefaultMinimizer(const char *type, const char *algo=nullptr)
Set the default Minimizer type and corresponding algorithms.
static const std::string & DefaultMinimizerType()
static const std::string & DefaultMinimizerAlgo()
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
void SetMinimizerAlgorithm(const char *type)
set minimizer algorithm
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition Minimizer.h:117
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition Minimizer.h:349
void SetErrorDef(double up)
set scale for calculating the errors
Definition Minimizer.h:362
void SetValidError(bool on)
flag to check if minimizer needs to perform accurate error analysis (e.g. run Hesse for Minuit)
Definition Minimizer.h:365
void SetTolerance(double tol)
set the tolerance
Definition Minimizer.h:352
void SetPrintLevel(int level)
set print level
Definition Minimizer.h:343
void SetStrategy(int strategyLevel)
set the strategy
Definition Minimizer.h:359
void SetPrecision(double prec)
set in the minimizer the objective function evaluation precision ( a value <=0 means the minimizer wi...
Definition Minimizer.h:356
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition Minimizer.h:346
void SetExtraOptions(const IOptions &extraOptions)
set only the extra options
Definition Minimizer.h:373
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition HFitImpl.cxx:133
std::string ToString(const T &val)
Utility function for conversion to strings.
Definition Util.h:50
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...