Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22Efficient 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#include "RooAddGenContext.h"
31
32#include "RooDataSet.h"
33#include "RooRandom.h"
34
35#include <sstream>
36
37
38////////////////////////////////////////////////////////////////////////////////
39/// Constructor
40
43 bool verbose) :
44 RooAbsGenContext(model,vars,prototype,auxProto,verbose), _isModel(false)
45{
46 cxcoutI(Generation) << "RooAddGenContext::ctor() setting up event special generator context for sum p.d.f. " << model.GetName()
47 << " for generation of observable(s) " << vars ;
48 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
49 if (auxProto && !auxProto->empty()) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
50 ccxcoutI(Generation) << std::endl;
51
52 // Constructor. Build an array of generator contexts for each product component PDF
53 _pdfSet = std::make_unique<RooArgSet>();
54 RooArgSet(model).snapshot(*_pdfSet, true);
55 _pdf = static_cast<RooAddPdf*>(_pdfSet->find(model.GetName())) ;
56
57 // Fix normalization set of this RooAddPdf
58 if (prototype)
59 {
60 RooArgSet coefNSet(vars) ;
61 coefNSet.add(*prototype->get()) ;
63 }
64
65 _nComp = model._pdfList.size() ;
66 _coefThresh.resize(_nComp+1);
67 _vars = std::make_unique<RooArgSet>();
68 vars.snapshot(*_vars, false);
69
70 for (const auto arg : model._pdfList) {
71 auto pdf = dynamic_cast<const RooAbsPdf *>(arg);
72 if (!pdf) {
73 coutF(Generation) << "Cannot generate events from an object that is not a PDF.\n\t"
74 << "The offending object is a " << arg->ClassName() << " named '" << arg->GetName() << "'." << std::endl;
75 throw std::invalid_argument("Trying to generate events from on object that is not a PDF.");
76 }
77
78 _gcList.emplace_back(pdf->genContext(vars,prototype,auxProto,verbose));
79 }
80
81 (static_cast<RooAddPdf*>(_pdf))->getProjCache(_vars.get()) ;
83}
84
85
86
87////////////////////////////////////////////////////////////////////////////////
88/// Constructor
89
92 bool verbose) :
93 RooAbsGenContext(model,vars,prototype,auxProto,verbose), _isModel(true)
94{
95 cxcoutI(Generation) << "RooAddGenContext::ctor() setting up event special generator context for sum resolution model " << model.GetName()
96 << " for generation of observable(s) " << vars ;
97 if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
98 if (auxProto && !auxProto->empty()) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
99 ccxcoutI(Generation) << std::endl;
100
101 // Constructor. Build an array of generator contexts for each product component PDF
102 _pdfSet = std::make_unique<RooArgSet>();
103 RooArgSet(model).snapshot(*_pdfSet, true);
104 _pdf = static_cast<RooAbsPdf*>(_pdfSet->find(model.GetName())) ;
105
106 _nComp = model._pdfList.size() ;
107 _coefThresh.resize(_nComp+1);
108 _vars = std::make_unique<RooArgSet>();
109 vars.snapshot(*_vars, false);
110
111 for (const auto obj : model._pdfList) {
112 auto pdf = static_cast<RooAbsPdf*>(obj);
113 _gcList.emplace_back(pdf->genContext(vars,prototype,auxProto,verbose));
114 }
115
116 (static_cast<RooAddModel*>(_pdf))->getProjCache(_vars.get()) ;
118}
119
120
121////////////////////////////////////////////////////////////////////////////////
122/// Attach given set of variables to internal p.d.f. clone
123
125{
127
128 // Forward initGenerator call to all components
129 for(auto& gc : _gcList) {
130 gc->attach(args) ;
131 }
132}
133
134
135
136////////////////////////////////////////////////////////////////////////////////
137/// One-time initialization of generator context. Attach theEvent
138/// to internal p.d.f clone and forward initialization call to
139/// the component generators
140
142{
144
145 if (_isModel) {
146 RooAddModel* amod = static_cast<RooAddModel*>(_pdf) ;
147 _pcache = amod->getProjCache(_vars.get()) ;
148 } else {
149 RooAddPdf* apdf = static_cast<RooAddPdf*>(_pdf) ;
150 _pcache = apdf->getProjCache(_vars.get(),nullptr) ;
151 }
152
153 // Forward initGenerator call to all components
154 for(auto& gc : _gcList) {
155 gc->initGenerator(theEvent) ;
156 }
157}
158
159
160////////////////////////////////////////////////////////////////////////////////
161/// Randomly choose one of the component contexts to generate this event,
162/// with a probability proportional to its coefficient
163
165{
166 // Throw a random number to determine which component to generate
168 double rand = RooRandom::uniform() ;
169 for (Int_t i=0 ; i<_nComp ; i++) {
170 if (rand>_coefThresh[i] && rand<_coefThresh[i+1]) {
171 _gcList[i]->generateEvent(theEvent,remaining) ;
172 return ;
173 }
174 }
175}
176
177
178////////////////////////////////////////////////////////////////////////////////
179/// Update the cumulative threshold table from the current coefficient
180/// values
181
183{
184 // Templated lambda to support RooAddModel and RooAddPdf
185 auto updateThresholdsImpl = [&](auto* pdf, auto * cache) {
186 pdf->updateCoefficients(*cache,_vars.get()) ;
187
188 _coefThresh[0] = 0. ;
189 for (Int_t i=0 ; i<_nComp ; i++) {
190 double coef = pdf->_coefCache[i];
191 if(coef < 0.0) {
192 std::stringstream errMsgStream;
193 errMsgStream << "RooAddGenContext::updateThresholds(): coefficient number " << i << " of the "
194 << pdf->ClassName() << " \"" << pdf->GetName() << "\"" << " is negative!"
195 << " The current RooAddGenContext doesn't support negative coefficients."
196 << " Please recreate a new generator context with " << pdf->ClassName() << "::genContext()";
197 auto const errMsg = errMsgStream.str();
198 cxcoutE(Generation) << errMsg << std::endl;
199 throw std::runtime_error(errMsg);
200 }
201 _coefThresh[i+1] = coef + _coefThresh[i];
202 }
203 };
204
206 : updateThresholdsImpl(static_cast<RooAddPdf*>(_pdf), _pcache);
207}
208
209
210////////////////////////////////////////////////////////////////////////////////
211/// Forward the setProtoDataOrder call to the component generator contexts
212
214{
216 for(auto& gc : _gcList) {
217 gc->setProtoDataOrder(lut) ;
218 }
219}
220
221
222
223////////////////////////////////////////////////////////////////////////////////
224/// Print the details of the context
225
226void RooAddGenContext::printMultiline(std::ostream &os, Int_t content, bool verbose, TString indent) const
227{
229 os << indent << "--- RooAddGenContext ---" << std::endl;
230 os << indent << "Using PDF ";
232
233 os << indent << "List of component generators" << std::endl;
235 indent2.Append(" ") ;
236 for(auto& gc : _gcList) {
237 gc->printMultiline(os,content,verbose,indent2) ;
238 }
239}
#define cxcoutI(a)
#define coutF(a)
#define cxcoutE(a)
#define ccxcoutI(a)
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.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
bool recursiveRedirectServers(const RooAbsCollection &newSet, bool mustReplaceAll=false, bool nameChange=false, bool recurseInNewSet=true)
Recursively replace all servers with the new servers in newSet.
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.
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
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...
void setProtoDataOrder(Int_t *lut) override
Forward the setProtoDataOrder call to the component generator contexts.
std::vector< double > _coefThresh
[_nComp] Array of coefficient thresholds
std::unique_ptr< RooArgSet > _pdfSet
Set owned all nodes of internal clone of p.d.f.
std::unique_ptr< RooArgSet > _vars
bool _isModel
Are we generating from a RooAddPdf or a RooAddModel.
void initGenerator(const RooArgSet &theEvent) override
One-time initialization of generator context.
std::vector< std::unique_ptr< RooAbsGenContext > > _gcList
List of component generator contexts.
void updateThresholds()
Update the cumulative threshold table from the current coefficient values.
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Print the details of the context.
AddCacheElem * _pcache
! RooAddPdf cache element
void attach(const RooArgSet &params) override
Attach given set of variables to internal p.d.f. clone.
Int_t _nComp
Number of PDF components.
RooAddGenContext(const RooAddPdf &model, const RooArgSet &vars, const RooDataSet *prototype=nullptr, const RooArgSet *auxProto=nullptr, bool _verbose=false)
Constructor.
RooAbsPdf * _pdf
Pointer to cloned p.d.f.
void generateEvent(RooArgSet &theEvent, Int_t remaining) override
Randomly choose one of the component contexts to generate this event, with a probability proportional...
RooAddModel is an efficient implementation of a sum of PDFs of the form.
Definition RooAddModel.h:26
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:32
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
Container class to hold unbinned data.
Definition RooDataSet.h:32
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 uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1)
Definition RooRandom.cxx:77
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
Basic string class.
Definition TString.h:137