EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Charge.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Charge.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 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 "Acts/Utilities/Units.hpp"
12 
13 #include <cassert>
14 #include <cmath>
15 
16 namespace Acts {
17 
51 
53 struct Neutral {
54  Neutral() = default;
58  template <typename T>
59  constexpr Neutral(T absQ) noexcept {
60  assert((absQ == 0.0f) and "Input charge must be zero");
61  // suppress `unused variable` warning in non-debug builds
62  (void)(absQ);
63  }
64 
65  template <typename T>
66  constexpr T extractCharge(T /* pInv */) const noexcept {
67  return 0;
68  }
69  template <typename T>
70  constexpr T extractMomentum(T pInv) const noexcept {
71  // the abs is not strictly needed. it is added to protect against invalid,
72  // i.e. negative, 1/p values to ensure that the output is still correct.
73  return std::abs(1 / pInv);
74  }
75 
80  friend constexpr bool operator==(Neutral, Neutral) noexcept { return true; }
81 };
82 
84 struct SinglyCharged {
85  SinglyCharged() = default;
89  template <typename T>
90  constexpr SinglyCharged(T absQ) noexcept {
91  assert((absQ == static_cast<T>(UnitConstants::e)) and
92  "Input charge magnitude must be e");
93  // suppress `unused variable` warning in non-debug builds
94  (void)(absQ);
95  }
96 
97  template <typename T>
98  constexpr T extractCharge(T qOverP) const noexcept {
99  return std::copysign(static_cast<T>(UnitConstants::e), qOverP);
100  }
101  template <typename T>
102  constexpr T extractMomentum(T qOverP) const noexcept {
103  return std::abs(static_cast<T>(UnitConstants::e) / qOverP);
104  }
105 
110  friend constexpr bool operator==(SinglyCharged, SinglyCharged) noexcept {
111  return true;
112  }
113 };
114 
120 class AnyCharge {
121  public:
123  AnyCharge() = delete;
125  template <typename T>
126  constexpr AnyCharge(T absQ) noexcept : m_magnitude(std::abs(absQ)) {
127  assert((0 <= absQ) and "Input charge magnitude must be zero or positive");
128  }
129 
130  template <typename T>
131  constexpr T extractCharge(T qOverP) const noexcept {
132  return std::copysign(static_cast<T>(m_magnitude), qOverP);
133  }
134  template <typename T>
135  constexpr T extractMomentum(T qOverP) const noexcept {
136  return (m_magnitude != 0.0f)
137  ? std::abs(static_cast<T>(m_magnitude) / qOverP)
138  : std::abs(1 / qOverP);
139  }
140 
141  private:
142  float m_magnitude;
143 
145  friend constexpr bool operator==(AnyCharge lhs, AnyCharge rhs) noexcept {
146  return lhs.m_magnitude == rhs.m_magnitude;
147  }
148 };
149 
151 
152 } // namespace Acts