File indexing completed on 2025-06-28 06:19:01
0001 #ifndef DataFormats_FEDRawData_RawDataBuffer_h
0002 #define DataFormats_FEDRawData_RawDataBuffer_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
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
0062
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_;
0084 std::vector<unsigned char> data_;
0085 bool phase1Range_ = false;
0086 };
0087
0088 #endif