Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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\file RooBinnedGenContext.cxx
19\class RooBinnedGenContext
20\ingroup Roofitcore
21
22Efficient implementation of the generator context specific for binned pdfs.
23**/
24
25#include "Riostream.h"
26
27
28#include "RooMsgService.h"
29#include "RooBinnedGenContext.h"
30#include "RooAbsPdf.h"
31#include "RooRealVar.h"
32#include "RooDataHist.h"
33#include "RooDataSet.h"
34#include "RooRandom.h"
35
36using std::endl, std::vector, std::ostream;
37
39
40
41////////////////////////////////////////////////////////////////////////////////
42/// Constructor
43
45 const RooDataSet *prototype, const RooArgSet* auxProto,
46 bool verbose) :
47 RooAbsGenContext(model,vars,prototype,auxProto,verbose)
48{
49 cxcoutI(Generation) << "RooBinnedGenContext::ctor() setting up event special generator context for sum p.d.f. " << model.GetName()
50 << " for generation of observable(s) " << vars ;
51 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
52 if (auxProto && !auxProto->empty()) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
53 ccxcoutI(Generation) << endl ;
54
55 // Constructor. Build an array of generator contexts for each product component PDF
56 RooArgSet(model).snapshot(_pdfSet, true);
57 _pdf = static_cast<RooAbsPdf*>(_pdfSet.find(model.GetName())) ;
59
60 // Fix normalization set of this RooAddPdf
61 if (prototype)
62 {
63 RooArgSet coefNSet(vars) ;
64 coefNSet.add(*prototype->get()) ;
65 _pdf->fixAddCoefNormalization(coefNSet) ;
66 }
67
69 _vars = std::unique_ptr<RooArgSet>{_pdf->getObservables(vars)};
70
71 // Create empty RooDataHist
72 _hist = std::make_unique<RooDataHist>("genData","genData",*_vars);
73
74 _expectedData = false ;
75}
76
77
79
80
81////////////////////////////////////////////////////////////////////////////////
82/// Attach given set of variables to internal p.d.f. clone
83
85{
87}
88
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// One-time initialization of generator context. Attach theEvent
93/// to internal p.d.f clone and forward initialization call to
94/// the component generators.
95
97{
99
100}
101
102
103////////////////////////////////////////////////////////////////////////////////
104
106{
107 _expectedData = flag ;
108}
109
110
111////////////////////////////////////////////////////////////////////////////////
112
113RooDataSet *RooBinnedGenContext::generate(double nEvt, bool /*skipInit*/, bool extended)
114{
115 // Scale to number of events and introduce Poisson fluctuations
116 _hist->reset() ;
117
118 double nEvents = nEvt ;
119
120 if (nEvents<=0) {
121 if (!_pdf->canBeExtended()) {
122 coutE(InputArguments) << "RooAbsPdf::generateBinned(" << GetName()
123 << ") ERROR: No event count provided and p.d.f does not provide expected number of events" << endl ;
124 return nullptr ;
125 } else {
126 // Don't round in expectedData mode
127 if (_expectedData || extended) {
128 nEvents = _pdf->expectedEvents(_vars.get());
129 } else {
130 nEvents = Int_t(_pdf->expectedEvents(_vars.get())+0.5) ;
131 }
132 }
133 }
134
135 // Sample p.d.f. distribution
136 _pdf->fillDataHist(_hist.get(),_vars.get(),1,true) ;
137
138 // Output container
139 RooDataSet* wudata = new RooDataSet("wu","wu",*_vars,RooFit::WeightVar()) ;
140
141 vector<int> histOut(_hist->numEntries()) ;
142 double histMax(-1) ;
143 Int_t histOutSum(0) ;
144 for (int i=0 ; i<_hist->numEntries() ; i++) {
145 _hist->get(i) ;
146 if (_expectedData) {
147
148 // Expected data, multiply p.d.f by nEvents
149 double w=_hist->weight()*nEvents ;
150 wudata->add(*_hist->get(),w) ;
151
152 } else if (extended) {
153
154 // Extended mode, set contents to Poisson(pdf*nEvents)
155 double w = RooRandom::randomGenerator()->Poisson(_hist->weight()*nEvents) ;
156 wudata->add(*_hist->get(),w) ;
157
158 } else {
159
160 // Regular mode, fill array of weights with Poisson(pdf*nEvents), but to not fill
161 // histogram yet.
162 if (_hist->weight()>histMax) {
163 histMax = _hist->weight() ;
164 }
165 histOut[i] = RooRandom::randomGenerator()->Poisson(_hist->weight()*nEvents) ;
166 histOutSum += histOut[i] ;
167 }
168 }
169
170
171 if (!_expectedData && !extended) {
172
173 // Second pass for regular mode - Trim/Extend dataset to exact number of entries
174
175 // Calculate difference between what is generated so far and what is requested
176 Int_t nEvtExtra = std::abs(Int_t(nEvents)-histOutSum) ;
177 Int_t wgt = (histOutSum>nEvents) ? -1 : 1 ;
178
179 // Perform simple binned accept/reject procedure to get to exact event count
180 while(nEvtExtra>0) {
181
182 Int_t ibinRand = RooRandom::randomGenerator()->Integer(_hist->numEntries()) ;
183 _hist->get(ibinRand) ;
184 double ranY = RooRandom::randomGenerator()->Uniform(histMax) ;
185
186 if (ranY<_hist->weight()) {
187 if (wgt==1) {
188 histOut[ibinRand]++ ;
189 } else {
190 // If weight is negative, prior bin content must be at least 1
191 if (histOut[ibinRand]>0) {
192 histOut[ibinRand]-- ;
193 } else {
194 continue ;
195 }
196 }
197 nEvtExtra-- ;
198 }
199 }
200
201 // Transfer working array to histogram
202 for (int i=0 ; i<_hist->numEntries() ; i++) {
203 _hist->get(i) ;
204 wudata->add(*_hist->get(),histOut[i]) ;
205 }
206
207 }
208
209 return wudata ;
210
211}
212
213
214
215////////////////////////////////////////////////////////////////////////////////
216/// this method is not implemented for this context
217
219{
220 assert(0) ;
221}
222
223
224
225////////////////////////////////////////////////////////////////////////////////
226/// Print the details of the context
227
228void RooBinnedGenContext::printMultiline(ostream &os, Int_t content, bool verbose, TString indent) const
229{
230 RooAbsGenContext::printMultiline(os,content,verbose,indent) ;
231 os << indent << "--- RooBinnedGenContext ---" << endl ;
232 os << indent << "Using PDF ";
234}
#define cxcoutI(a)
#define coutE(a)
#define ccxcoutI(a)
int Int_t
Definition RtypesCore.h:45
#define ClassImp(name)
Definition Rtypes.h:377
static void indent(ostringstream &buf, int indent_level)
bool recursiveRedirectServers(const RooAbsCollection &newServerList, bool mustReplaceAll=false, bool nameChange=false, bool recurseInNewSet=true)
Recursively replace all servers with the new servers in newSet.
void setOperMode(OperMode mode, bool recurseADirty=true)
Set the operation mode of this node.
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual void reset()
Abstract base class for generator contexts of RooAbsPdf objects.
RooArgSet _theEvent
Pointer to observable event being generated.
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Interface for multi-line printing.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
virtual double expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:219
RooDataHist * fillDataHist(RooDataHist *hist, const RooArgSet *nset, double scaleFactor, bool correctForBinVolume=false, bool showProgress=false) const
Fill a RooDataHist with values sampled from this function at the bin centers.
virtual void fixAddCoefNormalization(const RooArgSet &addNormSet=RooArgSet(), bool force=true)
Fix the interpretation of the coefficient of any RooAddPdf component in the expression tree headed by...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
Efficient implementation of the generator context specific for binned pdfs.
RooAbsPdf * _pdf
Pointer to cloned p.d.f.
~RooBinnedGenContext() override
void generateEvent(RooArgSet &theEvent, Int_t remaining) override
this method is not implemented for this context
std::unique_ptr< RooDataHist > _hist
Histogram.
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Print the details of the context.
RooArgSet _pdfSet
Set owned all nodes of internal clone of p.d.f.
RooBinnedGenContext(const RooAbsPdf &model, const RooArgSet &vars, const RooDataSet *prototype=nullptr, const RooArgSet *auxProto=nullptr, bool _verbose=false)
Constructor.
void setExpectedData(bool) override
std::unique_ptr< RooArgSet > _vars
void initGenerator(const RooArgSet &theEvent) override
One-time initialization of generator context.
RooDataSet * generate(double nEvents=0.0, bool skipInit=false, bool extendedMode=false) override
Generate the specified number of events with nEvents>0 and and return a dataset containing the genera...
void attach(const RooArgSet &params) override
Attach given set of variables to internal p.d.f. clone.
Container class to hold unbinned data.
Definition RooDataSet.h:57
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
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,...
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:48
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual ULong64_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition TRandom.cxx:404
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:682
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
Basic string class.
Definition TString.h:139
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)