Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    DataFormats/FEDRawData
0004 // Class:      TestReadFEDRawDataCollection
0005 //
0006 /**\class edmtest::TestReadFEDRawDataCollection
0007   Description: Used as part of tests that ensure the FEDRawDataCollection
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 FEDRawDataCollection 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:  1 May 2023
0017 
0018 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0019 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.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 <vector>
0033 
0034 namespace edmtest {
0035 
0036   class TestReadFEDRawDataCollection : public edm::global::EDAnalyzer<> {
0037   public:
0038     TestReadFEDRawDataCollection(edm::ParameterSet const&);
0039     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0040     void throwWithMessage(const char*) const;
0041     static void fillDescriptions(edm::ConfigurationDescriptions&);
0042 
0043   private:
0044     // Two FEDRawData elements should be enough to verify we can read
0045     // and write the whole collection. I arbitrarily chose elements
0046     // 0 and 3 of the Collection. Values are meaningless, we just
0047     // verify what we read matches what we wrote. For purposes of
0048     // this test that is enough.
0049     std::vector<unsigned int> expectedFEDData0_;
0050     std::vector<unsigned int> expectedFEDData3_;
0051     edm::EDGetTokenT<FEDRawDataCollection> fedRawDataCollectionToken_;
0052   };
0053 
0054   TestReadFEDRawDataCollection::TestReadFEDRawDataCollection(edm::ParameterSet const& iPSet)
0055       : expectedFEDData0_(iPSet.getParameter<std::vector<unsigned int>>("expectedFEDData0")),
0056         expectedFEDData3_(iPSet.getParameter<std::vector<unsigned int>>("expectedFEDData3")),
0057         fedRawDataCollectionToken_(consumes(iPSet.getParameter<edm::InputTag>("fedRawDataCollectionTag"))) {}
0058 
0059   void TestReadFEDRawDataCollection::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0060     auto const& fedRawDataCollection = iEvent.get(fedRawDataCollectionToken_);
0061     auto const& fedData0 = fedRawDataCollection.FEDData(0);
0062     if (fedData0.size() != expectedFEDData0_.size()) {
0063       throwWithMessage("fedData0 does not have expected size");
0064     }
0065     for (unsigned int i = 0; i < fedData0.size(); ++i) {
0066       if (fedData0.data()[i] != expectedFEDData0_[i]) {
0067         throwWithMessage("fedData0 does not have expected contents");
0068       }
0069     }
0070     auto const& fedData3 = fedRawDataCollection.FEDData(3);
0071     if (fedData3.size() != expectedFEDData3_.size()) {
0072       throwWithMessage("fedData3 does not have expected size");
0073     }
0074     for (unsigned int i = 0; i < fedData3.size(); ++i) {
0075       if (fedData3.data()[i] != expectedFEDData3_[i]) {
0076         throwWithMessage("fedData3 does not have expected contents");
0077       }
0078     }
0079   }
0080 
0081   void TestReadFEDRawDataCollection::throwWithMessage(const char* msg) const {
0082     throw cms::Exception("TestFailure") << "TestReadFEDRawDataCollection::analyze, " << msg;
0083   }
0084 
0085   void TestReadFEDRawDataCollection::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0086     edm::ParameterSetDescription desc;
0087     desc.add<std::vector<unsigned int>>("expectedFEDData0");
0088     desc.add<std::vector<unsigned int>>("expectedFEDData3");
0089     desc.add<edm::InputTag>("fedRawDataCollectionTag");
0090     descriptions.addDefault(desc);
0091   }
0092 }  // namespace edmtest
0093 
0094 using edmtest::TestReadFEDRawDataCollection;
0095 DEFINE_FWK_MODULE(TestReadFEDRawDataCollection);