Logo ROOT   6.10/09
Reference Guide
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  * Web : http://tmva.sourceforge.net *
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  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 /*! \class TMVA::MCFitter
29 \ingroup TMVA
30 
31 Fitter 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 
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// constructor
50 
52  const TString& name,
53  const std::vector<Interval*>& ranges,
54  const TString& theOption )
55 : TMVA::FitterBase( target, name, ranges, theOption ),
56  fSamples( 0 ),
57  fSigma ( 1 ),
58  fSeed ( 0 )
59 {
60  DeclareOptions();
61  ParseOptions();
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Declare MCFitter options
66 
68 {
69  DeclareOptionRef( fSamples = 100000, "SampleSize", "Number of Monte Carlo events in toy sample" );
70  DeclareOptionRef( fSigma = -1.0, "Sigma",
71  "If > 0: new points are generated according to Gauss around best value and with \"Sigma\" in units of interval length" );
72  DeclareOptionRef( fSeed = 100, "Seed", "Seed for the random generator (0 takes random seeds)" );
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// set MC fitter configuration parameters
77 
79 {
80  fSamples = samples;
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Execute fitting
85 
86 Double_t TMVA::MCFitter::Run( std::vector<Double_t>& pars )
87 {
88  Log() << kHEADER << "<MCFitter> Sampling, please be patient ..." << Endl;
89 
90  // sanity check
91  if ((Int_t)pars.size() != GetNpars())
92  Log() << kFATAL << "<Run> Mismatch in number of parameters: "
93  << GetNpars() << " != " << pars.size() << Endl;
94 
95  // timing of MC
98 
99  std::vector<Double_t> parameters;
100  std::vector<Double_t> bestParameters;
101 
102  TRandom3*rnd = new TRandom3( fSeed );
103  rnd->Uniform(0.,1.);
104 
105  std::vector<TMVA::GeneticRange*> rndRanges;
106 
107  // initial parameters (given by argument) are ignored
108  std::vector< TMVA::Interval* >::const_iterator rIt;
109  Double_t val;
110  for (rIt = fRanges.begin(); rIt<fRanges.end(); rIt++) {
111  rndRanges.push_back( new TMVA::GeneticRange( rnd, (*rIt) ) );
112  val = rndRanges.back()->Random();
113  parameters.push_back( val );
114  bestParameters.push_back( val );
115  }
116 
117  std::vector<Double_t>::iterator parIt;
118  std::vector<Double_t>::iterator parBestIt;
119 
120  Double_t estimator = 0;
121  Double_t bestFit = 0;
122 
123  // loop over all MC samples
124  for (Int_t sample = 0; sample < fSamples; sample++) {
125  if (fIPyCurrentIter) *fIPyCurrentIter = sample;
126  if (fExitFromTraining && *fExitFromTraining) break;
127 
128  // dice the parameters
129  parIt = parameters.begin();
130  if (fSigma > 0.0) {
131  parBestIt = bestParameters.begin();
132  for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); rndIt++) {
133  (*parIt) = (*rndIt)->Random( kTRUE, (*parBestIt), fSigma );
134  parIt++;
135  parBestIt++;
136  }
137  }
138  else {
139  for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); rndIt++) {
140  (*parIt) = (*rndIt)->Random();
141  parIt++;
142  }
143  }
144 
145  // test the estimator value for the parameters
146  estimator = EstimatorFunction( parameters );
147 
148  // if the estimator ist better (=smaller), take the new parameters as the best ones
149  if (estimator < bestFit || sample==0) {
150  bestFit = estimator;
151  bestParameters.swap( parameters );
152  }
153 
154  // whats the time please?
155  if ((fSamples<100) || sample%Int_t(fSamples/100.0) == 0) timer.DrawProgressBar( sample );
156  }
157  pars.swap( bestParameters ); // return best parameters found
158 
159  // get elapsed time
160  Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
161  << " " << Endl;
162 
163  return bestFit;
164 }
const std::vector< TMVA::Interval * > fRanges
Definition: FitterBase.h:85
UInt_t * fIPyCurrentIter
Definition: FitterBase.h:94
MsgLogger & Log() const
Definition: FitterBase.h:89
Double_t fSigma
Definition: MCFitter.h:61
Random number generator class based on M.
Definition: TRandom3.h:27
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Int_t fSamples
Definition: MCFitter.h:60
Base class for TMVA fitters.
Definition: FitterBase.h:51
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
bool * fExitFromTraining
Definition: FitterBase.h:95
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
UInt_t * fIPyMaxIter
Definition: FitterBase.h:94
STL namespace.
TStopwatch timer
Definition: pirndm.C:37
Double_t Run()
estimator function interface for fitting
Definition: FitterBase.cxx:74
void SetParameters(Int_t cycles)
set MC fitter configuration parameters
Definition: MCFitter.cxx:78
Int_t GetNpars() const
Definition: FitterBase.h:67
UInt_t fSeed
Definition: MCFitter.h:62
Double_t EstimatorFunction(std::vector< Double_t > &parameters)
estimator function interface for fitting
Definition: FitterBase.cxx:88
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition: Timer.cxx:134
The TMVA::Interval Class.
Definition: Interval.h:61
Fitter using Monte Carlo sampling of parameters.
Definition: MCFitter.h:43
void DeclareOptions()
Declare MCFitter options.
Definition: MCFitter.cxx:67
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
Abstract ClassifierFactory template that handles arbitrary types.
const char * GetName() const
Returns name of object.
Definition: FitterBase.h:70
void DrawProgressBar(Int_t, const TString &comment="")
draws progress bar in color or B&W caution:
Definition: Timer.cxx:190
Interface for a fitter &#39;target&#39;.
Definition: IFitterTarget.h:44
Range definition for genetic algorithm.
Definition: GeneticRange.h:42
const Bool_t kTRUE
Definition: RtypesCore.h:91
Timing information for training and evaluation of MVA methods.
Definition: Timer.h:58