@@ -133,17 +133,19 @@ class sbndaq::SBNDXARAPUCADecoder : public art::EDProducer {
133133 bool fverbose; /* *< If `true` it increases verbosity of console output for detailed processing steps. */
134134
135135 // Class methods.
136- void decode_fragment (uint64_t timestamp, std::vector<size_t > & fragment_indices, const artdaq::Fragment& fragment, std::vector <raw::OpDetWaveform>& prod_wvfms);
136+ void decode_fragment (uint64_t timestamp, uint64_t & nominal_frag_timestamp, int32_t & nominal_TTT, std::vector<size_t > & fragment_indices, const artdaq::Fragment& fragment, std::vector <raw::OpDetWaveform>& prod_wvfms, std::vector<std::vector< uint16_t >>& wvfms );
137137
138138 bool get_ptb_hlt_timestamp (art::Event& e, uint64_t corr_raw_timestamp, uint64_t & timestamp, uint16_t & hlt_code);
139139 bool get_spec_tdc_etrig_timestamp (art::Event& e, uint64_t corr_raw_timestamp, uint64_t & timestamp);
140- void shift_time (const artdaq::Fragment& fragment, uint64_t TTT_ticks, int64_t TTT_end_ns, uint64_t timestamp, uint32_t num_samples_per_wvfm, double & ini_wvfm_timestamp, double & end_wvfm_timestamp);
140+ void shift_time (uint64_t TTT_ticks, int64_t TTT_end_ns, uint64_t frag_timestamp , uint64_t timestamp, uint32_t num_samples_per_wvfm, double & ini_wvfm_timestamp, double & end_wvfm_timestamp);
141141 void decode_waveforms (const artdaq::Fragment& fragment, std::vector<std::vector<uint16_t >>& wvfms, size_t header_size, uint32_t num_channels, uint32_t num_samples_per_wvfm, uint32_t num_words_per_wvfms, uint32_t num_samples_per_group);
142142
143- void dump_waveforms (std::vector <raw::OpDetWaveform> & prod_wvfms, const std::vector<std::vector<uint16_t >>& wvfms, std::vector<size_t > & fragment_indices, size_t board_index, uint32_t num_channels, double ini_wvfm_timestamp, double end_wvfm_timestamp);
143+ void dump_waveforms (std::vector <raw::OpDetWaveform> & prod_wvfms, std::vector<std::vector<uint16_t >>& wvfms, std::vector<size_t > & fragment_indices, size_t board_index, uint32_t num_channels, double ini_wvfm_timestamp, double end_wvfm_timestamp);
144144 void save_prod_wvfm (size_t board_idx, size_t ch, double ini_wvfm_timestamp, const std::vector <std::vector <uint16_t > > & wvfms, std::vector <raw::OpDetWaveform> & prod_wvfms);
145145 void save_debug_wvfm (size_t board_idx, size_t fragment_idx, int ch, double ini_wvfm_timestamp, double end_wvfm_timestamp, const std::vector <std::vector <uint16_t > > & wvfms);
146146
147+ void combine_waveforms (std::vector<std::vector<uint16_t >>& wvfms, const std::vector<std::vector<uint16_t >>& fragment_wvfms, uint32_t num_channels);
148+
147149 uint16_t get_sample (uint64_t buffer, uint32_t msb, uint32_t lsb);
148150 uint32_t read_word (const uint32_t * & data_ptr);
149151 unsigned int get_channel_id (unsigned int board, unsigned int board_channel);
@@ -304,6 +306,10 @@ void sbndaq::SBNDXARAPUCADecoder::produce(art::Event& e)
304306 bool found_caen = false ;
305307
306308 std::vector<size_t > fragment_indices (fnum_caen_boards, 0 );
309+ std::vector<std::vector <uint16_t >> wvfms;
310+ std::cout << " wvfms_size: " << wvfms.size () << std::endl;
311+ int32_t nominal_TTT = TTT_DEFAULT;
312+ uint64_t nominal_frag_timestamp = TTT_DEFAULT;
307313
308314 if (fverbose | fdebug_fragments_handle) std::cout << " \n > SBNDXARAPUCADecoder::produce: searching for V1740 fragments..." << std::endl;
309315
@@ -341,7 +347,7 @@ void sbndaq::SBNDXARAPUCADecoder::produce(art::Event& e)
341347
342348 for (size_t f = 0 ; f < num_caen_fragments; f++) {
343349 const artdaq::Fragment fragment = *container_fragment[f].get ();
344- decode_fragment (timestamp, fragment_indices, fragment, *prod_wvfms);
350+ decode_fragment (timestamp, nominal_frag_timestamp, nominal_TTT, fragment_indices, fragment, *prod_wvfms, wvfms );
345351 } // End CAEN V1740 fragments loop.
346352 }
347353 } // End Container fragments loop.
@@ -353,7 +359,7 @@ void sbndaq::SBNDXARAPUCADecoder::produce(art::Event& e)
353359 // It searches for all CAEN V1740 fragments.
354360 for (size_t f = 0 ; f < frag_handle_size; f++) {
355361 const artdaq::Fragment fragment = fragment_handle->at (f);
356- decode_fragment (timestamp, fragment_indices, fragment, *prod_wvfms);
362+ decode_fragment (timestamp, nominal_frag_timestamp, nominal_TTT, fragment_indices, fragment, *prod_wvfms, wvfms );
357363 } // End CAEN V1740 fragments loop.
358364 }
359365 } // End extracting CAEN V1740 fragments.
@@ -599,7 +605,10 @@ bool sbndaq::SBNDXARAPUCADecoder::get_ptb_hlt_timestamp(art::Event& e, uint64_t
599605 * - Populates the output vector (`prod_wvfms`) with decoded waveforms and optionally generates debug waveforms output.
600606 *
601607 */
602- void sbndaq::SBNDXARAPUCADecoder::decode_fragment (uint64_t timestamp, std::vector<size_t > & fragment_indices, const artdaq::Fragment& fragment, std::vector <raw::OpDetWaveform>& prod_wvfms) {
608+
609+ void sbndaq::SBNDXARAPUCADecoder::decode_fragment (uint64_t timestamp, uint64_t & nominal_frag_timestamp, int32_t & nominal_TTT, std::vector<size_t > & fragment_indices, const artdaq::Fragment& fragment, std::vector <raw::OpDetWaveform>& prod_wvfms, std::vector<std::vector<uint16_t >>& wvfms) {
610+ std::cout << " decode_wvfms_size: " << wvfms.size () << std::endl;
611+
603612 auto fragment_id = fragment.fragmentID () - ffragment_id_offset;
604613 auto it = std::find (fboard_id_list.begin (), fboard_id_list.end (), fragment_id);
605614 size_t board_idx;
@@ -619,10 +628,13 @@ void sbndaq::SBNDXARAPUCADecoder::decode_fragment(uint64_t timestamp, std::vecto
619628 if (valid_fragment) {
620629 if (fverbose) std::cout << " \n > SBNDXARAPUCADecoder::decode_fragment: decoding V1740 CAEN fragment " << fragment_indices[board_idx] << " from the board " << board_idx << " (slot " << fboard_id_list[board_idx] << " ):" << std::endl;
621630
622- // bool is_nominal_length = false;
631+ bool is_nominal_length = false ;
623632 // bool is_within_nominal_length = false;
624- // bool is_first = false;
625- // int32_t nominal_TTT = TTT_DEFAULT;
633+ bool is_first = false ;
634+
635+
636+ double ini_wvfm_timestamp = 0 ;
637+ double end_wvfm_timestamp = 0 ;
626638
627639 // =============== Accesses Event metadata and Event header for this fragment =============== //
628640
@@ -646,14 +658,15 @@ void sbndaq::SBNDXARAPUCADecoder::decode_fragment(uint64_t timestamp, std::vecto
646658 uint32_t num_remaining_bits = num_bits_per_all_wvfms % BITS_PER_SAMPLE;
647659 uint32_t num_samples_per_wvfm = num_samples_per_all_wvfms / num_channels;
648660 uint32_t num_samples_per_group = num_samples_per_wvfm * NUM_CHANNELS_PER_GROUP;
661+ uint32_t num_nominal_samples_per_wvfm = metadata->nSamples ;
649662
650663 if (fverbose | fdebug_waveforms) {
651- if (metadata-> nSamples == num_samples_per_wvfm) {
664+ if (num_nominal_samples_per_wvfm == num_samples_per_wvfm) {
652665 std::cout << " > SBNDXARAPUCADecoder::decode_fragment: [NOMINAL FRAGMENT] " << num_samples_per_wvfm << " samples/waveform." << " (" << num_samples_per_group << " samples per group - 8 channels per group -)." << std::endl;
653666 } else {
654667 std::cout << " > SBNDXARAPUCADecoder::decode_fragment: [EXTENDED FRAGMENT] " << num_samples_per_wvfm << " samples/waveform." << " (" << num_samples_per_group << " samples per group - 8 channels per group -)." << std::endl;
655668 }
656- std::cout << " > SBNDXARAPUCADecoder::decode_fragment: nominal number of samples per waveform: " << metadata-> nSamples << " ." << std::endl;
669+ std::cout << " > SBNDXARAPUCADecoder::decode_fragment: nominal number of samples per waveform: " << num_nominal_samples_per_wvfm << " ." << std::endl;
657670 std::cout << " > SBNDXARAPUCADecoder::decode_fragment: number of words for this fragment: " << num_words_per_event << " (Header: " << num_words_per_header << " , Waveform: " << num_words_per_wvfms << " ) words." << std::endl;
658671 }
659672
@@ -665,10 +678,11 @@ void sbndaq::SBNDXARAPUCADecoder::decode_fragment(uint64_t timestamp, std::vecto
665678 std::cout << " \t Number of samples per group (this fragment): " << num_samples_per_group << std::endl;
666679 }
667680
668- std::vector <std::vector <uint16_t > > wvfms (num_channels, std::vector<uint16_t >(num_samples_per_wvfm, 0 ));
681+ // std::vector <std::vector <uint16_t> > wvfms(num_channels, std::vector<uint16_t>(num_samples_per_wvfm, 0));
669682
670683 // =============== Extracts timing information for this fragment =============== //
671684
685+ uint64_t frag_timestamp = fragment.timestamp (); // ns.
672686 uint32_t TTT_ticks = header.triggerTime ();
673687 int64_t TTT_end_ns = TTT_ticks * NANOSEC_PER_TICK; // ns.
674688
@@ -677,41 +691,38 @@ void sbndaq::SBNDXARAPUCADecoder::decode_fragment(uint64_t timestamp, std::vecto
677691 std::cout << " \t\t TTT header.triggerTimeRollOver(): " << header.triggerTimeRollOver () << std::endl;
678692 }
679693
680- // =============== Start decoding the waveforms =============== //
681- // std::vector <std::vector <uint16_t> > fragment_wvfms(num_channels, std::vector<uint16_t>(num_samples_per_wvfm, 0));
682- decode_waveforms (fragment, wvfms, header_size, num_channels, num_samples_per_wvfm, num_words_per_wvfms, num_samples_per_group);
683-
684- // =============== Shifts timing to the selected timing reference frame =============== //
685- double ini_wvfm_timestamp = 0 ;
686- double end_wvfm_timestamp = 0 ;
687- shift_time (fragment, TTT_ticks, TTT_end_ns, timestamp, num_samples_per_wvfm, ini_wvfm_timestamp, end_wvfm_timestamp);
688-
689- dump_waveforms (prod_wvfms, wvfms, fragment_indices, board_idx, num_channels, ini_wvfm_timestamp, end_wvfm_timestamp);
690- // // The decoded waveforms are dumped into two products:
691- // // - A xarapucadecoder-art.root file with the OpDetWaveforms as the product of this producer for further analysis.
692- // // - A decoder_hist.root file gathering a waveform histograms.
693- // if (fverbose) std::cout << " > SBNDXARAPUCADecoder::decode_fragment: binary decoding complete, dumping products..." << std::endl;
694- //
695- // uint32_t num_debug_wvfms;
696- //
697- // if (fstore_debug_waveforms == -1) {
698- // num_debug_wvfms = num_channels;
699- // } else {
700- // num_debug_wvfms = std::min<size_t>(num_channels, fstore_debug_waveforms);
701- // }
702- //
703- // uint32_t ch;
694+ // // =============== Start decoding the waveforms =============== //
695+ std::vector <std::vector <uint16_t > > fragment_wvfms (num_channels, std::vector<uint16_t >(num_samples_per_wvfm, 0 ));
696+ decode_waveforms (fragment, fragment_wvfms, header_size, num_channels, num_samples_per_wvfm, num_words_per_wvfms, num_samples_per_group);
704697//
705- // for (ch = 0; ch < num_debug_wvfms; ch++) {
706- // save_prod_wvfm(board_idx, ch, ini_wvfm_timestamp, wvfms, prod_wvfms);
707- // save_debug_wvfm(board_idx, fragment_indices[board_idx], ch, ini_wvfm_timestamp, end_wvfm_timestamp, wvfms);
708- // }
698+ // // =============== Shifts timing to the selected timing reference frame =============== //
709699//
710- // for (;ch < num_channels; ch++) {
711- // save_prod_wvfm(board_idx, ch, ini_wvfm_timestamp, wvfms, prod_wvfms);
712- // }
713- //
714- // fragment_indices[board_idx]++;
700+ // shift_time(fragment, TTT_ticks, TTT_end_ns, timestamp, num_samples_per_wvfm, ini_wvfm_timestamp, end_wvfm_timestamp);
701+ // dump_waveforms(prod_wvfms, wvfms, fragment_indices, board_idx, num_channels, ini_wvfm_timestamp, end_wvfm_timestamp);
702+ // combine_waveforms(wvfms, fragment_wvfms, num_channels);
703+
704+ is_nominal_length = (num_nominal_samples_per_wvfm == num_samples_per_wvfm);
705+ is_first = (fragment_indices[board_idx] == 0 );
706+
707+ if (is_nominal_length) {
708+ if (!is_first) {
709+ std::cout << " NOT FIRST NOMINAL fragment " << std::endl;
710+ std::cout << " nominal_TTT: " << nominal_TTT << " TTT_end_ns: " << TTT_end_ns << std::endl;
711+ std::cout << " nominal_frag_timestamp: " << nominal_frag_timestamp << " frag_timestamp : " << frag_timestamp << std::endl;
712+ shift_time (TTT_ticks, nominal_TTT, nominal_frag_timestamp, timestamp, num_nominal_samples_per_wvfm, ini_wvfm_timestamp, end_wvfm_timestamp);
713+ dump_waveforms (prod_wvfms, wvfms, fragment_indices, board_idx, num_channels, ini_wvfm_timestamp, end_wvfm_timestamp);
714+ } else {
715+ std::cout << " FIRST NOMINAL fragment " << std::endl;
716+ }
717+ combine_waveforms (wvfms, fragment_wvfms, num_channels);
718+ nominal_TTT = TTT_end_ns;
719+ nominal_frag_timestamp = frag_timestamp;
720+ } else {
721+ std::cout << " EXTENDED fragment " << std::endl;
722+ combine_waveforms (wvfms, fragment_wvfms, num_channels);
723+ }
724+
725+ fragment_indices[board_idx]++;
715726 }
716727}
717728
@@ -793,10 +804,9 @@ void sbndaq::SBNDXARAPUCADecoder::save_debug_wvfm(size_t board_idx, size_t fragm
793804
794805}
795806
796- void sbndaq::SBNDXARAPUCADecoder::shift_time (const artdaq::Fragment& fragment, uint64_t TTT_ticks, int64_t TTT_end_ns, uint64_t timestamp, uint32_t num_samples_per_wvfm, double & ini_wvfm_timestamp, double & end_wvfm_timestamp) {
807+ void sbndaq::SBNDXARAPUCADecoder::shift_time (uint64_t TTT_ticks, int64_t TTT_end_ns, uint64_t frag_timestamp , uint64_t timestamp, uint32_t num_samples_per_wvfm, double & ini_wvfm_timestamp, double & end_wvfm_timestamp) {
797808
798809 int64_t pulse_duration_ns = num_samples_per_wvfm * fns_per_sample; // ns.
799- uint64_t frag_timestamp = fragment.timestamp (); // ns.
800810 int64_t frag_timestamp_s = frag_timestamp / NANOSEC_IN_SEC; // s.
801811 int64_t frag_timestamp_ns = frag_timestamp % NANOSEC_IN_SEC; // ns.
802812
@@ -901,7 +911,7 @@ void sbndaq::SBNDXARAPUCADecoder::decode_waveforms(const artdaq::Fragment& fragm
901911 }
902912}
903913
904- void sbndaq::SBNDXARAPUCADecoder::dump_waveforms (std::vector <raw::OpDetWaveform> & prod_wvfms, const std::vector<std::vector<uint16_t >>& wvfms, std::vector<size_t > & fragment_indices, size_t board_idx, uint32_t num_channels, double ini_wvfm_timestamp, double end_wvfm_timestamp) {
914+ void sbndaq::SBNDXARAPUCADecoder::dump_waveforms (std::vector <raw::OpDetWaveform> & prod_wvfms, std::vector<std::vector<uint16_t >>& wvfms, std::vector<size_t > & fragment_indices, size_t board_idx, uint32_t num_channels, double ini_wvfm_timestamp, double end_wvfm_timestamp) {
905915
906916 // The decoded waveforms are dumped into two products:
907917 // - A xarapucadecoder-art.root file with the OpDetWaveforms as the product of this producer for further analysis.
@@ -927,7 +937,19 @@ void sbndaq::SBNDXARAPUCADecoder::dump_waveforms(std::vector <raw::OpDetWaveform
927937 save_prod_wvfm (board_idx, ch, ini_wvfm_timestamp, wvfms, prod_wvfms);
928938 }
929939
930- fragment_indices[board_idx]++;
940+ wvfms.clear ();
941+ }
942+
943+ void sbndaq::SBNDXARAPUCADecoder::combine_waveforms (std::vector<std::vector<uint16_t >>& wvfms, const std::vector<std::vector<uint16_t >>& fragment_wvfms, uint32_t num_channels) {
944+ if (wvfms.empty ()) {
945+ std::cout << " Empty waveforms, resizing to " << num_channels << " channels." << std::endl;
946+ wvfms.resize (num_channels);
947+ std::cout << " BEF COMB - wvfms_size = " << wvfms.size () << " x " << wvfms[0 ].size () << std::endl;
948+ }
949+ for (uint32_t ch = 0 ; ch < num_channels; ch++) {
950+ wvfms[ch].insert (wvfms[ch].end (), fragment_wvfms[ch].begin (), fragment_wvfms[ch].end ());
951+ }
952+ std::cout << " AFT COMB - wvfms_size = " << wvfms.size () << " x " << wvfms[0 ].size () << std::endl;
931953}
932954
933955/* *
0 commit comments