Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    DataFormats/HLTReco
0004 // Class:      TestReadTriggerEvent
0005 //
0006 /**\class edmtest::TestReadTriggerEvent
0007   Description: Used as part of tests that ensure the trigger::TriggerEvent
0008   data format can be persistently written and in a subsequent process
0009   read. First, this is done using the current release version for writing
0010   and reading. In addition, the output file of the write process should
0011   be saved permanently each time the trigger::TriggerEvent persistent data
0012   format changes. In unit tests, we read each of those saved files to verify
0013   that the current releases can read older versions of the data format.
0014 */
0015 // Original Author:  W. David Dagenhart
0016 //         Created:  8 May 2023
0017 
0018 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0019 #include "DataFormats/HLTReco/interface/TriggerTypeDefs.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/Frameworkfwd.h"
0022 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0027 #include "FWCore/Utilities/interface/EDGetToken.h"
0028 #include "FWCore/Utilities/interface/Exception.h"
0029 #include "FWCore/Utilities/interface/InputTag.h"
0030 #include "FWCore/Utilities/interface/StreamID.h"
0031 
0032 #include <string>
0033 #include <vector>
0034 
0035 namespace edmtest {
0036 
0037   class TestReadTriggerEvent : public edm::global::EDAnalyzer<> {
0038   public:
0039     TestReadTriggerEvent(edm::ParameterSet const&);
0040     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0041     void throwWithMessage(const char*) const;
0042     static void fillDescriptions(edm::ConfigurationDescriptions&);
0043 
0044   private:
0045     // These expected values are meaningless other than we use them
0046     // to check that values read from persistent storage match the values
0047     // we know were written.
0048     std::string expectedUsedProcessName_;
0049     std::vector<std::string> expectedCollectionTags_;
0050     std::vector<unsigned int> expectedCollectionKeys_;
0051 
0052     std::vector<int> expectedIds_;
0053     std::vector<double> expectedPts_;
0054     std::vector<double> expectedEtas_;
0055     std::vector<double> expectedPhis_;
0056     std::vector<double> expectedMasses_;
0057 
0058     std::vector<std::string> expectedFilterTags_;
0059     unsigned int expectedElementsPerVector_;
0060     std::vector<int> expectedFilterIds_;
0061     std::vector<unsigned int> expectedFilterKeys_;
0062 
0063     edm::EDGetTokenT<trigger::TriggerEvent> triggerEventToken_;
0064   };
0065 
0066   TestReadTriggerEvent::TestReadTriggerEvent(edm::ParameterSet const& iPSet)
0067       : expectedUsedProcessName_(iPSet.getParameter<std::string>("expectedUsedProcessName")),
0068         expectedCollectionTags_(iPSet.getParameter<std::vector<std::string>>("expectedCollectionTags")),
0069         expectedCollectionKeys_(iPSet.getParameter<std::vector<unsigned int>>("expectedCollectionKeys")),
0070         expectedIds_(iPSet.getParameter<std::vector<int>>("expectedIds")),
0071         expectedPts_(iPSet.getParameter<std::vector<double>>("expectedPts")),
0072         expectedEtas_(iPSet.getParameter<std::vector<double>>("expectedEtas")),
0073         expectedPhis_(iPSet.getParameter<std::vector<double>>("expectedPhis")),
0074         expectedMasses_(iPSet.getParameter<std::vector<double>>("expectedMasses")),
0075 
0076         expectedFilterTags_(iPSet.getParameter<std::vector<std::string>>("expectedFilterTags")),
0077         expectedElementsPerVector_(iPSet.getParameter<unsigned int>("expectedElementsPerVector")),
0078         expectedFilterIds_(iPSet.getParameter<std::vector<int>>("expectedFilterIds")),
0079         expectedFilterKeys_(iPSet.getParameter<std::vector<unsigned int>>("expectedFilterKeys")),
0080 
0081         triggerEventToken_(consumes(iPSet.getParameter<edm::InputTag>("triggerEventTag"))) {
0082     if (expectedIds_.size() != expectedPts_.size() || expectedIds_.size() != expectedEtas_.size() ||
0083         expectedIds_.size() != expectedPhis_.size() || expectedIds_.size() != expectedMasses_.size()) {
0084       throw cms::Exception("TestFailure")
0085           << "TestReadTriggerEvent, test configuration error: "
0086              "expectedIds, expectedPts, expectedEtas, expectedPhis, and expectedMasses should have the same size.";
0087     }
0088     if (expectedFilterIds_.size() != expectedElementsPerVector_ * expectedFilterTags_.size() ||
0089         expectedFilterKeys_.size() != expectedElementsPerVector_ * expectedFilterTags_.size()) {
0090       throw cms::Exception("TestFailure") << "TestReadTriggerEvent, test configuration error: "
0091                                              "size of expectedFilterIds and size of expectedFilterKeys "
0092                                              "should equal size of expectedFilterTags times expectedElementsPerVector";
0093     }
0094   }
0095 
0096   void TestReadTriggerEvent::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0097     auto const& triggerEvent = iEvent.get(triggerEventToken_);
0098 
0099     if (triggerEvent.usedProcessName() != expectedUsedProcessName_) {
0100       throwWithMessage("usedProcessName does not have expected value");
0101     }
0102 
0103     if (triggerEvent.collectionTags() != expectedCollectionTags_) {
0104       throwWithMessage("collectionTags do not have expected values");
0105     }
0106 
0107     trigger::Keys expectedKeys;
0108     expectedKeys.reserve(expectedCollectionKeys_.size());
0109     for (auto const& element : expectedCollectionKeys_) {
0110       expectedKeys.push_back(static_cast<trigger::size_type>(element));
0111     }
0112 
0113     if (triggerEvent.collectionKeys() != expectedKeys) {
0114       throwWithMessage("collectionKeys do not have expected values");
0115     }
0116 
0117     trigger::TriggerObjectCollection const& triggerObjectCollection = triggerEvent.getObjects();
0118     if (triggerObjectCollection.size() != expectedIds_.size()) {
0119       throwWithMessage("triggerObjectCollection does not have expected size");
0120     }
0121     for (unsigned int i = 0; i < triggerObjectCollection.size(); ++i) {
0122       trigger::TriggerObject const& triggerObject = triggerObjectCollection[i];
0123       if (triggerObject.id() != expectedIds_[i]) {
0124         throwWithMessage("triggerObjectCollection id does not have expected value");
0125       }
0126       if (triggerObject.pt() != static_cast<float>(expectedPts_[i])) {
0127         throwWithMessage("triggerObjectCollection pt does not have expected value");
0128       }
0129       if (triggerObject.eta() != static_cast<float>(expectedEtas_[i])) {
0130         throwWithMessage("triggerObjectCollection eta does not have expected value");
0131       }
0132       if (triggerObject.phi() != static_cast<float>(expectedPhis_[i])) {
0133         throwWithMessage("triggerObjectCollection phi does not have expected value");
0134       }
0135       if (triggerObject.mass() != static_cast<float>(expectedMasses_[i])) {
0136         throwWithMessage("triggerObjectCollection mass does not have expected value");
0137       }
0138     }
0139 
0140     if (triggerEvent.sizeFilters() != expectedFilterTags_.size()) {
0141       throwWithMessage("triggerFilters does not have expected size");
0142     }
0143 
0144     for (unsigned int i = 0; i < expectedFilterTags_.size(); ++i) {
0145       if (triggerEvent.filterLabel(i) != expectedFilterTags_[i]) {
0146         throwWithMessage("filterTags does not have expected value");
0147       }
0148       trigger::Vids const& filterIds = triggerEvent.filterIds(i);
0149       if (filterIds.size() != expectedElementsPerVector_) {
0150         throwWithMessage("filterIds does not have expected size");
0151       }
0152       trigger::Keys const& filterKeys = triggerEvent.filterKeys(i);
0153       if (filterKeys.size() != expectedElementsPerVector_) {
0154         throwWithMessage("filterKeys does not have expected size");
0155       }
0156       for (unsigned int j = 0; j < expectedElementsPerVector_; ++j) {
0157         if (filterIds[j] != expectedFilterIds_[i * expectedElementsPerVector_ + j]) {
0158           throwWithMessage("filterIds does not contain expected values");
0159         }
0160         if (filterKeys[j] != expectedFilterKeys_[i * expectedElementsPerVector_ + j]) {
0161           throwWithMessage("filterKeys does not contain expected values");
0162         }
0163       }
0164     }
0165   }
0166 
0167   void TestReadTriggerEvent::throwWithMessage(const char* msg) const {
0168     throw cms::Exception("TestFailure") << "TestReadTriggerEvent::analyze, " << msg;
0169   }
0170 
0171   void TestReadTriggerEvent::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0172     edm::ParameterSetDescription desc;
0173     desc.add<std::string>("expectedUsedProcessName");
0174     desc.add<std::vector<std::string>>("expectedCollectionTags");
0175     desc.add<std::vector<unsigned int>>("expectedCollectionKeys");
0176     desc.add<std::vector<int>>("expectedIds");
0177     desc.add<std::vector<double>>("expectedPts");
0178     desc.add<std::vector<double>>("expectedEtas");
0179     desc.add<std::vector<double>>("expectedPhis");
0180     desc.add<std::vector<double>>("expectedMasses");
0181     desc.add<std::vector<std::string>>("expectedFilterTags");
0182     desc.add<unsigned int>("expectedElementsPerVector");
0183     desc.add<std::vector<int>>("expectedFilterIds");
0184     desc.add<std::vector<unsigned int>>("expectedFilterKeys");
0185     desc.add<edm::InputTag>("triggerEventTag");
0186     descriptions.addDefault(desc);
0187   }
0188 }  // namespace edmtest
0189 
0190 using edmtest::TestReadTriggerEvent;
0191 DEFINE_FWK_MODULE(TestReadTriggerEvent);