Logo ROOT  
Reference Guide
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 
22 RooSimSplitGenContext is an efficient implementation of the generator context
23 specific for RooSimultaneous PDFs when generating more than one of the
24 component pdfs.
25 **/
26 
27 #include "RooFit.h"
28 
29 #include "RooSimSplitGenContext.h"
30 #include "RooSimultaneous.h"
31 #include "RooRealProxy.h"
32 #include "RooDataSet.h"
33 #include "Roo1DTable.h"
34 #include "RooCategory.h"
35 #include "RooMsgService.h"
36 #include "RooRandom.h"
37 #include "RooGlobalFunc.h"
38 
39 using namespace RooFit;
40 
41 #include <iostream>
42 #include <string>
43 
44 using namespace std;
45 
47 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Constructor of specialized generator context for RooSimultaneous p.d.f.s. This
51 /// context creates a dedicated context for each component p.d.f.s and delegates
52 /// generation of events to the appropriate component generator context
53 
54 RooSimSplitGenContext::RooSimSplitGenContext(const RooSimultaneous &model, const RooArgSet &vars, Bool_t verbose, Bool_t autoBinned, const char* binnedTag) :
55  RooAbsGenContext(model,vars,0,0,verbose), _pdf(&model)
56 {
57  // Determine if we are requested to generate the index category
58  RooAbsCategory *idxCat = (RooAbsCategory*) model._indexCat.absArg() ;
59  RooArgSet pdfVars(vars) ;
60 
61  RooArgSet allPdfVars(pdfVars) ;
62 
63  if (!idxCat->isDerived()) {
64  pdfVars.remove(*idxCat,kTRUE,kTRUE) ;
65  Bool_t doGenIdx = allPdfVars.find(idxCat->GetName())?kTRUE:kFALSE ;
66 
67  if (!doGenIdx) {
68  oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: This context must"
69  << " generate the index category" << endl ;
70  _isValid = kFALSE ;
71  _numPdf = 0 ;
72  // coverity[UNINIT_CTOR]
73  return ;
74  }
75  } else {
76  TIterator* sIter = idxCat->serverIterator() ;
77  RooAbsArg* server ;
78  Bool_t anyServer(kFALSE), allServers(kTRUE) ;
79  while((server=(RooAbsArg*)sIter->Next())) {
80  if (vars.find(server->GetName())) {
81  anyServer=kTRUE ;
82  pdfVars.remove(*server,kTRUE,kTRUE) ;
83  } else {
84  allServers=kFALSE ;
85  }
86  }
87  delete sIter ;
88 
89  if (anyServer && !allServers) {
90  oocoutE(_pdf,Generation) << "RooSimSplitGenContext::ctor(" << GetName() << ") ERROR: This context must"
91  << " generate all components of a derived index category" << endl ;
92  _isValid = kFALSE ;
93  _numPdf = 0 ;
94  // coverity[UNINIT_CTOR]
95  return ;
96  }
97  }
98 
99  // We must extended likelihood to determine the relative fractions of the components
100  _idxCatName = idxCat->GetName() ;
101  if (!model.canBeExtended()) {
102  oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << "): All components of the simultaneous PDF "
103  << "must be extended PDFs. Otherwise, it is impossible to calculate the number of events to be generated per component." << endl ;
104  _isValid = kFALSE ;
105  _numPdf = 0 ;
106  // coverity[UNINIT_CTOR]
107  return ;
108  }
109 
110  // Initialize fraction threshold array (used only in extended mode)
111  _numPdf = model._pdfProxyList.GetSize() ;
112  _fracThresh = new Double_t[_numPdf+1] ;
113  _fracThresh[0] = 0 ;
114 
115  // Generate index category and all registered PDFS
116  _proxyIter = model._pdfProxyList.MakeIterator() ;
117  _allVarsPdf.add(allPdfVars) ;
118  RooRealProxy* proxy ;
119  RooAbsPdf* pdf ;
120  Int_t i(1) ;
121  while((proxy=(RooRealProxy*)_proxyIter->Next())) {
122  pdf=(RooAbsPdf*)proxy->absArg() ;
123 
124  // Create generator context for this PDF
125  RooArgSet* compVars = pdf->getObservables(pdfVars) ;
126  RooAbsGenContext* cx = pdf->autoGenContext(*compVars,0,0,verbose,autoBinned,binnedTag) ;
127  delete compVars ;
128 
129  const auto state = idxCat->lookupIndex(proxy->name());
130 
131  cx->SetName(proxy->name()) ;
132  _gcList.push_back(cx) ;
133  _gcIndex.push_back(state);
134 
135  // Fill fraction threshold array
136  _fracThresh[i] = _fracThresh[i-1] + pdf->expectedEvents(&allPdfVars) ;
137  i++ ;
138  }
139 
140  for(i=0 ; i<_numPdf ; i++) {
142  }
143 
144  // Clone the index category
145  _idxCatSet = (RooArgSet*) RooArgSet(model._indexCat.arg()).snapshot(kTRUE) ;
146  if (!_idxCatSet) {
147  oocoutE(_pdf,Generation) << "RooSimSplitGenContext::RooSimSplitGenContext(" << GetName() << ") Couldn't deep-clone index category, abort," << endl ;
148  throw std::string("RooSimSplitGenContext::RooSimSplitGenContext() Couldn't deep-clone index category, abort") ;
149  }
150 
151  _idxCat = (RooAbsCategoryLValue*) _idxCatSet->find(model._indexCat.arg().GetName()) ;
152 }
153 
154 
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Destructor. Delete all owned subgenerator contexts
158 
160 {
161  delete[] _fracThresh ;
162  delete _idxCatSet ;
163  for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
164  delete (*iter) ;
165  }
166  delete _proxyIter ;
167 }
168 
169 
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Attach the index category clone to the given event buffer
173 
175 {
176  if (_idxCat->isDerived()) {
178  }
179 
180  // Forward initGenerator call to all components
181  for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
182  (*iter)->attach(args) ;
183  }
184 
185 }
186 
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// Perform one-time initialization of generator context
190 
192 {
193  // Attach the index category clone to the event
194  if (_idxCat->isDerived()) {
196  } else {
197  _idxCat = (RooAbsCategoryLValue*) theEvent.find(_idxCat->GetName()) ;
198  }
199 
200  // Forward initGenerator call to all components
201  for (vector<RooAbsGenContext*>::iterator iter = _gcList.begin() ; iter!=_gcList.end() ; ++iter) {
202  (*iter)->initGenerator(theEvent) ;
203  }
204 
205 }
206 
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 
212 {
213  if(!isValid()) {
214  coutE(Generation) << ClassName() << "::" << GetName() << ": context is not valid" << endl;
215  return 0;
216  }
217 
218 
219  // Calculate the expected number of events if necessary
220  if(nEvents <= 0) {
221  nEvents= _expectedEvents;
222  }
223  coutI(Generation) << ClassName() << "::" << GetName() << ":generate: will generate "
224  << nEvents << " events" << endl;
225 
226  if (_verbose) Print("v") ;
227 
228  // Perform any subclass implementation-specific initialization
229  // Can be skipped if this is a rerun with an identical configuration
230  if (!skipInit) {
232  }
233 
234  // Generate lookup table from expected event counts
235  vector<Double_t> nGen(_numPdf) ;
236  if (extendedMode ) {
237  _proxyIter->Reset() ;
238  RooRealProxy* proxy ;
239  Int_t i(0) ;
240  while((proxy=(RooRealProxy*)_proxyIter->Next())) {
241  RooAbsPdf* pdf=(RooAbsPdf*)proxy->absArg() ;
242  //nGen[i] = Int_t(pdf->expectedEvents(&_allVarsPdf)+0.5) ;
243  nGen[i] = pdf->expectedEvents(&_allVarsPdf) ;
244  i++ ;
245  }
246 
247  } else {
248  _proxyIter->Reset() ;
249  RooRealProxy* proxy ;
250  Int_t i(1) ;
251  _fracThresh[0] = 0 ;
252  while((proxy=(RooRealProxy*)_proxyIter->Next())) {
253  RooAbsPdf* pdf=(RooAbsPdf*)proxy->absArg() ;
255  i++ ;
256  }
257  for(i=0 ; i<_numPdf ; i++) {
259  }
260 
261  // Determine from that total number of events to be generated for each component
262  Double_t nGenSoFar(0) ;
263  while (nGenSoFar<nEvents) {
264  Double_t rand = RooRandom::uniform() ;
265  i=0 ;
266  for (i=0 ; i<_numPdf ; i++) {
267  if (rand>_fracThresh[i] && rand<_fracThresh[i+1]) {
268  nGen[i]++ ;
269  nGenSoFar++ ;
270  break ;
271  }
272  }
273  }
274  }
275 
276 
277 
278  // Now loop over states
279  _proxyIter->Reset() ;
280  map<string,RooAbsData*> dataMap ;
281  Int_t icomp(0) ;
282  RooRealProxy* proxy ;
283  while((proxy=(RooRealProxy*)_proxyIter->Next())) {
284 
285  // Calculate number of events to generate for this state
286  if (_gcList[icomp]) {
287  dataMap[proxy->GetName()] = _gcList[icomp]->generate(nGen[icomp],skipInit,extendedMode) ;
288  }
289 
290  icomp++ ;
291  }
292 
293  // Put all datasets together in a composite-store RooDataSet that links and owns the component datasets
294  RooDataSet* hmaster = new RooDataSet("hmaster","hmaster",_allVarsPdf,RooFit::Index((RooCategory&)*_idxCat),RooFit::Link(dataMap),RooFit::OwnLinked()) ;
295  return hmaster ;
296 }
297 
298 
299 
300 ////////////////////////////////////////////////////////////////////////////////
301 /// Forward to components
302 
304 {
305  for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
306  (*iter)->setExpectedData(flag) ;
307  }
308 }
309 
310 
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// this method is empty because it is not used by this context
314 
315 RooDataSet* RooSimSplitGenContext::createDataSet(const char* , const char* , const RooArgSet& )
316 {
317  return 0 ;
318 }
319 
320 
321 
322 ////////////////////////////////////////////////////////////////////////////////
323 /// this method is empty because it is not used in this type of context
324 
326 {
327  assert(0) ;
328 }
329 
330 
331 
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /// this method is empty because proto datasets are not supported by this context
335 
337 {
338  assert(0) ;
339 }
340 
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Detailed printing interface
344 
346 {
348  os << indent << "--- RooSimSplitGenContext ---" << endl ;
349  os << indent << "Using PDF ";
351 }
RooSimSplitGenContext::setExpectedData
virtual void setExpectedData(Bool_t)
Forward to components.
Definition: RooSimSplitGenContext.cxx:303
RooSimSplitGenContext::RooSimSplitGenContext
RooSimSplitGenContext(const RooSimultaneous &model, const RooArgSet &vars, Bool_t _verbose=kFALSE, Bool_t autoBinned=kTRUE, const char *binnedTag="")
Constructor of specialized generator context for RooSimultaneous p.d.f.s.
Definition: RooSimSplitGenContext.cxx:54
RooSimSplitGenContext::_idxCat
RooAbsCategoryLValue * _idxCat
Definition: RooSimSplitGenContext.h:50
RooAbsArg::serverIterator
TIterator * serverIterator() const
Definition: RooAbsArg.h:140
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
RooMsgService.h
RooSimSplitGenContext
RooSimSplitGenContext is an efficient implementation of the generator context specific for RooSimulta...
Definition: RooSimSplitGenContext.h:27
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
RooAbsPdf::autoGenContext
virtual RooAbsGenContext * autoGenContext(const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t verbose=kFALSE, Bool_t autoBinned=kTRUE, const char *binnedTag="") const
Definition: RooAbsPdf.cxx:2067
RooFit.h
RooSimultaneous.h
RooSimSplitGenContext::_pdf
const RooSimultaneous * _pdf
Definition: RooSimSplitGenContext.h:52
RooAbsGenContext::_theEvent
RooArgSet * _theEvent
Definition: RooAbsGenContext.h:78
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooAbsGenContext::_isValid
Bool_t _isValid
Definition: RooAbsGenContext.h:79
RooAbsGenContext::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsGenContext.h:50
RooSimSplitGenContext::generate
virtual RooDataSet * generate(Double_t nEvents=0, Bool_t skipInit=kFALSE, Bool_t extendedMode=kFALSE)
Generate the specified number of events with nEvents>0 and and return a dataset containing the genera...
Definition: RooSimSplitGenContext.cxx:211
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:598
RooAbsCollection::find
RooAbsArg * find(const char *name) const
Find object with given name in list.
Definition: RooAbsCollection.cxx:827
coutI
#define coutI(a)
Definition: RooMsgService.h:30
RooSimSplitGenContext::printMultiline
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Detailed printing interface.
Definition: RooSimSplitGenContext.cxx:345
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
RooSimSplitGenContext.h
RooAbsGenContext::_expectedEvents
UInt_t _expectedEvents
Definition: RooAbsGenContext.h:81
RooAbsArg::recursiveRedirectServers
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Recursively replace all servers with the new servers in newSet.
Definition: RooAbsArg.cxx:1125
oocoutE
#define oocoutE(o, a)
Definition: RooMsgService.h:48
TString
Basic string class.
Definition: TString.h:136
RooSimSplitGenContext::_allVarsPdf
RooArgSet _allVarsPdf
Definition: RooSimSplitGenContext.h:59
RooDataSet.h
RooSimSplitGenContext::_idxCatName
TString _idxCatName
Definition: RooSimSplitGenContext.h:55
bool
TIterator
Iterator abstract base class.
Definition: TIterator.h:30
RooSimSplitGenContext::~RooSimSplitGenContext
virtual ~RooSimSplitGenContext()
Destructor. Delete all owned subgenerator contexts.
Definition: RooSimSplitGenContext.cxx:159
RooSimSplitGenContext::generateEvent
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
this method is empty because it is not used in this type of context
Definition: RooSimSplitGenContext.cxx:325
RooAbsCategory
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
Definition: RooAbsCategory.h:37
RooSimSplitGenContext::_numPdf
Int_t _numPdf
Definition: RooSimSplitGenContext.h:56
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
RooAbsArg::isDerived
virtual Bool_t isDerived() const
Does value or shape of this arg depend on any other arg?
Definition: RooAbsArg.h:92
RooPrintable::kName
@ kName
Definition: RooPrintable.h:33
RooSimSplitGenContext::attach
virtual void attach(const RooArgSet &params)
Attach the index category clone to the given event buffer.
Definition: RooSimSplitGenContext.cxx:174
RooAbsGenContext::_verbose
Bool_t _verbose
Definition: RooAbsGenContext.h:80
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooAbsGenContext::isValid
Bool_t isValid() const
Definition: RooAbsGenContext.h:34
RooFit::Generation
@ Generation
Definition: RooGlobalFunc.h:60
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
RooRandom.h
RooArgSet::add
Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Add element to non-owning set.
Definition: RooArgSet.cxx:261
RooPrintable::kArgs
@ kArgs
Definition: RooPrintable.h:33
RooRealProxy.h
RooAbsGenContext
RooAbsGenContext is the abstract base class for generator contexts of RooAbsPdf objects.
Definition: RooAbsGenContext.h:26
RooCategory.h
TIterator::Next
virtual TObject * Next()=0
RooSimSplitGenContext::_idxCatSet
RooArgSet * _idxCatSet
Definition: RooSimSplitGenContext.h:51
RooSimSplitGenContext::_gcIndex
std::vector< int > _gcIndex
Definition: RooSimSplitGenContext.h:54
RooGlobalFunc.h
TIterator::Reset
virtual void Reset()=0
RooTemplateProxy< RooAbsReal >
RooPrintable::kClassName
@ kClassName
Definition: RooPrintable.h:33
Double_t
double Double_t
Definition: RtypesCore.h:59
RooAbsArg::getObservables
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Given a set of possible observables, return the observables that this PDF depends on.
Definition: RooAbsArg.h:294
Roo1DTable.h
RooCategory
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
RooAbsCategory::lookupIndex
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
Definition: RooAbsCategory.cxx:283
RooSimSplitGenContext::_gcList
std::vector< RooAbsGenContext * > _gcList
Definition: RooSimSplitGenContext.h:53
RooSimSplitGenContext::setProtoDataOrder
virtual void setProtoDataOrder(Int_t *lut)
this method is empty because proto datasets are not supported by this context
Definition: RooSimSplitGenContext.cxx:336
RooFit::OwnLinked
RooCmdArg OwnLinked()
Definition: RooGlobalFunc.cxx:144
RooPrintable::printStream
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,...
Definition: RooPrintable.cxx:75
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooPrintable::kSingleLine
@ kSingleLine
Definition: RooPrintable.h:34
RooDataSet
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooSimSplitGenContext::_fracThresh
Double_t * _fracThresh
Definition: RooSimSplitGenContext.h:57
RooAbsPdf
Definition: RooAbsPdf.h:43
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooFit::Index
RooCmdArg Index(RooCategory &icat)
Definition: RooGlobalFunc.cxx:107
RooSimultaneous
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
Definition: RooSimultaneous.h:37
RooSimSplitGenContext::initGenerator
virtual void initGenerator(const RooArgSet &theEvent)
Perform one-time initialization of generator context.
Definition: RooSimSplitGenContext.cxx:191
RooAbsCategoryLValue
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
Definition: RooAbsCategoryLValue.h:25
RooFit::Link
RooCmdArg Link(const char *state, RooAbsData &data)
Definition: RooGlobalFunc.cxx:135
RooRandom::uniform
static Double_t uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1)
Definition: RooRandom.cxx:83
TObject::ClassName
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
RooSimSplitGenContext::_proxyIter
TIterator * _proxyIter
Definition: RooSimSplitGenContext.h:60
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:33
RooSimSplitGenContext::createDataSet
RooDataSet * createDataSet(const char *name, const char *title, const RooArgSet &obs)
this method is empty because it is not used by this context
Definition: RooSimSplitGenContext.cxx:315
int
RooAbsPdf::expectedEvents
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events from this p.d.f for use in extended likelihood calculations.
Definition: RooAbsPdf.cxx:3386