index now returns enum type

This commit is contained in:
2025-10-23 17:34:54 +02:00
parent 790dd63ba3
commit 01fa61cf47
9 changed files with 44 additions and 55 deletions

View File

@@ -3,16 +3,10 @@
#include "aare/Cluster.hpp"
#include "aare/ClusterVector.hpp"
#include "aare/NDArray.hpp"
#include "aare/defs.hpp"
namespace aare {
enum class corner : int {
cTopLeft = 0,
cTopRight = 1,
cBottomLeft = 2,
cBottomRight = 3
};
enum class pixel : int {
pBottomLeft = 0,
pBottom = 1,
@@ -28,7 +22,7 @@ enum class pixel : int {
template <typename T> struct Eta2 {
double x;
double y;
int c{0};
corner c{0};
T sum;
};
@@ -66,11 +60,11 @@ calculate_eta2(const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &cl) {
(ClusterSizeX / 2) + (ClusterSizeY / 2) * ClusterSizeX;
auto max_sum = cl.max_sum_2x2();
eta.sum = max_sum.first;
int c = max_sum.second;
eta.sum = max_sum.sum;
corner c = max_sum.index;
// subcluster top right from center
switch (static_cast<corner>(c)) {
switch (c) {
case (corner::cTopLeft):
if ((cl.data[cluster_center_index - 1] +
cl.data[cluster_center_index]) != 0)

View File

@@ -46,7 +46,7 @@ struct Cluster {
* @return photon energy of subcluster, 2x2 subcluster index relative to
* cluster center
*/
Sum_index_pair<T, int> max_sum_2x2() const {
Sum_index_pair<T, corner> max_sum_2x2() const {
if constexpr (cluster_size_x == 3 && cluster_size_y == 3) {
std::array<T, 4> sum_2x2_subclusters;
@@ -57,10 +57,11 @@ struct Cluster {
int index = std::max_element(sum_2x2_subclusters.begin(),
sum_2x2_subclusters.end()) -
sum_2x2_subclusters.begin();
return Sum_index_pair<T, int>{sum_2x2_subclusters[index], index};
return Sum_index_pair<T, corner>{sum_2x2_subclusters[index],
corner{index}};
} else if constexpr (cluster_size_x == 2 && cluster_size_y == 2) {
return Sum_index_pair<T, int>{data[0] + data[1] + data[2] + data[3],
0};
return Sum_index_pair<T, corner>{
data[0] + data[1] + data[2] + data[3], corner{0}};
} else {
constexpr size_t cluster_center_index =
(ClusterSizeX / 2) + (ClusterSizeY / 2) * ClusterSizeX;
@@ -99,7 +100,8 @@ struct Cluster {
int index = std::max_element(sum_2x2_subcluster.begin(),
sum_2x2_subcluster.end()) -
sum_2x2_subcluster.begin();
return Sum_index_pair<T, int>{sum_2x2_subcluster[index], index};
return Sum_index_pair<T, corner>{sum_2x2_subcluster[index],
corner{index}};
}
}
};
@@ -127,8 +129,8 @@ reduce_to_2x2(const Cluster<T, ClusterSizeX, ClusterSizeY, CoordType> &c) {
(ClusterSizeX / 2) + (ClusterSizeY / 2) * ClusterSizeX;
int16_t index_bottom_left_max_2x2_subcluster =
(int(index / (ClusterSizeX - 1))) * ClusterSizeX +
index % (ClusterSizeX - 1);
(int(static_cast<int>(index) / (ClusterSizeX - 1))) * ClusterSizeX +
static_cast<int>(index) % (ClusterSizeX - 1);
result.x =
c.x + (index_bottom_left_max_2x2_subcluster - cluster_center_index) %
@@ -151,22 +153,22 @@ Cluster<T, 2, 2, int16_t> reduce_to_2x2(const Cluster<T, 3, 3, int16_t> &c) {
auto [s, i] = c.max_sum_2x2();
switch (i) {
case 0:
case corner::cTopLeft:
result.x = c.x - 1;
result.y = c.y + 1;
result.data = {c.data[0], c.data[1], c.data[3], c.data[4]};
break;
case 1:
case corner::cTopRight:
result.x = c.x;
result.y = c.y + 1;
result.data = {c.data[1], c.data[2], c.data[4], c.data[5]};
break;
case 2:
case corner::cBottomLeft:
result.x = c.x - 1;
result.y = c.y;
result.data = {c.data[3], c.data[4], c.data[6], c.data[7]};
break;
case 3:
case corner::cBottomRight:
result.x = c.x;
result.y = c.y;
result.data = {c.data[4], c.data[5], c.data[7], c.data[8]};

View File

@@ -5,6 +5,7 @@
#include "aare/ClusterFinderMT.hpp"
#include "aare/ClusterVector.hpp"
#include "aare/ProducerConsumerQueue.hpp"
#include "aare/defs.hpp"
namespace aare {

View File

@@ -438,8 +438,8 @@ bool ClusterFile<ClusterType, Enable>::is_selected(ClusterType &cl) {
if (m_noise_map) {
auto sum_1x1 = cl.data[cluster_center_index]; // central pixel
auto sum_2x2 = cl.max_sum_2x2().first; // highest sum of 2x2 subclusters
auto total_sum = cl.sum(); // sum of all pixels
auto sum_2x2 = cl.max_sum_2x2().sum; // highest sum of 2x2 subclusters
auto total_sum = cl.sum(); // sum of all pixels
auto noise =
(*m_noise_map)(cl.y, cl.x); // TODO! check if this is correct

View File

@@ -88,8 +88,8 @@ class ClusterVector<Cluster<T, ClusterSizeX, ClusterSizeY, CoordType>> {
* each cluster
* @return vector of sums index pairs for each cluster
*/
std::vector<Sum_index_pair<T, int>> sum_2x2() {
std::vector<Sum_index_pair<T, int>> sums_2x2(m_data.size());
std::vector<Sum_index_pair<T, corner>> sum_2x2() {
std::vector<Sum_index_pair<T, corner>> sums_2x2(m_data.size());
std::transform(
m_data.begin(), m_data.end(), sums_2x2.begin(),

View File

@@ -337,6 +337,13 @@ template <typename T1, typename T2> struct Sum_index_pair {
T2 index;
};
enum class corner : int {
cTopLeft = 0,
cTopRight = 1,
cBottomLeft = 2,
cBottomRight = 3
};
enum class TimingMode { Auto, Trigger };
enum class FrameDiscardPolicy { NoDiscard, Discard, DiscardPartial };

View File

@@ -10,11 +10,9 @@
#include <cstdint>
#include <filesystem>
#define PYBIND11_DETAILED_ERROR_MESSAGES
#define PYBIND11_DISABLE_STL_CONTAINERS
#include <pybind11/pybind11.h>
// #include <pybind11/stl.h>
// #include <pybind11/stl_bind.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
using pd_type = double;
@@ -49,31 +47,10 @@ void define_ClusterVector(py::module &m, const std::string &typestr) {
})
.def("sum_2x2",
[](ClusterVector<ClusterType> &self) {
// auto *vec =
// new std::vector<Sum_index_pair<Type, int>>(self.sum_2x2());
auto *vec = new std::vector<Sum_index_pair<Type, corner>>(
self.sum_2x2());
/*
py::capsule free_when_done(vec, [](void *f) {
std::vector<std::pair<Type, int>> *foo =
reinterpret_cast<std::vector<std::pair<Type, int>> *>(
f);
delete foo;
});
py::buffer result(
vec->data(),
static_cast<py::ssize_t>(sizeof(std::pair<Type, int>)),
fmt::format("T{{{}:sum:i:index}}",
py::format_descriptor<Type>::format()),
static_cast<py::ssize_t>(1),
std::vector<py::ssize_t>{
static_cast<py::ssize_t>(vec->size())},
std::vector<py::ssize_t>{static_cast<py::ssize_t>(
sizeof(std::pair<Type, int>))});
//,static_cast<py::object>(free_when_done));
*/
return self.sum_2x2(); // return_vector(vec);
return return_vector(vec);
})
.def_property_readonly("size", &ClusterVector<ClusterType>::size)
.def("item_size", &ClusterVector<ClusterType>::item_size)

View File

@@ -130,4 +130,11 @@ PYBIND11_MODULE(_aare, m) {
m, "Sum_index_pair_i",
fmt::format("T{{{}:sum:i:index}}",
py::format_descriptor<double>::format()));
using Sum_index_pair_d = Sum_index_pair<double, corner>;
PYBIND11_NUMPY_DTYPE(Sum_index_pair_d, sum, index);
using Sum_index_pair_f = Sum_index_pair<float, corner>;
PYBIND11_NUMPY_DTYPE(Sum_index_pair_f, sum, index);
using Sum_index_pair_i = Sum_index_pair<int, corner>;
PYBIND11_NUMPY_DTYPE(Sum_index_pair_i, sum, index);
}

View File

@@ -40,7 +40,8 @@ def test_max_2x2_sum():
assert cv.size == 2
max_2x2 = cv.sum_2x2()
assert max_2x2.size == 2
print(max_2x2[0])
assert max_2x2[0]["sum"] == 8
assert max_2x2[0]["index"] == 2
def test_make_a_hitmap_from_cluster_vector():