@@ -117,16 +117,6 @@ std::vector<sbnd::crt::FEBData> CRTDecoder::FragToFEB(const artdaq::Fragment &fr
117117
118118 for (unsigned i = 0 ; i < bern_frag_meta->hits_in_fragment (); ++i)
119119 {
120- const sbndaq::BernCRTHitV2 *bern_hit = bern_frag.eventdata (i);
121-
122- std::array<uint16_t , 32 > adc_array;
123- unsigned ii = 0 ;
124- for (auto const &adc : bern_hit->adc )
125- {
126- adc_array[ii] = adc;
127- ++ii;
128- }
129-
130120 SBND ::CRTChannelMapService::ModuleInfo_t module = fCRTChannelMapService ->GetModuleInfoFromFEBMAC5 (bern_frag_meta->MAC5 ());
131121
132122 if (!module .valid )
@@ -142,13 +132,40 @@ std::vector<sbnd::crt::FEBData> CRTDecoder::FragToFEB(const artdaq::Fragment &fr
142132 continue ;
143133 }
144134
135+ const sbndaq::BernCRTHitV2 *bern_hit = bern_frag.eventdata (i);
136+ // Fill ADC Array. If channel order is swapped in the GDML
137+ // compared to reality then we fill the array in reverse.
138+ std::array<uint16_t , 32 > adc_array;
139+ unsigned ii = module .channel_order_swapped ? 31 : 0 ;
140+ for (auto const &adc : bern_hit->adc )
141+ {
142+ adc_array[ii] = adc;
143+
144+ if (module .channel_order_swapped )
145+ --ii;
146+ else
147+ ++ii;
148+ }
149+
150+ // Timestamp field stores the T0 time corrected by the cable length
151+ // stored in the original fcl. We use it to get the cable delay for
152+ // the module and store it in the unused coinc slot.
153+ const int64_t whole_second_ns = static_cast <int64_t >(1e9 );
154+ int cable_length = bern_hit->timestamp % whole_second_ns - bern_hit->ts0 ;
155+
156+ if (cable_length < 0 )
157+ cable_length += whole_second_ns;
158+
159+ if (cable_length > 1000 || cable_length < 0 )
160+ throw std::runtime_error (" Why is the cable length: " + std::to_string (cable_length) + " ?" );
161+
145162 feb_datas.emplace_back (module .offline_module_id ,
146163 bern_hit->flags ,
147164 bern_hit->ts0 ,
148165 bern_hit->ts1 ,
149166 bern_hit->timestamp / 1e9 ,
150167 adc_array,
151- bern_hit-> coinc );
168+ cable_length );
152169
153170 std::string adc_string = " " ;
154171
0 commit comments