Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:15

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/Definitions/ParticleData.hpp"
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 
0013 #include <algorithm>
0014 #include <cassert>
0015 #include <cstdint>
0016 #include <iterator>
0017 #include <limits>
0018 #include <optional>
0019 #include <ostream>
0020 #include <type_traits>
0021 
0022 #include "ParticleDataTable.hpp"
0023 
0024 namespace {
0025 
0026 // TODO the following functions could be constexpr but we are currently limited
0027 // by `std::find`
0028 
0029 static inline std::optional<std::size_t> findIndexByPdg(std::int32_t pdg) {
0030   auto beg = std::cbegin(kParticlesPdgNumber);
0031   auto end = std::cend(kParticlesPdgNumber);
0032   // assumes sorted container of pdg numbers
0033   auto pos = std::find(beg, end, pdg);
0034   if (pos == end) {
0035     return std::nullopt;
0036   }
0037   return std::make_optional(std::distance(beg, pos));
0038 }
0039 
0040 // Find an element within a data column using sorted pdg numbers as the index.
0041 template <typename ColumnContainer>
0042 static inline auto findByPdg(std::int32_t pdg, const ColumnContainer& column)
0043     -> std::optional<std::decay_t<decltype(column[0])>> {
0044   // should be a static_assert, but that seems to fail on LLVM
0045   assert((std::size(column) == kParticlesCount) && "Inconsistent column size");
0046 
0047   auto index = findIndexByPdg(pdg);
0048   if (!index) {
0049     return std::nullopt;
0050   }
0051   return column[*index];
0052 }
0053 
0054 static constexpr float extractCharge(float value) {
0055   // convert three charge to regular charge in native units
0056   return (value / 3.0f) * Acts::UnitConstants::e;
0057 }
0058 
0059 static constexpr float extractMass(float value) {
0060   return value * Acts::UnitConstants::MeV;
0061 }
0062 
0063 }  // namespace
0064 
0065 std::optional<float> Acts::findCharge(Acts::PdgParticle pdg) {
0066   const auto charge =
0067       findByPdg(static_cast<std::int32_t>(pdg), kParticlesThreeCharge);
0068   if (!charge) {
0069     return std::nullopt;
0070   }
0071   return extractCharge(*charge);
0072 }
0073 
0074 std::optional<float> Acts::findMass(Acts::PdgParticle pdg) {
0075   const auto mass =
0076       findByPdg(static_cast<std::int32_t>(pdg), kParticlesMassMeV);
0077   if (!mass) {
0078     return std::nullopt;
0079   }
0080   return extractMass(*mass);
0081 }
0082 
0083 std::optional<std::string_view> Acts::findName(Acts::PdgParticle pdg) {
0084   return findByPdg(static_cast<std::int32_t>(pdg), kParticlesName);
0085 }
0086 
0087 std::optional<Acts::ParticleData> Acts::findParticleData(PdgParticle pdg) {
0088   auto index = findIndexByPdg(pdg);
0089   if (!index) {
0090     return std::nullopt;
0091   }
0092   ParticleData result;
0093   result.charge = extractCharge(kParticlesThreeCharge[*index]);
0094   result.mass = extractMass(kParticlesMassMeV[*index]);
0095   result.name = kParticlesName[*index];
0096   return {};
0097 }
0098 
0099 std::ostream& Acts::operator<<(std::ostream& os, Acts::PdgParticle pdg) {
0100   const auto name = Acts::findName(pdg);
0101   if (name) {
0102     os << *name;
0103   } else {
0104     os << static_cast<std::int32_t>(pdg);
0105   }
0106   return os;
0107 }
0108 
0109 std::optional<std::string_view> Acts::pdgToShortAbsString(PdgParticle pdg) {
0110   pdg = makeAbsolutePdgParticle(pdg);
0111   if (pdg == eElectron) {
0112     return "e";
0113   }
0114   if (pdg == eMuon) {
0115     return "mu";
0116   }
0117   if (pdg == eTau) {
0118     return "t";
0119   }
0120   if (pdg == eGamma) {
0121     return "g";
0122   }
0123   if (pdg == ePionZero) {
0124     return "pi0";
0125   }
0126   if (pdg == ePionPlus) {
0127     return "pi";
0128   }
0129   if (pdg == eKaonPlus) {
0130     return "K";
0131   }
0132   if (pdg == eNeutron) {
0133     return "n";
0134   }
0135   if (pdg == eProton) {
0136     return "p";
0137   }
0138   if (pdg == eLead) {
0139     return "lead";
0140   }
0141   return std::nullopt;
0142 }