From e6c4da9f80d2c5133b6b753fe9b8ace1498cd016 Mon Sep 17 00:00:00 2001 From: Manos Vourliotis Date: Fri, 27 Dec 2024 11:45:55 -0800 Subject: [PATCH] Port alpaka phi functions to DataFormats --- DataFormats/PortableMath/interface/deltaPhi.h | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 DataFormats/PortableMath/interface/deltaPhi.h diff --git a/DataFormats/PortableMath/interface/deltaPhi.h b/DataFormats/PortableMath/interface/deltaPhi.h new file mode 100644 index 0000000000000..fd61d6501eaff --- /dev/null +++ b/DataFormats/PortableMath/interface/deltaPhi.h @@ -0,0 +1,45 @@ +#ifndef DataFormats_PortableMath_deltaPhi_h +#define DataFormats_PortableMath_deltaPhi_h + +/* functions to compute heterogeneous deltaPhi + * Ported from the LST algorithm: + * https://github.com/cms-sw/cmssw/pull/45117#discussion_r1749143439 + */ + +namespace cms::alpakatools { + + // reduce to [-pi,pi] + template + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T reduceRange(TAcc const& acc, T x) { + constexpr T o2pi = 1. / (2. * M_PI); + if (alpaka::math::abs(acc, x) <= T(M_PI)) + return x; + T n = alpaka::math::round(acc, x * o2pi); + return x - n * T(2. * M_PI); + } + + template + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T phi(TAcc const& acc, T x, T y) { + return reduceRange(acc, M_PI + alpaka::math::atan2(acc, -y, -x)); + } + + template + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T deltaPhi(TAcc const& acc, T x1, T y1, T x2, T y2) { + T phi1 = phi(acc, x1, y1); + T phi2 = phi(acc, x2, y2); + return reduceRange(acc, phi2 - phi1); + } + + template + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T deltaPhi(T phi1, T phi2) { + return reduceRange(phi1 - phi2); + } + + template + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T deltaPhiChange(TAcc const& acc, T x1, T y1, T x2, T y2) { + return deltaPhi(acc, x1, y1, x2 - x1, y2 - y1); + } + +} // namespace cms::alpakatools + +#endif