Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
MCFitter.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : TMVA::MCFitter *
8 * *
9 * *
10 * Description: *
11 * Implementation *
12 * *
13 * Authors (alphabetical): *
14 * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15 * Peter Speckmayer <speckmay@mail.cern.ch> - CERN, Switzerland *
16 * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
17 * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18 * *
19 * Copyright (c) 2005: *
20 * CERN, Switzerland *
21 * MPI-K Heidelberg, Germany *
22 * *
23 * Redistribution and use in source and binary forms, with or without *
24 * modification, are permitted according to the terms listed in LICENSE *
25 * (see tmva/doc/LICENSE) *
26 **********************************************************************************/
27
28/*! \class TMVA::MCFitter
29\ingroup TMVA
30
31Fitter using Monte Carlo sampling of parameters.
32
33*/
34
35#include "TMVA/MCFitter.h"
36
37#include "TMVA/Configurable.h"
38#include "TMVA/FitterBase.h"
39#include "TMVA/GeneticRange.h"
40#include "TMVA/Interval.h"
41#include "TMVA/MsgLogger.h"
42#include "TMVA/Timer.h"
43#include "TMVA/Types.h"
44#include "TRandom3.h"
45
46
47////////////////////////////////////////////////////////////////////////////////
48/// constructor
49
51 const TString& name,
52 const std::vector<Interval*>& ranges,
53 const TString& theOption )
54: TMVA::FitterBase( target, name, ranges, theOption ),
55 fSamples( 0 ),
56 fSigma ( 1 ),
57 fSeed ( 0 )
58{
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Declare MCFitter options
65
67{
68 DeclareOptionRef( fSamples = 100000, "SampleSize", "Number of Monte Carlo events in toy sample" );
69 DeclareOptionRef( fSigma = -1.0, "Sigma",
70 "If > 0: new points are generated according to Gauss around best value and with \"Sigma\" in units of interval length" );
71 DeclareOptionRef( fSeed = 100, "Seed", "Seed for the random generator (0 takes random seeds)" );
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// set MC fitter configuration parameters
76
78{
79 fSamples = samples;
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Execute fitting
84
85Double_t TMVA::MCFitter::Run( std::vector<Double_t>& pars )
86{
87 Log() << kHEADER << "<MCFitter> Sampling, please be patient ..." << Endl;
88
89 // sanity check
90 if ((Int_t)pars.size() != GetNpars())
91 Log() << kFATAL << "<Run> Mismatch in number of parameters: "
92 << GetNpars() << " != " << pars.size() << Endl;
93
94 // timing of MC
95 Timer timer( fSamples, GetName() );
97
98 std::vector<Double_t> parameters;
99 std::vector<Double_t> bestParameters;
100
101 TRandom3*rnd = new TRandom3( fSeed );
102 rnd->Uniform(0.,1.);
103
104 std::vector<TMVA::GeneticRange*> rndRanges;
105
106 // initial parameters (given by argument) are ignored
107 std::vector< TMVA::Interval* >::const_iterator rIt;
108 Double_t val;
109 for (rIt = fRanges.begin(); rIt<fRanges.end(); ++rIt) {
110 rndRanges.push_back( new TMVA::GeneticRange( rnd, (*rIt) ) );
111 val = rndRanges.back()->Random();
112 parameters.push_back( val );
113 bestParameters.push_back( val );
114 }
115
116 std::vector<Double_t>::iterator parIt;
117 std::vector<Double_t>::iterator parBestIt;
118
119 Double_t estimator = 0;
120 Double_t bestFit = 0;
121
122 // loop over all MC samples
123 for (Int_t sample = 0; sample < fSamples; sample++) {
124 if (fIPyCurrentIter) *fIPyCurrentIter = sample;
126
127 // dice the parameters
128 parIt = parameters.begin();
129 if (fSigma > 0.0) {
130 parBestIt = bestParameters.begin();
131 for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); ++rndIt) {
132 (*parIt) = (*rndIt)->Random( kTRUE, (*parBestIt), fSigma );
133 ++parIt;
134 ++parBestIt;
135 }
136 }
137 else {
138 for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); ++rndIt) {
139 (*parIt) = (*rndIt)->Random();
140 ++parIt;
141 }
142 }
143
144 // test the estimator value for the parameters
145 estimator = EstimatorFunction( parameters );
146
147 // if the estimator ist better (=smaller), take the new parameters as the best ones
148 if (estimator < bestFit || sample==0) {
149 bestFit = estimator;
150 bestParameters.swap( parameters );
151 }
152
153 // whats the time please?
154 if ((fSamples<100) || sample%Int_t(fSamples/100.0) == 0) timer.DrawProgressBar( sample );
155 }
156 pars.swap( bestParameters ); // return best parameters found
157
158 // get elapsed time
159 Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
160 << " " << Endl;
161
162 return bestFit;
163}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
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
UInt_t * fIPyCurrentIter
Definition FitterBase.h:94
Double_t EstimatorFunction(std::vector< Double_t > &parameters)
estimator function interface for fitting
FitterBase(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > ranges, const TString &theOption)
constructor
bool * fExitFromTraining
Definition FitterBase.h:95
const char * GetName() const override
Returns name of object.
Definition FitterBase.h:70
Int_t GetNpars() const
Definition FitterBase.h:67
MsgLogger & Log() const
Definition FitterBase.h:89
UInt_t * fIPyMaxIter
Definition FitterBase.h:94
Double_t Run()
estimator function interface for fitting
const std::vector< TMVA::Interval * > fRanges
Definition FitterBase.h:85
Range definition for genetic algorithm.
Interface for a fitter 'target'.
UInt_t fSeed
Seed for the random generator (0 takes random seeds).
Definition MCFitter.h:63
void SetParameters(Int_t cycles)
set MC fitter configuration parameters
Definition MCFitter.cxx:77
void DeclareOptions() override
Declare MCFitter options.
Definition MCFitter.cxx:66
Double_t fSigma
new samples are generated randomly with a gaussian probability with fSigma around the current best va...
Definition MCFitter.h:62
MCFitter(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
Definition MCFitter.cxx:50
Int_t fSamples
number of MC samples
Definition MCFitter.h:61
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
void DrawProgressBar(Int_t, const TString &comment="")
draws progress bar in color or B&W caution:
Definition Timer.cxx:201
Random number generator class based on M.
Definition TRandom3.h:27
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:681
Basic string class.
Definition TString.h:138
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148