Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 final { return T::HasAbility::kWatchProcessBlock; }
0074       bool wantsInputProcessBlocks() const final { return T::HasAbility::kInputProcessBlockCache; }
0075       bool wantsGlobalRuns() const final { return T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache; }
0076       bool wantsGlobalLuminosityBlocks() const final {
0077         return T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache;
0078       }
0079 
0080     private:
0081       using MyGlobal = CallGlobal<T>;
0082       using MyInputProcessBlock = CallInputProcessBlock<T>;
0083       using MyWatchProcessBlock = CallWatchProcessBlock<T>;
0084       using MyGlobalRun = CallGlobalRun<T>;
0085       using MyGlobalRunSummary = CallGlobalRunSummary<T>;
0086       using MyGlobalLuminosityBlock = CallGlobalLuminosityBlock<T>;
0087       using MyGlobalLuminosityBlockSummary = CallGlobalLuminosityBlockSummary<T>;
0088 
0089       void setupStreamModules() final {
0090         this->createStreamModules([this](unsigned int iStreamModule) -> EDAnalyzerBase* {
0091           auto tmp = impl::makeStreamModule<T>(*m_pset, m_global.get());
0092           MyGlobal::set(tmp, m_global.get());
0093           MyInputProcessBlock::set(tmp, &m_inputProcessBlocks, iStreamModule);
0094           return tmp;
0095         });
0096         m_pset = nullptr;
0097       }
0098 
0099       void preallocLumis(unsigned int iNLumis) final {
0100         m_lumis.resize(iNLumis);
0101         m_lumiSummaries.resize(iNLumis);
0102       }
0103 
0104       void doBeginJob() final { MyGlobal::beginJob(m_global.get()); }
0105       void doEndJob() final { MyGlobal::endJob(m_global.get()); }
0106       void setupRun(EDAnalyzerBase* iProd, RunIndex iIndex) final { MyGlobalRun::set(iProd, m_runs[iIndex].get()); }
0107       void streamEndRunSummary(EDAnalyzerBase* iProd, edm::Run const& iRun, edm::EventSetup const& iES) final {
0108         auto s = m_runSummaries[iRun.index()].get();
0109         std::lock_guard<decltype(m_runSummaryLock)> guard(m_runSummaryLock);
0110         MyGlobalRunSummary::streamEndRunSummary(iProd, iRun, iES, s);
0111       }
0112 
0113       void setupLuminosityBlock(EDAnalyzerBase* iProd, LuminosityBlockIndex iIndex) final {
0114         MyGlobalLuminosityBlock::set(iProd, m_lumis[iIndex].get());
0115       }
0116       void streamEndLuminosityBlockSummary(EDAnalyzerBase* iProd,
0117                                            edm::LuminosityBlock const& iLumi,
0118                                            edm::EventSetup const& iES) final {
0119         auto s = m_lumiSummaries[iLumi.index()].get();
0120         std::lock_guard<decltype(m_lumiSummaryLock)> guard(m_lumiSummaryLock);
0121         MyGlobalLuminosityBlockSummary::streamEndLuminosityBlockSummary(iProd, iLumi, iES, s);
0122       }
0123 
0124       void doBeginProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0125         if constexpr (T::HasAbility::kWatchProcessBlock) {
0126           ProcessBlock processBlock(pbp, moduleDescription(), mcc, false);
0127           processBlock.setConsumer(consumer());
0128           ProcessBlock const& cnstProcessBlock = processBlock;
0129           MyWatchProcessBlock::beginProcessBlock(cnstProcessBlock, m_global.get());
0130         }
0131       }
0132 
0133       void doAccessInputProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0134         if constexpr (T::HasAbility::kInputProcessBlockCache) {
0135           ProcessBlock processBlock(pbp, moduleDescription(), mcc, false);
0136           processBlock.setConsumer(consumer());
0137           ProcessBlock const& cnstProcessBlock = processBlock;
0138           MyInputProcessBlock::accessInputProcessBlock(cnstProcessBlock, m_global.get(), m_inputProcessBlocks);
0139         }
0140       }
0141 
0142       void doEndProcessBlock(ProcessBlockPrincipal const& pbp, ModuleCallingContext const* mcc) final {
0143         if constexpr (T::HasAbility::kWatchProcessBlock) {
0144           ProcessBlock processBlock(pbp, moduleDescription(), mcc, true);
0145           processBlock.setConsumer(consumer());
0146           ProcessBlock const& cnstProcessBlock = processBlock;
0147           MyWatchProcessBlock::endProcessBlock(cnstProcessBlock, m_global.get());
0148         }
0149       }
0150 
0151       void doBeginRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0152         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache) {
0153           RunPrincipal const& rp = info.principal();
0154           Run r(rp, moduleDescription(), mcc, false);
0155           r.setConsumer(consumer());
0156           Run const& cnstR = r;
0157           RunIndex ri = rp.index();
0158           ESParentContext pc{mcc};
0159           const EventSetup c{info,
0160                              static_cast<unsigned int>(Transition::BeginRun),
0161                              this->consumer()->esGetTokenIndices(Transition::BeginRun),
0162                              pc};
0163           MyGlobalRun::beginRun(cnstR, c, m_global.get(), m_runs[ri]);
0164           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0165           MyGlobalRunSummary::beginRun(cnstR, c, &rc, m_runSummaries[ri]);
0166         }
0167       }
0168       void doEndRun(RunTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0169         if constexpr (T::HasAbility::kRunCache or T::HasAbility::kRunSummaryCache) {
0170           RunPrincipal const& rp = info.principal();
0171           Run r(rp, moduleDescription(), mcc, true);
0172           r.setConsumer(consumer());
0173 
0174           RunIndex ri = rp.index();
0175           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0176           ESParentContext pc{mcc};
0177           const EventSetup c{info,
0178                              static_cast<unsigned int>(Transition::EndRun),
0179                              this->consumer()->esGetTokenIndices(Transition::EndRun),
0180                              pc};
0181           MyGlobalRunSummary::globalEndRun(r, c, &rc, m_runSummaries[ri].get());
0182           MyGlobalRun::endRun(r, c, &rc);
0183         }
0184       }
0185 
0186       void doBeginLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0187         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache) {
0188           LuminosityBlockPrincipal const& lbp = info.principal();
0189           LuminosityBlock lb(lbp, moduleDescription(), mcc, false);
0190           lb.setConsumer(consumer());
0191           LuminosityBlock const& cnstLb = lb;
0192           LuminosityBlockIndex li = lbp.index();
0193           RunIndex ri = lbp.runPrincipal().index();
0194           typename T::RunContext rc(m_runs[ri].get(), m_global.get());
0195           ESParentContext pc{mcc};
0196           const EventSetup c{info,
0197                              static_cast<unsigned int>(Transition::BeginLuminosityBlock),
0198                              this->consumer()->esGetTokenIndices(Transition::BeginLuminosityBlock),
0199                              pc};
0200           MyGlobalLuminosityBlock::beginLuminosityBlock(cnstLb, c, &rc, m_lumis[li]);
0201           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0202           MyGlobalLuminosityBlockSummary::beginLuminosityBlock(cnstLb, c, &lc, m_lumiSummaries[li]);
0203         }
0204       }
0205       void doEndLuminosityBlock(LumiTransitionInfo const& info, ModuleCallingContext const* mcc) final {
0206         if constexpr (T::HasAbility::kLuminosityBlockCache or T::HasAbility::kLuminosityBlockSummaryCache) {
0207           LuminosityBlockPrincipal const& lbp = info.principal();
0208           LuminosityBlock lb(lbp, moduleDescription(), mcc, true);
0209           lb.setConsumer(consumer());
0210 
0211           LuminosityBlockIndex li = lbp.index();
0212           RunIndex ri = lbp.runPrincipal().index();
0213           typename T::LuminosityBlockContext lc(m_lumis[li].get(), m_runs[ri].get(), m_global.get());
0214           ESParentContext pc{mcc};
0215           const EventSetup c{info,
0216                              static_cast<unsigned int>(Transition::EndLuminosityBlock),
0217                              this->consumer()->esGetTokenIndices(Transition::EndLuminosityBlock),
0218                              pc};
0219           MyGlobalLuminosityBlockSummary::globalEndLuminosityBlock(lb, c, &lc, m_lumiSummaries[li].get());
0220           MyGlobalLuminosityBlock::endLuminosityBlock(lb, c, &lc);
0221         }
0222       }
0223 
0224       void doRespondToCloseOutputFile() final { MyInputProcessBlock::clearCaches(m_inputProcessBlocks); }
0225 
0226       void selectInputProcessBlocks(ProductRegistry const& productRegistry,
0227                                     ProcessBlockHelperBase const& processBlockHelperBase) final {
0228         MyInputProcessBlock::selectInputProcessBlocks(
0229             m_inputProcessBlocks, productRegistry, processBlockHelperBase, *consumer());
0230       }
0231 
0232       // ---------- member data --------------------------------
0233       typename impl::choose_unique_ptr<typename T::GlobalCache>::type m_global;
0234       typename impl::choose_unique_ptr<typename T::InputProcessBlockCache>::type m_inputProcessBlocks;
0235       typename impl::choose_shared_vec<typename T::RunCache const>::type m_runs;
0236       typename impl::choose_shared_vec<typename T::LuminosityBlockCache const>::type m_lumis;
0237       typename impl::choose_shared_vec<typename T::RunSummaryCache>::type m_runSummaries;
0238       typename impl::choose_mutex<typename T::RunSummaryCache>::type m_runSummaryLock;
0239       typename impl::choose_shared_vec<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaries;
0240       typename impl::choose_mutex<typename T::LuminosityBlockSummaryCache>::type m_lumiSummaryLock;
0241       ParameterSet const* m_pset;
0242     };
0243   }  // namespace stream
0244 
0245   template <>
0246   class MakeModuleHelper<edm::stream::EDAnalyzerAdaptorBase> {
0247     typedef edm::stream::EDAnalyzerAdaptorBase Base;
0248 
0249   public:
0250     template <typename ModType>
0251     static std::unique_ptr<Base> makeModule(ParameterSet const& pset) {
0252       typedef typename stream::BaseToAdaptor<Base, ModType>::Type Adaptor;
0253       auto module = std::make_unique<Adaptor>(pset);
0254       return std::unique_ptr<Base>(module.release());
0255     }
0256   };
0257 
0258 }  // namespace edm
0259 
0260 #endif