Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-13 03:37:55

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 preallocRuns(unsigned int iNRuns) final {
0105         m_runs.resize(iNRuns);
0106         m_runSummaries.resize(iNRuns);
0107       }
0108       void preallocLumis(unsigned int iNLumis) final {
0109         m_lumis.resize(iNLumis);
0110         m_lumiSummaries.resize(iNLumis);
0111       }
0112       void doBeginJob() final { MyGlobal::beginJob(m_global.get()); }
0113       void doEndJob() final { MyGlobal::endJob(m_global.get()); }
0114       void setupRun(M* iProd, RunIndex iIndex) final { MyGlobalRun::set(iProd, m_runs[iIndex].get()); }
0115       void streamEndRunSummary(M* iProd, edm::Run const& iRun, edm::EventSetup const& iES) final {
0116         auto s = m_runSummaries[iRun.index()].get();
0117         std::lock_guard<decltype(m_runSummaryLock)> guard(m_runSummaryLock);
0118         MyGlobalRunSummary::streamEndRunSummary(iProd, iRun, iES, s);
0119       }
0120 
0121       void setupLuminosityBlock(M* iProd, LuminosityBlockIndex iIndex) final {
0122         MyGlobalLuminosityBlock::set(iProd, m_lumis[iIndex].get());
0123       }
0124       void streamEndLuminosityBlockSummary(M* iProd,
0125                                            edm::LuminosityBlock const& iLumi,
0126                                            edm::EventSetup const& iES) final {
0127         auto s = m_lumiSummaries[iLumi.index()].get();
0128         std::lock_guard<decltype(m_lumiSummaryLock)> guard(m_lumiSummaryLock);
0129         MyGlobalLuminosityBlockSummary::streamEndLuminosityBlockSummary(iProd, iLumi, iES, s);
0130       }
0131 
0132       void doBeginProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0133         if constexpr (T::HasAbility::kWatchProcessBlock or T::HasAbility::kBeginProcessBlockProducer) {
0134           ProcessBlock processBlock(pbp, this->moduleDescription(), mcc, false);
0135           ProcessBlock const& cnstProcessBlock = processBlock;
0136           processBlock.setConsumer(this->consumer());
0137           MyWatchProcessBlock::beginProcessBlock(cnstProcessBlock, m_global.get());
0138           if constexpr (T::HasAbility::kBeginProcessBlockProducer) {
0139             processBlock.setProducer(this->producer());
0140             MyBeginProcessBlockProduce::produce(processBlock, m_global.get());
0141             this->commit(processBlock);
0142           }
0143         }
0144       }
0145 
0146       void doAccessInputProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0147         if constexpr (T::HasAbility::kInputProcessBlockCache) {
0148           ProcessBlock processBlock(pbp, this->moduleDescription(), mcc, false);
0149           ProcessBlock const& cnstProcessBlock = processBlock;
0150           processBlock.setConsumer(this->consumer());
0151           MyInputProcessBlock::accessInputProcessBlock(cnstProcessBlock, m_global.get(), m_inputProcessBlocks);
0152         }
0153       }
0154 
0155       void doEndProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0156         if constexpr (T::HasAbility::kWatchProcessBlock or T::HasAbility::kEndProcessBlockProducer) {
0157           ProcessBlock processBlock(pbp, this->moduleDescription(), mcc, true);
0158           ProcessBlock const& cnstProcessBlock = processBlock;
0159           processBlock.setConsumer(this->consumer());
0160           MyWatchProcessBlock::endProcessBlock(cnstProcessBlock, m_global.get());
0161           if constexpr (T::HasAbility::kEndProcessBlockProducer) {
0162             processBlock.setProducer(this->producer());
0163             MyEndProcessBlockProduce::produce(processBlock, m_global.get());
0164             this->commit(processBlock);
0165           }
0166         }
0167       }
0168 
0169       void doBeginRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0170         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache or T::HasAbility::kBeginRunProducer) {
0171           RunPrincipal const& rp = info.principal();
0172           Run r(rp, this->moduleDescription(), mcc, false);
0173           r.setConsumer(this->consumer());
0174           r.setProducer(this->producer());
0175           Run const& cnstR = r;
0176           RunIndex ri = rp.index();
0177           ESParentContext parentC(mcc);
0178           const EventSetup c{info,
0179                              static_cast<unsigned int>(Transition::BeginRun),
0180                              this->consumer()->esGetTokenIndices(Transition::BeginRun),
0181                              parentC};
0182           MyGlobalRun::beginRun(cnstR, c, m_global.get(), m_runs[ri]);
0183           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0184           MyGlobalRunSummary::beginRun(cnstR, c, &rc, m_runSummaries[ri]);
0185           if constexpr (T::HasAbility::kBeginRunProducer) {
0186             MyBeginRunProduce::produce(r, c, &rc);
0187             this->commit(r);
0188           }
0189         }
0190       }
0191 
0192       void doEndRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0193         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache or T::HasAbility::kEndRunProducer) {
0194           RunPrincipal const& rp = info.principal();
0195           Run r(rp, this->moduleDescription(), mcc, true);
0196           r.setConsumer(this->consumer());
0197           r.setProducer(this->producer());
0198 
0199           RunIndex ri = rp.index();
0200           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0201           ESParentContext parentC(mcc);
0202           const EventSetup c{info,
0203                              static_cast<unsigned int>(Transition::EndRun),
0204                              this->consumer()->esGetTokenIndices(Transition::EndRun),
0205                              parentC};
0206           MyGlobalRunSummary::globalEndRun(r, c, &rc, m_runSummaries[ri].get());
0207           if constexpr (T::HasAbility::kEndRunProducer) {
0208             MyEndRunProduce::produce(r, c, &rc, m_runSummaries[ri].get());
0209             this->commit(r);
0210           }
0211           MyGlobalRun::endRun(r, c, &rc);
0212         }
0213       }
0214 
0215       void doBeginLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0216         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache or
0217                       T::HasAbility::kBeginLuminosityBlockProducer) {
0218           LuminosityBlockPrincipal const& lbp = info.principal();
0219           LuminosityBlock lb(lbp, this->moduleDescription(), mcc, false);
0220           lb.setConsumer(this->consumer());
0221           lb.setProducer(this->producer());
0222           LuminosityBlock const& cnstLb = lb;
0223           LuminosityBlockIndex li = lbp.index();
0224           RunIndex ri = lbp.runPrincipal().index();
0225           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0226           ESParentContext parentC(mcc);
0227           const EventSetup c{info,
0228                              static_cast<unsigned int>(Transition::BeginLuminosityBlock),
0229                              this->consumer()->esGetTokenIndices(Transition::BeginLuminosityBlock),
0230                              parentC};
0231 
0232           MyGlobalLuminosityBlock::beginLuminosityBlock(cnstLb, c, &rc, m_lumis[li]);
0233           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0234           MyGlobalLuminosityBlockSummary::beginLuminosityBlock(cnstLb, c, &lc, m_lumiSummaries[li]);
0235           if constexpr (T::HasAbility::kBeginLuminosityBlockProducer) {
0236             MyBeginLuminosityBlockProduce::produce(lb, c, &lc);
0237             this->commit(lb);
0238           }
0239         }
0240       }
0241       void doEndLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0242         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache or
0243                       T::HasAbility::kEndLuminosityBlockProducer) {
0244           LuminosityBlockPrincipal const& lbp = info.principal();
0245           LuminosityBlock lb(lbp, this->moduleDescription(), mcc, true);
0246           lb.setConsumer(this->consumer());
0247           lb.setProducer(this->producer());
0248 
0249           LuminosityBlockIndex li = lbp.index();
0250           RunIndex ri = lbp.runPrincipal().index();
0251           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0252           ESParentContext parentC(mcc);
0253           const EventSetup c{info,
0254                              static_cast<unsigned int>(Transition::EndLuminosityBlock),
0255                              this->consumer()->esGetTokenIndices(Transition::EndLuminosityBlock),
0256                              parentC};
0257           MyGlobalLuminosityBlockSummary::globalEndLuminosityBlock(lb, c, &lc, m_lumiSummaries[li].get());
0258           if constexpr (T::HasAbility::kEndLuminosityBlockProducer) {
0259             MyEndLuminosityBlockProduce::produce(lb, c, &lc, m_lumiSummaries[li].get());
0260             this->commit(lb);
0261           }
0262           MyGlobalLuminosityBlock::endLuminosityBlock(lb, c, &lc);
0263         }
0264       }
0265 
0266       void doRespondToCloseOutputFile() final { MyInputProcessBlock::clearCaches(m_inputProcessBlocks); }
0267 
0268       using B::consumer;
0269 
0270       void selectInputProcessBlocks(ProductRegistry const& productRegistry,
0271                                     ProcessBlockHelperBase const& processBlockHelperBase) final {
0272         MyInputProcessBlock::selectInputProcessBlocks(
0273             m_inputProcessBlocks, productRegistry, processBlockHelperBase, *consumer());
0274       }
0275 
0276       // ---------- member data --------------------------------
0277       typename impl::choose_unique_ptr<typename T::GlobalCache>::type m_global;
0278       typename impl::choose_unique_ptr<typename T::InputProcessBlockCache>::type m_inputProcessBlocks;
0279       typename impl::choose_shared_vec<typename T::RunCache const>::type m_runs;
0280       typename impl::choose_shared_vec<typename T::LuminosityBlockCache const>::type m_lumis;
0281       typename impl::choose_shared_vec<typename T::RunSummaryCache>::type m_runSummaries;
0282       typename impl::choose_mutex<typename T::RunSummaryCache>::type m_runSummaryLock;
0283       typename impl::choose_shared_vec<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaries;
0284       typename impl::choose_mutex<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaryLock;
0285       ParameterSet const* m_pset;
0286     };
0287   }  // namespace stream
0288 }  // namespace edm
0289 
0290 #endif