File indexing completed on 2024-04-06 12:10:08
0001 #ifndef DQMServices_Core_DQMEDAnalyzer_h
0002 #define DQMServices_Core_DQMEDAnalyzer_h
0003
0004 #include "FWCore/Framework/interface/stream/makeGlobal.h"
0005
0006 struct DQMEDAnalyzerGlobalCache;
0007
0008
0009
0010
0011
0012
0013 namespace edm::stream::impl {
0014 template <typename T>
0015 T* makeStreamModule(edm::ParameterSet const& iPSet, DQMEDAnalyzerGlobalCache const* global) {
0016 return new T(iPSet);
0017 }
0018 }
0019
0020 #include "DQMServices/Core/interface/DQMStore.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/Run.h"
0023 #include "FWCore/ServiceRegistry/interface/Service.h"
0024 #include "FWCore/Framework/interface/stream/EDProducer.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "DataFormats/Histograms/interface/DQMToken.h"
0027
0028 struct DQMEDAnalyzerGlobalCache {
0029
0030
0031 mutable std::mutex master_;
0032 mutable edm::EDPutTokenT<DQMToken> lumiToken_;
0033 mutable edm::EDPutTokenT<DQMToken> runToken_;
0034 };
0035
0036
0037
0038
0039 class DQMEDAnalyzer : public edm::stream::EDProducer<edm::GlobalCache<DQMEDAnalyzerGlobalCache>,
0040 edm::EndRunProducer,
0041 edm::EndLuminosityBlockProducer,
0042 edm::Accumulator> {
0043 public:
0044 typedef dqm::reco::DQMStore DQMStore;
0045 typedef dqm::reco::MonitorElement MonitorElement;
0046
0047 virtual bool getCanSaveByLumi() { return true; }
0048
0049
0050
0051 static std::unique_ptr<DQMEDAnalyzerGlobalCache> initializeGlobalCache(edm::ParameterSet const&) {
0052 return std::make_unique<DQMEDAnalyzerGlobalCache>();
0053 }
0054
0055 DQMEDAnalyzer() {
0056
0057 runToken_ = this->template produces<DQMToken, edm::Transition::EndRun>("DQMGenerationRecoRun");
0058 lumiToken_ = this->template produces<DQMToken, edm::Transition::EndLuminosityBlock>("DQMGenerationRecoLumi");
0059 streamId_ = edm::StreamID::invalidStreamID().value();
0060 }
0061
0062 void beginStream(edm::StreamID id) final {
0063 assert(streamId_ == edm::StreamID::invalidStreamID().value() || streamId_ == id.value());
0064 this->streamId_ = id.value();
0065
0066
0067
0068
0069
0070 auto lock = std::scoped_lock(globalCache()->master_);
0071 if (globalCache()->runToken_.isUninitialized()) {
0072 globalCache()->lumiToken_ = lumiToken_;
0073 globalCache()->runToken_ = runToken_;
0074 }
0075 }
0076
0077 void beginRun(edm::Run const& run, edm::EventSetup const& setup) final {
0078
0079
0080
0081 edm::Service<DQMStore>()->initLumi(run.run(), 0, meId());
0082 edm::Service<DQMStore>()->enterLumi(run.run(), 0, meId());
0083 dqmBeginRun(run, setup);
0084 edm::Service<DQMStore>()->bookTransaction(
0085 [this, &run, &setup](DQMStore::IBooker& booker) {
0086 booker.cd();
0087 this->bookHistograms(booker, run, setup);
0088 },
0089 meId(),
0090 this->getCanSaveByLumi());
0091 edm::Service<DQMStore>()->initLumi(run.run(), 0, meId());
0092 edm::Service<DQMStore>()->enterLumi(run.run(), 0, meId());
0093 }
0094
0095 void beginLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const& setup) final {
0096 edm::Service<DQMStore>()->initLumi(lumi.run(), lumi.luminosityBlock(), meId());
0097 edm::Service<DQMStore>()->enterLumi(lumi.run(), lumi.luminosityBlock(), meId());
0098 }
0099
0100 void accumulate(edm::Event const& event, edm::EventSetup const& setup) final { analyze(event, setup); }
0101
0102 void endLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const& setup) final {
0103 edm::Service<DQMStore>()->leaveLumi(lumi.run(), lumi.luminosityBlock(), meId());
0104 }
0105
0106 static void globalEndLuminosityBlockProduce(edm::LuminosityBlock& lumi,
0107 edm::EventSetup const& setup,
0108 LuminosityBlockContext const* context) {
0109 lumi.emplace(context->global()->lumiToken_);
0110 }
0111
0112 void endRun(edm::Run const& run, edm::EventSetup const& setup) final {
0113 edm::Service<DQMStore>()->leaveLumi(run.run(), 0, meId());
0114 }
0115 static void globalEndRunProduce(edm::Run& run, edm::EventSetup const& setup, RunContext const* context) {
0116 run.emplace<DQMToken>(context->global()->runToken_);
0117 }
0118
0119 static void globalEndJob(DQMEDAnalyzerGlobalCache const*) {}
0120
0121
0122 virtual void dqmBeginRun(edm::Run const&, edm::EventSetup const&) {}
0123 virtual void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) = 0;
0124 virtual void analyze(edm::Event const&, edm::EventSetup const&) {}
0125
0126 protected:
0127 edm::EDPutTokenT<DQMToken> runToken_;
0128 edm::EDPutTokenT<DQMToken> lumiToken_;
0129 unsigned int streamId_;
0130 uint64_t meId() const { return (((uint64_t)streamId_) << 32) + this->moduleDescription().id(); }
0131 };
0132
0133 #endif