RooFoamGenerator.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooFoamGenerator.cxx 28259 2009-04-16 16:21:16Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 // 
00019 // BEGIN_HTML
00020 // Class RooFoamGenerator is a generic toy monte carlo generator that implement
00021 // the TFOAM sampling technique on any positively valued function.
00022 // The RooFoamGenerator generator is used by the various generator context
00023 // classes to take care of generation of observables for which p.d.fs
00024 // do not define internal methods
00025 // END_HTML
00026 //
00027 
00028 
00029 #include "RooFit.h"
00030 #include "Riostream.h"
00031 
00032 #include "RooFoamGenerator.h"
00033 #include "RooAbsReal.h"
00034 #include "RooCategory.h"
00035 #include "RooRealVar.h"
00036 #include "RooDataSet.h"
00037 #include "RooRandom.h"
00038 #include "RooErrorHandler.h"
00039 
00040 #include "TString.h"
00041 #include "TIterator.h"
00042 #include "RooMsgService.h"
00043 #include "TClass.h"
00044 #include "TFoam.h"
00045 #include "RooTFoamBinding.h"
00046 #include "RooNumGenFactory.h"
00047 #include "RooNumGenConfig.h"
00048 
00049 #include <assert.h>
00050 
00051 ClassImp(RooFoamGenerator)
00052   ;
00053 
00054 
00055 //_____________________________________________________________________________
00056 void RooFoamGenerator::registerSampler(RooNumGenFactory& fact)
00057 {
00058   // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
00059 
00060   // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
00061   RooRealVar nSample("nSample","Number of samples per cell",200,0,1e6) ;
00062   RooRealVar nCell1D("nCell1D","Number of cells for 1-dim generation",30,0,1e6) ;
00063   RooRealVar nCell2D("nCell2D","Number of cells for 2-dim generation",500,0,1e6) ;
00064   RooRealVar nCell3D("nCell3D","Number of cells for 3-dim generation",5000,0,1e6) ;
00065   RooRealVar nCellND("nCellND","Number of cells for N-dim generation",10000,0,1e6) ;
00066   RooRealVar chatLevel("chatLevel","TFOAM 'chat level' (verbosity)",0,0,2) ;
00067 
00068   RooFoamGenerator* proto = new RooFoamGenerator ;
00069   fact.storeProtoSampler(proto,RooArgSet(nSample,nCell1D,nCell2D,nCell3D,nCellND,chatLevel)) ;
00070 }
00071 
00072 
00073 
00074 
00075 //_____________________________________________________________________________
00076 RooFoamGenerator::RooFoamGenerator(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, Bool_t verbose, const RooAbsReal* maxFuncVal) :
00077   RooAbsNumGenerator(func,genVars,verbose,maxFuncVal)
00078 {
00079   _rvIter = _realVars.createIterator() ;
00080   _binding = new RooTFoamBinding(*_funcClone,_realVars) ;
00081  
00082   _tfoam = new TFoam("TFOAM") ;
00083   _tfoam->SetkDim(_realVars.getSize()) ;
00084   _tfoam->SetRho(_binding) ;
00085   _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
00086   switch(_realVars.getSize()) {
00087   case 1:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell1D")) ; break ;
00088   case 2:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell2D")) ; break ;
00089   case 3:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell3D")) ; break ;
00090   default:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCellND")) ; break ;
00091   }
00092   _tfoam->SetnSampl((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nSample")) ;
00093   _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
00094   _tfoam->SetChat((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("chatLevel")) ;
00095   _tfoam->Initialize() ;
00096 
00097   _vec = new Double_t[_realVars.getSize()] ;
00098   _xmin  = new Double_t[_realVars.getSize()] ;
00099   _range = new Double_t[_realVars.getSize()] ;
00100   
00101   TIterator* iter = _realVars.createIterator() ;
00102   RooRealVar* var ;
00103   Int_t i(0) ;
00104   while((var=(RooRealVar*)iter->Next())) {
00105     _xmin[i] = var->getMin() ;
00106     _range[i] = var->getMax() - var->getMin() ;
00107     i++ ;
00108   }
00109   delete iter ;
00110 
00111 }
00112 
00113 
00114 //_____________________________________________________________________________
00115 RooFoamGenerator::~RooFoamGenerator() 
00116 {
00117   // Destructor
00118   delete[] _vec ;
00119   delete[] _xmin ;
00120   delete[] _range ;
00121   delete _tfoam ;
00122   delete _binding ;
00123   delete _rvIter ;
00124 }
00125 
00126 
00127 
00128 //_____________________________________________________________________________
00129 const RooArgSet *RooFoamGenerator::generateEvent(UInt_t /*remaining*/, Double_t& /*resampleRatio*/) 
00130 {
00131   // are we actually generating anything? (the cache always contains at least our function value)
00132   const RooArgSet *event= _cache->get();
00133   if(event->getSize() == 1) return event;
00134 
00135   _tfoam->MakeEvent() ;
00136   _tfoam->GetMCvect(_vec) ;
00137   
00138   // Transfer contents to dataset
00139   RooRealVar* var ;  
00140   _rvIter->Reset() ;
00141   Int_t i(0) ;
00142   while((var=(RooRealVar*)_rvIter->Next())) {
00143     var->setVal(_xmin[i] + _range[i]*_vec[i]) ;
00144     i++ ;
00145   }
00146   return &_realVars ;
00147 }

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