Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-11 22:29:28

0001 #ifndef FWCore_Framework_one_implementors_h
0002 #define FWCore_Framework_one_implementors_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/Framework
0006 // Class  :     implementors
0007 //
0008 /**\file implementors.h "FWCore/Framework/interface/one/implementors.h"
0009 
0010  Description: Base classes used to implement the interfaces for the edm::one::* module  abilities
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Thu, 09 May 2013 18:40:17 GMT
0019 //
0020 
0021 // system include files
0022 #include <cstddef>
0023 #include <memory>
0024 #include <set>
0025 #include <string>
0026 #include <tuple>
0027 #include <utility>
0028 
0029 // user include files
0030 #include "FWCore/Common/interface/FWCoreCommonFwd.h"
0031 #include "FWCore/Concurrency/interface/SerialTaskQueue.h"
0032 #include "FWCore/Framework/interface/CacheHandle.h"
0033 #include "FWCore/Framework/interface/Frameworkfwd.h"
0034 #include "FWCore/Framework/interface/InputProcessBlockCacheImpl.h"
0035 #include "FWCore/Framework/interface/LuminosityBlock.h"
0036 #include "FWCore/Framework/interface/TransformerBase.h"
0037 #include "FWCore/Framework/interface/ProductRegistryHelper.h"
0038 #include "FWCore/Utilities/interface/EDGetToken.h"
0039 #include "FWCore/Utilities/interface/ProcessBlockIndex.h"
0040 #include "FWCore/Utilities/interface/RunIndex.h"
0041 #include "FWCore/Utilities/interface/LuminosityBlockIndex.h"
0042 #include "FWCore/Utilities/interface/propagate_const.h"
0043 #include "DataFormats/Common/interface/Wrapper.h"
0044 
0045 // forward declarations
0046 
0047 namespace edm {
0048   class SharedResourcesAcquirer;
0049 
0050   namespace one {
0051     namespace impl {
0052       template <bool V>
0053       struct OptionalSerialTaskQueueHolder;
0054 
0055       template <>
0056       struct OptionalSerialTaskQueueHolder<true> {
0057         edm::SerialTaskQueue* queue() { return &queue_; }
0058         edm::SerialTaskQueue queue_;
0059       };
0060 
0061       template <>
0062       struct OptionalSerialTaskQueueHolder<false> {
0063         edm::SerialTaskQueue* queue() { return nullptr; }
0064       };
0065 
0066       template <typename T>
0067       class SharedResourcesUser : public virtual T {
0068       public:
0069         template <typename... Args>
0070         SharedResourcesUser(Args... args) : T(args...) {}
0071         SharedResourcesUser(SharedResourcesUser const&) = delete;
0072         SharedResourcesUser& operator=(SharedResourcesUser const&) = delete;
0073 
0074         ~SharedResourcesUser() override {}
0075 
0076       protected:
0077         void usesResource(std::string const& iName);
0078         void usesResource();
0079 
0080       private:
0081         SharedResourcesAcquirer createAcquirer() override;
0082         std::set<std::string> resourceNames_;
0083       };
0084 
0085       template <typename T>
0086       class RunWatcher : public virtual T {
0087       public:
0088         RunWatcher() = default;
0089         RunWatcher(RunWatcher const&) = delete;
0090         RunWatcher& operator=(RunWatcher const&) = delete;
0091         ~RunWatcher() noexcept(false) override{};
0092 
0093       private:
0094         void doBeginRun_(Run const& rp, EventSetup const& c) final;
0095         void doEndRun_(Run const& rp, EventSetup const& c) final;
0096 
0097         virtual void beginRun(edm::Run const&, edm::EventSetup const&) = 0;
0098         virtual void endRun(edm::Run const&, edm::EventSetup const&) = 0;
0099       };
0100 
0101       template <typename T>
0102       class LuminosityBlockWatcher : public virtual T {
0103       public:
0104         LuminosityBlockWatcher() = default;
0105         LuminosityBlockWatcher(LuminosityBlockWatcher const&) = delete;
0106         LuminosityBlockWatcher& operator=(LuminosityBlockWatcher const&) = delete;
0107         ~LuminosityBlockWatcher() noexcept(false) override{};
0108 
0109       private:
0110         void doBeginLuminosityBlock_(LuminosityBlock const& rp, EventSetup const& c) final;
0111         void doEndLuminosityBlock_(LuminosityBlock const& rp, EventSetup const& c) final;
0112 
0113         virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) = 0;
0114         virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) = 0;
0115       };
0116 
0117       template <typename T>
0118       class WatchProcessBlock : public virtual T {
0119       public:
0120         WatchProcessBlock() = default;
0121         WatchProcessBlock(WatchProcessBlock const&) = delete;
0122         WatchProcessBlock& operator=(WatchProcessBlock const&) = delete;
0123         ~WatchProcessBlock() noexcept(false) override {}
0124 
0125       private:
0126         void doBeginProcessBlock_(ProcessBlock const&) final;
0127         void doEndProcessBlock_(ProcessBlock const&) final;
0128 
0129         virtual void beginProcessBlock(ProcessBlock const&) {}
0130         virtual void endProcessBlock(ProcessBlock const&) {}
0131       };
0132 
0133       template <typename T>
0134       class BeginProcessBlockProducer : public virtual T {
0135       public:
0136         BeginProcessBlockProducer() = default;
0137         BeginProcessBlockProducer(BeginProcessBlockProducer const&) = delete;
0138         BeginProcessBlockProducer& operator=(BeginProcessBlockProducer const&) = delete;
0139         ~BeginProcessBlockProducer() noexcept(false) override{};
0140 
0141       private:
0142         void doBeginProcessBlockProduce_(ProcessBlock&) final;
0143 
0144         virtual void beginProcessBlockProduce(edm::ProcessBlock&) = 0;
0145       };
0146 
0147       template <typename T>
0148       class EndProcessBlockProducer : public virtual T {
0149       public:
0150         EndProcessBlockProducer() = default;
0151         EndProcessBlockProducer(EndProcessBlockProducer const&) = delete;
0152         EndProcessBlockProducer& operator=(EndProcessBlockProducer const&) = delete;
0153         ~EndProcessBlockProducer() noexcept(false) override{};
0154 
0155       private:
0156         void doEndProcessBlockProduce_(ProcessBlock&) final;
0157 
0158         virtual void endProcessBlockProduce(edm::ProcessBlock&) = 0;
0159       };
0160 
0161       template <typename T>
0162       class BeginRunProducer : public virtual T {
0163       public:
0164         BeginRunProducer() = default;
0165         BeginRunProducer(BeginRunProducer const&) = delete;
0166         BeginRunProducer& operator=(BeginRunProducer const&) = delete;
0167         ~BeginRunProducer() noexcept(false) override{};
0168 
0169       private:
0170         void doBeginRunProduce_(Run& rp, EventSetup const& c) final;
0171 
0172         virtual void beginRunProduce(edm::Run&, edm::EventSetup const&) = 0;
0173       };
0174 
0175       template <typename T>
0176       class EndRunProducer : public virtual T {
0177       public:
0178         EndRunProducer() = default;
0179         EndRunProducer(EndRunProducer const&) = delete;
0180         EndRunProducer& operator=(EndRunProducer const&) = delete;
0181         ~EndRunProducer() noexcept(false) override{};
0182 
0183       private:
0184         void doEndRunProduce_(Run& rp, EventSetup const& c) final;
0185 
0186         virtual void endRunProduce(edm::Run&, edm::EventSetup const&) = 0;
0187       };
0188 
0189       template <typename T>
0190       class BeginLuminosityBlockProducer : public virtual T {
0191       public:
0192         BeginLuminosityBlockProducer() = default;
0193         BeginLuminosityBlockProducer(BeginLuminosityBlockProducer const&) = delete;
0194         BeginLuminosityBlockProducer& operator=(BeginLuminosityBlockProducer const&) = delete;
0195         ~BeginLuminosityBlockProducer() noexcept(false) override{};
0196 
0197       private:
0198         void doBeginLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c) final;
0199 
0200         virtual void beginLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) = 0;
0201       };
0202 
0203       template <typename T>
0204       class EndLuminosityBlockProducer : public virtual T {
0205       public:
0206         EndLuminosityBlockProducer() = default;
0207         EndLuminosityBlockProducer(EndLuminosityBlockProducer const&) = delete;
0208         EndLuminosityBlockProducer& operator=(EndLuminosityBlockProducer const&) = delete;
0209         ~EndLuminosityBlockProducer() noexcept(false) override{};
0210 
0211       private:
0212         void doEndLuminosityBlockProduce_(LuminosityBlock& lbp, EventSetup const& c) final;
0213 
0214         virtual void endLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) = 0;
0215       };
0216 
0217       template <typename T, typename... CacheTypes>
0218       class InputProcessBlockCacheHolder : public virtual T {
0219       public:
0220         InputProcessBlockCacheHolder() = default;
0221         InputProcessBlockCacheHolder(InputProcessBlockCacheHolder const&) = delete;
0222         InputProcessBlockCacheHolder& operator=(InputProcessBlockCacheHolder const&) = delete;
0223         ~InputProcessBlockCacheHolder() override {}
0224 
0225         std::tuple<CacheHandle<CacheTypes>...> processBlockCaches(Event const& event) const {
0226           return cacheImpl_.processBlockCaches(event);
0227         }
0228 
0229         template <std::size_t ICacheType, typename DataType, typename Func>
0230         void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& func) {
0231           cacheImpl_.template registerProcessBlockCacheFiller<ICacheType, DataType, Func>(token,
0232                                                                                           std::forward<Func>(func));
0233         }
0234 
0235         template <typename CacheType, typename DataType, typename Func>
0236         void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& func) {
0237           cacheImpl_.template registerProcessBlockCacheFiller<CacheType, DataType, Func>(token,
0238                                                                                          std::forward<Func>(func));
0239         }
0240 
0241         // This is intended for use by Framework unit tests only
0242         unsigned int cacheSize() const { return cacheImpl_.cacheSize(); }
0243 
0244       private:
0245         void doSelectInputProcessBlocks(ProductRegistry const& productRegistry,
0246                                         ProcessBlockHelperBase const& processBlockHelperBase) final {
0247           cacheImpl_.selectInputProcessBlocks(productRegistry, processBlockHelperBase, *this);
0248         }
0249 
0250         void doAccessInputProcessBlock_(ProcessBlock const& pb) final {
0251           cacheImpl_.accessInputProcessBlock(pb);
0252           accessInputProcessBlock(pb);
0253         }
0254 
0255         // Alternate method to access ProcessBlocks without using the caches
0256         // Mostly intended for unit testing, but might have other uses...
0257         virtual void accessInputProcessBlock(ProcessBlock const&) {}
0258 
0259         void clearInputProcessBlockCaches() final { cacheImpl_.clearCaches(); }
0260 
0261         edm::impl::InputProcessBlockCacheImpl<CacheTypes...> cacheImpl_;
0262       };
0263 
0264       template <typename T, typename C>
0265       class RunCacheHolder : public virtual T {
0266       public:
0267         RunCacheHolder() = default;
0268         RunCacheHolder(RunCacheHolder<T, C> const&) = delete;
0269         RunCacheHolder<T, C>& operator=(RunCacheHolder<T, C> const&) = delete;
0270         ~RunCacheHolder() noexcept(false) override{};
0271 
0272       protected:
0273         C* runCache(edm::RunIndex iID) { return cache_.get(); }
0274         C const* runCache(edm::RunIndex iID) const { return cache_.get(); }
0275 
0276       private:
0277         void doBeginRun_(Run const& rp, EventSetup const& c) final { cache_ = globalBeginRun(rp, c); }
0278         void doEndRun_(Run const& rp, EventSetup const& c) final {
0279           globalEndRun(rp, c);
0280           cache_ = nullptr;  // propagate_const<T> has no reset() function
0281         }
0282 
0283         virtual std::shared_ptr<C> globalBeginRun(edm::Run const&, edm::EventSetup const&) const = 0;
0284         virtual void globalEndRun(edm::Run const&, edm::EventSetup const&) = 0;
0285         //When threaded we will have a container for N items whre N is # of simultaneous runs
0286         edm::propagate_const<std::shared_ptr<C>> cache_;
0287       };
0288 
0289       template <typename T, typename C>
0290       class LuminosityBlockCacheHolder : public virtual T {
0291       public:
0292         LuminosityBlockCacheHolder() = default;
0293         LuminosityBlockCacheHolder(LuminosityBlockCacheHolder<T, C> const&) = delete;
0294         LuminosityBlockCacheHolder<T, C>& operator=(LuminosityBlockCacheHolder<T, C> const&) = delete;
0295         ~LuminosityBlockCacheHolder() noexcept(false) override{};
0296 
0297       protected:
0298         void preallocLumis(unsigned int iNLumis) final { caches_.reset(new std::shared_ptr<C>[iNLumis]); }
0299 
0300         C const* luminosityBlockCache(edm::LuminosityBlockIndex iID) const { return caches_[iID].get(); }
0301         C* luminosityBlockCache(edm::LuminosityBlockIndex iID) { return caches_[iID].get(); }
0302 
0303       private:
0304         void doBeginLuminosityBlock_(LuminosityBlock const& lp, EventSetup const& c) final {
0305           caches_[lp.index()] = globalBeginLuminosityBlock(lp, c);
0306         }
0307         void doEndLuminosityBlock_(LuminosityBlock const& lp, EventSetup const& c) final {
0308           globalEndLuminosityBlock(lp, c);
0309           caches_[lp.index()].reset();
0310         }
0311 
0312         virtual std::shared_ptr<C> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0313                                                               edm::EventSetup const&) const = 0;
0314         virtual void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) = 0;
0315         std::unique_ptr<std::shared_ptr<C>[]> caches_;
0316       };
0317 
0318       template <typename T>
0319       class Accumulator : public virtual T {
0320       public:
0321         Accumulator() = default;
0322         Accumulator(Accumulator const&) = delete;
0323         Accumulator& operator=(Accumulator const&) = delete;
0324         ~Accumulator() noexcept(false) override{};
0325 
0326       private:
0327         bool hasAccumulator() const override { return true; }
0328 
0329         void produce(Event& ev, EventSetup const& es) final { accumulate(ev, es); }
0330 
0331         virtual void accumulate(Event const& ev, EventSetup const& es) = 0;
0332       };
0333 
0334       template <typename T>
0335       class Transformer : public virtual T, private TransformerBase {
0336       public:
0337         Transformer() = default;
0338         Transformer(Transformer const&) = delete;
0339         Transformer& operator=(Transformer const&) = delete;
0340         ~Transformer() noexcept(false) override{};
0341 
0342         template <typename G, typename F>
0343         void registerTransform(ProductRegistryHelper::BranchAliasSetterT<G> iSetter,
0344                                F&& iF,
0345                                std::string productInstance = std::string()) {
0346           registerTransform(edm::EDPutTokenT<G>(iSetter), std::forward<F>(iF), std::move(productInstance));
0347         }
0348 
0349         template <typename G, typename F>
0350         void registerTransform(edm::EDPutTokenT<G> iToken, F iF, std::string productInstance = std::string()) {
0351           using ReturnTypeT = decltype(iF(std::declval<G>()));
0352           TypeID returnType(typeid(ReturnTypeT));
0353           TransformerBase::registerTransformImp(*this,
0354                                                 EDPutToken(iToken),
0355                                                 returnType,
0356                                                 std::move(productInstance),
0357                                                 [f = std::move(iF)](edm::WrapperBase const& iGotProduct) {
0358                                                   return std::make_unique<edm::Wrapper<ReturnTypeT>>(
0359                                                       WrapperBase::Emplace{},
0360                                                       f(*static_cast<edm::Wrapper<G> const&>(iGotProduct).product()));
0361                                                 });
0362         }
0363 
0364       private:
0365         size_t transformIndex_(edm::BranchDescription const& iBranch) const final {
0366           return TransformerBase::findMatchingIndex(*this, iBranch);
0367         }
0368         ProductResolverIndex transformPrefetch_(std::size_t iIndex) const final {
0369           return TransformerBase::prefetchImp(iIndex);
0370         }
0371         void transform_(std::size_t iIndex, edm::EventForTransformer& iEvent) const final {
0372           return TransformerBase::transformImp(iIndex, *this, iEvent);
0373         }
0374         void extendUpdateLookup(BranchType iBranchType, ProductResolverIndexHelper const& iHelper) override {
0375           if (iBranchType == InEvent) {
0376             TransformerBase::extendUpdateLookup(*this, this->moduleDescription(), iHelper);
0377           }
0378         }
0379       };
0380     }  // namespace impl
0381   }    // namespace one
0382 }  // namespace edm
0383 
0384 #endif