Skip to content

Commit

Permalink
Merge pull request #52 from DUNE-DAQ/mmatt15/crt
Browse files Browse the repository at this point in the history
ProtoDUNE CRT integration
  • Loading branch information
wesketchum authored Mar 27, 2024
2 parents afe8b28 + 2630985 commit 7e2c6b9
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 1 deletion.
145 changes: 145 additions & 0 deletions pybindsrc/CRTUnpacker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @file CRTUnpacker.cc Fast C++ -> numpy CRT (fixed size) format unpacker
*
* This is part of the DUNE DAQ , copyright 2020.
* Licensing/copyright details are in the COPYING file that you should have
* received with this code.
*/

#include "fddetdataformats/CRTFrame.hpp"
#include "daqdataformats/Fragment.hpp"

#include <cstdint>
#include <pybind11/numpy.h>

namespace py = pybind11;
namespace dunedaq::rawdatautils::crt {

/**
* @brief Gets number of CRTFrames in a fragment
*/
uint32_t get_n_frames(daqdataformats::Fragment const& frag){
return (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::CRTFrame);
}


/** \
* @brief Unpacks module numbers for CRTFrames into a numpy array with dimensions
* (nframes) \
*/
py::array_t<uint16_t> np_array_modules_data(void* data, int nframes){

py::array_t<uint16_t> modules(nframes);
auto ptr = static_cast<uint16_t*>(modules.request().ptr);

for (size_t i=0; i<(size_t)nframes; ++i) {
auto fr = reinterpret_cast<fddetdataformats::CRTFrame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::CRTFrame));
ptr[i] = fr->get_module();
}

return modules;
}

/** \
* @brief Unpacks module numbers for Fragment that contains CRTFrames into a numpy array with dimensions \
*/
py::array_t<uint16_t> np_array_modules(daqdataformats::Fragment& frag){
return np_array_modules_data(frag.get_data(), (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::CRTFrame));
}


/**
* @brief Unpacks data containing CRTFrames into a numpy array with the ADC
* values and dimension (number of CRTFrames, adcs_per_module (=64))
* Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
*/
py::array_t<int16_t> np_array_adc_data(void* data, int nframes){

const auto adcs_per_module = fddetdataformats::CRTFrame::s_num_adcs;

py::array_t<int16_t> ret(nframes * adcs_per_module);
auto ptr = static_cast<int16_t*>(ret.request().ptr);
for (size_t i=0; i<(size_t)nframes; ++i) {
auto fr = reinterpret_cast<fddetdataformats::CRTFrame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::CRTFrame));
for (size_t j=0; j<adcs_per_module; ++j) {
ptr[i*adcs_per_module + j] = fr->get_adc(j);
}
}
ret.resize({nframes, adcs_per_module});

return ret;
}

/**
* @brief Unpacks data containing CRTFrames into a numpy array with the channel
* values and dimension (number of CRTFrames, adcs_per_module (=64))
* Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
*/
py::array_t<uint8_t> np_array_channel_data(void* data, int nframes){

const auto adcs_per_module = fddetdataformats::CRTFrame::s_num_adcs;

py::array_t<uint8_t> ret(nframes * adcs_per_module);
auto ptr = static_cast<uint8_t*>(ret.request().ptr);
for (size_t i=0; i<(size_t)nframes; ++i) {
auto fr = reinterpret_cast<fddetdataformats::CRTFrame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::CRTFrame));
for (size_t j=0; j<adcs_per_module; ++j) {
ptr[i*adcs_per_module + j] = fr->get_channel(j);
}
}
ret.resize({nframes, adcs_per_module});

return ret;
}

/**
* @brief Unpacks data containing CRTFrames into a numpy array with the
* timestamps with dimension (number of CRTFrames)
* Warning: It doesn't check that nframes is a sensible value (can read out of bounds)
*/

py::array_t<uint64_t> np_array_timestamp_data(void* data, int nframes){

//const auto adcs_per_module = fddetdataformats::CRTFrame::s_num_adcs;

py::array_t<uint64_t> ret(nframes);
auto ptr = static_cast<uint64_t*>(ret.request().ptr);
for (size_t i=0; i<(size_t)nframes; ++i) {
auto fr = reinterpret_cast<fddetdataformats::CRTFrame*>(static_cast<char*>(data) + i * sizeof(fddetdataformats::CRTFrame));
ptr[i] = fr->get_timestamp();
}

return ret;
}


/**
* @brief Unpacks a Fragment containing CRTFrames into a numpy array with the
* ADC values and dimension (number of CRTFrames in the Fragment, 64)
*/
py::array_t<int16_t> np_array_adc(daqdataformats::Fragment& frag){
return np_array_adc_data(frag.get_data(), (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::CRTFrame));
}

/**
* @brief Unpacks a Fragment containing CRTFrames into a numpy array with the
* channel values and dimension (number of CRTFrames in the Fragment, 64)
*/
py::array_t<uint8_t> np_array_channel(daqdataformats::Fragment& frag){
return np_array_channel_data(frag.get_data(), (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::CRTFrame));
}


/**
* @brief Unpacks the timestamps in a Fragment containing WIBFrames into a numpy
* array with dimension (number of CRTFrames in the Fragment)
*/
py::array_t<uint64_t> np_array_timestamp(daqdataformats::Fragment& frag){
return np_array_timestamp_data(frag.get_data(), (frag.get_size() - sizeof(daqdataformats::FragmentHeader)) / sizeof(fddetdataformats::CRTFrame));
}

} // namespace dunedaq::rawdatautils::crt // NOLINT
25 changes: 24 additions & 1 deletion pybindsrc/unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "fddetdataformats/DAPHNEFrame.hpp"
#include "fddetdataformats/WIBEthFrame.hpp"
#include "fddetdataformats/TDE16Frame.hpp"
#include "fddetdataformats/CRTFrame.hpp"
#include "daqdataformats/Fragment.hpp"

#include <pybind11/numpy.h>
Expand Down Expand Up @@ -87,6 +88,18 @@ namespace tde {

}

namespace crt {
extern uint32_t get_n_frames(daqdataformats::Fragment const& frag);
extern py::array_t<int16_t> np_array_adc(daqdataformats::Fragment& frag);
extern py::array_t<int16_t> np_array_adc_data(void* data, int nframes);
extern py::array_t<uint8_t> np_array_channel(daqdataformats::Fragment& frag);
extern py::array_t<uint8_t> np_array_channel_data(void* data, int nframes);
extern py::array_t<uint64_t> np_array_timestamp(daqdataformats::Fragment& frag);
extern py::array_t<uint64_t> np_array_timestamp_data(void* data, int nframes);
extern py::array_t<uint16_t> np_array_modules(daqdataformats::Fragment& frag);
extern py::array_t<uint16_t> np_array_modules_data(void* data, int nframes);
}

namespace unpack {
namespace python {

Expand Down Expand Up @@ -136,7 +149,17 @@ register_unpack(py::module& m) {
tde_module.def("get_n_frames", &tde::get_n_frames);
tde_module.def("np_array_timestamp_data", &tde::np_array_timestamp_data);
tde_module.def("np_array_channel_data", &tde::np_array_channel_data);


py::module_ crt_module = m.def_submodule("crt");
crt_module.def("get_n_frames", &crt::get_n_frames);
crt_module.def("np_array_modules", &crt::np_array_modules);
crt_module.def("np_array_adc", &crt::np_array_adc);
crt_module.def("np_array_channel", &crt::np_array_channel);
crt_module.def("np_array_timestamp", &crt::np_array_timestamp);
crt_module.def("np_array_modules_data", &crt::np_array_modules_data);
crt_module.def("np_array_adc_data", &crt::np_array_adc_data);
crt_module.def("np_array_timestamp_data", &crt::np_array_timestamp_data);
crt_module.def("np_array_channel_data", &crt::np_array_channel_data);
}

} // namespace python
Expand Down
1 change: 1 addition & 0 deletions python/rawdatautils/unpack/crt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ..._daq_rawdatautils_py.unpack.crt import *
1 change: 1 addition & 0 deletions python/rawdatautils/unpack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rawdatautils.unpack.dataclasses import *
import rawdatautils.unpack.wibeth
import rawdatautils.unpack.daphne
import rawdatautils.unpack.crt
import h5py

#analysis imports
Expand Down
Loading

0 comments on commit 7e2c6b9

Please sign in to comment.