Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:12

0001 
0002 #include "DataFormats/Common/interface/Handle.h"
0003 #include "DataFormats/Common/interface/TriggerResults.h"
0004 #include "DataFormats/Provenance/interface/ParameterSetID.h"
0005 #include "FWCore/Common/interface/Provenance.h"
0006 #include "FWCore/Common/interface/TriggerNames.h"
0007 #include "FWCore/Common/interface/TriggerResultsByName.h"
0008 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/TriggerNamesService.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/Registry.h"
0014 #include "FWCore/ServiceRegistry/interface/Service.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "FWCore/Utilities/interface/ThreadSafeRegistry.h"
0017 #include "FWCore/Utilities/interface/Exception.h"
0018 
0019 #include <cstdlib>
0020 #include <iostream>
0021 #include <string>
0022 #include <vector>
0023 
0024 namespace edm {
0025   class EventSetup;
0026 }
0027 
0028 using namespace edm;
0029 
0030 namespace edmtest {
0031 
0032   class TestTriggerNames : public edm::one::EDAnalyzer<> {
0033   public:
0034     typedef std::vector<std::string> Strings;
0035 
0036     explicit TestTriggerNames(edm::ParameterSet const&);
0037     virtual ~TestTriggerNames();
0038 
0039     virtual void analyze(edm::Event const& e, edm::EventSetup const& c);
0040     void endJob();
0041 
0042   private:
0043     unsigned int iEvent_;
0044     Strings expected_trigger_paths_;
0045     Strings expected_trigger_previous_;
0046     Strings expected_end_paths_;
0047     bool streamerSource_;
0048     bool dumpPSetRegistry_;
0049     std::vector<unsigned int> expectedTriggerResultsHLT_;
0050     std::vector<unsigned int> expectedTriggerResultsPROD_;
0051   };
0052 
0053   // -----------------------------------------------------------------
0054 
0055   TestTriggerNames::TestTriggerNames(edm::ParameterSet const& ps)
0056       : iEvent_(0U),
0057         expected_trigger_paths_(ps.getUntrackedParameter<Strings>("trigPaths", Strings())),
0058         expected_trigger_previous_(ps.getUntrackedParameter<Strings>("trigPathsPrevious", Strings())),
0059         expected_end_paths_(ps.getUntrackedParameter<Strings>("endPaths", Strings())),
0060         streamerSource_(ps.getUntrackedParameter<bool>("streamerSource", false)),
0061         dumpPSetRegistry_(ps.getUntrackedParameter<bool>("dumpPSetRegistry", false)),
0062         expectedTriggerResultsHLT_(ps.getUntrackedParameter<std::vector<unsigned int> >("expectedTriggerResultsHLT",
0063                                                                                         std::vector<unsigned int>())),
0064         expectedTriggerResultsPROD_(ps.getUntrackedParameter<std::vector<unsigned int> >("expectedTriggerResultsPROD",
0065                                                                                          std::vector<unsigned int>())) {
0066     if (not expected_trigger_previous_.empty()) {
0067       consumesMany<edm::TriggerResults>();
0068     }
0069     if (not expectedTriggerResultsHLT_.empty()) {
0070       consumes<edm::TriggerResults>(edm::InputTag("TriggerResults", "", "HLT"));
0071     }
0072     if (not expectedTriggerResultsPROD_.empty()) {
0073       consumes<edm::TriggerResults>(edm::InputTag("TriggerResults", "", "PROD"));
0074     }
0075   }
0076 
0077   // -----------------------------------------------------------------
0078 
0079   TestTriggerNames::~TestTriggerNames() {}
0080 
0081   // -----------------------------------------------------------------
0082 
0083   void TestTriggerNames::analyze(edm::Event const& e, edm::EventSetup const&) {
0084     if (dumpPSetRegistry_) {
0085       pset::Registry* psetRegistry = pset::Registry::instance();
0086       psetRegistry->print(std::cout);
0087     }
0088 
0089     // Runs some tests on the TriggerNamesService
0090     if (expected_trigger_paths_.size() > 0) {
0091       Strings triggernames;
0092       edm::Service<edm::service::TriggerNamesService> tns;
0093       triggernames = tns->getTrigPaths();
0094       if (triggernames.size() != expected_trigger_paths_.size()) {
0095         throw cms::Exception("Test Failure") << "TestTriggerNames: "
0096                                              << "Expected and actual trigger path list not the same size" << std::endl;
0097       }
0098       for (Strings::size_type i = 0; i < expected_trigger_paths_.size(); ++i) {
0099         if (triggernames[i] != expected_trigger_paths_[i]) {
0100           throw cms::Exception("Test Failure") << "TestTriggerNames: "
0101                                                << "Expected and actual trigger paths don't match" << std::endl;
0102         }
0103       }
0104     }
0105 
0106     if (expected_end_paths_.size() > 0) {
0107       Strings endnames;
0108       edm::Service<edm::service::TriggerNamesService> tns;
0109       endnames = tns->getEndPaths();
0110       if (endnames.size() != expected_end_paths_.size()) {
0111         throw cms::Exception("Test Failure") << "TestTriggerNames: "
0112                                              << "Expected and actual end path list not the same size" << std::endl;
0113       }
0114       for (Strings::size_type i = 0; i < expected_end_paths_.size(); ++i) {
0115         if (endnames[i] != expected_end_paths_[i]) {
0116           throw cms::Exception("Test Failure") << "TestTriggerNames: "
0117                                                << "Expected and actual end paths don't match" << std::endl;
0118         }
0119       }
0120     }
0121 
0122     if (expected_trigger_previous_.size() > 0) {
0123       typedef std::vector<edm::Handle<edm::TriggerResults> > Trig;
0124       Trig prod;
0125       e.getManyByType(prod);
0126 
0127       if (prod.size() == 0) {
0128         throw cms::Exception("Test Failure")
0129             << "TestTriggerNames: "
0130             << "No TriggerResults object found, expected previous trigger results" << std::endl;
0131       }
0132 
0133       Strings triggernames;
0134       edm::Service<edm::service::TriggerNamesService> tns;
0135       auto index = 0U;
0136       while ((index < prod.size()) and (moduleDescription().processName() == prod[index].provenance()->processName())) {
0137         ++index;
0138       }
0139       if (tns->getTrigPaths(*prod[index], triggernames)) {
0140         if (triggernames.size() != expected_trigger_previous_.size()) {
0141           std::string et;
0142           for (auto const& n : expected_trigger_previous_) {
0143             et += n + " ";
0144           }
0145           std::string tn;
0146           for (auto const& n : triggernames) {
0147             tn += n + " ";
0148           }
0149           throw cms::Exception("Test Failure") << "TestTriggerNames: "
0150                                                << "Expected and actual previous trigger path lists not the same size"
0151                                                << "\n expected: " << et << "\n actual: " << tn << std::endl;
0152         }
0153         for (Strings::size_type i = 0; i < expected_trigger_previous_.size(); ++i) {
0154           if (triggernames[i] != expected_trigger_previous_[i]) {
0155             std::string et;
0156             for (auto const& n : expected_trigger_previous_) {
0157               et += n + " ";
0158             }
0159             std::string tn;
0160             for (auto const& n : triggernames) {
0161               tn += n + " ";
0162             }
0163 
0164             throw cms::Exception("Test Failure") << "TestTriggerNames: "
0165                                                  << "Expected and actual previous trigger paths don't match"
0166                                                  << "\n expected: " << et << "\n actual: " << tn << std::endl;
0167           }
0168         }
0169       } else {
0170         throw cms::Exception("Test Failure") << "TestTriggerNames: "
0171                                              << "Failed finding trigger names from a previous process" << std::endl;
0172       }
0173       bool fromPSetRegistry;
0174       if (tns->getTrigPaths(*prod[index], triggernames, fromPSetRegistry)) {
0175         if (!fromPSetRegistry) {
0176           throw cms::Exception("Test Failure") << "TestTriggerNames: "
0177                                                << "fromPSetRegistry returned with incorrect value" << std::endl;
0178         }
0179       }
0180 
0181       // The provenance of the TriggerResults object should also determine the
0182       // ID for the parameter set that lists the trigger paths.
0183       // Test this by getting this parameter set and verifying the trigger
0184       // paths are the correct size.
0185       if (!streamerSource_) {
0186         ParameterSet const& trigpset = parameterSet(prod[index].provenance()->stable(), e.processHistory());
0187         Strings trigpaths = trigpset.getParameter<Strings>("@trigger_paths");
0188         if (trigpaths.size() != expected_trigger_previous_.size()) {
0189           throw cms::Exception("Test Failure")
0190               << "TestTriggerNames: Using provenance\n"
0191               << "Expected and actual previous trigger path not the same size" << std::endl;
0192         }
0193       }
0194 
0195       // Look again using the TriggerNames class instead
0196       // of going to the service.
0197 
0198       TriggerNames const& triggerNamesFromEvent = e.triggerNames(*prod[index]);
0199 
0200       Strings namesFromEvent = triggerNamesFromEvent.triggerNames();
0201       if (namesFromEvent.size() != expected_trigger_previous_.size()) {
0202         throw cms::Exception("Test Failure")
0203             << "TestTriggerNames: While exercising TriggerNames class\n"
0204             << "Expected and actual previous trigger path lists not the same size" << std::endl;
0205       }
0206       for (Strings::size_type i = 0; i < expected_trigger_previous_.size(); ++i) {
0207         if (namesFromEvent[i] != expected_trigger_previous_[i]) {
0208           throw cms::Exception("Test Failure") << "TestTriggerNames: While exercising TriggerNames class\n"
0209                                                << "Expected and actual previous trigger paths don't match" << std::endl;
0210         }
0211         if (triggerNamesFromEvent.triggerName(i) != expected_trigger_previous_[i]) {
0212           throw cms::Exception("Test Failure") << "TestTriggerNames: While exercising TriggerNames class\n"
0213                                                << "name from index accessor\n"
0214                                                << "Expected and actual previous trigger paths don't match" << std::endl;
0215         }
0216         // Exercise the function that returns an index
0217         if (i != triggerNamesFromEvent.triggerIndex(expected_trigger_previous_[i])) {
0218           throw cms::Exception("Test Failure") << "TestTriggerNames: While exercising TriggerNames class\n"
0219                                                << "index from name accessor\n"
0220                                                << "Expected and actual previous trigger paths don't match" << std::endl;
0221         }
0222         if (triggerNamesFromEvent.size() != expected_trigger_previous_.size()) {
0223           throw cms::Exception("Test Failure") << "TestTriggerNames: While exercising TriggerNames class\n"
0224                                                << "Checking size accessor\n"
0225                                                << "Expected and actual previous trigger paths don't match" << std::endl;
0226         }
0227       }
0228       // This causes it to find the results in the map lookup in the TEST configuration
0229       // and exercises that execution path in the code.
0230       // If you follow the execution in the debugger in EventBase::triggerNames_ in EventBase.cc
0231       // you can verify this is working correctly.
0232       if (prod.size() > 1U) {
0233         e.triggerNames(*prod[1]);
0234       }
0235     }
0236 
0237     edm::InputTag tag("TriggerResults", "", "HLT");
0238     edm::Handle<edm::TriggerResults> hTriggerResults;
0239 
0240     if (expectedTriggerResultsHLT_.size() > 0) {
0241       if (e.getByLabel(tag, hTriggerResults)) {
0242         edm::TriggerResultsByName resultsByNameHLT = e.triggerResultsByName(*hTriggerResults);
0243 
0244         if (hTriggerResults->parameterSetID() != resultsByNameHLT.parameterSetID() ||
0245             hTriggerResults->wasrun() != resultsByNameHLT.wasrun() ||
0246             hTriggerResults->accept() != resultsByNameHLT.accept() ||
0247             hTriggerResults->error() != resultsByNameHLT.error() ||
0248             hTriggerResults->at(0).state() != resultsByNameHLT.at("p01").state() ||
0249             hTriggerResults->at(0).index() != resultsByNameHLT.at("p01").index() ||
0250             (*hTriggerResults)[0].state() != resultsByNameHLT["p01"].state() ||
0251             (*hTriggerResults)[0].index() != resultsByNameHLT["p01"].index() ||
0252             hTriggerResults->wasrun(0) != resultsByNameHLT.wasrun("p01") ||
0253             hTriggerResults->accept(0) != resultsByNameHLT.accept("p01") ||
0254             hTriggerResults->error(0) != resultsByNameHLT.error("p01") ||
0255             hTriggerResults->state(0) != resultsByNameHLT.state("p01") ||
0256             hTriggerResults->index(0) != resultsByNameHLT.index("p01") ||
0257             hTriggerResults->at(0).state() != resultsByNameHLT.at(0).state() ||
0258             hTriggerResults->at(0).index() != resultsByNameHLT.at(0).index() ||
0259             (*hTriggerResults)[0].state() != resultsByNameHLT[0].state() ||
0260             (*hTriggerResults)[0].index() != resultsByNameHLT[0].index() ||
0261             hTriggerResults->wasrun(0) != resultsByNameHLT.wasrun(0) ||
0262             hTriggerResults->accept(0) != resultsByNameHLT.accept(0) ||
0263             hTriggerResults->error(0) != resultsByNameHLT.error(0) ||
0264             hTriggerResults->state(0) != resultsByNameHLT.state(0) ||
0265             hTriggerResults->index(0) != resultsByNameHLT.index(0)) {
0266           throw cms::Exception("Test Failure")
0267               << "TestTriggerNames: While testing TriggerResultsByName class\n"
0268               << "TriggerResults values do not match TriggerResultsByName values" << std::endl;
0269         }
0270         edm::TriggerNames const& names = e.triggerNames(*hTriggerResults);
0271         if (names.triggerNames() != resultsByNameHLT.triggerNames() || names.size() != resultsByNameHLT.size() ||
0272             names.triggerName(0) != resultsByNameHLT.triggerName(0) ||
0273             names.triggerIndex("p01") != resultsByNameHLT.triggerIndex("p01")) {
0274           throw cms::Exception("Test Failure")
0275               << "TestTriggerNames: While testing TriggerResultsByName class\n"
0276               << "TriggerNames values do not match TriggerResultsByName values" << std::endl;
0277         }
0278       }
0279     }
0280 
0281     if (expectedTriggerResultsHLT_.size() > iEvent_) {
0282       if (!hTriggerResults.isValid()) {
0283         throw cms::Exception("Test Failure") << "TestTriggerNames: While testing TriggerResultsByName class\n"
0284                                              << "Invalid TriggerResults Handle for HLT" << std::endl;
0285       }
0286 
0287       edm::TriggerResultsByName resultsByNameHLT = e.triggerResultsByName(*hTriggerResults);
0288 
0289       if (!resultsByNameHLT.isValid()) {
0290         throw cms::Exception("Test Failure") << "TestTriggerNames: While testing TriggerResultsByName class\n"
0291                                              << "Invalid object for HLT" << std::endl;
0292       }
0293       if (resultsByNameHLT.accept("p02") != (expectedTriggerResultsHLT_[iEvent_] == 1)) {
0294         throw cms::Exception("Test Failure") << "TestTriggerNames: While testing TriggerResultsByName class\n"
0295                                              << "Expected and actual HLT trigger results don't match" << std::endl;
0296       }
0297       edm::LogAbsolute("TEST") << "Event " << iEvent_ << "  " << resultsByNameHLT.accept("p02") << std::endl;
0298     }
0299 
0300     edm::InputTag tagPROD("TriggerResults", "", "PROD");
0301     edm::Handle<edm::TriggerResults> hTriggerResultsPROD;
0302 
0303     if (expectedTriggerResultsPROD_.size() > iEvent_) {
0304       e.getByLabel(tagPROD, hTriggerResultsPROD);
0305 
0306       if (!hTriggerResultsPROD.isValid()) {
0307         throw cms::Exception("Test Failure") << "TestTriggerNames: While testing TriggerResultsByName class\n"
0308                                              << "Invalid TriggerResults Handle for PROD" << std::endl;
0309       }
0310 
0311       edm::TriggerResultsByName resultsByNamePROD = e.triggerResultsByName(*hTriggerResultsPROD);
0312       if (!resultsByNamePROD.isValid()) {
0313         throw cms::Exception("Test Failure") << "TestTriggerNames: While testing TriggerResultsByName class\n"
0314                                              << "Invalid object for PROD" << std::endl;
0315       }
0316       if (resultsByNamePROD.accept("p1") != (expectedTriggerResultsPROD_[iEvent_] == 1)) {
0317         throw cms::Exception("Test Failure") << "TestTriggerNames: While testing TriggerResultsByName class\n"
0318                                              << "Expected and actual PROD trigger results don't match" << std::endl;
0319       }
0320       edm::LogAbsolute("TEST") << "Event " << iEvent_ << "  " << resultsByNamePROD.accept("p1") << std::endl;
0321     }
0322     ++iEvent_;
0323   }
0324 
0325   void TestTriggerNames::endJob() {}
0326 }  // namespace edmtest
0327 
0328 using edmtest::TestTriggerNames;
0329 
0330 DEFINE_FWK_MODULE(TestTriggerNames);