Line data Source code
1 : /**
2 : * @file WIBUnpacker.cc Fast C++ -> numpy WIB format decoder
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/WIBFrame.hpp"
10 : #include "daqdataformats/Fragment.hpp"
11 :
12 : #include <cstdint>
13 : #include <pybind11/numpy.h>
14 :
15 : namespace py = pybind11;
16 : namespace dunedaq::rawdatautils::wib {
17 :
18 : /**
19 : * @brief Unpacks data containing WIBFrames into a numpy array with the ADC
20 : * values and dimension (number of WIBFrames, 256)
21 : * Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
22 : */
23 0 : py::array_t<uint16_t> np_array_adc_data(void* data, int nframes){
24 0 : py::array_t<uint16_t> ret(256 * nframes);
25 0 : auto ptr = static_cast<uint16_t*>(ret.request().ptr);
26 0 : for (size_t i=0; i<(size_t)nframes; ++i) {
27 0 : auto fr = reinterpret_cast<fddetdataformats::WIBFrame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::WIBFrame));
28 0 : for (size_t j=0; j<256; ++j)
29 0 : ptr[256 * i + j] = fr->get_channel(j);
30 : }
31 0 : ret.resize({nframes, 256});
32 :
33 0 : return ret;
34 0 : }
35 :
36 : /**
37 : * @brief Unpacks data containing WIBFrames into a numpy array with the
38 : * timestamps with dimension (number of WIBFrames)
39 : * Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
40 : */
41 0 : py::array_t<uint64_t> np_array_timestamp_data(void* data, int nframes){
42 0 : py::array_t<uint64_t> ret(nframes);
43 0 : auto ptr = static_cast<uint64_t*>(ret.request().ptr);
44 0 : for (size_t i=0; i<(size_t)nframes; ++i) {
45 0 : auto fr = reinterpret_cast<fddetdataformats::WIBFrame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::WIBFrame));
46 0 : ptr[i] = fr->get_timestamp();
47 : }
48 :
49 0 : return ret;
50 0 : }
51 :
52 : /**
53 : * @brief Unpacks a Fragment containing WIBFrames into a numpy array with the
54 : * ADC values and dimension (number of WIBFrames in the Fragment, 256)
55 : */
56 0 : py::array_t<uint16_t> np_array_adc(daqdataformats::Fragment& frag){
57 0 : return np_array_adc_data(frag.get_data(), (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::WIBFrame));
58 : }
59 :
60 : /**
61 : * @brief Unpacks the timestamps in a Fragment containing WIBFrames into a numpy
62 : * array with dimension (number of WIBFrames in the Fragment)
63 : */
64 0 : py::array_t<uint64_t> np_array_timestamp(daqdataformats::Fragment& frag){
65 0 : return np_array_timestamp_data(frag.get_data(), (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::WIBFrame));
66 : }
67 :
68 :
69 : } // namespace dunedaq::rawdatautils::wib // NOLINT
|