diff --git a/include/aare/NDArray.hpp b/include/aare/NDArray.hpp index 1a501eb..f73bfd6 100644 --- a/include/aare/NDArray.hpp +++ b/include/aare/NDArray.hpp @@ -217,6 +217,7 @@ class NDArray : public ArrayExpr, Ndim> { std::fill(shape_.begin(), shape_.end(), 0); std::fill(strides_.begin(), strides_.end(), 0); } + }; // Move assign @@ -380,12 +381,8 @@ NDArray NDArray::operator*(const T &value) { result *= value; return result; } -// template void NDArray::Print() { -// if (shape_[0] < 20 && shape_[1] < 20) -// Print_all(); -// else -// Print_some(); -// } + + template std::ostream &operator<<(std::ostream &os, const NDArray &arr) { @@ -437,4 +434,22 @@ NDArray load(const std::string &pathname, return img; } + +template +NDArray safe_divide(const NDArray &numerator, + const NDArray &denominator) { + if (numerator.shape() != denominator.shape()) { + throw std::runtime_error("Shapes of numerator and denominator must match"); + } + NDArray result(numerator.shape()); + for (ssize_t i = 0; i < numerator.size(); ++i) { + if (denominator[i] != 0) { + result[i] = static_cast(numerator[i]) / static_cast(denominator[i]); + } else { + result[i] = RT{0}; // or handle division by zero as needed + } + } + return result; +} + } // namespace aare \ No newline at end of file diff --git a/include/aare/calibration.hpp b/include/aare/calibration.hpp index 8d672b4..d61f706 100644 --- a/include/aare/calibration.hpp +++ b/include/aare/calibration.hpp @@ -161,6 +161,9 @@ NDArray calculate_pedestal_g0(NDView raw_data) { } return pedestal; } + + + template NDArray calculate_pedestal_g0(NDView raw_data, ssize_t n_threads) { @@ -191,20 +194,8 @@ NDArray calculate_pedestal_g0(NDView raw_data, accumulator += acc; count += cnt; } - NDArray pedestal( - std::array{raw_data.shape(1), raw_data.shape(2)}, 0); - for (int gain = 0; gain < 3; ++gain) { - for (int row = 0; row < raw_data.shape(1); ++row) { - for (int col = 0; col < raw_data.shape(2); ++col) { - if (count(row, col) != 0) { - pedestal(row, col) = - static_cast(accumulator(row, col)) / - static_cast(count(row, col)); - } - } - } - } - return pedestal; + + return safe_divide(accumulator, count); } @@ -212,8 +203,6 @@ NDArray calculate_pedestal_g0(NDView raw_data, template NDArray calculate_pedestal(NDView raw_data, ssize_t n_threads) { - NDArray switched( - std::array{raw_data.shape(1), raw_data.shape(2)}, 0); std::vector, NDArray>>> futures; futures.reserve(n_threads); @@ -244,20 +233,7 @@ NDArray calculate_pedestal(NDView raw_data, count += cnt; } - NDArray pedestal( - std::array{3, raw_data.shape(1), raw_data.shape(2)}, 0); - for (int gain = 0; gain < 3; ++gain) { - for (int row = 0; row < raw_data.shape(1); ++row) { - for (int col = 0; col < raw_data.shape(2); ++col) { - if (count(gain, row, col) != 0) { - pedestal(gain, row, col) = - static_cast(accumulator(gain, row, col)) / - static_cast(count(gain, row, col)); - } - } - } - } - return pedestal; + return safe_divide(accumulator, count); } /**