Skip to content

Commit

Permalink
Add the conversion of TrackerHitPlane from EDM4hep to LCIO (#42)
Browse files Browse the repository at this point in the history
* Add a conversion for TrackerHitPlane from EDM4hep to LCIO

* Add TrackerHitPlane to roundtrip tests
  • Loading branch information
tmadlener authored Nov 30, 2023
1 parent 0835064 commit b901528
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <edm4hep/SimCalorimeterHitCollection.h>
#include <edm4hep/SimTrackerHitCollection.h>
#include <edm4hep/RawTimeSeriesCollection.h>

#include <edm4hep/TrackCollection.h>
#include <edm4hep/TrackerHitCollection.h>
#include <edm4hep/TrackerHitPlaneCollection.h>
Expand All @@ -45,6 +44,7 @@
#include <IMPL/TrackImpl.h>
#include <IMPL/TrackStateImpl.h>
#include <IMPL/TrackerHitImpl.h>
#include <IMPL/TrackerHitPlaneImpl.h>
#include <IMPL/VertexImpl.h>
#include <LCIOSTLTypes.h>
#include <UTIL/CellIDEncoder.h>
Expand All @@ -67,6 +67,7 @@ namespace EDM4hep2LCIOConv {
struct CollectionsPairVectors {
ObjectMapT<lcio::TrackImpl*, edm4hep::Track> tracks {};
ObjectMapT<lcio::TrackerHitImpl*, edm4hep::TrackerHit> trackerHits {};
ObjectMapT<lcio::TrackerHitPlaneImpl*, edm4hep::TrackerHitPlane> trackerHitPlanes {};
ObjectMapT<lcio::SimTrackerHitImpl*, edm4hep::SimTrackerHit> simTrackerHits {};
ObjectMapT<lcio::CalorimeterHitImpl*, edm4hep::CalorimeterHit> caloHits {};
ObjectMapT<lcio::RawCalorimeterHitImpl*, edm4hep::RawCalorimeterHit> rawCaloHits {};
Expand All @@ -90,6 +91,12 @@ namespace EDM4hep2LCIOConv {
const std::string& cellIDstr,
TrackerHitMapT& trackerhits_vec);

template<typename TrackerHitPlaneMapT>
lcio::LCCollectionVec* convTrackerHitPlanes(
const edm4hep::TrackerHitPlaneCollection* const trackerhits_coll,
const std::string& cellIDstr,
TrackerHitPlaneMapT& trackerhits_vec);

template<typename SimTrHitMapT, typename MCParticleMapT>
lcio::LCCollectionVec* convSimTrackerHits(
const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ namespace EDM4hep2LCIOConv {
lcio_trh->setEDepError(edm_trh.getEDepError());
lcio_trh->setTime(edm_trh.getTime());
lcio_trh->setQuality(edm_trh.getQuality());
std::bitset<sizeof(uint32_t)> type_bits = edm_trh.getQuality();
for (int j = 0; j < sizeof(uint32_t); j++) {
lcio_trh->setQualityBit(j, (type_bits[j] == 0) ? 0 : 1);
}

// Save intermediate trackerhits ref
k4EDM4hep2LcioConv::detail::mapInsert(lcio_trh, edm_trh, trackerhits_vec);
Expand All @@ -144,6 +140,52 @@ namespace EDM4hep2LCIOConv {
return trackerhits;
}

template<typename TrackerHitPlaneMapT>
lcio::LCCollectionVec* convTrackerHitPlanes(
const edm4hep::TrackerHitPlaneCollection* const trackerhits_coll,
const std::string& cellIDstr,
TrackerHitPlaneMapT& trackerhits_vec)
{
auto* trackerHitPlanes = new lcio::LCCollectionVec(lcio::LCIO::TRACKERHITPLANE);

if (cellIDstr != "") {
lcio::CellIDEncoder<lcio::SimCalorimeterHitImpl> idEnc(cellIDstr, trackerHitPlanes);
}

for (const auto& edm_trh : (*trackerhits_coll)) {
if (edm_trh.isAvailable()) {
auto* lcio_trh = new lcio::TrackerHitPlaneImpl();

uint64_t combined_value = edm_trh.getCellID();
uint32_t* combined_value_ptr = reinterpret_cast<uint32_t*>(&combined_value);
lcio_trh->setCellID0(combined_value_ptr[0]);
lcio_trh->setCellID1(combined_value_ptr[1]);
lcio_trh->setType(edm_trh.getType());
const std::array positions {edm_trh.getPosition()[0], edm_trh.getPosition()[1], edm_trh.getPosition()[2]};
lcio_trh->setPosition(positions.data());
// No public setter in LCIO
// lcio_trh->setCovMatrix(edm_trh.getCovMatrix().data());
lcio_trh->setEDep(edm_trh.getEDep());
lcio_trh->setEDepError(edm_trh.getEDepError());
lcio_trh->setTime(edm_trh.getTime());
lcio_trh->setQuality(edm_trh.getQuality());

const std::array posU {edm_trh.getU()[0], edm_trh.getU()[1]};
lcio_trh->setU(posU.data());
lcio_trh->setdU(edm_trh.getDu());
const std::array posV {edm_trh.getV()[0], edm_trh.getV()[1]};
lcio_trh->setV(posV.data());
lcio_trh->setdV(edm_trh.getDv());

k4EDM4hep2LcioConv::detail::mapInsert(lcio_trh, edm_trh, trackerhits_vec);

trackerHitPlanes->addElement(lcio_trh);
}
}

return trackerHitPlanes;
}

// Convert EDM4hep SimTrackerHits to LCIO
// Add converted LCIO ptr and original EDM4hep collection to vector of pairs
// Add LCIO Collection Vector to LCIO event
Expand Down
6 changes: 5 additions & 1 deletion k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ namespace EDM4hep2LCIOConv {
auto lcColl = convTrackerHits(coll, cellIDStr, objectMappings.trackerHits);
lcioEvent->addCollection(lcColl, name);
}
else if (auto coll = dynamic_cast<const edm4hep::TrackerHitPlaneCollection*>(edmCollection)) {
auto lcColl = convTrackerHitPlanes(coll, cellIDStr, objectMappings.trackerHitPlanes);
lcioEvent->addCollection(lcColl, name);
}
else if (auto coll = dynamic_cast<const edm4hep::SimTrackerHitCollection*>(edmCollection)) {
auto lcColl = convSimTrackerHits(coll, cellIDStr, objectMappings.simTrackerHits, objectMappings.mcParticles);
lcioEvent->addCollection(lcColl, name);
Expand Down Expand Up @@ -93,7 +97,7 @@ namespace EDM4hep2LCIOConv {
std::cerr << "Error trying to convert requested " << edmCollection->getValueTypeName() << " with name " << name
<< "\n"
<< "List of supported types: "
<< "Track, TrackerHit, SimTrackerHit, "
<< "Track, TrackerHit, TrackerHitPlane, SimTrackerHit, "
<< "Cluster, CalorimeterHit, RawCalorimeterHit, "
<< "SimCalorimeterHit, Vertex, ReconstructedParticle, "
<< "MCParticle." << std::endl;
Expand Down
1 change: 1 addition & 0 deletions tests/edm4hep_roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ int main()
ASSERT_SAME_OR_ABORT(edm4hep::SimCalorimeterHitCollection, "simCaloHits");
ASSERT_SAME_OR_ABORT(edm4hep::TrackCollection, "tracks");
ASSERT_SAME_OR_ABORT(edm4hep::TrackerHitCollection, "trackerHits");
ASSERT_SAME_OR_ABORT(edm4hep::TrackerHitPlaneCollection, "trackerHitPlanes");
ASSERT_SAME_OR_ABORT(edm4hep::ClusterCollection, "clusters");

return 0;
Expand Down
27 changes: 27 additions & 0 deletions tests/src/CompareEDM4hepEDM4hep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "edm4hep/SimCalorimeterHitCollection.h"
#include "edm4hep/TrackCollection.h"
#include "edm4hep/TrackerHitCollection.h"
#include "edm4hep/TrackerHitPlaneCollection.h"
#include "edm4hep/ClusterCollection.h"

#include <edm4hep/TrackState.h>
Expand Down Expand Up @@ -218,6 +219,32 @@ bool compare(const edm4hep::TrackerHitCollection& origColl, const edm4hep::Track
return true;
}

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

REQUIRE_SAME(origHit.getCellID(), hit.getCellID(), "cellID in hit " << i);
REQUIRE_SAME(origHit.getType(), hit.getType(), "type in hit " << i);
REQUIRE_SAME(origHit.getQuality(), hit.getQuality(), "quality in hit " << i);
REQUIRE_SAME(origHit.getTime(), hit.getTime(), "time in hit " << i);
REQUIRE_SAME(origHit.getEDep(), hit.getEDep(), "EDep in hit " << i);
REQUIRE_SAME(origHit.getEDepError(), hit.getEDepError(), "EDepError in hit " << i);
REQUIRE_SAME(origHit.getPosition(), hit.getPosition(), "Position in hit " << i);
REQUIRE_SAME(origHit.getCovMatrix(), hit.getCovMatrix(), "CovMatrix in hit " << i);
REQUIRE_SAME(origHit.getU(), hit.getU(), "U in hit " << i);
REQUIRE_SAME(origHit.getV(), hit.getV(), "V in hit " << i);
REQUIRE_SAME(origHit.getDu(), hit.getDu(), "Du in hit " << i);
REQUIRE_SAME(origHit.getDv(), hit.getDv(), "Dv in hit " << i);
}

return true;
}

bool compare(const edm4hep::ClusterCollection& origColl, const edm4hep::ClusterCollection& roundtripColl)
{
REQUIRE_SAME(origColl.size(), roundtripColl.size(), "collection sizes");
Expand Down
4 changes: 4 additions & 0 deletions tests/src/CompareEDM4hepEDM4hep.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ bool compare(const edm4hep::TrackCollection& origColl, const edm4hep::TrackColle

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

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

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

#endif // K4EDM4HEP2LCIOCONV_TEST_COMPAREEDM4HEPEDM4HEP_H
24 changes: 24 additions & 0 deletions tests/src/EDM4hep2LCIOUtilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "edm4hep/RawCalorimeterHitCollection.h"
#include "edm4hep/SimCalorimeterHitCollection.h"
#include "edm4hep/TrackerHitCollection.h"
#include <edm4hep/TrackerHitPlaneCollection.h>
#include <edm4hep/EventHeaderCollection.h>
#include <edm4hep/RawTimeSeriesCollection.h>
#include "edm4hep/TrackCollection.h"
Expand Down Expand Up @@ -160,6 +161,28 @@ edm4hep::TrackerHitCollection createTrackerHits(const int num_elements)
return coll;
}

edm4hep::TrackerHitPlaneCollection createTrackerHitPlanes(const int num_elements)
{
edm4hep::TrackerHitPlaneCollection coll {};
for (int i = 0; i < num_elements; ++i) {
auto elem = coll.create();
elem.setCellID(createCellID(i));
elem.setType(i * 42 + 123);
elem.setQuality(i + 42);
elem.setTime(0.1f * i);
elem.setEDep(1.0f * i);
elem.setEDepError(2.0f * i);
elem.setU({1.2f * i, 2.1f * i});
elem.setV({3.4f * i, 4.3f * i});
elem.setDu(0.25f * i);
elem.setDv(0.5f * i);
// Covariance matrix not handled by conversion
// elem.setCovMatrix(createCov<3>());
}

return coll;
}

edm4hep::TrackCollection createTracks(
const int num_elements,
const int subdetectorhitnumbers,
Expand Down Expand Up @@ -305,6 +328,7 @@ podio::Frame createExampleEvent()
const auto& rawCaloHits = event.put(createRawCalorimeterHits(test_config::nRawCaloHits), "rawCaloHits");
const auto& tpcHits = event.put(createTPCHits(test_config::nTPCHits, test_config::nTPCRawWords), "tpcHits");
const auto& trackerHits = event.put(createTrackerHits(test_config::nTrackerHits), "trackerHits");
const auto& trackerHitPlanes = event.put(createTrackerHitPlanes(test_config::nTrackerHits), "trackerHitPlanes");
const auto& tracks = event.put(
createTracks(
test_config::nTracks,
Expand Down
1 change: 1 addition & 0 deletions tests/src/EDM4hep2LCIOUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace edm4hep {
class RawCalorimeterHitCollection;
class RawTimeSeriesCollection;
class TrackerHitCollection;
class TrackerHitPlaneCollection;
class TrackCollection;
class SimCalorimeterHitCollection;
class CaloHitContributionCollection;
Expand Down

0 comments on commit b901528

Please sign in to comment.