MCFitter.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: MCFitter.cxx 29195 2009-06-24 10:39:49Z brun $ 
00002 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : TMVA::MCFitter                                                        *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Implementation                                                            *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Peter Speckmayer <speckmay@mail.cern.ch> - CERN, Switzerland              *
00016  *      Joerg Stelzer    <Joerg.Stelzer@cern.ch>  - CERN, Switzerland             *
00017  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00018  *                                                                                *
00019  * Copyright (c) 2005:                                                            *
00020  *      CERN, Switzerland                                                         * 
00021  *      MPI-K Heidelberg, Germany                                                 * 
00022  *                                                                                *
00023  * Redistribution and use in source and binary forms, with or without             *
00024  * modification, are permitted according to the terms listed in LICENSE           *
00025  * (http://tmva.sourceforge.net/LICENSE)                                          *
00026  **********************************************************************************/
00027 
00028 //_______________________________________________________________________
00029 /*
00030   MCFitter
00031   
00032   Fitter using Monte Carlo sampling of parameters 
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    // constructor
00056    DeclareOptions();
00057    ParseOptions();
00058 }            
00059 
00060 //_______________________________________________________________________
00061 void TMVA::MCFitter::DeclareOptions() 
00062 {
00063    // Declare MCFitter options
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    // set MC fitter configuration parameters
00074    fSamples = samples;
00075 }
00076 
00077 //_______________________________________________________________________
00078 Double_t TMVA::MCFitter::Run( std::vector<Double_t>& pars )
00079 {
00080    // Execute fitting
00081    Log() << kINFO << "<MCFitter> Sampling, please be patient ..." << Endl;
00082    
00083    // sanity check
00084    if ((Int_t)pars.size() != GetNpars())
00085       Log() << kFATAL << "<Run> Mismatch in number of parameters: "
00086               << GetNpars() << " != " << pars.size() << Endl;
00087 
00088    // timing of MC
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    // initial parameters (given by argument) are ignored
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    // loop over all MC samples
00116    for (Int_t sample = 0; sample < fSamples; sample++) {
00117 
00118       // dice the parameters
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       // test the estimator value for the parameters
00136       estimator = EstimatorFunction( parameters );
00137 
00138       // if the estimator ist better (=smaller), take the new parameters as the best ones
00139       if (estimator < bestFit || sample==0) {
00140          bestFit = estimator;
00141          bestParameters.swap( parameters );
00142       }
00143 
00144       // whats the time please?
00145       if ((fSamples<100) || sample%Int_t(fSamples/100.0) == 0) timer.DrawProgressBar( sample );
00146    }
00147    pars.swap( bestParameters ); // return best parameters found
00148 
00149    // get elapsed time
00150    Log() << kINFO << "Elapsed time: " << timer.GetElapsedTime() 
00151            << "                           " << Endl;  
00152    
00153    return bestFit;
00154 }

Generated on Tue Jul 5 15:25:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1