// RooNumGenFactory is a factory to instantiate numeric integrators
// from a given function binding and a given configuration. The factory
// searches for a numeric integrator registered with the factory that
// has the ability to perform the numeric integration. The choice of
// method may depend on the number of dimensions integrated,
// the nature of the integration limits (closed or open ended) and
// the preference of the caller as encoded in the configuration object.
// END_HTML
#include "TClass.h"
#include "Riostream.h"
#include "RooFit.h"
#include "RooNumGenFactory.h"
#include "RooArgSet.h"
#include "RooAbsFunc.h"
#include "RooNumGenConfig.h"
#include "RooNumber.h"
#include "RooAcceptReject.h"
#include "RooFoamGenerator.h"
#include "RooSentinel.h"
#include "RooMsgService.h"
using namespace std ;
ClassImp(RooNumGenFactory)
;
RooNumGenFactory* RooNumGenFactory::_instance = 0 ;
RooNumGenFactory::RooNumGenFactory()
{
_instance = this ;
RooAcceptReject::registerSampler(*this) ;
RooFoamGenerator::registerSampler(*this) ;
RooNumGenConfig::defaultConfig().method1D(kFALSE,kFALSE).setLabel("RooFoamGenerator") ;
RooNumGenConfig::defaultConfig().method1D(kTRUE ,kFALSE).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().method1D(kFALSE,kTRUE ).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().method1D(kTRUE, kTRUE ).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().method2D(kFALSE,kFALSE).setLabel("RooFoamGenerator") ;
RooNumGenConfig::defaultConfig().method2D(kTRUE ,kFALSE).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().method2D(kFALSE,kTRUE ).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().method2D(kTRUE, kTRUE ).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().methodND(kFALSE,kFALSE).setLabel("RooFoamGenerator") ;
RooNumGenConfig::defaultConfig().methodND(kTRUE ,kFALSE).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().methodND(kFALSE,kTRUE ).setLabel("RooAcceptReject") ;
RooNumGenConfig::defaultConfig().methodND(kTRUE, kTRUE ).setLabel("RooAcceptReject") ;
}
RooNumGenFactory::~RooNumGenFactory()
{
std::map<std::string,RooAbsNumGenerator*>::iterator iter = _map.begin() ;
while (iter != _map.end()) {
delete iter->second ;
++iter ;
}
}
RooNumGenFactory::RooNumGenFactory(const RooNumGenFactory& other) : TObject(other)
{
}
RooNumGenFactory& RooNumGenFactory::instance()
{
if (_instance==0) {
new RooNumGenFactory ;
RooSentinel::activate() ;
}
return *_instance ;
}
void RooNumGenFactory::cleanup()
{
if (_instance) {
delete _instance ;
_instance = 0 ;
}
}
Bool_t RooNumGenFactory::storeProtoSampler(RooAbsNumGenerator* proto, const RooArgSet& defConfig)
{
TString name = proto->IsA()->GetName() ;
if (getProtoSampler(name)) {
return kTRUE ;
}
_map[name.Data()] = proto ;
RooNumGenConfig::defaultConfig().addConfigSection(proto,defConfig) ;
return kFALSE ;
}
const RooAbsNumGenerator* RooNumGenFactory::getProtoSampler(const char* name)
{
if (_map.count(name)==0) {
return 0 ;
}
return _map[name] ;
}
RooAbsNumGenerator* RooNumGenFactory::createSampler(RooAbsReal& func, const RooArgSet& genVars, const RooArgSet& condVars, const RooNumGenConfig& config, Bool_t verbose, RooAbsReal* maxFuncVal)
{
Int_t ndim = genVars.getSize() ;
Bool_t cond = (condVars.getSize() > 0) ? kTRUE : kFALSE ;
Bool_t hasCat(kFALSE) ;
TIterator* iter = genVars.createIterator() ;
RooAbsArg* arg ;
while ((arg=(RooAbsArg*)iter->Next())) {
if (arg->IsA()==RooCategory::Class()) {
hasCat=kTRUE ;
break ;
}
}
delete iter ;
TString method ;
switch(ndim) {
case 1:
method = config.method1D(cond,hasCat).getLabel() ;
break ;
case 2:
method = config.method2D(cond,hasCat).getLabel() ;
break ;
default:
method = config.methodND(cond,hasCat).getLabel() ;
break ;
}
if (!method.CompareTo("N/A")) {
oocoutE((TObject*)0,Integration) << "RooNumGenFactory::createSampler: No sampler method has been defined for "
<< (cond?"a conditional ":"a ") << ndim << "-dimensional p.d.f" << endl ;
return 0 ;
}
const RooAbsNumGenerator* proto = getProtoSampler(method) ;
RooAbsNumGenerator* engine = proto->clone(func,genVars,condVars,config,verbose,maxFuncVal) ;
return engine ;
}