EIC 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-2018 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 #include <cmath>
11 
12 namespace Acts {
13 namespace detail {
14 
16 template <typename T>
17 inline T wrap_periodic(T value, T start, T range) {
18  using std::floor;
19  // only wrap if really necessary
20  T diff = value - start;
21  return ((0 <= diff) && (diff < range))
22  ? value
23  : (value - range * floor(diff / range));
24 }
25 
27 template <typename T>
28 inline T radian_pos(T x) {
29  return wrap_periodic<T>(x, T(0), T(2 * M_PI));
30 }
31 
33 template <typename T>
34 inline T radian_sym(T x) {
35  return wrap_periodic<T>(x, T(-M_PI), T(2 * M_PI));
36 }
37 
41 template <typename T>
42 inline std::pair<T, T> ensureThetaBounds(T phi, T theta) {
43  T tmpPhi = radian_sym(phi);
44 
45  T tmpTht = std::fmod(theta, 2 * M_PI);
46  if (tmpTht < -M_PI) {
47  tmpTht = std::abs(tmpTht + 2 * M_PI);
48  } else if (tmpTht < 0) {
49  tmpTht *= -1;
50  tmpPhi += M_PI;
51  tmpPhi = tmpPhi > M_PI ? tmpPhi - 2 * M_PI : tmpPhi;
52  }
53  if (tmpTht > M_PI) {
54  tmpTht = 2 * M_PI - tmpTht;
55  tmpPhi += M_PI;
56  tmpPhi = tmpPhi > M_PI ? (tmpPhi - 2 * M_PI) : tmpPhi;
57  }
58 
59  return std::pair<T, T>(tmpPhi, tmpTht);
60 }
61 
62 } // namespace detail
63 } // namespace Acts