Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-26 02:25:00

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