00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "TMVA/MCFitter.h"
00038 #include "TMVA/GeneticRange.h"
00039 #include "TMVA/Interval.h"
00040 #include "TMVA/Timer.h"
00041 #include "TRandom3.h"
00042
00043 ClassImp(TMVA::MCFitter)
00044
00045
00046 TMVA::MCFitter::MCFitter( IFitterTarget& target,
00047 const TString& name,
00048 const std::vector<Interval*>& ranges,
00049 const TString& theOption )
00050 : TMVA::FitterBase( target, name, ranges, theOption ),
00051 fSamples( 0 ),
00052 fSigma ( 1 ),
00053 fSeed ( 0 )
00054 {
00055
00056 DeclareOptions();
00057 ParseOptions();
00058 }
00059
00060
00061 void TMVA::MCFitter::DeclareOptions()
00062 {
00063
00064 DeclareOptionRef( fSamples = 100000, "SampleSize", "Number of Monte Carlo events in toy sample" );
00065 DeclareOptionRef( fSigma = -1.0, "Sigma",
00066 "If > 0: new points are generated according to Gauss around best value and with \"Sigma\" in units of interval length" );
00067 DeclareOptionRef( fSeed = 100, "Seed", "Seed for the random generator (0 takes random seeds)" );
00068 }
00069
00070
00071 void TMVA::MCFitter::SetParameters( Int_t samples )
00072 {
00073
00074 fSamples = samples;
00075 }
00076
00077
00078 Double_t TMVA::MCFitter::Run( std::vector<Double_t>& pars )
00079 {
00080
00081 Log() << kINFO << "<MCFitter> Sampling, please be patient ..." << Endl;
00082
00083
00084 if ((Int_t)pars.size() != GetNpars())
00085 Log() << kFATAL << "<Run> Mismatch in number of parameters: "
00086 << GetNpars() << " != " << pars.size() << Endl;
00087
00088
00089 Timer timer( fSamples, GetName() );
00090
00091 std::vector<Double_t> parameters;
00092 std::vector<Double_t> bestParameters;
00093
00094 TRandom3*rnd = new TRandom3( fSeed );
00095 rnd->Uniform(0.,1.);
00096
00097 std::vector<TMVA::GeneticRange*> rndRanges;
00098
00099
00100 std::vector< TMVA::Interval* >::const_iterator rIt;
00101 Double_t val;
00102 for (rIt = fRanges.begin(); rIt<fRanges.end(); rIt++) {
00103 rndRanges.push_back( new TMVA::GeneticRange( rnd, (*rIt) ) );
00104 val = rndRanges.back()->Random();
00105 parameters.push_back( val );
00106 bestParameters.push_back( val );
00107 }
00108
00109 std::vector<Double_t>::iterator parIt;
00110 std::vector<Double_t>::iterator parBestIt;
00111
00112 Double_t estimator = 0;
00113 Double_t bestFit = 0;
00114
00115
00116 for (Int_t sample = 0; sample < fSamples; sample++) {
00117
00118
00119 parIt = parameters.begin();
00120 if (fSigma > 0.0) {
00121 parBestIt = bestParameters.begin();
00122 for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); rndIt++) {
00123 (*parIt) = (*rndIt)->Random( kTRUE, (*parBestIt), fSigma );
00124 parIt++;
00125 parBestIt++;
00126 }
00127 }
00128 else {
00129 for (std::vector<TMVA::GeneticRange*>::iterator rndIt = rndRanges.begin(); rndIt<rndRanges.end(); rndIt++) {
00130 (*parIt) = (*rndIt)->Random();
00131 parIt++;
00132 }
00133 }
00134
00135
00136 estimator = EstimatorFunction( parameters );
00137
00138
00139 if (estimator < bestFit || sample==0) {
00140 bestFit = estimator;
00141 bestParameters.swap( parameters );
00142 }
00143
00144
00145 if ((fSamples<100) || sample%Int_t(fSamples/100.0) == 0) timer.DrawProgressBar( sample );
00146 }
00147 pars.swap( bestParameters );
00148
00149
00150 Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime()
00151 << " " << Endl;
00152
00153 return bestFit;
00154 }