Logo ROOT   6.18/05
Reference Guide
RooAddGenContext.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 RooAddGenContext.cxx
19\class RooAddGenContext
20\ingroup Roofitcore
21
22RooAddGenContext is an efficient implementation of the
23generator context specific for RooAddPdf PDFs. The strategy
24of RooAddGenContext is to defer generation of each component
25to a dedicated generator context for that component and to
26randomly choose one of those context to generate an event,
27with a probability proportional to its associated coefficient.
28**/
29
30
31#include "RooFit.h"
32
33#include "Riostream.h"
34
35
36#include "RooMsgService.h"
37#include "RooAddGenContext.h"
38#include "RooAddGenContext.h"
39#include "RooAddPdf.h"
40#include "RooDataSet.h"
41#include "RooRandom.h"
42#include "RooAddModel.h"
43
44using namespace std;
45
47;
48
49
50////////////////////////////////////////////////////////////////////////////////
51/// Constructor
52
54 const RooDataSet *prototype, const RooArgSet* auxProto,
56 RooAbsGenContext(model,vars,prototype,auxProto,verbose), _isModel(kFALSE)
57{
58 cxcoutI(Generation) << "RooAddGenContext::ctor() setting up event special generator context for sum p.d.f. " << model.GetName()
59 << " for generation of observable(s) " << vars ;
60 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
61 if (auxProto && auxProto->getSize()>0) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
62 ccxcoutI(Generation) << endl ;
63
64 // Constructor. Build an array of generator contexts for each product component PDF
66 _pdf = (RooAddPdf*) _pdfSet->find(model.GetName()) ;
68
69 // Fix normalization set of this RooAddPdf
70 if (prototype)
71 {
72 RooArgSet coefNSet(vars) ;
73 coefNSet.add(*prototype->get()) ;
75 }
76
77 _nComp = model._pdfList.getSize() ;
78 _coefThresh = new Double_t[_nComp+1] ;
79 _vars = (RooArgSet*) vars.snapshot(kFALSE) ;
80
81 for (const auto arg : model._pdfList) {
82 auto pdf = dynamic_cast<const RooAbsPdf *>(arg);
83 if (!pdf) {
84 coutF(Generation) << "Cannot generate events from an object that is not a PDF.\n\t"
85 << "The offending object is a " << arg->IsA()->GetName() << " named '" << arg->GetName() << "'." << std::endl;
86 throw std::invalid_argument("Trying to generate events from on object that is not a PDF.");
87 }
88
89 RooAbsGenContext* cx = pdf->genContext(vars,prototype,auxProto,verbose) ;
90 _gcList.push_back(cx) ;
91 }
92
93 ((RooAddPdf*)_pdf)->getProjCache(_vars) ;
95
96 _mcache = 0 ;
97 _pcache = 0 ;
98}
99
100
101
102////////////////////////////////////////////////////////////////////////////////
103/// Constructor
104
106 const RooDataSet *prototype, const RooArgSet* auxProto,
107 Bool_t verbose) :
108 RooAbsGenContext(model,vars,prototype,auxProto,verbose), _isModel(kTRUE)
109{
110 cxcoutI(Generation) << "RooAddGenContext::ctor() setting up event special generator context for sum resolution model " << model.GetName()
111 << " for generation of observable(s) " << vars ;
112 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
113 if (auxProto && auxProto->getSize()>0) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
114 ccxcoutI(Generation) << endl ;
115
116 // Constructor. Build an array of generator contexts for each product component PDF
118 _pdf = (RooAbsPdf*) _pdfSet->find(model.GetName()) ;
119
120
121 model._pdfIter->Reset() ;
122 RooAbsPdf* pdf ;
123 _nComp = model._pdfList.getSize() ;
124 _coefThresh = new Double_t[_nComp+1] ;
125 _vars = (RooArgSet*) vars.snapshot(kFALSE) ;
126
127 while((pdf=(RooAbsPdf*)model._pdfIter->Next())) {
128 RooAbsGenContext* cx = pdf->genContext(vars,prototype,auxProto,verbose) ;
129 _gcList.push_back(cx) ;
130 }
131
132 ((RooAddModel*)_pdf)->getProjCache(_vars) ;
134
135 _mcache = 0 ;
136 _pcache = 0 ;
137}
138
139
140
141////////////////////////////////////////////////////////////////////////////////
142/// Destructor. Delete all owned subgenerator contexts
143
145{
146 delete[] _coefThresh ;
147 for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
148 delete *iter ;
149 }
150 delete _vars ;
151 delete _pdfSet ;
152}
153
154
155
156////////////////////////////////////////////////////////////////////////////////
157/// Attach given set of variables to internal p.d.f. clone
158
160{
162
163 // Forward initGenerator call to all components
164 for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
165 (*iter)->attach(args) ;
166 }
167}
168
169
170
171////////////////////////////////////////////////////////////////////////////////
172/// One-time initialization of generator contex. Attach theEvent
173/// to internal p.d.f clone and forward initialization call to
174/// the component generators
175
177{
178 _pdf->recursiveRedirectServers(theEvent) ;
179
180 if (_isModel) {
181 RooAddModel* amod = (RooAddModel*) _pdf ;
182 _mcache = amod->getProjCache(_vars) ;
183 } else {
184 RooAddPdf* apdf = (RooAddPdf*) _pdf ;
185 _pcache = apdf->getProjCache(_vars,0,"FULL_RANGE_ADDGENCONTEXT") ;
186 }
187
188 // Forward initGenerator call to all components
189 for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
190 (*iter)->initGenerator(theEvent) ;
191 }
192}
193
194
195////////////////////////////////////////////////////////////////////////////////
196/// Randomly choose one of the component contexts to generate this event,
197/// with a probability proportional to its coefficient
198
200{
201 // Throw a random number to determin which component to generate
204 Int_t i=0 ;
205 for (i=0 ; i<_nComp ; i++) {
206 if (rand>_coefThresh[i] && rand<_coefThresh[i+1]) {
207 _gcList[i]->generateEvent(theEvent,remaining) ;
208 return ;
209 }
210 }
211}
212
213
214////////////////////////////////////////////////////////////////////////////////
215/// Update the cumulative threshold table from the current coefficient
216/// values
217
219{
220 if (_isModel) {
221
222 RooAddModel* amod = (RooAddModel*) _pdf ;
224
225 _coefThresh[0] = 0. ;
226 Int_t i ;
227 for (i=0 ; i<_nComp ; i++) {
228 _coefThresh[i+1] = amod->_coefCache[i] ;
229 _coefThresh[i+1] += _coefThresh[i] ;
230 }
231
232 } else {
233
234 RooAddPdf* apdf = (RooAddPdf*) _pdf ;
235
237
238 _coefThresh[0] = 0. ;
239 Int_t i ;
240 for (i=0 ; i<_nComp ; i++) {
241 _coefThresh[i+1] = apdf->_coefCache[i] ;
242 _coefThresh[i+1] += _coefThresh[i] ;
243// cout << "RooAddGenContext::updateThresholds(" << GetName() << ") _coefThresh[" << i+1 << "] = " << _coefThresh[i+1] << endl ;
244 }
245
246 }
247
248}
249
250
251////////////////////////////////////////////////////////////////////////////////
252/// Forward the setProtoDataOrder call to the component generator contexts
253
255{
257 for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
258 (*iter)->setProtoDataOrder(lut) ;
259 }
260}
261
262
263
264////////////////////////////////////////////////////////////////////////////////
265/// Print the details of the context
266
268{
270 os << indent << "--- RooAddGenContext ---" << endl ;
271 os << indent << "Using PDF ";
273
274 os << indent << "List of component generators" << endl ;
275 TString indent2(indent) ;
276 indent2.Append(" ") ;
277 for (vector<RooAbsGenContext*>::const_iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
278 (*iter)->printMultiline(os,content,verbose,indent2) ;
279 }
280}
#define cxcoutI(a)
Definition: RooMsgService.h:83
#define coutF(a)
Definition: RooMsgService.h:35
#define ccxcoutI(a)
Definition: RooMsgService.h:84
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
static void indent(ostringstream &buf, int indent_level)
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Change cache operation mode to given mode.
Definition: RooAbsArg.cxx:1719
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1065
Int_t getSize() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsGenContext is the abstract base class for generator contexts of RooAbsPdf objects.
RooArgSet * _theEvent
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order of prototype data to that in the lookup tables passed as argument.
virtual RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t verbose=kFALSE) const
Interface function to create a generator context from a p.d.f.
Definition: RooAbsPdf.cxx:1709
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...
RooAddGenContext is an efficient implementation of the generator context specific for RooAddPdf PDFs.
RooAddGenContext(const RooAddPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
Constructor.
virtual void initGenerator(const RooArgSet &theEvent)
One-time initialization of generator contex.
virtual ~RooAddGenContext()
Destructor. Delete all owned subgenerator contexts.
virtual void setProtoDataOrder(Int_t *lut)
Forward the setProtoDataOrder call to the component generator contexts.
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
Randomly choose one of the component contexts to generate this event, with a probability proportional...
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print the details of the context.
void updateThresholds()
Update the cumulative threshold table from the current coefficient values.
std::vector< RooAbsGenContext * > _gcList
RooArgSet * _pdfSet
virtual void attach(const RooArgSet &params)
Attach given set of variables to internal p.d.f. clone.
RooAddModel::CacheElem * _mcache
Double_t * _coefThresh
RooAddPdf::CacheElem * _pcache
RooAddModel cache element.
const RooArgSet * _vars
TIterator * _pdfIter
List of supplemental normalization factors.
Definition: RooAddModel.h:136
CacheElem * getProjCache(const RooArgSet *nset, const RooArgSet *iset=0, const char *rangeName=0) const
Retrieve cache element with for calculation of p.d.f value with normalization set nset and integrated...
void updateCoefficients(CacheElem &cache, const RooArgSet *nset) const
Update the coefficient values in the given cache element: calculate new remainder fraction,...
RooListProxy _pdfList
Registry of component analytical integration codes.
Definition: RooAddModel.h:133
Double_t * _coefCache
Definition: RooAddModel.h:99
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
void updateCoefficients(CacheElem &cache, const RooArgSet *nset) const
Update the coefficient values in the given cache element: calculate new remainder fraction,...
Definition: RooAddPdf.cxx:645
CacheElem * getProjCache(const RooArgSet *nset, const RooArgSet *iset=0, const char *rangeName=0) const
Retrieve cache element for the computation of the PDF normalisation.
Definition: RooAddPdf.cxx:402
RooListProxy _pdfList
Registry of component analytical integration codes.
Definition: RooAddPdf.h:133
std::vector< double > _coefCache
Definition: RooAddPdf.h:102
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:134
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
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
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 Double_t uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1)
Definition: RooRandom.cxx:84
virtual void Reset()=0
virtual TObject * Next()=0
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Basic string class.
Definition: TString.h:131
TString & Append(const char *cs)
Definition: TString.h:559
@ Generation
Definition: RooGlobalFunc.h:57