Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
46 ClassImp(TMVA::MCFitter);
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// constructor
50 
51 TMVA::MCFitter::MCFitter( IFitterTarget& target,
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 
67 void TMVA::MCFitter::DeclareOptions()
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 
78 void TMVA::MCFitter::SetParameters( Int_t samples )
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
96  Timer timer( fSamples, GetName() );
97  if (fIPyMaxIter) *fIPyMaxIter = fSamples;
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 }