66 ldmx_log(debug) <<
"QIEDecoder: produce() starts! Event number: "
67 <<
event.getEventHeader().getEventNumber();
70 int n_samp = n_samples_;
71 ldmx_log(debug) <<
"num samples = " << n_samp;
73 ldmx_log(debug) <<
"Looking up input collection " << input_collection_ <<
"_"
75 const auto event_stream{
76 event.getCollection<uint8_t>(input_collection_, input_pass_name_)};
77 ldmx_log(debug) <<
"Got input collection" << input_collection_ <<
"_"
80 uint32_t time_epoch = 0;
83 for (
int i_w = 0; i_w < QIEStream::TIMESTAMP_LEN_BYTES; i_w++) {
84 int pos = QIEStream::TIMESTAMP_POS + i_w;
85 uint8_t time_word = event_stream.at(pos);
86 ldmx_log(debug) <<
"time stamp word at position " << pos
87 <<
" (with iW = " << i_w
88 <<
") = " << std::bitset<8>(time_word);
89 time_epoch |= (time_word << i_w * 8);
92 uint32_t time_clock = 0;
93 for (
int i_w = 0; i_w < QIEStream::TIMESTAMPCLOCK_LEN_BYTES; i_w++) {
94 int pos = QIEStream::TIMESTAMPCLOCK_POS + i_w;
95 uint8_t time_word = event_stream.at(pos);
96 ldmx_log(debug) <<
"time stamp ns word at position " << pos
97 <<
" (with iW = " << i_w
98 <<
") = " << std::bitset<8>(time_word);
99 time_clock |= (time_word << i_w * 8);
102 uint32_t time_spill = 0;
103 ldmx_log(debug) <<
"Before starting, timeSpill = " << time_spill <<
" ("
104 << std::bitset<64>(time_spill) <<
", or, " << std::hex
105 << time_spill << std::dec <<
") counts since start of spill";
107 for (
int i_w = 0; i_w < QIEStream::TIMESINCESPILL_LEN_BYTES; i_w++) {
108 int pos = QIEStream::TIMESINCESPILL_POS + i_w;
109 uint8_t time_word = event_stream.at(pos);
110 ldmx_log(debug) <<
"time since spill word at position " << pos
111 <<
" (with iW = " << i_w
112 <<
") = " << std::bitset<8>(time_word);
113 time_spill |= (time_word << i_w * 8);
115 ldmx_log(debug) <<
"time stamp words are : " << time_epoch <<
" ("
116 << std::bitset<64>(time_epoch) <<
") and " << time_clock
117 <<
" (" << std::bitset<64>(time_clock)
118 <<
") clock ticks, and " << time_spill <<
" ("
119 << std::bitset<64>(time_spill) <<
", or, " << std::hex
120 << time_spill << std::dec <<
") counts since start of spill";
122 int sig_bits_skip = 6;
123 int divisor = TMath::Power(2, 32 - sig_bits_skip);
126 time_spill = time_spill % divisor;
127 ldmx_log(debug) <<
"After taking it mod 2^" << 32 - sig_bits_skip
128 <<
" (which is " << divisor <<
", spill time is "
130 event.getEventHeader().setIntParameter(
"timeSinceSpill", time_spill);
132 TTimeStamp* time_stamp =
new TTimeStamp(time_epoch);
133 event.getEventHeader().setTimestamp(*time_stamp);
136 uint32_t trigger_id = 0;
138 for (
int i_w = 0; i_w < QIEStream::TRIGID_LEN_BYTES; i_w++) {
140 int pos = QIEStream::TRIGID_POS + i_w;
141 uint8_t t_i_dword = event_stream.at(pos);
142 ldmx_log(debug) <<
"trigger word at position " << pos
143 <<
" (with iW = " << i_w
144 <<
") = " << std::bitset<8>(t_i_dword);
145 trigger_id |= (t_i_dword << i_w * 8);
149 ldmx_log(debug) <<
" got triggerID " << std::bitset<32>(trigger_id);
154 ldmx_log(fatal) <<
"Got event number mismatch: framework reports "
155 <<
event.getEventHeader().getEventNumber()
156 <<
", stream says " << trigger_id;
169 uint8_t flags = event_stream.at(QIEStream::ERROR_POS);
171 bool is_ci_dskipped{
static_cast<bool>((flags >> QIEStream::CID_SKIP_POS) &
173 bool is_ci_dunsync{
static_cast<bool>((flags >> QIEStream::CID_UNSYNC_POS) &
184 uint8_t reference_checksum = 0;
185 int checksum{(flags >> QIEStream::CHECKSUM_POS) &
189 if (checksum != reference_checksum)
190 ldmx_log(fatal) <<
"Got checksum mismatch: expected "
191 << (int)reference_checksum <<
", stream says " << checksum;
192 if (is_ci_dunsync) ldmx_log(debug) <<
"Found unsynced CIDs!";
193 if (is_ci_dskipped) ldmx_log(fatal) <<
"Found skipped CIDs!";
196 std::vector<trigscint::TrigScintQIEDigis> out_digis;
197 std::map<int, std::vector<int>> ad_cmap;
198 std::map<int, std::vector<int>> td_cmap;
203 std::max(std::max(QIEStream::ERROR_POS, QIEStream::CHECKSUM_POS),
204 QIEStream::TRIGID_POS + (QIEStream::TRIGID_LEN_BYTES)) +
207 int n_words = n_samp * n_channels_ * 2 + i_wstart;
210 int i_word = i_wstart;
211 ldmx_log(debug) <<
"Event parsing starts at vector idx " << i_wstart
212 <<
" and nWords = " << n_words;
215 for (
int i_s = 0; i_s < n_samp; i_s++) {
216 for (
int i_q = 0; i_q < n_channels_; i_q++) {
217 if (i_word >= n_words) {
219 <<
"More words than expected! Breaking ADC loop in sample " << i_s
220 <<
" at iQ = " << i_q;
223 uint8_t val = event_stream.at(i_word);
225 ldmx_log(debug) <<
"got ADC value " << (unsigned)val
226 <<
" at channel (elec) idx " << i_q;
227 if (ad_cmap.find(i_q) == ad_cmap.end()) {
228 std::vector<int> adcs(n_samp, 0);
229 ad_cmap.insert(std::pair<
int, std::vector<int>>(i_q, adcs));
231 ad_cmap[i_q].at(i_s) = val;
235 for (
int i_q = 0; i_q < n_channels_; i_q++) {
236 if (i_word >= n_words) {
238 <<
"More words than expected! Breaking TDC loop in sample " << i_s
239 <<
" at iQ = " << i_q;
242 uint8_t val = event_stream.at(i_word);
245 ldmx_log(debug) <<
"got TDC value " << (unsigned)val
246 <<
" at channel (elec) idx " << i_q;
248 if (td_cmap.find(i_q) == td_cmap.end()) {
249 std::vector<int> tdcs(n_samp, 0);
250 td_cmap.insert(std::pair<
int, std::vector<int>>(i_q, tdcs));
257 td_cmap[i_q].at(i_s) = val;
261 ldmx_log(debug) <<
"Done with sample " << i_s;
264 ldmx_log(debug) <<
"Done reading in header, ADC and TDC for event "
266 for (std::map<
int, std::vector<int>>::iterator itr = ad_cmap.begin();
267 itr != ad_cmap.end(); ++itr) {
270 if (channel_map_.find(itr->first) == channel_map_.end()) {
272 <<
"Couldn't find the bar ID corresponding to electronics ID "
273 << itr->first <<
"!! Skipping.";
276 int bar = channel_map_[itr->first];
279 digi.
setTDC(td_cmap[itr->first]);
282 ldmx_log(debug) <<
"for bar 0, got time since spill "
284 out_digis.push_back(digi);
285 ldmx_log(debug) <<
"Iterator points to key " << itr->first
286 <<
" and mapped channel supposedly is " << bar;
287 ldmx_log(debug) <<
"Made digi with elecID = " << digi.
getElecID()
288 <<
", barID = " << digi.
getChanID() <<
", third adc value "
289 << digi.
getADC().at(2) <<
" and third tdc "
293 event.add(output_collection_, out_digis);