Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
RooProdGenContext.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 RooProdGenContext.cxx
19\class RooProdGenContext
20\ingroup Roofitcore
21
22RooProdGenContext is an efficient implementation of the generator context
23specific for RooProdPdf PDFs. The sim-context owns a list of
24component generator contexts that are used to generate the dependents
25for each component PDF sequentially.
26**/
27
28#include "Riostream.h"
29#include "RooMsgService.h"
30
31#include "RooProdGenContext.h"
32#include "RooProdPdf.h"
33#include "RooDataSet.h"
34#include "RooRealVar.h"
35#include "RooGlobalFunc.h"
36
37
38
39using namespace std;
40
42;
43
44
45////////////////////////////////////////////////////////////////////////////////
46
48 const RooDataSet *prototype, const RooArgSet* auxProto, bool verbose) :
49 RooAbsGenContext(model,vars,prototype,auxProto,verbose), _pdf(&model)
50{
51 // Constructor of optimization generator context for RooProdPdf objects
52
53 //Build an array of generator contexts for each product component PDF
54 cxcoutI(Generation) << "RooProdGenContext::ctor() setting up event special generator context for product p.d.f. " << model.GetName()
55 << " for generation of observable(s) " << vars ;
56 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
57 if (auxProto && !auxProto->empty()) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
58 ccxcoutI(Generation) << endl ;
59
60 // Make full list of dependents (generated & proto)
61 RooArgSet deps(vars) ;
62 if (prototype) {
63 deps.remove(*std::unique_ptr<RooArgSet>{model.getObservables(*prototype->get())},true,true) ;
64 }
65
66 // Factorize product in irreducible terms
67 RooLinkedList termList,depsList,impDepList,crossDepList,intList ;
68 model.factorizeProduct(deps,RooArgSet(),termList,depsList,impDepList,crossDepList,intList) ;
69
70 if (dologD(Generation)) {
71 cxcoutD(Generation) << "RooProdGenContext::ctor() factorizing product expression in irriducible terms " ;
72 for(auto * t : static_range_cast<RooArgSet*>(termList)) {
73 ccxcoutD(Generation) << *t ;
74 }
75 ccxcoutD(Generation) << std::endl;
76 }
77
78 RooArgSet genDeps ;
79 // First add terms that do not import observables
80
81 bool anyAction = true ;
82 bool go=true ;
83 while(go) {
84
85 auto termIter = termList.begin();
86 auto impIter = impDepList.begin();
87 auto normIter = depsList.begin();
88
89 bool anyPrevAction=anyAction ;
90 anyAction=false ;
91
92 if (termList.empty()) {
93 break ;
94 }
95
96 while(termIter != termList.end()) {
97
98 auto * term = static_cast<RooArgSet*>(*termIter);
99 auto * impDeps = static_cast<RooArgSet*>(*impIter);
100 auto * termDeps = static_cast<RooArgSet*>(*normIter);
101 if (impDeps==nullptr || termDeps==nullptr) {
102 break ;
103 }
104
105 cxcoutD(Generation) << "RooProdGenContext::ctor() analyzing product term " << *term << " with observable(s) " << *termDeps ;
106 if (!impDeps->empty()) {
107 ccxcoutD(Generation) << " which has dependence of external observable(s) " << *impDeps << " that to be generated first by other terms" ;
108 }
109 ccxcoutD(Generation) << std::endl;
110
111 // Add this term if we have no imported dependents, or imported dependents are already generated
112 RooArgSet neededDeps(*impDeps) ;
113 neededDeps.remove(genDeps,true,true) ;
114
115 if (!neededDeps.empty()) {
116 if (!anyPrevAction) {
117 cxcoutD(Generation) << "RooProdGenContext::ctor() no convergence in single term analysis loop, terminating loop and process remainder of terms as single unit " << endl ;
118 go=false ;
119 break ;
120 }
121 cxcoutD(Generation) << "RooProdGenContext::ctor() skipping this term for now because it needs imported dependents that are not generated yet" << endl ;
122 ++termIter;
123 ++impIter;
124 ++normIter;
125 continue ;
126 }
127
128 // Check if this component has any dependents that need to be generated
129 // e.g. it can happen that there are none if all dependents of this component are prototyped
130 if (termDeps->empty()) {
131 cxcoutD(Generation) << "RooProdGenContext::ctor() term has no observables requested to be generated, removing it" << endl ;
132
133 // Increment the iterators first, because Removing the corresponding element
134 // would invalidate them otherwise.
135 ++termIter;
136 ++normIter;
137 ++impIter;
138 termList.Remove(term);
139 depsList.Remove(termDeps);
140 impDepList.Remove(impDeps);
141
142 delete term ;
143 delete termDeps ;
144 delete impDeps ;
145 anyAction=true ;
146 continue ;
147 }
148
149 if (term->size()==1) {
150 // Simple term
151
152 auto pdf = static_cast<RooAbsPdf*>((*term)[0]);
153 std::unique_ptr<RooArgSet> pdfDep{pdf->getObservables(termDeps)};
154 if (!pdfDep->empty()) {
155 coutI(Generation) << "RooProdGenContext::ctor() creating subcontext for generation of observables " << *pdfDep << " from model " << pdf->GetName() << endl ;
156 std::unique_ptr<RooArgSet> auxProto2{pdf->getObservables(impDeps)};
157 _gcList.emplace_back(pdf->genContext(*pdfDep,prototype,auxProto2.get(),verbose)) ;
158 }
159
160// cout << "adding following dependents to list of generated observables: " ; pdfDep->Print("1") ;
161 genDeps.add(*pdfDep) ;
162
163 } else {
164
165 // Composite term
166 if (!termDeps->empty()) {
167 const std::string name = model.makeRGPPName("PRODGEN_",*term,RooArgSet(),RooArgSet(),nullptr) ;
168
169 // Construct auxiliary PDF expressing product of composite terms,
170 // following Conditional component specification of input model
171 RooLinkedList cmdList ;
172 RooLinkedList pdfSetList ;
173 RooArgSet fullPdfSet ;
174 for(auto * pdf : static_range_cast<RooAbsPdf*>(*term)) {
175
176 RooArgSet* pdfnset = model.findPdfNSet(*pdf) ;
177 RooArgSet* pdfSet = new RooArgSet(*pdf) ;
178 pdfSetList.Add(pdfSet) ;
179
180 if (pdfnset && !pdfnset->empty()) {
181 // This PDF requires a Conditional() construction
182 cmdList.Add(RooFit::Conditional(*pdfSet,*pdfnset).Clone()) ;
183// cout << "Conditional " << pdf->GetName() << " " ; pdfnset->Print("1") ;
184 } else {
185 fullPdfSet.add(*pdfSet) ;
186 }
187
188 }
189 auto multiPdf = std::make_unique<RooProdPdf>(name.c_str(),name.c_str(),fullPdfSet,cmdList) ;
190 cmdList.Delete() ;
191 pdfSetList.Delete() ;
192
193 multiPdf->setOperMode(RooAbsArg::ADirty,true) ;
194 multiPdf->useDefaultGen(true) ;
195
196 coutI(Generation) << "RooProdGenContext()::ctor creating subcontext for generation of observables " << *termDeps
197 << "for irriducuble composite term using sub-product object " << multiPdf->GetName() ;
198 _gcList.emplace_back(multiPdf->genContext(*termDeps,prototype,auxProto,verbose));
199 _ownedMultiProds.addOwned(std::move(multiPdf));
200
201 genDeps.add(*termDeps) ;
202
203 }
204 }
205
206 // Increment the iterators first, because Removing the corresponding
207 // element would invalidate them otherwise.
208 ++termIter;
209 ++normIter;
210 ++impIter;
211 termList.Remove(term);
212 depsList.Remove(termDeps);
213 impDepList.Remove(impDeps);
214 delete term ;
215 delete termDeps ;
216 delete impDeps ;
217 anyAction=true ;
218 }
219 }
220
221 // Check if there are any left over terms that cannot be generated
222 // separately due to cross dependency of observables
223 if (!termList.empty()) {
224
225 cxcoutD(Generation) << "RooProdGenContext::ctor() there are left-over terms that need to be generated separately" << endl ;
226
227 // Concatenate remaining terms
228 auto normIter = depsList.begin();
229 RooArgSet trailerTerm ;
230 RooArgSet trailerTermDeps ;
231 for(auto * term : static_range_cast<RooArgSet*>(termList)) {
232 auto* termDeps = static_cast<RooArgSet*>(*normIter);
233 trailerTerm.add(*term) ;
234 trailerTermDeps.add(*termDeps) ;
235 ++normIter;
236 }
237
238 const std::string name = model.makeRGPPName("PRODGEN_",trailerTerm,RooArgSet(),RooArgSet(),nullptr) ;
239
240 // Construct auxiliary PDF expressing product of composite terms,
241 // following Partial/Full component specification of input model
242 RooLinkedList cmdList ;
243 RooLinkedList pdfSetList ;
244 RooArgSet fullPdfSet ;
245
246 for(auto * pdf : static_range_cast<RooAbsPdf*>(trailerTerm)) {
247
248 RooArgSet* pdfnset = model.findPdfNSet(*pdf) ;
249 RooArgSet* pdfSet = new RooArgSet(*pdf) ;
250 pdfSetList.Add(pdfSet) ;
251
252 if (pdfnset && !pdfnset->empty()) {
253 // This PDF requires a Conditional() construction
254 cmdList.Add(RooFit::Conditional(*pdfSet,*pdfnset).Clone()) ;
255 } else {
256 fullPdfSet.add(*pdfSet) ;
257 }
258
259 }
260// cmdList.Print("v") ;
261 auto multiPdf = std::make_unique<RooProdPdf>(name.c_str(),name.c_str(),fullPdfSet,cmdList);
262 cmdList.Delete() ;
263 pdfSetList.Delete() ;
264
265 multiPdf->setOperMode(RooAbsArg::ADirty,true) ;
266 multiPdf->useDefaultGen(true) ;
267
268 cxcoutD(Generation) << "RooProdGenContext(" << model.GetName() << "): creating context for irreducible composite trailer term "
269 << multiPdf->GetName() << " that generates observables " << trailerTermDeps << endl ;
270 _gcList.emplace_back(multiPdf->genContext(trailerTermDeps,prototype,auxProto,verbose));
271
272 _ownedMultiProds.addOwned(std::move(multiPdf));
273 }
274
275 // Now check if the are observables in vars that are not generated by any of the above p.d.f.s
276 // If not, generate uniform distributions for these using a special context
277 _uniObs.add(vars) ;
278 _uniObs.remove(genDeps,true,true) ;
279 if (!_uniObs.empty()) {
280 coutI(Generation) << "RooProdGenContext(" << model.GetName() << "): generating uniform distribution for non-dependent observable(s) " << _uniObs << std::endl;
281 }
282
283
284 // We own contents of lists filled by factorizeProduct()
285 termList.Delete() ;
286 depsList.Delete() ;
287 impDepList.Delete() ;
288 crossDepList.Delete() ;
289 intList.Delete() ;
290
291}
292
293
294
295////////////////////////////////////////////////////////////////////////////////
296/// Destructor. Delete all owned subgenerator contexts
297
299
300
301////////////////////////////////////////////////////////////////////////////////
302/// Attach generator to given event buffer
303
305{
306 //Forward initGenerator call to all components
307 for (auto const& elem : _gcList) {
308 elem->attach(args) ;
309 }
310}
311
312
313////////////////////////////////////////////////////////////////////////////////
314/// One-time initialization of generator context, forward to component generators
315
317{
318 // Forward initGenerator call to all components
319 for (auto const& elem : _gcList) {
320 elem->initGenerator(theEvent) ;
321 }
322}
323
324
325
326////////////////////////////////////////////////////////////////////////////////
327/// Generate a single event of the product by generating the components
328/// of the products sequentially. The subcontext have been order such
329/// that all conditional dependencies are correctly taken into account
330/// when processed in sequential order
331
333{
334 // Loop over the component generators
335
336 for (auto const& elem : _gcList) {
337 elem->generateEvent(theEvent,remaining) ;
338 }
339
340 // Generate uniform variables (non-dependents)
341 if (!_uniObs.empty()) {
342 for(auto * arglv : dynamic_range_cast<RooAbsLValue*>(_uniObs)) {
343 if (arglv) {
344 arglv->randomize() ;
345 }
346 }
347 theEvent.assign(_uniObs) ;
348 }
349}
350
351
352
353////////////////////////////////////////////////////////////////////////////////
354/// Set the traversal order of the prototype dataset by the
355/// given lookup table
356
358{
359 // Forward call to component generators
361
362 for (auto const& elem : _gcList) {
363 elem->setProtoDataOrder(lut) ;
364 }
365}
366
367
368
369////////////////////////////////////////////////////////////////////////////////
370/// Detailed printing interface
371
372void RooProdGenContext::printMultiline(ostream &os, Int_t content, bool verbose, TString indent) const
373{
374 RooAbsGenContext::printMultiline(os,content,verbose,indent) ;
375 os << indent << "--- RooProdGenContext ---" << endl ;
376 os << indent << "Using PDF ";
378 os << indent << "List of component generators" << endl ;
379
380 TString indent2(indent) ;
381 indent2.Append(" ") ;
382
383 for (auto const& elem : _gcList) {
384 elem->printMultiline(os,content,verbose,indent2) ;
385 }
386}
#define coutI(a)
#define cxcoutI(a)
#define cxcoutD(a)
#define dologD(a)
#define ccxcoutD(a)
#define ccxcoutI(a)
#define ClassImp(name)
Definition Rtypes.h:377
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
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 remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
virtual bool add(const RooAbsArg &var, bool silent=false)
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...
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
Abstract base class for generator contexts of RooAbsPdf objects.
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
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.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooDataSet is a 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'.
RooLinkedList is an collection class for internal use, storing a collection of RooAbsArg pointers in ...
RooLinkedListIterImpl end() const
bool empty() const
void Delete(Option_t *o=nullptr) override
Remove all elements in collection and delete all elements NB: Collection does not own elements,...
virtual void Add(TObject *arg)
RooLinkedListIterImpl begin() const
virtual bool Remove(TObject *arg)
Remove object from collection.
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,...
RooProdGenContext is an efficient implementation of the generator context specific for RooProdPdf PDF...
std::list< std::unique_ptr< RooAbsGenContext > > _gcList
List of component generator contexts.
void setProtoDataOrder(Int_t *lut) override
Set the traversal order of the prototype dataset by the given lookup table.
void attach(const RooArgSet &params) override
Attach generator to given event buffer.
RooArgSet _ownedMultiProds
Owned auxiliary multi-term product PDFs.
const RooProdPdf * _pdf
Original PDF.
void initGenerator(const RooArgSet &theEvent) override
One-time initialization of generator context, forward to component generators.
RooArgSet _uniObs
Observable to be generated with flat distribution.
~RooProdGenContext() override
Destructor. Delete all owned subgenerator contexts.
void generateEvent(RooArgSet &theEvent, Int_t remaining) override
Generate a single event of the product by generating the components of the products sequentially.
RooProdGenContext(const RooProdPdf &model, const RooArgSet &vars, const RooDataSet *prototype=nullptr, const RooArgSet *auxProto=nullptr, bool _verbose=false)
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Detailed printing interface.
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:33
void factorizeProduct(const RooArgSet &normSet, const RooArgSet &intSet, RooLinkedList &termList, RooLinkedList &normList, RooLinkedList &impDepList, RooLinkedList &crossDepList, RooLinkedList &intList) const
Factorize product in irreducible terms for given choice of integration/normalization.
std::string makeRGPPName(const char *pfx, const RooArgSet &term, const RooArgSet &iset, const RooArgSet &nset, const char *isetRangeName) const
Make an appropriate automatic name for a RooGenProdProj object in getPartIntList()
RooArgSet * findPdfNSet(RooAbsPdf const &pdf) const
Look up user specified normalization set for given input PDF component.
TObject * Clone(const char *newname="") const override
Make a clone of an object using the Streamer facility.
Definition TNamed.cxx:74
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
TString & Append(const char *cs)
Definition TString.h:576
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool depsAreCond=false)