// Class RooGenContext implement a universal generator context for all
// RooAbsPdf classes that do not have or need a specialized generator
// context. This generator context queries the input p.d.f which observables
// it can generate internally and delegates generation of those observables
// to the p.d.f if it deems that safe. The other observables are generated
// use a RooAcceptReject sampling technique.
// END_HTML
#include "RooFit.h"
#include "RooMsgService.h"
#include "Riostream.h"
#include "RooGenContext.h"
#include "RooGenContext.h"
#include "RooAbsPdf.h"
#include "RooDataSet.h"
#include "RooRealIntegral.h"
#include "RooAcceptReject.h"
#include "RooRealVar.h"
#include "RooDataHist.h"
#include "RooErrorHandler.h"
#include "TString.h"
#include "TIterator.h"
ClassImp(RooGenContext)
;
RooGenContext::RooGenContext(const RooAbsPdf &model, const RooArgSet &vars,
const RooDataSet *prototype, const RooArgSet* auxProto,
Bool_t verbose, const RooArgSet* forceDirect) :
RooAbsGenContext(model,vars,prototype,auxProto,verbose),
_cloneSet(0), _pdfClone(0), _acceptRejectFunc(0), _generator(0),
_maxVar(0), _uniIter(0), _updateFMaxPerEvent(0)
{
cxcoutI(Generation) << "RooGenContext::ctor() setting up event generator context for p.d.f. " << model.GetName()
<< " for generation of observable(s) " << vars ;
if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
if (auxProto && auxProto->getSize()>0) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
if (forceDirect && forceDirect->getSize()>0) ccxcoutI(Generation) << " with internal generation forced for observables " << *forceDirect ;
ccxcoutI(Generation) << endl ;
RooArgSet nodes(model,model.GetName());
_cloneSet= (RooArgSet*) nodes.snapshot(kTRUE);
if (!_cloneSet) {
coutE(Generation) << "RooGenContext::RooGenContext(" << GetName() << ") Couldn't deep-clone PDF, abort," << endl ;
RooErrorHandler::softAbort() ;
}
_pdfClone = (RooAbsPdf*)_cloneSet->find(model.GetName());
if (prototype&&_pdfClone->dependsOn(*prototype->get())) {
RooArgSet fullNormSet(vars) ;
fullNormSet.add(*prototype->get()) ;
_pdfClone->fixAddCoefNormalization(fullNormSet) ;
}
_isValid= kTRUE;
TIterator *iterator= vars.createIterator();
TIterator *servers= _pdfClone->serverIterator();
const RooAbsArg *tmp = 0;
const RooAbsArg *arg = 0;
while((_isValid && (tmp= (const RooAbsArg*)iterator->Next()))) {
if(!tmp->isFundamental()) {
coutE(Generation) << "RooGenContext::ctor(): cannot generate values for derived \"" << tmp->GetName() << "\"" << endl;
_isValid= kFALSE;
continue;
}
arg= (const RooAbsArg*)_cloneSet->find(tmp->GetName());
if(0 == arg) {
coutI(Generation) << "RooGenContext::ctor() WARNING model does not depend on \"" << tmp->GetName()
<< "\" which will have uniform distribution" << endl;
_uniformVars.add(*tmp);
}
else {
const RooAbsArg *direct= arg ;
if (forceDirect==0 || !forceDirect->find(direct->GetName())) {
if (!_pdfClone->isDirectGenSafe(*arg)) {
cxcoutD(Generation) << "RooGenContext::ctor() observable " << arg->GetName() << " has been determined to be unsafe for internal generation" << endl;
direct=0 ;
}
}
if(direct) {
_directVars.add(*arg);
} else {
_otherVars.add(*arg);
}
}
}
delete servers;
delete iterator;
if(!isValid()) {
coutE(Generation) << "RooGenContext::ctor() constructor failed with errors" << endl;
return;
}
if (_directVars.getSize()>0) {
cxcoutD(Generation) << "RooGenContext::ctor() observables " << _directVars << " are safe for internal generator (if supported by p.d.f)" << endl ;
}
if (_otherVars.getSize()>0) {
cxcoutD(Generation) << "RooGenContext::ctor() observables " << _otherVars << " are NOT safe for internal generator (if supported by p.d.f)" << endl ;
}
Bool_t staticInitOK = !_pdfClone->dependsOn(_protoVars) ;
if (!staticInitOK) {
cxcoutD(Generation) << "RooGenContext::ctor() Model depends on supplied protodata observables, static initialization of internal generator will not be allowed" << endl ;
}
RooArgSet generatedVars;
if (_directVars.getSize()>0) {
_code= _pdfClone->getGenerator(_directVars,generatedVars,staticInitOK);
cxcoutD(Generation) << "RooGenContext::ctor() Model indicates that it can internally generate observables "
<< generatedVars << " with configuration identifier " << _code << endl ;
} else {
_code = 0 ;
}
_directVars.remove(generatedVars);
_otherVars.add(_directVars);
_directVars.removeAll();
_directVars.add(generatedVars);
cxcoutI(Generation) << "RooGenContext::ctor() Context will" ;
if (_directVars.getSize()>0) ccxcoutI(Generation) << " let model internally generate observables " << _directVars ;
if (_directVars.getSize()>0 && _otherVars.getSize()>0) ccxcoutI(Generation) << " and" ;
if (_otherVars.getSize()>0) ccxcoutI(Generation) << " generate variables " << _otherVars << " with accept/reject sampling" ;
if (_uniformVars.getSize()>0) ccxcoutI(Generation) << ", non-observable variables " << _uniformVars << " will be generated with flat distribution" ;
ccxcoutI(Generation) << endl ;
RooArgSet *depList= _pdfClone->getObservables(_theEvent);
depList->remove(_otherVars);
TString nname(_pdfClone->GetName()) ;
nname.Append("_AccRej") ;
TString ntitle(_pdfClone->GetTitle()) ;
ntitle.Append(" (Accept/Reject)") ;
if (_protoVars.getSize()==0) {
if(depList->getSize()==0) {
Int_t maxFindCode = _pdfClone->getMaxVal(_otherVars) ;
if (maxFindCode != 0) {
coutI(Generation) << "RooGenContext::ctor() no prototype data provided, all observables are generated with accept/reject and "
<< "model supports maximum finding, initial sampling phase skipped" << endl ;
Double_t maxVal = _pdfClone->maxVal(maxFindCode) / _pdfClone->getNorm(_theEvent) ;
_maxVar = new RooRealVar("funcMax","function maximum",maxVal) ;
cxcoutD(Generation) << "RooGenContext::ctor() maximum value returned by RooAbsPdf::maxVal() is " << maxVal << endl ;
}
}
if (_otherVars.getSize()>0) {
_pdfClone->getVal(&vars) ;
_acceptRejectFunc= new RooRealIntegral(nname,ntitle,*_pdfClone,*depList,&vars);
cxcoutI(Generation) << "RooGenContext::ctor() accept/reject sampling function is " << _acceptRejectFunc->GetName() << endl ;
} else {
_acceptRejectFunc = 0 ;
}
} else {
depList->remove(_protoVars,kTRUE,kTRUE) ;
_acceptRejectFunc= (RooRealIntegral*) _pdfClone->createIntegral(*depList,vars) ;
cxcoutI(Generation) << "RooGenContext::ctor() accept/reject sampling function is " << _acceptRejectFunc->GetName() << endl ;
if (_directVars.getSize()==0) {
Int_t maxFindCode = _pdfClone->getMaxVal(_otherVars) ;
if (maxFindCode != 0) {
coutI(Generation) << "RooGenContext::ctor() prototype data provided, all observables are generated with accept/reject and "
<< "model supports maximum finding, initial sampling phase skipped" << endl ;
_maxVar = new RooRealVar("funcMax","function maximum",1) ;
_updateFMaxPerEvent = maxFindCode ;
cxcoutD(Generation) << "RooGenContext::ctor() maximum value must be reevaluated for each event with configuration code " << maxFindCode << endl ;
}
}
if (!_maxVar) {
RooArgSet otherAndProto(_otherVars) ;
RooArgSet* protoDeps = model.getObservables(_protoVars) ;
otherAndProto.add(*protoDeps) ;
delete protoDeps ;
if (_otherVars.getSize()>0) {
cxcoutD(Generation) << "RooGenContext::ctor() sampling observables through accept/reject sample and have prototype data and no "
<< "maximum function value provided by mode, will determine maximum value through initial sampling space "
<< "of accept/reject observables plus prototype observables: " << otherAndProto << endl ;
RooAcceptReject maxFinder(*_acceptRejectFunc,otherAndProto,0,_verbose) ;
Double_t max = maxFinder.getFuncMax() ;
_maxVar = new RooRealVar("funcMax","function maximum",max) ;
cxcoutD(Generation) << "RooGenContext::ctor() maximum function value found through initial sampling is " << max << endl ;
}
}
}
if (_acceptRejectFunc) {
cxcoutD(Generation) << "RooGenContext::ctor() creating accept reject generator from a/r function for observables " << _otherVars << endl ;
_generator= new RooAcceptReject(*_acceptRejectFunc,_otherVars,_maxVar,_verbose);
} else {
_generator = 0 ;
}
delete depList;
_otherVars.add(_uniformVars);
}
RooGenContext::~RooGenContext()
{
delete _cloneSet;
if (_generator) delete _generator;
if (_acceptRejectFunc) delete _acceptRejectFunc;
if (_maxVar) delete _maxVar ;
if (_uniIter) delete _uniIter ;
}
void RooGenContext::attach(const RooArgSet& args)
{
_pdfClone->recursiveRedirectServers(args,kFALSE);
if (_acceptRejectFunc) {
_acceptRejectFunc->recursiveRedirectServers(args,kFALSE) ;
}
if (_generator) {
_generator->attachParameters(args) ;
}
}
void RooGenContext::initGenerator(const RooArgSet &theEvent)
{
attach(theEvent) ;
_pdfClone->resetErrorCounters();
if (_directVars.getSize()>0) {
cxcoutD(Generation) << "RooGenContext::initGenerator() initializing internal generator of model with code " << _code << endl ;
_pdfClone->initGenerator(_code) ;
}
if (_uniformVars.getSize()>0) {
_uniIter = _uniformVars.createIterator() ;
}
}
void RooGenContext::generateEvent(RooArgSet &theEvent, Int_t remaining)
{
if(_otherVars.getSize() > 0) {
if (_updateFMaxPerEvent!=0) {
Double_t max = _pdfClone->maxVal(_updateFMaxPerEvent)/_pdfClone->getNorm(_otherVars) ;
cxcoutD(Generation) << "RooGenContext::initGenerator() reevaluation of maximum function value is required for each event, new value is " << max << endl ;
_maxVar->setVal(max) ;
}
if (_generator) {
const RooArgSet *subEvent= _generator->generateEvent(remaining);
if(0 == subEvent) {
coutE(Generation) << "RooGenContext::generateEvent ERROR accept/reject generator failed" << endl ;
return;
}
theEvent= *subEvent;
}
}
if(_directVars.getSize() > 0) {
_pdfClone->generateEvent(_code);
}
if (_uniIter) {
_uniIter->Reset() ;
RooAbsArg* uniVar ;
while((uniVar=(RooAbsArg*)_uniIter->Next())) {
RooAbsLValue* arglv = dynamic_cast<RooAbsLValue*>(uniVar) ;
if (!arglv) {
coutE(Generation) << "RooGenContext::generateEvent(" << GetName() << ") ERROR: uniform variable " << uniVar->GetName() << " is not an lvalue" << endl ;
RooErrorHandler::softAbort() ;
}
arglv->randomize() ;
}
theEvent = _uniformVars ;
}
}
void RooGenContext::printMultiline(ostream &os, Int_t content, Bool_t verbose, TString indent) const
{
RooAbsGenContext::printMultiline(os,content,verbose,indent);
os << indent << " --- RooGenContext --- " << endl ;
os << indent << "Using PDF ";
_pdfClone->printStream(os,kName|kArgs|kClassName,kSingleLine,indent);
if(verbose) {
os << indent << "Use PDF generator for " << _directVars << endl ;
os << indent << "Use accept/reject for " << _otherVars << endl ;
if (_protoVars.getSize()>0) {
os << indent << "Prototype observables are " << _protoVars << endl ;
}
}
}
Last change: Mon Aug 25 11:36:32 2008
Last generated: 2008-08-25 11:36
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.