File indexing completed on 2023-03-17 11:02:29
0001 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0002 #include "FWCore/Framework/interface/global/EDProducer.h"
0003 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/Utilities/interface/EDMException.h"
0009
0010 #include <algorithm>
0011 #include <string>
0012 #include <vector>
0013
0014 namespace {
0015
0016 std::vector<std::string> g_constructed;
0017 std::vector<std::string> g_destructed;
0018
0019 std::vector<std::string> g_lumi_constructed;
0020 std::vector<std::string> g_lumi_destructed;
0021
0022 std::vector<std::string> g_run_constructed;
0023 std::vector<std::string> g_run_destructed;
0024
0025 std::vector<std::string> g_process_constructed;
0026 std::vector<std::string> g_process_destructed;
0027 }
0028
0029 namespace edmtest {
0030
0031 class TestModuleDeleteProducer : public edm::global::EDProducer<> {
0032 public:
0033 explicit TestModuleDeleteProducer(edm::ParameterSet const& p)
0034 : moduleLabel_{p.getParameter<std::string>("@module_label")} {
0035 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginProcess")) {
0036 consumes<IntProduct, edm::InProcess>(tag);
0037 }
0038 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun")) {
0039 consumes<IntProduct, edm::InRun>(tag);
0040 }
0041 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi")) {
0042 consumes<IntProduct, edm::InLumi>(tag);
0043 }
0044 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent")) {
0045 consumes<IntProduct>(tag);
0046 }
0047 produces<IntProduct>();
0048 g_constructed.push_back(moduleLabel_);
0049 }
0050 ~TestModuleDeleteProducer() override { g_destructed.push_back(moduleLabel_); }
0051 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0052 edm::ParameterSetDescription desc;
0053 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginProcess", std::vector<edm::InputTag>{});
0054 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
0055 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
0056 desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
0057 descriptions.addDefault(desc);
0058 }
0059 void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override {
0060 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0061 }
0062
0063 private:
0064 std::string moduleLabel_;
0065 };
0066
0067 class TestModuleDeleteInLumiProducer : public edm::global::EDProducer<edm::BeginLuminosityBlockProducer> {
0068 public:
0069 explicit TestModuleDeleteInLumiProducer(edm::ParameterSet const& p)
0070 : moduleLabel_{p.getParameter<std::string>("@module_label")} {
0071 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginProcess")) {
0072 consumes<IntProduct, edm::InProcess>(tag);
0073 }
0074 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun")) {
0075 consumes<IntProduct, edm::InRun>(tag);
0076 }
0077 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi")) {
0078 consumes<IntProduct, edm::InLumi>(tag);
0079 }
0080 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent")) {
0081 consumes<IntProduct, edm::InRun>(tag);
0082 }
0083 produces<IntProduct, edm::Transition::BeginLuminosityBlock>();
0084 g_lumi_constructed.push_back(moduleLabel_);
0085 }
0086 ~TestModuleDeleteInLumiProducer() override { g_lumi_destructed.push_back(moduleLabel_); }
0087 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0088 edm::ParameterSetDescription desc;
0089 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginProcess", std::vector<edm::InputTag>{});
0090 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
0091 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
0092 desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
0093 descriptions.addDefault(desc);
0094 }
0095 void globalBeginLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const override {
0096 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0097 }
0098 void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override {
0099 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0100 }
0101
0102 private:
0103 std::string moduleLabel_;
0104 };
0105
0106 class TestModuleDeleteInRunProducer : public edm::global::EDProducer<edm::BeginRunProducer> {
0107 public:
0108 explicit TestModuleDeleteInRunProducer(edm::ParameterSet const& p)
0109 : moduleLabel_{p.getParameter<std::string>("@module_label")} {
0110 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginProcess")) {
0111 consumes<IntProduct, edm::InProcess>(tag);
0112 }
0113 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun")) {
0114 consumes<IntProduct, edm::InRun>(tag);
0115 }
0116 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi")) {
0117 consumes<IntProduct, edm::InLumi>(tag);
0118 }
0119 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent")) {
0120 consumes<IntProduct, edm::InRun>(tag);
0121 }
0122 produces<IntProduct, edm::Transition::BeginRun>();
0123 g_run_constructed.push_back(moduleLabel_);
0124 }
0125 ~TestModuleDeleteInRunProducer() override { g_run_destructed.push_back(moduleLabel_); }
0126 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0127 edm::ParameterSetDescription desc;
0128 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginProcess", std::vector<edm::InputTag>{});
0129 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
0130 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
0131 desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
0132 descriptions.addDefault(desc);
0133 }
0134 void globalBeginRunProduce(edm::Run&, edm::EventSetup const&) const override {
0135 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0136 }
0137 void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override {
0138 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0139 }
0140
0141 private:
0142 std::string moduleLabel_;
0143 };
0144
0145 class TestModuleDeleteInProcessProducer : public edm::global::EDProducer<edm::BeginProcessBlockProducer> {
0146 public:
0147 explicit TestModuleDeleteInProcessProducer(edm::ParameterSet const& p)
0148 : moduleLabel_{p.getParameter<std::string>("@module_label")} {
0149 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginRun")) {
0150 consumes<IntProduct, edm::InRun>(tag);
0151 }
0152 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcBeginLumi")) {
0153 consumes<IntProduct, edm::InLumi>(tag);
0154 }
0155 for (const auto& tag : p.getUntrackedParameter<std::vector<edm::InputTag>>("srcEvent")) {
0156 consumes<IntProduct, edm::InRun>(tag);
0157 }
0158 produces<IntProduct, edm::Transition::BeginProcessBlock>();
0159 g_process_constructed.push_back(moduleLabel_);
0160 }
0161 ~TestModuleDeleteInProcessProducer() override { g_process_destructed.push_back(moduleLabel_); }
0162 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0163 edm::ParameterSetDescription desc;
0164 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginRun", std::vector<edm::InputTag>{});
0165 desc.addUntracked<std::vector<edm::InputTag>>("srcBeginLumi", std::vector<edm::InputTag>{});
0166 desc.addUntracked<std::vector<edm::InputTag>>("srcEvent", std::vector<edm::InputTag>{});
0167 descriptions.addDefault(desc);
0168 }
0169 void beginProcessBlockProduce(edm::ProcessBlock&) override {
0170 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0171 }
0172 void produce(edm::StreamID, edm::Event& e, edm::EventSetup const& c) const override {
0173 throw edm::Exception(edm::errors::NotFound) << "Intentional 'NotFound' exception for testing purposes\n";
0174 }
0175
0176 private:
0177 std::string moduleLabel_;
0178 };
0179
0180 class TestModuleDeleteAnalyzer : public edm::global::EDAnalyzer<> {
0181 public:
0182 explicit TestModuleDeleteAnalyzer(edm::ParameterSet const& ){};
0183
0184 void beginJob() override {
0185 if (g_constructed.size() == 0) {
0186 throw cms::Exception("LogicError")
0187 << "No TestModuleDeleteProducer modules constructed in this job, the test is meaningless";
0188 }
0189
0190 auto formatException =
0191 [](std::vector<std::string>& constructed, std::vector<std::string>& destructed, cms::Exception& ex) {
0192 ex << " Modules constructed but not destructed:\n";
0193 auto newEnd = std::remove_if(constructed.begin(), constructed.end(), [&](const std::string& label) {
0194 auto found = std::find(destructed.begin(), destructed.end(), label);
0195 if (found != destructed.end()) {
0196 destructed.erase(found);
0197 return true;
0198 }
0199 return false;
0200 });
0201 constructed.erase(newEnd, constructed.end());
0202 for (const auto& lab : constructed) {
0203 ex << " " << lab << "\n";
0204 }
0205 };
0206
0207 if (g_constructed.size() != g_destructed.size()) {
0208 cms::Exception ex("Assert");
0209 ex << "Number of TestModuleDeleteProducer constructors " << g_constructed.size()
0210 << " differs from the number of destructors " << g_destructed.size() << ".";
0211 formatException(g_constructed, g_destructed, ex);
0212 throw ex;
0213 }
0214
0215 if (g_lumi_constructed.size() == 0) {
0216 throw cms::Exception("LogicError")
0217 << "No TestModuleDeleteInLumiProducer modules constructed in this job, the test is meaningless";
0218 }
0219 if (g_lumi_constructed.size() != g_lumi_destructed.size()) {
0220 cms::Exception ex("Assert");
0221 ex << "Number of TestModuleDeleteInLumiProducer constructors " << g_lumi_constructed.size()
0222 << " differs from the number of destructors " << g_lumi_destructed.size() << ".";
0223 formatException(g_lumi_constructed, g_lumi_destructed, ex);
0224 throw ex;
0225 }
0226
0227 if (g_run_constructed.size() == 0) {
0228 throw cms::Exception("LogicError")
0229 << "No TestModuleDeleteInRunProducer modules constructed in this job, the test is meaningless";
0230 }
0231 if (g_run_constructed.size() != g_run_destructed.size()) {
0232 cms::Exception ex("Assert");
0233 ex << "Number of TestModuleDeleteInRunProducer constructors " << g_run_constructed.size()
0234 << " differs from the number of destructors " << g_run_destructed.size() << ".";
0235 formatException(g_run_constructed, g_run_destructed, ex);
0236 throw ex;
0237 }
0238
0239 if (g_process_constructed.size() == 0) {
0240 throw cms::Exception("LogicError")
0241 << "No TestModuleDeleteInProcessProducer modules constructed in this job, the test is meaningless";
0242 }
0243 if (g_process_constructed.size() != g_process_destructed.size()) {
0244 cms::Exception ex("Assert");
0245 ex << "Number of TestModuleDeleteInProcessProducer constructors " << g_process_constructed.size()
0246 << " differs from the number of destructors " << g_process_destructed.size() << ".";
0247 formatException(g_process_constructed, g_process_destructed, ex);
0248 throw ex;
0249 }
0250 }
0251
0252 void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& c) const override {}
0253 };
0254 }
0255
0256 DEFINE_FWK_MODULE(edmtest::TestModuleDeleteProducer);
0257 DEFINE_FWK_MODULE(edmtest::TestModuleDeleteInLumiProducer);
0258 DEFINE_FWK_MODULE(edmtest::TestModuleDeleteInRunProducer);
0259 DEFINE_FWK_MODULE(edmtest::TestModuleDeleteInProcessProducer);
0260 DEFINE_FWK_MODULE(edmtest::TestModuleDeleteAnalyzer);