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() );
96 if (fIPyMaxIter) *fIPyMaxIter = fSamples;
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
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;
125 if (fExitFromTraining && *fExitFromTraining) break;
126
127 // dice the parameters
128 parIt = parameters.begin();
129 if (fSigma > 0.0) {
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) {
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
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
const_iterator begin() const
const_iterator end() const
virtual void ParseOptions()
options parser
Base class for TMVA fitters.
Definition FitterBase.h:51
Double_t Run()
estimator function interface for fitting
Range definition for genetic algorithm.
Interface for a fitter 'target'.
void SetParameters(Int_t cycles)
set MC fitter configuration parameters
Definition MCFitter.cxx:77
void DeclareOptions() override
Declare MCFitter options.
Definition MCFitter.cxx:66
MCFitter(IFitterTarget &target, const TString &name, const std::vector< TMVA::Interval * > &ranges, const TString &theOption)
constructor
Definition MCFitter.cxx:50
Timing information for training and evaluation of MVA methods.
Definition Timer.h:58
Random number generator class based on M.
Definition TRandom3.h:27
Basic string class.
Definition TString.h:138
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148