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