LDMX Software
ECONDDecoder.cxx
1#include "Packing/ECONDDecoder.h"
2
3#include <cstring>
4
6#include "DetDescr/HcalElectronicsID.h"
11
12namespace packing {
13
14namespace {
15
20uint32_t toDigiWord(uint32_t hgcroc_word) {
21 const uint32_t tc{(hgcroc_word >> 31) & 1u}, tp{(hgcroc_word >> 30) & 1u};
22 return (hgcroc_word & 0x3FFFFFFFu) | (tp << 31) | (tc << 30);
23}
24
25const std::array<const char*, MultiSampleECONDEventPacket::N_CORRUPTION>
26 FRAME_FLAG_NAMES = {"resync (missing capture header)",
27 "lost sync",
28 "no trailer",
29 "trailer sample count mismatch",
30 "ECON ID mismatch",
31 "sample index mismatch",
32 "truncated frame",
33 "non-zero words after trailer",
34 "no sample of interest"};
35
36const std::array<const char*, ECONDEventPacket::N_CORRUPTION>
37 PACKET_FLAG_NAMES = {"bad header marker (missing ECON-D header)",
38 "bad length",
39 "bad header CRC-8",
40 "bad CRC-32",
41 "bad payload (link sub-packets do not fit)",
42 "truncated (T flag)"};
43
45template <std::size_t N>
46int toMask(const std::array<bool, N>& flags) {
47 int mask{0};
48 for (std::size_t i{0}; i < N; i++) {
49 if (flags[i]) mask |= (1 << i);
50 }
51 return mask;
52}
53
54} // namespace
55
57 raw_data_name_ = ps.get<std::string>("raw_data_name");
58 raw_data_pass_ = ps.get<std::string>("raw_data_pass");
59 digi_output_name_ = ps.get<std::string>("digi_output_name");
60 info_output_name_ = ps.get<std::string>("info_output_name");
61 is_ecal_ = ps.get<bool>("is_ecal");
62 n_links_ = ps.get<int>("n_links");
63 fiber_ = ps.get<int>("fiber");
64 translate_eid_ = ps.get<bool>("translate_eid");
65 roc_version_ = ps.get<int>("roc_version");
66 econd_id_ = ps.get<int>("econd_id");
67 drop_suppressed_ = ps.get<bool>("drop_suppressed");
68 packet_ = std::make_unique<MultiSampleECONDEventPacket>(n_links_, econd_id_);
69}
70
71unsigned int ECONDDecoder::channelID(int i_link, int i_ch) const {
72 if (is_ecal_) {
73 return ldmx::EcalElectronicsID(fiber_, i_link, i_ch).raw();
74 }
75 return ldmx::HcalElectronicsID(fiber_, i_link, i_ch).raw();
76}
77
82 digis.setNumSamplesPerDigi(0);
84
85 // e.g. an EventBuilder event without a fragment from this subsystem
86 if (not event.exists(raw_data_name_, raw_data_pass_)) {
88 info.setFrame(1 << ldmx::ECONDEventInfo::NO_FRAME, -1, 0, 0);
89 event.add(digi_output_name_, digis);
90 event.add(info_output_name_, info);
91 return;
92 }
93
94 const auto& raw_binary{
95 event.getCollection<uint8_t>(raw_data_name_, raw_data_pass_)};
96 n_frames_++;
97
98 int frame_corruption{0};
99 if (raw_binary.size() % 4 != 0) {
100 ldmx_log(debug) << raw_data_name_ << " has " << raw_binary.size()
101 << " bytes which is not a multiple of 4, ignoring the "
102 << raw_binary.size() % 4 << " trailing bytes";
103 frame_corruption |= (1 << ldmx::ECONDEventInfo::BAD_FRAME_SIZE);
105 }
106 // copy into 32-bit words, the data is little endian
107 std::vector<uint32_t> words(raw_binary.size() / 4);
108 std::memcpy(words.data(), raw_binary.data(), words.size() * 4);
109
110 packet_->from(words);
111 const auto& pkt{*packet_};
112 frame_corruption |= toMask(pkt.corruption_);
113 for (std::size_t i{0}; i < pkt.corruption_.size(); i++) {
114 if (pkt.corruption_[i]) n_frame_flags_[i]++;
115 }
116
117 bool all_good = (frame_corruption == 0) and not pkt.samples_.empty();
118 for (std::size_t i_sample{0}; i_sample < pkt.samples_.size(); i_sample++) {
119 const auto& header{pkt.headers_[i_sample]};
120 const auto& sample{pkt.samples_[i_sample]};
121 n_packets_++;
122
123 ldmx::ECONDSampleInfo sample_info;
124 sample_info.setCaptureHeader(header.econd_id_, header.i_l1a_,
125 header.is_soi_, header.econd_len_);
126 int flags = (sample.passthrough_ ? 1 : 0) | (sample.expected_ ? 2 : 0) |
127 (sample.match_ ? 4 : 0) | (sample.truncated_ ? 8 : 0) |
128 (sample.subpacket_error_ ? 16 : 0);
129 sample_info.setHeader(sample.payload_length_, flags, sample.ht_,
130 sample.ebo_, sample.bx_, sample.l1a_, sample.orbit_,
131 sample.reset_request_);
132 sample_info.setCorruption(toMask(sample.corruption_));
133
134 for (std::size_t i{0}; i < sample.corruption_.size(); i++) {
135 if (sample.corruption_[i]) n_packet_flags_[i]++;
136 }
137
138 if (sample.isCorrupt()) {
140 all_good = false;
141 } else if (sample.isEmpty()) {
143 all_good = false;
144 } else {
146 for (const auto& link : sample.links_) {
147 if (link.badStat()) n_links_bad_stat_++;
148 ldmx::ECONDLinkInfo link_info;
149 link_info.set(link.stat_, link.is_empty_, link.adc_cm0_, link.adc_cm1_,
150 link.calib_);
151 sample_info.addLink(link_info);
152 }
153 }
154
155 // L1A numbers are 6 bits
156 if (i_sample > 0 and not sample.isCorrupt() and
157 not pkt.samples_[i_sample - 1].isCorrupt() and
158 sample.l1a_ != ((pkt.samples_[i_sample - 1].l1a_ + 1) & 0x3f)) {
159 n_l1a_jumps_++;
160 }
161
162 info.addSample(sample_info);
163 }
164
165 info.setFrame(frame_corruption, pkt.trailer_n_samples_, pkt.i_soi_,
166 pkt.n_skipped_words_);
167
168 if (all_good) {
169 std::size_t n_samples{pkt.samples_.size()};
170 digis.setNumSamplesPerDigi(n_samples);
171 digis.setSampleOfInterestIndex(pkt.i_soi_);
172 // resort from (sample, link, channel) to (link, channel, sample)
173 std::vector<uint32_t> digi(n_samples);
174 for (int i_link{0}; i_link < n_links_; i_link++) {
175 for (int i_ch{0}; i_ch < 36; i_ch++) {
176 bool suppressed{true};
177 for (std::size_t i_sample{0}; i_sample < n_samples; i_sample++) {
178 digi[i_sample] =
179 toDigiWord(pkt.samples_[i_sample].links_[i_link].channels_[i_ch]);
180 if (digi[i_sample] != 0) suppressed = false;
181 }
182 if (drop_suppressed_ and suppressed) continue;
183 unsigned int id{channelID(i_link, i_ch)};
184 if (translate_eid_) {
185 if (is_ecal_) {
186 const auto& detmap{getCondition<ecal::EcalDetectorMap>(
189 if (not detmap.exists(eid)) continue;
190 id = detmap.get(eid).raw();
191 } else {
192 const auto& detmap{getCondition<hcal::HcalDetectorMap>(
195 if (not detmap.exists(eid)) continue;
196 id = detmap.get(eid).raw();
197 }
198 }
199 digis.addDigi(id, digi);
200 }
201 }
203 }
204 info.setDigisFilled(all_good);
205
206 event.add(digi_output_name_, digis);
207 event.add(info_output_name_, info);
208}
209
211 ldmx_log(info) << "ECON-D decoding summary for " << raw_data_name_;
212 ldmx_log(info) << " frames seen: " << n_frames_
213 << ", digis filled: " << n_frames_filled_;
214 if (n_no_frame_ > 0) {
215 ldmx_log(info) << " events without a frame: " << n_no_frame_;
216 }
217 if (n_bad_frame_size_ > 0) {
218 ldmx_log(info) << " frames with size not a multiple of 4 bytes: "
220 }
221 for (std::size_t i{0}; i < n_frame_flags_.size(); i++) {
222 if (n_frame_flags_[i] > 0) {
223 ldmx_log(info) << " frames with " << FRAME_FLAG_NAMES[i] << ": "
224 << n_frame_flags_[i];
225 }
226 }
227 ldmx_log(info) << " packets seen: " << n_packets_
228 << ", decoded: " << n_packets_decoded_
229 << ", empty: " << n_packets_empty_
230 << ", corrupt (skipped): " << n_packets_corrupt_;
231 for (std::size_t i{0}; i < n_packet_flags_.size(); i++) {
232 if (n_packet_flags_[i] > 0) {
233 ldmx_log(info) << " packets with " << PACKET_FLAG_NAMES[i] << ": "
234 << n_packet_flags_[i];
235 }
236 }
237 if (n_links_bad_stat_ > 0) {
238 ldmx_log(info) << " decoded links with bad Stat bits: "
240 }
241 ldmx_log(info) << " L1A jumps between samples: " << n_l1a_jumps_;
242}
243
244} // namespace packing
245
Classes holding the ECON-D packet data that does not fit into an HgcrocDigiCollection.
Class which contains logic for how the detector items connect to and relate with the reconstruction c...
Class that identifies a location in the Ecal readout chain.
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Class which contains logic for how the detector items connect to and relate with the reconstruction c...
Class that represents a digitized hit in a calorimeter cell readout by an HGCROC.
static constexpr const char * CONDITIONS_OBJECT_NAME
The name of the EID <-> DetID map for the ECal.
const T & getCondition(const std::string &condition_name)
Access a conditions object for the current event.
Implements an event buffer system for storing event data.
Definition Event.h:40
bool exists(const std::string &name, const std::string &passName, bool unique=true) const
Check for the existence of an object or collection with the given name and pass name in the event.
Definition Event.cxx:107
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
static constexpr const char * CONDITIONS_OBJECT_NAME
The name of the EID <-> DetID map for the ECal.
RawValue raw() const
Definition DetectorID.h:69
Supporting data for one event decoded from a single ECON-D.
Data from one eRx ("link") sub-packet of an ECON-D packet that is not a normal channel.
void set(int stat, bool is_empty, int adc_cm0, int adc_cm1, uint32_t calib)
Set the link data.
Capture header and ECON-D header data for one sample (L1A)
void setHeader(int payload_length, int flags, int ht, int ebo, int bx, int l1a, int orbit, int reset_request)
Set the ECON-D event header data.
void setCaptureHeader(int econ_id, int i_sample, bool is_soi, int length)
Set the capture header data.
void addLink(const ECONDLinkInfo &link)
add the data from the next link
void setCorruption(int mask)
set the corruption mask
Identifies a location in the Ecal readout chain.
Identifies a location in the Hcal readout chain.
Represents a collection of the digi hits readout by an HGCROC.
void setNumSamplesPerDigi(unsigned int n)
Set number of samples for each digi.
void setSampleOfInterestIndex(unsigned int n)
Set index of sample of interest.
void setVersion(int v)
Set the version of the ROC we have read.
void addDigi(unsigned int id, const std::vector< Sample > &digi)
Add samples to collection.
decode event packets built by ECON-D chips
int roc_version_
ROC version for the digi collection.
int econd_id_
ECON-D ID to decode from a merged frame (-1: the first one)
long n_l1a_jumps_
number of L1A jumps between consecutive samples
std::string raw_data_pass_
name of pass to get raw binary data from
long n_packets_empty_
number of empty packets (payload length 0)
void configure(framework::config::Parameters &ps) override
configure the decoder
bool translate_eid_
translate electronics IDs into detector IDs
std::string raw_data_name_
name of event object of raw binary data
int fiber_
optical fiber index used in the electronics ID
std::string digi_output_name_
output name of digi collection
int n_links_
number of enabled eRx on the ECON-D
std::string info_output_name_
output name of supporting data object
long n_links_bad_stat_
number of decoded link sub-packets with bad Stat bits
void onProcessEnd() override
print a summary of the decoding
void produce(framework::Event &event) override
decode the raw binary data into HgcrocDigiCollection that can be used later for reconstruction or ana...
long n_packets_
number of packets seen
unsigned int channelID(int i_link, int i_ch) const
get the channel ID for the input link and channel
long n_packets_corrupt_
number of corrupted packets (skipped)
std::array< long, MultiSampleECONDEventPacket::N_CORRUPTION > n_frame_flags_
number of frames with each frame-level flag
std::array< long, ECONDEventPacket::N_CORRUPTION > n_packet_flags_
number of packets with each corruption flag
long n_packets_decoded_
number of packets decoded without corruption and not empty
long n_no_frame_
number of events without an input frame
long n_bad_frame_size_
number of frames with a size that is not a multiple of 4 bytes
bool drop_suppressed_
skip channels suppressed in every sample
long n_frames_filled_
number of frames whose digis were filled
bool is_ecal_
which calorimeter subsystem we are decoding (true == Ecal, false == Hcal)
long n_frames_
number of frames seen
std::unique_ptr< MultiSampleECONDEventPacket > packet_
decoding buffer