Files
aare/python/src/bind_Eta.hpp
T
mazzol_a 2736d975c5
Build on RHEL9 / build (push) Successful in 2m29s
Build on RHEL8 / build (push) Successful in 2m52s
Run tests using data on local RHEL8 / build (push) Successful in 3m49s
Build on local RHEL8 / build (push) Successful in 2m32s
Dev/enable custom etas (#305)
- Allowing the users more flexibility to play around with custom eta
functions without touching the c++ code

- passing vector of eta values to ``transform_eta_values`` 

```
from aare import Interpolator, ClusterVector, Etai, Cluster
import numpy as np 

def custom_eta(cluster_pixel_coordinate_x, cluster_pixel_coordinate_y, cluster_data):
    # dummy custom eta function that just returns the sum of the cluster data
    eta = Etai()
    eta.x = 0.1 # dummy x value
    eta.y = 0.1 # dummy y value
    eta.sum = np.sum(cluster_data) # sum of the cluster data as the "energy
    return eta

# Create a dummy eta distribution and bins
eta_distribution = np.zeros((10, 10, 1)) # dummy eta distribution
etax_bins = np.linspace(0, 1.0, 11)
etay_bins = np.linspace(0, 1.0, 11)
e_bins = np.array([0., 10.]) # dummy energy bins

# Create the interpolator
interpolator = Interpolator(eta_distribution, etax_bins, etay_bins, e_bins)

# Create a dummy cluster vector
cluster_vector = ClusterVector()
cluster_vector.push_back(Cluster(10, 5, np.ones(shape=9, dtype = np.int32)))
cluster_vector.push_back(Cluster(20, 10, np.ones(shape=9, dtype = np.int32)))

# Create dummy etas for the clusters
cluster_array = np.array(cluster_vector)
etas = np.array([custom_eta(cluster["x"], cluster["y"], cluster["data"]) for cluster in cluster_array])

# transform eta values to uniform coordinates 
uniform_coordinates = interpolator.transform_eta_values(etas)

# Interpolate to get the photon coordinates e.g. apply interpolation logic 
photon_coordinates_x = cluster_array["x"] + uniform_coordinates["x"] # add to pixel coordinate 
photon_coordinates_y = cluster_array["y"] + uniform_coordinates["y"] # add to pixel coordinate 

```
advantage: full control over interpolation logic, 
downside: inefficient quite some loops in python
- passing pre computed eta values to interpolate function 
```
Interpolator.interpolate(cluster_vector, etas) 
```
downside: less flexibility in interpolation logic. 
downside: People might misuse it instead of using interpolate directly
with a pre compiled eta function implemented in c++
2026-04-24 14:01:13 +02:00

104 lines
3.7 KiB
C++

#include "aare/CalculateEta.hpp"
#include <cstdint>
// #include <pybind11/native_enum.h> only for version 3
#include <pybind11/pybind11.h>
namespace py = pybind11;
using namespace ::aare;
template <typename T>
void define_eta(py::module &m, const std::string &typestr) {
auto class_name = fmt::format("Eta{}", typestr);
py::class_<Eta2<T>>(m, class_name.c_str())
.def(py::init<>())
.def_readwrite("x", &Eta2<T>::x, "eta x value")
.def_readwrite("y", &Eta2<T>::y, "eta y value")
.def_readwrite("c", &Eta2<T>::c,
"eta corner value cTopLeft, cTopRight, "
"cBottomLeft, cBottomRight")
.def_readwrite("sum", &Eta2<T>::sum, "photon energy of cluster");
}
void define_corner_enum(py::module &m) {
py::enum_<corner>(m, "corner", "enum.Enum")
.value("cTopLeft", corner::cTopLeft)
.value("cTopRight", corner::cTopRight)
.value("cBottomLeft", corner::cBottomLeft)
.value("cBottomRight", corner::cBottomRight)
.export_values();
}
template <typename Type, uint8_t CoordSizeX, uint8_t CoordSizeY,
typename CoordType = uint16_t>
void register_calculate_2x2eta(py::module &m) {
using ClusterType = Cluster<Type, CoordSizeX, CoordSizeY, CoordType>;
m.def(
"calculate_eta2",
[](const aare::ClusterVector<ClusterType> &clusters) {
auto eta2 = new std::vector<Eta2<typename ClusterType::value_type>>(
calculate_eta2(clusters));
return return_vector(eta2);
},
R"(calculates eta2x2)", py::arg("clusters"));
m.def(
"calculate_eta2",
[](const aare::Cluster<Type, CoordSizeX, CoordSizeY, CoordType>
&cluster) { return calculate_eta2(cluster); },
R"(calculates eta2x2)", py::arg("cluster"));
m.def(
"calculate_full_eta2",
[](const aare::Cluster<Type, CoordSizeX, CoordSizeY, CoordType>
&cluster) { return calculate_full_eta2(cluster); },
R"(calculates full eta2x2)", py::arg("cluster"));
m.def(
"calculate_full_eta2",
[](const aare::ClusterVector<ClusterType> &clusters) {
auto eta2 = new std::vector<Eta2<typename ClusterType::value_type>>(
calculate_full_eta2(clusters));
return return_vector(eta2);
},
R"(calculates full eta2x2)", py::arg("clusters"));
}
template <typename Type, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
typename CoordType = uint16_t>
void register_calculate_3x3eta(py::module &m) {
using ClusterType = Cluster<Type, ClusterSizeX, ClusterSizeY, CoordType>;
m.def(
"calculate_eta3",
[](const aare::ClusterVector<ClusterType> &clusters) {
auto eta = new std::vector<Eta2<Type>>(calculate_eta3(clusters));
return return_vector(eta);
},
R"(calculates eta3x3 using entire cluster)", py::arg("clusters"));
m.def(
"calculate_cross_eta3",
[](const aare::ClusterVector<ClusterType> &clusters) {
auto eta =
new std::vector<Eta2<Type>>(calculate_cross_eta3(clusters));
return return_vector(eta);
},
R"(calculates eta3x3 taking into account cross pixels in cluster)",
py::arg("clusters"));
m.def(
"calculate_eta3",
[](const ClusterType &cluster) { return calculate_eta3(cluster); },
R"(calculates eta3x3 using entire cluster)", py::arg("cluster"));
m.def(
"calculate_cross_eta3",
[](const ClusterType &cluster) {
return calculate_cross_eta3(cluster);
},
R"(calculates eta3x3 taking into account cross pixels in cluster)",
py::arg("cluster"));
}