// Class RooAcceptReject is a generic toy monte carlo generator implement
// the accept/reject sampling technique on any positively valued function.
// The RooAcceptReject generator is used by the various generator context
// classes to take care of generation of observables for which p.d.fs
// do not define internal methods
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "RooAcceptReject.h"
#include "RooAcceptReject.h"
#include "RooAbsReal.h"
#include "RooCategory.h"
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooRandom.h"
#include "RooErrorHandler.h"
#include "TString.h"
#include "TIterator.h"
#include "RooMsgService.h"
#include "TClass.h"
#include "TFoam.h"
#include "RooRealBinding.h"
#include "RooNumGenFactory.h"
#include "RooNumGenConfig.h"
#include <assert.h>
ClassImp(RooAcceptReject)
;
void RooAcceptReject::registerSampler(RooNumGenFactory& fact)
{
RooRealVar nTrial0D("nTrial0D","Number of trial samples for cat-only generation",100,0,1e9) ;
RooRealVar nTrial1D("nTrial1D","Number of trial samples for 1-dim generation",1000,0,1e9) ;
RooRealVar nTrial2D("nTrial2D","Number of trial samples for 2-dim generation",100000,0,1e9) ;
RooRealVar nTrial3D("nTrial3D","Number of trial samples for N-dim generation",10000000,0,1e9) ;
RooAcceptReject* proto = new RooAcceptReject ;
fact.storeProtoSampler(proto,RooArgSet(nTrial0D,nTrial1D,nTrial2D,nTrial3D)) ;
}
RooAcceptReject::RooAcceptReject(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, Bool_t verbose, const RooAbsReal* maxFuncVal) :
RooAbsNumGenerator(func,genVars,verbose,maxFuncVal)
{
_minTrialsArray[0] = static_cast<Int_t>(config.getConfigSection("RooAcceptReject").getRealValue("nTrial0D")) ;
_minTrialsArray[1] = static_cast<Int_t>(config.getConfigSection("RooAcceptReject").getRealValue("nTrial1D")) ;
_minTrialsArray[2] = static_cast<Int_t>(config.getConfigSection("RooAcceptReject").getRealValue("nTrial2D")) ;
_minTrialsArray[3] = static_cast<Int_t>(config.getConfigSection("RooAcceptReject").getRealValue("nTrial3D")) ;
_realSampleDim = _realVars.getSize() ;
TIterator* iter = _catVars.createIterator() ;
RooAbsCategory* cat ;
_catSampleMult = 1 ;
while((cat=(RooAbsCategory*)iter->Next())) {
_catSampleMult *= cat->numTypes() ;
}
delete iter ;
if (!_funcMaxVal) {
if(_realSampleDim > 3) {
_minTrials= _minTrialsArray[3]*_catSampleMult;
coutW(Generation) << fName << "::" << ClassName() << ": WARNING: generating " << _realSampleDim
<< " variables with accept-reject may not be accurate" << endl;
}
else {
_minTrials= _minTrialsArray[_realSampleDim]*_catSampleMult;
}
} else {
_minTrials=0 ;
}
if (_realSampleDim > 1) {
coutW(Generation) << "RooAcceptReject::ctor(" << fName
<< ") WARNING: performing accept/reject sampling on a p.d.f in " << _realSampleDim << " dimensions "
<< "without prior knowledge on maximum value of p.d.f. Determining maximum value by taking " << _minTrials
<< " trial samples. If p.d.f contains sharp peaks smaller than average distance between trial sampling points"
<< " these may be missed and p.d.f. may be sampled incorrectly." << endl ;
}
if (_minTrials>10000) {
coutW(Generation) << "RooAcceptReject::ctor(" << fName << "): WARNING: " << _minTrials << " trial samples requested by p.d.f for "
<< _realSampleDim << "-dimensional accept/reject sampling, this may take some time" << endl ;
}
if(_verbose) {
coutI(Generation) << fName << "::" << ClassName() << ":" << endl
<< " Initializing accept-reject generator for" << endl << " ";
_funcClone->printStream(ccoutI(Generation),kName,kSingleLine);
if (_funcMaxVal) {
ccoutI(Generation) << " Function maximum provided, no trial sampling performed" << endl ;
} else {
ccoutI(Generation) << " Real sampling dimension is " << _realSampleDim << endl;
ccoutI(Generation) << " Category sampling multiplier is " << _catSampleMult << endl ;
ccoutI(Generation) << " Min sampling trials is " << _minTrials << endl;
}
if (_catVars.getSize()>0) {
ccoutI(Generation) << " Will generate category vars "<< _catVars << endl ;
}
if (_realVars.getSize()>0) {
ccoutI(Generation) << " Will generate real vars " << _realVars << endl ;
}
}
_nextCatVar= _catVars.createIterator();
_nextRealVar= _realVars.createIterator();
assert(0 != _nextCatVar && 0 != _nextRealVar);
_maxFuncVal= 0;
_funcSum= 0;
_totalEvents= 0;
_eventsUsed= 0;
}
RooAcceptReject::~RooAcceptReject()
{
delete _nextCatVar;
delete _nextRealVar;
}
const RooArgSet *RooAcceptReject::generateEvent(UInt_t remaining, Double_t& resampleRatio)
{
const RooArgSet *event= _cache->get();
if(event->getSize() == 1) return event;
if (!_funcMaxVal) {
while(_totalEvents < _minTrials) {
addEventToCache();
if (_cache->numEntries()>1000000) {
coutI(Generation) << "RooAcceptReject::generateEvent: resetting event cache" << endl ;
_cache->reset() ;
_eventsUsed = 0 ;
}
}
event= 0;
Double_t oldMax2(_maxFuncVal);
while(0 == event) {
if (_maxFuncVal>oldMax2) {
cxcoutD(Generation) << "RooAcceptReject::generateEvent maxFuncVal has changed, need to resample already accepted events by factor"
<< oldMax2 << "/" << _maxFuncVal << "=" << oldMax2/_maxFuncVal << endl ;
resampleRatio=oldMax2/_maxFuncVal ;
}
event= nextAcceptedEvent();
if(event) break;
_cache->reset();
_eventsUsed= 0;
if(_totalEvents*_maxFuncVal <= 0) {
coutE(Generation) << "RooAcceptReject::generateEvent: cannot estimate efficiency...giving up" << endl;
return 0;
}
Double_t eff= _funcSum/(_totalEvents*_maxFuncVal);
Long64_t extra= 1 + (Long64_t)(1.05*remaining/eff);
cxcoutD(Generation) << "RooAcceptReject::generateEvent: adding " << extra << " events to the cache, eff = " << eff << endl;
Double_t oldMax(_maxFuncVal);
while(extra--) {
addEventToCache();
if((_maxFuncVal > oldMax)) {
cxcoutD(Generation) << "RooAcceptReject::generateEvent: estimated function maximum increased from "
<< oldMax << " to " << _maxFuncVal << endl;
oldMax = _maxFuncVal ;
}
}
}
if (_eventsUsed>1000000) {
_cache->reset() ;
_eventsUsed = 0 ;
}
} else {
_maxFuncVal = _funcMaxVal->getVal() ;
event = 0 ;
while(0==event) {
addEventToCache() ;
event = nextAcceptedEvent() ;
}
}
return event;
}
const RooArgSet *RooAcceptReject::nextAcceptedEvent()
{
const RooArgSet *event = 0;
while((event= _cache->get(_eventsUsed))) {
_eventsUsed++ ;
Double_t r= RooRandom::uniform();
if(r*_maxFuncVal > _funcValPtr->getVal()) {
continue;
}
if(_verbose && (_eventsUsed%1000==0)) {
cerr << "RooAcceptReject: accepted event (used " << _eventsUsed << " of "
<< _cache->numEntries() << " so far)" << endl;
}
break;
}
return event;
}
void RooAcceptReject::addEventToCache()
{
_nextCatVar->Reset();
RooCategory *cat = 0;
while((cat= (RooCategory*)_nextCatVar->Next())) cat->randomize();
_nextRealVar->Reset();
RooRealVar *real = 0;
while((real= (RooRealVar*)_nextRealVar->Next())) real->randomize();
Double_t val= _funcClone->getVal();
_funcValPtr->setVal(val);
if(val > _maxFuncVal) _maxFuncVal= 1.05*val;
_funcSum+= val;
_cache->fill();
_totalEvents++;
if (_verbose &&_totalEvents%10000==0) {
cerr << "RooAcceptReject: generated " << _totalEvents << " events so far." << endl ;
}
}
Double_t RooAcceptReject::getFuncMax()
{
while(_totalEvents < _minTrials) {
addEventToCache();
if (_cache->numEntries()>1000000) {
coutI(Generation) << "RooAcceptReject::getFuncMax: resetting event cache" << endl ;
_cache->reset() ;
_eventsUsed = 0 ;
}
}
return _maxFuncVal ;
}