Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooAbsCachedPdf.cxx
Go to the documentation of this file.
1  /*****************************************************************************
2  * Project: RooFit *
3  * *
4  * Copyright (c) 2000-2005, Regents of the University of California *
5  * and Stanford University. All rights reserved. *
6  * *
7  * Redistribution and use in source and binary forms, *
8  * with or without modification, are permitted according to the terms *
9  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
10  *****************************************************************************/
11 
12 /**
13 \file RooAbsCachedPdf.cxx
14 \class RooAbsCachedPdf
15 \ingroup Roofitcore
16 
17 RooAbsCachedPdf is the abstract base class for p.d.f.s that need or
18 want to cache their evaluate() output in a RooHistPdf defined in
19 terms of the used observables. This base class manages the creation
20 and storage of all RooHistPdf cache p.d.fs and the RooDataHists
21 that define their shape. Implementations of RooAbsCachedPdf must
22 define member function fillCacheObject() which serves to fill an
23 already created RooDataHist with the p.d.fs function values. In
24 addition the member functions actualObservables() and
25 actualParameters() must be define which report what the actual
26 observables to be cached are for a given set of observables passed
27 by the user to getVal() and on which parameters need to be tracked
28 for changes to trigger a refilling of the cache histogram.
29 **/
30 
31 #include "Riostream.h"
32 using namespace std ;
33 
34 #include "RooFit.h"
35 #include "TString.h"
36 #include "RooAbsCachedPdf.h"
37 #include "RooAbsReal.h"
38 #include "RooMsgService.h"
39 #include "RooDataHist.h"
40 #include "RooHistPdf.h"
41 #include "RooGlobalFunc.h"
42 #include "RooRealVar.h"
43 #include "RooChangeTracker.h"
45 
46 ClassImp(RooAbsCachedPdf);
47 
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Constructor
52 
53 RooAbsCachedPdf::RooAbsCachedPdf(const char *name, const char *title, Int_t ipOrder) :
54  RooAbsPdf(name,title),
55  _cacheMgr(this,10),
56  _ipOrder(ipOrder),
57  _disableCache(kFALSE)
58  {
59  }
60 
61 
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Copy constructor
65 
66 RooAbsCachedPdf::RooAbsCachedPdf(const RooAbsCachedPdf& other, const char* name) :
67  RooAbsPdf(other,name),
68  _cacheMgr(other._cacheMgr,this),
69  _ipOrder(other._ipOrder),
70  _disableCache(other._disableCache)
71  {
72  }
73 
74 
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Destructor
78 
79 RooAbsCachedPdf::~RooAbsCachedPdf()
80 {
81 }
82 
83 
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Implementation of getVal() overriding default implementation
87 /// of RooAbsPdf. Return normalized value stored in cache p.d.f
88 /// rather than return value of evaluate() which is undefined
89 /// for RooAbsCachedPdf
90 
91 Double_t RooAbsCachedPdf::getValV(const RooArgSet* nset) const
92 {
93  if (_disableCache) {
94  return RooAbsPdf::getValV(nset) ;
95  }
96 
97  // Calculate current unnormalized value of object
98  PdfCacheElem* cache = getCache(nset) ;
99 
100  Double_t value = cache->pdf()->getVal(nset) ;
101 
102  _value = value ;
103  return _value ;
104 }
105 
106 
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Return pointer to RooHistPdf cache pdf for given choice of observables
110 
111 RooAbsPdf* RooAbsCachedPdf::getCachePdf(const RooArgSet* nset) const
112 {
113  PdfCacheElem* cache = getCache(nset) ;
114 
115  if (cache) {
116  return cache->pdf() ;
117  } else {
118  return 0 ;
119  }
120 }
121 
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Return pointer to RooDataHist cache histogram for given choice of observables
125 
126 RooDataHist* RooAbsCachedPdf::getCacheHist(const RooArgSet* nset) const
127 {
128  PdfCacheElem* cache = getCache(nset) ;
129 
130  if (cache) {
131  return cache->hist() ;
132  } else {
133  return 0 ;
134  }
135 }
136 
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Mark all bins of given cache as unitialized (value -1)
140 
141 void RooAbsCachedPdf::clearCacheObject(PdfCacheElem& cache) const
142 {
143  cache.hist()->setAllWeights(-1) ;
144 }
145 
146 
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Retrieve cache object associated with given choice of observables. If cache object
150 /// does not exist, create and fill and register it on the fly. If recalculate=false
151 /// recalculation of cache contents of existing caches that are marked dirty due to
152 /// dependent parameter changes is suppressed.
153 
154 RooAbsCachedPdf::PdfCacheElem* RooAbsCachedPdf::getCache(const RooArgSet* nset, Bool_t recalculate) const
155 {
156  // Check if this configuration was created becfore
157  Int_t sterileIdx(-1) ;
158  PdfCacheElem* cache = (PdfCacheElem*) _cacheMgr.getObj(nset,0,&sterileIdx) ;
159 
160  // Check if we have a cache histogram in the global expensive object cache
161  if (cache) {
162  if (cache->paramTracker()->hasChanged(kTRUE) && (recalculate || !cache->pdf()->haveUnitNorm()) ) {
163  cxcoutD(Eval) << "RooAbsCachedPdf::getCache(" << GetName() << ") cache " << cache << " pdf "
164  << cache->pdf()->GetName() << " requires recalculation as parameters changed" << endl ;
165  fillCacheObject(*cache) ;
166  cache->pdf()->setValueDirty() ;
167  }
168  return cache ;
169  }
170 
171  // Create and fill cache
172  cache = createCache(nset) ;
173 
174  // Check if we have contents registered already in global expensive object cache
175  RooDataHist* htmp = (RooDataHist*) expensiveObjectCache().retrieveObject(cache->hist()->GetName(),RooDataHist::Class(),cache->paramTracker()->parameters()) ;
176 
177  if (htmp) {
178 
179  cache->hist()->reset() ;
180  cache->hist()->add(*htmp) ;
181 
182  } else {
183 
184  fillCacheObject(*cache) ;
185 
186  RooDataHist* eoclone = new RooDataHist(*cache->hist()) ;
187  eoclone->removeSelfFromDir() ;
188  expensiveObjectCache().registerObject(GetName(),cache->hist()->GetName(),*eoclone,cache->paramTracker()->parameters()) ;
189 
190  }
191 
192 
193  // Store this cache configuration
194  Int_t code = _cacheMgr.setObj(nset,0,((RooAbsCacheElement*)cache),0) ;
195 
196  coutI(Caching) << "RooAbsCachedPdf::getCache(" << GetName() << ") creating new cache " << cache << " with pdf "
197  << cache->pdf()->GetName() << " for nset " << (nset?*nset:RooArgSet()) << " with code " << code ;
198  if (htmp) {
199  ccoutI(Caching) << " from preexisting content." ;
200  }
201  ccoutI(Caching) << endl ;
202 
203  return cache ;
204 }
205 
206 
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Constructor of cache object which owns RooDataHist cache histogram,
211 /// RooHistPdf pdf that represents is shape and RooChangeTracker meta
212 /// object that tracks changes in listed dependent parameter of cache.
213 
214 RooAbsCachedPdf::PdfCacheElem::PdfCacheElem(const RooAbsCachedPdf& self, const RooArgSet* nsetIn) :
215  _pdf(0), _paramTracker(0), _hist(0), _norm(0)
216 {
217  // Create cache object itself -- Default implementation is a RooHistPdf
218  RooArgSet* nset2 = self.actualObservables(nsetIn?*nsetIn:RooArgSet()) ;
219 
220  RooArgSet orderedObs ;
221  if (nset2) {
222  self.preferredObservableScanOrder(*nset2,orderedObs) ;
223  }
224 
225  // Create RooDataHist
226  TString hname = self.GetName() ;
227  hname.Append("_") ;
228  hname.Append(self.inputBaseName()) ;
229  hname.Append("_CACHEHIST") ;
230  hname.Append(self.cacheNameSuffix(orderedObs)) ;
231  hname.Append(self.histNameSuffix()) ;
232  _hist = new RooDataHist(hname,hname,orderedObs,self.binningName()) ;
233  _hist->removeSelfFromDir() ;
234 
235  //RooArgSet* observables= self.getObservables(orderedObs) ;
236  // cout << "orderedObs = " << orderedObs << " observables = " << *observables << endl ;
237 
238  // Get set of p.d.f. observable corresponding to set of histogram observables
239  RooArgSet pdfObs ;
240  RooArgSet pdfFinalObs ;
241  TIterator* iter = orderedObs.createIterator() ;
242  RooAbsArg* harg ;
243  while((harg=(RooAbsArg*)iter->Next())) {
244  RooAbsArg& po = self.pdfObservable(*harg) ;
245  pdfObs.add(po) ;
246  if (po.isFundamental()) {
247  pdfFinalObs.add(po) ;
248  } else {
249  RooArgSet* tmp = po.getVariables() ;
250  pdfFinalObs.add(*tmp) ;
251  delete tmp ;
252  }
253  }
254  delete iter ;
255 
256  // Create RooHistPdf
257  TString pdfname = self.inputBaseName() ;
258  pdfname.Append("_CACHE") ;
259  pdfname.Append(self.cacheNameSuffix(pdfFinalObs)) ;
260  _pdf = new RooHistPdf(pdfname,pdfname,pdfObs,orderedObs,*_hist,self.getInterpolationOrder()) ;
261  if (nsetIn) {
262  _nset.addClone(*nsetIn) ;
263  }
264 
265  // Create pseudo-object that tracks changes in parameter values
266 
267  RooArgSet* params = self.actualParameters(pdfFinalObs) ;
268  params->remove(pdfFinalObs,kTRUE,kTRUE) ;
269 
270  string name= Form("%s_CACHEPARAMS",_pdf->GetName()) ;
271  _paramTracker = new RooChangeTracker(name.c_str(),name.c_str(),*params,kTRUE) ;
272  _paramTracker->hasChanged(kTRUE) ; // clear dirty flag as cache is up-to-date upon creation
273 
274  // Introduce formal dependency of RooHistPdf on parameters so that const optimization code
275  // makes the correct decisions
276  _pdf->addServerList(*params) ;
277 
278  // Set initial state of cache to dirty
279  _pdf->setValueDirty() ;
280 
281  //delete observables ;
282  delete params ;
283  delete nset2 ;
284 
285 }
286 
287 
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 /// Construct string with unique suffix for cache objects based on
291 /// observable names that define cache configuration
292 
293 TString RooAbsCachedPdf::cacheNameSuffix(const RooArgSet& nset) const
294 {
295  TString name ;
296  name.Append("_Obs[") ;
297  if (nset.getSize()>0) {
298  TIterator* iter = nset.createIterator() ;
299  RooAbsArg* arg ;
300  Bool_t first(kTRUE) ;
301  while((arg=(RooAbsArg*)iter->Next())) {
302  if (first) {
303  first=kFALSE ;
304  } else {
305  name.Append(",") ;
306  }
307  name.Append(arg->GetName()) ;
308  }
309  delete iter ;
310  }
311 
312  name.Append("]") ;
313  const char* payloadUS = payloadUniqueSuffix() ;
314  if (payloadUS) {
315  name.Append(payloadUS) ;
316  }
317  return name ;
318 }
319 
320 
321 
322 ////////////////////////////////////////////////////////////////////////////////
323 /// Change the interpolation order that is used in RooHistPdf cache
324 /// representation smoothing the RooDataHist shapes.
325 
326 void RooAbsCachedPdf::setInterpolationOrder(Int_t order)
327 {
328  _ipOrder = order ;
329 
330  Int_t i ;
331  for (i=0 ; i<_cacheMgr.cacheSize() ; i++) {
332  PdfCacheElem* cache = (PdfCacheElem*) _cacheMgr.getObjByIndex(i) ;
333  if (cache) {
334  cache->pdf()->setInterpolationOrder(order) ;
335  }
336  }
337 }
338 
339 
340 
341 ////////////////////////////////////////////////////////////////////////////////
342 /// Returns all RooAbsArg objects contained in the cache element
343 
344 RooArgList RooAbsCachedPdf::PdfCacheElem::containedArgs(Action)
345 {
346  RooArgList ret(*_pdf) ;
347  ret.add(*_paramTracker) ;
348  if (_norm) ret.add(*_norm) ;
349  return ret ;
350 }
351 
352 
353 
354 ////////////////////////////////////////////////////////////////////////////////
355 /// Cache element destructor
356 
357 RooAbsCachedPdf::PdfCacheElem::~PdfCacheElem()
358 {
359  if (_norm) {
360  delete _norm ;
361  }
362  if (_pdf) {
363  delete _pdf ;
364  }
365  if (_paramTracker) {
366  delete _paramTracker ;
367  }
368  if (_hist) {
369  delete _hist ;
370  }
371 }
372 
373 
374 
375 ////////////////////////////////////////////////////////////////////////////////
376 /// Print contents of cache when printing self as part of object tree
377 
378 void RooAbsCachedPdf::PdfCacheElem::printCompactTreeHook(ostream& os, const char* indent, Int_t curElem, Int_t maxElem)
379 {
380  if (curElem==0) {
381  os << indent << "--- RooAbsCachedPdf begin cache ---" << endl ;
382  }
383 
384  TString indent2(indent) ;
385  os << Form("[%d] Configuration for observables ",curElem) << _nset << endl ;
386  indent2 += Form("[%d] ",curElem) ;
387  _pdf->printCompactTree(os,indent2) ;
388  if (_norm) {
389  os << Form("[%d] Norm ",curElem) ;
390  _norm->printStream(os,kName|kArgs,kSingleLine) ;
391  }
392 
393  if (curElem==maxElem) {
394  os << indent << "--- RooAbsCachedPdf end cache --- " << endl ;
395  }
396 }
397 
398 
399 
400 ////////////////////////////////////////////////////////////////////////////////
401 /// Force RooRealIntegral to offer all our actual observable for internal
402 /// integration
403 
404 Bool_t RooAbsCachedPdf::forceAnalyticalInt(const RooAbsArg& dep) const
405 {
406  RooArgSet* actObs = actualObservables(dep) ;
407  Bool_t ret = (actObs->getSize()>0) ;
408  delete actObs ;
409  return ret ;
410 }
411 
412 
413 
414 ////////////////////////////////////////////////////////////////////////////////
415 /// Advertises internal (analytical) integration capabilities. Call
416 /// is forwarded to RooHistPdf cache p.d.f of cache that is used for
417 /// given choice of observables
418 
419 Int_t RooAbsCachedPdf::getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName) const
420 {
421  if (allVars.getSize()==0) {
422  return 0 ;
423  }
424 
425  PdfCacheElem* cache = getCache(normSet?normSet:&allVars) ;
426  Int_t code = cache->pdf()->getAnalyticalIntegralWN(allVars,analVars,normSet,rangeName) ;
427 
428  if (code==0) {
429  return 0 ;
430  }
431 
432  RooArgSet* all = new RooArgSet ;
433  RooArgSet* ana = new RooArgSet ;
434  RooArgSet* nrm = new RooArgSet ;
435  all->addClone(allVars) ;
436  ana->addClone(analVars) ;
437  if (normSet) {
438  nrm->addClone(*normSet) ;
439  }
440  std::vector<Int_t> codeList(2);
441  codeList[0] = code ;
442  codeList[1] = cache->pdf()->haveUnitNorm() ? 1 : 0 ;
443  Int_t masterCode = _anaReg.store(codeList,all,ana,nrm)+1 ; // takes ownership of all sets
444 
445 
446  // Mark all observables as internally integrated
447  if (cache->pdf()->haveUnitNorm()) {
448  analVars.add(allVars,kTRUE) ;
449  }
450 
451  return masterCode ;
452 }
453 
454 
455 
456 ////////////////////////////////////////////////////////////////////////////////
457 /// Implements internal (analytical) integration capabilities. Call
458 /// is forwarded to RooHistPdf cache p.d.f of cache that is used for
459 /// given choice of observables
460 
461 Double_t RooAbsCachedPdf::analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const
462 {
463  if (code==0) {
464  return getVal(normSet) ;
465  }
466 
467  RooArgSet *allVars(0),*anaVars(0),*normSet2(0),*dummy(0) ;
468  const std::vector<Int_t> codeList = _anaReg.retrieve(code-1,allVars,anaVars,normSet2,dummy) ;
469 
470  PdfCacheElem* cache = getCache(normSet2?normSet2:anaVars,kFALSE) ;
471  Double_t ret = cache->pdf()->analyticalIntegralWN(codeList[0],normSet,rangeName) ;
472 
473  if (codeList[1]>0) {
474  RooArgSet factObs(*allVars) ;
475  factObs.remove(*anaVars,kTRUE,kTRUE) ;
476  TIterator* iter = factObs.createIterator() ;
477  RooAbsLValue* arg ;
478  while((arg=dynamic_cast<RooAbsLValue*>(iter->Next()))) {
479  ret *= arg->volume(rangeName) ;
480  }
481  delete iter ;
482  }
483 
484  return ret ;
485 }
486 
487 
488 
489 
490