Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-19 01:43:36

0001 #ifndef FWCore_Framework_limited_implementors_h
0002 #define FWCore_Framework_limited_implementors_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/Framework
0006 // Class  :     implementors
0007 //
0008 /**\file implementors.h "FWCore/Framework/interface/limited/implementors.h"
0009 
0010  Description: Base classes used to implement the interfaces for the edm::limited::* module  abilities
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Thu, 18 Jul 2013 11:52:34 GMT
0019 //
0020 
0021 // system include files
0022 #include <memory>
0023 #include <mutex>
0024 #include <string>
0025 #include <tuple>
0026 #include <utility>
0027 #include <vector>
0028 
0029 // user include files
0030 #include "FWCore/Common/interface/FWCoreCommonFwd.h"
0031 #include "FWCore/Framework/interface/CacheHandle.h"
0032 #include "FWCore/Framework/interface/Frameworkfwd.h"
0033 #include "FWCore/Framework/interface/InputProcessBlockCacheImpl.h"
0034 #include "FWCore/Framework/interface/LuminosityBlock.h"
0035 #include "FWCore/Utilities/interface/EDGetToken.h"
0036 #include "FWCore/Utilities/interface/StreamID.h"
0037 #include "FWCore/Utilities/interface/ProcessBlockIndex.h"
0038 #include "FWCore/Utilities/interface/RunIndex.h"
0039 #include "FWCore/Utilities/interface/LuminosityBlockIndex.h"
0040 #include "FWCore/Utilities/interface/propagate_const.h"
0041 
0042 // forward declarations
0043 namespace edm {
0044 
0045   namespace limited {
0046     namespace impl {
0047       class EmptyType {
0048       public:
0049         EmptyType(edm::ParameterSet const&) {}
0050       };
0051 
0052       template <typename T, typename C>
0053       class StreamCacheHolder : public virtual T {
0054       public:
0055         StreamCacheHolder(edm::ParameterSet const& iPSet) : T(iPSet) {}
0056         StreamCacheHolder(StreamCacheHolder<T, C> const&) = delete;
0057         StreamCacheHolder<T, C>& operator=(StreamCacheHolder<T, C> const&) = delete;
0058         ~StreamCacheHolder() override {
0059           for (auto c : caches_) {
0060             delete c;
0061           }
0062         }
0063 
0064       protected:
0065         C* streamCache(edm::StreamID iID) const { return caches_[iID.value()]; }
0066 
0067       private:
0068         void preallocStreams(unsigned int iNStreams) final { caches_.resize(iNStreams, static_cast<C*>(nullptr)); }
0069         void doBeginStream_(StreamID id) final { caches_[id.value()] = beginStream(id).release(); }
0070         void doEndStream_(StreamID id) final {
0071           endStream(id);
0072           delete caches_[id.value()];
0073           caches_[id.value()] = nullptr;
0074         }
0075         void doStreamBeginRun_(StreamID id, Run const& rp, EventSetup const& c) final { streamBeginRun(id, rp, c); }
0076         void doStreamEndRun_(StreamID id, Run const& rp, EventSetup const& c) final { streamEndRun(id, rp, c); }
0077         void doStreamBeginLuminosityBlock_(StreamID id, LuminosityBlock const& lbp, EventSetup const& c) final {
0078           streamBeginLuminosityBlock(id, lbp, c);
0079         }
0080         void doStreamEndLuminosityBlock_(StreamID id, LuminosityBlock const& lbp, EventSetup const& c) final {
0081           streamEndLuminosityBlock(id, lbp, c);
0082         }
0083 
0084         virtual std::unique_ptr<C> beginStream(edm::StreamID) const = 0;
0085         virtual void streamBeginRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const {}
0086         virtual void streamBeginLuminosityBlock(edm::StreamID,
0087                                                 edm::LuminosityBlock const&,
0088                                                 edm::EventSetup const&) const {}
0089         virtual void streamEndLuminosityBlock(edm::StreamID,
0090                                               edm::LuminosityBlock const&,
0091                                               edm::EventSetup const&) const {}
0092         virtual void streamEndRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const {}
0093         virtual void endStream(edm::StreamID) const {}
0094 
0095         //When threaded we will have a container for N items whre N is # of streams
0096         std::vector<C*> caches_;
0097       };
0098 
0099       template <typename T, typename... CacheTypes>
0100       class InputProcessBlockCacheHolder : public virtual T {
0101       public:
0102         InputProcessBlockCacheHolder(edm::ParameterSet const& iPSet) : T(iPSet) {}
0103         InputProcessBlockCacheHolder(InputProcessBlockCacheHolder const&) = delete;
0104         InputProcessBlockCacheHolder& operator=(InputProcessBlockCacheHolder const&) = delete;
0105         ~InputProcessBlockCacheHolder() noexcept(false) override {}
0106 
0107         std::tuple<CacheHandle<CacheTypes>...> processBlockCaches(Event const& event) const {
0108           return cacheImpl_.processBlockCaches(event);
0109         }
0110 
0111         template <std::size_t ICacheType, typename DataType, typename Func>
0112         void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& func) {
0113           cacheImpl_.template registerProcessBlockCacheFiller<ICacheType, DataType, Func>(token,
0114                                                                                           std::forward<Func>(func));
0115         }
0116 
0117         template <typename CacheType, typename DataType, typename Func>
0118         void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& func) {
0119           cacheImpl_.template registerProcessBlockCacheFiller<CacheType, DataType, Func>(token,
0120                                                                                          std::forward<Func>(func));
0121         }
0122 
0123         // This is intended for use by Framework unit tests only
0124         unsigned int cacheSize() const { return cacheImpl_.cacheSize(); }
0125 
0126       private:
0127         void doSelectInputProcessBlocks(ProductRegistry const& productRegistry,
0128                                         ProcessBlockHelperBase const& processBlockHelperBase) final {
0129           cacheImpl_.selectInputProcessBlocks(productRegistry, processBlockHelperBase, *this);
0130         }
0131 
0132         void doAccessInputProcessBlock_(ProcessBlock const& pb) final {
0133           cacheImpl_.accessInputProcessBlock(pb);
0134           accessInputProcessBlock(pb);
0135         }
0136 
0137         // Alternate method to access ProcessBlocks without using the caches
0138         // Mostly intended for unit testing, but might have other uses...
0139         virtual void accessInputProcessBlock(ProcessBlock const&) {}
0140 
0141         void clearInputProcessBlockCaches() final { cacheImpl_.clearCaches(); }
0142 
0143         edm::impl::InputProcessBlockCacheImpl<CacheTypes...> cacheImpl_;
0144       };
0145 
0146       template <typename T, typename C>
0147       class RunCacheHolder : public virtual T {
0148       public:
0149         RunCacheHolder(edm::ParameterSet const& iPSet) : T(iPSet) {}
0150         RunCacheHolder(RunCacheHolder<T, C> const&) = delete;
0151         RunCacheHolder<T, C>& operator=(RunCacheHolder<T, C> const&) = delete;
0152         ~RunCacheHolder() noexcept(false) override{};
0153 
0154       protected:
0155         C const* runCache(edm::RunIndex iID) const { return cache_.get(); }
0156 
0157       private:
0158         void doBeginRun_(Run const& rp, EventSetup const& c) final { cache_ = globalBeginRun(rp, c); }
0159         void doEndRun_(Run const& rp, EventSetup const& c) final {
0160           globalEndRun(rp, c);
0161           cache_ = nullptr;  // propagate_const<T> has no reset() function
0162         }
0163 
0164         virtual std::shared_ptr<C> globalBeginRun(edm::Run const&, edm::EventSetup const&) const = 0;
0165         virtual void globalEndRun(edm::Run const&, edm::EventSetup const&) const = 0;
0166         //When threaded we will have a container for N items whre N is # of simultaneous runs
0167         edm::propagate_const<std::shared_ptr<C>> cache_;
0168       };
0169 
0170       template <typename T, typename C>
0171       class LuminosityBlockCacheHolder : public virtual T {
0172       public:
0173         LuminosityBlockCacheHolder(edm::ParameterSet const& iPSet) : T(iPSet) {}
0174         LuminosityBlockCacheHolder(LuminosityBlockCacheHolder<T, C> const&) = delete;
0175         LuminosityBlockCacheHolder<T, C>& operator=(LuminosityBlockCacheHolder<T, C> const&) = delete;
0176         ~LuminosityBlockCacheHolder() noexcept(false) override{};
0177 
0178       protected:
0179         C const* luminosityBlockCache(edm::LuminosityBlockIndex iID) const { return caches_[iID].get(); }
0180 
0181       private:
0182         void preallocLumis(unsigned int iNLumis) final { caches_.reset(new std::shared_ptr<C>[iNLumis]); }
0183 
0184         void doBeginLuminosityBlock_(LuminosityBlock const& lp, EventSetup const& c) final {
0185           caches_[lp.index()] = globalBeginLuminosityBlock(lp, c);
0186         }
0187         void doEndLuminosityBlock_(LuminosityBlock const& lp, EventSetup const& c) final {
0188           globalEndLuminosityBlock(lp, c);
0189           caches_[lp.index()].reset();
0190         }
0191 
0192         virtual std::shared_ptr<C> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0193                                                               edm::EventSetup const&) const = 0;
0194         virtual void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const = 0;
0195         //When threaded we will have a container for N items whre N is # of simultaneous runs
0196         std::unique_ptr<std::shared_ptr<C>[]> caches_;
0197       };
0198 
0199       template <typename T, typename C>
0200       class EndRunSummaryProducer;
0201 
0202       template <typename T, typename C>
0203       class RunSummaryCacheHolder : public virtual T {
0204       public:
0205         RunSummaryCacheHolder(edm::ParameterSet const& iPSet) : T(iPSet) {}
0206         RunSummaryCacheHolder(RunSummaryCacheHolder<T, C> const&) = delete;
0207         RunSummaryCacheHolder<T, C>& operator=(RunSummaryCacheHolder<T, C> const&) = delete;
0208         ~RunSummaryCacheHolder() noexcept(false) override{};
0209 
0210       private:
0211         friend class EndRunSummaryProducer<T, C>;
0212         void doBeginRunSummary_(edm::Run const& rp, EventSetup const& c) final {
0213           cache_ = globalBeginRunSummary(rp, c);
0214         }
0215         void doStreamEndRunSummary_(StreamID id, Run const& rp, EventSetup const& c) final {
0216           //NOTE: in future this will need to be serialized
0217           std::lock_guard<std::mutex> guard(mutex_);
0218           streamEndRunSummary(id, rp, c, cache_.get());
0219         }
0220         void doEndRunSummary_(Run const& rp, EventSetup const& c) final { globalEndRunSummary(rp, c, cache_.get()); }
0221 
0222         virtual std::shared_ptr<C> globalBeginRunSummary(edm::Run const&, edm::EventSetup const&) const = 0;
0223         virtual void streamEndRunSummary(StreamID, edm::Run const&, edm::EventSetup const&, C*) const = 0;
0224 
0225         virtual void globalEndRunSummary(edm::Run const&, edm::EventSetup const&, C*) const = 0;
0226 
0227         //When threaded we will have a container for N items where N is # of simultaneous runs
0228         std::shared_ptr<C> cache_;
0229         std::mutex mutex_;
0230       };
0231 
0232       template <typename T, typename C>
0233       class EndLuminosityBlockSummaryProducer;
0234 
0235       template <typename T, typename C>
0236       class LuminosityBlockSummaryCacheHolder : public virtual T {
0237       public:
0238         LuminosityBlockSummaryCacheHolder(edm::ParameterSet const& iPSet) : T(iPSet) {}
0239         LuminosityBlockSummaryCacheHolder(LuminosityBlockSummaryCacheHolder<T, C> const&) = delete;
0240         LuminosityBlockSummaryCacheHolder<T, C>& operator=(LuminosityBlockSummaryCacheHolder<T, C> const&) = delete;
0241         ~LuminosityBlockSummaryCacheHolder() noexcept(false) override{};
0242 
0243       private:
0244         void preallocLumisSummary(unsigned int iNLumis) final { caches_.reset(new std::shared_ptr<C>[iNLumis]); }
0245 
0246         friend class EndLuminosityBlockSummaryProducer<T, C>;
0247 
0248         void doBeginLuminosityBlockSummary_(edm::LuminosityBlock const& lb, EventSetup const& c) final {
0249           caches_[lb.index()] = globalBeginLuminosityBlockSummary(lb, c);
0250         }
0251 
0252         void doStreamEndLuminosityBlockSummary_(StreamID id, LuminosityBlock const& lb, EventSetup const& c) final {
0253           std::lock_guard<std::mutex> guard(mutex_);
0254           streamEndLuminosityBlockSummary(id, lb, c, caches_[lb.index()].get());
0255         }
0256         void doEndLuminosityBlockSummary_(LuminosityBlock const& lb, EventSetup const& c) final {
0257           globalEndLuminosityBlockSummary(lb, c, caches_[lb.index()].get());
0258           maybeClearCache(lb);
0259         }
0260 
0261         virtual std::shared_ptr<C> globalBeginLuminosityBlockSummary(edm::LuminosityBlock const&,
0262                                                                      edm::EventSetup const&) const = 0;
0263         virtual void streamEndLuminosityBlockSummary(StreamID,
0264                                                      edm::LuminosityBlock const&,
0265                                                      edm::EventSetup const&,
0266                                                      C*) const = 0;
0267 
0268         virtual void globalEndLuminosityBlockSummary(edm::LuminosityBlock const&, edm::EventSetup const&, C*) const = 0;
0269 
0270         virtual void maybeClearCache(LuminosityBlock const& lb) { caches_[lb.index()].reset(); }
0271 
0272         //When threaded we will have a container for N items where N is # of simultaneous Lumis
0273         std::unique_ptr<std::shared_ptr<C>[]> caches_;
0274         std::mutex mutex_;
0275       };
0276 
0277       template <typename T>
0278       class WatchProcessBlock : public virtual T {
0279       public:
0280         WatchProcessBlock(edm::ParameterSet const& iPSet) : T(iPSet) {}
0281         WatchProcessBlock(WatchProcessBlock const&) = delete;
0282         WatchProcessBlock& operator=(WatchProcessBlock const&) = delete;
0283         ~WatchProcessBlock() noexcept(false) override{};
0284 
0285       private:
0286         void doBeginProcessBlock_(ProcessBlock const&) final;
0287         void doEndProcessBlock_(ProcessBlock const&) final;
0288 
0289         virtual void beginProcessBlock(ProcessBlock const&) {}
0290         virtual void endProcessBlock(ProcessBlock const&) {}
0291       };
0292 
0293       template <typename T>
0294       class BeginProcessBlockProducer : public virtual T {
0295       public:
0296         BeginProcessBlockProducer(edm::ParameterSet const& iPSet) : T(iPSet) {}
0297         BeginProcessBlockProducer(BeginProcessBlockProducer const&) = delete;
0298         BeginProcessBlockProducer& operator=(BeginProcessBlockProducer const&) = delete;
0299         ~BeginProcessBlockProducer() noexcept(false) override{};
0300 
0301       private:
0302         void doBeginProcessBlockProduce_(ProcessBlock&) final;
0303 
0304         virtual void beginProcessBlockProduce(edm::ProcessBlock&) = 0;
0305       };
0306 
0307       template <typename T>
0308       class EndProcessBlockProducer : public virtual T {
0309       public:
0310         EndProcessBlockProducer(edm::ParameterSet const& iPSet) : T(iPSet) {}
0311         EndProcessBlockProducer(EndProcessBlockProducer const&) = delete;
0312         EndProcessBlockProducer& operator=(EndProcessBlockProducer const&) = delete;
0313         ~EndProcessBlockProducer() noexcept(false) override{};
0314 
0315       private:
0316         void doEndProcessBlockProduce_(ProcessBlock&) final;
0317 
0318         virtual void endProcessBlockProduce(edm::ProcessBlock&) = 0;
0319       };
0320 
0321       template <typename T>
0322       class BeginRunProducer : public virtual T {
0323       public:
0324         BeginRunProducer(edm::ParameterSet const& iPSet) : T(iPSet) {}
0325         BeginRunProducer(BeginRunProducer const&) = delete;
0326         BeginRunProducer& operator=(BeginRunProducer const&) = delete;
0327         ~BeginRunProducer() noexcept(false) override{};
0328 
0329       private:
0330         void doBeginRunProduce_(Run& rp, EventSetup const& c) final;
0331 
0332         virtual void globalBeginRunProduce(edm::Run&, edm::EventSetup const&) const = 0;
0333       };
0334 
0335       template <typename T>
0336       class EndRunProducer : public virtual T {
0337       public:
0338         EndRunProducer(edm::ParameterSet const& iPSet) : T(iPSet) {}
0339         EndRunProducer(EndRunProducer const&) = delete;
0340         EndRunProducer& operator=(EndRunProducer const&) = delete;
0341         ~EndRunProducer() noexcept(false) override{};
0342 
0343       private:
0344         void doEndRunProduce_(Run& rp, EventSetup const& c) final;
0345 
0346         virtual void globalEndRunProduce(edm::Run&, edm::EventSetup const&) const = 0;
0347       };
0348 
0349       template <typename T, typename C>
0350       class EndRunSummaryProducer : public RunSummaryCacheHolder<T, C> {
0351       public:
0352         EndRunSummaryProducer(edm::ParameterSet const& iPSet) : T(iPSet), RunSummaryCacheHolder<T, C>(iPSet) {}
0353         EndRunSummaryProducer(EndRunSummaryProducer const&) = delete;
0354         EndRunSummaryProducer& operator=(EndRunSummaryProducer const&) = delete;
0355         ~EndRunSummaryProducer() noexcept(false) override{};
0356 
0357       private:
0358         void doEndRunProduce_(Run& rp, EventSetup const& c) final {
0359           globalEndRunProduce(rp, c, RunSummaryCacheHolder<T, C>::cache_.get());
0360         }
0361 
0362         virtual void globalEndRunProduce(edm::Run&, edm::EventSetup const&, C const*) const = 0;
0363       };
0364 
0365       template <typename T>
0366       class BeginLuminosityBlockProducer : public virtual T {
0367       public:
0368         BeginLuminosityBlockProducer(edm::ParameterSet const& iPSet) : T(iPSet) {}
0369         BeginLuminosityBlockProducer(BeginLuminosityBlockProducer const&) = delete;
0370         BeginLuminosityBlockProducer& operator=(BeginLuminosityBlockProducer const&) = delete;
0371         ~BeginLuminosityBlockProducer() noexcept(false) override{};
0372 
0373       private:
0374         void doBeginLuminosityBlockProduce_(LuminosityBlock& lb, EventSetup const& c) final;
0375         virtual void globalBeginLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const = 0;
0376       };
0377 
0378       template <typename T>
0379       class EndLuminosityBlockProducer : public virtual T {
0380       public:
0381         EndLuminosityBlockProducer(edm::ParameterSet const& iPSet) : T(iPSet) {}
0382         EndLuminosityBlockProducer(EndLuminosityBlockProducer const&) = delete;
0383         EndLuminosityBlockProducer& operator=(EndLuminosityBlockProducer const&) = delete;
0384         ~EndLuminosityBlockProducer() noexcept(false) override{};
0385 
0386       private:
0387         void doEndLuminosityBlockProduce_(LuminosityBlock& lb, EventSetup const& c) final;
0388         virtual void globalEndLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const = 0;
0389       };
0390 
0391       template <typename T, typename S>
0392       class EndLuminosityBlockSummaryProducer : public LuminosityBlockSummaryCacheHolder<T, S> {
0393       public:
0394         EndLuminosityBlockSummaryProducer(edm::ParameterSet const& iPSet)
0395             : T(iPSet), LuminosityBlockSummaryCacheHolder<T, S>(iPSet) {}
0396         EndLuminosityBlockSummaryProducer(EndLuminosityBlockSummaryProducer const&) = delete;
0397         EndLuminosityBlockSummaryProducer& operator=(EndLuminosityBlockSummaryProducer const&) = delete;
0398         ~EndLuminosityBlockSummaryProducer() noexcept(false) override{};
0399 
0400       private:
0401         void doEndLuminosityBlockProduce_(LuminosityBlock& lb, EventSetup const& c) final {
0402           globalEndLuminosityBlockProduce(lb, c, LuminosityBlockSummaryCacheHolder<T, S>::caches_[lb.index()].get());
0403           LuminosityBlockSummaryCacheHolder<T, S>::caches_[lb.index()].reset();
0404         }
0405 
0406         virtual void globalEndLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&, S const*) const = 0;
0407 
0408         // Do nothing because the cache is cleared in doEndLuminosityBlockProduce_
0409         void maybeClearCache(LuminosityBlock const&) final {}
0410       };
0411 
0412       template <typename T>
0413       class Accumulator : public virtual T {
0414       public:
0415         Accumulator(edm::ParameterSet const& iPSet) : T(iPSet) {}
0416         Accumulator() = default;
0417         Accumulator(Accumulator const&) = delete;
0418         Accumulator& operator=(Accumulator const&) = delete;
0419         ~Accumulator() noexcept(false) override{};
0420 
0421       private:
0422         bool hasAccumulator() const override { return true; }
0423 
0424         void produce(StreamID streamID, Event& ev, EventSetup const& es) const final { accumulate(streamID, ev, es); }
0425 
0426         virtual void accumulate(StreamID streamID, Event const& ev, EventSetup const& es) const = 0;
0427       };
0428     }  // namespace impl
0429   }    // namespace limited
0430 }  // namespace edm
0431 
0432 #endif