Logo ROOT  
Reference Guide
RooConvGenContext.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 RooConvGenContext.cxx
19\class RooConvGenContext
20\ingroup Roofitcore
21
22RooConvGenContext is an efficient implementation of the generator context
23specific for RooAbsAnaConvPdf objects. The physics model is generated
24with a truth resolution model and the requested resolution model is generated
25separately as a PDF. The convolution variable of the physics model is
26subsequently explicitly smeared with the resolution model distribution.
27**/
28
29#include "RooMsgService.h"
30#include "RooErrorHandler.h"
31#include "RooConvGenContext.h"
32#include "RooAbsAnaConvPdf.h"
33#include "RooNumConvPdf.h"
34#include "RooFFTConvPdf.h"
35#include "RooProdPdf.h"
36#include "RooDataSet.h"
37#include "RooArgSet.h"
38#include "RooTruthModel.h"
39#include "Riostream.h"
40
41
42using namespace std;
43
45;
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Constructor for specialized generator context for analytical convolutions.
50///
51/// Builds a generator for the physics PDF convoluted with the truth model
52/// and a generator for the resolution model as PDF. Events are generated
53/// by sampling events from the p.d.f and smearings from the resolution model
54/// and adding these to obtain a distribution of events consistent with the
55/// convolution of these two. The advantage of this procedure is so that
56/// both p.d.f and resolution model can take advantage of any internal
57/// generators that may be defined.
58
60 const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose) :
61 RooAbsGenContext(model,vars,prototype,auxProto,verbose), _pdfVarsOwned(0), _modelVarsOwned(0)
62{
63 cxcoutI(Generation) << "RooConvGenContext::ctor() setting up special generator context for analytical convolution p.d.f. " << model.GetName()
64 << " for generation of observable(s) " << vars << endl ;
65
66 // Clone PDF and change model to internal truth model
68 if (!_pdfCloneSet) {
69 coutE(Generation) << "RooConvGenContext::RooConvGenContext(" << GetName() << ") Couldn't deep-clone PDF, abort," << endl ;
71 }
72
74 RooTruthModel truthModel("truthModel","Truth resolution model",*pdfClone->convVar()) ;
75 pdfClone->changeModel(truthModel) ;
76 auto convV = dynamic_cast<RooRealVar*>(pdfClone->convVar());
77 if (!convV) {
78 throw std::runtime_error("RooConvGenContext only works with convolution variables of type RooRealVar.");
79 }
80 convV->removeRange();
81
82 // Create generator for physics X truth model
83 _pdfVars = (RooArgSet*) pdfClone->getObservables(&vars) ; ;
84 _pdfGen = pdfClone->genContext(*_pdfVars,prototype,auxProto,verbose) ;
85
86 // Clone resolution model and use as normal PDF
87 _modelCloneSet = (RooArgSet*) RooArgSet(*model._convSet.at(0)).snapshot(kTRUE) ;
88 if (!_modelCloneSet) {
89 coutE(Generation) << "RooConvGenContext::RooConvGenContext(" << GetName() << ") Couldn't deep-clone resolution model, abort," << endl ;
91 }
93 _modelCloneSet->find(model._convSet.at(0)->GetName())->Clone("smearing") ;
94 _modelCloneSet->addOwned(*modelClone) ;
95 modelClone->changeBasis(0) ;
96 convV = dynamic_cast<RooRealVar*>(&modelClone->convVar());
97 if (!convV) {
98 throw std::runtime_error("RooConvGenContext only works with convolution variables of type RooRealVar.");
99 }
100 convV->removeRange();
101
102 // Create generator for resolution model as PDF
103 _modelVars = (RooArgSet*) modelClone->getObservables(&vars) ;
104
105 _modelVars->add(modelClone->convVar()) ;
106 _convVarName = modelClone->convVar().GetName() ;
107 _modelGen = modelClone->genContext(*_modelVars,prototype,auxProto,verbose) ;
108
109 if (prototype) {
110 _pdfVars->add(*prototype->get()) ;
111 _modelVars->add(*prototype->get()) ;
112 }
113
114 // WVE ADD FOR DEBUGGING
115 if (auxProto) {
116 _pdfVars->add(*auxProto) ;
117 _modelVars->add(*auxProto) ;
118 }
119
120// cout << "RooConvGenContext::ctor(" << this << "," << GetName() << ") _pdfVars = " << _pdfVars << " " ; _pdfVars->Print("1") ;
121// cout << "RooConvGenContext::ctor(" << this << "," << GetName() << ") _modelVars = " << _modelVars << " " ; _modelVars->Print("1") ;
122}
123
124
125
126////////////////////////////////////////////////////////////////////////////////
127/// Constructor for specialized generator context for numerical convolutions.
128///
129/// Builds a generator for the physics PDF convoluted with the truth model
130/// and a generator for the resolution model as PDF. Events are generated
131/// by sampling events from the p.d.f and smearings from the resolution model
132/// and adding these to obtain a distribution of events consistent with the
133/// convolution of these two. The advantage of this procedure is so that
134/// both p.d.f and resolution model can take advantage of any internal
135/// generators that may be defined.
136
138 const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose) :
139 RooAbsGenContext(model,vars,prototype,auxProto,verbose)
140{
141 cxcoutI(Generation) << "RooConvGenContext::ctor() setting up special generator context for numeric convolution p.d.f. " << model.GetName()
142 << " for generation of observable(s) " << vars << endl ;
143
144 // Create generator for physics X truth model
147 _pdfGen = ((RooAbsPdf&)model.conv().clonePdf()).genContext(*_pdfVars,prototype,auxProto,verbose) ;
148 _pdfCloneSet = 0 ;
149
150 // Create generator for resolution model as PDF
153 _convVarName = model.conv().cloneVar().GetName() ;
154 _modelGen = ((RooAbsPdf&)model.conv().cloneModel()).genContext(*_modelVars,prototype,auxProto,verbose) ;
156 _modelCloneSet->add(model.conv().cloneModel()) ;
157
158 if (prototype) {
159 _pdfVars->add(*prototype->get()) ;
160 _modelVars->add(*prototype->get()) ;
161 }
162}
163
164
165
166////////////////////////////////////////////////////////////////////////////////
167/// Constructor for specialized generator context for FFT numerical convolutions.
168///
169/// Builds a generator for the physics PDF convoluted with the truth model
170/// and a generator for the resolution model as PDF. Events are generated
171/// by sampling events from the p.d.f and smearings from the resolution model
172/// and adding these to obtain a distribution of events consistent with the
173/// convolution of these two. The advantage of this procedure is so that
174/// both p.d.f and resolution model can take advantage of any internal
175/// generators that may be defined.
176
178 const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose) :
179 RooAbsGenContext(model,vars,prototype,auxProto,verbose)
180{
181 cxcoutI(Generation) << "RooConvGenContext::ctor() setting up special generator context for fft convolution p.d.f. " << model.GetName()
182 << " for generation of observable(s) " << vars << endl ;
183
184 _convVarName = model._x.arg().GetName() ;
185
186 // Create generator for physics model
187 _pdfCloneSet = (RooArgSet*) RooArgSet(model._pdf1.arg()).snapshot(kTRUE) ;
188 RooAbsPdf* pdfClone = (RooAbsPdf*) _pdfCloneSet->find(model._pdf1.arg().GetName()) ;
189 RooRealVar* cvPdf = (RooRealVar*) _pdfCloneSet->find(model._x.arg().GetName()) ;
190 cvPdf->removeRange() ;
191 RooArgSet* tmp1 = pdfClone->getObservables(&vars) ;
194 _pdfGen = pdfClone->genContext(*_pdfVars,prototype,auxProto,verbose) ;
195
196 // Create generator for resolution model
197 _modelCloneSet = (RooArgSet*) RooArgSet(model._pdf2.arg()).snapshot(kTRUE) ;
198 RooAbsPdf* modelClone = (RooAbsPdf*) _modelCloneSet->find(model._pdf2.arg().GetName()) ;
199 RooRealVar* cvModel = (RooRealVar*) _modelCloneSet->find(model._x.arg().GetName()) ;
200 cvModel->removeRange() ;
201 RooArgSet* tmp2 = modelClone->getObservables(&vars) ;
204 _modelGen = modelClone->genContext(*_pdfVars,prototype,auxProto,verbose) ;
205
206 delete tmp1 ;
207 delete tmp2 ;
208
209 if (prototype) {
210 _pdfVars->add(*prototype->get()) ;
211 _modelVars->add(*prototype->get()) ;
212 }
213}
214
215
216
217////////////////////////////////////////////////////////////////////////////////
218/// Destructor
219
221{
222 // Destructor. Delete all owned subgenerator contexts
223 delete _pdfGen ;
224 delete _modelGen ;
225 delete _pdfCloneSet ;
226 delete _modelCloneSet ;
227 delete _modelVars ;
228 delete _pdfVars ;
229 delete _pdfVarsOwned ;
230 delete _modelVarsOwned ;
231}
232
233
234
235////////////////////////////////////////////////////////////////////////////////
236/// Attach given set of arguments to internal clones of
237/// pdf and resolution model
238
240{
241 // Find convolution variable in input and output sets
244
245 // Replace all servers in _pdfVars and _modelVars with those in theEvent, except for the convolution variable
246 RooArgSet* pdfCommon = (RooArgSet*) args.selectCommon(*_pdfVars) ;
247 pdfCommon->remove(*cvPdf,kTRUE,kTRUE) ;
248
249 RooArgSet* modelCommon = (RooArgSet*) args.selectCommon(*_modelVars) ;
250 modelCommon->remove(*cvModel,kTRUE,kTRUE) ;
251
252 _pdfGen->attach(*pdfCommon) ;
253 _modelGen->attach(*modelCommon) ;
254
255 delete pdfCommon ;
256 delete modelCommon ;
257}
258
259
260////////////////////////////////////////////////////////////////////////////////
261/// One-time initialization of generator context, attaches
262/// the context to the supplied event container
263
265{
266 // Find convolution variable in input and output sets
269 _cvOut = (RooRealVar*) theEvent.find(_convVarName) ;
270
271 // Replace all servers in _pdfVars and _modelVars with those in theEvent, except for the convolution variable
272 RooArgSet* pdfCommon = (RooArgSet*) theEvent.selectCommon(*_pdfVars) ;
273 pdfCommon->remove(*_cvPdf,kTRUE,kTRUE) ;
274 _pdfVars->replace(*pdfCommon) ;
275 delete pdfCommon ;
276
277 RooArgSet* modelCommon = (RooArgSet*) theEvent.selectCommon(*_modelVars) ;
278 modelCommon->remove(*_cvModel,kTRUE,kTRUE) ;
279 _modelVars->replace(*modelCommon) ;
280 delete modelCommon ;
281
282 // Initialize component generators
285}
286
287
288
289////////////////////////////////////////////////////////////////////////////////
290/// Generate a single event
291
293{
294 while(1) {
295
296 // Generate pdf and model data
297 _modelGen->generateEvent(*_modelVars,remaining) ;
298 _pdfGen->generateEvent(*_pdfVars,remaining) ;
299
300 // Construct smeared convolution variable
301 Double_t convValSmeared = _cvPdf->getVal() + _cvModel->getVal() ;
302 if (_cvOut->isValidReal(convValSmeared)) {
303 // Smeared value in acceptance range, transfer values to output set
304 theEvent = *_modelVars ;
305 theEvent = *_pdfVars ;
306 _cvOut->setVal(convValSmeared) ;
307 return ;
308 }
309 }
310}
311
312
313
314////////////////////////////////////////////////////////////////////////////////
315/// Set the traversal order for events in the prototype dataset
316/// The argument is a array of integers with a size identical
317/// to the number of events in the prototype dataset. Each element
318/// should contain an integer in the range 1-N.
319
321{
325}
326
327
328////////////////////////////////////////////////////////////////////////////////
329/// Print the details of this generator context
330
332{
334 os << indent << "--- RooConvGenContext ---" << endl ;
335 os << indent << "List of component generators" << endl ;
336
337 TString indent2(indent) ;
338 indent2.Append(" ") ;
339
340 _modelGen->printMultiline(os,content,verbose,indent2);
341 _pdfGen->printMultiline(os,content,verbose,indent2);
342}
#define cxcoutI(a)
Definition: RooMsgService.h:85
#define coutE(a)
Definition: RooMsgService.h:33
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
static void indent(ostringstream &buf, int indent_level)
RooAbsAnaConvPdf is the base class for PDFs that represent a physics model that can be analytically c...
virtual Bool_t changeModel(const RooResolutionModel &newModel)
Change the current resolution model to newModel.
virtual RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t verbose=kFALSE) const
Create a generator context for this p.d.f.
RooAbsRealLValue * convVar()
Retrieve the convolution variable.
RooListProxy _convSet
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Return the observables of this pdf given a set of observables.
Definition: RooAbsArg.h:276
RooAbsCollection * selectCommon(const RooAbsCollection &refColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
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.
virtual void attach(const RooArgSet &params)
Interface to attach given parameters to object in this context.
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
virtual void initGenerator(const RooArgSet &theEvent)
Interface function to initialize context for generation for given set of observables.
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)=0
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:1998
virtual Bool_t isValidReal(Double_t value, Bool_t printError=kFALSE) const
Check if given value is valid.
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:90
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:74
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:126
virtual Bool_t addOwned(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:92
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
RooConvGenContext is an efficient implementation of the generator context specific for RooAbsAnaConvP...
virtual ~RooConvGenContext()
Destructor.
RooArgSet * _modelCloneSet
RooArgSet * _modelVarsOwned
RooConvGenContext(const RooFFTConvPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
Constructor for specialized generator context for FFT numerical convolutions.
RooAbsGenContext * _pdfGen
RooAbsGenContext * _modelGen
RooArgSet * _pdfCloneSet
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print the details of this generator context.
RooArgSet * _pdfVarsOwned
RooRealVar * _cvModel
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
Generate a single event.
virtual void initGenerator(const RooArgSet &theEvent)
One-time initialization of generator context, attaches the context to the supplied event container.
virtual void attach(const RooArgSet &params)
Attach given set of arguments to internal clones of pdf and resolution model.
RooArgSet * _modelVars
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order for events in the prototype dataset The argument is a array of integers with ...
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
static void softAbort()
PDF for the numerical (FFT) convolution of two PDFs.
Definition: RooFFTConvPdf.h:25
RooRealProxy _pdf1
Definition: RooFFTConvPdf.h:69
RooRealProxy _x
Definition: RooFFTConvPdf.h:67
RooRealProxy _pdf2
Definition: RooFFTConvPdf.h:70
Numeric 1-dimensional convolution operator PDF.
Definition: RooNumConvPdf.h:26
RooNumConvolution & conv() const
Definition: RooNumConvPdf.h:63
RooAbsReal & cloneModel() const
RooRealVar & cloneVar() const
RooAbsReal & clonePdf() const
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
void removeRange(const char *name=0)
Remove range limits for binning with given name. Empty name means default range.
Definition: RooRealVar.cxx:409
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:261
RooResolutionModel is the base class for PDFs that represent a resolution model that can be convolute...
virtual void changeBasis(RooFormulaVar *basis)
Change the basis function we convolute with.
RooAbsRealLValue & convVar() const
Return the convolution variable of the resolution model.
const T & arg() const
Return reference to object held in proxy.
RooTruthModel is an implementation of RooResolution model that provides a delta-function resolution m...
Definition: RooTruthModel.h:21
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:74
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:67