Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-14 11:45:05

0001 #ifndef FWCore_Framework_stream_ProducingModuleAdaptor_h
0002 #define FWCore_Framework_stream_ProducingModuleAdaptor_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/Framework
0006 // Class  :     ProducingModuleAdaptor
0007 //
0008 /**\class edm::stream::ProducingModuleAdaptor ProducingModuleAdaptor.h "ProducingModuleAdaptor.h"
0009 
0010  Description: Adapts an edm::stream::EDProducer<> to work with an edm::Worker
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Fri, 02 Aug 2013 18:09:18 GMT
0019 //
0020 
0021 // system include files
0022 
0023 // user include files
0024 #include "FWCore/Framework/interface/ProcessBlock.h"
0025 #include "FWCore/Framework/interface/Run.h"
0026 #include "FWCore/Framework/interface/LuminosityBlock.h"
0027 #include "FWCore/Framework/interface/RunPrincipal.h"
0028 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0029 #include "FWCore/Framework/interface/EventSetup.h"
0030 #include "FWCore/Framework/interface/stream/callAbilities.h"
0031 #include "FWCore/Framework/interface/stream/dummy_helpers.h"
0032 #include "FWCore/Framework/interface/stream/makeGlobal.h"
0033 #include "FWCore/Framework/interface/TransitionInfoTypes.h"
0034 #include "FWCore/ServiceRegistry/interface/ESParentContext.h"
0035 // forward declarations
0036 
0037 namespace edm {
0038   class ConfigurationDescriptions;
0039   namespace stream {
0040 
0041     template <typename T, typename M, typename B>
0042     class ProducingModuleAdaptor : public B {
0043     public:
0044       ProducingModuleAdaptor(edm::ParameterSet const& iPSet) : m_pset(&iPSet) {
0045         m_runs.resize(1);
0046         m_lumis.resize(1);
0047         m_runSummaries.resize(1);
0048         m_lumiSummaries.resize(1);
0049         typename T::GlobalCache const* dummy = nullptr;
0050         m_global = impl::makeGlobal<T>(iPSet, dummy);
0051         typename T::InputProcessBlockCache const* dummyInputProcessBlockCacheImpl = nullptr;
0052         m_inputProcessBlocks = impl::makeInputProcessBlockCacheImpl(dummyInputProcessBlockCacheImpl);
0053       }
0054       ProducingModuleAdaptor(const ProducingModuleAdaptor&) = delete;                   // stop default
0055       const ProducingModuleAdaptor& operator=(const ProducingModuleAdaptor&) = delete;  // stop default
0056       ~ProducingModuleAdaptor() override { this->deleteModulesEarly(); }
0057 
0058       static void fillDescriptions(ConfigurationDescriptions& descriptions) { T::fillDescriptions(descriptions); }
0059       static void prevalidate(ConfigurationDescriptions& descriptions) { T::prevalidate(descriptions); }
0060 
0061       bool wantsProcessBlocks() const final {
0062         return T::HasAbility::kWatchProcessBlock or T::HasAbility::kBeginProcessBlockProducer or
0063                T::HasAbility::kEndProcessBlockProducer;
0064       }
0065       bool wantsInputProcessBlocks() const final { return T::HasAbility::kInputProcessBlockCache; }
0066       bool wantsGlobalRuns() const final {
0067         return T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache or T::HasAbility::kBeginRunProducer or
0068                T::HasAbility::kEndRunProducer;
0069       }
0070       bool wantsGlobalLuminosityBlocks() const final {
0071         return T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache or
0072                T::HasAbility::kBeginLuminosityBlockProducer or T::HasAbility::kEndLuminosityBlockProducer;
0073       }
0074 
0075       bool hasAcquire() const final { return T::HasAbility::kExternalWork; }
0076 
0077       bool hasAccumulator() const final { return T::HasAbility::kAccumulator; }
0078 
0079     private:
0080       using MyGlobal = CallGlobal<T>;
0081       using MyInputProcessBlock = CallInputProcessBlock<T>;
0082       using MyWatchProcessBlock = CallWatchProcessBlock<T>;
0083       using MyBeginProcessBlockProduce = CallBeginProcessBlockProduce<T>;
0084       using MyEndProcessBlockProduce = CallEndProcessBlockProduce<T>;
0085       using MyGlobalRun = CallGlobalRun<T>;
0086       using MyGlobalRunSummary = CallGlobalRunSummary<T>;
0087       using MyBeginRunProduce = CallBeginRunProduce<T>;
0088       using MyEndRunProduce = CallEndRunProduce<T>;
0089       using MyGlobalLuminosityBlock = CallGlobalLuminosityBlock<T>;
0090       using MyGlobalLuminosityBlockSummary = CallGlobalLuminosityBlockSummary<T>;
0091       using MyBeginLuminosityBlockProduce = CallBeginLuminosityBlockProduce<T>;
0092       using MyEndLuminosityBlockProduce = CallEndLuminosityBlockProduce<T>;
0093 
0094       void setupStreamModules() final {
0095         this->createStreamModules([this](unsigned int iStreamModule) -> M* {
0096           auto tmp = impl::makeStreamModule<T>(*m_pset, m_global.get());
0097           MyGlobal::set(tmp, m_global.get());
0098           MyInputProcessBlock::set(tmp, &m_inputProcessBlocks, iStreamModule);
0099           return tmp;
0100         });
0101         m_pset = nullptr;
0102       }
0103 
0104       void preallocLumis(unsigned int iNLumis) final {
0105         m_lumis.resize(iNLumis);
0106         m_lumiSummaries.resize(iNLumis);
0107       }
0108       void doBeginJob() final { MyGlobal::beginJob(m_global.get()); }
0109       void doEndJob() final { MyGlobal::endJob(m_global.get()); }
0110       void setupRun(M* iProd, RunIndex iIndex) final { MyGlobalRun::set(iProd, m_runs[iIndex].get()); }
0111       void streamEndRunSummary(M* iProd, edm::Run const& iRun, edm::EventSetup const& iES) final {
0112         auto s = m_runSummaries[iRun.index()].get();
0113         std::lock_guard<decltype(m_runSummaryLock)> guard(m_runSummaryLock);
0114         MyGlobalRunSummary::streamEndRunSummary(iProd, iRun, iES, s);
0115       }
0116 
0117       void setupLuminosityBlock(M* iProd, LuminosityBlockIndex iIndex) final {
0118         MyGlobalLuminosityBlock::set(iProd, m_lumis[iIndex].get());
0119       }
0120       void streamEndLuminosityBlockSummary(M* iProd,
0121                                            edm::LuminosityBlock const& iLumi,
0122                                            edm::EventSetup const& iES) final {
0123         auto s = m_lumiSummaries[iLumi.index()].get();
0124         std::lock_guard<decltype(m_lumiSummaryLock)> guard(m_lumiSummaryLock);
0125         MyGlobalLuminosityBlockSummary::streamEndLuminosityBlockSummary(iProd, iLumi, iES, s);
0126       }
0127 
0128       void doBeginProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0129         if constexpr (T::HasAbility::kWatchProcessBlock or T::HasAbility::kBeginProcessBlockProducer) {
0130           ProcessBlock processBlock(pbp, this->moduleDescription(), mcc, false);
0131           ProcessBlock const& cnstProcessBlock = processBlock;
0132           processBlock.setConsumer(this->consumer());
0133           MyWatchProcessBlock::beginProcessBlock(cnstProcessBlock, m_global.get());
0134           if constexpr (T::HasAbility::kBeginProcessBlockProducer) {
0135             processBlock.setProducer(this->producer());
0136             MyBeginProcessBlockProduce::produce(processBlock, m_global.get());
0137             this->commit(processBlock);
0138           }
0139         }
0140       }
0141 
0142       void doAccessInputProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0143         if constexpr (T::HasAbility::kInputProcessBlockCache) {
0144           ProcessBlock processBlock(pbp, this->moduleDescription(), mcc, false);
0145           ProcessBlock const& cnstProcessBlock = processBlock;
0146           processBlock.setConsumer(this->consumer());
0147           MyInputProcessBlock::accessInputProcessBlock(cnstProcessBlock, m_global.get(), m_inputProcessBlocks);
0148         }
0149       }
0150 
0151       void doEndProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0152         if constexpr (T::HasAbility::kWatchProcessBlock or T::HasAbility::kEndProcessBlockProducer) {
0153           ProcessBlock processBlock(pbp, this->moduleDescription(), mcc, true);
0154           ProcessBlock const& cnstProcessBlock = processBlock;
0155           processBlock.setConsumer(this->consumer());
0156           MyWatchProcessBlock::endProcessBlock(cnstProcessBlock, m_global.get());
0157           if constexpr (T::HasAbility::kEndProcessBlockProducer) {
0158             processBlock.setProducer(this->producer());
0159             MyEndProcessBlockProduce::produce(processBlock, m_global.get());
0160             this->commit(processBlock);
0161           }
0162         }
0163       }
0164 
0165       void doBeginRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0166         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache or T::HasAbility::kBeginRunProducer) {
0167           RunPrincipal const& rp = info.principal();
0168           Run r(rp, this->moduleDescription(), mcc, false);
0169           r.setConsumer(this->consumer());
0170           r.setProducer(this->producer());
0171           Run const& cnstR = r;
0172           RunIndex ri = rp.index();
0173           ESParentContext parentC(mcc);
0174           const EventSetup c{info,
0175                              static_cast<unsigned int>(Transition::BeginRun),
0176                              this->consumer()->esGetTokenIndices(Transition::BeginRun),
0177                              parentC};
0178           MyGlobalRun::beginRun(cnstR, c, m_global.get(), m_runs[ri]);
0179           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0180           MyGlobalRunSummary::beginRun(cnstR, c, &rc, m_runSummaries[ri]);
0181           if constexpr (T::HasAbility::kBeginRunProducer) {
0182             MyBeginRunProduce::produce(r, c, &rc);
0183             this->commit(r);
0184           }
0185         }
0186       }
0187 
0188       void doEndRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0189         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache or T::HasAbility::kEndRunProducer) {
0190           RunPrincipal const& rp = info.principal();
0191           Run r(rp, this->moduleDescription(), mcc, true);
0192           r.setConsumer(this->consumer());
0193           r.setProducer(this->producer());
0194 
0195           RunIndex ri = rp.index();
0196           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0197           ESParentContext parentC(mcc);
0198           const EventSetup c{info,
0199                              static_cast<unsigned int>(Transition::EndRun),
0200                              this->consumer()->esGetTokenIndices(Transition::EndRun),
0201                              parentC};
0202           MyGlobalRunSummary::globalEndRun(r, c, &rc, m_runSummaries[ri].get());
0203           if constexpr (T::HasAbility::kEndRunProducer) {
0204             MyEndRunProduce::produce(r, c, &rc, m_runSummaries[ri].get());
0205             this->commit(r);
0206           }
0207           MyGlobalRun::endRun(r, c, &rc);
0208         }
0209       }
0210 
0211       void doBeginLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0212         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache or
0213                       T::HasAbility::kBeginLuminosityBlockProducer) {
0214           LuminosityBlockPrincipal const& lbp = info.principal();
0215           LuminosityBlock lb(lbp, this->moduleDescription(), mcc, false);
0216           lb.setConsumer(this->consumer());
0217           lb.setProducer(this->producer());
0218           LuminosityBlock const& cnstLb = lb;
0219           LuminosityBlockIndex li = lbp.index();
0220           RunIndex ri = lbp.runPrincipal().index();
0221           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0222           ESParentContext parentC(mcc);
0223           const EventSetup c{info,
0224                              static_cast<unsigned int>(Transition::BeginLuminosityBlock),
0225                              this->consumer()->esGetTokenIndices(Transition::BeginLuminosityBlock),
0226                              parentC};
0227 
0228           MyGlobalLuminosityBlock::beginLuminosityBlock(cnstLb, c, &rc, m_lumis[li]);
0229           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0230           MyGlobalLuminosityBlockSummary::beginLuminosityBlock(cnstLb, c, &lc, m_lumiSummaries[li]);
0231           if constexpr (T::HasAbility::kBeginLuminosityBlockProducer) {
0232             MyBeginLuminosityBlockProduce::produce(lb, c, &lc);
0233             this->commit(lb);
0234           }
0235         }
0236       }
0237       void doEndLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0238         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache or
0239                       T::HasAbility::kEndLuminosityBlockProducer) {
0240           LuminosityBlockPrincipal const& lbp = info.principal();
0241           LuminosityBlock lb(lbp, this->moduleDescription(), mcc, true);
0242           lb.setConsumer(this->consumer());
0243           lb.setProducer(this->producer());
0244 
0245           LuminosityBlockIndex li = lbp.index();
0246           RunIndex ri = lbp.runPrincipal().index();
0247           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0248           ESParentContext parentC(mcc);
0249           const EventSetup c{info,
0250                              static_cast<unsigned int>(Transition::EndLuminosityBlock),
0251                              this->consumer()->esGetTokenIndices(Transition::EndLuminosityBlock),
0252                              parentC};
0253           MyGlobalLuminosityBlockSummary::globalEndLuminosityBlock(lb, c, &lc, m_lumiSummaries[li].get());
0254           if constexpr (T::HasAbility::kEndLuminosityBlockProducer) {
0255             MyEndLuminosityBlockProduce::produce(lb, c, &lc, m_lumiSummaries[li].get());
0256             this->commit(lb);
0257           }
0258           MyGlobalLuminosityBlock::endLuminosityBlock(lb, c, &lc);
0259         }
0260       }
0261 
0262       void doRespondToCloseOutputFile() final { MyInputProcessBlock::clearCaches(m_inputProcessBlocks); }
0263 
0264       using B::consumer;
0265 
0266       void selectInputProcessBlocks(ProductRegistry const& productRegistry,
0267                                     ProcessBlockHelperBase const& processBlockHelperBase) final {
0268         MyInputProcessBlock::selectInputProcessBlocks(
0269             m_inputProcessBlocks, productRegistry, processBlockHelperBase, *consumer());
0270       }
0271 
0272       // ---------- member data --------------------------------
0273       typename impl::choose_unique_ptr<typename T::GlobalCache>::type m_global;
0274       typename impl::choose_unique_ptr<typename T::InputProcessBlockCache>::type m_inputProcessBlocks;
0275       typename impl::choose_shared_vec<typename T::RunCache const>::type m_runs;
0276       typename impl::choose_shared_vec<typename T::LuminosityBlockCache const>::type m_lumis;
0277       typename impl::choose_shared_vec<typename T::RunSummaryCache>::type m_runSummaries;
0278       typename impl::choose_mutex<typename T::RunSummaryCache>::type m_runSummaryLock;
0279       typename impl::choose_shared_vec<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaries;
0280       typename impl::choose_mutex<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaryLock;
0281       ParameterSet const* m_pset;
0282     };
0283   }  // namespace stream
0284 }  // namespace edm
0285 
0286 #endif