55void RooAcceptReject::registerSampler(RooNumGenFactory& fact)
57 RooRealVar nTrial0D(
"nTrial0D",
"Number of trial samples for cat-only generation",100,0,1e9) ;
58 RooRealVar nTrial1D(
"nTrial1D",
"Number of trial samples for 1-dim generation",1000,0,1e9) ;
59 RooRealVar nTrial2D(
"nTrial2D",
"Number of trial samples for 2-dim generation",100000,0,1e9) ;
60 RooRealVar nTrial3D(
"nTrial3D",
"Number of trial samples for N-dim generation",10000000,0,1e9) ;
62 RooAcceptReject*
proto =
new RooAcceptReject ;
63 fact.storeProtoSampler(
proto,
RooArgSet(nTrial0D,nTrial1D,nTrial2D,nTrial3D)) ;
76 : RooAbsNumGenerator(func, genVars,
verbose, maxFuncVal), _realSampleDim(_realVars.
size()), _catSampleMult(1)
78 _minTrialsArray[0] =
static_cast<Int_t>(config.
getConfigSection(
"RooAcceptReject").getRealValue(
"nTrial0D")) ;
79 _minTrialsArray[1] =
static_cast<Int_t>(config.
getConfigSection(
"RooAcceptReject").getRealValue(
"nTrial1D")) ;
80 _minTrialsArray[2] =
static_cast<Int_t>(config.
getConfigSection(
"RooAcceptReject").getRealValue(
"nTrial2D")) ;
81 _minTrialsArray[3] =
static_cast<Int_t>(config.
getConfigSection(
"RooAcceptReject").getRealValue(
"nTrial3D")) ;
84 _catSampleMult *= cat->numTypes() ;
91 if(_realSampleDim > 3) {
92 _minTrials= _minTrialsArray[3]*_catSampleMult;
93 oocoutW(
nullptr, Generation) << _funcClone->GetName() <<
"::RooAcceptReject" <<
": WARNING: generating " << _realSampleDim
94 <<
" variables with accept-reject may not be accurate" << std::endl;
97 _minTrials= _minTrialsArray[_realSampleDim]*_catSampleMult;
99 if (_realSampleDim > 1) {
100 oocoutW(
nullptr, Generation) <<
"RooAcceptReject::ctor(" << _funcClone->GetName()
101 <<
") WARNING: performing accept/reject sampling on a p.d.f in "
102 << _realSampleDim <<
" dimensions without prior knowledge on maximum value "
103 <<
"of p.d.f. Determining maximum value by taking " << _minTrials
104 <<
" trial samples. If p.d.f contains sharp peaks smaller than average "
105 <<
"distance between trial sampling points these may be missed and p.d.f. "
106 <<
"may be sampled incorrectly." << std::endl ;
114 if (_minTrials>10000) {
115 oocoutW(
nullptr, Generation) <<
"RooAcceptReject::ctor(" << func.
GetName() <<
"): WARNING: " << _minTrials <<
" trial samples requested by p.d.f for "
116 << _realSampleDim <<
"-dimensional accept/reject sampling, this may take some time" << std::endl ;
121 oocoutI(
nullptr, Generation) << func.
GetName() <<
"::RooAcceptReject" <<
":" << std::endl
122 <<
" Initializing accept-reject generator for" << std::endl <<
" ";
125 ooccoutI(
nullptr, Generation) <<
" Function maximum provided, no trial sampling performed" << std::endl ;
127 ooccoutI(
nullptr, Generation) <<
" Real sampling dimension is " << _realSampleDim << std::endl;
128 ooccoutI(
nullptr, Generation) <<
" Category sampling multiplier is " << _catSampleMult << std::endl ;
129 ooccoutI(
nullptr, Generation) <<
" Min sampling trials is " << _minTrials << std::endl;
131 if (!_catVars.empty()) {
132 ooccoutI(
nullptr, Generation) <<
" Will generate category vars "<< _catVars << std::endl ;
134 if (!_realVars.empty()) {
135 ooccoutI(
nullptr, Generation) <<
" Will generate real vars " << _realVars << std::endl ;
152const RooArgSet *RooAcceptReject::generateEvent(
UInt_t remaining,
double& resampleRatio)
156 if(event->size() == 1)
return event;
164 while(_totalEvents < _minTrials) {
168 if (_cache->numEntries()>1000000) {
169 oocoutI(
nullptr, Generation) <<
"RooAcceptReject::generateEvent: resetting event cache" << std::endl;
176 double oldMax2(_maxFuncVal);
177 while(
nullptr == event) {
179 if (_maxFuncVal>oldMax2) {
180 oocxcoutD(
nullptr, Generation) <<
"RooAcceptReject::generateEvent maxFuncVal has changed, need to resample already accepted events by factor"
181 << oldMax2 <<
"/" << _maxFuncVal <<
"=" << oldMax2/_maxFuncVal << std::endl ;
182 resampleRatio=oldMax2/_maxFuncVal ;
184 event= nextAcceptedEvent();
192 if(_totalEvents*_maxFuncVal <= 0) {
193 oocoutE(
nullptr, Generation) <<
"RooAcceptReject::generateEvent: cannot estimate efficiency...giving up" << std::endl;
197 double eff= _funcSum/(_totalEvents*_maxFuncVal);
199 oocxcoutD(
nullptr, Generation) <<
"RooAcceptReject::generateEvent: adding " << extra <<
" events to the cache, eff = " << eff << std::endl;
200 double oldMax(_maxFuncVal);
203 if((_maxFuncVal > oldMax)) {
204 oocxcoutD(
nullptr, Generation) <<
"RooAcceptReject::generateEvent: estimated function maximum increased from "
205 << oldMax <<
" to " << _maxFuncVal << std::endl;
206 oldMax = _maxFuncVal ;
213 if (_eventsUsed>1000000) {
220 _maxFuncVal = _funcMaxVal->getVal() ;
224 while(
nullptr==event) {
226 event = nextAcceptedEvent() ;
242const RooArgSet *RooAcceptReject::nextAcceptedEvent()
245 while((event= _cache->get(_eventsUsed))) {
249 if(
r*_maxFuncVal > _funcValPtr->getVal()) {
255 if(_verbose && (_eventsUsed%1000==0)) {
256 std::cerr <<
"RooAcceptReject: accepted event (used " << _eventsUsed <<
" of "
257 << _cache->numEntries() <<
" so far)" << std::endl;
271void RooAcceptReject::addEventToCache()
280 double val= _funcClone->getVal();
281 _funcValPtr->setVal(val);
286 if(val > _maxFuncVal) _maxFuncVal= 1.05*val;
293 if (_verbose &&_totalEvents%10000==0) {
294 std::cerr <<
"RooAcceptReject: generated " << _totalEvents <<
" events so far." << std::endl ;
299double RooAcceptReject::getFuncMax()
306 while(_totalEvents < _minTrials) {
310 if (_cache->numEntries()>1000000) {
311 oocoutI(
nullptr, Generation) <<
"RooAcceptReject::getFuncMax: resetting event cache" << std::endl ;
320std::string
const& RooAcceptReject::generatorName()
const {
321 static const std::string
name =
"RooAcceptReject";
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
long long Long64_t
Portable signed long integer 8 bytes.
Storage_t const & get() const
Const access to the underlying stl container.
Abstract base class for objects that represent a real value and implements functionality common to al...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Holds the configuration parameters of the various numeric integrators used by RooRealIntegral.
const RooArgSet & getConfigSection(const char *name) const
Retrieve configuration information specific to integrator with given name.
static double uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1).
Variable that can be changed from the outside.
const char * GetName() const override
Returns name of object.