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