Process the event and put new data products into it.
85 {
86 ldmx_log(debug) << "ZCCMDecoder: produce() starts! Event number: "
87 << event.getEventHeader().getEventNumber();
88
89
90 int n_samp = n_samples_;
91 ldmx_log(debug) << "num samples = " << n_samp;
92
93 ldmx_log(debug) << "Looking up input collection " << input_collection_ << "_"
94 << input_pass_name_;
95 const auto event_output{
96 event.getCollection<uint8_t>(input_collection_, input_pass_name_)};
97 ldmx_log(debug) << "Got input collection " << input_collection_ << "_"
98 << input_pass_name_;
99
100
101 std::size_t n_modules =
102 std::ranges::count_if(modules_used_, [](int x) { return x != 0; });
103
104
105
107 ZCCMOutput::TIMESTAMP_LEN_BYTES)) {
108 ldmx_log(debug) << "No " << input_collection_
109 << " frame in this event, writing empty digis";
110 std::vector<trigscint::TrigScintQIEDigis> no_digis;
111 for (uint i = 0; i < n_modules; i++)
112 event.
add(output_collection_ + Form(
"%i", i + 1), no_digis);
113 return;
114 }
115
116
117 uint32_t time_stamp = 0;
118 for (int i_w = 0; i_w < ZCCMOutput::TIMESTAMP_LEN_BYTES; i_w++) {
120 uint8_t time_word = event_output.at(pos);
121 ldmx_log(debug) << "time stamp word at position " << pos
122 << " (with iW = " << i_w
123 << ") = " << std::bitset<8>(time_word);
124 time_stamp |= (time_word << i_w * 8);
125 }
126
127 event.getEventHeader().setIntParameter("TS Timestamp", time_stamp);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 std::map<int, std::vector<int>> adc_map;
145 std::map<int, std::vector<int>> tdc_map;
146 std::map<int, std::vector<int>> cid_map;
147 std::map<int, std::vector<int>> bc0_map;
148 std::map<int, std::vector<int>> ce_map;
149
150 std::vector<std::vector<trigscint::TrigScintQIEDigis>> out_digis;
151
152 out_digis.resize(n_modules);
153
154
155
156 int word_length = ZCCMOutput::SAMPLE_WORD_LEN_BYTES;
157 int num_messages =
158 ((int)event_output.size() - ZCCMOutput::EVENTDATA_POS) / word_length;
159
160
161
162 int n_lanes = n_channels_ / ZCCMOutput::NUM_CHAN_PER_LANE;
163 int num_expected_messages = n_samp * n_lanes;
164 if (num_messages != num_expected_messages)
165 ldmx_log(warn) << "Unexpected stream length! Num messages is "
166 << num_messages << ", expect " << num_expected_messages;
167
168
169
170 int i_word = 0;
171 int start_lane = -1;
172 int sample_nb = -1;
173
174 while (i_word < num_messages) {
175 uint word_pos = ZCCMOutput::EVENTDATA_POS + i_word * word_length;
176
177 uint16_t empty =
178 event_output.at(word_pos + ZCCMOutput::EMPTY_WORD_SAMPLE_WORD_POS);
179 uint8_t lane = event_output.at(word_pos + ZCCMOutput::LANE_SAMPLE_WORD_POS);
180 uint8_t flag =
181 event_output.at(word_pos + ZCCMOutput::FLAGS_SAMPLE_WORD_POS);
182
183 ldmx_log(debug) << "Start of message " << i_word << ".\n\tEmpty word is "
184 << std::bitset<16>(empty) << ", read at position "
185 << word_pos + ZCCMOutput::EMPTY_WORD_SAMPLE_WORD_POS
186 << ". \n\tFlag word is " << std::bitset<8>(flag)
187 << ", read at position "
188 << word_pos + ZCCMOutput::FLAGS_SAMPLE_WORD_POS
189 << ". \n\tLane is " << std::bitset<8>(lane)
190 << ", read at position "
191 << word_pos + ZCCMOutput::LANE_SAMPLE_WORD_POS << ".";
192
193 if (start_lane == -1) {
194 start_lane = lane;
195 ldmx_log(debug) << "Set time sample start lane to " << start_lane;
196 }
197
198 int cid{(flag >> ZCCMOutput::CAPID_POS_IN_FLAG) &
200 ldmx_log(trace) << "Got Cap ID " << cid;
201 bool bc0{static_cast<bool>((flag >> ZCCMOutput::BC0_POS_IN_FLAG) &
203 ldmx_log(trace) << "Got BC0 flag " << bc0;
204 bool ce{static_cast<bool>((flag >> ZCCMOutput::CE_POS_IN_FLAG) &
206 ldmx_log(trace) << "Got CE flag " << ce;
207 int empty_bits{(flag >> ZCCMOutput::EMPTY_FLAG_WORD_POS_IN_FLAG) &
209 if (empty_bits)
210 ldmx_log(fatal) << "Empty bits of flag not empty: " << empty_bits;
211
212
213
214
215
216
217
218
219
220
221
222
223
224 if (lane == start_lane)
225 sample_nb++;
226
227 uint8_t cid_val = cid;
228
229
230 for (int i_c = 0; i_c < ZCCMOutput::NUM_CHAN_PER_LANE; i_c++) {
231 if (i_word >= num_expected_messages) {
232 ldmx_log(fatal)
233 << "More words than expected! Breaking event data loop in sample "
234 << sample_nb << " at lane = " << int(lane) << ", channel nb "
235 << i_c;
236 break;
237 }
238 uint8_t adc_val =
239 event_output.at(word_pos + ZCCMOutput::ADC_SAMPLE_WORD_POS + i_c);
240 uint8_t tdc_val =
241 event_output.at(word_pos + ZCCMOutput::TDC_SAMPLE_WORD_POS + i_c);
242
243
244 int module = module_map_[lane];
245 int elec_id = 100 * lane + 10 * module + i_c;
246
247 ldmx_log(trace) << "got ADC value " << (unsigned)adc_val
248 << " and TDC value " << (unsigned)tdc_val
249 << " at channel idx " << i_c << " with elec id "
250 << elec_id;
251 if (adc_map.find(elec_id) == adc_map.end()) {
252 std::vector<int> adcs(n_samp, 0);
253 adc_map.insert(std::pair<int, std::vector<int>>(elec_id, adcs));
254 }
255 adc_map[elec_id].at(sample_nb) = adc_val;
256
257
258 if (tdc_map.find(elec_id) == tdc_map.end()) {
259 std::vector<int> tdcs(n_samp, 0);
260 tdc_map.insert(std::pair<int, std::vector<int>>(elec_id, tdcs));
261 }
262 tdc_map[elec_id].at(sample_nb) = tdc_val;
263 if (cid_map.find(elec_id) == cid_map.end()) {
264 std::vector<int> cids(n_samp, 0);
265 cid_map.insert(std::pair<int, std::vector<int>>(elec_id, cids));
266 std::vector<int> bc0s(n_samp, 0);
267 bc0_map.insert(std::pair<int, std::vector<int>>(elec_id, bc0s));
268 std::vector<int> ces(n_samp, 0);
269 ce_map.insert(std::pair<int, std::vector<int>>(elec_id, ces));
270 }
271 cid_map[elec_id].at(sample_nb) = cid_val;
272 bc0_map[elec_id].at(sample_nb) = bc0;
273 ce_map[elec_id].at(sample_nb) = ce;
274 }
275 ldmx_log(debug) << "Done with lane " << int(lane) << " which we think is "
276 << i_word % n_lanes << " in sample " << sample_nb;
277
278 if (int(lane) != i_word % n_lanes)
279 ldmx_log(fatal) << "Lane ordering has been messed up! Expect lane "
280 << i_word % n_lanes << ", but we got lane " << int(lane)
281 << " in sample " << sample_nb;
282
283 i_word++;
284 }
285
286 ldmx_log(debug) << "Done reading in header, ADC and TDC for event "
287 << event.getEventNumber();
288
289
290
291 for (std::map<int, std::vector<int>>::iterator itr = adc_map.begin();
292 itr != adc_map.end(); ++itr) {
293 TrigScintQIEDigis digi;
294 digi.setADC(itr->second);
295 if (channel_map_.find(itr->first) == channel_map_.end()) {
296 ldmx_log(fatal)
297 << "Couldn't find the bar ID corresponding to electronics ID "
298 << itr->first << "!! Skipping.";
299 continue;
300 }
301 int bar = channel_map_[itr->first];
302 digi.setElecID(itr->first);
303 digi.setChanID(bar);
304 int module = itr->first / 10 % 10;
305 digi.setModuleID(module);
306 int lane = itr->first / 100;
307 digi.setLaneID(lane);
308 digi.setTDC(tdc_map[itr->first]);
309 digi.setCID(cid_map[itr->first]);
310 digi.setBC0(bc0_map[itr->first]);
311 digi.setCE(ce_map[itr->first]);
312 if (bar == 0)
313 ldmx_log(debug) << "for bar 0, got time since spill "
314 << digi.getTimeSinceSpill();
315 out_digis[module].push_back(digi);
316 ldmx_log(debug) << "Iterator points to key " << itr->first
317 << " and mapped channel supposedly is " << bar;
318 ldmx_log(debug) << "Made digi with elecID = " << digi.getElecID()
319 << ", barID = " << digi.getChanID() << ", third adc value "
320 << digi.getADC().at(2) << " and third tdc "
321 << digi.getTDC().at(2);
322 }
323
324
325 for (uint i = 0; i < n_modules; i++)
326 event.
add(output_collection_ + Form(
"%i", i + 1), out_digis[i]);
327}
void add(const std::string &collectionName, T &obj)
Adds an object to the event bus.
static const int TIMESTAMP_POS
for each time sample, every lane each sends a message: [ 1B ADC for 6 channels, 2B empty,...