Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
periodic.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file periodic.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <cmath>
12 
13 namespace Acts {
14 namespace detail {
15 
17 template <typename T>
18 inline T wrap_periodic(T value, T start, T range) {
19  using std::floor;
20  // only wrap if really necessary
21  T diff = value - start;
22  return ((0 <= diff) && (diff < range))
23  ? value
24  : (value - range * floor(diff / range));
25 }
26 
36 template <typename T>
37 inline T difference_periodic(T lhs, T rhs, T range) {
38  using std::fmod;
39  T delta = fmod(lhs - rhs, range);
40  // check if |delta| is larger than half the range. if that is the case, we
41  // can move either rhs/lhs by one range/period to get a smaller |delta|.
42  if ((2 * delta) < -range) {
43  delta += range;
44  } else if (range <= (2 * delta)) {
45  delta -= range;
46  }
47  return delta;
48 }
49 
51 template <typename T>
52 inline T radian_pos(T x) {
53  return wrap_periodic<T>(x, T(0), T(2 * M_PI));
54 }
55 
57 template <typename T>
58 inline T radian_sym(T x) {
59  return wrap_periodic<T>(x, T(-M_PI), T(2 * M_PI));
60 }
61 
78 template <typename T>
79 inline std::pair<T, T> normalizePhiTheta(T phi, T theta) {
80  // wrap to [0,2pi). while the nominal range of theta is [0,pi], it is
81  // periodic, i.e. describes identical positions, in the full [0,2pi) range.
82  // moving it first to the periodic range simplifies further steps as the
83  // possible range of theta becomes fixed.
84  theta = radian_pos(theta);
85  if (M_PI < theta) {
86  // theta is in the second half of the great circle and outside its nominal
87  // range. need to change both phi and theta to be within range.
88  phi += M_PI;
89  theta = 2 * M_PI - theta;
90  }
91  return {radian_sym(phi), theta};
92 }
93 
94 } // namespace detail
95 } // namespace Acts