Logo ROOT  
Reference Guide
RooAbsGenContext.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 /**
18 \file RooAbsGenContext.cxx
19 \class RooAbsGenContext
20 \ingroup Roofitcore
21 
22 RooAbsGenContext is the abstract base class for generator contexts of
23 RooAbsPdf objects. A generator context is an object that controls
24 the generation of events from a given p.d.f in one or more sessions.
25 This class defines the common interface for all such contexts and organizes
26 storage of common components, such as the observables definition, the
27 prototype data etc..
28 **/
29 
30 #include "RooFit.h"
31 
32 #include "TClass.h"
33 
34 #include "RooAbsGenContext.h"
35 #include "RooRandom.h"
36 #include "RooAbsPdf.h"
37 #include "RooDataSet.h"
38 #include "RooMsgService.h"
39 #include "RooGlobalFunc.h"
40 
41 #include "Riostream.h"
42 
43 
44 using namespace std;
45 
47 ;
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Constructor
52 
54  const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose) :
55  TNamed(model),
56  _prototype(prototype),
57  _theEvent(0),
58  _isValid(kTRUE),
59  _verbose(verbose),
60  _protoOrder(0),
61  _genData(0)
62 {
63  // Check PDF dependents
64  if (model.recursiveCheckObservables(&vars)) {
65  coutE(Generation) << "RooAbsGenContext::ctor: Error in PDF dependents" << endl ;
66  _isValid = kFALSE ;
67  return ;
68  }
69 
70  // Make a snapshot of the generated variables that we can overwrite.
72 
73  // Analyze the prototype dataset, if one is specified
74  _nextProtoIndex= 0;
75  if(0 != _prototype) {
76  TIterator *protoIterator= _prototype->get()->createIterator();
77  const RooAbsArg *proto = 0;
78  while((proto= (const RooAbsArg*)protoIterator->Next())) {
79  // is this variable being generated or taken from the prototype?
80  if(!_theEvent->contains(*proto)) {
83  }
84  }
85  delete protoIterator;
86  }
87 
88  // Add auxiliary protovars to _protoVars, if provided
89  if (auxProto) {
90  _protoVars.add(*auxProto) ;
91  _theEvent->addClone(*auxProto) ;
92  }
93 
94  // Remember the default number of events to generate when no prototype dataset is provided.
95  _extendMode = model.extendMode() ;
96  if (model.canBeExtended()) {
97  _expectedEvents= (Int_t)(model.expectedEvents(_theEvent) + 0.5);
98  } else {
99  _expectedEvents= 0 ;
100  }
101 
102  // Save normalization range
103  if (model.normRange()) {
104  _normRange = model.normRange() ;
105  }
106 }
107 
108 
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Destructor
112 
114 {
115  if(0 != _theEvent) delete _theEvent;
116  if (_protoOrder) delete[] _protoOrder ;
117 }
118 
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Interface to attach given parameters to object in this context
123 
124 void RooAbsGenContext::attach(const RooArgSet& /*params*/)
125 {
126 }
127 
128 
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Create an empty dataset to hold the events that will be generated
132 
133 RooDataSet* RooAbsGenContext::createDataSet(const char* name, const char* title, const RooArgSet& obs)
134 {
135  RooDataSet* ret = new RooDataSet(name, title, obs);
136  ret->setDirtyProp(kFALSE) ;
137  return ret ;
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Generate the specified number of events with nEvents>0 and
143 /// and return a dataset containing the generated events. With nEvents<=0,
144 /// generate the number of events in the prototype dataset, if available,
145 /// or else the expected number of events, if non-zero.
146 /// If extendedMode = true generate according to a Poisson(nEvents)
147 /// The returned dataset belongs to the caller. Return zero in case of an error.
148 /// Generation of individual events is delegated to a virtual generateEvent()
149 /// method. A virtual initGenerator() method is also called just before the
150 /// first call to generateEvent().
151 
152 RooDataSet *RooAbsGenContext::generate(Double_t nEvents, Bool_t skipInit, Bool_t extendedMode)
153 {
154  if(!isValid()) {
155  coutE(Generation) << ClassName() << "::" << GetName() << ": context is not valid" << endl;
156  return 0;
157  }
158 
159  // Calculate the expected number of events if necessary
160  if(nEvents <= 0) {
161  if(_prototype) {
162  nEvents= (Int_t)_prototype->numEntries();
163  }
164  else {
166  coutE(Generation) << ClassName() << "::" << GetName()
167  << ":generate: PDF not extendable: cannot calculate expected number of events" << endl;
168  return 0;
169  }
170  nEvents= _expectedEvents;
171  }
172  if(nEvents <= 0) {
173  coutE(Generation) << ClassName() << "::" << GetName()
174  << ":generate: cannot calculate expected number of events" << endl;
175  return 0;
176  }
177  coutI(Generation) << ClassName() << "::" << GetName() << ":generate: will generate "
178  << nEvents << " events" << endl;
179 
180  }
181 
182  if (extendedMode) {
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 ;
187  }
188 
189  // check that any prototype dataset still defines the variables we need
190  // (this is necessary since we never make a private clone, for efficiency)
191  if(_prototype) {
192  const RooArgSet *vars= _prototype->get();
193  TIterator *iterator= _protoVars.createIterator();
194  const RooAbsArg *arg = 0;
195  Bool_t ok(kTRUE);
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;
200 
201  // WVE disable this for the moment
202  // ok= kFALSE;
203  }
204  delete iterator;
205  // coverity[DEADCODE]
206  if(!ok) return 0;
207  }
208 
209  if (_verbose) Print("v") ;
210 
211  // create a new dataset
212  TString name(GetName()),title(GetTitle());
213  name.Append("Data");
214  title.Prepend("Generated From ");
215 
216  // WVE need specialization here for simultaneous pdfs
217  _genData = createDataSet(name.Data(),title.Data(),*_theEvent) ;
218 
219  // Perform any subclass implementation-specific initialization
220  // Can be skipped if this is a rerun with an identical configuration
221  if (!skipInit) {
223  }
224 
225  // Loop over the events to generate
226  Int_t evt(0) ;
227  while(_genData->numEntries()<nEvents) {
228 
229  // first, load values from the prototype dataset, if one was provided
230  if(0 != _prototype) {
232 
234 
235  const RooArgSet *subEvent= _prototype->get(actualProtoIdx);
236  _nextProtoIndex++;
237  if(0 != subEvent) {
238  *_theEvent= *subEvent;
239  }
240  else {
241  coutE(Generation) << ClassName() << "::" << GetName() << ":generate: cannot load event "
242  << actualProtoIdx << " from prototype dataset" << endl;
243  return 0;
244  }
245  }
246 
247  // delegate the generation of the rest of this event to our subclass implementation
248  generateEvent(*_theEvent, (Int_t)(nEvents - _genData->numEntries()));
249 
250 
251  // WVE add check that event is in normRange
253  continue ;
254  }
255 
257  evt++ ;
258  }
259 
261  _genData = 0 ;
262  output->setDirtyProp(kTRUE) ;
263 
264  return output;
265 }
266 
267 
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 /// Interface function to initialize context for generation for given
271 /// set of observables
272 
274 {
275 }
276 
277 
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// Print name of context
281 
282 void RooAbsGenContext::printName(ostream& os) const
283 {
284  os << GetName() ;
285 }
286 
287 
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 /// Print title of context
291 
292 void RooAbsGenContext::printTitle(ostream& os) const
293 {
294  os << GetTitle() ;
295 }
296 
297 
298 
299 ////////////////////////////////////////////////////////////////////////////////
300 /// Print class name of context
301 
302 void RooAbsGenContext::printClassName(ostream& os) const
303 {
304  os << IsA()->GetName() ;
305 }
306 
307 
308 
309 ////////////////////////////////////////////////////////////////////////////////
310 /// Print arguments of context, i.e. the observables being generated in this context
311 
312 void RooAbsGenContext::printArgs(ostream& os) const
313 {
314  os << "[ " ;
315  TIterator* iter = _theEvent->createIterator() ;
316  RooAbsArg* arg ;
317  Bool_t first(kTRUE) ;
318  while((arg=(RooAbsArg*)iter->Next())) {
319  if (first) {
320  first=kFALSE ;
321  } else {
322  os << "," ;
323  }
324  os << arg->GetName() ;
325  }
326  os << "]" ;
327  delete iter ;
328 }
329 
330 
331 
332 ////////////////////////////////////////////////////////////////////////////////
333 /// Interface for multi-line printing
334 
335 void RooAbsGenContext::printMultiline(ostream &/*os*/, Int_t /*contents*/, Bool_t /*verbose*/, TString /*indent*/) const
336 {
337 }
338 
339 
340 
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Set the traversal order of prototype data to that in the lookup tables
344 /// passed as argument. The LUT must be an array of integers with the same
345 /// size as the number of entries in the prototype dataset and must contain
346 /// integer values in the range [0,Nevt-1]
347 
349 {
350  // Delete any previous lookup table
351  if (_protoOrder) {
352  delete[] _protoOrder ;
353  _protoOrder = 0 ;
354  }
355 
356  // Copy new lookup table if provided and needed
357  if (lut && _prototype) {
359  _protoOrder = new Int_t[n] ;
360  Int_t i ;
361  for (i=0 ; i<n ; i++) {
362  _protoOrder[i] = lut[i] ;
363  }
364  }
365 }
366 
367 
368 
369 
370 ////////////////////////////////////////////////////////////////////////////////
371 /// Rescale existing output buffer with given ratio
372 
374 {
375 
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)) ;
379 
380  cxcoutD(Generation) << "RooGenContext::resampleData*( existing production trimmed from " << nOrig << " to " << trimmedData->numEntries() << " events" << endl ;
381 
382  delete _genData ;
383  _genData = trimmedData ;
384 
385  if (_prototype) {
386  // Push back proto index by trimmed amount to force recycling of the
387  // proto entries that were trimmed away
388  _nextProtoIndex -= (nOrig-nTarg) ;
389  while (_nextProtoIndex<0) {
391  }
392  }
393 
394 }
395 
396 
397 
398 
399 ////////////////////////////////////////////////////////////////////////////////
400 /// Define default contents when printing
401 
403 {
404  return kName|kClassName|kValue ;
405 }
406 
407 
408 
409 ////////////////////////////////////////////////////////////////////////////////
410 /// Define default print style
411 
413 {
414  if (opt && TString(opt).Contains("v")) {
415  return kVerbose ;
416  }
417  return kStandard ;
418 }
n
const Int_t n
Definition: legend1.C:16
first
Definition: first.py:1
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooPrintable::kVerbose
@ kVerbose
Definition: RooPrintable.h:34
RooMsgService.h
RooAbsGenContext::printName
virtual void printName(std::ostream &os) const
Print name of context.
Definition: RooAbsGenContext.cxx:282
RooFit.h
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
TString::Prepend
TString & Prepend(const char *cs)
Definition: TString.h:661
TString::Data
const char * Data() const
Definition: TString.h:369
RooArgSet::isInRange
Bool_t isInRange(const char *rangeSpec)
Definition: RooArgSet.cxx:962
RooPrintable::kStandard
@ kStandard
Definition: RooPrintable.h:34
RooAbsGenContext::_theEvent
RooArgSet * _theEvent
Definition: RooAbsGenContext.h:78
RooAbsGenContext::_normRange
TString _normRange
Definition: RooAbsGenContext.h:86
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
RooFit::EventRange
RooCmdArg EventRange(Int_t nStart, Int_t nStop)
Definition: RooGlobalFunc.cxx:180
coutE
#define coutE(a)
Definition: RooMsgService.h:33
output
static void output(int code)
Definition: gifencode.c:226
RooPrintable::StyleOption
StyleOption
Definition: RooPrintable.h:34
RooAbsGenContext::_isValid
Bool_t _isValid
Definition: RooAbsGenContext.h:79
RooAbsGenContext::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsGenContext.h:50
RooAbsGenContext::generate
virtual RooDataSet * generate(Double_t nEvents=0, Bool_t skipInit=kFALSE, Bool_t extendedMode=kFALSE)
Generate the specified number of events with nEvents>0 and and return a dataset containing the genera...
Definition: RooAbsGenContext.cxx:152
Int_t
int Int_t
Definition: RtypesCore.h:45
RooAbsGenContext.h
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
coutI
#define coutI(a)
Definition: RooMsgService.h:30
TClass.h
RooArgSet::add
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
RooAbsGenContext::resampleData
void resampleData(Double_t &ratio)
Rescale existing output buffer with given ratio.
Definition: RooAbsGenContext.cxx:373
RooDataSet::addFast
virtual void addFast(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the 'data' argset, to the data set.
Definition: RooDataSet.cxx:1266
RooAbsGenContext::RooAbsGenContext
RooAbsGenContext(const RooAbsPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
Constructor.
Definition: RooAbsGenContext.cxx:53
RooAbsGenContext::_expectedEvents
UInt_t _expectedEvents
Definition: RooAbsGenContext.h:81
TString
Definition: TString.h:136
RooAbsGenContext::~RooAbsGenContext
virtual ~RooAbsGenContext()
Destructor.
Definition: RooAbsGenContext.cxx:113
RooDataSet.h
bool
TIterator
Definition: TIterator.h:30
RooDataSet::get
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:1038
RooAbsGenContext::setProtoDataOrder
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order of prototype data to that in the lookup tables passed as argument.
Definition: RooAbsGenContext.cxx:348
RooAbsGenContext::initGenerator
virtual void initGenerator(const RooArgSet &theEvent)
Interface function to initialize context for generation for given set of observables.
Definition: RooAbsGenContext.cxx:273
RooAbsGenContext::printMultiline
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
Definition: RooAbsGenContext.cxx:335
RooArgSet::snapshot
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:134
RooAbsGenContext::_extendMode
RooAbsPdf::ExtendMode _extendMode
Definition: RooAbsGenContext.h:84
RooAbsCollection::contains
Bool_t contains(const RooAbsArg &var) const
Check if collection contains an argument with the same name as var.
Definition: RooAbsCollection.h:102
RooPrintable::kName
@ kName
Definition: RooPrintable.h:33
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
RooAbsPdf::CanNotBeExtended
@ CanNotBeExtended
Definition: RooAbsPdf.h:229
RooAbsGenContext::_verbose
Bool_t _verbose
Definition: RooAbsGenContext.h:80
RooAbsPdf.h
RooAbsGenContext::_nextProtoIndex
Int_t _nextProtoIndex
Definition: RooAbsGenContext.h:83
RooAbsGenContext::isValid
Bool_t isValid() const
Definition: RooAbsGenContext.h:34
RooAbsGenContext::defaultPrintContents
virtual Int_t defaultPrintContents(Option_t *opt) const
Define default contents when printing.
Definition: RooAbsGenContext.cxx:402
TNamed
Definition: TNamed.h:29
RooFit::Generation
@ Generation
Definition: RooGlobalFunc.h:67
RooAbsData::setDirtyProp
void setDirtyProp(Bool_t flag)
Control propagation of dirty flags from observables in dataset.
Definition: RooAbsData.cxx:362
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsGenContext::defaultPrintStyle
virtual StyleOption defaultPrintStyle(Option_t *opt) const
Define default print style.
Definition: RooAbsGenContext.cxx:412
RooRandom.h
RooAbsGenContext::_protoOrder
Int_t * _protoOrder
Definition: RooAbsGenContext.h:85
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:118
RooAbsGenContext
Definition: RooAbsGenContext.h:26
RooAbsData::numEntries
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Definition: RooAbsData.cxx:307
RooPrintable::kValue
@ kValue
Definition: RooPrintable.h:33
RooAbsGenContext::createDataSet
virtual RooDataSet * createDataSet(const char *name, const char *title, const RooArgSet &obs)
Create an empty dataset to hold the events that will be generated.
Definition: RooAbsGenContext.cxx:133
RooAbsGenContext::_protoVars
RooArgSet _protoVars
Definition: RooAbsGenContext.h:82
TIterator::Next
virtual TObject * Next()=0
RooGlobalFunc.h
proto
const char * proto
Definition: civetweb.c:16604
RooPrintable::kClassName
@ kClassName
Definition: RooPrintable.h:33
Double_t
double Double_t
Definition: RtypesCore.h:59
TRandom::Poisson
virtual Int_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition: TRandom.cxx:391
RooAbsData::reduce
RooAbsData * reduce(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg())
Create a reduced copy of this dataset.
Definition: RooAbsData.cxx:382
RooAbsGenContext::printArgs
virtual void printArgs(std::ostream &os) const
Print arguments of context, i.e. the observables being generated in this context.
Definition: RooAbsGenContext.cxx:312
name
char name[80]
Definition: TGX11.cxx:110
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooDataSet
Definition: RooDataSet.h:33
RooAbsGenContext::_genData
RooDataSet * _genData
Definition: RooAbsGenContext.h:88
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooAbsArg
Definition: RooAbsArg.h:73
RooRandom::randomGenerator
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:53
RooArgSet::addClone
virtual void addClone(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:96
RooAbsPdf
Definition: RooAbsPdf.h:40
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
Riostream.h
RooAbsGenContext::_prototype
const RooDataSet * _prototype
Definition: RooAbsGenContext.h:77
TObject::ClassName
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
cxcoutI
#define cxcoutI(a)
Definition: RooMsgService.h:85
RooAbsGenContext::attach
virtual void attach(const RooArgSet &params)
Interface to attach given parameters to object in this context.
Definition: RooAbsGenContext.cxx:124
RooAbsGenContext::generateEvent
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)=0
RooAbsGenContext::printClassName
virtual void printClassName(std::ostream &os) const
Print class name of context.
Definition: RooAbsGenContext.cxx:302
RooAbsGenContext::printTitle
virtual void printTitle(std::ostream &os) const
Print title of context.
Definition: RooAbsGenContext.cxx:292
RooArgSet
Definition: RooArgSet.h:28
cxcoutD
#define cxcoutD(a)
Definition: RooMsgService.h:81
int