Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParticleData.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ParticleData.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018-2023 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 
10 
12 
13 #include <algorithm>
14 #include <cassert>
15 #include <cstdint>
16 #include <iterator>
17 #include <limits>
18 #include <optional>
19 #include <ostream>
20 #include <type_traits>
21 
22 #include "ParticleDataTable.hpp"
23 
24 namespace {
25 
26 // TODO the following functions could be constexpr but we are currently limited
27 // by `std::find`
28 
29 static inline std::optional<std::size_t> findIndexByPdg(std::int32_t pdg) {
30  auto beg = std::cbegin(kParticlesPdgNumber);
31  auto end = std::cend(kParticlesPdgNumber);
32  // assumes sorted container of pdg numbers
33  auto pos = std::find(beg, end, pdg);
34  if (pos == end) {
35  return std::nullopt;
36  }
37  return std::make_optional(std::distance(beg, pos));
38 }
39 
40 // Find an element within a data column using sorted pdg numbers as the index.
41 template <typename ColumnContainer>
42 static inline auto findByPdg(std::int32_t pdg, const ColumnContainer& column)
43  -> std::optional<std::decay_t<decltype(column[0])>> {
44  // should be a static_assert, but that seems to fail on LLVM
45  assert((std::size(column) == kParticlesCount) and "Inconsistent column size");
46 
47  auto index = findIndexByPdg(pdg);
48  if (!index) {
49  return std::nullopt;
50  }
51  return column[*index];
52 }
53 
54 static constexpr inline float extractCharge(float value) {
55  // convert three charge to regular charge in native units
56  return (value / 3.0f) * Acts::UnitConstants::e;
57 }
58 
59 static constexpr inline float extractMass(float value) {
60  return value * Acts::UnitConstants::MeV;
61 }
62 
63 } // namespace
64 
65 std::optional<float> Acts::findCharge(Acts::PdgParticle pdg) {
66  const auto charge =
67  findByPdg(static_cast<std::int32_t>(pdg), kParticlesThreeCharge);
68  if (!charge) {
69  return std::nullopt;
70  }
71  return extractCharge(*charge);
72 }
73 
74 std::optional<float> Acts::findMass(Acts::PdgParticle pdg) {
75  const auto mass =
76  findByPdg(static_cast<std::int32_t>(pdg), kParticlesMassMeV);
77  if (!mass) {
78  return std::nullopt;
79  }
80  return extractMass(*mass);
81 }
82 
83 std::optional<std::string_view> Acts::findName(Acts::PdgParticle pdg) {
84  return findByPdg(static_cast<std::int32_t>(pdg), kParticlesName);
85 }
86 
87 std::optional<Acts::ParticleData> Acts::findParticleData(PdgParticle pdg) {
88  auto index = findIndexByPdg(pdg);
89  if (!index) {
90  return std::nullopt;
91  }
92  ParticleData result;
93  result.charge = extractCharge(kParticlesThreeCharge[*index]);
94  result.mass = extractMass(kParticlesMassMeV[*index]);
95  result.name = kParticlesName[*index];
96  return {};
97 }
98 
99 std::ostream& Acts::operator<<(std::ostream& os, Acts::PdgParticle pdg) {
100  const auto name = Acts::findName(pdg);
101  if (name) {
102  os << *name;
103  } else {
104  os << static_cast<std::int32_t>(pdg);
105  }
106  return os;
107 }
108 
109 std::optional<std::string_view> Acts::pdgToShortAbsString(PdgParticle pdg) {
110  pdg = makeAbsolutePdgParticle(pdg);
111  if (pdg == eElectron) {
112  return "e";
113  }
114  if (pdg == eMuon) {
115  return "mu";
116  }
117  if (pdg == eTau) {
118  return "t";
119  }
120  if (pdg == eTau) {
121  return "g";
122  }
123  if (pdg == ePionZero) {
124  return "pi0";
125  }
126  if (pdg == ePionPlus) {
127  return "pi";
128  }
129  if (pdg == eNeutron) {
130  return "n";
131  }
132  if (pdg == eProton) {
133  return "p";
134  }
135  if (pdg == eLead) {
136  return "lead";
137  }
138  return std::nullopt;
139 }