ATLAS Offline Software
NSWTriggerSTGL1AElink.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
5 
6 #include <cmath>
7 #include <cstddef>
8 #include <iterator>
9 #include <sstream>
10 #include <stdexcept>
11 #include <string>
12 
17 #include "ers/ers.h"
18 
21  // TODO: once format finalized, checking a minimum size
22 
23  // 2 felix header 32b words already decoded;
24  constexpr static auto START_DATA = std::size_t{2 * 32};
25  auto readPointer = std::size_t{START_DATA};
26  int version = parse_version_workaround(readPointer);
27  decode_header(readPointer, version);
28  decode_data(readPointer);
29  decode_trailer(readPointer);
30 }
31 
32 void Muon::nsw::NSWTriggerSTGL1AElink::decode_header(std::size_t& readPointer, int version) {
33 
34  // This part is constant for all versions
35  m_head_fragID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_fragID);
36  m_head_sectID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_sectID);
37  m_head_EC = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_EC);
38  m_head_flags = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_flags);
39  m_head_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_BCID);
40  m_head_orbit = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_orbit);
41  m_head_spare = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_head_spare);
42  m_L1ID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_L1ID);
43 
44  ERS_DEBUG(2, Muon::nsw::format("\n TP header: \n"
45  " fradID: {}\n"
46  " sectID: {}\n"
47  " EC: {}\n"
48  " flags: {}\n"
49  " BCID: {}\n"
50  " orbit: {}\n"
51  " spare: {}\n"
52  " L1ID: {}",
53  m_head_fragID, m_head_sectID, m_head_EC, m_head_flags, m_head_BCID, m_head_orbit, m_head_spare, m_L1ID));
54 
55  if (version == 1)
56  {
57  m_l1a_open_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_open_BCID);
58  m_l1a_req_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_req_BCID);
59  m_l1a_close_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_close_BCID);
60  m_l1a_timeout = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_timeout); // overflow count
61  m_l1a_wdw_matching_engines_usage =
62  Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_wdw_matching_engines_usage);
63  m_l1a_open_BCID_offset = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_open_BCID_offset);
64  m_l1a_req_BCID_offset = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_req_BCID_offset);
65  m_l1a_close_BCID_offset = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_close_BCID_offset);
66  m_l1a_timeout_config = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_timeout_config);
67 
68  m_l1a_link_const = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_link_const);
69  }
70  else
71  {
72  m_l1a_versionID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_versionID);
73  m_l1a_local_req_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_local_req_BCID);
74  m_l1a_local_rel_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_local_rel_BCID);
75  m_l1a_open_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_open_BCID);
76  m_l1a_req_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_req_BCID);
77  m_l1a_close_BCID = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_close_BCID);
78  m_l1a_timeout = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_timeout);
79  m_l1a_open_BCID_offset = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_open_BCID_offset);
80  m_l1a_req_BCID_offset = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_req_BCID_offset);
81  m_l1a_close_BCID_offset = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_close_BCID_offset);
82  m_l1a_timeout_config = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_timeout_config);
83  m_l1a_busy_thr = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_busy_thr);
84  m_l1a_engine_snapshot = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_engine_snapshot);
85  m_l1a_link_const = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_link_const);
86  m_l1a_padding = Muon::nsw::decode_and_advance<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::size_l1a_padding);
87  }
88 
89 }
90 
92 {
94  auto anchor = Muon::nsw::decode_at_loc<uint64_t>(m_data, readPointer, Muon::nsw::STGTPL1A::loc_version1_anchor, Muon::nsw::STGTPL1A::size_l1a_link_const);
95 
96  if (anchor == anchor_value)
97  {
98  return 1;
99  }
100 
101  return 2;
102 }
103 
104 void Muon::nsw::NSWTriggerSTGL1AElink::decode_data(std::size_t& readPointer) {
105  static constexpr auto PADDING_BITS_END = std::size_t{16};
106  const auto endOfData = m_wordCountFlx * WORD_SIZE - Muon::nsw::STGTPL1A::size_trailer_CRC - PADDING_BITS_END;
107  while (readPointer < endOfData) {
108  static constexpr auto SIZE_DATA_HEADER = STGTPL1A::size_stream_head_nbits + STGTPL1A::size_stream_head_nwords +
110  if (readPointer + SIZE_DATA_HEADER > endOfData) {
111  throw std::length_error(
112  Muon::nsw::format("Read pointer ({}) would excede memory dedicated to data chunks ({}) while parsing the header (size: {})",
113  readPointer, endOfData, SIZE_DATA_HEADER));
114  }
115  const auto header_data = decode_data_header(readPointer);
116 
117  if (header_data.total_expected_size > m_wordCountFlx - ceil(readPointer / WORD_SIZE) + 1) {
118  throw std::length_error(Muon::nsw::format("STG TP stream size {} larger than expected packet size {}",
119  header_data.total_expected_size,
120  m_wordCountFlx - ceil(readPointer / WORD_SIZE) + 1));
121  }
122  if (header_data.nwords * header_data.data_size + readPointer > endOfData) {
123  throw std::length_error(Muon::nsw::format("Requested to decode {} bits but only {} bits are remaining",
124  header_data.nwords * header_data.data_size, endOfData - readPointer));
125  }
126 
127  m_stream_data.push_back(decode_data_payload(readPointer, header_data));
128  }
129  analyze_data();
130 }
131 
133  std::size_t& readPointer) {
134  const auto current_stream_head_nbits = decode(readPointer, STGTPL1A::size_stream_head_nbits);
135  const auto current_stream_head_nwords = decode(readPointer, STGTPL1A::size_stream_head_nwords);
136  const auto current_stream_head_fifo_size = decode(readPointer, STGTPL1A::size_stream_head_fifo_size);
137  const auto current_stream_head_streamID = decode(readPointer, STGTPL1A::size_stream_head_streamID);
138 
139  // zero padding to multiples of 16bits - TP logic - this is the real number
140  // of bits to read
141  const auto corrected_current_stream_head_nbits = correct_size_for_padding(current_stream_head_nbits);
142 
143  m_stream_head_nbits.push_back(corrected_current_stream_head_nbits);
144  m_stream_head_nwords.push_back(current_stream_head_nwords);
145  m_stream_head_fifo_size.push_back(current_stream_head_fifo_size);
146  m_stream_head_streamID.push_back(current_stream_head_streamID);
147 
148  const auto data_size = static_cast<std::size_t>(std::ceil(corrected_current_stream_head_nbits / WORD_SIZE_DOUBLE));
149  const auto total_expected_size = data_size * current_stream_head_nwords;
150 
151  ERS_DEBUG(2, Muon::nsw::format("stream_head_nbits: {}", corrected_current_stream_head_nbits));
152  ERS_DEBUG(2, Muon::nsw::format("stream_head_nwords: {}", current_stream_head_nwords));
153  ERS_DEBUG(2, Muon::nsw::format("stream_head_fifo_size: {}", current_stream_head_fifo_size));
154  ERS_DEBUG(2, Muon::nsw::format("stream_head_streamID: {}", current_stream_head_streamID));
155  ERS_DEBUG(2, Muon::nsw::format("total_expected_size: {}", data_size * current_stream_head_nwords));
156  ERS_DEBUG(2, Muon::nsw::format("m_wordCountFlx: {}, ceil(readPointer/{}): {}", m_wordCountFlx, WORD_SIZE_DOUBLE,
157  ceil(readPointer / WORD_SIZE_DOUBLE)));
158 
159  return {corrected_current_stream_head_nbits, current_stream_head_nwords, current_stream_head_fifo_size,
160  current_stream_head_streamID, total_expected_size, data_size};
161 }
162 
163 std::vector<std::vector<std::uint32_t>> Muon::nsw::NSWTriggerSTGL1AElink::decode_data_payload(
164  std::size_t& readPointer, const DataHeader& header) const {
165  std::vector<std::vector<std::uint32_t>> current_stream_data{};
166 
167  for (std::size_t i = 0; i < header.nwords; ++i) {
168  std::vector<std::uint32_t> data{};
169  for (std::size_t j = 0; j < header.data_size; ++j) {
170  data.push_back(decode(readPointer, WORD_SIZE));
171  }
172  current_stream_data.push_back(data);
173  }
174  return current_stream_data;
175 }
176 
177 void Muon::nsw::NSWTriggerSTGL1AElink::decode_trailer(std::size_t& readPointer) {
178  // TODO: warning: how the swROD is behaving if the last work is a uint16 only? Just 0-padding?
179  m_trailer_CRC = decode(readPointer, Muon::nsw::STGTPL1A::size_trailer_CRC);
180 }
181 
182 std::uint64_t Muon::nsw::NSWTriggerSTGL1AElink::decode(std::size_t& readPointer, const std::size_t size) const {
183  return Muon::nsw::decode_and_advance<std::uint64_t, std::uint32_t>(m_data, readPointer, size);
184 }
185 
187  auto counterChunk = std::size_t{0};
188  for (const auto& dataChunk : m_stream_data) {
189  for (const auto& dataWord : dataChunk) {
190  const auto expectedSize =
191  static_cast<std::size_t>(std::ceil(m_stream_head_nbits.at(counterChunk) / WORD_SIZE_DOUBLE));
192  if (std::size(dataWord) != expectedSize) {
193  throw std::length_error(Muon::nsw::format("Stream data size {} does not match expected number of messages {}",
194  std::size(dataWord), expectedSize));
195  }
196  switch (m_stream_head_streamID.at(counterChunk)) {
198  m_pad_packets.emplace_back(dataWord);
199  break;
201  m_segment_packets.emplace_back(dataWord);
202  break;
203  default:
204  throw std::runtime_error(Muon::nsw::format("Invalid stream type {}", m_stream_head_streamID.at(counterChunk)));
205  }
206  }
207  ++counterChunk;
208  }
209 }
210 
212  static constexpr auto PADDING = 16;
213  if (initial % PADDING) {
214  return ((initial + PADDING - 1) / PADDING) * PADDING;
215  } else {
216  return initial;
217  }
218 }
Muon::nsw::STGTPL1A::size_l1a_wdw_matching_engines_usage
constexpr int size_l1a_wdw_matching_engines_usage
Definition: NSWSTGTPDecodeBitmaps.h:44
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Muon::nsw::STGTPL1A::size_head_EC
constexpr int size_head_EC
Definition: NSWSTGTPDecodeBitmaps.h:20
header
Definition: hcg.cxx:526
Muon::nsw::STGTPL1A::version1_anchor_value
constexpr uint32_t version1_anchor_value
Definition: NSWSTGTPDecodeBitmaps.h:46
Muon::nsw::STGTPL1A::size_stream_head_nbits
constexpr int size_stream_head_nbits
Definition: NSWSTGTPDecodeBitmaps.h:50
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Muon::nsw::STGTPL1A::size_head_BCID
constexpr int size_head_BCID
Definition: NSWSTGTPDecodeBitmaps.h:22
m_data
std::vector< T > m_data
Definition: TrackTruthMatchingBaseAlg.cxx:660
Muon::nsw::STGTPL1A::size_l1a_busy_thr
constexpr int size_l1a_busy_thr
Definition: NSWSTGTPDecodeBitmaps.h:38
Muon::nsw::STGTPL1A::size_l1a_timeout_config
constexpr int size_l1a_timeout_config
Definition: NSWSTGTPDecodeBitmaps.h:37
NSWSTGTPDecodeBitmaps.h
Muon::nsw::STGTPL1A::size_l1a_local_req_BCID
constexpr int size_l1a_local_req_BCID
Definition: NSWSTGTPDecodeBitmaps.h:28
Muon::nsw::STGTPL1A::size_l1a_padding
constexpr int size_l1a_padding
Definition: NSWSTGTPDecodeBitmaps.h:41
Muon::nsw::STGTPL1A::size_stream_head_streamID
constexpr int size_stream_head_streamID
Definition: NSWSTGTPDecodeBitmaps.h:53
Muon::nsw::STGTPL1A::size_l1a_close_BCID_offset
constexpr int size_l1a_close_BCID_offset
Definition: NSWSTGTPDecodeBitmaps.h:36
Muon::nsw::STGTPL1A::size_head_flags
constexpr int size_head_flags
Definition: NSWSTGTPDecodeBitmaps.h:21
Muon::nsw::STGTPL1A::size_l1a_versionID
constexpr int size_l1a_versionID
Definition: NSWSTGTPDecodeBitmaps.h:27
Muon::nsw::STGTPL1A::size_l1a_req_BCID_offset
constexpr int size_l1a_req_BCID_offset
Definition: NSWSTGTPDecodeBitmaps.h:35
Muon::nsw::STGTPL1A::size_l1a_req_BCID
constexpr int size_l1a_req_BCID
Definition: NSWSTGTPDecodeBitmaps.h:31
Muon::nsw::STGTPL1A::size_trailer_CRC
constexpr int size_trailer_CRC
Definition: NSWSTGTPDecodeBitmaps.h:54
STGTPPackets.h
Muon::nsw::STGTPSegments::merge_stream_header
constexpr int merge_stream_header
Definition: NSWSTGTPDecodeBitmaps.h:77
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
Muon::nsw::STGTPL1A::size_head_spare
constexpr int size_head_spare
Definition: NSWSTGTPDecodeBitmaps.h:24
Muon::nsw::STGTPL1A::size_stream_head_fifo_size
constexpr int size_stream_head_fifo_size
Definition: NSWSTGTPDecodeBitmaps.h:52
lumiFormat.i
int i
Definition: lumiFormat.py:92
Muon::nsw::STGTPL1A::size_l1a_link_const
constexpr int size_l1a_link_const
Definition: NSWSTGTPDecodeBitmaps.h:40
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
Muon::nsw::STGTPL1A::size_stream_head_nwords
constexpr int size_stream_head_nwords
Definition: NSWSTGTPDecodeBitmaps.h:51
Muon::nsw::STGTPL1A::loc_version1_anchor
constexpr int loc_version1_anchor
Definition: NSWSTGTPDecodeBitmaps.h:45
Muon::nsw::format
std::string format(const std::string &str, const T &arg)
Definition: NSWDecodeHelper.h:40
Muon::nsw::STGTPL1A::size_l1a_timeout
constexpr int size_l1a_timeout
Definition: NSWSTGTPDecodeBitmaps.h:33
Muon::nsw::STGTPL1A::size_l1a_open_BCID_offset
constexpr int size_l1a_open_BCID_offset
Definition: NSWSTGTPDecodeBitmaps.h:34
python.PerfMonSerializer.decode
def decode(s)
Definition: PerfMonSerializer.py:388
Muon::nsw::STGTPL1A::size_head_fragID
constexpr int size_head_fragID
Definition: NSWSTGTPDecodeBitmaps.h:18
Muon::nsw::STGTPL1A::size_l1a_open_BCID
constexpr int size_l1a_open_BCID
Definition: NSWSTGTPDecodeBitmaps.h:30
Muon::nsw::STGTPL1A::size_head_sectID
constexpr int size_head_sectID
Definition: NSWSTGTPDecodeBitmaps.h:19
NSWResourceId.h
get_generator_info.version
version
Definition: get_generator_info.py:33
Muon::nsw::STGTPPad::pad_stream_header
constexpr std::size_t pad_stream_header
Definition: NSWSTGTPDecodeBitmaps.h:61
Muon::nsw::STGTPL1A::size_l1a_engine_snapshot
constexpr int size_l1a_engine_snapshot
Definition: NSWSTGTPDecodeBitmaps.h:39
Muon::nsw::STGTPL1A::size_L1ID
constexpr int size_L1ID
Definition: NSWSTGTPDecodeBitmaps.h:25
python.web.remaining
remaining
Definition: web.py:132
Muon::nsw::STGTPL1A::size_head_orbit
constexpr int size_head_orbit
Definition: NSWSTGTPDecodeBitmaps.h:23
Muon::nsw::STGTPL1A::size_l1a_local_rel_BCID
constexpr int size_l1a_local_rel_BCID
Definition: NSWSTGTPDecodeBitmaps.h:29
Muon::nsw::STGTPL1A::size_l1a_close_BCID
constexpr int size_l1a_close_BCID
Definition: NSWSTGTPDecodeBitmaps.h:32