Logo ROOT   6.07/09
Reference Guide
RooAddGenContext.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 RooAddGenContext.cxx
19 \class RooAddGenContext
20 \ingroup Roofitcore
21 
22 RooAddGenContext is an efficient implementation of the
23 generator context specific for RooAddPdf PDFs. The strategy
24 of RooAddGenContext is to defer generation of each component
25 to a dedicated generator context for that component and to
26 randomly choose one of those context to generate an event,
27 with a probability proportional to its associated coefficient
28 **/
29 
30 
31 #include "RooFit.h"
32 
33 #include "Riostream.h"
34 
35 
36 #include "RooMsgService.h"
37 #include "RooAddGenContext.h"
38 #include "RooAddGenContext.h"
39 #include "RooAddPdf.h"
40 #include "RooDataSet.h"
41 #include "RooRandom.h"
42 #include "RooAddModel.h"
43 
44 using namespace std;
45 
47 ;
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Constructor
52 
54  const RooDataSet *prototype, const RooArgSet* auxProto,
55  Bool_t verbose) :
56  RooAbsGenContext(model,vars,prototype,auxProto,verbose), _isModel(kFALSE)
57 {
58  cxcoutI(Generation) << "RooAddGenContext::ctor() setting up event special generator context for sum p.d.f. " << model.GetName()
59  << " for generation of observable(s) " << vars ;
60  if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
61  if (auxProto && auxProto->getSize()>0) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
62  ccxcoutI(Generation) << endl ;
63 
64  // Constructor. Build an array of generator contexts for each product component PDF
66  _pdf = (RooAddPdf*) _pdfSet->find(model.GetName()) ;
68 
69  // Fix normalization set of this RooAddPdf
70  if (prototype)
71  {
72  RooArgSet coefNSet(vars) ;
73  coefNSet.add(*prototype->get()) ;
75  }
76 
77  model._pdfIter->Reset() ;
78  RooAbsPdf* pdf ;
79  _nComp = model._pdfList.getSize() ;
80  _coefThresh = new Double_t[_nComp+1] ;
81  _vars = (RooArgSet*) vars.snapshot(kFALSE) ;
82 
83  while((pdf=(RooAbsPdf*)model._pdfIter->Next())) {
84  RooAbsGenContext* cx = pdf->genContext(vars,prototype,auxProto,verbose) ;
85  _gcList.push_back(cx) ;
86  }
87 
88  ((RooAddPdf*)_pdf)->getProjCache(_vars) ;
90 
91  _mcache = 0 ;
92  _pcache = 0 ;
93 }
94 
95 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Constructor
99 
101  const RooDataSet *prototype, const RooArgSet* auxProto,
102  Bool_t verbose) :
103  RooAbsGenContext(model,vars,prototype,auxProto,verbose), _isModel(kTRUE)
104 {
105  cxcoutI(Generation) << "RooAddGenContext::ctor() setting up event special generator context for sum resolution model " << model.GetName()
106  << " for generation of observable(s) " << vars ;
107  if (prototype) ccxcoutI(Generation) << " with prototype data for " << *prototype->get() ;
108  if (auxProto && auxProto->getSize()>0) ccxcoutI(Generation) << " with auxiliary prototypes " << *auxProto ;
109  ccxcoutI(Generation) << endl ;
110 
111  // Constructor. Build an array of generator contexts for each product component PDF
112  _pdfSet = (RooArgSet*) RooArgSet(model).snapshot(kTRUE) ;
113  _pdf = (RooAbsPdf*) _pdfSet->find(model.GetName()) ;
114 
115 
116  model._pdfIter->Reset() ;
117  RooAbsPdf* pdf ;
118  _nComp = model._pdfList.getSize() ;
119  _coefThresh = new Double_t[_nComp+1] ;
120  _vars = (RooArgSet*) vars.snapshot(kFALSE) ;
121 
122  while((pdf=(RooAbsPdf*)model._pdfIter->Next())) {
123  RooAbsGenContext* cx = pdf->genContext(vars,prototype,auxProto,verbose) ;
124  _gcList.push_back(cx) ;
125  }
126 
127  ((RooAddModel*)_pdf)->getProjCache(_vars) ;
129 
130  _mcache = 0 ;
131  _pcache = 0 ;
132 }
133 
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Destructor. Delete all owned subgenerator contexts
138 
140 {
141  delete[] _coefThresh ;
142  for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
143  delete *iter ;
144  }
145  delete _vars ;
146  delete _pdfSet ;
147 }
148 
149 
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Attach given set of variables to internal p.d.f. clone
153 
155 {
157 
158  // Forward initGenerator call to all components
159  for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
160  (*iter)->attach(args) ;
161  }
162 }
163 
164 
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// One-time initialization of generator contex. Attach theEvent
168 /// to internal p.d.f clone and forward initialization call to
169 /// the component generators
170 
172 {
173  _pdf->recursiveRedirectServers(theEvent) ;
174 
175  if (_isModel) {
176  RooAddModel* amod = (RooAddModel*) _pdf ;
177  _mcache = amod->getProjCache(_vars) ;
178  } else {
179  RooAddPdf* apdf = (RooAddPdf*) _pdf ;
180  _pcache = apdf->getProjCache(_vars,0,"FULL_RANGE_ADDGENCONTEXT") ;
181  }
182 
183  // Forward initGenerator call to all components
184  for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
185  (*iter)->initGenerator(theEvent) ;
186  }
187 }
188 
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// Randomly choose one of the component contexts to generate this event,
192 /// with a probability proportional to its coefficient
193 
195 {
196  // Throw a random number to determin which component to generate
197  updateThresholds() ;
198  Double_t rand = RooRandom::uniform() ;
199  Int_t i=0 ;
200  for (i=0 ; i<_nComp ; i++) {
201  if (rand>_coefThresh[i] && rand<_coefThresh[i+1]) {
202  _gcList[i]->generateEvent(theEvent,remaining) ;
203  return ;
204  }
205  }
206 }
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Update the cumulative threshold table from the current coefficient
211 /// values
212 
214 {
215  if (_isModel) {
216 
217  RooAddModel* amod = (RooAddModel*) _pdf ;
219 
220  _coefThresh[0] = 0. ;
221  Int_t i ;
222  for (i=0 ; i<_nComp ; i++) {
223  _coefThresh[i+1] = amod->_coefCache[i] ;
224  _coefThresh[i+1] += _coefThresh[i] ;
225  }
226 
227  } else {
228 
229  RooAddPdf* apdf = (RooAddPdf*) _pdf ;
230 
232 
233  _coefThresh[0] = 0. ;
234  Int_t i ;
235  for (i=0 ; i<_nComp ; i++) {
236  _coefThresh[i+1] = apdf->_coefCache[i] ;
237  _coefThresh[i+1] += _coefThresh[i] ;
238 // cout << "RooAddGenContext::updateThresholds(" << GetName() << ") _coefThresh[" << i+1 << "] = " << _coefThresh[i+1] << endl ;
239  }
240 
241  }
242 
243 }
244 
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// Forward the setProtoDataOrder call to the component generator contexts
248 
250 {
252  for (vector<RooAbsGenContext*>::iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
253  (*iter)->setProtoDataOrder(lut) ;
254  }
255 }
256 
257 
258 
259 ////////////////////////////////////////////////////////////////////////////////
260 /// Print the details of the context
261 
263 {
264  RooAbsGenContext::printMultiline(os,content,verbose,indent) ;
265  os << indent << "--- RooAddGenContext ---" << endl ;
266  os << indent << "Using PDF ";
268 
269  os << indent << "List of component generators" << endl ;
270  TString indent2(indent) ;
271  indent2.Append(" ") ;
272  for (vector<RooAbsGenContext*>::const_iterator iter=_gcList.begin() ; iter!=_gcList.end() ; ++iter) {
273  (*iter)->printMultiline(os,content,verbose,indent2) ;
274  }
275 }
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
#define cxcoutI(a)
Definition: RooMsgService.h:84
TIterator * _pdfIter
List of supplemental normalization factors.
Definition: RooAddModel.h:136
virtual void Reset()=0
void updateCoefficients(CacheElem &cache, const RooArgSet *nset) const
Update the coefficient values in the given cache element: calculate new remainder fraction...
Definition: RooAddPdf.cxx:679
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, which is interpreted as an OR of &#39;enum ContentsOptions&#39; values and in the style given by &#39;enum StyleOption&#39;.
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
Randomly choose one of the component contexts to generate this event, with a probability proportional...
virtual void attach(const RooArgSet &params)
Attach given set of variables to internal p.d.f. clone.
RooListProxy _pdfList
Registry of component analytical integration codes.
Definition: RooAddPdf.h:133
virtual ~RooAddGenContext()
Destructor. Delete all owned subgenerator contexts.
CacheElem * getProjCache(const RooArgSet *nset, const RooArgSet *iset=0, const char *rangeName=0) const
Retrieve cache element with for calculation of p.d.f value with normalization set nset and integrated...
Definition: RooAddPdf.cxx:431
Basic string class.
Definition: TString.h:137
Double_t * _coefCache
Definition: RooAddPdf.h:103
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooAddPdf::CacheElem * _pcache
RooAddModel cache element.
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void setProtoDataOrder(Int_t *lut)
Forward the setProtoDataOrder call to the component generator contexts.
STL namespace.
const RooArgSet * _vars
Double_t * _coefThresh
CacheElem * getProjCache(const RooArgSet *nset, const RooArgSet *iset=0, const char *rangeName=0) const
Retrieve cache element with for calculation of p.d.f value with normalization set nset and integrated...
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print the details of the context.
TString & Append(const char *cs)
Definition: TString.h:492
TIterator * _pdfIter
List of supplemental normalization factors.
Definition: RooAddPdf.h:136
RooAbsGenContext is the abstract base class for generator contexts of RooAbsPdf objects.
RooListProxy _pdfList
Registry of component analytical integration codes.
Definition: RooAddModel.h:133
RooAddGenContext(const RooAddPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
Constructor.
virtual void initGenerator(const RooArgSet &theEvent)
One-time initialization of generator contex.
void updateThresholds()
Update the cumulative threshold table from the current coefficient values.
RooAbsArg * find(const char *name) const
Find object with given name in list.
bool verbose
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
static void indent(ostringstream &buf, int indent_level)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooArgSet * _theEvent
#define ClassImp(name)
Definition: Rtypes.h:279
static Double_t uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1)
Definition: RooRandom.cxx:84
double Double_t
Definition: RtypesCore.h:55
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Interface for multi-line printing.
virtual void setProtoDataOrder(Int_t *lut)
Set the traversal order of prototype data to that in the lookup tables passed as argument.
Double_t * _coefCache
Definition: RooAddModel.h:99
RooAddModel::CacheElem * _mcache
void updateCoefficients(CacheElem &cache, const RooArgSet *nset) const
Update the coefficient values in the given cache element: calculate new remainder fraction...
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
#define ccxcoutI(a)
Definition: RooMsgService.h:85
virtual TObject * Next()=0
RooArgSet * _pdfSet
RooAddGenContext is an efficient implementation of the generator context specific for RooAddPdf PDFs...
void setOperMode(OperMode mode, Bool_t recurseADirty=kTRUE)
Change cache operation mode to given mode.
Definition: RooAbsArg.cxx:1754
virtual void fixAddCoefNormalization(const RooArgSet &addNormSet=RooArgSet(), Bool_t force=kTRUE)
Fix the interpretation of the coefficient of any RooAddPdf component in the expression tree headed by...
Int_t getSize() const
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1088
const Bool_t kTRUE
Definition: Rtypes.h:91
std::vector< RooAbsGenContext * > _gcList
return
Definition: HLFactory.cxx:514
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event &#39;index&#39;.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448
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:1638