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