3#include <G4NistManager.hh>
4#include <TDatabasePDG.h>
11#include <system_error>
23 auto convert_to_int(std::string_view str,
int& val)
25 auto res = std::from_chars(str.begin(), str.end(), val);
26 if (res.ec != std::errc())
29 "conversion failed with the input: {}. Reason: {}", str, std::make_error_code(res.ec).message());
33 auto convert_nuclear_pid_to_name(int32_t pid) -> std::string
35 const auto isomeric_number = pid %
MASS_POS;
39 auto* nist_manager = G4NistManager::Instance();
40 auto* element = nist_manager->FindOrBuildElement(atomic_number);
42 if (isomeric_number == 0)
44 return fmt::format(
"{}-{}", element->GetName(), mass_number);
46 return fmt::format(
"{}-{}s", element->GetName(), mass_number, isomeric_number);
49 inline auto convert_basic_pid_to_name(int32_t pid) -> std::string
51 auto* particle = TDatabasePDG::Instance()->GetParticle(pid);
52 if (particle ==
nullptr)
54 throw R3B::logic_error(fmt::format(
"Failed to query a basic particle name from a pid: {}", pid));
56 return particle->GetName();
59 inline auto convert_basic_name_to_pid(
const std::string& name) -> int32_t
61 auto* particle = TDatabasePDG::Instance()->GetParticle(name.c_str());
62 if (particle ==
nullptr)
64 throw R3B::logic_error(fmt::format(
"Failed to query a basic particle pid from the name: {:?}", name));
66 return particle->PdgCode();
69 inline auto convert_nuclear_name_to_pid(
const std::string_view name) -> int32_t
71 const auto dash_pos = name.find(
'-');
72 auto element_name = name.substr(0, dash_pos);
73 const auto s_pos = name.find(
's', dash_pos);
75 auto isomeric_num = 0;
76 if (s_pos != std::string_view::npos)
78 convert_to_int(name.substr(dash_pos + 1, s_pos - dash_pos - 1), mass_num);
79 convert_to_int(name.substr(s_pos + 1, std::string_view::npos), isomeric_num);
83 convert_to_int(name.substr(dash_pos + 1, std::string_view::npos), mass_num);
85 auto* nist_manager = G4NistManager::Instance();
86 auto* element = nist_manager->FindOrBuildElement(std::string{ element_name });
87 if (element ==
nullptr)
89 throw R3B::logic_error(fmt::format(
"Cannot find any element with the name {:?}", element_name));
91 auto atomic_number =
static_cast<int32_t
>(element->GetZ());
96 auto convert_pid_to_name(int32_t pid) -> std::string
101 return convert_nuclear_pid_to_name(pid);
103 return convert_basic_pid_to_name(pid);
110 auto convert_name_to_pid(
const std::string& name) -> int32_t
115 return convert_nuclear_name_to_pid(name);
117 return convert_basic_name_to_pid(name);
153 const auto dash_pos = name.find(
'-');
154 if (dash_pos == std::string::npos)
159 if (name.size() == dash_pos + 1)
181 const auto name = convert_pid_to_name(pid);
191 const auto pid = convert_name_to_pid(name);
std::unordered_map< int32_t, std::string > pid_to_name_
static constexpr auto nuclear_id_min
std::unordered_map< std::string, int32_t > name_to_pid_
static auto get_type(const std::string &name) -> Type
Get the type of the particle from its name.
auto get_pid(const std::string &name) -> int32_t
auto get_name(int32_t pid) -> std::string
static constexpr auto nuclear_id_max
void register_entry(int32_t pid)
constexpr auto PDG_FULL_POS
constexpr auto ATOMIC_POS