Line data Source code
1 : /**
2 : * @file WIB2Unpacker.cc Fast C++ -> numpy WIB2 format unpacker
3 : *
4 : * This is part of the DUNE DAQ , copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "fddetdataformats/WIB2Frame.hpp"
10 : #include "daqdataformats/Fragment.hpp"
11 :
12 : #include <cstdint>
13 : #include <pybind11/numpy.h>
14 :
15 : namespace py = pybind11;
16 : namespace dunedaq::rawdatautils::wib2 {
17 :
18 : /**
19 : * @brief Gets number of WIB2Frames in a fragment
20 : */
21 0 : uint32_t get_n_frames(daqdataformats::Fragment const& frag){
22 0 : return (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::WIB2Frame);
23 : }
24 :
25 : /**
26 : * @brief Unpacks data containing WIB2Frames into a numpy array with the ADC
27 : * values and dimension (number of WIB2Frames, 256)
28 : * Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
29 : */
30 0 : py::array_t<uint16_t> np_array_adc_data(void* data, int nframes){
31 0 : py::array_t<uint16_t> ret(256 * nframes);
32 0 : auto ptr = static_cast<uint16_t*>(ret.request().ptr);
33 0 : for (size_t i=0; i<(size_t)nframes; ++i) {
34 0 : auto fr = reinterpret_cast<fddetdataformats::WIB2Frame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::WIB2Frame));
35 0 : for (size_t j=0; j<256; ++j)
36 0 : ptr[256 * i + j] = fr->get_adc(j);
37 : }
38 0 : ret.resize({nframes, 256});
39 :
40 0 : return ret;
41 0 : }
42 :
43 : /**
44 : * @brief Unpacks data containing WIB2Frames into a numpy array with the
45 : * timestamps with dimension (number of WIB2Frames)
46 : * Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
47 : */
48 0 : py::array_t<uint64_t> np_array_timestamp_data(void* data, int nframes){
49 0 : py::array_t<uint64_t> ret(nframes);
50 0 : auto ptr = static_cast<uint64_t*>(ret.request().ptr);
51 0 : for (size_t i=0; i<(size_t)nframes; ++i) {
52 0 : auto fr = reinterpret_cast<fddetdataformats::WIB2Frame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::WIB2Frame));
53 0 : ptr[i] = fr->get_timestamp();
54 : }
55 :
56 0 : return ret;
57 0 : }
58 :
59 : /**
60 : * @brief Unpacks a Fragment containing WIB2Frames into a numpy array with the
61 : * ADC values and dimension (number of WIB2Frames in the Fragment, 256)
62 : */
63 0 : py::array_t<uint16_t> np_array_adc(daqdataformats::Fragment const& frag){
64 0 : return np_array_adc_data(frag.get_data(), get_n_frames(frag));
65 : }
66 :
67 : /**
68 : * @brief Unpacks the timestamps in a Fragment containing WIBFrames into a numpy
69 : * array with dimension (number of WIB2Frames in the Fragment)
70 : */
71 0 : py::array_t<uint64_t> np_array_timestamp(daqdataformats::Fragment const& frag){
72 0 : return np_array_timestamp_data(frag.get_data(), get_n_frames(frag));
73 : }
74 :
75 :
76 : } // namespace dunedaq::rawdatautils::wib2 // NOLINT
|