Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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