64 ldmx_log(debug) <<
"QIEDecoder: produce() starts! Event number: "
65 <<
event.getEventHeader().getEventNumber();
68 int n_samp = n_samples_;
69 ldmx_log(debug) <<
"num samples = " << n_samp;
71 ldmx_log(debug) <<
"Looking up input collection " << input_collection_ <<
"_"
73 const auto event_stream{
74 event.getCollection<uint8_t>(input_collection_, input_pass_name_)};
75 ldmx_log(debug) <<
"Got input collection" << input_collection_ <<
"_"
78 uint32_t time_epoch = 0;
81 for (
int i_w = 0; i_w < QIEStream::TIMESTAMP_LEN_BYTES; i_w++) {
82 int pos = QIEStream::TIMESTAMP_POS + i_w;
83 uint8_t time_word = event_stream.at(pos);
84 ldmx_log(debug) <<
"time stamp word at position " << pos
85 <<
" (with iW = " << i_w
86 <<
") = " << std::bitset<8>(time_word);
87 time_epoch |= (time_word << i_w * 8);
90 uint32_t time_clock = 0;
91 for (
int i_w = 0; i_w < QIEStream::TIMESTAMPCLOCK_LEN_BYTES; i_w++) {
92 int pos = QIEStream::TIMESTAMPCLOCK_POS + i_w;
93 uint8_t time_word = event_stream.at(pos);
94 ldmx_log(debug) <<
"time stamp ns word at position " << pos
95 <<
" (with iW = " << i_w
96 <<
") = " << std::bitset<8>(time_word);
97 time_clock |= (time_word << i_w * 8);
100 uint32_t time_spill = 0;
101 ldmx_log(debug) <<
"Before starting, timeSpill = " << time_spill <<
" ("
102 << std::bitset<64>(time_spill) <<
", or, " << std::hex
103 << time_spill << std::dec <<
") counts since start of spill";
105 for (
int i_w = 0; i_w < QIEStream::TIMESINCESPILL_LEN_BYTES; i_w++) {
106 int pos = QIEStream::TIMESINCESPILL_POS + i_w;
107 uint8_t time_word = event_stream.at(pos);
108 ldmx_log(debug) <<
"time since spill word at position " << pos
109 <<
" (with iW = " << i_w
110 <<
") = " << std::bitset<8>(time_word);
111 time_spill |= (time_word << i_w * 8);
113 ldmx_log(debug) <<
"time stamp words are : " << time_epoch <<
" ("
114 << std::bitset<64>(time_epoch) <<
") and " << time_clock
115 <<
" (" << std::bitset<64>(time_clock)
116 <<
") clock ticks, and " << time_spill <<
" ("
117 << std::bitset<64>(time_spill) <<
", or, " << std::hex
118 << time_spill << std::dec <<
") counts since start of spill";
120 int sig_bits_skip = 6;
121 int divisor = TMath::Power(2, 32 - sig_bits_skip);
124 time_spill = time_spill % divisor;
125 ldmx_log(debug) <<
"After taking it mod 2^" << 32 - sig_bits_skip
126 <<
" (which is " << divisor <<
", spill time is "
128 event.getEventHeader().setIntParameter(
"timeSinceSpill", time_spill);
130 TTimeStamp* time_stamp =
new TTimeStamp(time_epoch);
131 event.getEventHeader().setTimestamp(*time_stamp);
134 uint32_t trigger_id = 0;
136 for (
int i_w = 0; i_w < QIEStream::TRIGID_LEN_BYTES; i_w++) {
138 int pos = QIEStream::TRIGID_POS + i_w;
139 uint8_t t_i_dword = event_stream.at(pos);
140 ldmx_log(debug) <<
"trigger word at position " << pos
141 <<
" (with iW = " << i_w
142 <<
") = " << std::bitset<8>(t_i_dword);
143 trigger_id |= (t_i_dword << i_w * 8);
147 ldmx_log(debug) <<
" got triggerID " << std::bitset<32>(trigger_id);
152 ldmx_log(fatal) <<
"Got event number mismatch: framework reports "
153 <<
event.getEventHeader().getEventNumber()
154 <<
", stream says " << trigger_id;
167 uint8_t flags = event_stream.at(QIEStream::ERROR_POS);
169 bool is_ci_dskipped{
static_cast<bool>((flags >> QIEStream::CID_SKIP_POS) &
171 bool is_ci_dunsync{
static_cast<bool>((flags >> QIEStream::CID_UNSYNC_POS) &
182 uint8_t reference_checksum = 0;
183 int checksum{(flags >> QIEStream::CHECKSUM_POS) &
187 if (checksum != reference_checksum)
188 ldmx_log(fatal) <<
"Got checksum mismatch: expected "
189 << (int)reference_checksum <<
", stream says " << checksum;
190 if (is_ci_dunsync) ldmx_log(debug) <<
"Found unsynced CIDs!";
191 if (is_ci_dskipped) ldmx_log(fatal) <<
"Found skipped CIDs!";
194 std::vector<trigscint::TrigScintQIEDigis> out_digis;
195 std::map<int, std::vector<int>> ad_cmap;
196 std::map<int, std::vector<int>> td_cmap;
201 std::max(std::max(QIEStream::ERROR_POS, QIEStream::CHECKSUM_POS),
202 QIEStream::TRIGID_POS + (QIEStream::TRIGID_LEN_BYTES)) +
205 int n_words = n_samp * n_channels_ * 2 + i_wstart;
208 int i_word = i_wstart;
209 ldmx_log(debug) <<
"Event parsing starts at vector idx " << i_wstart
210 <<
" and nWords = " << n_words;
213 for (
int i_s = 0; i_s < n_samp; i_s++) {
214 for (
int i_q = 0; i_q < n_channels_; i_q++) {
215 if (i_word >= n_words) {
217 <<
"More words than expected! Breaking ADC loop in sample " << i_s
218 <<
" at iQ = " << i_q;
221 uint8_t val = event_stream.at(i_word);
223 ldmx_log(debug) <<
"got ADC value " << (unsigned)val
224 <<
" at channel (elec) idx " << i_q;
225 if (ad_cmap.find(i_q) == ad_cmap.end()) {
226 std::vector<int> adcs(n_samp, 0);
227 ad_cmap.insert(std::pair<
int, std::vector<int>>(i_q, adcs));
229 ad_cmap[i_q].at(i_s) = val;
233 for (
int i_q = 0; i_q < n_channels_; i_q++) {
234 if (i_word >= n_words) {
236 <<
"More words than expected! Breaking TDC loop in sample " << i_s
237 <<
" at iQ = " << i_q;
240 uint8_t val = event_stream.at(i_word);
243 ldmx_log(debug) <<
"got TDC value " << (unsigned)val
244 <<
" at channel (elec) idx " << i_q;
246 if (td_cmap.find(i_q) == td_cmap.end()) {
247 std::vector<int> tdcs(n_samp, 0);
248 td_cmap.insert(std::pair<
int, std::vector<int>>(i_q, tdcs));
255 td_cmap[i_q].at(i_s) = val;
259 ldmx_log(debug) <<
"Done with sample " << i_s;
262 ldmx_log(debug) <<
"Done reading in header, ADC and TDC for event "
264 for (std::map<
int, std::vector<int>>::iterator itr = ad_cmap.begin();
265 itr != ad_cmap.end(); ++itr) {
268 if (channel_map_.find(itr->first) == channel_map_.end()) {
270 <<
"Couldn't find the bar ID corresponding to electronics ID "
271 << itr->first <<
"!! Skipping.";
274 int bar = channel_map_[itr->first];
277 digi.
setTDC(td_cmap[itr->first]);
280 ldmx_log(debug) <<
"for bar 0, got time since spill "
282 out_digis.push_back(digi);
283 ldmx_log(debug) <<
"Iterator points to key " << itr->first
284 <<
" and mapped channel supposedly is " << bar;
285 ldmx_log(debug) <<
"Made digi with elecID = " << digi.
getElecID()
286 <<
", barID = " << digi.
getChanID() <<
", third adc value "
287 << digi.
getADC().at(2) <<
" and third tdc "
291 event.add(output_collection_, out_digis);