Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22RooAbsGenContext is the abstract base class for generator contexts of
23RooAbsPdf objects. A generator context is an object that controls
24the generation of events from a given p.d.f in one or more sessions.
25This class defines the common interface for all such contexts and organizes
26storage of common components, such as the observables definition, the
27prototype 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
44using 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 _isValid(kTRUE),
58 _verbose(verbose),
59 _protoOrder(0),
60 _genData(0)
61{
62 // Check PDF dependents
63 if (model.recursiveCheckObservables(&vars)) {
64 coutE(Generation) << "RooAbsGenContext::ctor: Error in PDF dependents" << endl ;
66 return ;
67 }
68
69 // Make a snapshot of the generated variables that we can overwrite.
70 vars.snapshot(_theEvent, false);
71
72 // Analyze the prototype dataset, if one is specified
74 if(0 != _prototype) {
75 TIterator *protoIterator= _prototype->get()->createIterator();
76 const RooAbsArg *proto = 0;
77 while((proto= (const RooAbsArg*)protoIterator->Next())) {
78 // is this variable being generated or taken from the prototype?
79 if(!_theEvent.contains(*proto)) {
82 }
83 }
84 delete protoIterator;
85 }
86
87 // Add auxiliary protovars to _protoVars, if provided
88 if (auxProto) {
89 _protoVars.add(*auxProto) ;
90 _theEvent.addClone(*auxProto);
91 }
92
93 // Remember the default number of events to generate when no prototype dataset is provided.
94 _extendMode = model.extendMode() ;
95 if (model.canBeExtended()) {
97 } else {
99 }
100
101 // Save normalization range
102 if (model.normRange()) {
103 _normRange = model.normRange() ;
104 }
105}
106
107
108
109////////////////////////////////////////////////////////////////////////////////
110/// Destructor
111
113{
114 if (_protoOrder) delete[] _protoOrder ;
115}
116
117
118
119////////////////////////////////////////////////////////////////////////////////
120/// Interface to attach given parameters to object in this context
121
122void RooAbsGenContext::attach(const RooArgSet& /*params*/)
123{
124}
125
126
127
128////////////////////////////////////////////////////////////////////////////////
129/// Create an empty dataset to hold the events that will be generated
130
131RooDataSet* RooAbsGenContext::createDataSet(const char* name, const char* title, const RooArgSet& obs)
132{
133 RooDataSet* ret = new RooDataSet(name, title, obs);
134 ret->setDirtyProp(kFALSE) ;
135 return ret ;
136}
137
138
139////////////////////////////////////////////////////////////////////////////////
140/// Generate the specified number of events with nEvents>0 and
141/// and return a dataset containing the generated events. With nEvents<=0,
142/// generate the number of events in the prototype dataset, if available,
143/// or else the expected number of events, if non-zero.
144/// If extendedMode = true generate according to a Poisson(nEvents)
145/// The returned dataset belongs to the caller. Return zero in case of an error.
146/// Generation of individual events is delegated to a virtual generateEvent()
147/// method. A virtual initGenerator() method is also called just before the
148/// first call to generateEvent().
149
151{
152 if(!isValid()) {
153 coutE(Generation) << ClassName() << "::" << GetName() << ": context is not valid" << endl;
154 return 0;
155 }
156
157 // Calculate the expected number of events if necessary
158 if(nEvents <= 0) {
159 if(_prototype) {
160 nEvents= (Int_t)_prototype->numEntries();
161 }
162 else {
164 coutE(Generation) << ClassName() << "::" << GetName()
165 << ":generate: PDF not extendable: cannot calculate expected number of events" << endl;
166 return 0;
167 }
168 nEvents= _expectedEvents;
169 }
170 if(nEvents <= 0) {
171 coutE(Generation) << ClassName() << "::" << GetName()
172 << ":generate: cannot calculate expected number of events" << endl;
173 return 0;
174 }
175 coutI(Generation) << ClassName() << "::" << GetName() << ":generate: will generate "
176 << nEvents << " events" << endl;
177
178 }
179
180 if (extendedMode) {
181 double nExpEvents = nEvents;
182 nEvents = RooRandom::randomGenerator()->Poisson(nEvents) ;
183 cxcoutI(Generation) << " Extended mode active, number of events generated (" << nEvents << ") is Poisson fluctuation on "
184 << GetName() << "::expectedEvents() = " << nExpEvents << endl ;
185 }
186
187 // check that any prototype dataset still defines the variables we need
188 // (this is necessary since we never make a private clone, for efficiency)
189 if(_prototype) {
190 const RooArgSet *vars= _prototype->get();
192 const RooAbsArg *arg = 0;
193 Bool_t ok(kTRUE);
194 while((arg= (const RooAbsArg*)iterator->Next())) {
195 if(vars->contains(*arg)) continue;
196 coutE(InputArguments) << ClassName() << "::" << GetName() << ":generate: prototype dataset is missing \""
197 << arg->GetName() << "\"" << endl;
198
199 // WVE disable this for the moment
200 // ok= kFALSE;
201 }
202 delete iterator;
203 // coverity[DEADCODE]
204 if(!ok) return 0;
205 }
206
207 if (_verbose) Print("v") ;
208
209 // create a new dataset
210 TString name(GetName()),title(GetTitle());
211 name.Append("Data");
212 title.Prepend("Generated From ");
213
214 // WVE need specialization here for simultaneous pdfs
215 _genData = createDataSet(name.Data(), title.Data(), _theEvent);
216
217 // Perform any subclass implementation-specific initialization
218 // Can be skipped if this is a rerun with an identical configuration
219 if (!skipInit) {
221 }
222
223 // Loop over the events to generate
224 Int_t evt(0) ;
225 while(_genData->numEntries()<nEvents) {
226
227 // first, load values from the prototype dataset, if one was provided
228 if(0 != _prototype) {
230
232
233 const RooArgSet *subEvent= _prototype->get(actualProtoIdx);
235 if(0 != subEvent) {
236 _theEvent.assign(*subEvent);
237 }
238 else {
239 coutE(Generation) << ClassName() << "::" << GetName() << ":generate: cannot load event "
240 << actualProtoIdx << " from prototype dataset" << endl;
241 return 0;
242 }
243 }
244
245 // delegate the generation of the rest of this event to our subclass implementation
247
248
249 // WVE add check that event is in normRange
251 continue ;
252 }
253
255 evt++ ;
256 }
257
259 _genData = 0 ;
261
262 return output;
263}
264
265
266
267////////////////////////////////////////////////////////////////////////////////
268/// Interface function to initialize context for generation for given
269/// set of observables
270
272{
273}
274
275
276
277////////////////////////////////////////////////////////////////////////////////
278/// Print name of context
279
280void RooAbsGenContext::printName(ostream& os) const
281{
282 os << GetName() ;
283}
284
285
286
287////////////////////////////////////////////////////////////////////////////////
288/// Print title of context
289
290void RooAbsGenContext::printTitle(ostream& os) const
291{
292 os << GetTitle() ;
293}
294
295
296
297////////////////////////////////////////////////////////////////////////////////
298/// Print class name of context
299
300void RooAbsGenContext::printClassName(ostream& os) const
301{
302 os << IsA()->GetName() ;
303}
304
305
306
307////////////////////////////////////////////////////////////////////////////////
308/// Print arguments of context, i.e. the observables being generated in this context
309
310void RooAbsGenContext::printArgs(ostream& os) const
311{
312 os << "[ " ;
314 RooAbsArg* arg ;
316 while((arg=(RooAbsArg*)iter->Next())) {
317 if (first) {
318 first=kFALSE ;
319 } else {
320 os << "," ;
321 }
322 os << arg->GetName() ;
323 }
324 os << "]" ;
325 delete iter ;
326}
327
328
329
330////////////////////////////////////////////////////////////////////////////////
331/// Interface for multi-line printing
332
333void RooAbsGenContext::printMultiline(ostream &/*os*/, Int_t /*contents*/, Bool_t /*verbose*/, TString /*indent*/) const
334{
335}
336
337
338
339
340////////////////////////////////////////////////////////////////////////////////
341/// Set the traversal order of prototype data to that in the lookup tables
342/// passed as argument. The LUT must be an array of integers with the same
343/// size as the number of entries in the prototype dataset and must contain
344/// integer values in the range [0,Nevt-1]
345
347{
348 // Delete any previous lookup table
349 if (_protoOrder) {
350 delete[] _protoOrder ;
351 _protoOrder = 0 ;
352 }
353
354 // Copy new lookup table if provided and needed
355 if (lut && _prototype) {
357 _protoOrder = new Int_t[n] ;
358 Int_t i ;
359 for (i=0 ; i<n ; i++) {
360 _protoOrder[i] = lut[i] ;
361 }
362 }
363}
364
365
366
367
368////////////////////////////////////////////////////////////////////////////////
369/// Rescale existing output buffer with given ratio
370
372{
373
374 Int_t nOrig = _genData->numEntries() ;
375 Int_t nTarg = Int_t(nOrig*ratio+0.5) ;
376 RooDataSet* trimmedData = (RooDataSet*) _genData->reduce(RooFit::EventRange(0,nTarg)) ;
377
378 cxcoutD(Generation) << "RooGenContext::resampleData*( existing production trimmed from " << nOrig << " to " << trimmedData->numEntries() << " events" << endl ;
379
380 delete _genData ;
381 _genData = trimmedData ;
382
383 if (_prototype) {
384 // Push back proto index by trimmed amount to force recycling of the
385 // proto entries that were trimmed away
386 _nextProtoIndex -= (nOrig-nTarg) ;
387 while (_nextProtoIndex<0) {
389 }
390 }
391
392}
393
394
395
396
397////////////////////////////////////////////////////////////////////////////////
398/// Define default contents when printing
399
401{
402 return kName|kClassName|kValue ;
403}
404
405
406
407////////////////////////////////////////////////////////////////////////////////
408/// Define default print style
409
411{
412 if (opt && TString(opt).Contains("v")) {
413 return kVerbose ;
414 }
415 return kStandard ;
416}
#define coutI(a)
#define cxcoutI(a)
#define cxcoutD(a)
#define coutE(a)
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
const char * proto
Definition civetweb.c:16613
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
Bool_t recursiveCheckObservables(const RooArgSet *nset) const
Recursively call checkObservables on all nodes in the expression tree.
Bool_t contains(const RooAbsArg &var) const
Check if collection contains an argument with the same name as var.
virtual RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE)
Add a clone of the specified argument to list.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
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.
void setDirtyProp(Bool_t flag)
Control propagation of dirty flags from observables in dataset.
RooAbsGenContext is the abstract base class for generator contexts of RooAbsPdf objects.
virtual RooDataSet * createDataSet(const char *name, const char *title, const RooArgSet &obs)
Create an empty dataset to hold the events that will be generated.
RooAbsPdf::ExtendMode _extendMode
virtual StyleOption defaultPrintStyle(Option_t *opt) const
Define default print style.
virtual Int_t defaultPrintContents(Option_t *opt) const
Define default contents when printing.
Bool_t isValid() const
virtual void printTitle(std::ostream &os) const
Print title of context.
virtual void attach(const RooArgSet &params)
Interface to attach given parameters to object in this context.
RooDataSet * _genData
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
virtual void printName(std::ostream &os) const
Print name of context.
virtual void initGenerator(const RooArgSet &theEvent)
Interface function to initialize context for generation for given set of observables.
const RooDataSet * _prototype
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
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...
RooAbsGenContext(const RooAbsPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
Constructor.
void resampleData(Double_t &ratio)
Rescale existing output buffer with given ratio.
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)=0
virtual void printArgs(std::ostream &os) const
Print arguments of context, i.e. the observables being generated in this context.
virtual void printClassName(std::ostream &os) const
Print class name of context.
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order of prototype data to that in the lookup tables passed as argument.
virtual ~RooAbsGenContext()
Destructor.
Bool_t canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:262
@ CanNotBeExtended
Definition RooAbsPdf.h:256
const char * normRange() const
Definition RooAbsPdf.h:292
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
virtual ExtendMode extendMode() const
Returns ability of PDF to provide extended likelihood terms.
Definition RooAbsPdf.h:260
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:158
Bool_t isInRange(const char *rangeSpec)
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:36
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
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.
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:53
Iterator abstract base class.
Definition TIterator.h:30
virtual TObject * Next()=0
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:200
virtual Int_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition TRandom.cxx:402
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
const char * Data() const
Definition TString.h:369
TString & Prepend(const char *cs)
Definition TString.h:661
RooCmdArg EventRange(Int_t nStart, Int_t nStop)
const Int_t n
Definition legend1.C:16
Definition first.py:1
static void output(int code)
Definition gifencode.c:226