Skip to content

Commit

Permalink
refactor: Drop bool conversion in favor of isAlive method in `Fatra…
Browse files Browse the repository at this point in the history
…s::Particle` (#2266)

Makes the state check for the particle more explicit
  • Loading branch information
andiwand committed Jul 4, 2023
1 parent 0c906ad commit af748f6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
9 changes: 6 additions & 3 deletions Fatras/include/ActsFatras/EventData/Particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ class Particle {
Scalar energy() const { return std::hypot(m_mass, m_absMomentum); }

/// Check if the particle is alive, i.e. is not at rest.
constexpr operator bool() const { return Scalar(0) < m_absMomentum; }
/// Check if the particle is dead, i.e is at rest.
constexpr bool operator!() const { return m_absMomentum <= Scalar(0); }
constexpr bool isAlive() const { return Scalar(0) < m_absMomentum; }

constexpr bool isSecondary() const {
return particleId().vertexSecondary() != 0 ||
particleId().generation() != 0 || particleId().subParticle() != 0;
}

// simulation specific properties

Expand Down
10 changes: 5 additions & 5 deletions Tests/UnitTests/Fatras/EventData/ParticleTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(Construct) {
BOOST_CHECK_EQUAL(particle.transverseMomentum(), Particle::Scalar(0));
BOOST_CHECK_EQUAL(particle.absoluteMomentum(), Particle::Scalar(0));
// particle is created at rest and thus not alive
BOOST_CHECK(not particle);
BOOST_CHECK(not particle.isAlive());
}

BOOST_AUTO_TEST_CASE(CorrectEnergy) {
Expand Down Expand Up @@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE(CorrectEnergy) {
Particle::Scalar(std::hypot(1_GeV, 2_GeV) - 100_MeV));
CHECK_CLOSE_REL(particle.unitDirection().norm(), 1, eps);
// particle is still alive
BOOST_CHECK(particle);
BOOST_CHECK(particle.isAlive());

// loose some more energy
particle.correctEnergy(-200_MeV);
Expand All @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(CorrectEnergy) {
Particle::Scalar(std::hypot(1_GeV, 2_GeV) - 300_MeV));
CHECK_CLOSE_REL(particle.unitDirection().norm(), 1, eps);
// particle is still alive
BOOST_CHECK(particle);
BOOST_CHECK(particle.isAlive());

// loose a lot of energy
particle.correctEnergy(-3_GeV);
Expand All @@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(CorrectEnergy) {
BOOST_CHECK_EQUAL(particle.energy(), particle.mass());
CHECK_CLOSE_REL(particle.unitDirection().norm(), 1, eps);
// particle is not alive anymore
BOOST_CHECK(not particle);
BOOST_CHECK(not particle.isAlive());

// lossing even more energy does nothing
particle.correctEnergy(-10_GeV);
Expand All @@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(CorrectEnergy) {
BOOST_CHECK_EQUAL(particle.energy(), particle.mass());
CHECK_CLOSE_REL(particle.unitDirection().norm(), 1, eps);
// particle is still not alive
BOOST_CHECK(not particle);
BOOST_CHECK(not particle.isAlive());
}

BOOST_AUTO_TEST_SUITE_END()
2 changes: 1 addition & 1 deletion Tests/UnitTests/Fatras/Kernel/SimulationActorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct MockInteractionList {
generated.push_back(particle);
particle.correctEnergy(-energyLoss);
// break if particle is not alive anymore
return !particle;
return !particle.isAlive();
}

template <typename generator_t>
Expand Down

0 comments on commit af748f6

Please sign in to comment.