10 output_collection_ = ps.
get<std::string>(
"output_collection");
11 input_collection_ = ps.
get<std::string>(
"input_collection");
12 input_pass_name_ = ps.
get<std::string>(
"input_pass_name");
13 channel_map_file_name_ = ps.
get<std::string>(
"channel_map_file");
14 n_channels_ = ps.
get<
int>(
"number_channels");
15 verbose_ = ps.
get<
bool>(
"verbose");
17 ldmx_log(debug) <<
"In configure, got parameters:" <<
"\noutput_collection = "
19 <<
"\ninput_collection = " << input_collection_
20 <<
"\ninput_pass_name = " << input_pass_name_
21 <<
"\nchannel_map_file = " << channel_map_file_name_
22 <<
"\nnumber_channels = " << n_channels_
23 <<
"\nverbose = " << verbose_;
26 channel_map_file_.open(channel_map_file_name_, std::ios::in);
27 if (!channel_map_file_.is_open()) {
30 "The channel mapping file cannot be opened.");
33 ldmx_log(fatal) <<
"The channel mapping file cannot be opened.";
37 while (!channel_map_file_.eof()) {
38 channel_map_file_ >> el_id >> ch_id;
46 channel_map_.insert(std::pair<int, int>(ch_id, el_id));
47 ldmx_log(debug) <<
"elID " << el_id <<
" chID " << ch_id;
49 if (el_id != n_channels_ - 1)
50 ldmx_log(fatal) <<
"The set number of channels " << n_channels_
51 <<
" seems not to match the number from the map (+1) :"
53 channel_map_file_.close();
59 ldmx_log(debug) <<
"QIEEncoder: produce() starts! Event number: "
60 <<
event.getEventHeader().getEventNumber();
62 std::vector<trigscint::QIEStream> qie_outs;
63 int n_samp = QIEStream::NUM_SAMPLES;
64 std::vector<int> init_vec(n_samp, 0);
65 ldmx_log(debug) <<
"num samples = " << n_samp;
69 for (
int i_q = 0; i_q < n_channels_; i_q++) {
76 qie_outs.push_back(qie_out);
79 ldmx_log(debug) <<
"Looking up input collection " << input_collection_ <<
"_"
82 input_collection_, input_pass_name_)};
83 ldmx_log(debug) <<
"Got input collection" << input_collection_ <<
"_"
86 bool is_ci_dunsync =
false;
88 bool is_ci_dskipped =
false;
89 bool is_cr_c0malformed =
false;
90 bool is_cr_c1malformed =
false;
93 ldmx_log(debug) <<
"entering loop over digis ";
94 for (
auto &digi : digis) {
95 int bar = digi.getChanID();
96 auto itr = channel_map_.find(bar);
97 if (itr == channel_map_.end()) {
98 ldmx_log(fatal) <<
"Couldn't find an entry for bar " << bar
99 <<
"; check the (choice of) channel map!. Exiting event "
100 <<
event.getEventHeader().getEventNumber();
103 int idx = itr->second;
105 qie_outs.at(idx).setChannelID(bar);
106 qie_outs.at(idx).setElectronicsID(idx);
107 ldmx_log(debug) <<
"Channel " << bar <<
" elec ID "
108 << qie_outs.at(idx).getElectronicsID();
109 std::vector<int> l_etdcs;
110 std::vector<uint8_t> cids;
111 for (
int i_s = 0; i_s < n_samp; i_s++) {
112 int tdc = digi.getTDC().at(i_s);
113 int cid = digi.getCID().at(i_s);
114 if (cids.size() > 0 && (cid % 4) != ((cids.back() + 1) % 4)) {
117 is_ci_dskipped =
true;
120 std::vector<uint8_t> adcs;
121 int adc = digi.getADC().at(i_s);
122 uint8_t mant = adc % 64;
123 uint8_t exp = adc / 64;
124 ldmx_log(debug) <<
"\tSample " << i_s << std::left << std::setw(6)
125 <<
" ADC " << adc <<
",\texp " << unsigned(exp)
126 <<
" mant " << unsigned(mant) <<
",\tTDC = " << tdc
127 <<
", LE TDC = " << std::bitset<8>(tdc / 16)
128 <<
" and capID= " << cid;
129 adcs.push_back(64 * exp + mant);
130 ldmx_log(debug) <<
"Combined ADC: " << std::showbase
131 << std::bitset<8>(adcs.back()) <<
" and original adc "
132 << std::bitset<8>(adc) << std::dec;
136 l_etdcs.push_back(tdc);
137 cids.push_back((uint8_t)cid);
139 if (first_cid == -1) {
142 first_cid = cids.back();
144 if (first_cid != cids.back()) {
148 qie_outs.at(idx).setADC(digi.getADC());
149 qie_outs.at(idx).setTDC(l_etdcs);
151 if (is_ci_dunsync) ldmx_log(debug) <<
"Found unsynced CIDs!";
152 if (is_ci_dskipped) ldmx_log(info) <<
"Found skipped CIDs!";
163 uint16_t trigger_id =
event.getEventHeader().getEventNumber();
164 uint8_t random_checksum =
168 flags |= (is_cr_c0malformed << QIEStream::CRC0_ERR_POS);
169 flags |= (is_cr_c1malformed << QIEStream::CRC1_ERR_POS);
170 flags |= (is_ci_dunsync << QIEStream::CID_UNSYNC_POS);
171 flags |= (is_ci_dskipped << QIEStream::CID_SKIP_POS);
172 ldmx_log(debug) <<
"FLAGS: " << std::bitset<8>(flags);
174 std::vector<uint8_t> out_word;
175 std::vector<uint8_t> trigger_i_dwords;
176 for (
int i_w = QIEStream::TRIGID_LEN_BYTES - 1; i_w >= 0; i_w--) {
178 uint8_t t_i_dword = trigger_id >> i_w * 8;
179 trigger_i_dwords.push_back(t_i_dword);
180 out_word.push_back(t_i_dword);
183 out_word.push_back(flags);
184 out_word.push_back(random_checksum);
187 std::cout <<
"header word ";
188 for (
auto word : out_word) std::cout << std::bitset<8>(word) <<
" ";
189 std::cout << std::endl;
194 for (
int i_s = 0; i_s < n_samp; i_s++) {
195 for (
int i_q = 0; i_q < n_channels_; i_q++) {
196 out_word.push_back(qie_outs.at(i_q).getADC().at(i_s));
198 for (
int i_q = 0; i_q < n_channels_; i_q++) {
199 out_word.push_back(qie_outs.at(i_q).getTDC().at(i_s));
205 std::cout <<
"total word ";
208 std::max(std::max(QIEStream::ERROR_POS, QIEStream::CHECKSUM_POS),
209 QIEStream::TRIGID_POS + (QIEStream::TRIGID_LEN_BYTES)) +
211 for (
auto word : out_word) {
212 if ((widx - i_wstart) % n_channels_ == 0) {
213 int sample = (widx - i_wstart) / n_channels_;
215 std::cout <<
"\n sample " << sample / 2 <<
" | ";
217 std::cout <<
"\n TDC: ";
219 std::cout << (unsigned)word <<
" ";
224 std::cout << std::endl;
227 event.add(output_collection_, out_word);