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