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 
22 RooConvGenContext is an efficient implementation of the generator context
23 specific for RooAbsAnaConvPdf objects. The physics model is generated
24 with a truth resolution model and the requested resolution model is generated
25 separately as a PDF. The convolution variable of the physics model is
26 subsequently 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 
42 using 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 
73  RooAbsAnaConvPdf* pdfClone = (RooAbsAnaConvPdf*) _pdfCloneSet->find(model.GetName()) ;
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  }
92  RooResolutionModel* modelClone = (RooResolutionModel*)
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
145  _pdfVarsOwned = (RooArgSet*) model.conv().clonePdf().getObservables(&vars)->snapshot(kTRUE) ;
147  _pdfGen = ((RooAbsPdf&)model.conv().clonePdf()).genContext(*_pdfVars,prototype,auxProto,verbose) ;
148  _pdfCloneSet = 0 ;
149 
150  // Create generator for resolution model as PDF
151  _modelVarsOwned = (RooArgSet*) model.conv().cloneModel().getObservables(&vars)->snapshot(kTRUE) ;
153  _convVarName = model.conv().cloneVar().GetName() ;
154  _modelGen = ((RooAbsPdf&)model.conv().cloneModel()).genContext(*_modelVars,prototype,auxProto,verbose) ;
155  _modelCloneSet = new RooArgSet ;
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) ;
192  _pdfVarsOwned = (RooArgSet*) tmp1->snapshot(kTRUE) ;
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 {
324  _pdfGen->setProtoDataOrder(lut) ;
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 }
RooConvGenContext::RooConvGenContext
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.
Definition: RooConvGenContext.cxx:177
RooErrorHandler::softAbort
static void softAbort()
Definition: RooErrorHandler.h:44
RooTruthModel.h
RooAbsPdf::genContext
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:2005
RooAbsAnaConvPdf::convVar
RooAbsRealLValue * convVar()
Retrieve the convolution variable.
Definition: RooAbsAnaConvPdf.cxx:312
RooRealVar::setVal
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:216
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooMsgService.h
RooConvGenContext::_pdfVars
RooArgSet * _pdfVars
Definition: RooConvGenContext.h:59
RooConvGenContext::_cvModel
RooRealVar * _cvModel
Definition: RooConvGenContext.h:63
RooResolutionModel::convVar
RooAbsRealLValue & convVar() const
Return the convolution variable of the resolution model.
Definition: RooResolutionModel.h:43
RooArgSet.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooAbsAnaConvPdf::changeModel
virtual Bool_t changeModel(const RooResolutionModel &newModel)
Change the current resolution model to newModel.
Definition: RooAbsAnaConvPdf.cxx:208
RooConvGenContext::_cvPdf
RooRealVar * _cvPdf
Definition: RooConvGenContext.h:64
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooConvGenContext::_pdfGen
RooAbsGenContext * _pdfGen
Definition: RooConvGenContext.h:54
RooConvGenContext::attach
virtual void attach(const RooArgSet &params)
Attach given set of arguments to internal clones of pdf and resolution model.
Definition: RooConvGenContext.cxx:239
RooAbsCollection::remove
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
Definition: RooAbsCollection.cxx:584
RooConvGenContext::_pdfCloneSet
RooArgSet * _pdfCloneSet
Definition: RooConvGenContext.h:61
RooAbsCollection::find
RooAbsArg * find(const char *name) const
Find object with given name in list.
Definition: RooAbsCollection.cxx:813
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
RooArgSet::add
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
TString
Definition: TString.h:136
RooDataSet.h
bool
RooDataSet::get
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:1038
RooNumConvPdf.h
RooArgSet::addOwned
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
RooConvGenContext::_modelGen
RooAbsGenContext * _modelGen
Definition: RooConvGenContext.h:55
RooConvGenContext::_modelCloneSet
RooArgSet * _modelCloneSet
Definition: RooConvGenContext.h:62
RooAbsGenContext::setProtoDataOrder
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order of prototype data to that in the lookup tables passed as argument.
Definition: RooAbsGenContext.cxx:348
RooAbsGenContext::initGenerator
virtual void initGenerator(const RooArgSet &theEvent)
Interface function to initialize context for generation for given set of observables.
Definition: RooAbsGenContext.cxx:273
RooAbsGenContext::printMultiline
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
Definition: RooAbsGenContext.cxx:335
RooArgSet::snapshot
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:134
RooConvGenContext::printMultiline
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print the details of this generator context.
Definition: RooConvGenContext.cxx:331
RooConvGenContext::_convVarName
TString _convVarName
Definition: RooConvGenContext.h:56
RooResolutionModel
Definition: RooResolutionModel.h:26
RooProdPdf.h
RooConvGenContext::_modelVarsOwned
RooArgSet * _modelVarsOwned
Definition: RooConvGenContext.h:58
RooAbsAnaConvPdf.h
RooFit::Generation
@ Generation
Definition: RooGlobalFunc.h:67
RooConvGenContext
Definition: RooConvGenContext.h:31
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
TNamed::Clone
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:74
RooAbsCollection::selectCommon
RooAbsCollection * selectCommon(const RooAbsCollection &refColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
Definition: RooAbsCollection.cxx:700
RooAbsCollection::replace
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
Definition: RooAbsCollection.cxx:533
RooAbsGenContext
Definition: RooAbsGenContext.h:26
RooConvGenContext::generateEvent
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
Generate a single event.
Definition: RooConvGenContext.cxx:292
RooConvGenContext::_cvOut
RooRealVar * _cvOut
Definition: RooConvGenContext.h:65
RooConvGenContext::initGenerator
virtual void initGenerator(const RooArgSet &theEvent)
One-time initialization of generator context, attaches the context to the supplied event container.
Definition: RooConvGenContext.cxx:264
RooConvGenContext::~RooConvGenContext
virtual ~RooConvGenContext()
Destructor.
Definition: RooConvGenContext.cxx:220
Double_t
double Double_t
Definition: RtypesCore.h:59
RooFFTConvPdf
PDF for the numerical (FFT) convolution of two PDFs.
Definition: RooFFTConvPdf.h:25
RooAbsArg::getObservables
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Return the observables of this pdf given a set of observables.
Definition: RooAbsArg.h:294
RooConvGenContext::_modelVars
RooArgSet * _modelVars
Definition: RooConvGenContext.h:60
RooRealVar::removeRange
void removeRange(const char *name=0)
Remove range limits for binning with given name. Empty name means default range.
Definition: RooRealVar.cxx:358
RooNumConvPdf
Definition: RooNumConvPdf.h:26
RooFFTConvPdf.h
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooErrorHandler.h
RooDataSet
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooResolutionModel::changeBasis
virtual void changeBasis(RooFormulaVar *basis)
Change the basis function we convolute with.
Definition: RooResolutionModel.cxx:196
RooAbsPdf
Definition: RooAbsPdf.h:40
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooAbsRealLValue::isValidReal
virtual Bool_t isValidReal(Double_t value, Bool_t printError=kFALSE) const
Check if given value is valid.
Definition: RooAbsRealLValue.cxx:124
RooRealVar
Definition: RooRealVar.h:36
RooConvGenContext::setProtoDataOrder
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order for events in the prototype dataset The argument is a array of integers with ...
Definition: RooConvGenContext.cxx:320
Riostream.h
cxcoutI
#define cxcoutI(a)
Definition: RooMsgService.h:85
RooAbsGenContext::attach
virtual void attach(const RooArgSet &params)
Interface to attach given parameters to object in this context.
Definition: RooAbsGenContext.cxx:124
RooConvGenContext.h
RooConvGenContext::_pdfVarsOwned
RooArgSet * _pdfVarsOwned
Definition: RooConvGenContext.h:57
RooAbsGenContext::generateEvent
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)=0
RooAbsAnaConvPdf
Definition: RooAbsAnaConvPdf.h:34
RooArgSet
Definition: RooArgSet.h:28
RooTruthModel
Definition: RooTruthModel.h:21
int
RooAbsAnaConvPdf::genContext
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.
Definition: RooAbsAnaConvPdf.cxx:252