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
38
39
40////////////////////////////////////////////////////////////////////////////////
41/// Constructor
42
45 bool verbose) :
46 RooAbsGenContext(model,vars,prototype,auxProto,verbose)
47{
48 cxcoutI(Generation) << "RooBinnedGenContext::ctor() setting up event special generator context for sum p.d.f. " << model.GetName()
49 << " for generation of observable(s) " << vars ;
50 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
51 if (auxProto && !auxProto->empty()) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
52 ccxcoutI(Generation) << std::endl ;
53
54 // Constructor. Build an array of generator contexts for each product component PDF
55 RooArgSet(model).snapshot(_pdfSet, true);
56 _pdf = static_cast<RooAbsPdf*>(_pdfSet.find(model.GetName())) ;
58
59 // Fix normalization set of this RooAddPdf
60 if (prototype)
61 {
62 RooArgSet coefNSet(vars) ;
63 coefNSet.add(*prototype->get()) ;
65 }
66
68 _vars = std::unique_ptr<RooArgSet>{_pdf->getObservables(vars)};
69
70 // Create empty RooDataHist
71 _hist = std::make_unique<RooDataHist>("genData","genData",*_vars);
72
74}
75
76
78
79
80////////////////////////////////////////////////////////////////////////////////
81/// Attach given set of variables to internal p.d.f. clone
82
87
88
89
90////////////////////////////////////////////////////////////////////////////////
91/// One-time initialization of generator context. Attach theEvent
92/// to internal p.d.f clone and forward initialization call to
93/// the component generators.
94
100
101
102////////////////////////////////////////////////////////////////////////////////
103
108
109
110////////////////////////////////////////////////////////////////////////////////
111
112RooDataSet *RooBinnedGenContext::generate(double nEvt, bool /*skipInit*/, bool extended)
113{
114 // Scale to number of events and introduce Poisson fluctuations
115 _hist->reset() ;
116
117 double nEvents = nEvt ;
118
119 if (nEvents<=0) {
120 if (!_pdf->canBeExtended()) {
121 coutE(InputArguments) << "RooAbsPdf::generateBinned(" << GetName()
122 << ") ERROR: No event count provided and p.d.f does not provide expected number of events" << std::endl ;
123 return nullptr ;
124 } else {
125 // Don't round in expectedData mode
126 if (_expectedData || extended) {
127 nEvents = _pdf->expectedEvents(_vars.get());
128 } else {
129 nEvents = Int_t(_pdf->expectedEvents(_vars.get())+0.5) ;
130 }
131 }
132 }
133
134 // Sample p.d.f. distribution
135 _pdf->fillDataHist(_hist.get(),_vars.get(),1,true) ;
136
137 // Output container
138 RooDataSet* wudata = new RooDataSet("wu","wu",*_vars,RooFit::WeightVar()) ;
139
140 vector<int> histOut(_hist->numEntries()) ;
141 double histMax(-1) ;
142 Int_t histOutSum(0) ;
143 for (int i=0 ; i<_hist->numEntries() ; i++) {
144 _hist->get(i) ;
145 if (_expectedData) {
146
147 // Expected data, multiply p.d.f by nEvents
148 double w=_hist->weight()*nEvents ;
149 wudata->add(*_hist->get(),w) ;
150
151 } else if (extended) {
152
153 // Extended mode, set contents to Poisson(pdf*nEvents)
154 double w = RooRandom::randomGenerator()->Poisson(_hist->weight()*nEvents) ;
155 wudata->add(*_hist->get(),w) ;
156
157 } else {
158
159 // Regular mode, fill array of weights with Poisson(pdf*nEvents), but to not fill
160 // histogram yet.
161 if (_hist->weight()>histMax) {
162 histMax = _hist->weight() ;
163 }
164 histOut[i] = RooRandom::randomGenerator()->Poisson(_hist->weight()*nEvents) ;
165 histOutSum += histOut[i] ;
166 }
167 }
168
169
170 if (!_expectedData && !extended) {
171
172 // Second pass for regular mode - Trim/Extend dataset to exact number of entries
173
174 // Calculate difference between what is generated so far and what is requested
175 Int_t nEvtExtra = std::abs(Int_t(nEvents)-histOutSum) ;
176 Int_t wgt = (histOutSum>nEvents) ? -1 : 1 ;
177
178 // Perform simple binned accept/reject procedure to get to exact event count
179 while(nEvtExtra>0) {
180
181 Int_t ibinRand = RooRandom::randomGenerator()->Integer(_hist->numEntries()) ;
182 _hist->get(ibinRand) ;
183 double ranY = RooRandom::randomGenerator()->Uniform(histMax) ;
184
185 if (ranY<_hist->weight()) {
186 if (wgt==1) {
187 histOut[ibinRand]++ ;
188 } else {
189 // If weight is negative, prior bin content must be at least 1
190 if (histOut[ibinRand]>0) {
191 histOut[ibinRand]-- ;
192 } else {
193 continue ;
194 }
195 }
196 nEvtExtra-- ;
197 }
198 }
199
200 // Transfer working array to histogram
201 for (int i=0 ; i<_hist->numEntries() ; i++) {
202 _hist->get(i) ;
203 wudata->add(*_hist->get(),histOut[i]) ;
204 }
205
206 }
207
208 return wudata ;
209
210}
211
212
213
214////////////////////////////////////////////////////////////////////////////////
215/// this method is not implemented for this context
216
221
222
223
224////////////////////////////////////////////////////////////////////////////////
225/// Print the details of the context
226
227void RooBinnedGenContext::printMultiline(ostream &os, Int_t content, bool verbose, TString indent) const
228{
230 os << indent << "--- RooBinnedGenContext ---" << std::endl ;
231 os << indent << "Using PDF ";
233}
#define cxcoutI(a)
#define coutE(a)
#define ccxcoutI(a)
int Int_t
Definition RtypesCore.h:45
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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.
RooAbsArg * find(const char *name) const
Find object with given name in list.
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:218
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:24
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:159
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:34
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:47
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)