Logo ROOT   6.08/07
Reference Guide
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 //_______________________________________________________________________
26 //
27 // Fitter using MINUIT
28 //_______________________________________________________________________
29 
30 #include "TMVA/MinuitFitter.h"
31 
32 #include "TMVA/Configurable.h"
33 #include "TMVA/FitterBase.h"
34 #include "TMVA/IFitterTarget.h"
35 #include "TMVA/Interval.h"
36 #include "TMVA/MinuitWrapper.h"
37 #include "TMVA/MsgLogger.h"
38 #include "TMVA/Timer.h"
39 #include "TMVA/Types.h"
40 
41 #include "TFitter.h"
42 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// constructor
47 
49  const TString& name,
50  std::vector<TMVA::Interval*>& ranges,
51  const TString& theOption )
52 : TMVA::FitterBase( target, name, ranges, theOption ),
54 {
55  // default parameters settings for Simulated Annealing algorithm
56  DeclareOptions();
57  ParseOptions();
58 
59  Init(); // initialise the TFitter
60 }
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// destructor
64 
66 {
67  delete fMinWrap;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// declare SA options
72 
74 {
75  DeclareOptionRef(fErrorLevel = 1, "ErrorLevel", "TMinuit: error level: 0.5=logL fit, 1=chi-squared fit" );
76  DeclareOptionRef(fPrintLevel = -1, "PrintLevel", "TMinuit: output level: -1=least, 0, +1=all garbage" );
77  DeclareOptionRef(fFitStrategy = 2, "FitStrategy", "TMinuit: fit strategy: 2=best" );
78  DeclareOptionRef(fPrintWarnings = kFALSE, "PrintWarnings", "TMinuit: suppress warnings" );
79  DeclareOptionRef(fUseImprove = kTRUE, "UseImprove", "TMinuit: use IMPROVE" );
80  DeclareOptionRef(fUseMinos = kTRUE, "UseMinos", "TMinuit: use MINOS" );
81  DeclareOptionRef(fBatch = kFALSE, "SetBatch", "TMinuit: use batch mode" );
82  DeclareOptionRef(fMaxCalls = 1000, "MaxCalls", "TMinuit: approximate maximum number of function calls" );
83  DeclareOptionRef(fTolerance = 0.1, "Tolerance", "TMinuit: tolerance to the function value at the minimum" );
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// minuit-specific settings
88 
90 {
91  Double_t args[10];
92 
93  // Execute fitting
94  if (!fBatch) Log() << kINFO << "<MinuitFitter> Init " << Endl;
95 
96  // timing of MC
97  Timer timer;
98 
99  // initialize first -> prepare the fitter
100 
101  // instantiate minuit
102  // maximum number of fit parameters is equal to
103  // (2xnpar as workaround for TMinuit allocation bug (taken from RooMinuit))
105 
106  // output level
107  args[0] = fPrintLevel;
108  fMinWrap->ExecuteCommand( "SET PRINTOUT", args, 1 );
109 
110  if (fBatch) fMinWrap->ExecuteCommand( "SET BAT", args, 0 );
111 
112  // set fitter object, and clear
113  fMinWrap->Clear();
114 
115  // error level: 1 (2*log(L) fit
116  args[0] = fErrorLevel;
117  fMinWrap->ExecuteCommand( "SET ERR", args, 1 );
118 
119  // print warnings ?
120  if (!fPrintWarnings) fMinWrap->ExecuteCommand( "SET NOWARNINGS", args, 0 );
121 
122  // define fit strategy
123  args[0] = fFitStrategy;
124  fMinWrap->ExecuteCommand( "SET STRATEGY", args, 1 );
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// performs the fit
129 
130 Double_t TMVA::MinuitFitter::Run( std::vector<Double_t>& pars )
131 {
132  // minuit-specific settings
133  Double_t args[10];
134 
135  // Execute fitting
136  if ( !fBatch ) Log() << kINFO << "<MinuitFitter> Fitting, please be patient ... " << Endl;
137 
138  // sanity check
139  if ((Int_t)pars.size() != GetNpars())
140  Log() << kFATAL << "<Run> Mismatch in number of parameters: (a)"
141  << GetNpars() << " != " << pars.size() << Endl;
142 
143  // timing of MC
144  Timer* timer = 0;
145  if (!fBatch) timer = new Timer();
146 
147  // define fit parameters
148  for (Int_t ipar=0; ipar<fNpars; ipar++) {
149  fMinWrap->SetParameter( ipar, Form( "Par%i",ipar ),
150  pars[ipar], fRanges[ipar]->GetWidth()/100.0,
151  fRanges[ipar]->GetMin(), fRanges[ipar]->GetMax() );
152  if (fRanges[ipar]->GetWidth() == 0.0) fMinWrap->FixParameter( ipar );
153  }
154 
155  // --------- execute the fit
156 
157  // continue with usual case
158  args[0] = fMaxCalls;
159  args[1] = fTolerance;
160 
161  // MIGRAD
162  fMinWrap->ExecuteCommand( "MIGrad", args, 2 );
163 
164  // IMPROVE
165  if (fUseImprove) fMinWrap->ExecuteCommand( "IMProve", args, 0 );
166 
167  // MINOS
168  if (fUseMinos) {
169  args[0] = 500;
170  fMinWrap->ExecuteCommand( "MINOs", args, 1 );
171  }
172 
173  // retrieve fit result (statistics)
174  Double_t chi2;
175  Double_t edm;
176  Double_t errdef;
177  Int_t nvpar;
178  Int_t nparx;
179  fMinWrap->GetStats( chi2, edm, errdef, nvpar, nparx );
180 
181  // sanity check
182  if (GetNpars() != nparx) {
183  Log() << kFATAL << "<Run> Mismatch in number of parameters: "
184  << GetNpars() << " != " << nparx << Endl;
185  }
186 
187  // retrieve parameters
188  for (Int_t ipar=0; ipar<GetNpars(); ipar++) {
189  Double_t errp, errm, errsym, globcor, currVal, currErr;
190  fMinWrap->GetParameter( ipar, currVal, currErr );
191  pars[ipar] = currVal;
192  fMinWrap->GetErrors( ipar, errp, errm, errsym, globcor );
193  }
194 
195  // clean up
196 
197  // get elapsed time
198  if (!fBatch) {
199  Log() << kINFO << "Elapsed time: " << timer->GetElapsedTime()
200  << " " << Endl;
201  delete timer;
202  }
203 
204  fMinWrap->Clear();
205 
206  return chi2;
207 }
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// performs the fit by calliung Run(pars)
211 
212 Double_t TMVA::MinuitFitter::EstimatorFunction( std::vector<Double_t>& pars )
213 {
214  return Run( pars );
215 }
216 
217 
const std::vector< TMVA::Interval * > fRanges
Definition: FitterBase.h:91
Int_t GetStats(Double_t &amin, Double_t &edm, Double_t &errdef, Int_t &nvpar, Int_t &nparx)
return global fit parameters amin : chisquare edm : estimated distance to minimum errdef nvpar : numb...
MsgLogger & Log() const
Definition: FitterBase.h:95
Int_t SetParameter(Int_t ipar, const char *parname, Double_t value, Double_t verr, Double_t vlow, Double_t vhigh)
set initial values for a parameter ipar : parameter number parname : parameter name value : initial p...
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
virtual ~MinuitFitter()
destructor
Int_t GetErrors(Int_t ipar, Double_t &eplus, Double_t &eminus, Double_t &eparab, Double_t &globcc)
return current errors for a parameter ipar : parameter number eplus : upper error eminus : lower erro...
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
TString GetElapsedTime(Bool_t Scientific=kTRUE)
Definition: Timer.cxx:129
virtual Int_t GetParameter(Int_t parNo, Double_t &currentValue, Double_t &currentError) const
return parameter value and error
Definition: TMinuit.cxx:850
virtual Int_t FixParameter(Int_t parNo)
fix a parameter
Definition: TMinuit.cxx:836
TStopwatch timer
Definition: pirndm.C:37
Double_t Run()
estimator function interface for fitting
Definition: FitterBase.cxx:80
void Init()
minuit-specific settings
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
Int_t GetNpars() const
Definition: FitterBase.h:73
void Clear(Option_t *=0)
reset the fitter environment
RooCmdArg Timer(Bool_t flag=kTRUE)
char * Form(const char *fmt,...)
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
MinuitWrapper * fMinWrap
Definition: MinuitFitter.h:67
IFitterTarget & fFitterTarget
Definition: FitterBase.h:90
Abstract ClassifierFactory template that handles arbitrary types.
Double_t EstimatorFunction(std::vector< Double_t > &pars)
performs the fit by calliung Run(pars)
const Bool_t kTRUE
Definition: Rtypes.h:91
void DeclareOptions()
declare SA options
Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs)
Execute a fitter command; command : command string args : list of nargs command arguments.
char name[80]
Definition: TGX11.cxx:109