Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/RooAbsSelfCachedReal.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Project: RooFit
0003  *
0004  * Copyright (c) 2023, CERN
0005  *
0006  * Redistribution and use in source and binary forms,
0007  * with or without modification, are permitted according to the terms
0008  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
0009  */
0010 
0011 #ifndef RooFit_RooAbsSelfCachedReal_h
0012 #define RooFit_RooAbsSelfCachedReal_h
0013 
0014 /**
0015 \file RooAbsSelfCachedReal.h
0016 \class RooAbsSelfCached
0017 \ingroup Roofitcore
0018 
0019 Abstract base class for functions whose
0020 output is cached in terms of a histogram in all observables between
0021 getVal() and evaluate(). For certain p.d.f.s that are very
0022 expensive to calculate it may be beneficial to implement them as a
0023 RooAbsSelfCached rather than a RooAbsReal/Pdf. Class
0024 RooAbsSelfCached is designed to have its interface identical to
0025 that of RooAbsReal/Pdf, so any p.d.f can make use of its caching
0026 functionality by merely switching its base class.  Existing
0027 RooAbsReal/Pdf objects can also be cached a posteriori with the
0028 RooCachedReal/Pdf wrapper function that takes any RooAbsReal/Pdf object as
0029 input.
0030 **/
0031 
0032 #include <RooAbsCachedPdf.h>
0033 #include <RooAbsCachedReal.h>
0034 #include <RooDataHist.h>
0035 #include <RooHistPdf.h>
0036 #include <RooMsgService.h>
0037 #include <RooRealProxy.h>
0038 
0039 #include <Riostream.h>
0040 
0041 template <class Base_t>
0042 class RooAbsSelfCached : public Base_t {
0043 public:
0044    RooAbsSelfCached() {}
0045    /// Constructor
0046    RooAbsSelfCached(const char *name, const char *title, int ipOrder = 0) : Base_t(name, title, ipOrder) {}
0047 
0048    /// Copy constructor
0049    RooAbsSelfCached(const RooAbsSelfCached &other, const char *name = nullptr) : Base_t(other, name) {}
0050 
0051 protected:
0052    const char *inputBaseName() const override
0053    {
0054       // Use own name as base name for caches
0055       return Base_t::GetName();
0056    }
0057    RooFit::OwningPtr<RooArgSet> actualObservables(const RooArgSet &nset) const override;
0058    RooFit::OwningPtr<RooArgSet> actualParameters(const RooArgSet &nset) const override;
0059    void fillCacheObject(typename Base_t::CacheElem &cache) const override;
0060 
0061 private:
0062    ClassDefOverride(RooAbsSelfCached, 0); // Abstract base class for self-caching functions
0063 };
0064 
0065 using RooAbsSelfCachedReal = RooAbsSelfCached<RooAbsCachedReal>;
0066 using RooAbsSelfCachedPdf = RooAbsSelfCached<RooAbsCachedPdf>;
0067 
0068 ////////////////////////////////////////////////////////////////////////////////
0069 /// Fill cache with sampling of function as defined by the evaluate() implementation
0070 
0071 template <class Base_t>
0072 void RooAbsSelfCached<Base_t>::fillCacheObject(typename Base_t::CacheElem &cache) const
0073 {
0074    RooDataHist &cacheHist = *cache.hist();
0075 
0076    // Make deep clone of self in non-caching mde and attach to dataset observables
0077    RooArgSet cloneSet;
0078    RooArgSet(*this).snapshot(cloneSet, true);
0079    RooAbsSelfCached *clone2 = (RooAbsSelfCached *)cloneSet.find(Base_t::GetName());
0080    clone2->disableCache(true);
0081    clone2->attachDataSet(cacheHist);
0082 
0083    // Iterator over all bins of RooDataHist and fill weights
0084    for (int i = 0; i < cacheHist.numEntries(); i++) {
0085       const RooArgSet *obs = cacheHist.get(i);
0086       double wgt = clone2->getVal(obs);
0087       cacheHist.set(i, wgt, 0.);
0088    }
0089 
0090    cache.setUnitNorm();
0091 }
0092 
0093 ////////////////////////////////////////////////////////////////////////////////
0094 /// Defines observables to be cached, given a set of user defined observables
0095 /// Returns the subset of nset that are observables this p.d.f
0096 
0097 template <class Base_t>
0098 RooFit::OwningPtr<RooArgSet> RooAbsSelfCached<Base_t>::actualObservables(const RooArgSet &nset) const
0099 {
0100    // Make list of servers
0101    RooArgSet serverSet;
0102 
0103    for (auto server : Base_t::servers()) {
0104       serverSet.add(*server);
0105    }
0106 
0107    // Return servers that are in common with given normalization set
0108    return RooFit::OwningPtr<RooArgSet>{serverSet.selectCommon(nset)};
0109 }
0110 
0111 template <>
0112 inline RooFit::OwningPtr<RooArgSet> RooAbsSelfCached<RooAbsCachedPdf>::actualObservables(const RooArgSet & /*nset*/) const
0113 {
0114    // Make list of servers
0115    auto serverSet = new RooArgSet;
0116 
0117    for (auto server : servers()) {
0118       serverSet->add(*server);
0119    }
0120 
0121    // Return servers that are in common with given normalization set.
0122    // For unknown reasons, the original implementation in RooAbsSelfCachedPdf
0123    // skipped the "selectCommon" strep, which is why this is the only method
0124    // that is implemented separately for RooAbsSelfCachedPdf.
0125    return RooFit::OwningPtr<RooArgSet>{serverSet};
0126 }
0127 
0128 ////////////////////////////////////////////////////////////////////////////////
0129 /// Defines parameters on which cache contents depends. Returns
0130 /// subset of variables of self that is not contained in the
0131 /// supplied nset
0132 
0133 template <class Base_t>
0134 RooFit::OwningPtr<RooArgSet> RooAbsSelfCached<Base_t>::actualParameters(const RooArgSet &nset) const
0135 {
0136    // Make list of servers
0137    RooArgSet *serverSet = new RooArgSet;
0138 
0139    for (auto server : Base_t::servers()) {
0140       serverSet->add(*server);
0141    }
0142 
0143    // Remove all given observables from server list
0144    serverSet->remove(nset, true, true);
0145 
0146    return RooFit::OwningPtr<RooArgSet>{serverSet};
0147 }
0148 
0149 #endif