From f58272feb970374dd5092fc4582fd2f5cd1aa721 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 1 Dec 2024 00:59:18 -0500 Subject: [PATCH] feat: `particle::get` utility function --- src/iguana/algorithms/TypeDefs.h | 33 +++++++++++++++++++ .../clas12/PhotonGBTFilter/Algorithm.cc | 15 ++------- .../clas12/PhotonGBTFilter/Algorithm.h | 7 ---- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/iguana/algorithms/TypeDefs.h b/src/iguana/algorithms/TypeDefs.h index d97e4e08..95a0fca4 100644 --- a/src/iguana/algorithms/TypeDefs.h +++ b/src/iguana/algorithms/TypeDefs.h @@ -3,6 +3,7 @@ #pragma once #include +#include #include namespace iguana { @@ -92,6 +93,38 @@ namespace iguana { { kaon_minus, 0.493677 } }; + /// @brief get a particle property given a PDG code + /// + /// Example: + /// ```cpp + /// auto mass = particle::get(particle::mass, particle::PDG::photon); // mass => 0.0 + /// ``` + /// @param property the particle property, such as `particle::name`, `particle::title`, or `particle::mass` + /// @param pdg_code the `particle::PDG` value + /// @returns the value of the property, if defined for this `pdg_code` + template + std::optional const get(std::unordered_map const& property, PDG const& pdg_code) + { + if(auto const& it = property.find(pdg_code); it != property.end()) + return it->second; + return std::nullopt; + } + + /// @brief get a particle property given a PDG code + /// + /// Example: + /// ```cpp + /// auto mass = particle::get(particle::mass, 22); // mass => 0.0 + /// ``` + /// @param property the particle property, such as `particle::name`, `particle::title`, or `particle::mass` + /// @param pdg_code the `particle::PDG` value + /// @returns the value of the property, if defined for this `pdg_code` + template + std::optional const get(std::unordered_map const& property, int const& pdg_code) + { + return get(property, static_cast(pdg_code)); + } + // clang-format on } diff --git a/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.cc b/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.cc index 51ddc500..72c7ff23 100644 --- a/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.cc @@ -160,15 +160,15 @@ namespace iguana::clas12 { auto calo_PART = calo_map.at(inner_row); auto pid = particleBank.getInt("pid",inner_row); - auto mass = GetMass(pid); + auto mass = particle::get(particle::mass, pid); // Skip over particle if its mass was undefined - if (mass == -1.0) continue; + if (!mass.value()) continue; auto px = particleBank.getFloat("px",inner_row); auto py = particleBank.getFloat("py",inner_row); auto pz = particleBank.getFloat("pz",inner_row); auto p = sqrt(px*px+py*py+pz*pz); - auto E = sqrt(p*p+mass*mass); + auto E = sqrt(p*p+mass.value()*mass.value()); auto th = acos(pz/p); // Skip over particle if it is not in the forward detector (necessary for model compatibility) if (ForwardDetectorFilter(th)==false) continue; @@ -367,15 +367,6 @@ namespace iguana::clas12 { return v; } - double PhotonGBTFilter::GetMass(int pid) const { - auto it = particle::mass.find(static_cast(pid)); - if (it != particle::mass.end()) { - return it->second; - } else { - return -1.0; // Default mass if pid not found - } - } - std::function const &)> PhotonGBTFilter::getModelFunction(int runnum) const { for (const auto &entry : modelMap) { diff --git a/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.h b/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.h index fb87f499..9c68442a 100644 --- a/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/PhotonGBTFilter/Algorithm.h @@ -101,13 +101,6 @@ namespace iguana::clas12 { /// @returns a ROOT::Math::XYZVector with the coordinates of the particle in the calorimeter ROOT::Math::XYZVector GetParticleCaloVector(PhotonGBTFilter::calo_row_data calo_row) const; - - /// Gets the mass of a particle given its PID - /// @param pid the particle ID to get the mass for - /// @returns the mass of the particle in GeV; returns -1.0 if the PID is not recognized - double GetMass(int pid) const; - - /// Gets the model function for the run number /// @param runnum the run of the associated event /// @returns GBT function for the run period