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