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
22RooSimSplitGenContext is an efficient 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
37using namespace RooFit;
38
39#include <iostream>
40#include <string>
41
42using namespace std;
43
45
46
47////////////////////////////////////////////////////////////////////////////////
48/// Constructor of specialized generator context for RooSimultaneous p.d.f.s. This
49/// context creates a dedicated context for each component p.d.f.s and delegates
50/// generation of events to the appropriate component generator context
51
52RooSimSplitGenContext::RooSimSplitGenContext(const RooSimultaneous &model, const RooArgSet &vars, bool verbose, bool autoBinned, const char* binnedTag) :
53 RooAbsGenContext(model,vars,0,0,verbose), _pdf(&model)
54{
55 // Determine if we are requested to generate the index category
56 RooAbsCategoryLValue const& idxCat = model.indexCat();
57 RooArgSet pdfVars(vars) ;
58
59 RooArgSet allPdfVars(pdfVars) ;
60
61 RooArgSet catsAmongAllVars;
62 allPdfVars.selectCommon(model.flattenedCatList(), catsAmongAllVars);
63
64 if(catsAmongAllVars.size() != model.flattenedCatList().size()) {
65 oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: This context must"
66 << " generate all components of the index category" << endl ;
67 _isValid = false ;
68 _numPdf = 0 ;
69 // coverity[UNINIT_CTOR]
70 return ;
71 }
72
73 // We must extended likelihood to determine the relative fractions of the components
74 _idxCatName = idxCat.GetName() ;
75 if (!model.canBeExtended()) {
76 oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << "): All components of the simultaneous PDF "
77 << "must be extended PDFs. Otherwise, it is impossible to calculate the number of events to be generated per component." << endl ;
78 _isValid = false ;
79 _numPdf = 0 ;
80 // coverity[UNINIT_CTOR]
81 return ;
82 }
83
84 // Initialize fraction threshold array (used only in extended mode)
85 _numPdf = model._pdfProxyList.GetSize() ;
86 _fracThresh = new double[_numPdf+1] ;
87 _fracThresh[0] = 0 ;
88
89 // Generate index category and all registered PDFS
90 _allVarsPdf.add(allPdfVars) ;
91 Int_t i(1) ;
92 for(auto * proxy : static_range_cast<RooRealProxy*>(model._pdfProxyList)) {
93 auto pdf = static_cast<RooAbsPdf*>(proxy->absArg());
94
95 // Create generator context for this PDF
96 std::unique_ptr<RooArgSet> compVars{pdf->getObservables(pdfVars)};
97 RooAbsGenContext* cx = pdf->autoGenContext(*compVars,0,0,verbose,autoBinned,binnedTag) ;
98
99 const auto state = idxCat.lookupIndex(proxy->name());
100
101 cx->SetName(proxy->name()) ;
102 _gcList.push_back(cx) ;
103 _gcIndex.push_back(state);
104
105 // Fill fraction threshold array
106 _fracThresh[i] = _fracThresh[i-1] + pdf->expectedEvents(&allPdfVars) ;
107 i++ ;
108 }
109
110 for(i=0 ; i<_numPdf ; i++) {
112 }
113
114 // Clone the index category
115 _idxCatSet = (RooArgSet*) RooArgSet(model.indexCat()).snapshot(true) ;
116 if (!_idxCatSet) {
117 oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << ") Couldn't deep-clone index category, abort," << endl ;
118 throw std::string("RooSimSplitGenContext::RooSimSplitGenContext() Couldn't deep-clone index category, abort") ;
119 }
120
121 _idxCat = static_cast<RooAbsCategoryLValue*>(_idxCatSet->find(model.indexCat().GetName()));
122}
123
124
125
126////////////////////////////////////////////////////////////////////////////////
127/// Destructor. Delete all owned subgenerator contexts
128
130{
131 delete[] _fracThresh ;
132 delete _idxCatSet ;
133 for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
134 delete (*iter) ;
135 }
136}
137
138
139
140////////////////////////////////////////////////////////////////////////////////
141/// Attach the index category clone to the given event buffer
142
144{
145 if (_idxCat->isDerived()) {
147 }
148
149 // Forward initGenerator call to all components
150 for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
151 (*iter)->attach(args) ;
152 }
153
154}
155
156
157////////////////////////////////////////////////////////////////////////////////
158/// Perform one-time initialization of generator context
159
161{
162 // Attach the index category clone to the event
163 if (_idxCat->isDerived()) {
165 } else {
167 }
168
169 // Forward initGenerator call to all components
170 for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
171 (*iter)->initGenerator(theEvent) ;
172 }
173
174}
175
176
177
178////////////////////////////////////////////////////////////////////////////////
179
180RooDataSet* RooSimSplitGenContext::generate(double nEvents, bool skipInit, bool extendedMode)
181{
182 if(!isValid()) {
183 coutE(Generation) << ClassName() << "::" << GetName() << ": context is not valid" << endl;
184 return 0;
185 }
186
187
188 // Calculate the expected number of events if necessary
189 if(nEvents <= 0) {
190 nEvents= _expectedEvents;
191 }
192 coutI(Generation) << ClassName() << "::" << GetName() << ":generate: will generate "
193 << nEvents << " events" << endl;
194
195 if (_verbose) Print("v") ;
196
197 // Perform any subclass implementation-specific initialization
198 // Can be skipped if this is a rerun with an identical configuration
199 if (!skipInit) {
201 }
202
203 // Generate lookup table from expected event counts
204 vector<double> nGen(_numPdf) ;
205 if (extendedMode ) {
206 Int_t i(0) ;
207 for(auto * proxy : static_range_cast<RooRealProxy*>(_pdf->_pdfProxyList)) {
208 RooAbsPdf* pdf=(RooAbsPdf*)proxy->absArg() ;
209 //nGen[i] = Int_t(pdf->expectedEvents(&_allVarsPdf)+0.5) ;
210 nGen[i] = pdf->expectedEvents(&_allVarsPdf) ;
211 i++ ;
212 }
213
214 } else {
215 Int_t i(1) ;
216 _fracThresh[0] = 0 ;
217 for(auto * proxy : static_range_cast<RooRealProxy*>(_pdf->_pdfProxyList)) {
218 RooAbsPdf* pdf=(RooAbsPdf*)proxy->absArg() ;
220 i++ ;
221 }
222 for(i=0 ; i<_numPdf ; i++) {
224 }
225
226 // Determine from that total number of events to be generated for each component
227 double nGenSoFar(0) ;
228 while (nGenSoFar<nEvents) {
229 double rand = RooRandom::uniform() ;
230 i=0 ;
231 for (i=0 ; i<_numPdf ; i++) {
232 if (rand>_fracThresh[i] && rand<_fracThresh[i+1]) {
233 nGen[i]++ ;
234 nGenSoFar++ ;
235 break ;
236 }
237 }
238 }
239 }
240
241
242
243 // Now loop over states
244 map<string,RooAbsData*> dataMap ;
245 Int_t icomp(0) ;
246 for(auto * proxy : static_range_cast<RooRealProxy*>(_pdf->_pdfProxyList)) {
247
248 // Calculate number of events to generate for this state
249 if (_gcList[icomp]) {
250 dataMap[proxy->GetName()] = _gcList[icomp]->generate(nGen[icomp],skipInit,extendedMode) ;
251 }
252
253 icomp++ ;
254 }
255
256 // Put all datasets together in a composite-store RooDataSet that links and owns the component datasets
257 RooDataSet* hmaster = new RooDataSet("hmaster","hmaster",_allVarsPdf,RooFit::Index((RooCategory&)*_idxCat),RooFit::Link(dataMap),RooFit::OwnLinked()) ;
258 return hmaster ;
259}
260
261
262
263////////////////////////////////////////////////////////////////////////////////
264/// Forward to components
265
267{
268 for(auto * elem : _gcList) {
269 elem->setExpectedData(flag) ;
270 }
271}
272
273
274
275////////////////////////////////////////////////////////////////////////////////
276/// this method is empty because it is not used by this context
277
278RooDataSet* RooSimSplitGenContext::createDataSet(const char* , const char* , const RooArgSet& )
279{
280 return nullptr;
281}
282
283
284
285////////////////////////////////////////////////////////////////////////////////
286/// this method is empty because it is not used in this type of context
287
289{
290 assert(0) ;
291}
292
293
294
295
296////////////////////////////////////////////////////////////////////////////////
297/// this method is empty because proto datasets are not supported by this context
298
300{
301 assert(0) ;
302}
303
304
305////////////////////////////////////////////////////////////////////////////////
306/// Detailed printing interface
307
308void RooSimSplitGenContext::printMultiline(ostream &os, Int_t content, bool verbose, TString indent) const
309{
310 RooAbsGenContext::printMultiline(os,content,verbose,indent) ;
311 os << indent << "--- RooSimSplitGenContext ---" << endl ;
312 os << indent << "Using PDF ";
314}
#define coutI(a)
#define oocoutE(o, a)
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
static void indent(ostringstream &buf, int indent_level)
bool recursiveRedirectServers(const RooAbsCollection &newServerList, bool mustReplaceAll=false, bool nameChange=false, bool recurseInNewSet=true)
Recursively replace all servers with the new servers in newSet.
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
virtual bool isDerived() const
Does value or shape of this arg depend on any other arg?
Definition RooAbsArg.h:94
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
bool selectCommon(const RooAbsCollection &refColl, RooAbsCollection &outColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
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.
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
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:278
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooCategory is an object to represent discrete states.
Definition RooCategory.h:28
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:57
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:81
RooSimSplitGenContext is an efficient implementation of the generator context specific for RooSimulta...
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.
double * _fracThresh
fraction thresholds
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.
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.
RooArgSet * _idxCatSet
Owner of index category components.
RooAbsCategoryLValue * _idxCat
Clone of index category.
TString _idxCatName
Name of index category.
RooSimultaneous 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:47
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
Basic string class.
Definition TString.h:139
RooCmdArg OwnLinked()
RooCmdArg Index(RooCategory &icat)
RooCmdArg Link(const char *state, RooAbsData &data)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18