diff --git a/include/aare/Cluster.hpp b/include/aare/Cluster.hpp index a4d2428..332a757 100755 --- a/include/aare/Cluster.hpp +++ b/include/aare/Cluster.hpp @@ -8,6 +8,7 @@ #pragma once +#include "defs.hpp" #include #include #include @@ -45,7 +46,7 @@ struct Cluster { * @return photon energy of subcluster, 2x2 subcluster index relative to * cluster center */ - std::pair max_sum_2x2() const { + Sum_index_pair max_sum_2x2() const { if constexpr (cluster_size_x == 3 && cluster_size_y == 3) { std::array sum_2x2_subclusters; @@ -56,9 +57,10 @@ struct Cluster { int index = std::max_element(sum_2x2_subclusters.begin(), sum_2x2_subclusters.end()) - sum_2x2_subclusters.begin(); - return std::make_pair(sum_2x2_subclusters[index], index); + return Sum_index_pair{sum_2x2_subclusters[index], index}; } else if constexpr (cluster_size_x == 2 && cluster_size_y == 2) { - return std::make_pair(data[0] + data[1] + data[2] + data[3], 0); + return Sum_index_pair{data[0] + data[1] + data[2] + data[3], + 0}; } else { constexpr size_t cluster_center_index = (ClusterSizeX / 2) + (ClusterSizeY / 2) * ClusterSizeX; @@ -97,7 +99,7 @@ struct Cluster { int index = std::max_element(sum_2x2_subcluster.begin(), sum_2x2_subcluster.end()) - sum_2x2_subcluster.begin(); - return std::make_pair(sum_2x2_subcluster[index], index); + return Sum_index_pair{sum_2x2_subcluster[index], index}; } } }; diff --git a/include/aare/ClusterVector.hpp b/include/aare/ClusterVector.hpp index 00f4d25..146ac43 100644 --- a/include/aare/ClusterVector.hpp +++ b/include/aare/ClusterVector.hpp @@ -86,15 +86,14 @@ class ClusterVector> { /** * @brief Sum the pixels in the 2x2 subcluster with the biggest pixel sum in * each cluster - * @return std::vector vector of sums for each cluster + * @return vector of sums index pairs for each cluster */ - std::vector sum_2x2() { - std::vector sums_2x2(m_data.size()); + std::vector> sum_2x2() { + std::vector> sums_2x2(m_data.size()); - std::transform(m_data.begin(), m_data.end(), sums_2x2.begin(), - [](const ClusterType &cluster) { - return cluster.max_sum_2x2().first; - }); + std::transform( + m_data.begin(), m_data.end(), sums_2x2.begin(), + [](const ClusterType &cluster) { return cluster.max_sum_2x2(); }); return sums_2x2; } diff --git a/include/aare/defs.hpp b/include/aare/defs.hpp index 7ae6269..1ddd1b0 100644 --- a/include/aare/defs.hpp +++ b/include/aare/defs.hpp @@ -331,6 +331,12 @@ enum DACIndex { SLOW_ADC_TEMP }; +// helper pair class to easily expose in python +template struct Sum_index_pair { + T1 sum; + T2 index; +}; + enum class TimingMode { Auto, Trigger }; enum class FrameDiscardPolicy { NoDiscard, Discard, DiscardPartial }; diff --git a/python/src/bind_Cluster.hpp b/python/src/bind_Cluster.hpp index b6f4272..b5d8c28 100644 --- a/python/src/bind_Cluster.hpp +++ b/python/src/bind_Cluster.hpp @@ -58,7 +58,11 @@ void define_Cluster(py::module &m, const std::string &typestr) { &Cluster::x) .def_readonly("y", - &Cluster::y); + &Cluster::y) + + .def( + "max_sum_2x2", + &Cluster::max_sum_2x2); } template #include +#define PYBIND11_DETAILED_ERROR_MESSAGES +#define PYBIND11_DISABLE_STL_CONTAINERS #include -#include -#include +// #include +// #include namespace py = pybind11; using pd_type = double; @@ -46,8 +49,31 @@ void define_ClusterVector(py::module &m, const std::string &typestr) { }) .def("sum_2x2", [](ClusterVector &self) { - auto *vec = new std::vector(self.sum_2x2()); - return return_vector(vec); + // auto *vec = + // new std::vector>(self.sum_2x2()); + + /* + py::capsule free_when_done(vec, [](void *f) { + std::vector> *foo = + reinterpret_cast> *>( + f); + delete foo; + }); + + py::buffer result( + vec->data(), + static_cast(sizeof(std::pair)), + fmt::format("T{{{}:sum:i:index}}", + py::format_descriptor::format()), + static_cast(1), + std::vector{ + static_cast(vec->size())}, + std::vector{static_cast( + sizeof(std::pair))}); + //,static_cast(free_when_done)); + */ + + return self.sum_2x2(); // return_vector(vec); }) .def_property_readonly("size", &ClusterVector::size) .def("item_size", &ClusterVector::item_size) diff --git a/python/src/module.cpp b/python/src/module.cpp index 5de2fef..07e4d4c 100644 --- a/python/src/module.cpp +++ b/python/src/module.cpp @@ -9,6 +9,7 @@ #include "bind_ClusterFinderMT.hpp" #include "bind_ClusterVector.hpp" #include "bind_calibration.hpp" +#include "utils/bind_Vector.hpp" // TODO! migrate the other names #include "ctb_raw_file.hpp" @@ -114,4 +115,19 @@ PYBIND11_MODULE(_aare, m) { reduce_to_3x3(m); reduce_to_3x3(m); reduce_to_3x3(m); + + define_Vector>( + m, "Sum_index_pair_d", + fmt::format("T{{{}:sum:i:index}}", + py::format_descriptor::format())); + + define_Vector>( + m, "Sum_index_pair_f", + fmt::format("T{{{}:sum:i:index}}", + py::format_descriptor::format())); + + define_Vector>( + m, "Sum_index_pair_i", + fmt::format("T{{{}:sum:i:index}}", + py::format_descriptor::format())); } diff --git a/python/src/utils/bind_Vector.hpp b/python/src/utils/bind_Vector.hpp new file mode 100644 index 0000000..bcbcd57 --- /dev/null +++ b/python/src/utils/bind_Vector.hpp @@ -0,0 +1,30 @@ +#pragma once +#include +// #include +// #include +#include + +namespace py = pybind11; + +// TODO add index, itemize +template +void define_Vector(py::module &m, const std::string &type_str, + const std::string &format_string) { + auto class_name = "Vector_" + type_str; + + py::class_>(m, class_name.c_str(), py::buffer_protocol()) + + .def(py::init()) + + .def_buffer([&format_string]( + std::vector &self) -> py::buffer_info { + return py::buffer_info( + self.data(), static_cast(sizeof(Type)), + format_string, static_cast(1), + std::vector{static_cast(self.size())}, + std::vector{ + static_cast(sizeof(Type))}); + }); +} + +// fmt::format("T{{{}:sum:i:index}}",py::format_descriptor::format()) \ No newline at end of file diff --git a/python/tests/test_ClusterVector.py b/python/tests/test_ClusterVector.py index fa98e3f..489ce76 100644 --- a/python/tests/test_ClusterVector.py +++ b/python/tests/test_ClusterVector.py @@ -32,6 +32,17 @@ def test_push_back_on_cluster_vector(): assert arr[0]['y'] == 22 +def test_max_2x2_sum(): + """max_2x2_sum""" + cv = _aare.ClusterVector_Cluster3x3i() + cv.push_back(_aare.Cluster3x3i(19, 22, np.array([0,1,0,2,3,0,2,1,0], dtype=np.int32))) + cv.push_back(_aare.Cluster3x3i(19, 22, np.ones(9, dtype=np.int32))) + assert cv.size == 2 + max_2x2 = cv.sum_2x2() + assert max_2x2.size == 2 + print(max_2x2[0]) + + def test_make_a_hitmap_from_cluster_vector(): cv = _aare.ClusterVector_Cluster3x3i()