Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:13:45

0001 #ifndef DQMServices_Core_DQMGlobalEDAnalyzer_h
0002 #define DQMServices_Core_DQMGlobalEDAnalyzer_h
0003 
0004 #include "DQMServices/Core/interface/DQMStore.h"
0005 #include "DataFormats/Histograms/interface/DQMToken.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/Run.h"
0008 #include "FWCore/Framework/interface/global/EDProducer.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 
0012 template <typename H, typename... Args>
0013 class DQMGlobalEDAnalyzerBase
0014     : public edm::global::EDProducer<edm::RunCache<H>,
0015                                      // DQMGlobalEDAnalyzer are fundamentally unable to produce histograms for any
0016                                      // other scope than MonitorElement::Scope::RUN.
0017                                      edm::EndRunProducer,
0018                                      edm::Accumulator,
0019                                      Args...> {
0020 public:
0021   typedef dqm::reco::DQMStore DQMStore;
0022   typedef dqm::reco::MonitorElement MonitorElement;
0023 
0024   // framework calls in the order of invocation
0025   DQMGlobalEDAnalyzerBase() {
0026     // for whatever reason we need the explicit `template` keyword here.
0027     runToken_ = this->template produces<DQMToken, edm::Transition::EndRun>("DQMGenerationRecoRun");
0028     dqmstore_ = edm::Service<DQMStore>().operator->();
0029   }
0030 
0031   std::shared_ptr<H> globalBeginRun(edm::Run const& run, edm::EventSetup const& setup) const final {
0032     auto h = std::make_shared<H>();
0033 
0034     dqmBeginRun(run, setup, *h);
0035 
0036     // in case of concurrent runs, this will create clones of the already
0037     // booked MEs.
0038     dqmstore_->bookTransaction(
0039         [&, this](DQMStore::IBooker& b) {
0040           // this runs while holding the DQMStore lock
0041           b.cd();
0042           bookHistograms(b, run, setup, *h);
0043         },
0044         // The run number is part of the module ID here, since we want distinct
0045         // local MEs for each run cache.
0046         meId(run),
0047         /* canSaveByLumi */ false);
0048     dqmstore_->initLumi(run.run(), /* lumi */ 0, meId(run));
0049     dqmstore_->enterLumi(run.run(), /* lumi */ 0, meId(run));
0050     return h;
0051   }
0052 
0053   void accumulate(edm::StreamID id, edm::Event const& event, edm::EventSetup const& setup) const final {
0054     auto const& h = *this->runCache(event.getRun().index());
0055     dqmAnalyze(event, setup, h);
0056   }
0057 
0058   // Subsystems could safely override this, but any changes to MEs would not be
0059   // noticeable since the product was made already.
0060   void globalEndRun(edm::Run const&, edm::EventSetup const&) const final{};
0061 
0062   // methods to be implemented by the user, in order of invocation
0063   virtual void dqmBeginRun(edm::Run const&, edm::EventSetup const&, H&) const {}
0064   virtual void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&, H&) const = 0;
0065   // TODO: rename this analyze() for consistency.
0066   virtual void dqmAnalyze(edm::Event const&, edm::EventSetup const&, H const&) const = 0;
0067 
0068 protected:
0069   DQMStore* dqmstore_;
0070   edm::EDPutTokenT<DQMToken> runToken_;
0071   uint64_t meId(edm::Run const& run) const { return (((uint64_t)run.run()) << 32) + this->moduleDescription().id(); }
0072 };
0073 
0074 // Case without RunSummaryCache
0075 template <typename H, typename... Args>
0076 class DQMGlobalEDAnalyzer : public DQMGlobalEDAnalyzerBase<H, Args...> {
0077 public:
0078   void globalEndRunProduce(edm::Run& run, edm::EventSetup const& setup) const final {
0079     auto const& h = *this->runCache(run.index());
0080     dqmEndRun(run, setup, h);
0081     this->dqmstore_->leaveLumi(run.run(), /* lumi */ 0, this->meId(run));
0082     run.emplace(this->runToken_);
0083   }
0084 
0085   virtual void dqmEndRun(edm::Run const&, edm::EventSetup const&, H const&) const {}
0086 };
0087 
0088 // Case with RunSummaryCache, must be the second template argument
0089 template <typename H,    // type for RunCache
0090           typename RSC,  // type for RunSummaryCache
0091           typename... Args>
0092 class DQMGlobalRunSummaryEDAnalyzer : public DQMGlobalEDAnalyzerBase<H, edm::RunSummaryCache<RSC>, Args...> {
0093 public:
0094   void globalEndRunProduce(edm::Run& run, edm::EventSetup const& setup, RSC const* runSummaryCache) const final {
0095     auto const& h = *this->runCache(run.index());
0096     dqmEndRun(run, setup, h, *runSummaryCache);
0097     this->dqmstore_->leaveLumi(run.run(), /* lumi */ 0, this->meId(run));
0098     run.emplace(this->runToken_);
0099   }
0100 
0101   virtual void dqmEndRun(edm::Run const&, edm::EventSetup const&, H const&, RSC const&) const {}
0102 };
0103 
0104 #endif  // DQMServices_Core_DQMGlobalEDAnalyzer_h