Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-28 06:19:01

0001 // -*- C++ -*-
0002 //
0003 // Package:    DataFormats/FEDRawData
0004 // Class:      TestReadRawDataBuffer
0005 //
0006 /**\class edmtest::TestReadRawDataBuffer
0007   Description: Used as part of tests that ensure the RawDataBuffer
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 RawDataBuffer 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/RawDataBuffer.h"
0019 #include "DataFormats/FEDRawData/interface/SLinkRocketHeaders.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 TestReadRawDataBuffer : public edm::global::EDAnalyzer<> {
0037   public:
0038     TestReadRawDataBuffer(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> dataPattern1_;
0050     std::vector<unsigned int> dataPattern2_;
0051     edm::EDGetTokenT<RawDataBuffer> rawDataBufferToken_;
0052   };
0053 
0054   TestReadRawDataBuffer::TestReadRawDataBuffer(edm::ParameterSet const& iPSet)
0055       : dataPattern1_(iPSet.getParameter<std::vector<unsigned int>>("dataPattern1")),
0056         dataPattern2_(iPSet.getParameter<std::vector<unsigned int>>("dataPattern2")),
0057         rawDataBufferToken_(consumes(iPSet.getParameter<edm::InputTag>("rawDataBufferTag"))) {}
0058 
0059   void TestReadRawDataBuffer::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0060     auto const& rawDataBuffer = iEvent.get(rawDataBufferToken_);
0061 
0062     auto const& fragData0 = rawDataBuffer.fragmentData(0);
0063     auto const& fragData1 = rawDataBuffer.fragmentData(1);
0064     auto const& fragData2 = rawDataBuffer.fragmentData(30);
0065     auto const& fragDataHigh = rawDataBuffer.fragmentData(298457834);
0066 
0067     assert(fragData0.size());
0068     assert(fragData1.size());
0069     assert(fragData2.size());
0070     assert(fragDataHigh.size());
0071 
0072     auto hdrsize = sizeof(SLinkRocketHeader_v3);
0073     auto trsize = sizeof(SLinkRocketTrailer_v3);
0074 
0075     auto hdrView0 = makeSLinkRocketHeaderView(fragData0.dataHeader(hdrsize));
0076     auto trlView0 = makeSLinkRocketTrailerView(fragData0.dataTrailer(trsize), hdrView0->version());
0077 
0078     auto hdrView1 = makeSLinkRocketHeaderView(fragData1.dataHeader(hdrsize));
0079     auto trlView1 = makeSLinkRocketTrailerView(fragData1.dataTrailer(trsize), hdrView1->version());
0080 
0081     auto hdrView2 = makeSLinkRocketHeaderView(fragData2.dataHeader(hdrsize));
0082     auto trlView2 = makeSLinkRocketTrailerView(fragData2.dataTrailer(trsize), hdrView2->version());
0083 
0084     auto hdrViewHigh = makeSLinkRocketHeaderView(fragDataHigh.dataHeader(hdrsize));
0085     auto trlViewHigh = makeSLinkRocketTrailerView(fragDataHigh.dataTrailer(trsize), hdrViewHigh->version());
0086 
0087     auto src0data = fragData0.payload(hdrsize, trsize);
0088     for (size_t i = 0; i < src0data.size(); i++) {
0089       if (src0data[i] != dataPattern1_[i % std::size(dataPattern1_)])
0090         throwWithMessage("data id 0 does not have expected contents");
0091     }
0092     auto src1data = fragData1.payload(hdrsize, trsize);
0093     for (size_t i = 0; i < src1data.size(); i++) {
0094       if (src1data[i] != dataPattern2_[i % std::size(dataPattern2_)])
0095         throwWithMessage("data id 3 does not have expected contents");
0096     }
0097     auto src2data = fragData2.payload(hdrsize, trsize);
0098     for (size_t i = 0; i < src2data.size(); i++) {
0099       if (src2data[i] != dataPattern1_[i % std::size(dataPattern1_)])
0100         throwWithMessage("data id 30 does not have expected contents");
0101     }
0102     auto srcHighdata = fragDataHigh.payload(hdrsize, trsize);
0103     for (size_t i = 0; i < srcHighdata.size(); i++) {
0104       if (srcHighdata[i] != (dataPattern2_[i % std::size(dataPattern2_)]))
0105         throwWithMessage("data id (high) does not have expected contents");
0106     }
0107 
0108     //todo other two
0109   }
0110 
0111   void TestReadRawDataBuffer::throwWithMessage(const char* msg) const {
0112     throw cms::Exception("TestFailure") << "TestReadRawDataBuffer::analyze, " << msg;
0113   }
0114 
0115   void TestReadRawDataBuffer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0116     edm::ParameterSetDescription desc;
0117     desc.add<std::vector<unsigned int>>("dataPattern1");
0118     desc.add<std::vector<unsigned int>>("dataPattern2");
0119     desc.add<edm::InputTag>("rawDataBufferTag");
0120     descriptions.addDefault(desc);
0121   }
0122 }  // namespace edmtest
0123 
0124 using edmtest::TestReadRawDataBuffer;
0125 DEFINE_FWK_MODULE(TestReadRawDataBuffer);