Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    DataFormats/DetId
0004 // Class:      TestReadVectorDetId
0005 //
0006 /**\class edmtest::TestReadVectorDetId
0007   Description: Used as part of tests that ensure the std::vector<DetId>
0008   raw 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 VectorDetId 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:  25 September 2023
0017 
0018 #include "DataFormats/DetId/interface/DetId.h"
0019 #include "FWCore/Framework/interface/Event.h"
0020 #include "FWCore/Framework/interface/Frameworkfwd.h"
0021 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0022 #include "FWCore/Framework/interface/MakerMacros.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0026 #include "FWCore/Utilities/interface/EDGetToken.h"
0027 #include "FWCore/Utilities/interface/Exception.h"
0028 #include "FWCore/Utilities/interface/InputTag.h"
0029 #include "FWCore/Utilities/interface/StreamID.h"
0030 
0031 #include <vector>
0032 
0033 namespace edmtest {
0034 
0035   class TestReadVectorDetId : public edm::global::EDAnalyzer<> {
0036   public:
0037     TestReadVectorDetId(edm::ParameterSet const&);
0038     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0039     void throwWithMessage(const char*) const;
0040     static void fillDescriptions(edm::ConfigurationDescriptions&);
0041 
0042   private:
0043     // This expected value is meaningless other than we use it
0044     // to check that values read from persistent storage match the values
0045     // we know were written.
0046     unsigned int expectedTestValue_;
0047 
0048     edm::EDGetTokenT<std::vector<DetId>> collectionToken_;
0049   };
0050 
0051   TestReadVectorDetId::TestReadVectorDetId(edm::ParameterSet const& iPSet)
0052       : expectedTestValue_(iPSet.getParameter<unsigned int>("expectedTestValue")),
0053         collectionToken_(consumes(iPSet.getParameter<edm::InputTag>("collectionTag"))) {}
0054 
0055   void TestReadVectorDetId::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0056     auto const& vectorDetIds = iEvent.get(collectionToken_);
0057 
0058     unsigned int expectedNumberOfDetIds = (iEvent.id().event() - 1) % 10;
0059     unsigned int expectedDetId = expectedTestValue_ + iEvent.id().event();
0060     unsigned int numberOfDetIds = 0;
0061     for (const auto& detId : vectorDetIds) {
0062       ++numberOfDetIds;
0063       expectedDetId += iEvent.id().event();
0064       if (detId.rawId() != expectedDetId) {
0065         throwWithMessage("DetId in vector of DetIds does not have expected value");
0066       }
0067     }
0068     if (numberOfDetIds != expectedNumberOfDetIds) {
0069       throwWithMessage("Number of DetIds does not match expected value");
0070     }
0071   }
0072 
0073   void TestReadVectorDetId::throwWithMessage(const char* msg) const {
0074     throw cms::Exception("TestFailure") << "TestReadVectorDetId::analyze, " << msg;
0075   }
0076 
0077   void TestReadVectorDetId::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0078     edm::ParameterSetDescription desc;
0079     desc.add<unsigned int>("expectedTestValue");
0080     desc.add<edm::InputTag>("collectionTag");
0081     descriptions.addDefault(desc);
0082   }
0083 }  // namespace edmtest
0084 
0085 using edmtest::TestReadVectorDetId;
0086 DEFINE_FWK_MODULE(TestReadVectorDetId);