ROOT  6.06/09
Reference Guide
RooBinnedGenContext.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 //
19 // BEGIN_HTML
20 // RooBinnedGenContext is an efficient implementation of the
21 // generator context specific for binned pdfs
22 // END_HTML
23 //
24 
25 
26 #include "RooFit.h"
27 
28 #include "Riostream.h"
29 
30 
31 #include "RooMsgService.h"
32 #include "RooBinnedGenContext.h"
33 #include "RooAbsPdf.h"
34 #include "RooRealVar.h"
35 #include "RooDataHist.h"
36 #include "RooDataSet.h"
37 #include "RooRandom.h"
38 
39 using namespace std;
40 
42 ;
43 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Constructor
47 
49  const RooDataSet *prototype, const RooArgSet* auxProto,
50  Bool_t verbose) :
51  RooAbsGenContext(model,vars,prototype,auxProto,verbose)
52 {
53  cxcoutI(Generation) << "RooBinnedGenContext::ctor() setting up event special generator context for sum p.d.f. " << model.GetName()
54  << " for generation of observable(s) " << vars ;
55  if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
56  if (auxProto && auxProto->getSize()>0) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
57  ccxcoutI(Generation) << endl ;
58 
59  // Constructor. Build an array of generator contexts for each product component PDF
61  _pdf = (RooAbsPdf*) _pdfSet->find(model.GetName()) ;
63 
64  // Fix normalization set of this RooAddPdf
65  if (prototype)
66  {
67  RooArgSet coefNSet(vars) ;
68  coefNSet.add(*prototype->get()) ;
69  _pdf->fixAddCoefNormalization(coefNSet) ;
70  }
71 
73  _vars = _pdf->getObservables(vars) ;
74 
75  // If pdf has boundary definitions, follow those for the binning
76  RooFIter viter = _vars->fwdIterator() ;
77  RooAbsArg* var ;
78  while((var=viter.next())) {
79  RooRealVar* rvar = dynamic_cast<RooRealVar*>(var) ;
80  if (rvar) {
81  list<Double_t>* binb = model.binBoundaries(*rvar,rvar->getMin(),rvar->getMax()) ;
82  delete binb ;
83  }
84  }
85 
86 
87  // Create empty RooDataHist
88  _hist = new RooDataHist("genData","genData",*_vars) ;
89 
91 }
92 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Destructor. Delete all owned subgenerator contexts
96 
98 {
99  delete _vars ;
100  delete _pdfSet ;
101  delete _hist ;
102 }
103 
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Attach given set of variables to internal p.d.f. clone
108 
110 {
112 }
113 
114 
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// One-time initialization of generator contex. Attach theEvent
118 /// to internal p.d.f clone and forward initialization call to
119 /// the component generators
120 
122 {
123  _pdf->recursiveRedirectServers(theEvent) ;
124 
125 }
126 
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 
131 {
132  _expectedData = flag ;
133 }
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 
139 {
140  // Scale to number of events and introduce Poisson fluctuations
141  _hist->reset() ;
142 
143  Double_t nEvents = nEvt ;
144 
145  if (nEvents<=0) {
146  if (!_pdf->canBeExtended()) {
147  coutE(InputArguments) << "RooAbsPdf::generateBinned(" << GetName()
148  << ") ERROR: No event count provided and p.d.f does not provide expected number of events" << endl ;
149  return 0 ;
150  } else {
151  // Don't round in expectedData mode
152  if (_expectedData || extended) {
153  nEvents = _pdf->expectedEvents(_vars) ;
154  } else {
155  nEvents = Int_t(_pdf->expectedEvents(_vars)+0.5) ;
156  }
157  }
158  }
159 
160  // Sample p.d.f. distribution
162 
163  // Output container
164  RooRealVar weight("weight","weight",0,1e9) ;
165  RooArgSet tmp(*_vars) ;
166  tmp.add(weight) ;
167  RooDataSet* wudata = new RooDataSet("wu","wu",tmp,RooFit::WeightVar("weight")) ;
168 
169  vector<int> histOut(_hist->numEntries()) ;
170  Double_t histMax(-1) ;
171  Int_t histOutSum(0) ;
172  for (int i=0 ; i<_hist->numEntries() ; i++) {
173  _hist->get(i) ;
174  if (_expectedData) {
175 
176  // Expected data, multiply p.d.f by nEvents
178  wudata->add(*_hist->get(),w) ;
179 
180  } else if (extended) {
181 
182  // Extended mode, set contents to Poisson(pdf*nEvents)
184  wudata->add(*_hist->get(),w) ;
185 
186  } else {
187 
188  // Regular mode, fill array of weights with Poisson(pdf*nEvents), but to not fill
189  // histogram yet.
190  if (_hist->weight()>histMax) {
191  histMax = _hist->weight() ;
192  }
194  histOutSum += histOut[i] ;
195  }
196  }
197 
198 
199  if (!_expectedData && !extended) {
200 
201  // Second pass for regular mode - Trim/Extend dataset to exact number of entries
202 
203  // Calculate difference between what is generated so far and what is requested
204  Int_t nEvtExtra = abs(Int_t(nEvents)-histOutSum) ;
205  Int_t wgt = (histOutSum>nEvents) ? -1 : 1 ;
206 
207  // Perform simple binned accept/reject procedure to get to exact event count
208  while(nEvtExtra>0) {
209 
211  _hist->get(ibinRand) ;
212  Double_t ranY = RooRandom::randomGenerator()->Uniform(histMax) ;
213 
214  if (ranY<_hist->weight()) {
215  if (wgt==1) {
216  histOut[ibinRand]++ ;
217  } else {
218  // If weight is negative, prior bin content must be at least 1
219  if (histOut[ibinRand]>0) {
220  histOut[ibinRand]-- ;
221  } else {
222  continue ;
223  }
224  }
225  nEvtExtra-- ;
226  }
227  }
228 
229  // Transfer working array to histogram
230  for (int i=0 ; i<_hist->numEntries() ; i++) {
231  _hist->get(i) ;
232  wudata->add(*_hist->get(),histOut[i]) ;
233  }
234 
235  }
236 
237  return wudata ;
238 
239 }
240 
241 
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// this method is not implemented for this context
245 
247 {
248  assert(0) ;
249 }
250 
251 
252 
253 ////////////////////////////////////////////////////////////////////////////////
254 /// Print the details of the context
255 
257 {
258  RooAbsGenContext::printMultiline(os,content,verbose,indent) ;
259  os << indent << "--- RooBinnedGenContext ---" << endl ;
260  os << indent << "Using PDF ";
262 }
#define coutE(a)
Definition: RooMsgService.h:35
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:278
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
#define cxcoutI(a)
Definition: RooMsgService.h:84
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer, which is interpreted as an OR of 'enum ContentsOptions' values and in the style given by 'enum StyleOption'.
RooFIter fwdIterator() const
#define assert(cond)
Definition: unittest.h:542
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print the details of the context.
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Definition: RooAbsArg.h:194
Basic string class.
Definition: TString.h:137
virtual Double_t getMin(const char *name=0) const
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events from this p.d.f for use in extended likelihood calculations.
Definition: RooAbsPdf.cxx:2930
virtual ~RooBinnedGenContext()
Destructor. Delete all owned subgenerator contexts.
virtual const RooArgSet * get() const
Definition: RooDataHist.h:77
virtual void attach(const RooArgSet &params)
Attach given set of variables to internal p.d.f. clone.
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:320
static Vc_ALWAYS_INLINE Vector< T > abs(const Vector< T > &x)
Definition: vector.h:450
if on multiple lines(like in C++).**The" * configuration fragment. * * The "import myobject continue
Parses the configuration file.
Definition: HLFactory.cxx:368
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:53
const int nEvents
Definition: testRooFit.cxx:42
virtual void initGenerator(const RooArgSet &theEvent)
One-time initialization of generator contex.
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual Double_t weight() const
Definition: RooDataHist.h:96
bool verbose
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static void indent(ostringstream &buf, int indent_level)
virtual void add(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...
RooAbsArg * next()
RooArgSet * _theEvent
double Double_t
Definition: RtypesCore.h:55
Bool_t canBeExtended() const
Definition: RooAbsPdf.h:216
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
RooCmdArg WeightVar(const char *name, Bool_t reinterpretAsWeight=kFALSE)
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
this method is not implemented for this context
RooDataHist * fillDataHist(RooDataHist *hist, const RooArgSet *nset, Double_t scaleFactor, Bool_t correctForBinVolume=kFALSE, Bool_t showProgress=kFALSE) const
Fill a RooDataHist with values sampled from this function at the bin centers.
const RooArgSet * _vars
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual Int_t numEntries() const
Return the number of bins.
virtual void setExpectedData(Bool_t)
virtual Double_t getMax(const char *name=0) const
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
#define ccxcoutI(a)
Definition: RooMsgService.h:85
virtual void reset()
Reset all bin weights to zero.
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Change cache operation mode to given mode.
Definition: RooAbsArg.cxx:1753
ClassImp(RooBinnedGenContext)
RooBinnedGenContext(const RooAbsPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
Constructor.
virtual void fixAddCoefNormalization(const RooArgSet &addNormSet=RooArgSet(), Bool_t force=kTRUE)
Fix the interpretation of the coefficient of any RooAddPdf component in the expression tree headed by...
virtual Int_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition: TRandom.cxx:362
Int_t getSize() const
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1087
const Bool_t kTRUE
Definition: Rtypes.h:91
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...
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448