Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-14 23:36:18

0001 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0002 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0003 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0004 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0005 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/EventSetup.h"
0009 #include "FWCore/Framework/interface/ESHandle.h"
0010 #include "FWCore/Integration/interface/ESTestData.h"
0011 #include "FWCore/Integration/interface/ESTestRecords.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/Framework/interface/MakerMacros.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0016 #include "FWCore/Utilities/interface/ESGetToken.h"
0017 
0018 #include <algorithm>
0019 #include <vector>
0020 
0021 namespace edmtest {
0022 
0023   class ESTestAnalyzerA : public edm::stream::EDAnalyzer<> {
0024   public:
0025     explicit ESTestAnalyzerA(edm::ParameterSet const&);
0026     void analyze(const edm::Event&, const edm::EventSetup&) override;
0027 
0028     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0029 
0030   private:
0031     std::vector<int> const runsToGetDataFor_;
0032     std::vector<int> const expectedValues_;
0033     edm::ESGetToken<ESTestDataA, ESTestRecordA> const token_;
0034   };
0035 
0036   ESTestAnalyzerA::ESTestAnalyzerA(edm::ParameterSet const& pset)
0037       : runsToGetDataFor_(pset.getParameter<std::vector<int>>("runsToGetDataFor")),
0038         expectedValues_(pset.getUntrackedParameter<std::vector<int>>("expectedValues")),
0039         token_(esConsumes()) {
0040     assert(expectedValues_.empty() or expectedValues_.size() == runsToGetDataFor_.size());
0041   }
0042 
0043   void ESTestAnalyzerA::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0044     auto found = std::find(runsToGetDataFor_.begin(), runsToGetDataFor_.end(), ev.run());
0045     if (found != runsToGetDataFor_.end()) {
0046       auto const& dataA = es.getData(token_);
0047       edm::LogAbsolute("ESTestAnalyzerA")
0048           << "ESTestAnalyzerA: process = " << moduleDescription().processName() << ": Data value = " << dataA.value();
0049       if (not expectedValues_.empty()) {
0050         if (expectedValues_[found - runsToGetDataFor_.begin()] != dataA.value()) {
0051           throw cms::Exception("TestError") << "Exptected value " << expectedValues_[found - runsToGetDataFor_.begin()]
0052                                             << " but saw " << dataA.value();
0053         }
0054       }
0055     }
0056   }
0057 
0058   void ESTestAnalyzerA::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0059     //The following says we do not know what parameters are allowed so do no validation
0060     // Please change this to state exactly what you do use, even if it is no parameters
0061     edm::ParameterSetDescription desc;
0062     desc.setComment("Test module for the EventSetup");
0063     desc.add<std::vector<int>>("runsToGetDataFor")
0064         ->setComment("ID number for each Run for which we should get EventSetup data.");
0065     desc.addUntracked<std::vector<int>>("expectedValues", std::vector<int>())
0066         ->setComment("EventSetup value expected for each Run. If empty, no values compared.");
0067     descriptions.addDefault(desc);
0068   }
0069 
0070   class ESTestAnalyzerB : public edm::one::EDAnalyzer<> {
0071   public:
0072     explicit ESTestAnalyzerB(edm::ParameterSet const&);
0073     void analyze(const edm::Event&, const edm::EventSetup&) override;
0074 
0075     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0076 
0077   private:
0078     const std::vector<int> runsToGetDataFor_;
0079     const std::vector<int> expectedValues_;
0080     const edm::ESGetToken<ESTestDataB, ESTestRecordB> dataBToken_;
0081 
0082     unsigned int expectedIndex_ = 0;
0083   };
0084 
0085   ESTestAnalyzerB::ESTestAnalyzerB(edm::ParameterSet const& pset)
0086       : runsToGetDataFor_(pset.getParameter<std::vector<int>>("runsToGetDataFor")),
0087         expectedValues_(pset.getUntrackedParameter<std::vector<int>>("expectedValues")),
0088         dataBToken_(esConsumes()) {}
0089 
0090   void ESTestAnalyzerB::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0091     if (std::find(runsToGetDataFor_.begin(), runsToGetDataFor_.end(), ev.run()) != runsToGetDataFor_.end()) {
0092       edm::ESHandle<ESTestDataB> dataB = es.getHandle(dataBToken_);
0093       edm::LogAbsolute("ESTestAnalyzerB")
0094           << "ESTestAnalyzerB: process = " << moduleDescription().processName() << ": Data value = " << dataB->value();
0095 
0096       if (expectedIndex_ < expectedValues_.size()) {
0097         if (expectedValues_[expectedIndex_] != dataB->value()) {
0098           throw cms::Exception("TestError")
0099               << "Expected does not match actual value, "
0100               << "expected value = " << expectedValues_[expectedIndex_] << " actual value = " << dataB->value();
0101         }
0102         ++expectedIndex_;
0103       }
0104     }
0105   }
0106 
0107   void ESTestAnalyzerB::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0108     edm::ParameterSetDescription desc;
0109     desc.add<std::vector<int>>("runsToGetDataFor")
0110         ->setComment("ID number for each Run for which we should get EventSetup data.");
0111     desc.addUntracked<std::vector<int>>("expectedValues", std::vector<int>())
0112         ->setComment("EventSetup value expected for each Run. If empty, no values compared.");
0113     descriptions.addDefault(desc);
0114   }
0115 
0116   class ESTestAnalyzerJ : public edm::stream::EDAnalyzer<> {
0117   public:
0118     explicit ESTestAnalyzerJ(edm::ParameterSet const&) : dataJToken_(esConsumes()) {}
0119     void analyze(const edm::Event&, const edm::EventSetup&) override;
0120 
0121   private:
0122     const edm::ESGetToken<ESTestDataJ, ESTestRecordJ> dataJToken_;
0123   };
0124 
0125   void ESTestAnalyzerJ::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0126     ESTestDataJ const& dataJ = es.getData(dataJToken_);
0127     edm::LogAbsolute("ESTestAnalyzerJ") << "ESTestAnalyzerJ: process = " << moduleDescription().processName()
0128                                         << ": Data values = " << dataJ.value();
0129   }
0130 
0131   class ESTestAnalyzerL : public edm::stream::EDAnalyzer<> {
0132   public:
0133     explicit ESTestAnalyzerL(edm::ParameterSet const& iConfig)
0134         : edToken_(consumes(iConfig.getParameter<edm::InputTag>("src"))), esToken_(esConsumes()) {}
0135     void analyze(const edm::Event&, const edm::EventSetup&) override;
0136 
0137   private:
0138     edm::EDGetTokenT<IntProduct> edToken_;
0139     edm::ESGetToken<ESTestDataJ, ESTestRecordJ> esToken_;
0140   };
0141 
0142   void ESTestAnalyzerL::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0143     auto const& intData = ev.get(edToken_);
0144     auto const& dataJ = es.getData(esToken_);
0145     edm::LogAbsolute("ESTestAnalyzerJ") << "ESTestAnalyzerL: process = " << moduleDescription().processName()
0146                                         << ": ED value " << intData.value << ": ES value = " << dataJ.value();
0147   }
0148 
0149   class ESTestAnalyzerIncorrectConsumes : public edm::stream::EDAnalyzer<> {
0150   public:
0151     explicit ESTestAnalyzerIncorrectConsumes(edm::ParameterSet const& iConfig) {};
0152     void analyze(const edm::Event&, const edm::EventSetup&) override;
0153 
0154   private:
0155     edm::ESGetToken<ESTestDataJ, ESTestRecordJ> esToken_;
0156   };
0157 
0158   void ESTestAnalyzerIncorrectConsumes::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0159     esToken_ = esConsumes();
0160     edm::LogAbsolute("ESTestAnalyzerIncorrectConsumes")
0161         << "Succeeded to call esConsumes() in analyze(), should not happen!";
0162   }
0163 
0164 }  // namespace edmtest
0165 using namespace edmtest;
0166 DEFINE_FWK_MODULE(ESTestAnalyzerA);
0167 DEFINE_FWK_MODULE(ESTestAnalyzerB);
0168 DEFINE_FWK_MODULE(ESTestAnalyzerJ);
0169 DEFINE_FWK_MODULE(ESTestAnalyzerL);
0170 DEFINE_FWK_MODULE(ESTestAnalyzerIncorrectConsumes);