Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DataFormats_FEDRawData_RawDataBuffer_h
0002 #define DataFormats_FEDRawData_RawDataBuffer_h
0003 
0004 /** \class RawDataBuffer
0005  *  A product storing raw data for all SourceIDs in a Event, intended
0006  *  for persistent storage in ROOT Streamer or root files.
0007  *  For Phase-2 source ID is 32-bit, so map is used instead of linear vector
0008  *  as in the FEDRawDataCollection.  Legacy mode is added to use it for
0009  *  Phase-1 data.
0010  *  IMPORTANT: this is not a final format version for Phase-2 and only intended
0011  *  for detector development. It will not be frozen until the end of LS3.
0012  *  \author S. Morovic
0013  */
0014 
0015 #include "DataFormats/Common/interface/traits.h"
0016 #include "FWCore/Utilities/interface/Exception.h"
0017 
0018 #include <vector>
0019 #include <map>
0020 #include <span>
0021 
0022 /*
0023  * Phase2 source or FED wrapper pointing to a span of RawDataBuffer data
0024  * */
0025 
0026 class RawFragmentWrapper {
0027 private:
0028   uint32_t sourceId_;
0029   bool valid_;
0030   uint32_t size_;
0031   std::span<const unsigned char> span_;
0032 
0033 public:
0034   RawFragmentWrapper() : sourceId_(0), valid_(false) {}
0035   RawFragmentWrapper(uint32_t sourceId, std::span<const unsigned char>&& span)
0036       : sourceId_(sourceId), valid_(true), span_(span) {}
0037   std::span<const unsigned char> const& data() const { return span_; }
0038   std::span<const unsigned char> dataHeader(uint32_t expSize) const {
0039     if (expSize > span_.size())
0040       throw cms::Exception("RawFragmentWrapper") << "Expected trailer too large: " << expSize << " > " << span_.size();
0041     return span_.subspan(0, expSize);
0042   }
0043   std::span<const unsigned char> dataTrailer(uint32_t expSize) const {
0044     if (expSize > span_.size())
0045       throw cms::Exception("RawFragmentWrapper") << "Expected trailer too large: " << expSize << " > " << span_.size();
0046     return span_.subspan(span_.size() - expSize, expSize);
0047   }
0048   std::span<const unsigned char> payload(uint32_t expSizeHeader, uint32_t expSizeTrailer) const {
0049     if (expSizeHeader + expSizeTrailer > span_.size())
0050       throw cms::Exception("RawFragmentWrapper")
0051           << "Trailer and header too large: " << expSizeHeader << " + " << expSizeTrailer << " > " << span_.size();
0052     return span_.subspan(expSizeHeader, span_.size() - expSizeTrailer - expSizeHeader);
0053   }
0054 
0055   uint32_t size() const { return span_.size(); }
0056   uint32_t sourceId() const { return sourceId_; }
0057   bool isValid() const { return valid_; }
0058 };
0059 
0060 /*
0061  * Contains metadata vector with source ID or FED ID offsets and size
0062  * and data buffer as vector of bytes
0063  */
0064 
0065 class RawDataBuffer : public edm::DoNotRecordParents {
0066 public:
0067   RawDataBuffer();
0068   RawDataBuffer(uint32_t preallocSize);
0069 
0070   unsigned char* addSource(uint32_t sourceId, unsigned char const* buf, uint32_t size);
0071   void setPhase1Range() { phase1Range_ = true; }
0072 
0073   const RawFragmentWrapper fragmentData(uint32_t sourceId) const;
0074   const RawFragmentWrapper fragmentData(
0075       std::map<uint32_t, std::pair<uint32_t, uint32_t>>::const_iterator const& it) const;
0076 
0077   unsigned char getByte(unsigned int pos) const { return data_.at(pos); }
0078   std::vector<unsigned char> data() const { return data_; }
0079   std::map<uint32_t, std::pair<uint32_t, uint32_t>> const& map() const { return map_; }
0080 
0081 private:
0082   uint32_t usedSize_ = 0;
0083   std::map<uint32_t, std::pair<uint32_t, uint32_t>> map_;  //map of source id fragment offset and size pairs
0084   std::vector<unsigned char> data_;                        //raw data byte vector
0085   bool phase1Range_ = false;
0086 };
0087 
0088 #endif