Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:17

0001 
0002 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0003 #include "FWCore/Framework/interface/one/EDFilter.h"
0004 #include "FWCore/Framework/interface/GetterOfProducts.h"
0005 #include "FWCore/Framework/interface/one/OutputModule.h"
0006 #include "FWCore/Framework/interface/global/OutputModule.h"
0007 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0008 #include "FWCore/Framework/interface/TypeMatch.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
0013 #include "FWCore/ServiceRegistry/interface/PathContext.h"
0014 #include "FWCore/ServiceRegistry/interface/PlaceInPathContext.h"
0015 #include "DataFormats/Common/interface/Handle.h"
0016 #include "DataFormats/Common/interface/TriggerResults.h"
0017 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0018 #include "FWCore/Framework/interface/MakerMacros.h"
0019 
0020 #include <string>
0021 #include <iostream>
0022 #include <algorithm>
0023 #include <numeric>
0024 #include <iterator>
0025 
0026 using namespace edm;
0027 
0028 namespace edm {
0029   class ModuleCallingContext;
0030 }
0031 
0032 namespace edmtest {
0033 
0034   class TestResultAnalyzer : public edm::one::EDAnalyzer<> {
0035   public:
0036     explicit TestResultAnalyzer(edm::ParameterSet const&);
0037     virtual ~TestResultAnalyzer();
0038 
0039     virtual void analyze(edm::Event const& e, edm::EventSetup const& c) override;
0040     void endJob() override;
0041 
0042   private:
0043     edm::GetterOfProducts<edm::TriggerResults> getterOfTriggerResults_;
0044     int passed_;
0045     int failed_;
0046     std::string name_;
0047     int numbits_;
0048   };
0049 
0050   class TestContextAnalyzer : public edm::global::EDAnalyzer<> {
0051   public:
0052     explicit TestContextAnalyzer(edm::ParameterSet const&);
0053 
0054     virtual void analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const& c) const override;
0055     std::string expected_pathname_;     // if empty, we don't know
0056     std::string expected_modulelabel_;  // if empty, we don't know
0057   };
0058 
0059   // -------
0060 
0061   class TestFilterModule : public edm::one::EDFilter<> {
0062   public:
0063     explicit TestFilterModule(edm::ParameterSet const&);
0064     virtual ~TestFilterModule();
0065 
0066     virtual bool filter(edm::Event& e, edm::EventSetup const& c) override;
0067     void endJob() override;
0068 
0069   private:
0070     int count_;
0071     int accept_rate_;  // how many out of 100 will be accepted?
0072     bool onlyOne_;
0073   };
0074 
0075   // -------
0076 
0077   class SewerModule : public edm::one::OutputModule<> {
0078   public:
0079     explicit SewerModule(edm::ParameterSet const&);
0080     virtual ~SewerModule();
0081 
0082     static void fillDescriptions(ConfigurationDescriptions& descriptions);
0083 
0084   private:
0085     virtual void write(edm::EventForOutput const& e) override;
0086     virtual void writeLuminosityBlock(edm::LuminosityBlockForOutput const&) override {}
0087     virtual void writeRun(edm::RunForOutput const&) override {}
0088     virtual void endJob() override;
0089 
0090     std::string name_;
0091     int num_pass_;
0092     int total_;
0093   };
0094 
0095   class ExternalWorkSewerModule : public edm::global::OutputModule<edm::ExternalWork> {
0096   public:
0097     explicit ExternalWorkSewerModule(edm::ParameterSet const&);
0098 
0099     static void fillDescriptions(ConfigurationDescriptions& descriptions);
0100 
0101   private:
0102     void write(edm::EventForOutput const& e) override;
0103     void acquire(edm::StreamID, edm::EventForOutput const& e, edm::WaitingTaskWithArenaHolder) const override;
0104     void writeLuminosityBlock(edm::LuminosityBlockForOutput const&) override {}
0105     void writeRun(edm::RunForOutput const&) override {}
0106     void endJob() override;
0107 
0108     const std::string name_;
0109     const int num_pass_;
0110     mutable std::atomic<int> total_;
0111     mutable std::atomic<int> totalAcquire_;
0112   };
0113 
0114   // -----------------------------------------------------------------
0115 
0116   TestResultAnalyzer::TestResultAnalyzer(edm::ParameterSet const& ps)
0117       : getterOfTriggerResults_(edm::TypeMatch(), this),
0118         passed_(),
0119         failed_(),
0120         name_(ps.getUntrackedParameter<std::string>("name", "DEFAULT")),
0121         numbits_(ps.getUntrackedParameter<int>("numbits", -1)) {
0122     callWhenNewProductsRegistered(getterOfTriggerResults_);
0123   }
0124 
0125   TestResultAnalyzer::~TestResultAnalyzer() {}
0126 
0127   void TestResultAnalyzer::analyze(edm::Event const& e, edm::EventSetup const&) {
0128     typedef std::vector<edm::Handle<edm::TriggerResults> > Trig;
0129     Trig prod;
0130     getterOfTriggerResults_.fillHandles(e, prod);
0131 
0132     if (prod.size() == 0)
0133       return;
0134     if (prod.size() > 1) {
0135       std::cerr << "More than one trigger result in the event, using first one" << std::endl;
0136     }
0137 
0138     if (prod[0]->accept())
0139       ++passed_;
0140     else
0141       ++failed_;
0142 
0143     if (numbits_ < 0)
0144       return;
0145 
0146     unsigned int numbits = numbits_;
0147     if (numbits != prod[0]->size()) {
0148       std::cerr << "TestResultAnalyzer named: " << name_ << " should have " << numbits << ", got " << prod[0]->size()
0149                 << " in TriggerResults\n";
0150       abort();
0151     }
0152   }
0153 
0154   void TestResultAnalyzer::endJob() {
0155     std::cerr << "TESTRESULTANALYZER " << name_ << ": "
0156               << "passed=" << passed_ << " failed=" << failed_ << "\n";
0157   }
0158 
0159   // ---------
0160 
0161   TestContextAnalyzer::TestContextAnalyzer(edm::ParameterSet const& ps)
0162       : expected_pathname_(ps.getUntrackedParameter<std::string>("pathname", "")),
0163         expected_modulelabel_(ps.getUntrackedParameter<std::string>("modlabel", "")) {}
0164 
0165   void TestContextAnalyzer::analyze(edm::StreamID, edm::Event const& e, edm::EventSetup const&) const {
0166     assert(e.moduleCallingContext()->moduleDescription()->moduleLabel() == moduleDescription().moduleLabel());
0167 
0168     if (!expected_pathname_.empty()) {
0169       assert(expected_pathname_ == e.moduleCallingContext()->placeInPathContext()->pathContext()->pathName());
0170     }
0171 
0172     if (!expected_modulelabel_.empty()) {
0173       assert(expected_modulelabel_ == moduleDescription().moduleLabel());
0174     }
0175   }
0176 
0177   // ---------
0178 
0179   TestFilterModule::TestFilterModule(edm::ParameterSet const& ps)
0180       : count_(),
0181         accept_rate_(ps.getUntrackedParameter<int>("acceptValue", 1)),
0182         onlyOne_(ps.getUntrackedParameter<bool>("onlyOne", false)) {}
0183 
0184   TestFilterModule::~TestFilterModule() {}
0185 
0186   bool TestFilterModule::filter(edm::Event& e, edm::EventSetup const&) {
0187     assert(e.moduleCallingContext()->moduleDescription()->moduleLabel() == moduleDescription().moduleLabel());
0188 
0189     ++count_;
0190     if (onlyOne_)
0191       return count_ % accept_rate_ == 0;
0192     else
0193       return count_ % 100 <= accept_rate_;
0194   }
0195 
0196   void TestFilterModule::endJob() {}
0197 
0198   // ---------
0199 
0200   SewerModule::SewerModule(edm::ParameterSet const& ps)
0201       : edm::one::OutputModuleBase::OutputModuleBase(ps),
0202         edm::one::OutputModule<>(ps),
0203         name_(ps.getParameter<std::string>("name")),
0204         num_pass_(ps.getParameter<int>("shouldPass")),
0205         total_() {}
0206 
0207   SewerModule::~SewerModule() {}
0208 
0209   void SewerModule::write(edm::EventForOutput const&) { ++total_; }
0210 
0211   void SewerModule::endJob() {
0212     std::cerr << "SEWERMODULE " << name_ << ": should pass " << num_pass_ << ", did pass " << total_ << "\n";
0213 
0214     if (total_ != num_pass_) {
0215       std::cerr << "number passed should be " << num_pass_ << ", but got " << total_ << "\n";
0216       abort();
0217     }
0218   }
0219 
0220   void SewerModule::fillDescriptions(ConfigurationDescriptions& descriptions) {
0221     ParameterSetDescription desc;
0222     desc.setComment("Tracks number of times the write method is called.");
0223     desc.add<std::string>("name")->setComment("name used in printout");
0224     desc.add<int>("shouldPass")->setComment("number of times write should be called");
0225     edm::one::OutputModule<>::fillDescription(desc, std::vector<std::string>(1U, std::string("drop *")));
0226     descriptions.add("sewerModule", desc);
0227   }
0228 
0229   // ---------
0230 
0231   ExternalWorkSewerModule::ExternalWorkSewerModule(edm::ParameterSet const& ps)
0232       : edm::global::OutputModuleBase::OutputModuleBase(ps),
0233         edm::global::OutputModule<edm::ExternalWork>(ps),
0234         name_(ps.getParameter<std::string>("name")),
0235         num_pass_(ps.getParameter<int>("shouldPass")),
0236         total_(0),
0237         totalAcquire_(0) {}
0238 
0239   void ExternalWorkSewerModule::acquire(edm::StreamID,
0240                                         edm::EventForOutput const&,
0241                                         WaitingTaskWithArenaHolder task) const {
0242     ++totalAcquire_;
0243   }
0244   void ExternalWorkSewerModule::write(edm::EventForOutput const&) { ++total_; }
0245 
0246   void ExternalWorkSewerModule::endJob() {
0247     std::cerr << "EXTERNALWORKSEWERMODULE " << name_ << ": should pass " << num_pass_ << ", did pass " << total_.load()
0248               << " with acquire " << totalAcquire_.load() << "\n";
0249 
0250     if (total_.load() != num_pass_) {
0251       std::cerr << "number passed should be " << num_pass_ << ", but got " << total_.load() << "\n";
0252       abort();
0253     }
0254 
0255     if (total_.load() != totalAcquire_.load()) {
0256       std::cerr << "write() called " << total_.load() << ", but acquire called " << totalAcquire_.load() << "\n";
0257       abort();
0258     }
0259   }
0260 
0261   void ExternalWorkSewerModule::fillDescriptions(ConfigurationDescriptions& descriptions) {
0262     ParameterSetDescription desc;
0263     desc.setComment("Tracks number of times the write and acquire methods are called.");
0264     desc.add<std::string>("name")->setComment("name used in printout");
0265     desc.add<int>("shouldPass")->setComment("number of times write/acquire should be called");
0266     edm::one::OutputModule<>::fillDescription(desc, std::vector<std::string>(1U, std::string("drop *")));
0267     descriptions.add("externalWorkSewerModule", desc);
0268   }
0269 
0270 }  // namespace edmtest
0271 
0272 using edmtest::ExternalWorkSewerModule;
0273 using edmtest::SewerModule;
0274 using edmtest::TestContextAnalyzer;
0275 using edmtest::TestFilterModule;
0276 using edmtest::TestResultAnalyzer;
0277 
0278 DEFINE_FWK_MODULE(TestFilterModule);
0279 DEFINE_FWK_MODULE(TestResultAnalyzer);
0280 DEFINE_FWK_MODULE(SewerModule);
0281 DEFINE_FWK_MODULE(ExternalWorkSewerModule);
0282 DEFINE_FWK_MODULE(TestContextAnalyzer);