ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 ////////////////////////////////////////////////////////////////////////////////
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/FitterBase.h"
40 #include "TMVA/GeneticRange.h"
41 #include "TMVA/Interval.h"
42 #include "TMVA/MsgLogger.h"
43 #include "TMVA/Timer.h"
44 #include "TMVA/Types.h"
45 #include "TRandom3.h"
46 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// constructor
51 
52 TMVA::MCFitter::MCFitter( IFitterTarget& target,
53  const TString& name,
54  const std::vector<Interval*>& ranges,
55  const TString& theOption )
56  : TMVA::FitterBase( target, name, ranges, theOption ),
57  fSamples( 0 ),
58  fSigma ( 1 ),
59  fSeed ( 0 )
60 {
61  DeclareOptions();
62  ParseOptions();
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Declare MCFitter options
67 
69 {
70  DeclareOptionRef( fSamples = 100000, "SampleSize", "Number of Monte Carlo events in toy sample" );
71  DeclareOptionRef( fSigma = -1.0, "Sigma",
72  "If > 0: new points are generated according to Gauss around best value and with \"Sigma\" in units of interval length" );
73  DeclareOptionRef( fSeed = 100, "Seed", "Seed for the random generator (0 takes random seeds)" );
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// set MC fitter configuration parameters
78 
80 {
81  fSamples = samples;
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Execute fitting
86 
87 Double_t TMVA::MCFitter::Run( std::vector<Double_t>& pars )
88 {
89  Log() << kINFO << "<MCFitter> Sampling, please be patient ..." << Endl;
90 
91  // sanity check
92  if ((Int_t)pars.size() != GetNpars())
93  Log() << kFATAL << "<Run> Mismatch in number of parameters: "
94  << GetNpars() << " != " << pars.size() << Endl;
95 
96  // timing of MC
97  Timer timer( fSamples, GetName() );
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 
126  // dice the parameters
127  parIt = parameters.begin();
128  if (fSigma > 0.0) {
129  parBestIt = bestParameters.begin();
130  for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); rndIt++) {
131  (*parIt) = (*rndIt)->Random( kTRUE, (*parBestIt), fSigma );
132  parIt++;
133  parBestIt++;
134  }
135  }
136  else {
137  for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); rndIt++) {
138  (*parIt) = (*rndIt)->Random();
139  parIt++;
140  }
141  }
142 
143  // test the estimator value for the parameters
144  estimator = EstimatorFunction( parameters );
145 
146  // if the estimator ist better (=smaller), take the new parameters as the best ones
147  if (estimator < bestFit || sample==0) {
148  bestFit = estimator;
149  bestParameters.swap( parameters );
150  }
151 
152  // whats the time please?
153  if ((fSamples<100) || sample%Int_t(fSamples/100.0) == 0) timer.DrawProgressBar( sample );
154  }
155  pars.swap( bestParameters ); // return best parameters found
156 
157  // get elapsed time
158  Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
159  << " " << Endl;
160 
161  return bestFit;
162 }
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
TStopwatch timer
Definition: pirndm.C:37
Double_t Run()
estimator function interface for fitting
Definition: FitterBase.cxx:79
void SetParameters(Int_t cycles)
set MC fitter configuration parameters
Definition: MCFitter.cxx:79
ClassImp(TMVA::MCFitter) TMVA
constructor
Definition: MCFitter.cxx:47
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:68
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
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