Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-11 03:34:11

0001 #ifndef FWCore_Framework_stream_EDAnalyzerAdaptor_h
0002 #define FWCore_Framework_stream_EDAnalyzerAdaptor_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/Framework
0006 // Class  :     EDAnalyzerAdaptor
0007 //
0008 /**\class edm::stream::EDAnalyzerAdaptor EDAnalyzerAdaptor.h "EDAnalyzerAdaptor.h"
0009 
0010  Description: Adapts an edm::stream::EDAnalyzer<> 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/EDAnalyzerAdaptorBase.h"
0031 #include "FWCore/Framework/interface/stream/callAbilities.h"
0032 #include "FWCore/Framework/interface/stream/dummy_helpers.h"
0033 #include "FWCore/Framework/interface/stream/makeGlobal.h"
0034 #include "FWCore/Framework/interface/maker/MakeModuleHelper.h"
0035 #include "FWCore/Framework/interface/TransitionInfoTypes.h"
0036 #include "FWCore/ServiceRegistry/interface/ESParentContext.h"
0037 
0038 // forward declarations
0039 
0040 namespace edm {
0041   namespace stream {
0042 
0043     template <typename ABase, typename ModType>
0044     struct BaseToAdaptor;
0045 
0046     template <typename T>
0047     class EDAnalyzerAdaptor;
0048     template <typename ModType>
0049     struct BaseToAdaptor<EDAnalyzerAdaptorBase, ModType> {
0050       typedef EDAnalyzerAdaptor<ModType> Type;
0051     };
0052 
0053     template <typename T>
0054     class EDAnalyzerAdaptor : public EDAnalyzerAdaptorBase {
0055     public:
0056       EDAnalyzerAdaptor(edm::ParameterSet const& iPSet) : m_pset(&iPSet) {
0057         m_runs.resize(1);
0058         m_lumis.resize(1);
0059         m_runSummaries.resize(1);
0060         m_lumiSummaries.resize(1);
0061         typename T::GlobalCache const* dummy = nullptr;
0062         m_global = impl::makeGlobal<T>(iPSet, dummy);
0063         typename T::InputProcessBlockCache const* dummyInputProcessBlockCacheImpl = nullptr;
0064         m_inputProcessBlocks = impl::makeInputProcessBlockCacheImpl(dummyInputProcessBlockCacheImpl);
0065       }
0066       EDAnalyzerAdaptor(const EDAnalyzerAdaptor&) = delete;                   // stop default
0067       const EDAnalyzerAdaptor& operator=(const EDAnalyzerAdaptor&) = delete;  // stop default
0068       ~EDAnalyzerAdaptor() override { deleteModulesEarly(); }
0069 
0070       static void fillDescriptions(ConfigurationDescriptions& descriptions) { T::fillDescriptions(descriptions); }
0071       static void prevalidate(ConfigurationDescriptions& descriptions) { T::prevalidate(descriptions); }
0072 
0073       bool wantsProcessBlocks() const noexcept final { return T::HasAbility::kWatchProcessBlock; }
0074       bool wantsInputProcessBlocks() const noexcept final { return T::HasAbility::kInputProcessBlockCache; }
0075       bool wantsGlobalRuns() const noexcept final {
0076         return T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache;
0077       }
0078       bool wantsStreamRuns() const noexcept final { return T::HasAbility::kWatchRuns; }
0079       bool wantsGlobalLuminosityBlocks() const noexcept final {
0080         return T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache;
0081       }
0082       bool wantsStreamLuminosityBlocks() const noexcept final { return T::HasAbility::kWatchLuminosityBlocks; }
0083 
0084     private:
0085       using MyGlobal = CallGlobal<T>;
0086       using MyInputProcessBlock = CallInputProcessBlock<T>;
0087       using MyWatchProcessBlock = CallWatchProcessBlock<T>;
0088       using MyGlobalRun = CallGlobalRun<T>;
0089       using MyGlobalRunSummary = CallGlobalRunSummary<T>;
0090       using MyGlobalLuminosityBlock = CallGlobalLuminosityBlock<T>;
0091       using MyGlobalLuminosityBlockSummary = CallGlobalLuminosityBlockSummary<T>;
0092 
0093       void setupStreamModules() final {
0094         this->createStreamModules([this](unsigned int iStreamModule) -> EDAnalyzerBase* {
0095           auto tmp = impl::makeStreamModule<T>(*m_pset, m_global.get());
0096           MyGlobal::set(tmp, m_global.get());
0097           MyInputProcessBlock::set(tmp, &m_inputProcessBlocks, iStreamModule);
0098           return tmp;
0099         });
0100         m_pset = nullptr;
0101       }
0102 
0103       void preallocRuns(unsigned int iNRuns) final {
0104         m_runs.resize(iNRuns);
0105         m_runSummaries.resize(iNRuns);
0106       }
0107       void preallocLumis(unsigned int iNLumis) final {
0108         m_lumis.resize(iNLumis);
0109         m_lumiSummaries.resize(iNLumis);
0110       }
0111 
0112       void doBeginJob() final { MyGlobal::beginJob(m_global.get()); }
0113       void doEndJob() final { MyGlobal::endJob(m_global.get()); }
0114       void setupRun(EDAnalyzerBase* iProd, RunIndex iIndex) final { MyGlobalRun::set(iProd, m_runs[iIndex].get()); }
0115       void streamEndRunSummary(EDAnalyzerBase* 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(EDAnalyzerBase* iProd, LuminosityBlockIndex iIndex) final {
0122         MyGlobalLuminosityBlock::set(iProd, m_lumis[iIndex].get());
0123       }
0124       void streamEndLuminosityBlockSummary(EDAnalyzerBase* 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) {
0134           ProcessBlock processBlock(pbp, moduleDescription(), mcc, false);
0135           processBlock.setConsumer(consumer());
0136           ProcessBlock const& cnstProcessBlock = processBlock;
0137           MyWatchProcessBlock::beginProcessBlock(cnstProcessBlock, m_global.get());
0138         }
0139       }
0140 
0141       void doAccessInputProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0142         if constexpr (T::HasAbility::kInputProcessBlockCache) {
0143           ProcessBlock processBlock(pbp, moduleDescription(), mcc, false);
0144           processBlock.setConsumer(consumer());
0145           ProcessBlock const& cnstProcessBlock = processBlock;
0146           MyInputProcessBlock::accessInputProcessBlock(cnstProcessBlock, m_global.get(), m_inputProcessBlocks);
0147         }
0148       }
0149 
0150       void doEndProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0151         if constexpr (T::HasAbility::kWatchProcessBlock) {
0152           ProcessBlock processBlock(pbp, moduleDescription(), mcc, true);
0153           processBlock.setConsumer(consumer());
0154           ProcessBlock const& cnstProcessBlock = processBlock;
0155           MyWatchProcessBlock::endProcessBlock(cnstProcessBlock, m_global.get());
0156         }
0157       }
0158 
0159       void doBeginRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0160         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache) {
0161           RunPrincipal const& rp = info.principal();
0162           Run r(rp, moduleDescription(), mcc, false);
0163           r.setConsumer(consumer());
0164           Run const& cnstR = r;
0165           RunIndex ri = rp.index();
0166           ESParentContext pc{mcc};
0167           const EventSetup c{info,
0168                              static_cast<unsigned int>(Transition::BeginRun),
0169                              this->consumer()->esGetTokenIndices(Transition::BeginRun),
0170                              pc};
0171           MyGlobalRun::beginRun(cnstR, c, m_global.get(), m_runs[ri]);
0172           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0173           MyGlobalRunSummary::beginRun(cnstR, c, &rc, m_runSummaries[ri]);
0174         }
0175       }
0176       void doEndRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0177         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache) {
0178           RunPrincipal const& rp = info.principal();
0179           Run r(rp, moduleDescription(), mcc, true);
0180           r.setConsumer(consumer());
0181 
0182           RunIndex ri = rp.index();
0183           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0184           ESParentContext pc{mcc};
0185           const EventSetup c{info,
0186                              static_cast<unsigned int>(Transition::EndRun),
0187                              this->consumer()->esGetTokenIndices(Transition::EndRun),
0188                              pc};
0189           MyGlobalRunSummary::globalEndRun(r, c, &rc, m_runSummaries[ri].get());
0190           MyGlobalRun::endRun(r, c, &rc);
0191         }
0192       }
0193 
0194       void doBeginLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0195         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache) {
0196           LuminosityBlockPrincipal const& lbp = info.principal();
0197           LuminosityBlock lb(lbp, moduleDescription(), mcc, false);
0198           lb.setConsumer(consumer());
0199           LuminosityBlock const& cnstLb = lb;
0200           LuminosityBlockIndex li = lbp.index();
0201           RunIndex ri = lbp.runPrincipal().index();
0202           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0203           ESParentContext pc{mcc};
0204           const EventSetup c{info,
0205                              static_cast<unsigned int>(Transition::BeginLuminosityBlock),
0206                              this->consumer()->esGetTokenIndices(Transition::BeginLuminosityBlock),
0207                              pc};
0208           MyGlobalLuminosityBlock::beginLuminosityBlock(cnstLb, c, &rc, m_lumis[li]);
0209           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0210           MyGlobalLuminosityBlockSummary::beginLuminosityBlock(cnstLb, c, &lc, m_lumiSummaries[li]);
0211         }
0212       }
0213       void doEndLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0214         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache) {
0215           LuminosityBlockPrincipal const& lbp = info.principal();
0216           LuminosityBlock lb(lbp, moduleDescription(), mcc, true);
0217           lb.setConsumer(consumer());
0218 
0219           LuminosityBlockIndex li = lbp.index();
0220           RunIndex ri = lbp.runPrincipal().index();
0221           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0222           ESParentContext pc{mcc};
0223           const EventSetup c{info,
0224                              static_cast<unsigned int>(Transition::EndLuminosityBlock),
0225                              this->consumer()->esGetTokenIndices(Transition::EndLuminosityBlock),
0226                              pc};
0227           MyGlobalLuminosityBlockSummary::globalEndLuminosityBlock(lb, c, &lc, m_lumiSummaries[li].get());
0228           MyGlobalLuminosityBlock::endLuminosityBlock(lb, c, &lc);
0229         }
0230       }
0231 
0232       void doRespondToCloseOutputFile() final { MyInputProcessBlock::clearCaches(m_inputProcessBlocks); }
0233 
0234       void selectInputProcessBlocks(ProductRegistry const& productRegistry,
0235                                     ProcessBlockHelperBase const& processBlockHelperBase) final {
0236         MyInputProcessBlock::selectInputProcessBlocks(
0237             m_inputProcessBlocks, productRegistry, processBlockHelperBase, *consumer());
0238       }
0239 
0240       // ---------- member data --------------------------------
0241       typename impl::choose_unique_ptr<typename T::GlobalCache>::type m_global;
0242       typename impl::choose_unique_ptr<typename T::InputProcessBlockCache>::type m_inputProcessBlocks;
0243       typename impl::choose_shared_vec<typename T::RunCache const>::type m_runs;
0244       typename impl::choose_shared_vec<typename T::LuminosityBlockCache const>::type m_lumis;
0245       typename impl::choose_shared_vec<typename T::RunSummaryCache>::type m_runSummaries;
0246       typename impl::choose_mutex<typename T::RunSummaryCache>::type m_runSummaryLock;
0247       typename impl::choose_shared_vec<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaries;
0248       typename impl::choose_mutex<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaryLock;
0249       ParameterSet const* m_pset;
0250     };
0251   }  // namespace stream
0252 
0253   template <>
0254   class MakeModuleHelper<edm::stream::EDAnalyzerAdaptorBase> {
0255     typedef edm::stream::EDAnalyzerAdaptorBase Base;
0256 
0257   public:
0258     template <typename ModType>
0259     static std::unique_ptr<Base> makeModule(ParameterSet const& pset) {
0260       typedef typename stream::BaseToAdaptor<Base, ModType>::Type Adaptor;
0261       auto module = std::make_unique<Adaptor>(pset);
0262       return std::unique_ptr<Base>(module.release());
0263     }
0264   };
0265 
0266 }  // namespace edm
0267 
0268 #endif