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 * Web : http://tmva.sourceforge.net *
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 * (http://tmva.sourceforge.net/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
43
44////////////////////////////////////////////////////////////////////////////////
45/// constructor
46
48 const TString& name,
49 std::vector<TMVA::Interval*>& ranges,
50 const TString& theOption )
51: TMVA::FitterBase( target, name, ranges, theOption ),
53{
54 // default parameters settings for Simulated Annealing algorithm
57
58 Init(); // initialise the TFitter
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// destructor
63
65{
66 delete fMinWrap;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// declare SA options
71
73{
74 DeclareOptionRef(fErrorLevel = 1, "ErrorLevel", "TMinuit: error level: 0.5=logL fit, 1=chi-squared fit" );
75 DeclareOptionRef(fPrintLevel = -1, "PrintLevel", "TMinuit: output level: -1=least, 0, +1=all garbage" );
76 DeclareOptionRef(fFitStrategy = 2, "FitStrategy", "TMinuit: fit strategy: 2=best" );
77 DeclareOptionRef(fPrintWarnings = kFALSE, "PrintWarnings", "TMinuit: suppress warnings" );
78 DeclareOptionRef(fUseImprove = kTRUE, "UseImprove", "TMinuit: use IMPROVE" );
79 DeclareOptionRef(fUseMinos = kTRUE, "UseMinos", "TMinuit: use MINOS" );
80 DeclareOptionRef(fBatch = kFALSE, "SetBatch", "TMinuit: use batch mode" );
81 DeclareOptionRef(fMaxCalls = 1000, "MaxCalls", "TMinuit: approximate maximum number of function calls" );
82 DeclareOptionRef(fTolerance = 0.1, "Tolerance", "TMinuit: tolerance to the function value at the minimum" );
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// minuit-specific settings
87
89{
90 Double_t args[10];
91
92 // Execute fitting
93 if (!fBatch) Log() << kINFO << "<MinuitFitter> Init " << Endl;
94
95 // timing of MC
96 Timer timer;
97
98 // initialize first -> prepare the fitter
99
100 // instantiate minuit
101 // maximum number of fit parameters is equal to
102 // (2xnpar as workaround for TMinuit allocation bug (taken from RooMinuit))
103 fMinWrap = new MinuitWrapper( fFitterTarget, 2*GetNpars() );
104
105 // output level
106 args[0] = fPrintLevel;
107 fMinWrap->ExecuteCommand( "SET PRINTOUT", args, 1 );
108
109 if (fBatch) fMinWrap->ExecuteCommand( "SET BAT", args, 0 );
110
111 // set fitter object, and clear
112 fMinWrap->Clear();
113
114 // error level: 1 (2*log(L) fit
115 args[0] = fErrorLevel;
116 fMinWrap->ExecuteCommand( "SET ERR", args, 1 );
117
118 // print warnings ?
119 if (!fPrintWarnings) fMinWrap->ExecuteCommand( "SET NOWARNINGS", args, 0 );
120
121 // define fit strategy
122 args[0] = fFitStrategy;
123 fMinWrap->ExecuteCommand( "SET STRATEGY", args, 1 );
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// performs the fit
128
129Double_t TMVA::MinuitFitter::Run( std::vector<Double_t>& pars )
130{
131 // minuit-specific settings
132 Double_t args[10];
133
134 // Execute fitting
135 if ( !fBatch ) Log() << kINFO << "<MinuitFitter> Fitting, please be patient ... " << Endl;
136
137 // sanity check
138 if ((Int_t)pars.size() != GetNpars())
139 Log() << kFATAL << "<Run> Mismatch in number of parameters: (a)"
140 << GetNpars() << " != " << pars.size() << Endl;
141
142 // timing of MC
143 Timer* timer = 0;
144 if (!fBatch) timer = new Timer();
145
146 // define fit parameters
147 for (Int_t ipar=0; ipar<fNpars; ipar++) {
148 fMinWrap->SetParameter( ipar, TString::Format( "Par%i",ipar ),
149 pars[ipar], fRanges[ipar]->GetWidth()/100.0,
150 fRanges[ipar]->GetMin(), fRanges[ipar]->GetMax() );
151 if (fRanges[ipar]->GetWidth() == 0.0) fMinWrap->FixParameter( ipar );
152 }
153
154 // --------- execute the fit
155
156 // continue with usual case
157 args[0] = fMaxCalls;
158 args[1] = fTolerance;
159
160 // MIGRAD
161 fMinWrap->ExecuteCommand( "MIGrad", args, 2 );
162
163 // IMPROVE
164 if (fUseImprove) fMinWrap->ExecuteCommand( "IMProve", args, 0 );
165
166 // MINOS
167 if (fUseMinos) {
168 args[0] = 500;
169 fMinWrap->ExecuteCommand( "MINOs", args, 1 );
170 }
171
172 // retrieve fit result (statistics)
173 Double_t chi2;
174 Double_t edm;
175 Double_t errdef;
176 Int_t nvpar;
177 Int_t nparx;
178 fMinWrap->GetStats( chi2, edm, errdef, nvpar, nparx );
179
180 // sanity check
181 if (GetNpars() != nparx) {
182 Log() << kFATAL << "<Run> Mismatch in number of parameters: "
183 << GetNpars() << " != " << nparx << Endl;
184 }
185
186 // retrieve parameters
187 for (Int_t ipar=0; ipar<GetNpars(); ipar++) {
188 Double_t errp, errm, errsym, globcor, currVal, currErr;
189 fMinWrap->GetParameter( ipar, currVal, currErr );
190 pars[ipar] = currVal;
191 fMinWrap->GetErrors( ipar, errp, errm, errsym, globcor );
192 }
193
194 // clean up
195
196 // get elapsed time
197 if (!fBatch) {
198 Log() << kINFO << "Elapsed time: " << timer->GetElapsedTime()
199 << " " << Endl;
200 delete timer;
201 }
202
203 fMinWrap->Clear();
204
205 return chi2;
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// performs the fit by calling Run(pars)
210
212{
213 return Run( pars );
214}
215
216
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
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'.
/Fitter using MINUIT
void Init()
minuit-specific settings
virtual ~MinuitFitter()
destructor
Double_t EstimatorFunction(std::vector< Double_t > &pars)
performs the fit by calling Run(pars)
MinuitFitter(IFitterTarget &target, const TString &name, std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
void DeclareOptions()
declare SA options
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:146
virtual void Clear(Option_t *="")
Definition TObject.h:119
Basic string class.
Definition TString.h:139
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:2356
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148