Skip to content

Commit

Permalink
Fix flaky tests by disregarding order of mc-reco links (#130)
Browse files Browse the repository at this point in the history
* Sort linked particles before comparing kinematics
  • Loading branch information
tmadlener authored Oct 4, 2024
1 parent d401c9b commit 79f25af
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions tests/src/compare_delphes_converter_outputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,26 @@ template <typename DelphesT> std::vector<GenParticle*> getAssociatedMCParticles(
*/
template <typename DelphesT>
bool compareMCRelations(const DelphesT* delphesCand, edm4hep::ReconstructedParticle edm4hepCand,
const edm4hep::MCRecoParticleAssociationCollection& associations) {
const auto delphesGenParticles = getAssociatedMCParticles(delphesCand);
const auto edm4hepMCParticles = getAssociatedMCParticles(edm4hepCand, associations);
const edm4hep::RecoMCParticleLinkCollection& associations) {
auto delphesGenParticles = getAssociatedMCParticles(delphesCand);
auto edm4hepMCParticles = getAssociatedMCParticles(edm4hepCand, associations);

if (delphesGenParticles.size() != edm4hepMCParticles.size()) {
return false;
}

// In some cases the order of the vectors is not exactly the same, because
// Delphes(!) seems to write them in a different order from time to time even
// with equal random seeds. Here we sort them simply by the x coordinate of
// the momentum since that is easiest to do without having to wade into the
// subtleties of potentially small differences in energy in a four vector
// depending on whether energy is stored directly or whether we compute it via
// the momentum and the mass.
std::sort(delphesGenParticles.begin(), delphesGenParticles.end(),
[](const auto* a, const auto* b) { return a->P4().X() < b->P4().X(); });
std::sort(edm4hepMCParticles.begin(), edm4hepMCParticles.end(),
[](const auto& a, const auto& b) { return a.getMomentum().x < b.getMomentum().x; });

for (size_t i = 0; i < delphesGenParticles.size(); ++i) {
if (!compareKinematics(delphesGenParticles[i], edm4hepMCParticles[i])) {
return false;
Expand Down

0 comments on commit 79f25af

Please sign in to comment.