Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:22

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