Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    DummyReadDQMStore
0004 // Class:      DummyReadDQMStore
0005 //
0006 /**\class DummyReadDQMStore DummyReadDQMStore.cc DQMServices/DummyReadDQMStore/src/DummyReadDQMStore.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Christopher Jones
0015 //         Created:  Fri Apr 29 18:05:50 CDT 2011
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/EDAnalyzer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 
0031 #include "DQMServices/Core/interface/DQMStore.h"
0032 #include "FWCore/ServiceRegistry/interface/Service.h"
0033 
0034 //
0035 // class declaration
0036 //
0037 namespace {
0038   typedef dqm::legacy::MonitorElement MonitorElement;
0039   typedef dqm::legacy::DQMStore DQMStore;
0040 
0041   class ReaderBase {
0042   public:
0043     virtual ~ReaderBase() = default;
0044     virtual void read(int run, int lumi) = 0;
0045   };
0046 
0047   class TH1Reader : public ReaderBase {
0048   public:
0049     TH1Reader(const edm::ParameterSet& iPSet, DQMStore& iStore, bool iSetLumiFlag)
0050         : m_store(&iStore),
0051           m_element(nullptr),
0052           m_runs(iPSet.getUntrackedParameter<std::vector<int> >("runs")),
0053           m_lumis(iPSet.getUntrackedParameter<std::vector<int> >("lumis")),
0054           m_means(iPSet.getUntrackedParameter<std::vector<double> >("means")),
0055           m_entries(iPSet.getUntrackedParameter<std::vector<double> >("entries")) {
0056       assert(m_means.size() == m_entries.size());
0057       std::string extension;
0058       if (iSetLumiFlag) {
0059         extension = "_lumi";
0060       }
0061       m_name = iPSet.getUntrackedParameter<std::string>("name") + extension;
0062     }
0063 
0064     ~TH1Reader() override{};
0065 
0066     void read(int run, int lumi) override {
0067       double expected_mean = -1, expected_entries = -1;
0068       for (unsigned int i = 0; i < m_runs.size(); i++) {
0069         if (m_runs[i] == run && m_lumis[i] == lumi) {
0070           expected_mean = m_means[i];
0071           expected_entries = m_entries[i];
0072         }
0073       }
0074       assert(expected_entries != -1 || !"Unexpected run/lumi!");
0075 
0076       m_element = m_store->get(m_name);
0077       if (nullptr == m_element) {
0078         throw cms::Exception("MissingElement") << "The element: " << m_name << " was not found";
0079       }
0080       TH1* hist = m_element->getTH1();
0081 
0082       if (hist->GetEntries() != expected_entries) {
0083         throw cms::Exception("WrongEntries")
0084             << "The element: " << m_name << " for run " << run << " lumi " << lumi << " was expected  to have "
0085             << expected_entries << " entries but instead has " << hist->GetEntries();
0086       }
0087 
0088       if (hist->GetMean() != expected_mean) {
0089         throw cms::Exception("WrongEntries")
0090             << "The element: " << m_name << " for run " << run << " lumi " << lumi << " was expected  to have "
0091             << expected_mean << " mean but instead has " << hist->GetMean();
0092       }
0093     }
0094 
0095   private:
0096     std::string m_name;
0097     DQMStore* m_store;
0098     MonitorElement* m_element;
0099     std::vector<int> m_runs;
0100     std::vector<int> m_lumis;
0101     std::vector<double> m_means;
0102     std::vector<double> m_entries;
0103   };
0104 
0105 }  // namespace
0106 
0107 class DummyReadDQMStore : public edm::EDAnalyzer {
0108 public:
0109   explicit DummyReadDQMStore(const edm::ParameterSet&);
0110   ~DummyReadDQMStore() override;
0111 
0112   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0113 
0114 private:
0115   void beginJob() override;
0116   void analyze(const edm::Event&, const edm::EventSetup&) override;
0117   void endJob() override;
0118 
0119   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0120   void endRun(edm::Run const&, edm::EventSetup const&) override;
0121   void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0122   void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0123 
0124   // ----------member data ---------------------------
0125   std::vector<std::shared_ptr<ReaderBase> > m_runReaders;
0126   std::vector<std::shared_ptr<ReaderBase> > m_lumiReaders;
0127 };
0128 
0129 //
0130 // constants, enums and typedefs
0131 //
0132 
0133 //
0134 // static data member definitions
0135 //
0136 
0137 //
0138 // constructors and destructor
0139 //
0140 DummyReadDQMStore::DummyReadDQMStore(const edm::ParameterSet& iConfig) {
0141   edm::Service<DQMStore> dstore;
0142 
0143   typedef std::vector<edm::ParameterSet> PSets;
0144   const PSets& runElements = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >("runElements");
0145   m_runReaders.reserve(runElements.size());
0146   for (PSets::const_iterator it = runElements.begin(), itEnd = runElements.end(); it != itEnd; ++it) {
0147     switch (it->getUntrackedParameter<unsigned int>("type", 1)) {
0148       case 1:
0149         m_runReaders.push_back(std::shared_ptr<ReaderBase>(new TH1Reader(*it, *dstore, false)));
0150         break;
0151       case 2:
0152         m_runReaders.push_back(std::shared_ptr<ReaderBase>(new TH1Reader(*it, *dstore, false)));
0153         break;
0154     }
0155   }
0156 
0157   const PSets& lumiElements = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >("lumiElements");
0158   m_lumiReaders.reserve(lumiElements.size());
0159   for (PSets::const_iterator it = lumiElements.begin(), itEnd = lumiElements.end(); it != itEnd; ++it) {
0160     switch (it->getUntrackedParameter<unsigned int>("type", 1)) {
0161       case 1:
0162         m_lumiReaders.push_back(std::shared_ptr<ReaderBase>(new TH1Reader(*it, *dstore, true)));
0163         break;
0164       case 2:
0165         m_lumiReaders.push_back(std::shared_ptr<ReaderBase>(new TH1Reader(*it, *dstore, true)));
0166         break;
0167     }
0168   }
0169 }
0170 
0171 DummyReadDQMStore::~DummyReadDQMStore() {
0172   // do anything here that needs to be done at desctruction time
0173   // (e.g. close files, deallocate resources etc.)
0174 }
0175 
0176 //
0177 // member functions
0178 //
0179 
0180 // ------------ method called to produce the data  ------------
0181 void DummyReadDQMStore::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0182   using namespace edm;
0183   /* This is an event example
0184    //Read 'ExampleData' from the Event
0185    Handle<ExampleData> pIn;
0186    iEvent.getByLabel("example",pIn);
0187 
0188    //Use the ExampleData to create an ExampleData2 which 
0189    // is put into the Event
0190    std::unique_ptr<ExampleData2> pOut(new ExampleData2(*pIn));
0191    iEvent.put(pOut);
0192 */
0193 
0194   /* this is an EventSetup example
0195    //Read SetupData from the SetupRecord in the EventSetup
0196    ESHandle<SetupData> pSetup;
0197    iSetup.get<SetupRecord>().get(pSetup);
0198 */
0199 }
0200 
0201 // ------------ method called once each job just before starting event loop  ------------
0202 void DummyReadDQMStore::beginJob() {}
0203 
0204 // ------------ method called once each job just after ending the event loop  ------------
0205 void DummyReadDQMStore::endJob() {}
0206 
0207 // ------------ method called when starting to processes a run  ------------
0208 void DummyReadDQMStore::beginRun(edm::Run const&, edm::EventSetup const&) {}
0209 
0210 // ------------ method called when ending the processing of a run  ------------
0211 void DummyReadDQMStore::endRun(edm::Run const& run, edm::EventSetup const&) {
0212   for (std::vector<std::shared_ptr<ReaderBase> >::iterator it = m_runReaders.begin(), itEnd = m_runReaders.end();
0213        it != itEnd;
0214        ++it) {
0215     (*it)->read(run.run(), 0);
0216   }
0217 }
0218 
0219 // ------------ method called when starting to processes a luminosity block  ------------
0220 void DummyReadDQMStore::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}
0221 
0222 // ------------ method called when ending the processing of a luminosity block  ------------
0223 void DummyReadDQMStore::endLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
0224   for (std::vector<std::shared_ptr<ReaderBase> >::iterator it = m_lumiReaders.begin(), itEnd = m_lumiReaders.end();
0225        it != itEnd;
0226        ++it) {
0227     (*it)->read(lumi.run(), lumi.luminosityBlock());
0228   }
0229 }
0230 
0231 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0232 void DummyReadDQMStore::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0233   //The following says we do not know what parameters are allowed so do no validation
0234   // Please change this to state exactly what you do use, even if it is no parameters
0235   edm::ParameterSetDescription desc;
0236   desc.setUnknown();
0237   descriptions.addDefault(desc);
0238 }
0239 
0240 //define this as a plug-in
0241 DEFINE_FWK_MODULE(DummyReadDQMStore);