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
amc::Packet::Packet
Packet(const uint64_t *d)
Definition: AMCSpec.h:130
amc::Trailer::getCRC
unsigned int getCRC() const
Definition: AMCSpec.h:108
amc::Packet::addPayload
void addPayload(const uint64_t *, unsigned int)
Definition: AMCSpec.cc:121
mps_fire.i
i
Definition: mps_fire.py:428
start
Definition: start.py:1
MessageLogger.h
amc::BlockHeader::BlkNo_shift
static const unsigned int BlkNo_shift
Definition: AMCSpec.h:39
amc::Packet::finalize
void finalize(unsigned int lv1, unsigned int bx, bool legacy_mc=false, bool mtf7_mode=false)
Definition: AMCSpec.cc:125
amc::Trailer::writeCRC
static void writeCRC(const uint64_t *start, uint64_t *end)
Definition: AMCSpec.cc:94
amc::BlockHeader::BlockHeader
BlockHeader()
Definition: AMCSpec.h:15
amc::Trailer
Definition: AMCSpec.h:102
amc::Packet::trailer_
Trailer trailer_
Definition: AMCSpec.h:159
amc::Header
Definition: AMCSpec.h:59
amc::BlockHeader::getSegmented
unsigned int getSegmented() const
Definition: AMCSpec.h:32
amc::BlockHeader::BoardID_mask
static const unsigned int BoardID_mask
Definition: AMCSpec.h:44
l1GtPatternGenerator_cfi.bx
bx
Definition: l1GtPatternGenerator_cfi.py:18
cms::CRC32Calculator
Definition: CRC32Calculator.h:62
cms::CRC32Calculator::checksum
std::uint32_t checksum()
Definition: CRC32Calculator.h:66
amc::Trailer::Trailer
Trailer()
Definition: AMCSpec.h:104
amc::Trailer::check
bool check(unsigned int crc, unsigned int lv1_id, unsigned int size, bool mtf7_mode=false) const
Definition: AMCSpec.cc:82
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
amc::Trailer::CRC_shift
static const unsigned int CRC_shift
Definition: AMCSpec.h:122
amc::split_block_size
static const unsigned int split_block_size
Definition: AMCSpec.h:9
amc::Packet::payload_
std::vector< uint64_t > payload_
Definition: AMCSpec.h:161
amc::BlockHeader::Valid_bit_shift
static const unsigned int Valid_bit_shift
Definition: AMCSpec.h:51
amc::BlockHeader::Size_mask
static const unsigned int Size_mask
Definition: AMCSpec.h:38
amc::Header::Header
Header()
Definition: AMCSpec.h:61
RPCNoise_example.check
check
Definition: RPCNoise_example.py:71
amc::BlockHeader::getBlocks
unsigned int getBlocks() const
Definition: AMCSpec.cc:47
amc::BlockHeader::Segmented_bit_shift
static const unsigned int Segmented_bit_shift
Definition: AMCSpec.h:48
mps_fire.end
end
Definition: mps_fire.py:242
amc::Trailer::raw
uint64_t raw() const
Definition: AMCSpec.h:112
svgfig.load
def load(fileName)
Definition: svgfig.py:547
amc::Packet::header_
Header header_
Definition: AMCSpec.h:158
amc::BlockHeader::getMore
unsigned int getMore() const
Definition: AMCSpec.h:31
amc::BlockHeader::data_
uint64_t data_
Definition: AMCSpec.h:54
amc::BlockHeader::Present_bit_shift
static const unsigned int Present_bit_shift
Definition: AMCSpec.h:50
amc::BlockHeader::More_bit_shift
static const unsigned int More_bit_shift
Definition: AMCSpec.h:47
amc::Packet::block
std::vector< uint64_t > block(unsigned int id) const
Definition: AMCSpec.cc:143
amc::BlockHeader::AmcNo_shift
static const unsigned int AmcNo_shift
Definition: AMCSpec.h:41
amc::Trailer::getSize
unsigned int getSize() const
Definition: AMCSpec.h:110
amc::BlockHeader::Length_bit_shift
static const unsigned int Length_bit_shift
Definition: AMCSpec.h:46
amc::BlockHeader::getBlockSize
unsigned int getBlockSize() const
Definition: AMCSpec.cc:57
groupFilesInBlocks.block
block
Definition: groupFilesInBlocks.py:150
amc::Header::raw
std::vector< uint64_t > raw() const
Definition: AMCSpec.h:79
amc::BlockHeader::BlkNo_mask
static const unsigned int BlkNo_mask
Definition: AMCSpec.h:40
amc::BlockHeader::Enabled_bit_shift
static const unsigned int Enabled_bit_shift
Definition: AMCSpec.h:49
res
Definition: Electron.h:6
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
amc::BlockHeader::getAMCNumber
unsigned int getAMCNumber() const
Definition: AMCSpec.h:28
EnviromentSettings.user
user
Definition: EnviromentSettings.py:30
amc::BlockHeader::BoardID_shift
static const unsigned int BoardID_shift
Definition: AMCSpec.h:43
amc::BlockHeader::getBoardID
unsigned int getBoardID() const
Definition: AMCSpec.h:29
amc::Header::getSize
unsigned int getSize() const
Definition: AMCSpec.h:76
amc::Trailer::CRC_mask
static const unsigned int CRC_mask
Definition: AMCSpec.h:123
amc::Trailer::LV1ID_mask
static const unsigned int LV1ID_mask
Definition: AMCSpec.h:121
amc::BlockHeader::getSize
unsigned int getSize() const
Definition: AMCSpec.h:30
amc
Definition: AMCSpec.h:8
amc::BlockHeader::AmcNo_mask
static const unsigned int AmcNo_mask
Definition: AMCSpec.h:42
CRC32Calculator.h
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
amc::Packet::block_header_
BlockHeader block_header_
Definition: AMCSpec.h:154
AMCSpec.h
amc::BlockHeader::CRC_bit_shift
static const unsigned int CRC_bit_shift
Definition: AMCSpec.h:52
TauDecayModes.dec
dec
Definition: TauDecayModes.py:142
amc::Packet::data
std::unique_ptr< uint64_t[]> data()
Definition: AMCSpec.cc:154
sipixelconstants::CRC_shift
constexpr uint32_t CRC_shift
Definition: SiPixelDigiConstants.h:22
amc::BlockHeader::Size_shift
static const unsigned int Size_shift
Definition: AMCSpec.h:37
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
amc::Trailer::getLV1ID
unsigned int getLV1ID() const
Definition: AMCSpec.h:109
sipixelconstants::CRC_mask
constexpr uint64_t CRC_mask
Definition: SiPixelDigiConstants.h:30
amc::Packet::size
unsigned int size() const
Definition: AMCSpec.h:154