Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
MinuitFitter.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Andraes Hoecker
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : MinuitFitter *
8 * *
9 * *
10 * Description: *
11 * Implementation *
12 * *
13 * Authors (alphabetical): *
14 * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15 * *
16 * Copyright (c) 2005: *
17 * CERN, Switzerland *
18 * MPI-K Heidelberg, Germany *
19 * *
20 * Redistribution and use in source and binary forms, with or without *
21 * modification, are permitted according to the terms listed in LICENSE *
22 * (see tmva/doc/LICENSE) *
23 **********************************************************************************/
24
25/*! \class TMVA::MinuitFitter
26\ingroup TMVA
27/Fitter using MINUIT
28*/
29#include "TMVA/MinuitFitter.h"
30
31#include "TMVA/Configurable.h"
32#include "TMVA/FitterBase.h"
33#include "TMVA/IFitterTarget.h"
34#include "TMVA/Interval.h"
35#include "TMVA/MinuitWrapper.h"
36#include "TMVA/MsgLogger.h"
37#include "TMVA/Timer.h"
38#include "TMVA/Types.h"
39
40#include "TFitter.h"
41
42
43////////////////////////////////////////////////////////////////////////////////
44/// constructor
45
47 const TString& name,
48 std::vector<TMVA::Interval*>& ranges,
49 const TString& theOption )
50: TMVA::FitterBase( target, name, ranges, theOption ),
52{
53 // default parameters settings for Simulated Annealing algorithm
56
57 Init(); // initialise the TFitter
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// destructor
62
67
68////////////////////////////////////////////////////////////////////////////////
69/// declare SA options
70
72{
73 DeclareOptionRef(fErrorLevel = 1, "ErrorLevel", "TMinuit: error level: 0.5=logL fit, 1=chi-squared fit" );
74 DeclareOptionRef(fPrintLevel = -1, "PrintLevel", "TMinuit: output level: -1=least, 0, +1=all garbage" );
75 DeclareOptionRef(fFitStrategy = 2, "FitStrategy", "TMinuit: fit strategy: 2=best" );
76 DeclareOptionRef(fPrintWarnings = kFALSE, "PrintWarnings", "TMinuit: suppress warnings" );
77 DeclareOptionRef(fUseImprove = kTRUE, "UseImprove", "TMinuit: use IMPROVE" );
78 DeclareOptionRef(fUseMinos = kTRUE, "UseMinos", "TMinuit: use MINOS" );
79 DeclareOptionRef(fBatch = kFALSE, "SetBatch", "TMinuit: use batch mode" );
80 DeclareOptionRef(fMaxCalls = 1000, "MaxCalls", "TMinuit: approximate maximum number of function calls" );
81 DeclareOptionRef(fTolerance = 0.1, "Tolerance", "TMinuit: tolerance to the function value at the minimum" );
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// minuit-specific settings
86
88{
89 Double_t args[10];
90
91 // Execute fitting
92 if (!fBatch) Log() << kINFO << "<MinuitFitter> Init " << Endl;
93
94 // timing of MC
95 Timer timer;
96
97 // initialize first -> prepare the fitter
98
99 // instantiate minuit
100 // maximum number of fit parameters is equal to
101 // (2xnpar as workaround for TMinuit allocation bug (taken from RooMinuit))
103
104 // output level
105 args[0] = fPrintLevel;
106 fMinWrap->ExecuteCommand( "SET PRINTOUT", args, 1 );
107
108 if (fBatch) fMinWrap->ExecuteCommand( "SET BAT", args, 0 );
109
110 // set fitter object, and clear
111 fMinWrap->Clear();
112
113 // error level: 1 (2*log(L) fit
114 args[0] = fErrorLevel;
115 fMinWrap->ExecuteCommand( "SET ERR", args, 1 );
116
117 // print warnings ?
118 if (!fPrintWarnings) fMinWrap->ExecuteCommand( "SET NOWARNINGS", args, 0 );
119
120 // define fit strategy
121 args[0] = fFitStrategy;
122 fMinWrap->ExecuteCommand( "SET STRATEGY", args, 1 );
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// performs the fit
127
128Double_t TMVA::MinuitFitter::Run( std::vector<Double_t>& pars )
129{
130 // minuit-specific settings
131 Double_t args[10];
132
133 // Execute fitting
134 if ( !fBatch ) Log() << kINFO << "<MinuitFitter> Fitting, please be patient ... " << Endl;
135
136 // sanity check
137 if ((Int_t)pars.size() != GetNpars())
138 Log() << kFATAL << "<Run> Mismatch in number of parameters: (a)"
139 << GetNpars() << " != " << pars.size() << Endl;
140
141 // timing of MC
142 Timer* timer = 0;
143 if (!fBatch) timer = new Timer();
144
145 // define fit parameters
146 for (Int_t ipar=0; ipar<fNpars; ipar++) {
147 fMinWrap->SetParameter( ipar, TString::Format( "Par%i",ipar ),
148 pars[ipar], fRanges[ipar]->GetWidth()/100.0,
149 fRanges[ipar]->GetMin(), fRanges[ipar]->GetMax() );
150 if (fRanges[ipar]->GetWidth() == 0.0) fMinWrap->FixParameter( ipar );
151 }
152
153 // --------- execute the fit
154
155 // continue with usual case
156 args[0] = fMaxCalls;
157 args[1] = fTolerance;
158
159 // MIGRAD
160 fMinWrap->ExecuteCommand( "MIGrad", args, 2 );
161
162 // IMPROVE
163 if (fUseImprove) fMinWrap->ExecuteCommand( "IMProve", args, 0 );
164
165 // MINOS
166 if (fUseMinos) {
167 args[0] = 500;
168 fMinWrap->ExecuteCommand( "MINOs", args, 1 );
169 }
170
171 // retrieve fit result (statistics)
172 Double_t chi2;
173 Double_t edm;
174 Double_t errdef;
175 Int_t nvpar;
176 Int_t nparx;
177 fMinWrap->GetStats( chi2, edm, errdef, nvpar, nparx );
178
179 // sanity check
180 if (GetNpars() != nparx) {
181 Log() << kFATAL << "<Run> Mismatch in number of parameters: "
182 << GetNpars() << " != " << nparx << Endl;
183 }
184
185 // retrieve parameters
186 for (Int_t ipar=0; ipar<GetNpars(); ipar++) {
187 Double_t errp, errm, errsym, globcor, currVal, currErr;
188 fMinWrap->GetParameter( ipar, currVal, currErr );
189 pars[ipar] = currVal;
190 fMinWrap->GetErrors( ipar, errp, errm, errsym, globcor );
191 }
192
193 // clean up
194
195 // get elapsed time
196 if (!fBatch) {
197 Log() << kINFO << "Elapsed time: " << timer->GetElapsedTime()
198 << " " << Endl;
199 delete timer;
200 }
201
202 fMinWrap->Clear();
203
204 return chi2;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// performs the fit by calling Run(pars)
209
211{
212 return Run( pars );
213}
214
215
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
char name[80]
Definition TGX11.cxx:148
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
virtual void ParseOptions()
options parser
FitterBase(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > ranges, const TString &theOption)
constructor
IFitterTarget & fFitterTarget
Definition FitterBase.h:84
Int_t GetNpars() const
Definition FitterBase.h:67
MsgLogger & Log() const
Definition FitterBase.h:89
Double_t Run()
estimator function interface for fitting
const std::vector< TMVA::Interval * > fRanges
Definition FitterBase.h:85
IFitterTarget()
constructor
Bool_t fPrintWarnings
minuit warnings level
Int_t fErrorLevel
minuit error level
Int_t fPrintLevel
minuit printout level
void Init()
minuit-specific settings
virtual ~MinuitFitter()
destructor
void DeclareOptions() override
declare SA options
Double_t Run(std::vector< Double_t > &pars) override
performs the fit
Bool_t fUseImprove
flag for 'IMPROVE' use
Bool_t fBatch
batch mode
Int_t fMaxCalls
(approximate) maximum number of function calls
Int_t fFitStrategy
minuit strategy level
MinuitWrapper * fMinWrap
Double_t EstimatorFunction(std::vector< Double_t > &pars) override
performs the fit by calling Run(pars)
Bool_t fUseMinos
flag for 'MINOS' use
MinuitFitter(IFitterTarget &target, const TString &name, std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
Double_t fTolerance
tolerance to the function value at the minimum
Wrapper around MINUIT.
Timing information for training and evaluation of MVA methods.
Definition Timer.h:58
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition Timer.cxx:145
Basic string class.
Definition TString.h:138
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2385
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148