CMS 3D CMS Logo

AMCSpec.cc
Go to the documentation of this file.
1 #include <iomanip>
2 
5 
7 
8 #define EDM_ML_DEBUG 1
9 
10 namespace amc {
11  BlockHeader::BlockHeader(unsigned int amc_no, unsigned int board_id, unsigned int size, unsigned int block) {
12  // Determine size
13  unsigned int max_block_no = 0;
14  if (size >= 0x13ff)
15  max_block_no = (size - 1023) / 4096;
16 
17  if (block != max_block_no)
19  else if (block != 0)
20  size -= split_block_size * max_block_no;
21 
22  data_ = (static_cast<uint64_t>(size & Size_mask) << Size_shift) |
23  (static_cast<uint64_t>(block & BlkNo_mask) << BlkNo_shift) |
24  (static_cast<uint64_t>(amc_no & AmcNo_mask) << AmcNo_shift) |
25  (static_cast<uint64_t>(board_id & BoardID_mask) << BoardID_shift) | (1llu << Enabled_bit_shift) |
26  (1llu << Present_bit_shift);
27 
28  if (block == getBlocks() - 1) {
29  // Last block
30  data_ |= (1llu << CRC_bit_shift) | (1llu << Valid_bit_shift) | (1llu << Length_bit_shift);
31  }
32 
33  if (block == 0 && getBlocks() == 1) {
34  // Bits already zeroed - only one block
35  } else if (block == 0) {
36  // First of many blocks
37  data_ |= 1llu << More_bit_shift;
38  } else if (block == getBlocks() - 1) {
39  // Last of many blocks
40  data_ |= 1llu << Segmented_bit_shift;
41  } else {
42  // Intermediate of many blocks
43  data_ |= (1llu << More_bit_shift) | (1llu << Segmented_bit_shift);
44  }
45  }
46 
47  unsigned int BlockHeader::getBlocks() const {
48  // The first block of a segmented event has a size of 1023, all
49  // following have a max size of 4096. Segmentation only happens
50  // for AMC payloads >= 0x13ff 64 bit words.
51  unsigned int size = getSize();
52  if (size >= 0x13ff)
53  return (size - 1023) / 4096 + 1;
54  return 1;
55  }
56 
57  unsigned int BlockHeader::getBlockSize() const {
58  // More and not Segmented means the first of multiple blocks. For
59  // these, getSize() returns the total size of the AMC packet, not the
60  // size of the first block.
61  if (getMore() && !getSegmented())
62  return split_block_size;
63  return getSize();
64  }
65 
66  Header::Header(unsigned int amc_no,
67  unsigned int lv1_id,
68  unsigned int bx_id,
69  unsigned int size,
70  unsigned int or_n,
71  unsigned int board_id,
72  unsigned int user)
73  : data0_((uint64_t(amc_no & AmcNo_mask) << AmcNo_shift) | (uint64_t(lv1_id & LV1ID_mask) << LV1ID_shift) |
74  (uint64_t(bx_id & BX_mask) << BX_shift) | (uint64_t(size & Size_mask) << Size_shift)),
75  data1_((uint64_t(or_n & OrN_mask) << OrN_shift) | (uint64_t(board_id & BoardID_mask) << BoardID_shift) |
76  (uint64_t(user & User_mask) << User_shift)) {}
77 
78  Trailer::Trailer(unsigned int crc, unsigned int lv1_id, unsigned int size)
79  : data_((uint64_t(crc & CRC_mask) << CRC_shift) | (uint64_t(lv1_id & LV1ID_mask) << LV1ID_shift) |
80  (uint64_t(size & Size_mask) << Size_shift)) {}
81 
82  bool Trailer::check(unsigned int crc, unsigned int lv1_id, unsigned int size, bool mtf7_mode) const {
83  if ((crc != getCRC() || size != getSize() || (lv1_id & LV1ID_mask) != getLV1ID()) && !mtf7_mode) {
84  edm::LogWarning("L1T") << "Found AMC trailer with:"
85  << "\n\tLV1 ID " << getLV1ID() << ", size " << getSize() << ", CRC " << std::hex
86  << std::setw(8) << std::setfill('0') << getCRC() << std::dec << "\nBut expected:"
87  << "\n\tLV1 ID " << (lv1_id & LV1ID_mask) << ", size " << size << ", CRC " << std::hex
88  << std::setw(8) << std::setfill('0') << crc;
89  return false;
90  }
91  return true;
92  }
93 
95  std::string dstring(reinterpret_cast<const char *>(start), reinterpret_cast<const char *>(end) + 4);
96  auto crc = cms::CRC32Calculator(dstring).checksum();
97 
98  *end = ((*end) & ~(uint64_t(CRC_mask) << CRC_shift)) | (static_cast<uint64_t>(crc & CRC_mask) << CRC_shift);
99  }
100 
101  Packet::Packet(unsigned int amc,
102  unsigned int board,
103  unsigned int lv1id,
104  unsigned int orbit,
105  unsigned int bx,
106  const std::vector<uint64_t> &load,
107  unsigned int user)
108  : block_header_(amc, board, load.size() + 3), // add 3 words for header (2) and trailer (1)
109  header_(amc, lv1id, bx, load.size() + 3, orbit, board, user),
110  trailer_(0, lv1id, load.size() + 3) {
111  auto hdata = header_.raw();
112  payload_.reserve(load.size() + 3);
113  payload_.insert(payload_.end(), hdata.begin(), hdata.end());
114  payload_.insert(payload_.end(), load.begin(), load.end());
115  payload_.insert(payload_.end(), trailer_.raw());
116 
117  auto ptr = payload_.data();
118  Trailer::writeCRC(ptr, ptr + payload_.size() - 1);
119  }
120 
121  void Packet::addPayload(const uint64_t *data, unsigned int size) {
122  payload_.insert(payload_.end(), data, data + size);
123  }
124 
125  void Packet::finalize(unsigned int lv1, unsigned int bx, bool legacy_mc, bool mtf7_mode) {
126  if (legacy_mc) {
127  header_ =
129 
130  payload_.insert(payload_.begin(), {0, 0});
131  payload_.insert(payload_.end(), {0});
132  } else {
133  header_ = Header(payload_.data());
134  trailer_ = Trailer(&payload_.back());
135 
136  std::string check(reinterpret_cast<const char *>(payload_.data()), payload_.size() * 8 - 4);
137  auto crc = cms::CRC32Calculator(check).checksum();
138 
139  trailer_.check(crc, lv1, header_.getSize(), mtf7_mode);
140  }
141  }
142 
143  std::vector<uint64_t> Packet::block(unsigned int id) const {
144  if (id == 0 and id == block_header_.getBlocks() - 1) {
145  return payload_;
146  } else if (id == block_header_.getBlocks() - 1) {
147  return std::vector<uint64_t>(payload_.begin() + id * split_block_size, payload_.end());
148  } else {
149  return std::vector<uint64_t>(payload_.begin() + id * split_block_size,
150  payload_.begin() + (id + 1) * split_block_size);
151  }
152  }
153 
154  std::unique_ptr<uint64_t[]> Packet::data() {
155  // Remove 3 words: 2 for the header, 1 for the trailer
156  std::unique_ptr<uint64_t[]> res(new uint64_t[payload_.size() - 3]);
157  for (unsigned int i = 0; i < payload_.size() - 3; ++i)
158  res.get()[i] = payload_[i + 2];
159  return res;
160  }
161 } // namespace amc
size
Write out results.
Definition: start.py:1
static const unsigned int CRC_mask
Definition: AMCSpec.h:123
static const unsigned int BoardID_mask
Definition: AMCSpec.h:44
static const unsigned int Size_shift
Definition: AMCSpec.h:37
static const unsigned int CRC_bit_shift
Definition: AMCSpec.h:52
unsigned int getLV1ID() const
Definition: AMCSpec.h:109
static void writeCRC(const uint64_t *start, uint64_t *end)
Definition: AMCSpec.cc:94
unsigned int getCRC() const
Definition: AMCSpec.h:108
BlockHeader block_header_
Definition: AMCSpec.h:154
unsigned int getBlocks() const
Definition: AMCSpec.cc:47
static const unsigned int CRC_shift
Definition: AMCSpec.h:122
static const unsigned int Segmented_bit_shift
Definition: AMCSpec.h:48
std::vector< uint64_t > raw() const
Definition: AMCSpec.h:79
static const unsigned int Present_bit_shift
Definition: AMCSpec.h:50
uint64_t raw() const
Definition: AMCSpec.h:112
Definition: Electron.h:6
static const unsigned int AmcNo_shift
Definition: AMCSpec.h:41
static const unsigned int BlkNo_shift
Definition: AMCSpec.h:39
void addPayload(const uint64_t *, unsigned int)
Definition: AMCSpec.cc:121
void finalize(unsigned int lv1, unsigned int bx, bool legacy_mc=false, bool mtf7_mode=false)
Definition: AMCSpec.cc:125
static const unsigned int Valid_bit_shift
Definition: AMCSpec.h:51
constexpr uint64_t CRC_mask
bool check(unsigned int crc, unsigned int lv1_id, unsigned int size, bool mtf7_mode=false) const
Definition: AMCSpec.cc:82
static const unsigned int BlkNo_mask
Definition: AMCSpec.h:40
static const unsigned int Size_mask
Definition: AMCSpec.h:38
static const unsigned int split_block_size
Definition: AMCSpec.h:9
unsigned int getMore() const
Definition: AMCSpec.h:31
unsigned int getAMCNumber() const
Definition: AMCSpec.h:28
static const unsigned int More_bit_shift
Definition: AMCSpec.h:47
Trailer trailer_
Definition: AMCSpec.h:159
uint64_t data_
Definition: AMCSpec.h:54
static const unsigned int AmcNo_mask
Definition: AMCSpec.h:42
static const unsigned int LV1ID_mask
Definition: AMCSpec.h:121
constexpr uint32_t CRC_shift
static const unsigned int BoardID_shift
Definition: AMCSpec.h:43
unsigned long long uint64_t
Definition: Time.h:13
unsigned int getBoardID() const
Definition: AMCSpec.h:29
def load(fileName)
Definition: svgfig.py:547
unsigned int getSize() const
Definition: AMCSpec.h:30
unsigned int getSize() const
Definition: AMCSpec.h:76
unsigned int getSize() const
Definition: AMCSpec.h:110
Header header_
Definition: AMCSpec.h:158
static const unsigned int Length_bit_shift
Definition: AMCSpec.h:46
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
std::unique_ptr< uint64_t[]> data()
Definition: AMCSpec.cc:154
std::vector< uint64_t > block(unsigned int id) const
Definition: AMCSpec.cc:143
static const unsigned int Enabled_bit_shift
Definition: AMCSpec.h:49
unsigned int getSegmented() const
Definition: AMCSpec.h:32
unsigned int size() const
Definition: AMCSpec.h:154
Log< level::Warning, false > LogWarning
std::uint32_t checksum()
Packet(const uint64_t *d)
Definition: AMCSpec.h:130
Definition: AMCSpec.h:8
unsigned int getBlockSize() const
Definition: AMCSpec.cc:57
std::vector< uint64_t > payload_
Definition: AMCSpec.h:161