Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-17 23:36:16

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     virtual void analyze(const edm::Event&, const edm::EventSetup&);
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     virtual void analyze(const edm::Event&, const edm::EventSetup&);
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 ESTestAnalyzerK : public edm::global::EDAnalyzer<> {
0117   public:
0118     explicit ESTestAnalyzerK(edm::ParameterSet const&);
0119     void analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const override;
0120 
0121   private:
0122     const std::vector<int> runsToGetDataFor_;
0123     const edm::ESGetToken<ESTestDataK, ESTestRecordK> dataKToken_;
0124   };
0125 
0126   ESTestAnalyzerK::ESTestAnalyzerK(edm::ParameterSet const& pset)
0127       : runsToGetDataFor_(pset.getParameter<std::vector<int>>("runsToGetDataFor")), dataKToken_(esConsumes()) {}
0128 
0129   void ESTestAnalyzerK::analyze(edm::StreamID, edm::Event const& ev, edm::EventSetup const& es) const {
0130     if (std::find(runsToGetDataFor_.begin(), runsToGetDataFor_.end(), ev.run()) != runsToGetDataFor_.end()) {
0131       ESTestDataK const& dataK = es.getData(dataKToken_);
0132       edm::LogAbsolute("ESTestAnalyzerK")
0133           << "ESTestAnalyzerK: process = " << moduleDescription().processName() << ": Data value = " << dataK.value();
0134     }
0135   }
0136 
0137   class ESTestAnalyzerAZ : public edm::global::EDAnalyzer<> {
0138   public:
0139     explicit ESTestAnalyzerAZ(edm::ParameterSet const&);
0140     void analyze(edm::StreamID, const edm::Event&, const edm::EventSetup&) const override;
0141 
0142     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0143 
0144   private:
0145     std::vector<int> const runsToGetDataFor_;
0146     std::vector<int> const expectedValuesA_;
0147     std::vector<int> const expectedValuesZ_;
0148     edm::ESGetToken<ESTestDataA, ESTestRecordA> dataAToken_;
0149     edm::ESGetToken<ESTestDataZ, ESTestRecordZ> dataZToken_;
0150   };
0151 
0152   ESTestAnalyzerAZ::ESTestAnalyzerAZ(edm::ParameterSet const& pset)
0153       : runsToGetDataFor_(pset.getParameter<std::vector<int>>("runsToGetDataFor")),
0154         expectedValuesA_(pset.getUntrackedParameter<std::vector<int>>("expectedValuesA")),
0155         expectedValuesZ_(pset.getUntrackedParameter<std::vector<int>>("expectedValuesZ")),
0156         dataAToken_(esConsumes(edm::ESInputTag("", "foo"))),
0157         dataZToken_(esConsumes(edm::ESInputTag("", "foo"))) {
0158     assert(expectedValuesA_.empty() or expectedValuesA_.size() == runsToGetDataFor_.size());
0159     assert(expectedValuesZ_.empty() or expectedValuesZ_.size() == runsToGetDataFor_.size());
0160   }
0161 
0162   void ESTestAnalyzerAZ::analyze(edm::StreamID, edm::Event const& ev, edm::EventSetup const& es) const {
0163     auto found = std::find(runsToGetDataFor_.begin(), runsToGetDataFor_.end(), ev.run());
0164 
0165     if (found != runsToGetDataFor_.end()) {
0166       ESTestDataA const& dataA = es.getData(dataAToken_);
0167 
0168       ESTestDataZ const& dataZ = es.getData(dataZToken_);
0169 
0170       edm::LogAbsolute("ESTestAnalyzerAZ") << "ESTestAnalyzerAZ: process = " << moduleDescription().processName()
0171                                            << ": Data values = " << dataA.value() << "  " << dataZ.value();
0172 
0173       if (not expectedValuesA_.empty()) {
0174         if (expectedValuesA_[found - runsToGetDataFor_.begin()] != dataA.value()) {
0175           throw cms::Exception("TestError")
0176               << "Exptected value for A " << expectedValuesA_[found - runsToGetDataFor_.begin()] << " but saw "
0177               << dataA.value();
0178         }
0179       }
0180 
0181       if (not expectedValuesZ_.empty()) {
0182         if (expectedValuesZ_[found - runsToGetDataFor_.begin()] != dataZ.value()) {
0183           throw cms::Exception("TestError")
0184               << "Exptected value for Z " << expectedValuesZ_[found - runsToGetDataFor_.begin()] << " but saw "
0185               << dataZ.value();
0186         }
0187       }
0188     }
0189   }
0190 
0191   void ESTestAnalyzerAZ::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0192     edm::ParameterSetDescription desc;
0193     desc.setComment("Test module for the EventSetup");
0194     desc.add<std::vector<int>>("runsToGetDataFor")
0195         ->setComment("ID number for each Run for which we should get EventSetup data.");
0196     desc.addUntracked<std::vector<int>>("expectedValuesA", std::vector<int>())
0197         ->setComment("EventSetup value for ESTestDataA:foo expected for each Run. If empty, no values compared.");
0198     desc.addUntracked<std::vector<int>>("expectedValuesZ", std::vector<int>())
0199         ->setComment("EventSetup value for ESTestDataZ:foo expected for each Run. If empty, no values compared.");
0200     descriptions.addDefault(desc);
0201   }
0202 
0203   class ESTestAnalyzerJ : public edm::stream::EDAnalyzer<> {
0204   public:
0205     explicit ESTestAnalyzerJ(edm::ParameterSet const&) : dataJToken_(esConsumes()) {}
0206     void analyze(const edm::Event&, const edm::EventSetup&) override;
0207 
0208   private:
0209     const edm::ESGetToken<ESTestDataJ, ESTestRecordJ> dataJToken_;
0210   };
0211 
0212   void ESTestAnalyzerJ::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0213     ESTestDataJ const& dataJ = es.getData(dataJToken_);
0214     edm::LogAbsolute("ESTestAnalyzerJ") << "ESTestAnalyzerJ: process = " << moduleDescription().processName()
0215                                         << ": Data values = " << dataJ.value();
0216   }
0217 
0218   class ESTestAnalyzerL : public edm::stream::EDAnalyzer<> {
0219   public:
0220     explicit ESTestAnalyzerL(edm::ParameterSet const& iConfig)
0221         : edToken_(consumes(iConfig.getParameter<edm::InputTag>("src"))), esToken_(esConsumes()) {}
0222     void analyze(const edm::Event&, const edm::EventSetup&) override;
0223 
0224   private:
0225     edm::EDGetTokenT<IntProduct> edToken_;
0226     edm::ESGetToken<ESTestDataJ, ESTestRecordJ> esToken_;
0227   };
0228 
0229   void ESTestAnalyzerL::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0230     auto const& intData = ev.get(edToken_);
0231     auto const& dataJ = es.getData(esToken_);
0232     edm::LogAbsolute("ESTestAnalyzerJ") << "ESTestAnalyzerL: process = " << moduleDescription().processName()
0233                                         << ": ED value " << intData.value << ": ES value = " << dataJ.value();
0234   }
0235 
0236   class ESTestAnalyzerIncorrectConsumes : public edm::stream::EDAnalyzer<> {
0237   public:
0238     explicit ESTestAnalyzerIncorrectConsumes(edm::ParameterSet const& iConfig){};
0239     void analyze(const edm::Event&, const edm::EventSetup&) override;
0240 
0241   private:
0242     edm::ESGetToken<ESTestDataJ, ESTestRecordJ> esToken_;
0243   };
0244 
0245   void ESTestAnalyzerIncorrectConsumes::analyze(edm::Event const& ev, edm::EventSetup const& es) {
0246     esToken_ = esConsumes();
0247     edm::LogAbsolute("ESTestAnalyzerIncorrectConsumes")
0248         << "Succeeded to call esConsumes() in analyze(), should not happen!";
0249   }
0250 
0251 }  // namespace edmtest
0252 using namespace edmtest;
0253 DEFINE_FWK_MODULE(ESTestAnalyzerA);
0254 DEFINE_FWK_MODULE(ESTestAnalyzerB);
0255 DEFINE_FWK_MODULE(ESTestAnalyzerK);
0256 DEFINE_FWK_MODULE(ESTestAnalyzerAZ);
0257 DEFINE_FWK_MODULE(ESTestAnalyzerJ);
0258 DEFINE_FWK_MODULE(ESTestAnalyzerL);
0259 DEFINE_FWK_MODULE(ESTestAnalyzerIncorrectConsumes);