From 25aac942e22663d11e50bf1e5b4c8287349895cb Mon Sep 17 00:00:00 2001 From: Leander Schlegel Date: Sun, 21 Apr 2024 19:42:05 +0200 Subject: [PATCH 1/5] bugfix to prevent photons getting pmass unequal to zero and commented lines for non-nuclei --- src/ParticleState.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ParticleState.cpp b/src/ParticleState.cpp index 9f867c384..ad7aa4fa9 100644 --- a/src/ParticleState.cpp +++ b/src/ParticleState.cpp @@ -55,8 +55,10 @@ void ParticleState::setId(int newId) { if (id < 0) charge *= -1; // anti-nucleus } else { - if (abs(id) == 11) + if (abs(id) == 11) //electron or positron pmass = mass_electron; + if (abs(id) == 22) //photon + pmass = 0.0; charge = HepPID::charge(id) * eplus; } } From a22b225623485aaf5a26e2c7d2fa0153364cb4d9 Mon Sep 17 00:00:00 2001 From: Leander Schlegel Date: Mon, 22 Apr 2024 18:35:51 +0200 Subject: [PATCH 2/5] introduction of new general function particleMass for non-nuclei and nuclei as suggested by Lukas and Julien --- include/crpropa/ParticleMass.h | 10 +++++++++- src/ParticleMass.cpp | 11 +++++++++++ src/ParticleState.cpp | 6 +----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/crpropa/ParticleMass.h b/include/crpropa/ParticleMass.h index f4e5a6d6c..fdfd8579d 100644 --- a/include/crpropa/ParticleMass.h +++ b/include/crpropa/ParticleMass.h @@ -6,7 +6,15 @@ namespace crpropa { * \addtogroup PhysicsDefinitions * @{ */ - + + /** Get the particle mass by lookup from a table. + For nuclei, the function nuclearMass is called, for the case of + electrons or positrons the mass_electron is returned and for all + other cases like photons and also neutrinos, zero mass is returned. + @param id id of the particle following the PDG numbering scheme + @returns The mass of a the particle + */ + double particleMass(int id); /** Get the nucleus mass by lookup from a table. The masses are the atomic masses from the NIST database: http://www.nist.gov/pml/data/comp.cfm diff --git a/src/ParticleMass.cpp b/src/ParticleMass.cpp index a2a5c5090..cc474eb2a 100644 --- a/src/ParticleMass.cpp +++ b/src/ParticleMass.cpp @@ -53,6 +53,17 @@ struct NuclearMassTable { static NuclearMassTable nuclearMassTable; +double particleMass(int id) { + double m; + if (isNucleus(id)) + return nuclearMass(id); + if (abs(id) == 11) + m = mass_electron; + else + m = 0.0; + return m; +} + double nuclearMass(int id) { int A = massNumber(id); int Z = chargeNumber(id); diff --git a/src/ParticleState.cpp b/src/ParticleState.cpp index ad7aa4fa9..85736a856 100644 --- a/src/ParticleState.cpp +++ b/src/ParticleState.cpp @@ -49,16 +49,12 @@ double ParticleState::getRigidity() const { void ParticleState::setId(int newId) { id = newId; + pmass = particleMass(id); if (isNucleus(id)) { - pmass = nuclearMass(id); charge = chargeNumber(id) * eplus; if (id < 0) charge *= -1; // anti-nucleus } else { - if (abs(id) == 11) //electron or positron - pmass = mass_electron; - if (abs(id) == 22) //photon - pmass = 0.0; charge = HepPID::charge(id) * eplus; } } From e900b39f746b706c8098c43f1cfc7d599f08cdd7 Mon Sep 17 00:00:00 2001 From: Leander Schlegel Date: Mon, 22 Apr 2024 18:45:09 +0200 Subject: [PATCH 3/5] updated CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4332cf998..cf17ffbb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,12 @@ ### Bug fixes: * Fixed sign for exponential decay of magn. field strength with Galactic height in LogarithmicSpiralField * Fixed r term in source distribution for SNR and Pulsar + * Fixed wrong mass inheritance for secondaries other than nuclei or electron/positron ### New features: ### Interface changes: + * Added new backwards-compatible function particleMass that returns particle mass also for non-nuclei ### Features that are deprecated and will be removed after this release * EBL model from Finke et al. 2022 From 8376c76cc47f37d54088dfe8b01d3998d3676285 Mon Sep 17 00:00:00 2001 From: Leander Schlegel Date: Tue, 23 Apr 2024 15:35:15 +0200 Subject: [PATCH 4/5] implementation of comments: codestyle and optimization of particleMass function --- CHANGELOG.md | 2 +- include/crpropa/ParticleMass.h | 2 ++ src/ParticleMass.cpp | 7 ++----- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf17ffbb7..4d7d0ccc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,9 @@ * Fixed wrong mass inheritance for secondaries other than nuclei or electron/positron ### New features: + * Added new backwards-compatible function particleMass that returns particle mass also for non-nuclei ### Interface changes: - * Added new backwards-compatible function particleMass that returns particle mass also for non-nuclei ### Features that are deprecated and will be removed after this release * EBL model from Finke et al. 2022 diff --git a/include/crpropa/ParticleMass.h b/include/crpropa/ParticleMass.h index fdfd8579d..06175f20c 100644 --- a/include/crpropa/ParticleMass.h +++ b/include/crpropa/ParticleMass.h @@ -15,6 +15,7 @@ namespace crpropa { @returns The mass of a the particle */ double particleMass(int id); + /** Get the nucleus mass by lookup from a table. The masses are the atomic masses from the NIST database: http://www.nist.gov/pml/data/comp.cfm @@ -25,6 +26,7 @@ namespace crpropa { @returns The mass of a the nucleus */ double nuclearMass(int id); + /** Get the nucleus mass by lookup from a table. The masses are the atomic masses from the NIST database: http://www.nist.gov/pml/data/comp.cfm diff --git a/src/ParticleMass.cpp b/src/ParticleMass.cpp index cc474eb2a..075668487 100644 --- a/src/ParticleMass.cpp +++ b/src/ParticleMass.cpp @@ -54,14 +54,11 @@ struct NuclearMassTable { static NuclearMassTable nuclearMassTable; double particleMass(int id) { - double m; if (isNucleus(id)) return nuclearMass(id); if (abs(id) == 11) - m = mass_electron; - else - m = 0.0; - return m; + return mass_electron; + return 0.0; } double nuclearMass(int id) { From cb9f2d9fa8e8bf29a7f41458251c0fed812e9590 Mon Sep 17 00:00:00 2001 From: Leander Schlegel Date: Tue, 23 Apr 2024 17:35:59 +0200 Subject: [PATCH 5/5] extending the test of addSecondary() for the secondaries mass and adding new test for the particleMass() function --- test/testCore.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/testCore.cpp b/test/testCore.cpp index 7aebff63a..b6738ac2a 100644 --- a/test/testCore.cpp +++ b/test/testCore.cpp @@ -149,6 +149,16 @@ TEST(ParticleID, isNucleus) { EXPECT_FALSE(isNucleus(11)); } +TEST(ParticleMass, particleMass) { + //particleMass(int id) interfaces nuclearMass for nuclei + EXPECT_DOUBLE_EQ(nuclearMass(nucleusId(1,1)), particleMass(nucleusId(1,1))); + //particleMass(int id) for electron/positron, photon and neutrino + EXPECT_DOUBLE_EQ(mass_electron,particleMass(11)); + EXPECT_DOUBLE_EQ(mass_electron,particleMass(-11)); + EXPECT_DOUBLE_EQ(0.0,particleMass(22)); + EXPECT_DOUBLE_EQ(0.0,particleMass(14)); +} + TEST(HepPID, consistencyWithReferenceImplementation) { // Tests the performance improved version against the default one unsigned long testPID = rand() % 1000000000 + 1000000000; @@ -219,8 +229,14 @@ TEST(Candidate, addSecondary) { c.addSecondary(nucleusId(1,1), 200); c.addSecondary(nucleusId(1,1), 200, 5.); - Candidate s1 = *c.secondaries[0]; - Candidate s2 = *c.secondaries[1]; + c.addSecondary(11, 200); + c.addSecondary(14, 200); + c.addSecondary(22, 200); + Candidate s1 = *c.secondaries[0]; //proton + Candidate s2 = *c.secondaries[1]; //proton + Candidate s3 = *c.secondaries[2]; //electron + Candidate s4 = *c.secondaries[3]; //neutrino + Candidate s5 = *c.secondaries[4]; //photon EXPECT_EQ(nucleusId(1,1), s1.current.getId()); EXPECT_EQ(200, s1.current.getEnergy()); @@ -231,6 +247,9 @@ TEST(Candidate, addSecondary) { EXPECT_TRUE(Vector3d(1,2,3) == s1.created.getPosition()); EXPECT_TRUE(Vector3d(0,0,1) == s1.created.getDirection()); EXPECT_TRUE(s1.getTagOrigin() == "SEC"); + EXPECT_EQ(mass_electron,s3.current.getMass()); + EXPECT_EQ(0.0,s4.current.getMass()); + EXPECT_EQ(0.0,s5.current.getMass()); EXPECT_EQ(15., s2.getWeight()); }