Skip to content

Commit

Permalink
Make roundtrip tests check ParticleIDs in new format
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Mar 12, 2024
1 parent 3e81ef5 commit bc4f8c8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 39 deletions.
3 changes: 3 additions & 0 deletions tests/edm4hep_roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int main()
ASSERT_SAME_OR_ABORT(edm4hep::TrackerHitPlaneCollection, "trackerHitPlanes");
ASSERT_SAME_OR_ABORT(edm4hep::ClusterCollection, "clusters");
ASSERT_SAME_OR_ABORT(edm4hep::ReconstructedParticleCollection, "recos");
ASSERT_SAME_OR_ABORT(edm4hep::ParticleIDCollection, "recos_PID_algo1");
ASSERT_SAME_OR_ABORT(edm4hep::ParticleIDCollection, "recos_PID_algo2");
ASSERT_SAME_OR_ABORT(edm4hep::ParticleIDCollection, "recos_PID_algo3");

return 0;
}
26 changes: 16 additions & 10 deletions tests/src/CompareEDM4hepEDM4hep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "edm4hep/TrackerHitPlaneCollection.h"
#include "edm4hep/ClusterCollection.h"
#include "edm4hep/ReconstructedParticleCollection.h"
#include "edm4hep/ParticleIDCollection.h"

#include <edm4hep/TrackState.h>
#include <iostream>
Expand Down Expand Up @@ -321,17 +322,22 @@ bool compare(
relParticles[iP].getObjectID(),
"related particle " << iP << " in reco particle " << i);
}

const auto origRelPIDs = origReco.getParticleIDs();
const auto relPIDs = reco.getParticleIDs();
REQUIRE_SAME(origRelPIDs.size(), relPIDs.size(), "number of related ParticleIDs in reco particle" << i);
for (size_t iP = 0; iP < relPIDs.size(); ++iP) {
REQUIRE_SAME(
origRelPIDs[iP].getObjectID(),
relPIDs[iP].getObjectID(),
"related ParticleID " << iP << " in reco particle " << i);
}
}

return true;
}

bool compare(const edm4hep::ParticleIDCollection& origColl, const edm4hep::ParticleIDCollection& roundtripColl)
{
REQUIRE_SAME(origColl.size(), roundtripColl.size(), "collection sizes");
for (size_t i = 0; i < origColl.size(); ++i) {
const auto origPid = origColl[i];
const auto pid = roundtripColl[i];

REQUIRE_SAME(origPid.getAlgorithmType(), pid.getAlgorithmType(), "algorithm type in ParticleID " << i);

REQUIRE_SAME(
origPid.getParticle().getObjectID(), pid.getParticle().getObjectID(), "related particle in ParticleID " << i);
}
return true;
}
2 changes: 2 additions & 0 deletions tests/src/CompareEDM4hepEDM4hep.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ bool compare(
const edm4hep::ReconstructedParticleCollection& origColl,
const edm4hep::ReconstructedParticleCollection& roundtripColl);

bool compare(const edm4hep::ParticleIDCollection& origColl, const edm4hep::ParticleIDCollection& roundtripColl);

#endif // K4EDM4HEP2LCIOCONV_TEST_COMPAREEDM4HEPEDM4HEP_H
59 changes: 38 additions & 21 deletions tests/src/EDM4hep2LCIOUtilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,15 @@ edm4hep::ClusterCollection createClusters(
return clusterColl;
}

std::tuple<edm4hep::ReconstructedParticleCollection, edm4hep::ParticleIDCollection> createRecoParticles(
edm4hep::ReconstructedParticleCollection createRecoParticles(
const int nRecos,
const edm4hep::TrackCollection& tracks,
const std::vector<test_config::IdxPair>& trackIdcs,
const edm4hep::ClusterCollection& clusters,
const std::vector<test_config::IdxPair>& clusterIdcs,
const std::vector<test_config::IdxPair>& recIdcs,
const std::vector<test_config::IdxPair>& pidAlgTypes)
const std::vector<test_config::IdxPair>& recIdcs)
{
auto recoColl = edm4hep::ReconstructedParticleCollection {};
auto pidColl = edm4hep::ParticleIDCollection {};
for (int i = 0; i < nRecos; ++i) {
auto reco = recoColl.create();
reco.setCharge(1.23f);
Expand All @@ -355,13 +353,28 @@ std::tuple<edm4hep::ReconstructedParticleCollection, edm4hep::ParticleIDCollecti
recoColl[fromIdx].addToParticles(recoColl[toIdx]);
}

for (const auto& [recIdx, pidAlgType] : pidAlgTypes) {
auto pid = pidColl.create();
pid.setAlgorithmType(pidAlgType);
recoColl[recIdx].addToParticleIDs(pid);
return recoColl;
}

std::vector<edm4hep::ParticleIDCollection> createParticleIDs(
const std::vector<std::vector<int>>& recoIdcs,
const edm4hep::ReconstructedParticleCollection& recoParticles)
{
std::vector<edm4hep::ParticleIDCollection> collections;
collections.reserve(recoIdcs.size());

int algoId = 0;
for (const auto& idcs : recoIdcs) {
algoId++;
auto& coll = collections.emplace_back();
for (const auto idx : idcs) {
auto pid = coll.create();
pid.setAlgorithmType(algoId);
pid.setParticle(recoParticles[idx]);
}
}

return {std::move(recoColl), std::move(pidColl)};
return collections;
}

podio::Frame createExampleEvent()
Expand Down Expand Up @@ -401,18 +414,22 @@ podio::Frame createExampleEvent()
event.put(std::move(tmpSimCaloHits), "simCaloHits");
event.put(std::move(tmpCaloHitConts), "caloHitContributions");

auto [recColl, pidColl] = createRecoParticles(
test_config::nRecoParticles,
tracks,
test_config::recoTrackIdcs,
clusters,
test_config::recoClusterIdcs,
test_config::recoRecoIdcs,
test_config::recoPIDTypes);

event.put(std::move(recColl), "recos");
// Make sure to use the same name as is generated for the LCIO to EDM4hep conversion
event.put(std::move(pidColl), "recos_particleIDs");
const auto& recoColl = event.put(
createRecoParticles(
test_config::nRecoParticles,
tracks,
test_config::recoTrackIdcs,
clusters,
test_config::recoClusterIdcs,
test_config::recoRecoIdcs),
"recos");

int algoId = 1;
for (auto& pidColl : createParticleIDs(test_config::pidRecoIdcs, recoColl)) {
// Make sure to use the same name as is generated for the LCIO to EDM4hep
// conversion
event.put(std::move(pidColl), "recos_PID_algo" + std::to_string(algoId++));
}

return event;
}
16 changes: 8 additions & 8 deletions tests/src/EDM4hep2LCIOUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ namespace test_config {
/// particle
const static std::vector<IdxPair> recoRecoIdcs = {{0, 3}, {2, 2}, {5, 1}, {5, 0}, {1, 2}, {2, 4}};

/// The ParticleIDs algorithmType that will be create for some reco particles.
/// The first index is the reco paritcle to which a ParticleID with algorithm
/// type of the second index will be attached
const static std::vector<IdxPair> recoPIDTypes = {{0, 1}, {0, 2}, {1, 1}, {2, 1}, {2, 2}, {3, 1}, {4, 2}};

/// The number of entries for the generated ParticleID collections
const static std::vector<std::vector<int>> pidRecoIdcs = {{1, 3, 4}, {2, 3}, {0, 1, 2, 3, 4, 5}};
} // namespace test_config

/**
Expand Down Expand Up @@ -176,14 +173,17 @@ edm4hep::ClusterCollection createClusters(
const std::vector<test_config::IdxPair>& clusterHitIdcs,
const std::vector<test_config::IdxPair>& clusterClusterIdcs);

std::tuple<edm4hep::ReconstructedParticleCollection, edm4hep::ParticleIDCollection> createRecoParticles(
edm4hep::ReconstructedParticleCollection createRecoParticles(
const int nRecos,
const edm4hep::TrackCollection& tracks,
const std::vector<test_config::IdxPair>& trackIdcs,
const edm4hep::ClusterCollection& clusters,
const std::vector<test_config::IdxPair>& clusterIdcs,
const std::vector<test_config::IdxPair>& recIdcs,
const std::vector<test_config::IdxPair>& pidAlgTypes);
const std::vector<test_config::IdxPair>& recIdcs);

std::vector<edm4hep::ParticleIDCollection> createParticleIDs(
const std::vector<std::vector<int>>& recoIdcs,
const edm4hep::ReconstructedParticleCollection& recoParticles);

/**
* Create an example event that can be used to test the converter.
Expand Down

0 comments on commit bc4f8c8

Please sign in to comment.