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
64{
65 delete fMinWrap;
66}
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
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))
102 fMinWrap = new MinuitWrapper( fFitterTarget, 2*GetNpars() );
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)
173 Double_t edm;
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++) {
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
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 target
char name[80]
Definition TGX11.cxx:110
virtual void ParseOptions()
options parser
Base class for TMVA fitters.
Definition FitterBase.h:51
Double_t Run()
estimator function interface for fitting
Interface for a fitter 'target'.
void Init()
minuit-specific settings
virtual ~MinuitFitter()
destructor
void DeclareOptions() override
declare SA options
Double_t EstimatorFunction(std::vector< Double_t > &pars) override
performs the fit by calling Run(pars)
MinuitFitter(IFitterTarget &target, const TString &name, std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
Wrapper around MINUIT.
Timing information for training and evaluation of MVA methods.
Definition Timer.h:58
virtual void Clear(Option_t *="")
Definition TObject.h:125
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:2384
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148