Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAbsSelfCachedReal.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 *
4 * Copyright (c) 2023, CERN
5 *
6 * Redistribution and use in source and binary forms,
7 * with or without modification, are permitted according to the terms
8 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
9 */
10
11#ifndef RooFit_RooAbsSelfCachedReal_h
12#define RooFit_RooAbsSelfCachedReal_h
13
14/**
15\file RooAbsSelfCached.h
16\class RooAbsSelfCached
17\ingroup Roofitcore
18
19Abstract base class for functions whose
20output is cached in terms of a histogram in all observables between
21getVal() and evaluate(). For certain p.d.f.s that are very
22expensive to calculate it may be beneficial to implement them as a
23RooAbsSelfCached rather than a RooAbsReal/Pdf. Class
24RooAbsSelfCached is designed to have its interface identical to
25that of RooAbsReal/Pdf, so any p.d.f can make use of its caching
26functionality by merely switching its base class. Existing
27RooAbsReal/Pdf objects can also be cached a posteriori with the
28RooCachedReal/Pdf wrapper function that takes any RooAbsReal/Pdf object as
29input.
30**/
31
32#include <RooAbsCachedPdf.h>
33#include <RooAbsCachedReal.h>
34#include <RooDataHist.h>
35#include <RooHistPdf.h>
36#include <RooMsgService.h>
37#include <RooRealProxy.h>
38
39#include <Riostream.h>
40
41template <class Base_t>
42class RooAbsSelfCached : public Base_t {
43public:
45 /// Constructor
46 RooAbsSelfCached(const char *name, const char *title, int ipOrder = 0) : Base_t(name, title, ipOrder) {}
47
48 /// Copy constructor
49 RooAbsSelfCached(const RooAbsSelfCached &other, const char *name = nullptr) : Base_t(other, name) {}
50
51protected:
52 const char *inputBaseName() const override
53 {
54 // Use own name as base name for caches
55 return Base_t::GetName();
56 }
59 void fillCacheObject(typename Base_t::CacheElem &cache) const override;
60
61private:
62 ClassDefOverride(RooAbsSelfCached, 0); // Abstract base class for self-caching functions
63};
64
67
68////////////////////////////////////////////////////////////////////////////////
69/// Fill cache with sampling of function as defined by the evaluate() implementation
70
71template <class Base_t>
72void RooAbsSelfCached<Base_t>::fillCacheObject(typename Base_t::CacheElem &cache) const
73{
74 RooDataHist &cacheHist = *cache.hist();
75
76 // Make deep clone of self in non-caching mde and attach to dataset observables
77 RooArgSet cloneSet;
78 RooArgSet(*this).snapshot(cloneSet, true);
79 RooAbsSelfCached *clone2 = (RooAbsSelfCached *)cloneSet.find(Base_t::GetName());
80 clone2->disableCache(true);
81 clone2->attachDataSet(cacheHist);
82
83 // Iterator over all bins of RooDataHist and fill weights
84 for (int i = 0; i < cacheHist.numEntries(); i++) {
85 const RooArgSet *obs = cacheHist.get(i);
86 double wgt = clone2->getVal(obs);
87 cacheHist.set(i, wgt, 0.);
88 }
89
90 cache.setUnitNorm();
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Defines observables to be cached, given a set of user defined observables
95/// Returns the subset of nset that are observables this p.d.f
96
97template <class Base_t>
99{
100 // Make list of servers
101 RooArgSet serverSet;
102
103 for (auto server : Base_t::servers()) {
104 serverSet.add(*server);
105 }
106
107 // Return servers that are in common with given normalization set
108 return RooFit::OwningPtr<RooArgSet>{static_cast<RooArgSet *>(serverSet.selectCommon(nset))};
109}
110
111template <>
113{
114 // Make list of servers
115 auto serverSet = new RooArgSet;
116
117 for (auto server : servers()) {
118 serverSet->add(*server);
119 }
120
121 // Return servers that are in common with given normalization set.
122 // For unknown reasons, the original implementation in RooAbsSelfCachedPdf
123 // skipped the "selectCommon" strep, which is why this is the only method
124 // that is implemented separately for RooAbsSelfCachedPdf.
125 return RooFit::OwningPtr<RooArgSet>{serverSet};
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// Defines parameters on which cache contents depends. Returns
130/// subset of variables of self that is not contained in the
131/// supplied nset
132
133template <class Base_t>
135{
136 // Make list of servers
137 RooArgSet *serverSet = new RooArgSet;
138
139 for (auto server : Base_t::servers()) {
140 serverSet->add(*server);
141 }
142
143 // Remove all given observables from server list
144 serverSet->remove(nset, true, true);
145
146 return RooFit::OwningPtr<RooArgSet>{serverSet};
147}
148
149#endif
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
char name[80]
Definition TGX11.cxx:110
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
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.
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Abstract base class for functions whose output is cached in terms of a histogram in all observables b...
RooFit::OwningPtr< RooArgSet > actualParameters(const RooArgSet &nset) const override
Defines parameters on which cache contents depends.
const char * inputBaseName() const override
void fillCacheObject(typename Base_t::CacheElem &cache) const override
Fill cache with sampling of function as defined by the evaluate() implementation.
RooAbsSelfCached(const RooAbsSelfCached &other, const char *name=nullptr)
Copy constructor.
RooFit::OwningPtr< RooArgSet > actualObservables(const RooArgSet &nset) const override
Defines observables to be cached, given a set of user defined observables Returns the subset of nset ...
RooAbsSelfCached(const char *name, const char *title, int ipOrder=0)
Constructor.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:39
void set(std::size_t binNumber, double weight, double wgtErr)
Set bin content of bin that was last loaded with get(std::size_t).
const RooArgSet * get() const override
Get bin centre of current bin.
Definition RooDataHist.h:76
T * OwningPtr
An alias for raw pointers for indicating that the return type of a RooFit function is an owning point...
Definition Config.h:35