14 output_collection_ = ps.
get<std::string>(
"output_collection");
15 input_collection_ = ps.
get<std::string>(
"input_collection");
16 input_pass_name_ = ps.
get<std::string>(
"input_pass_name");
17 channel_map_file_name_ = ps.
get<std::string>(
"channel_map_file");
18 n_channels_ = ps.
get<
int>(
"number_channels");
19 verbose_ = ps.
get<
bool>(
"verbose");
21 ldmx_log(debug) <<
"In configure, got parameters:" <<
"\noutput_collection = "
23 <<
"\ninput_collection = " << input_collection_
24 <<
"\ninput_pass_name = " << input_pass_name_
25 <<
"\nchannel_map_file = " << channel_map_file_name_
26 <<
"\nnumber_channels = " << n_channels_
27 <<
"\nverbose = " << verbose_;
30 channel_map_file_.open(channel_map_file_name_, std::ios::in);
31 if (!channel_map_file_.is_open()) {
34 "The channel mapping file cannot be opened.");
37 ldmx_log(fatal) <<
"The channel mapping file cannot be opened.";
41 while (!channel_map_file_.eof()) {
42 channel_map_file_ >> el_id >> ch_id;
50 channel_map_.insert(std::pair<int, int>(ch_id, el_id));
51 ldmx_log(debug) <<
"elID " << el_id <<
" chID " << ch_id;
53 if (el_id != n_channels_ - 1)
54 ldmx_log(fatal) <<
"The set number of channels " << n_channels_
55 <<
" seems not to match the number from the map (+1) :"
57 channel_map_file_.close();
63 ldmx_log(debug) <<
"QIEEncoder: produce() starts! Event number: "
64 <<
event.getEventHeader().getEventNumber();
66 std::vector<trigscint::QIEStream> qie_outs;
67 int n_samp = QIEStream::NUM_SAMPLES;
68 std::vector<int> init_vec(n_samp, 0);
69 ldmx_log(debug) <<
"num samples = " << n_samp;
73 for (
int i_q = 0; i_q < n_channels_; i_q++) {
80 qie_outs.push_back(qie_out);
83 ldmx_log(debug) <<
"Looking up input collection " << input_collection_ <<
"_"
86 input_collection_, input_pass_name_)};
87 ldmx_log(debug) <<
"Got input collection" << input_collection_ <<
"_"
90 bool is_ci_dunsync =
false;
92 bool is_ci_dskipped =
false;
93 bool is_cr_c0malformed =
false;
94 bool is_cr_c1malformed =
false;
97 ldmx_log(debug) <<
"entering loop over digis ";
98 for (
auto& digi : digis) {
99 int bar = digi.getChanID();
100 auto itr = channel_map_.find(bar);
101 if (itr == channel_map_.end()) {
102 ldmx_log(fatal) <<
"Couldn't find an entry for bar " << bar
103 <<
"; check the (choice of) channel map!. Exiting event "
104 <<
event.getEventHeader().getEventNumber();
107 int idx = itr->second;
109 qie_outs.at(idx).setChannelID(bar);
110 qie_outs.at(idx).setElectronicsID(idx);
111 ldmx_log(debug) <<
"Channel " << bar <<
" elec ID "
112 << qie_outs.at(idx).getElectronicsID();
113 std::vector<int> l_etdcs;
114 std::vector<uint8_t> cids;
115 for (
int i_s = 0; i_s < n_samp; i_s++) {
116 int tdc = digi.getTDC().at(i_s);
117 int cid = digi.getCID().at(i_s);
118 if (cids.size() > 0 && (cid % 4) != ((cids.back() + 1) % 4)) {
121 is_ci_dskipped =
true;
124 std::vector<uint8_t> adcs;
125 int adc = digi.getADC().at(i_s);
126 uint8_t mant = adc % 64;
127 uint8_t exp = adc / 64;
128 ldmx_log(debug) <<
"\tSample " << i_s << std::left << std::setw(6)
129 <<
" ADC " << adc <<
",\texp " << unsigned(exp)
130 <<
" mant " << unsigned(mant) <<
",\tTDC = " << tdc
131 <<
", LE TDC = " << std::bitset<8>(tdc / 16)
132 <<
" and capID= " << cid;
133 adcs.push_back(64 * exp + mant);
134 ldmx_log(debug) <<
"Combined ADC: " << std::showbase
135 << std::bitset<8>(adcs.back()) <<
" and original adc "
136 << std::bitset<8>(adc) << std::dec;
140 l_etdcs.push_back(tdc);
141 cids.push_back((uint8_t)cid);
143 if (first_cid == -1) {
146 first_cid = cids.back();
148 if (first_cid != cids.back()) {
152 qie_outs.at(idx).setADC(digi.getADC());
153 qie_outs.at(idx).setTDC(l_etdcs);
155 if (is_ci_dunsync) ldmx_log(debug) <<
"Found unsynced CIDs!";
156 if (is_ci_dskipped) ldmx_log(info) <<
"Found skipped CIDs!";
167 uint16_t trigger_id =
event.getEventHeader().getEventNumber();
168 uint8_t random_checksum =
172 flags |= (is_cr_c0malformed << QIEStream::CRC0_ERR_POS);
173 flags |= (is_cr_c1malformed << QIEStream::CRC1_ERR_POS);
174 flags |= (is_ci_dunsync << QIEStream::CID_UNSYNC_POS);
175 flags |= (is_ci_dskipped << QIEStream::CID_SKIP_POS);
176 ldmx_log(debug) <<
"FLAGS: " << std::bitset<8>(flags);
178 std::vector<uint8_t> out_word;
179 std::vector<uint8_t> trigger_i_dwords;
180 for (
int i_w = QIEStream::TRIGID_LEN_BYTES - 1; i_w >= 0; i_w--) {
182 uint8_t t_i_dword = trigger_id >> i_w * 8;
183 trigger_i_dwords.push_back(t_i_dword);
184 out_word.push_back(t_i_dword);
187 out_word.push_back(flags);
188 out_word.push_back(random_checksum);
191 std::cout <<
"header word ";
192 for (
auto word : out_word) std::cout << std::bitset<8>(word) <<
" ";
193 std::cout << std::endl;
198 for (
int i_s = 0; i_s < n_samp; i_s++) {
199 for (
int i_q = 0; i_q < n_channels_; i_q++) {
200 out_word.push_back(qie_outs.at(i_q).getADC().at(i_s));
202 for (
int i_q = 0; i_q < n_channels_; i_q++) {
203 out_word.push_back(qie_outs.at(i_q).getTDC().at(i_s));
209 std::cout <<
"total word ";
212 std::max(std::max(QIEStream::ERROR_POS, QIEStream::CHECKSUM_POS),
213 QIEStream::TRIGID_POS + (QIEStream::TRIGID_LEN_BYTES)) +
215 for (
auto word : out_word) {
216 if ((widx - i_wstart) % n_channels_ == 0) {
217 int sample = (widx - i_wstart) / n_channels_;
219 std::cout <<
"\n sample " << sample / 2 <<
" | ";
221 std::cout <<
"\n TDC: ";
223 std::cout << (unsigned)word <<
" ";
228 std::cout << std::endl;
231 event.add(output_collection_, out_word);