Skip to content

Commit f9359f6

Browse files
sawenzelclaude
andcommitted
Warn only once for unknown PDG codes in the Stack prototypes
The PDG database is now checked directly, so an unknown code no longer constructs a TParticle twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3bf08c0 commit f9359f6

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎Detectors/Base/src/Stack.cxx‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "CommonUtils/ConfigurationMacroHelper.h"
2828

2929
#include "TLorentzVector.h" // for TLorentzVector
30+
#include "TDatabasePDG.h" // for TDatabasePDG
3031
#include "TParticle.h" // for TParticle
3132
#include "TRefArray.h" // for TRefArray
3233
#include "TVirtualMC.h" // for VMC
@@ -36,6 +37,7 @@
3637
#include <cassert>
3738
#include <cstddef> // for NULL
3839
#include <cmath>
40+
#include <optional>
3941
#include <unordered_map>
4042
#include "DetectorsBase/SimRandom.h"
4143

@@ -50,21 +52,20 @@ namespace
5052
TParticle makeParticle(int pdg, int status, int mother1, int mother2, int daughter1, int daughter2,
5153
double px, double py, double pz, double e, double vx, double vy, double vz, double t)
5254
{
53-
struct Prototype {
54-
TParticle particle;
55-
bool known; // whether the PDG code is in TDatabasePDG
56-
};
57-
thread_local std::unordered_map<int, Prototype> prototypes;
55+
// an empty optional marks a PDG code that is not in TDatabasePDG
56+
thread_local std::unordered_map<int, std::optional<TParticle>> prototypes;
5857
auto it = prototypes.find(pdg);
5958
if (it == prototypes.end()) {
60-
TParticle proto(pdg, 0, -1, -1, -1, -1, 0., 0., 0., 0., 0., 0., 0., 0.);
61-
bool known = proto.GetPDG(1) != nullptr;
62-
it = prototypes.emplace(pdg, Prototype{proto, known}).first;
59+
std::optional<TParticle> proto;
60+
if (TDatabasePDG::Instance()->GetParticle(pdg)) {
61+
proto.emplace(pdg, 0, -1, -1, -1, -1, 0., 0., 0., 0., 0., 0., 0., 0.);
62+
}
63+
it = prototypes.emplace(pdg, std::move(proto)).first;
6364
}
64-
if (!it->second.known) { // the mass then comes from the 4-momentum
65+
if (!it->second) { // the mass then comes from the 4-momentum
6566
return TParticle(pdg, status, mother1, mother2, daughter1, daughter2, px, py, pz, e, vx, vy, vz, t);
6667
}
67-
TParticle p(it->second.particle); // the copy keeps the PDG record and the mass
68+
TParticle p(*it->second); // the copy keeps the PDG record and the mass
6869
p.SetStatusCode(status);
6970
p.SetFirstMother(mother1);
7071
p.SetLastMother(mother2);

0 commit comments

Comments
 (0)