Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooSimSplitGenContext.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 RooSimSplitGenContext.cxx
19\class RooSimSplitGenContext
20\ingroup Roofitcore
21
22Efficient implementation of the generator context
23specific for RooSimultaneous PDFs when generating more than one of the
24component pdfs.
25**/
26
28#include "RooSimultaneous.h"
29#include "RooRealProxy.h"
30#include "RooDataSet.h"
31#include "Roo1DTable.h"
32#include "RooCategory.h"
33#include "RooMsgService.h"
34#include "RooRandom.h"
35#include "RooGlobalFunc.h"
36
37#include <iostream>
38#include <string>
39
40
41
42////////////////////////////////////////////////////////////////////////////////
43/// Constructor of specialized generator context for RooSimultaneous p.d.f.s. This
44/// context creates a dedicated context for each component p.d.f.s and delegates
45/// generation of events to the appropriate component generator context
46
47RooSimSplitGenContext::RooSimSplitGenContext(const RooSimultaneous &model, const RooArgSet &vars, bool verbose, bool autoBinned, const char* binnedTag) :
48 RooAbsGenContext(model,vars,nullptr,nullptr,verbose), _pdf(&model)
49{
50 // Determine if we are requested to generate the index category
51 RooAbsCategoryLValue const& idxCat = model.indexCat();
52 RooArgSet pdfVars(vars) ;
53
55
57 allPdfVars.selectCommon(model.flattenedCatList(), catsAmongAllVars);
58
59 if(catsAmongAllVars.size() != model.flattenedCatList().size()) {
60 oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: This context must"
61 << " generate all components of the index category" << std::endl ;
62 _isValid = false ;
63 _numPdf = 0 ;
64 // coverity[UNINIT_CTOR]
65 return ;
66 }
67
68 // We must extended likelihood to determine the relative fractions of the components
69 _idxCatName = idxCat.GetName() ;
70 if (!model.canBeExtended()) {
71 oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << "): All components of the simultaneous PDF "
72 << "must be extended PDFs. Otherwise, it is impossible to calculate the number of events to be generated per component." << std::endl ;
73 _isValid = false ;
74 _numPdf = 0 ;
75 // coverity[UNINIT_CTOR]
76 return ;
77 }
78
79 // Initialize fraction threshold array (used only in extended mode)
80 _numPdf = model._pdfProxyList.GetSize() ;
81
82 // Generate index category and all registered PDFS
85 auto pdf = static_cast<RooAbsPdf*>(proxy->absArg());
86
87 // Create generator context for this PDF
88 std::unique_ptr<RooArgSet> compVars{pdf->getObservables(pdfVars)};
89 RooAbsGenContext* cx = pdf->autoGenContext(*compVars,nullptr,nullptr,verbose,autoBinned,binnedTag) ;
90
91 const auto state = idxCat.lookupIndex(proxy->name());
92
93 cx->SetName(proxy->name()) ;
94 _gcList.push_back(cx) ;
95 _gcIndex.push_back(state);
96 }
97
98 // Clone the index category
99 if(RooArgSet(model.indexCat()).snapshot(_idxCatSet, true)) {
100 oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << ") Couldn't deep-clone index category, abort," << std::endl ;
101 throw std::string("RooSimSplitGenContext::RooSimSplitGenContext() Couldn't deep-clone index category, abort") ;
102 }
103 _idxCat = static_cast<RooAbsCategoryLValue*>(_idxCatSet.find(model.indexCat().GetName()));
104}
105
106
107
108////////////////////////////////////////////////////////////////////////////////
109/// Destructor. Delete all owned subgenerator contexts
110
117
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Attach the index category clone to the given event buffer
122
124{
125 if (_idxCat->isDerived()) {
127 }
128
129 // Forward initGenerator call to all components
130 for (RooAbsGenContext *item : _gcList) {
131 item->attach(args) ;
132 }
133
134}
135
136
137////////////////////////////////////////////////////////////////////////////////
138/// Perform one-time initialization of generator context
139
141{
142 // Attach the index category clone to the event
143 if (_idxCat->isDerived()) {
145 } else {
146 _idxCat = static_cast<RooAbsCategoryLValue*>(theEvent.find(_idxCat->GetName())) ;
147 }
148
149 // Forward initGenerator call to all components
150 for (RooAbsGenContext *item : _gcList) {
151 item->initGenerator(theEvent) ;
152 }
153
154}
155
156
157
158////////////////////////////////////////////////////////////////////////////////
159
161{
162 if(!isValid()) {
163 coutE(Generation) << ClassName() << "::" << GetName() << ": context is not valid" << std::endl;
164 return nullptr;
165 }
166
167
168 // Calculate the expected number of events if necessary
169 if(nEvents <= 0) {
170 nEvents= _expectedEvents;
171 }
172 coutI(Generation) << ClassName() << "::" << GetName() << ":generate: will generate "
173 << nEvents << " events" << std::endl;
174
175 if (_verbose) Print("v") ;
176
177 // Perform any subclass implementation-specific initialization
178 // Can be skipped if this is a rerun with an identical configuration
179 if (!skipInit) {
181 }
182
183 // Generate lookup table from expected event counts
184 std::vector<double> nGen(_numPdf);
185 std::vector<double> nExpected;
186 nExpected.reserve(_numPdf) ;
187 double nExpectedTotal = 0.;
188 for(auto * proxy : static_range_cast<RooRealProxy*>(_pdf->_pdfProxyList)) {
189 RooAbsPdf* pdf=static_cast<RooAbsPdf*>(proxy->absArg()) ;
190 nExpected.push_back(pdf->expectedEvents(&_allVarsPdf));
191 nExpectedTotal += nExpected.back();
192 }
193
194 // We don't randomize events in two cases:
195 // 1. When the generation is extended, each component pdf will already
196 // randomize the expected number of events so we don't need to do it here.
197 // 2. If we want to create an expected Asimov dataset.
199 nGen = nExpected;
200 } else {
201 // Determine from that total number of events to be generated for each component
202 double nGenSoFar(0) ;
203 while (nGenSoFar<nEvents) {
205 double cumsum = 0;
206 for (int i=0 ; i<_numPdf ; i++) {
207 if (rand >= cumsum && rand < cumsum + nExpected[i]) {
208 nGen[i]++ ;
209 nGenSoFar++ ;
210 break ;
211 }
212 cumsum += nExpected[i];
213 }
214 }
215 }
216
217
218
219 // Now loop over states
220 std::map<std::string,RooAbsData*> dataMap ;
221 Int_t icomp(0) ;
222 for(auto * proxy : static_range_cast<RooRealProxy*>(_pdf->_pdfProxyList)) {
223
224 // Calculate number of events to generate for this state
225 if (_gcList[icomp]) {
226 dataMap[proxy->GetName()] = _gcList[icomp]->generate(nGen[icomp],skipInit,extendedMode) ;
227 }
228
229 icomp++ ;
230 }
231
232 // Put all datasets together in a composite-store RooDataSet that links and owns the component datasets
234 return hmaster ;
235}
236
237
238
239////////////////////////////////////////////////////////////////////////////////
240/// Forward to components
241
243{
246 elem->setExpectedData(flag) ;
247 }
248}
249
250
251
252////////////////////////////////////////////////////////////////////////////////
253/// this method is empty because it is not used by this context
254
255RooDataSet* RooSimSplitGenContext::createDataSet(const char* , const char* , const RooArgSet& )
256{
257 return nullptr;
258}
259
260
261
262////////////////////////////////////////////////////////////////////////////////
263/// this method is empty because it is not used in this type of context
264
269
270
271
272
273////////////////////////////////////////////////////////////////////////////////
274/// this method is empty because proto datasets are not supported by this context
275
280
281
282////////////////////////////////////////////////////////////////////////////////
283/// Detailed printing interface
284
285void RooSimSplitGenContext::printMultiline(std::ostream &os, Int_t content, bool verbose, TString indent) const
286{
288 os << indent << "--- RooSimSplitGenContext ---" << std::endl ;
289 os << indent << "Using PDF ";
290 _pdf->printStream(os,kName|kArgs|kClassName,kSingleLine,indent);
291}
#define coutI(a)
#define oocoutE(o, a)
#define coutE(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.
bool recursiveRedirectServers(const RooAbsCollection &newSet, bool mustReplaceAll=false, bool nameChange=false, bool recurseInNewSet=true)
Recursively replace all servers with the new servers in newSet.
virtual bool isDerived() const
Does value or shape of this arg depend on any other arg?
Definition RooAbsArg.h:97
Abstract base class for objects that represent a discrete value that can be set from the outside,...
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
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.
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
UInt_t _expectedEvents
Number of expected events from extended p.d.f.
bool _verbose
Verbose messaging?
bool _isValid
Is context in valid state?
bool isValid() const
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
virtual double expectedEvents(const RooArgSet *nset) const
Return expected number of events to be used in calculation of extended likelihood.
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:214
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Object to represent discrete states.
Definition RooCategory.h:28
Container class to hold unbinned data.
Definition RooDataSet.h:32
static double uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1)
Definition RooRandom.cxx:77
void setExpectedData(bool) override
Forward to components.
void generateEvent(RooArgSet &theEvent, Int_t remaining) override
this method is empty because it is not used in this type of context
std::vector< RooAbsGenContext * > _gcList
List of component generator contexts.
RooArgSet _allVarsPdf
All pdf variables.
void attach(const RooArgSet &params) override
Attach the index category clone to the given event buffer.
RooSimSplitGenContext(const RooSimultaneous &model, const RooArgSet &vars, bool _verbose=false, bool autoBinned=true, const char *binnedTag="")
Constructor of specialized generator context for RooSimultaneous p.d.f.s.
void initGenerator(const RooArgSet &theEvent) override
Perform one-time initialization of generator context.
RooDataSet * createDataSet(const char *name, const char *title, const RooArgSet &obs) override
this method is empty because it is not used by this context
Int_t _numPdf
Number of generated PDFs.
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Detailed printing interface.
RooArgSet _idxCatSet
Owner of index category components.
const RooSimultaneous * _pdf
Original PDF.
RooDataSet * generate(double nEvents=0, bool skipInit=false, bool extendedMode=false) override
Generate the specified number of events with nEvents>0 and and return a dataset containing the genera...
std::vector< int > _gcIndex
Index value corresponding to component.
void setProtoDataOrder(Int_t *lut) override
this method is empty because proto datasets are not supported by this context
~RooSimSplitGenContext() override
Destructor. Delete all owned subgenerator contexts.
RooAbsCategoryLValue * _idxCat
Clone of index category.
TString _idxCatName
Name of index category.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
TList _pdfProxyList
List of PDF proxies (named after applicable category state)
RooArgSet const & flattenedCatList() const
Internal utility function to get a list of all category components for this RooSimultaneous.
const RooAbsCategoryLValue & indexCat() const
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
Basic string class.
Definition TString.h:138
RooCmdArg OwnLinked()
RooCmdArg Index(RooCategory &icat)
RooCmdArg Link(const char *state, RooAbsData &data)