46 ClassImp(RooAbsGenContext);
53 RooAbsGenContext::RooAbsGenContext(
const RooAbsPdf& model,
const RooArgSet &vars,
54 const RooDataSet *prototype,
const RooArgSet* auxProto, Bool_t verbose) :
56 _prototype(prototype),
64 if (model.recursiveCheckObservables(&vars)) {
65 coutE(Generation) <<
"RooAbsGenContext::ctor: Error in PDF dependents" << endl ;
71 _theEvent= (RooArgSet*)vars.snapshot(kFALSE);
76 TIterator *protoIterator= _prototype->get()->createIterator();
77 const RooAbsArg *proto = 0;
78 while((proto= (
const RooAbsArg*)protoIterator->Next())) {
80 if(!_theEvent->contains(*proto)) {
81 _protoVars.add(*proto);
82 _theEvent->addClone(*proto);
90 _protoVars.add(*auxProto) ;
91 _theEvent->addClone(*auxProto) ;
95 _extendMode = model.extendMode() ;
96 if (model.canBeExtended()) {
97 _expectedEvents= (Int_t)(model.expectedEvents(_theEvent) + 0.5);
103 if (model.normRange()) {
104 _normRange = model.normRange() ;
113 RooAbsGenContext::~RooAbsGenContext()
115 if(0 != _theEvent)
delete _theEvent;
116 if (_protoOrder)
delete[] _protoOrder ;
124 void RooAbsGenContext::attach(
const RooArgSet& )
133 RooDataSet* RooAbsGenContext::createDataSet(
const char* name,
const char* title,
const RooArgSet& obs)
135 RooDataSet* ret =
new RooDataSet(name, title, obs);
136 ret->setDirtyProp(kFALSE) ;
152 RooDataSet *RooAbsGenContext::generate(Double_t nEvents, Bool_t skipInit, Bool_t extendedMode)
155 coutE(Generation) << ClassName() <<
"::" << GetName() <<
": context is not valid" << endl;
162 nEvents= (Int_t)_prototype->numEntries();
165 if (_extendMode == RooAbsPdf::CanNotBeExtended) {
166 coutE(Generation) << ClassName() <<
"::" << GetName()
167 <<
":generate: PDF not extendable: cannot calculate expected number of events" << endl;
170 nEvents= _expectedEvents;
173 coutE(Generation) << ClassName() <<
"::" << GetName()
174 <<
":generate: cannot calculate expected number of events" << endl;
177 coutI(Generation) << ClassName() <<
"::" << GetName() <<
":generate: will generate "
178 << nEvents <<
" events" << endl;
183 double nExpEvents = nEvents;
184 nEvents = RooRandom::randomGenerator()->Poisson(nEvents) ;
185 cxcoutI(Generation) <<
" Extended mode active, number of events generated (" << nEvents <<
") is Poisson fluctuation on "
186 << GetName() <<
"::expectedEvents() = " << nExpEvents << endl ;
192 const RooArgSet *vars= _prototype->get();
193 TIterator *iterator= _protoVars.createIterator();
194 const RooAbsArg *arg = 0;
196 while((arg= (
const RooAbsArg*)iterator->Next())) {
197 if(vars->contains(*arg))
continue;
198 coutE(InputArguments) << ClassName() <<
"::" << GetName() <<
":generate: prototype dataset is missing \""
199 << arg->GetName() <<
"\"" << endl;
209 if (_verbose) Print(
"v") ;
212 TString name(GetName()),title(GetTitle());
214 title.Prepend(
"Generated From ");
217 _genData = createDataSet(name.Data(),title.Data(),*_theEvent) ;
222 initGenerator(*_theEvent);
227 while(_genData->numEntries()<nEvents) {
230 if(0 != _prototype) {
231 if(_nextProtoIndex >= _prototype->numEntries()) _nextProtoIndex= 0;
233 Int_t actualProtoIdx = _protoOrder ? _protoOrder[_nextProtoIndex] : _nextProtoIndex ;
235 const RooArgSet *subEvent= _prototype->get(actualProtoIdx);
238 *_theEvent= *subEvent;
241 coutE(Generation) << ClassName() <<
"::" << GetName() <<
":generate: cannot load event "
242 << actualProtoIdx <<
" from prototype dataset" << endl;
248 generateEvent(*_theEvent, (Int_t)(nEvents - _genData->numEntries()));
252 if (_normRange.Length()>0 && !_theEvent->isInRange(_normRange.Data())) {
256 _genData->addFast(*_theEvent);
260 RooDataSet* output = _genData ;
262 output->setDirtyProp(kTRUE) ;
273 void RooAbsGenContext::initGenerator(
const RooArgSet&)
282 void RooAbsGenContext::printName(ostream& os)
const
292 void RooAbsGenContext::printTitle(ostream& os)
const
302 void RooAbsGenContext::printClassName(ostream& os)
const
304 os << IsA()->GetName() ;
312 void RooAbsGenContext::printArgs(ostream& os)
const
315 TIterator* iter = _theEvent->createIterator() ;
317 Bool_t first(kTRUE) ;
318 while((arg=(RooAbsArg*)iter->Next())) {
324 os << arg->GetName() ;
335 void RooAbsGenContext::printMultiline(ostream &, Int_t , Bool_t , TString )
const
348 void RooAbsGenContext::setProtoDataOrder(Int_t* lut)
352 delete[] _protoOrder ;
357 if (lut && _prototype) {
358 Int_t n = _prototype->numEntries() ;
359 _protoOrder =
new Int_t[n] ;
361 for (i=0 ; i<n ; i++) {
362 _protoOrder[i] = lut[i] ;
373 void RooAbsGenContext::resampleData(Double_t& ratio)
376 Int_t nOrig = _genData->numEntries() ;
377 Int_t nTarg = Int_t(nOrig*ratio+0.5) ;
378 RooDataSet* trimmedData = (RooDataSet*) _genData->reduce(RooFit::EventRange(0,nTarg)) ;
380 cxcoutD(Generation) <<
"RooGenContext::resampleData*( existing production trimmed from " << nOrig <<
" to " << trimmedData->numEntries() <<
" events" << endl ;
383 _genData = trimmedData ;
388 _nextProtoIndex -= (nOrig-nTarg) ;
389 while (_nextProtoIndex<0) {
390 _nextProtoIndex += _prototype->numEntries() ;
402 Int_t RooAbsGenContext::defaultPrintContents(Option_t* )
const
404 return kName|kClassName|kValue ;
412 RooPrintable::StyleOption RooAbsGenContext::defaultPrintStyle(Option_t* opt)
const
414 if (opt && TString(opt).Contains(
"v")) {