// RooSimSplitGenContext is an efficient implementation of the generator context
// specific for RooSimultaneous PDFs when generating more than one of the
// component pdfs.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "RooSimSplitGenContext.h"
#include "RooSimultaneous.h"
#include "RooRealProxy.h"
#include "RooDataSet.h"
#include "Roo1DTable.h"
#include "RooCategory.h"
#include "RooMsgService.h"
#include "RooRandom.h"
#include "RooGlobalFunc.h"
using namespace RooFit ;
#include <string>
using namespace std;
ClassImp(RooSimSplitGenContext)
;
RooSimSplitGenContext::RooSimSplitGenContext(const RooSimultaneous &model, const RooArgSet &vars, Bool_t verbose, Bool_t autoBinned, const char* binnedTag) :
RooAbsGenContext(model,vars,0,0,verbose), _pdf(&model)
{
RooAbsCategory *idxCat = (RooAbsCategory*) model._indexCat.absArg() ;
RooArgSet pdfVars(vars) ;
RooArgSet allPdfVars(pdfVars) ;
if (!idxCat->isDerived()) {
pdfVars.remove(*idxCat,kTRUE,kTRUE) ;
Bool_t doGenIdx = allPdfVars.find(idxCat->GetName())?kTRUE:kFALSE ;
if (!doGenIdx) {
oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: This context must"
<< " generate the index category" << endl ;
_isValid = kFALSE ;
_numPdf = 0 ;
return ;
}
} else {
TIterator* sIter = idxCat->serverIterator() ;
RooAbsArg* server ;
Bool_t anyServer(kFALSE), allServers(kTRUE) ;
while((server=(RooAbsArg*)sIter->Next())) {
if (vars.find(server->GetName())) {
anyServer=kTRUE ;
pdfVars.remove(*server,kTRUE,kTRUE) ;
} else {
allServers=kFALSE ;
}
}
delete sIter ;
if (anyServer && !allServers) {
oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: This context must"
<< " generate all components of a derived index category" << endl ;
_isValid = kFALSE ;
_numPdf = 0 ;
return ;
}
}
_idxCatName = idxCat->GetName() ;
if (!model.canBeExtended()) {
oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: Need either extended mode"
<< " to calculate number of events per category" << endl ;
_isValid = kFALSE ;
_numPdf = 0 ;
return ;
}
_numPdf = model._pdfProxyList.GetSize() ;
_fracThresh = new Double_t[_numPdf+1] ;
_fracThresh[0] = 0 ;
_proxyIter = model._pdfProxyList.MakeIterator() ;
_allVarsPdf.add(allPdfVars) ;
RooRealProxy* proxy ;
RooAbsPdf* pdf ;
Int_t i(1) ;
while((proxy=(RooRealProxy*)_proxyIter->Next())) {
pdf=(RooAbsPdf*)proxy->absArg() ;
RooArgSet* compVars = pdf->getObservables(pdfVars) ;
RooAbsGenContext* cx = pdf->autoGenContext(*compVars,0,0,verbose,autoBinned,binnedTag) ;
delete compVars ;
const RooCatType* state = idxCat->lookupType(proxy->name()) ;
cx->SetName(proxy->name()) ;
_gcList.push_back(cx) ;
_gcIndex.push_back(state->getVal()) ;
_fracThresh[i] = _fracThresh[i-1] + pdf->expectedEvents(&allPdfVars) ;
i++ ;
}
for(i=0 ; i<_numPdf ; i++) {
_fracThresh[i] /= _fracThresh[_numPdf] ;
}
_idxCatSet = (RooArgSet*) RooArgSet(model._indexCat.arg()).snapshot(kTRUE) ;
if (!_idxCatSet) {
oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << ") Couldn't deep-clone index category, abort," << endl ;
throw std::string("RooSimSplitGenContext::RooSimSplitGenContext() Couldn't deep-clone index category, abort") ;
}
_idxCat = (RooAbsCategoryLValue*) _idxCatSet->find(model._indexCat.arg().GetName()) ;
}
RooSimSplitGenContext::~RooSimSplitGenContext()
{
delete[] _fracThresh ;
delete _idxCatSet ;
for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
delete (*iter) ;
}
delete _proxyIter ;
}
void RooSimSplitGenContext::attach(const RooArgSet& args)
{
if (_idxCat->isDerived()) {
_idxCat->recursiveRedirectServers(args,kTRUE) ;
}
for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
(*iter)->attach(args) ;
}
}
void RooSimSplitGenContext::initGenerator(const RooArgSet &theEvent)
{
if (_idxCat->isDerived()) {
_idxCat->recursiveRedirectServers(theEvent,kTRUE) ;
} else {
_idxCat = (RooAbsCategoryLValue*) theEvent.find(_idxCat->GetName()) ;
}
for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
(*iter)->initGenerator(theEvent) ;
}
}
RooDataSet* RooSimSplitGenContext::generate(Double_t nEvents, Bool_t skipInit, Bool_t extendedMode)
{
if(!isValid()) {
coutE(Generation) << ClassName() << "::" << GetName() << ": context is not valid" << endl;
return 0;
}
if(nEvents <= 0) {
nEvents= _expectedEvents;
}
coutI(Generation) << ClassName() << "::" << GetName() << ":generate: will generate "
<< nEvents << " events" << endl;
if (_verbose) Print("v") ;
if (!skipInit) {
initGenerator(*_theEvent);
}
vector<Double_t> nGen(_numPdf) ;
if (extendedMode ) {
_proxyIter->Reset() ;
RooRealProxy* proxy ;
Int_t i(0) ;
while((proxy=(RooRealProxy*)_proxyIter->Next())) {
RooAbsPdf* pdf=(RooAbsPdf*)proxy->absArg() ;
nGen[i] = pdf->expectedEvents(&_allVarsPdf) ;
i++ ;
}
} else {
_proxyIter->Reset() ;
RooRealProxy* proxy ;
Int_t i(1) ;
_fracThresh[0] = 0 ;
while((proxy=(RooRealProxy*)_proxyIter->Next())) {
RooAbsPdf* pdf=(RooAbsPdf*)proxy->absArg() ;
_fracThresh[i] = _fracThresh[i-1] + pdf->expectedEvents(&_allVarsPdf) ;
i++ ;
}
for(i=0 ; i<_numPdf ; i++) {
_fracThresh[i] /= _fracThresh[_numPdf] ;
}
Double_t nGenSoFar(0) ;
while (nGenSoFar<nEvents) {
Double_t rand = RooRandom::uniform() ;
i=0 ;
for (i=0 ; i<_numPdf ; i++) {
if (rand>_fracThresh[i] && rand<_fracThresh[i+1]) {
nGen[i]++ ;
nGenSoFar++ ;
break ;
}
}
}
}
_proxyIter->Reset() ;
map<string,RooAbsData*> dataMap ;
Int_t icomp(0) ;
RooRealProxy* proxy ;
while((proxy=(RooRealProxy*)_proxyIter->Next())) {
if (_gcList[icomp]) {
dataMap[proxy->GetName()] = _gcList[icomp]->generate(nGen[icomp],skipInit,extendedMode) ;
}
icomp++ ;
}
RooDataSet* hmaster = new RooDataSet("hmaster","hmaster",_allVarsPdf,RooFit::Index((RooCategory&)*_idxCat),RooFit::Link(dataMap),RooFit::OwnLinked()) ;
return hmaster ;
}
void RooSimSplitGenContext::setExpectedData(Bool_t flag)
{
for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
(*iter)->setExpectedData(flag) ;
}
}
RooDataSet* RooSimSplitGenContext::createDataSet(const char* , const char* , const RooArgSet& )
{
return 0 ;
}
void RooSimSplitGenContext::generateEvent(RooArgSet &, Int_t )
{
assert(0) ;
}
void RooSimSplitGenContext::setProtoDataOrder(Int_t* )
{
assert(0) ;
}
void RooSimSplitGenContext::printMultiline(ostream &os, Int_t content, Bool_t verbose, TString indent) const
{
RooAbsGenContext::printMultiline(os,content,verbose,indent) ;
os << indent << "--- RooSimSplitGenContext ---" << endl ;
os << indent << "Using PDF ";
_pdf->printStream(os,kName|kArgs|kClassName,kSingleLine,indent);
}
RooSimSplitGenContext.cxx:1 RooSimSplitGenContext.cxx:2 RooSimSplitGenContext.cxx:3 RooSimSplitGenContext.cxx:4 RooSimSplitGenContext.cxx:5 RooSimSplitGenContext.cxx:6 RooSimSplitGenContext.cxx:7 RooSimSplitGenContext.cxx:8 RooSimSplitGenContext.cxx:9 RooSimSplitGenContext.cxx:10 RooSimSplitGenContext.cxx:11 RooSimSplitGenContext.cxx:12 RooSimSplitGenContext.cxx:13 RooSimSplitGenContext.cxx:14 RooSimSplitGenContext.cxx:15 RooSimSplitGenContext.cxx:16 RooSimSplitGenContext.cxx:17 RooSimSplitGenContext.cxx:18 RooSimSplitGenContext.cxx:19 RooSimSplitGenContext.cxx:20 RooSimSplitGenContext.cxx:21 RooSimSplitGenContext.cxx:22 RooSimSplitGenContext.cxx:23 RooSimSplitGenContext.cxx:24 RooSimSplitGenContext.cxx:25 RooSimSplitGenContext.cxx:26 RooSimSplitGenContext.cxx:27 RooSimSplitGenContext.cxx:28 RooSimSplitGenContext.cxx:29 RooSimSplitGenContext.cxx:30 RooSimSplitGenContext.cxx:31 RooSimSplitGenContext.cxx:32 RooSimSplitGenContext.cxx:33 RooSimSplitGenContext.cxx:34 RooSimSplitGenContext.cxx:35 RooSimSplitGenContext.cxx:36 RooSimSplitGenContext.cxx:37 RooSimSplitGenContext.cxx:38 RooSimSplitGenContext.cxx:39 RooSimSplitGenContext.cxx:40 RooSimSplitGenContext.cxx:41 RooSimSplitGenContext.cxx:42 RooSimSplitGenContext.cxx:43 RooSimSplitGenContext.cxx:44 RooSimSplitGenContext.cxx:45 RooSimSplitGenContext.cxx:46 RooSimSplitGenContext.cxx:47 RooSimSplitGenContext.cxx:48 RooSimSplitGenContext.cxx:49 RooSimSplitGenContext.cxx:50 RooSimSplitGenContext.cxx:51 RooSimSplitGenContext.cxx:52 RooSimSplitGenContext.cxx:53 RooSimSplitGenContext.cxx:54 RooSimSplitGenContext.cxx:55 RooSimSplitGenContext.cxx:56 RooSimSplitGenContext.cxx:57 RooSimSplitGenContext.cxx:58 RooSimSplitGenContext.cxx:59 RooSimSplitGenContext.cxx:60 RooSimSplitGenContext.cxx:61 RooSimSplitGenContext.cxx:62 RooSimSplitGenContext.cxx:63 RooSimSplitGenContext.cxx:64 RooSimSplitGenContext.cxx:65 RooSimSplitGenContext.cxx:66 RooSimSplitGenContext.cxx:67 RooSimSplitGenContext.cxx:68 RooSimSplitGenContext.cxx:69 RooSimSplitGenContext.cxx:70 RooSimSplitGenContext.cxx:71 RooSimSplitGenContext.cxx:72 RooSimSplitGenContext.cxx:73 RooSimSplitGenContext.cxx:74 RooSimSplitGenContext.cxx:75 RooSimSplitGenContext.cxx:76 RooSimSplitGenContext.cxx:77 RooSimSplitGenContext.cxx:78 RooSimSplitGenContext.cxx:79 RooSimSplitGenContext.cxx:80 RooSimSplitGenContext.cxx:81 RooSimSplitGenContext.cxx:82 RooSimSplitGenContext.cxx:83 RooSimSplitGenContext.cxx:84 RooSimSplitGenContext.cxx:85 RooSimSplitGenContext.cxx:86 RooSimSplitGenContext.cxx:87 RooSimSplitGenContext.cxx:88 RooSimSplitGenContext.cxx:89 RooSimSplitGenContext.cxx:90 RooSimSplitGenContext.cxx:91 RooSimSplitGenContext.cxx:92 RooSimSplitGenContext.cxx:93 RooSimSplitGenContext.cxx:94 RooSimSplitGenContext.cxx:95 RooSimSplitGenContext.cxx:96 RooSimSplitGenContext.cxx:97 RooSimSplitGenContext.cxx:98 RooSimSplitGenContext.cxx:99 RooSimSplitGenContext.cxx:100 RooSimSplitGenContext.cxx:101 RooSimSplitGenContext.cxx:102 RooSimSplitGenContext.cxx:103 RooSimSplitGenContext.cxx:104 RooSimSplitGenContext.cxx:105 RooSimSplitGenContext.cxx:106 RooSimSplitGenContext.cxx:107 RooSimSplitGenContext.cxx:108 RooSimSplitGenContext.cxx:109 RooSimSplitGenContext.cxx:110 RooSimSplitGenContext.cxx:111 RooSimSplitGenContext.cxx:112 RooSimSplitGenContext.cxx:113 RooSimSplitGenContext.cxx:114 RooSimSplitGenContext.cxx:115 RooSimSplitGenContext.cxx:116 RooSimSplitGenContext.cxx:117 RooSimSplitGenContext.cxx:118 RooSimSplitGenContext.cxx:119 RooSimSplitGenContext.cxx:120 RooSimSplitGenContext.cxx:121 RooSimSplitGenContext.cxx:122 RooSimSplitGenContext.cxx:123 RooSimSplitGenContext.cxx:124 RooSimSplitGenContext.cxx:125 RooSimSplitGenContext.cxx:126 RooSimSplitGenContext.cxx:127 RooSimSplitGenContext.cxx:128 RooSimSplitGenContext.cxx:129 RooSimSplitGenContext.cxx:130 RooSimSplitGenContext.cxx:131 RooSimSplitGenContext.cxx:132 RooSimSplitGenContext.cxx:133 RooSimSplitGenContext.cxx:134 RooSimSplitGenContext.cxx:135 RooSimSplitGenContext.cxx:136 RooSimSplitGenContext.cxx:137 RooSimSplitGenContext.cxx:138 RooSimSplitGenContext.cxx:139 RooSimSplitGenContext.cxx:140 RooSimSplitGenContext.cxx:141 RooSimSplitGenContext.cxx:142 RooSimSplitGenContext.cxx:143 RooSimSplitGenContext.cxx:144 RooSimSplitGenContext.cxx:145 RooSimSplitGenContext.cxx:146 RooSimSplitGenContext.cxx:147 RooSimSplitGenContext.cxx:148 RooSimSplitGenContext.cxx:149 RooSimSplitGenContext.cxx:150 RooSimSplitGenContext.cxx:151 RooSimSplitGenContext.cxx:152 RooSimSplitGenContext.cxx:153 RooSimSplitGenContext.cxx:154 RooSimSplitGenContext.cxx:155 RooSimSplitGenContext.cxx:156 RooSimSplitGenContext.cxx:157 RooSimSplitGenContext.cxx:158 RooSimSplitGenContext.cxx:159 RooSimSplitGenContext.cxx:160 RooSimSplitGenContext.cxx:161 RooSimSplitGenContext.cxx:162 RooSimSplitGenContext.cxx:163 RooSimSplitGenContext.cxx:164 RooSimSplitGenContext.cxx:165 RooSimSplitGenContext.cxx:166 RooSimSplitGenContext.cxx:167 RooSimSplitGenContext.cxx:168 RooSimSplitGenContext.cxx:169 RooSimSplitGenContext.cxx:170 RooSimSplitGenContext.cxx:171 RooSimSplitGenContext.cxx:172 RooSimSplitGenContext.cxx:173 RooSimSplitGenContext.cxx:174 RooSimSplitGenContext.cxx:175 RooSimSplitGenContext.cxx:176 RooSimSplitGenContext.cxx:177 RooSimSplitGenContext.cxx:178 RooSimSplitGenContext.cxx:179 RooSimSplitGenContext.cxx:180 RooSimSplitGenContext.cxx:181 RooSimSplitGenContext.cxx:182 RooSimSplitGenContext.cxx:183 RooSimSplitGenContext.cxx:184 RooSimSplitGenContext.cxx:185 RooSimSplitGenContext.cxx:186 RooSimSplitGenContext.cxx:187 RooSimSplitGenContext.cxx:188 RooSimSplitGenContext.cxx:189 RooSimSplitGenContext.cxx:190 RooSimSplitGenContext.cxx:191 RooSimSplitGenContext.cxx:192 RooSimSplitGenContext.cxx:193 RooSimSplitGenContext.cxx:194 RooSimSplitGenContext.cxx:195 RooSimSplitGenContext.cxx:196 RooSimSplitGenContext.cxx:197 RooSimSplitGenContext.cxx:198 RooSimSplitGenContext.cxx:199 RooSimSplitGenContext.cxx:200 RooSimSplitGenContext.cxx:201 RooSimSplitGenContext.cxx:202 RooSimSplitGenContext.cxx:203 RooSimSplitGenContext.cxx:204 RooSimSplitGenContext.cxx:205 RooSimSplitGenContext.cxx:206 RooSimSplitGenContext.cxx:207 RooSimSplitGenContext.cxx:208 RooSimSplitGenContext.cxx:209 RooSimSplitGenContext.cxx:210 RooSimSplitGenContext.cxx:211 RooSimSplitGenContext.cxx:212 RooSimSplitGenContext.cxx:213 RooSimSplitGenContext.cxx:214 RooSimSplitGenContext.cxx:215 RooSimSplitGenContext.cxx:216 RooSimSplitGenContext.cxx:217 RooSimSplitGenContext.cxx:218 RooSimSplitGenContext.cxx:219 RooSimSplitGenContext.cxx:220 RooSimSplitGenContext.cxx:221 RooSimSplitGenContext.cxx:222 RooSimSplitGenContext.cxx:223 RooSimSplitGenContext.cxx:224 RooSimSplitGenContext.cxx:225 RooSimSplitGenContext.cxx:226 RooSimSplitGenContext.cxx:227 RooSimSplitGenContext.cxx:228 RooSimSplitGenContext.cxx:229 RooSimSplitGenContext.cxx:230 RooSimSplitGenContext.cxx:231 RooSimSplitGenContext.cxx:232 RooSimSplitGenContext.cxx:233 RooSimSplitGenContext.cxx:234 RooSimSplitGenContext.cxx:235 RooSimSplitGenContext.cxx:236 RooSimSplitGenContext.cxx:237 RooSimSplitGenContext.cxx:238 RooSimSplitGenContext.cxx:239 RooSimSplitGenContext.cxx:240 RooSimSplitGenContext.cxx:241 RooSimSplitGenContext.cxx:242 RooSimSplitGenContext.cxx:243 RooSimSplitGenContext.cxx:244 RooSimSplitGenContext.cxx:245 RooSimSplitGenContext.cxx:246 RooSimSplitGenContext.cxx:247 RooSimSplitGenContext.cxx:248 RooSimSplitGenContext.cxx:249 RooSimSplitGenContext.cxx:250 RooSimSplitGenContext.cxx:251 RooSimSplitGenContext.cxx:252 RooSimSplitGenContext.cxx:253 RooSimSplitGenContext.cxx:254 RooSimSplitGenContext.cxx:255 RooSimSplitGenContext.cxx:256 RooSimSplitGenContext.cxx:257 RooSimSplitGenContext.cxx:258 RooSimSplitGenContext.cxx:259 RooSimSplitGenContext.cxx:260 RooSimSplitGenContext.cxx:261 RooSimSplitGenContext.cxx:262 RooSimSplitGenContext.cxx:263 RooSimSplitGenContext.cxx:264 RooSimSplitGenContext.cxx:265 RooSimSplitGenContext.cxx:266 RooSimSplitGenContext.cxx:267 RooSimSplitGenContext.cxx:268 RooSimSplitGenContext.cxx:269 RooSimSplitGenContext.cxx:270 RooSimSplitGenContext.cxx:271 RooSimSplitGenContext.cxx:272 RooSimSplitGenContext.cxx:273 RooSimSplitGenContext.cxx:274 RooSimSplitGenContext.cxx:275 RooSimSplitGenContext.cxx:276 RooSimSplitGenContext.cxx:277 RooSimSplitGenContext.cxx:278 RooSimSplitGenContext.cxx:279 RooSimSplitGenContext.cxx:280 RooSimSplitGenContext.cxx:281 RooSimSplitGenContext.cxx:282 RooSimSplitGenContext.cxx:283 RooSimSplitGenContext.cxx:284 RooSimSplitGenContext.cxx:285 RooSimSplitGenContext.cxx:286 RooSimSplitGenContext.cxx:287 RooSimSplitGenContext.cxx:288 RooSimSplitGenContext.cxx:289 RooSimSplitGenContext.cxx:290 RooSimSplitGenContext.cxx:291 RooSimSplitGenContext.cxx:292 RooSimSplitGenContext.cxx:293 RooSimSplitGenContext.cxx:294 RooSimSplitGenContext.cxx:295 RooSimSplitGenContext.cxx:296 RooSimSplitGenContext.cxx:297 RooSimSplitGenContext.cxx:298 RooSimSplitGenContext.cxx:299 RooSimSplitGenContext.cxx:300 RooSimSplitGenContext.cxx:301 RooSimSplitGenContext.cxx:302 RooSimSplitGenContext.cxx:303 RooSimSplitGenContext.cxx:304 RooSimSplitGenContext.cxx:305 RooSimSplitGenContext.cxx:306 RooSimSplitGenContext.cxx:307 RooSimSplitGenContext.cxx:308 RooSimSplitGenContext.cxx:309 RooSimSplitGenContext.cxx:310 RooSimSplitGenContext.cxx:311 RooSimSplitGenContext.cxx:312 RooSimSplitGenContext.cxx:313 RooSimSplitGenContext.cxx:314 RooSimSplitGenContext.cxx:315 RooSimSplitGenContext.cxx:316 RooSimSplitGenContext.cxx:317 RooSimSplitGenContext.cxx:318 RooSimSplitGenContext.cxx:319 RooSimSplitGenContext.cxx:320 RooSimSplitGenContext.cxx:321 RooSimSplitGenContext.cxx:322 RooSimSplitGenContext.cxx:323 RooSimSplitGenContext.cxx:324 RooSimSplitGenContext.cxx:325 RooSimSplitGenContext.cxx:326 RooSimSplitGenContext.cxx:327 RooSimSplitGenContext.cxx:328 RooSimSplitGenContext.cxx:329 RooSimSplitGenContext.cxx:330 RooSimSplitGenContext.cxx:331 RooSimSplitGenContext.cxx:332 RooSimSplitGenContext.cxx:333 RooSimSplitGenContext.cxx:334 RooSimSplitGenContext.cxx:335 RooSimSplitGenContext.cxx:336 RooSimSplitGenContext.cxx:337 RooSimSplitGenContext.cxx:338 RooSimSplitGenContext.cxx:339 RooSimSplitGenContext.cxx:340 RooSimSplitGenContext.cxx:341 RooSimSplitGenContext.cxx:342 RooSimSplitGenContext.cxx:343 RooSimSplitGenContext.cxx:344 RooSimSplitGenContext.cxx:345 RooSimSplitGenContext.cxx:346