From 07ab63c81d187eb689ecf4542537d87620c4a3e5 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 22 Feb 2023 16:57:15 +0100 Subject: [PATCH 01/83] Add LCIO to EDM4hep conversion functionality --- k4EDM4hep2LcioConv/CMakeLists.txt | 4 +- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 394 +++++++ k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 985 ++++++++++++++++++ 3 files changed, 1382 insertions(+), 1 deletion(-) create mode 100644 k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h create mode 100644 k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp diff --git a/k4EDM4hep2LcioConv/CMakeLists.txt b/k4EDM4hep2LcioConv/CMakeLists.txt index 2eee9981..2b036ede 100644 --- a/k4EDM4hep2LcioConv/CMakeLists.txt +++ b/k4EDM4hep2LcioConv/CMakeLists.txt @@ -1,5 +1,7 @@ add_library(k4EDM4hep2LcioConv SHARED - src/k4EDM4hep2LcioConv.cpp) + src/k4EDM4hep2LcioConv.cpp + src/k4Lcio2EDM4hepConv.cpp + ) target_include_directories(k4EDM4hep2LcioConv PUBLIC ${LCIO_INCLUDE_DIRS} diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h new file mode 100644 index 00000000..d7137ac3 --- /dev/null +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -0,0 +1,394 @@ +// EDM4hep +#include "edm4hep/CaloHitContributionCollection.h" +#include "edm4hep/CalorimeterHitCollection.h" +#include "edm4hep/ClusterCollection.h" +#include "edm4hep/EventHeaderCollection.h" +#include "edm4hep/MCParticleCollection.h" +#include "edm4hep/MCRecoCaloAssociationCollection.h" +#include "edm4hep/MCRecoCaloParticleAssociationCollection.h" +#include "edm4hep/MCRecoClusterParticleAssociationCollection.h" +#include "edm4hep/MCRecoParticleAssociationCollection.h" +#include "edm4hep/MCRecoTrackParticleAssociationCollection.h" +#include "edm4hep/MCRecoTrackerAssociationCollection.h" +#include "edm4hep/MCRecoTrackerHitPlaneAssociationCollection.h" +#include "edm4hep/ParticleIDCollection.h" +#include "edm4hep/RawCalorimeterHitCollection.h" +#include "edm4hep/RecoParticleVertexAssociationCollection.h" +#include "edm4hep/ReconstructedParticleCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" +#include "edm4hep/SimTrackerHitCollection.h" +#include "edm4hep/TPCHitCollection.h" +#include "edm4hep/TrackCollection.h" +#include "edm4hep/TrackerHitCollection.h" +#include "edm4hep/TrackerHitPlaneCollection.h" +#include "edm4hep/VertexCollection.h" + +// LCIO +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "podio/Frame.h" + +#include +#include +#include +#include +#include + +namespace LCIO2EDM4hepConv { + template + using TypeMapT = std::unordered_map; + + /** + * Maping holding all the original and converted objects in a 1:1 mapping in a + * way that makes the lookup from LCIO to EDM4hep easy. + */ + struct LcioEdmTypeMapping { + TypeMapT tracks {}; + TypeMapT trackerhits {}; + TypeMapT simtrackerhits {}; + TypeMapT calohits {}; + TypeMapT rawcalohits {}; + TypeMapT simcalohits {}; + TypeMapT tpchits {}; + TypeMapT clusters {}; + TypeMapT vertices {}; + TypeMapT recoparticles {}; + TypeMapT mcparticles {}; + TypeMapT trackerhitplane {}; + }; + + using CollNamePair = std::tuple>; + + /** + * Convert a complete LCEvent from LCIO to EDM4hep + */ + podio::Frame convertEvent(EVENT::LCEvent* evt); + + /** + * Convert an LCIOCollection by dispatching to the specific conversion + * function for the corresponding type (after querying the input collection). + * Populates the correct object mapping along the way. + * + * Returns a vector of names and collections (since some LCIO collections will + * result in more than one EDM4hep collection) + */ + std::vector + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping); + + /** + * Resolve all relations in all converted objects that are held in the map. + * Dispatch to the correpsonding implementation for all the types that have + * relations + */ + void resolveRelations(LcioEdmTypeMapping& typeMapping); + + /** + * Convert LCRelation collections into the corresponding Association collections in EDM4hep + */ + std::vector createAssociations( + const LcioEdmTypeMapping& typeMapping, + const std::vector>& LCRelation); + + /** + * Convert a subset collection, dispatching to the correct function for the + * type of the input collection + */ + std::unique_ptr + fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type); + + inline edm4hep::Vector3f Vector3fFrom(const double* v) { return edm4hep::Vector3f(v[0], v[1], v[2]); } + + inline edm4hep::Vector3f Vector3fFrom(const EVENT::FloatVec& v) { return edm4hep::Vector3f(v[0], v[1], v[2]); } + + /** + * Convert a TrackState + */ + edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState); + + /** + * Convert an MCParticle collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertMCParticle( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& mcparticlesMap); + + /** + * Convert a ReconstructedParticle collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertReconstructedParticle( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& recoparticlesMap); + + /** + * Convert a Vertex collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertVertex( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& vertexMap); + + /** + * Convert a SimTrackerHit collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertSimTrackerHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimTrHitMap); + + /** + * Convert a TPCHit collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertTPCHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TPCHitMap); + + /** + * Convert a TrackerHit collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertTrackerHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitMap); + + /** + * Convert a TrackerHitPlane collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertTrackerHitPlane( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitPlaneMap); + + /** + * Convert a Track collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertTrack( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackMap); + + /** + * Convert a SimCalorimeterHit collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + * + * NOTE: Returns two collections, since the contributions are a separate + * CaloHitContributions are a separate collection in EDM4hep. The name for the + * contributions is simply _contributions + */ + std::vector convertSimCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimCaloHitMap); + + /** + * Convert a RawCalorimeterHit collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertRawCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& rawCaloHitMap); + + /** + * Convert a CalorimeterHit collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& caloHitMap); + + /** + * Convert a ParticleID collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertParticleID( + const std::string& name, + EVENT::LCCollection* LCCollection); + + /** + * Convert a Cluster collection and return the resulting collection. + * Simultaneously populates the mapping from LCIO to EDM4hep objects. + */ + std::unique_ptr convertCluster( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& clusterMap); + + /** + * Helper function to create a subset collection from an existing (LCIO) + * subset collection. Needs the object mapping as input to resolve to the + * correct EDM4hep object for a given LCIO object. + * + * NOTE: Users responsibility to call this with the right inputs (i.e. + * matching types) + */ + template + auto handleSubsetColl(EVENT::LCCollection* lcioColl, const TypeMapT& elemMap) + { + auto edm4hepColl = std::make_unique(); + edm4hepColl->setSubsetCollection(); + + UTIL::LCIterator lcioIter(lcioColl); + while (const auto lcioElem = lcioIter.next()) { + const auto it = elemMap.find(lcioElem); + if (it != elemMap.end()) { + edm4hepColl->push_back(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep object for an LCIO object in a subset collection" << std::endl; + } + } + + return edm4hepColl; + } + + namespace detail { + /// Helper function for generic map lookup + template + std::optional mapLookup(const LCIOT* elem, const TypeMapT& map) + { + if (const auto& it = map.find(elem); it != map.end()) { + return std::optional(it->second); + } + return std::nullopt; + } + } // namespace detail + + /** + * Create an Association collection from an LCRelations collection. Templated + * on the From and To types as well as the direction of the relations in the + * input LCRelations collection with respect to the order in which they are + * mentioned in the Association collection of EDM4hep (since those are not + * directed). + * + * Necessary inputs apart from the LCRelations collection are the correct LCIO + * to EDM4hep object mappings to actually resolve the necessary relations. + */ + template< + typename CollT, + bool Reverse, + typename FromLCIOT, + typename ToLCIOT, + typename FromEDM4hepT, + typename ToEDM4hepT> + std::unique_ptr createAssociationCollection( + EVENT::LCCollection* relations, + const TypeMapT& fromMap, + const TypeMapT& toMap) + { + auto assocColl = std::make_unique(); + auto relIter = UTIL::LCIterator(relations); + + while (const auto rel = relIter.next()) { + auto assoc = assocColl->create(); + assoc.setWeight(rel->getWeight()); + const auto lcioTo = static_cast(rel->getTo()); + const auto lcioFrom = static_cast(rel->getFrom()); + const auto edm4hepTo = detail::mapLookup(lcioTo, toMap); + const auto edm4hepFrom = detail::mapLookup(lcioFrom, fromMap); + if (edm4hepTo.has_value() && edm4hepFrom.has_value()) { + if constexpr (Reverse) { + if constexpr (std::is_same_v) { + assoc.setVertex(*edm4hepTo); + } + else { + assoc.setSim(*edm4hepTo); + } + assoc.setRec(*edm4hepFrom); + } + else { + if constexpr (std::is_same_v) { + assoc.setVertex(*edm4hepFrom); + } + else { + assoc.setSim(*edm4hepFrom); + } + assoc.setRec(*edm4hepTo); + } + } + } + + return assocColl; + } + + /** + * Resolve the relations for the MCParticles. + */ + void resolveRelationsMCParticle(TypeMapT& mcparticlesMap); + + /** + * Resolve the relations for SimTrackerHits + */ + void resolveRelationsSimTrackerHit( + TypeMapT& SimTrHitMap, + const TypeMapT& mcparticlesMap); + + /** + * Resolve the relations for ReconstructedParticles + */ + void resolveRelationsRecoParticle( + TypeMapT& recoparticlesMap, + const TypeMapT& vertexMap, + const TypeMapT& clusterMap, + const TypeMapT& tracksMap); + + /** + * Resolve the relations for SimCalorimeterHits + * + * TODO: Handle the Contributions correctly + */ + void resolveRelationsSimCalorimeterHit( + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap); + + /** + * Resolve the relations for Clusters + */ + void resolveRelationsCluster( + TypeMapT& clustersMap, + const TypeMapT& caloHitMap); + + /** + * Resolve the relations for Tracks + */ + void resolveRelationsTrack( + TypeMapT& tracksMap, + const TypeMapT& trackerHitMap); + + /** + * Resolve the relations for Vertices + */ + void resolveRelationsVertex( + TypeMapT& vertexMap, + const TypeMapT& recoparticleMap); + +} // namespace LCIO2EDM4hepConv diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp new file mode 100644 index 00000000..42096a15 --- /dev/null +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -0,0 +1,985 @@ +#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" + +#include + +namespace LCIO2EDM4hepConv { + edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState) + { + auto edmtrackState = edm4hep::TrackState {}; + edmtrackState.location = trackState->getLocation(); + edmtrackState.D0 = trackState->getD0(); + edmtrackState.phi = trackState->getPhi(); + edmtrackState.omega = trackState->getOmega(); + edmtrackState.Z0 = trackState->getZ0(); + edmtrackState.tanLambda = trackState->getTanLambda(); + // not available in lcio + edmtrackState.time = -1; + auto refPoint = trackState->getReferencePoint(); + edmtrackState.referencePoint = Vector3fFrom({refPoint[0], refPoint[1], refPoint[2]}); + auto& covMatrix = trackState->getCovMatrix(); + edmtrackState.covMatrix = { + covMatrix[0], + covMatrix[1], + covMatrix[2], + covMatrix[3], + covMatrix[4], + covMatrix[5], + covMatrix[6], + covMatrix[7], + covMatrix[8], + covMatrix[9], + covMatrix[10], + covMatrix[11], + covMatrix[12], + covMatrix[13], + covMatrix[15], + 0, + 0, + 0, + 0, + 0, + 0}; + + return edmtrackState; + } + + std::unique_ptr convertMCParticle( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& mcparticlesMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setPDG(rval->getPDG()); + lval.setGeneratorStatus(rval->getGeneratorStatus()); + lval.setSimulatorStatus(rval->getSimulatorStatus()); + lval.setCharge(rval->getCharge()); + lval.setTime(rval->getTime()); + lval.setMass(rval->getMass()); + lval.setSpin(edm4hep::Vector3f(rval->getSpin())); + lval.setColorFlow(edm4hep::Vector2i(rval->getColorFlow())); + lval.setVertex(edm4hep::Vector3d(rval->getVertex())); + lval.setEndpoint(edm4hep::Vector3d(rval->getEndpoint())); + lval.setMomentum(Vector3fFrom(rval->getMomentum())); + lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); + + const auto [iterator, inserted] = mcparticlesMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element" << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertReconstructedParticle( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& recoparticlesMap) + { + + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setCharge(rval->getCharge()); + auto& m = rval->getCovMatrix(); // 10 parameters + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); + lval.setEnergy(rval->getEnergy()); + lval.setGoodnessOfPID(rval->getGoodnessOfPID()); + lval.setMass(rval->getMass()); + lval.setMomentum(Vector3fFrom(rval->getMomentum())); + lval.setReferencePoint(rval->getReferencePoint()); + lval.setType(rval->getType()); + + const auto [iterator, inserted] = recoparticlesMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertVertex( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& vertexMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary + lval.setChi2(rval->getChi2()); + lval.setProbability(rval->getProbability()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getCovMatrix(); // 6 parameters + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + // FIXME: the algorithm type in LCIO is a string, but an integer is expected + // lval.setAlgorithmType(rval->getAlgorithmType()); + // lval.setAssociatedParticle(); //set it when convert ReconstructedParticle + // + for (auto v : rval->getParameters()) { + lval.addToParameters(v); + } + + const auto [iterator, inserted] = vertexMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertSimTrackerHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimTrHitMap) + { + + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEDep(rval->getEDep()); + lval.setTime(rval->getTime()); + lval.setPathLength(rval->getPathLength()); + lval.setQuality(rval->getQuality()); + lval.setPosition(rval->getPosition()); + lval.setMomentum(rval->getMomentum()); + + const auto [iterator, inserted] = SimTrHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertTPCHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TPCHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setCellID(rval->getCellID()); + lval.setTime(rval->getTime()); + lval.setCharge(rval->getCharge()); + lval.setQuality(rval->getQuality()); + for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { + lval.addToRawDataWords(rval->getRawDataWord(j)); + } + const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertTrackerHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setType(rval->getType()); + lval.setQuality(rval->getQuality()); + lval.setTime(rval->getTime()); + lval.setEDep(rval->getEDep()); + lval.setEDepError(rval->getEDepError()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getCovMatrix(); + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + + const auto [iterator, inserted] = TrackerHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertTrackerHitPlane( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitPlaneMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setType(rval->getType()); + lval.setQuality(rval->getQuality()); + lval.setTime(rval->getTime()); + lval.setEDep(rval->getEDep()); + lval.setEDepError(rval->getEDepError()); + lval.setPosition(rval->getPosition()); + lval.setU({rval->getU()[0], rval->getU()[1]}); + lval.setV({rval->getV()[0], rval->getV()[1]}); + lval.setDu(rval->getdU()); + lval.setDv(rval->getdV()); + auto& m = rval->getCovMatrix(); + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + + const auto [iterator, inserted] = TrackerHitPlaneMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertTrack( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setType(rval->getType()); + lval.setChi2(rval->getChi2()); + lval.setNdf(rval->getNdf()); + lval.setDEdx(rval->getdEdx()); + lval.setDEdxError(rval->getdEdxError()); + lval.setRadiusOfInnermostHit(rval->getRadiusOfInnermostHit()); + + auto subdetectorHitNum = rval->getSubdetectorHitNumbers(); + for (auto hitNum : subdetectorHitNum) { + lval.addToSubDetectorHitNumbers(hitNum); + } + auto& trackStates = rval->getTrackStates(); + for (auto& trackState : trackStates) { + lval.addToTrackStates(convertTrackState(trackState)); + } + auto quantities = edm4hep::Quantity {}; + quantities.value = rval->getdEdx(); + quantities.error = rval->getdEdxError(); + lval.addToDxQuantities(quantities); + const auto [iterator, inserted] = TrackMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::vector convertSimCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimCaloHitMap) + { + auto dest = std::make_unique(); + auto contr = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEnergy(rval->getEnergy()); + lval.setPosition(rval->getPosition()); + + auto NMCParticle = rval->getNMCParticles(); + for (unsigned j = 0; j < NMCParticle; j++) { + auto edm_contr = contr->create(); + + edm_contr.setPDG(rval->getPDGCont(j)); + edm_contr.setTime(rval->getTimeCont(j)); + edm_contr.setEnergy(rval->getEnergyCont(j)); + edm_contr.setStepPosition(rval->getStepPosition(j)); + + lval.addToContributions(edm_contr); + } + const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + std::vector results; + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_contribution", std::move(contr)); + + return results; + } + + std::unique_ptr convertRawCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& rawCaloHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setAmplitude(rval->getAmplitude()); + lval.setTimeStamp(rval->getTimeStamp()); + + const auto [iterator, inserted] = rawCaloHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& caloHitMap) + { + + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEnergy(rval->getEnergy()); + lval.setEnergyError(rval->getEnergyError()); + lval.setPosition(rval->getPosition()); + lval.setTime(rval->getTime()); + lval.setType(rval->getType()); + + const auto [iterator, inserted] = caloHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertParticleID( + const std::string& name, + EVENT::LCCollection* LCCollection) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setType(rval->getType()); + lval.setPDG(rval->getPDG()); + lval.setAlgorithmType(rval->getAlgorithmType()); + lval.setLikelihood(rval->getLikelihood()); + + for (auto v : rval->getParameters()) { + lval.addToParameters(v); + } + } + + return dest; + } + + std::unique_ptr convertCluster( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& clusterMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setEnergy(rval->getEnergy()); + lval.setEnergyError(rval->getEnergyError()); + lval.setITheta(rval->getITheta()); + lval.setPhi(rval->getIPhi()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getPositionError(); + lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); + lval.setType(rval->getType()); + lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); + + const auto [iterator, inserted] = clusterMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->first; + const auto existingId = existing->id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::vector + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) + { + std::string type = LCCollection->getTypeName(); + std::vector retColls; + if (type == "MCParticle") { + retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcparticles)); + } + else if (type == "ReconstructedParticle") { + retColls.emplace_back(name, convertReconstructedParticle(name, LCCollection, typeMapping.recoparticles)); + } + else if (type == "Vertex") { + retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); + } + else if (type == "Track") { + retColls.emplace_back(name, convertTrack(name, LCCollection, typeMapping.tracks)); + } + else if (type == "Cluster") { + retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); + } + else if (type == "SimCalorimeterHit") { + return convertSimCalorimeterHit(name, LCCollection, typeMapping.simcalohits); + } + else if (type == "RawCalorimeterHit") { + retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawcalohits)); + } + else if (type == "CalorimeterHit") { + retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.calohits)); + } + else if (type == "SimTrackerHit") { + retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simtrackerhits)); + } + else if (type == "TPCHit") { + retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpchits)); + } + else if (type == "TrackerHit") { + retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerhits)); + } + else if (type == "TrackerHitPlane") { + retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerhitplane)); + } + else if (type == "ParticleID") { + retColls.emplace_back(name, convertParticleID(name, LCCollection)); + } + return retColls; + } + + podio::Frame convertEvent(EVENT::LCEvent* evt) + { + auto typeMapping = LcioEdmTypeMapping {}; + std::vector>> edmevent; + std::vector> LCRelations; + const auto& lcnames = evt->getCollectionNames(); + // In this loop the data gets converted. + for (const auto& lcioname : *lcnames) { + const auto& lcioColl = evt->getCollection(lcioname); + const auto& lciotype = lcioColl->getTypeName(); + if (lciotype == "LCRelations") { + LCRelations.push_back(std::make_pair(lcioname, lcioColl)); + // We handle Relations (aka Associations) once we have converted all the + // data parts. + continue; + } + + if (!lcioColl->isSubset()) { + for (auto&& [name, edmColl] : convertCollection(lcioname, lcioColl, typeMapping)) { + if (edmColl != nullptr) { + edmevent.emplace_back(std::move(name), std::move(edmColl)); + } + } + } + } + + // Filling of the Subset Colections + for (const auto& lcioname : *lcnames) { + + auto lcioColl = evt->getCollection(lcioname); + if (lcioColl->isSubset()) { + const auto& lciotype = lcioColl->getTypeName(); + auto edmColl = fillSubSet(lcioColl, typeMapping, lciotype); + if (edmColl != nullptr) { + edmevent.emplace_back(lcioname, std::move(edmColl)); + } + } + } + // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. + resolveRelations(typeMapping); + auto assoCollVec = createAssociations(typeMapping, LCRelations); + + podio::Frame event; + // Now everything is done and we simply populate a Frame + for (auto& [name, coll] : edmevent) { + event.put(std::move(coll), name); + } + for (auto& [name, coll] : assoCollVec) { + event.put(std::move(coll), name); + } + return event; + } + + void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) + { + + int edmnum = 1; + for (auto& [lcio, edm] : mcparticlesMap) { + edmnum++; + auto daughters = lcio->getDaughters(); + auto parents = lcio->getParents(); + + int dnum = 1; + for (auto d : daughters) { + const auto it = mcparticlesMap.find(d); + dnum++; + if (it != mcparticlesMap.end()) { + edm.addToDaughters(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " + "while trying to resolve the daughters of MCParticles" + << std::endl; + } + } + for (auto p : parents) { + const auto it = mcparticlesMap.find(p); + if (it != mcparticlesMap.end()) { + edm.addToParents(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the parents of MCParticles Collections" + << std::endl; + } + } + } + } + + void resolveRelationsSimTrackerHit( + TypeMapT& SimTrHitMap, + TypeMapT& mcparticlesMap) + { + + for (auto& [lcio, edm] : SimTrHitMap) { + auto mcps = lcio->getMCParticle(); + const auto it = mcparticlesMap.find(mcps); + if (it != mcparticlesMap.end()) { + edm.setMCParticle(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the SimTrackHit Relations" + << std::endl; + } + } + } + + void resolveRelationsRecocParticle( + TypeMapT& recoparticlesMap, + const TypeMapT& vertexMap, + const TypeMapT& clusterMap, + const TypeMapT& tracksMap) + { + + int edmnum = 1; + for (auto& [lcio, edm] : recoparticlesMap) { + edmnum++; + auto vertex = lcio->getStartVertex(); + auto clusters = lcio->getClusters(); + auto tracks = lcio->getTracks(); + auto parents = lcio->getParticles(); + + const auto it = vertexMap.find(vertex); + if (it != vertexMap.end()) { + edm.setStartVertex(it->second); + } + else { + if (it != nullptr) { + std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + for (auto c : clusters) { + const auto it = clusterMap.find(c); + if (it != clusterMap.end()) { + edm.addToClusters(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + for (auto t : tracks) { + const auto it = tracksMap.find(t); + if (it != tracksMap.end()) { + edm.addToTracks(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + for (auto p : parents) { + const auto it = recoparticlesMap.find(p); + if (it != recoparticlesMap.end()) { + edm.addToParticles(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " + "while trying to resolve the ReconstructedParticles parents Relations" + << std::endl; + } + } + } + } + + void resolveRelationsSimCalorimeterHit( + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap) + { + for (auto& [lcio, edm] : SimCaloHitMap) { + auto contributionLen = lcio->getNMCParticles(); + for (auto i = 0; i < contributionLen; i++) { + auto mcp = lcio->getParticleCont(i); + const auto it = mcparticlesMap.find(mcp); + // if (it != mcparticlesMap.end()) { + // edm.getContributions(i).setParticle(it->second); + // see https://github.com/AIDASoft/podio/issues/347 + // } + } + } + } + + void resolveRelationsCluster( + TypeMapT& clustersMap, + const TypeMapT& caloHitMap) + { + for (auto& [lcio, edm] : clustersMap) { + auto clusters = lcio->getClusters(); + auto calohits = lcio->getCalorimeterHits(); + auto shape = lcio->getShape(); + auto subdetectorEnergies = lcio->getSubdetectorEnergies(); + for (auto c : clusters) { + const auto it = clustersMap.find(c); + if (it != clustersMap.end()) { + edm.addToClusters(it->second); + } + else { + std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; + } + } + for (auto cal : calohits) { + const auto it = caloHitMap.find(cal); + if (it != caloHitMap.end()) { + edm.addToHits(it->second); + } + else { + std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; + } + } + for (auto s : shape) { + edm.addToShapeParameters(s); + } + for (auto subE : subdetectorEnergies) { + edm.addToSubdetectorEnergies(subE); + } + } + } + + void resolveRelationsTrack( + TypeMapT& tracksMap, + const TypeMapT& trackerHitMap, + const TypeMapT& TPCHitMap, + const TypeMapT& trackerhitplaneMap) + { + for (auto& [lcio, edm] : tracksMap) { + auto tracks = lcio->getTracks(); + auto trackerHits = lcio->getTrackerHits(); + for (auto t : tracks) { + const auto it = tracksMap.find(t); + if (it != tracksMap.end()) { + edm.addToTracks(it->second); + } + else { + std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; + } + } + for (auto th : trackerHits) { + const auto it = trackerHitMap.find(th); + if (it != trackerHitMap.end()) { + edm.addToTrackerHits(it->second); + } + else { + // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" + // <<" This is due to it being a TrackerHitPlane or TPCHit" << std::endl; + + /* + // This Code looks for the trackerHit in the TPCHit Map aswell as the trackerHitPlane Map. Those relations can + not be set for a track in edm4HEP. In all tests the missing trackerHits were located in either of these maps. + const auto tpchit = dynamic_cast(th); + const auto trackerhitplane = dynamic_cast(th); + if (tpchit != nullptr){ + const auto it = TPCHitMap.find(tpchit); + if (it != TPCHitMap.end()) { + std::cout << "trackerHit found in TPCHit map !" << std::endl; + } + else { + std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; + } + } + else if(trackerhitplane != nullptr){ + const auto it = trackerhitplaneMap.find(trackerhitplane); + if (it != trackerhitplaneMap.end()) { + std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; + } + else { + std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; + } + } + */ + } + } + } + } + + void resolveRelationsVertex( + TypeMapT& vertexMap, + const TypeMapT& recoparticleMap) + { + for (auto& [lcio, edm] : vertexMap) { + auto recoparticle = lcio->getAssociatedParticle(); + + const auto it = recoparticleMap.find(recoparticle); + if (it != recoparticleMap.end()) { + edm.setAssociatedParticle(it->second); + } + else { + std::cerr << "Couldn't find associated Particle to add to Vertex " + << "Relations in edm" << std::endl; + } + } + } + + void resolveRelations(LcioEdmTypeMapping& typeMapping) + { + resolveRelationsMCParticle(typeMapping.mcparticles); + resolveRelationsRecoParticle( + typeMapping.recoparticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + resolveRelationsSimTrackerHit(typeMapping.simtrackerhits, typeMapping.mcparticles); + resolveRelationsSimCalorimeterHit(typeMapping.simcalohits, typeMapping.mcparticles); + resolveRelationsCluster(typeMapping.clusters, typeMapping.calohits); + resolveRelationsTrack( + typeMapping.tracks, typeMapping.trackerhits, typeMapping.tpchits, typeMapping.trackerhitplane); + resolveRelationsVertex(typeMapping.vertices, typeMapping.recoparticles); + } + + std::vector createAssociations( + const LcioEdmTypeMapping& typeMapping, + const std::vector>& LCRelation) + { + + std::vector assoCollVec; + for (const auto [name, relations] : LCRelation) { + const auto& params = relations->getParameters(); + + const auto& fromType = params.getStringVal("FromType"); + const auto& toType = params.getStringVal("ToType"); + + if (fromType == "MCParticle" && toType == "ReconstructedParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcparticles, typeMapping.recoparticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.recoparticles, typeMapping.mcparticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.calohits, typeMapping.simcalohits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simcalohits, typeMapping.calohits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Cluster" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.clusters, typeMapping.mcparticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "MCParticle" && toType == "Cluster") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcparticles, typeMapping.clusters); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "MCParticle" && toType == "Track") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcparticles, typeMapping.tracks); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Track" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.tracks, typeMapping.mcparticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.trackerhits, typeMapping.simtrackerhits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simtrackerhits, typeMapping.trackerhits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simtrackerhits, typeMapping.trackerhitplane); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.trackerhitplane, typeMapping.simtrackerhits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "ReconstructedParticle" && toType == "Vertex") { + auto mc_a = createAssociationCollection( + relations, typeMapping.recoparticles, typeMapping.vertices); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Vertex" && toType == "reconstructedparticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.vertices, typeMapping.recoparticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + } + + return assoCollVec; + } + + std::unique_ptr + fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) + { + if (type == "MCParticle") { + const auto& map = typeMapping.mcparticles; + return handleSubsetColl(LCCollection, map); + } + else if (type == "ReconstructedParticle") { + const auto& map = typeMapping.recoparticles; + return handleSubsetColl(LCCollection, map); + } + else if (type == "Vertex") { + const auto& map = typeMapping.vertices; + return handleSubsetColl(LCCollection, map); + } + else if (type == "Track") { + const auto& map = typeMapping.tracks; + return handleSubsetColl(LCCollection, map); + } + else if (type == "Cluster") { + const auto& map = typeMapping.clusters; + return handleSubsetColl(LCCollection, map); + } + else if (type == "SimCalorimeterHit") { + const auto& map = typeMapping.simcalohits; + return handleSubsetColl(LCCollection, map); + } + else if (type == "RawCalorimeterHit") { + const auto& map = typeMapping.rawcalohits; + return handleSubsetColl(LCCollection, map); + } + else if (type == "CalorimeterHit") { + const auto& map = typeMapping.calohits; + return handleSubsetColl(LCCollection, map); + } + else if (type == "SimTrackerHit") { + const auto& map = typeMapping.simtrackerhits; + return handleSubsetColl(LCCollection, map); + } + else if (type == "TPCHit") { + const auto& map = typeMapping.tpchits; + return handleSubsetColl(LCCollection, map); + } + else if (type == "TrackerHit") { + const auto& map = typeMapping.trackerhits; + return handleSubsetColl(LCCollection, map); + } + else if (type == "TrackerHitPlane") { + const auto& map = typeMapping.trackerhitplane; + return handleSubsetColl(LCCollection, map); + } + else { + return nullptr; + } + } + +} // namespace LCIO2EDM4hepConv From 5a9d333ef35679fc2eeabe220812cdae8affb9b7 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 22 Feb 2023 19:45:07 +0100 Subject: [PATCH 02/83] Minor cleanups --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 42096a15..803f965f 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -14,9 +14,9 @@ namespace LCIO2EDM4hepConv { edmtrackState.tanLambda = trackState->getTanLambda(); // not available in lcio edmtrackState.time = -1; - auto refPoint = trackState->getReferencePoint(); + const auto refPoint = trackState->getReferencePoint(); edmtrackState.referencePoint = Vector3fFrom({refPoint[0], refPoint[1], refPoint[2]}); - auto& covMatrix = trackState->getCovMatrix(); + const auto& covMatrix = trackState->getCovMatrix(); edmtrackState.covMatrix = { covMatrix[0], covMatrix[1], @@ -82,7 +82,6 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& recoparticlesMap) { - auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { const auto* rval = static_cast(LCCollection->getElementAt(i)); @@ -149,7 +148,6 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& SimTrHitMap) { - auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -399,7 +397,6 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& caloHitMap) { - auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -485,7 +482,7 @@ namespace LCIO2EDM4hepConv { std::vector convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) { - std::string type = LCCollection->getTypeName(); + const auto& type = LCCollection->getTypeName(); std::vector retColls; if (type == "MCParticle") { retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcparticles)); @@ -532,7 +529,7 @@ namespace LCIO2EDM4hepConv { podio::Frame convertEvent(EVENT::LCEvent* evt) { auto typeMapping = LcioEdmTypeMapping {}; - std::vector>> edmevent; + std::vector edmevent; std::vector> LCRelations; const auto& lcnames = evt->getCollectionNames(); // In this loop the data gets converted. @@ -584,7 +581,6 @@ namespace LCIO2EDM4hepConv { void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) { - int edmnum = 1; for (auto& [lcio, edm] : mcparticlesMap) { edmnum++; @@ -622,7 +618,6 @@ namespace LCIO2EDM4hepConv { TypeMapT& SimTrHitMap, TypeMapT& mcparticlesMap) { - for (auto& [lcio, edm] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); const auto it = mcparticlesMap.find(mcps); @@ -643,7 +638,6 @@ namespace LCIO2EDM4hepConv { const TypeMapT& clusterMap, const TypeMapT& tracksMap) { - int edmnum = 1; for (auto& [lcio, edm] : recoparticlesMap) { edmnum++; @@ -843,9 +837,8 @@ namespace LCIO2EDM4hepConv { const LcioEdmTypeMapping& typeMapping, const std::vector>& LCRelation) { - std::vector assoCollVec; - for (const auto [name, relations] : LCRelation) { + for (const auto& [name, relations] : LCRelation) { const auto& params = relations->getParameters(); const auto& fromType = params.getStringVal("FromType"); From 402a92bc7ae541d015f0f689b816e5228097640d Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 22 Feb 2023 19:45:18 +0100 Subject: [PATCH 03/83] Add new header to PUBLIC_HEADER of target --- k4EDM4hep2LcioConv/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/CMakeLists.txt b/k4EDM4hep2LcioConv/CMakeLists.txt index 2b036ede..951e232c 100644 --- a/k4EDM4hep2LcioConv/CMakeLists.txt +++ b/k4EDM4hep2LcioConv/CMakeLists.txt @@ -14,7 +14,8 @@ target_link_libraries(k4EDM4hep2LcioConv PUBLIC set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES - PUBLIC_HEADER "include/${CMAKE_PROJECT_NAME}/k4EDM4hep2LcioConv.h") + PUBLIC_HEADER "include/${CMAKE_PROJECT_NAME}/k4EDM4hep2LcioConv.h;include/${CMAKE_PROJECT_NAME}/k4Lcio2EDM4hepConv.h" + ) install(TARGETS k4EDM4hep2LcioConv EXPORT ${CMAKE_PROJECT_NAME}Targets From 00f78f937d5ea6c555dd19fa6255b5dc1cf3c836 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 09:38:48 +0100 Subject: [PATCH 04/83] Make object mapping names easier to read --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 18 ++--- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 78 +++++++++---------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index d7137ac3..db756812 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -62,17 +62,17 @@ namespace LCIO2EDM4hepConv { */ struct LcioEdmTypeMapping { TypeMapT tracks {}; - TypeMapT trackerhits {}; - TypeMapT simtrackerhits {}; - TypeMapT calohits {}; - TypeMapT rawcalohits {}; - TypeMapT simcalohits {}; - TypeMapT tpchits {}; + TypeMapT trackerHits {}; + TypeMapT simTrackerHits {}; + TypeMapT caloHits {}; + TypeMapT rawCaloHits {}; + TypeMapT simCaloHits {}; + TypeMapT tpcHits {}; TypeMapT clusters {}; TypeMapT vertices {}; - TypeMapT recoparticles {}; - TypeMapT mcparticles {}; - TypeMapT trackerhitplane {}; + TypeMapT recoParticles {}; + TypeMapT mcParticles {}; + TypeMapT trackerHitPlanes {}; }; using CollNamePair = std::tuple>; diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 803f965f..9864d224 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -485,10 +485,10 @@ namespace LCIO2EDM4hepConv { const auto& type = LCCollection->getTypeName(); std::vector retColls; if (type == "MCParticle") { - retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcparticles)); + retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); } else if (type == "ReconstructedParticle") { - retColls.emplace_back(name, convertReconstructedParticle(name, LCCollection, typeMapping.recoparticles)); + retColls.emplace_back(name, convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles)); } else if (type == "Vertex") { retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); @@ -500,25 +500,25 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); } else if (type == "SimCalorimeterHit") { - return convertSimCalorimeterHit(name, LCCollection, typeMapping.simcalohits); + return convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits); } else if (type == "RawCalorimeterHit") { - retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawcalohits)); + retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); } else if (type == "CalorimeterHit") { - retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.calohits)); + retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.caloHits)); } else if (type == "SimTrackerHit") { - retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simtrackerhits)); + retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simTrackerHits)); } else if (type == "TPCHit") { - retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpchits)); + retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpcHits)); } else if (type == "TrackerHit") { - retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerhits)); + retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); } else if (type == "TrackerHitPlane") { - retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerhitplane)); + retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); } else if (type == "ParticleID") { retColls.emplace_back(name, convertParticleID(name, LCCollection)); @@ -822,15 +822,15 @@ namespace LCIO2EDM4hepConv { void resolveRelations(LcioEdmTypeMapping& typeMapping) { - resolveRelationsMCParticle(typeMapping.mcparticles); + resolveRelationsMCParticle(typeMapping.mcParticles); resolveRelationsRecoParticle( - typeMapping.recoparticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); - resolveRelationsSimTrackerHit(typeMapping.simtrackerhits, typeMapping.mcparticles); - resolveRelationsSimCalorimeterHit(typeMapping.simcalohits, typeMapping.mcparticles); - resolveRelationsCluster(typeMapping.clusters, typeMapping.calohits); + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); + resolveRelationsSimCalorimeterHit(typeMapping.simCaloHits, typeMapping.mcParticles); + resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); resolveRelationsTrack( - typeMapping.tracks, typeMapping.trackerhits, typeMapping.tpchits, typeMapping.trackerhitplane); - resolveRelationsVertex(typeMapping.vertices, typeMapping.recoparticles); + typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); + resolveRelationsVertex(typeMapping.vertices, typeMapping.recoParticles); } std::vector createAssociations( @@ -846,72 +846,72 @@ namespace LCIO2EDM4hepConv { if (fromType == "MCParticle" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcparticles, typeMapping.recoparticles); + relations, typeMapping.mcParticles, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.recoparticles, typeMapping.mcparticles); + relations, typeMapping.recoParticles, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.calohits, typeMapping.simcalohits); + relations, typeMapping.caloHits, typeMapping.simCaloHits); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.simcalohits, typeMapping.calohits); + relations, typeMapping.simCaloHits, typeMapping.caloHits); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "Cluster" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.clusters, typeMapping.mcparticles); + relations, typeMapping.clusters, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "MCParticle" && toType == "Cluster") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcparticles, typeMapping.clusters); + relations, typeMapping.mcParticles, typeMapping.clusters); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "MCParticle" && toType == "Track") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcparticles, typeMapping.tracks); + relations, typeMapping.mcParticles, typeMapping.tracks); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "Track" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.tracks, typeMapping.mcparticles); + relations, typeMapping.tracks, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.trackerhits, typeMapping.simtrackerhits); + relations, typeMapping.trackerHits, typeMapping.simTrackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.simtrackerhits, typeMapping.trackerhits); + relations, typeMapping.simTrackerHits, typeMapping.trackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { auto mc_a = createAssociationCollection( - relations, typeMapping.simtrackerhits, typeMapping.trackerhitplane); + relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.trackerhitplane, typeMapping.simtrackerhits); + relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "ReconstructedParticle" && toType == "Vertex") { auto mc_a = createAssociationCollection( - relations, typeMapping.recoparticles, typeMapping.vertices); + relations, typeMapping.recoParticles, typeMapping.vertices); assoCollVec.emplace_back(name, std::move(mc_a)); } else if (fromType == "Vertex" && toType == "reconstructedparticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.vertices, typeMapping.recoparticles); + relations, typeMapping.vertices, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } } @@ -923,11 +923,11 @@ namespace LCIO2EDM4hepConv { fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) { if (type == "MCParticle") { - const auto& map = typeMapping.mcparticles; + const auto& map = typeMapping.mcParticles; return handleSubsetColl(LCCollection, map); } else if (type == "ReconstructedParticle") { - const auto& map = typeMapping.recoparticles; + const auto& map = typeMapping.recoParticles; return handleSubsetColl(LCCollection, map); } else if (type == "Vertex") { @@ -943,31 +943,31 @@ namespace LCIO2EDM4hepConv { return handleSubsetColl(LCCollection, map); } else if (type == "SimCalorimeterHit") { - const auto& map = typeMapping.simcalohits; + const auto& map = typeMapping.simCaloHits; return handleSubsetColl(LCCollection, map); } else if (type == "RawCalorimeterHit") { - const auto& map = typeMapping.rawcalohits; + const auto& map = typeMapping.rawCaloHits; return handleSubsetColl(LCCollection, map); } else if (type == "CalorimeterHit") { - const auto& map = typeMapping.calohits; + const auto& map = typeMapping.caloHits; return handleSubsetColl(LCCollection, map); } else if (type == "SimTrackerHit") { - const auto& map = typeMapping.simtrackerhits; + const auto& map = typeMapping.simTrackerHits; return handleSubsetColl(LCCollection, map); } else if (type == "TPCHit") { - const auto& map = typeMapping.tpchits; + const auto& map = typeMapping.tpcHits; return handleSubsetColl(LCCollection, map); } else if (type == "TrackerHit") { - const auto& map = typeMapping.trackerhits; + const auto& map = typeMapping.trackerHits; return handleSubsetColl(LCCollection, map); } else if (type == "TrackerHitPlane") { - const auto& map = typeMapping.trackerhitplane; + const auto& map = typeMapping.trackerHitPlanes; return handleSubsetColl(LCCollection, map); } else { From 3a1b768513e1a66ce7d82e778dafaaeef6bf267e Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 09:49:50 +0100 Subject: [PATCH 05/83] Remove unnecessary temp variables --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 9864d224..641cbc3b 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -923,52 +923,40 @@ namespace LCIO2EDM4hepConv { fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) { if (type == "MCParticle") { - const auto& map = typeMapping.mcParticles; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.mcParticles); } else if (type == "ReconstructedParticle") { - const auto& map = typeMapping.recoParticles; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.recoParticles); } else if (type == "Vertex") { - const auto& map = typeMapping.vertices; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.vertices); } else if (type == "Track") { - const auto& map = typeMapping.tracks; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.tracks); } else if (type == "Cluster") { - const auto& map = typeMapping.clusters; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.clusters); } else if (type == "SimCalorimeterHit") { - const auto& map = typeMapping.simCaloHits; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.simCaloHits); } else if (type == "RawCalorimeterHit") { - const auto& map = typeMapping.rawCaloHits; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); } else if (type == "CalorimeterHit") { - const auto& map = typeMapping.caloHits; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.caloHits); } else if (type == "SimTrackerHit") { - const auto& map = typeMapping.simTrackerHits; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); } else if (type == "TPCHit") { - const auto& map = typeMapping.tpcHits; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.tpcHits); } else if (type == "TrackerHit") { - const auto& map = typeMapping.trackerHits; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.trackerHits); } else if (type == "TrackerHitPlane") { - const auto& map = typeMapping.trackerHitPlanes; - return handleSubsetColl(LCCollection, map); + return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); } else { return nullptr; From 589c0474cd147baf46cc1a1c040b93e7f35e6260 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 09:50:10 +0100 Subject: [PATCH 06/83] Remove unnecessary nullptr check --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 641cbc3b..b0b20953 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -651,11 +651,9 @@ namespace LCIO2EDM4hepConv { edm.setStartVertex(it->second); } else { - if (it != nullptr) { - std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; - } + std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; } for (auto c : clusters) { const auto it = clusterMap.find(c); From cad698efc116c6299113d1d8e2e38ab99a35b329 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 09:57:19 +0100 Subject: [PATCH 07/83] Add particleID relation resolution for RecoParticles --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 4 +- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 43 +++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index db756812..f3c045d6 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -73,6 +73,7 @@ namespace LCIO2EDM4hepConv { TypeMapT recoParticles {}; TypeMapT mcParticles {}; TypeMapT trackerHitPlanes {}; + TypeMapT particleIDs {}; }; using CollNamePair = std::tuple>; @@ -359,7 +360,8 @@ namespace LCIO2EDM4hepConv { TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, - const TypeMapT& tracksMap); + const TypeMapT& tracksMap, + const TypeMapT& particleIDMap); /** * Resolve the relations for SimCalorimeterHits diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index b0b20953..ed2c67c0 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -636,18 +636,15 @@ namespace LCIO2EDM4hepConv { TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, - const TypeMapT& tracksMap) + const TypeMapT& tracksMap, + const TypeMapT& particleIDMap) { int edmnum = 1; for (auto& [lcio, edm] : recoparticlesMap) { edmnum++; - auto vertex = lcio->getStartVertex(); - auto clusters = lcio->getClusters(); - auto tracks = lcio->getTracks(); - auto parents = lcio->getParticles(); - const auto it = vertexMap.find(vertex); - if (it != vertexMap.end()) { + auto vertex = lcio->getStartVertex(); + if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { edm.setStartVertex(it->second); } else { @@ -655,6 +652,8 @@ namespace LCIO2EDM4hepConv { "while trying to resolve the ReconstructedParticle Relations" << std::endl; } + + auto clusters = lcio->getClusters(); for (auto c : clusters) { const auto it = clusterMap.find(c); if (it != clusterMap.end()) { @@ -666,6 +665,8 @@ namespace LCIO2EDM4hepConv { << std::endl; } } + + auto tracks = lcio->getTracks(); for (auto t : tracks) { const auto it = tracksMap.find(t); if (it != tracksMap.end()) { @@ -677,6 +678,8 @@ namespace LCIO2EDM4hepConv { << std::endl; } } + + auto parents = lcio->getParticles(); for (auto p : parents) { const auto it = recoparticlesMap.find(p); if (it != recoparticlesMap.end()) { @@ -688,6 +691,23 @@ namespace LCIO2EDM4hepConv { << std::endl; } } + + auto particleIDUsed = lcio->getParticleIDUsed(); + if (const auto it = particleIDMap.find(particleIDUsed); it != particleIDMap.end()) { + edm.setParticleIDUsed(it->second); + } + else { + std::cerr << "Cannot find corresponding ParticleIDUsed for a LCIO RecoParticle" << std::endl; + } + + for (auto pid : lcio->getParticleIDs()) { + if (const auto it = particleIDMap.find(pid); it != particleIDMap.end()) { + edm.addToParticleIDs(it->second); + } + else { + std::cerr << "Cannot find corresponding ParticleID for a LCIO Recoparticle" << std::endl; + } + } } } @@ -822,7 +842,11 @@ namespace LCIO2EDM4hepConv { { resolveRelationsMCParticle(typeMapping.mcParticles); resolveRelationsRecoParticle( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + typeMapping.recoParticles, + typeMapping.vertices, + typeMapping.clusters, + typeMapping.tracks, + typeMapping.particleIDs); resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); resolveRelationsSimCalorimeterHit(typeMapping.simCaloHits, typeMapping.mcParticles); resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); @@ -956,6 +980,9 @@ namespace LCIO2EDM4hepConv { else if (type == "TrackerHitPlane") { return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); } + else if (type == "ParticleID") { + return handleSubsetColl(LCCollection, typeMapping.particleIDs); + } else { return nullptr; } From defd6cf619d71390807aff6b0c412b66ab2c2c75 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 10:05:40 +0100 Subject: [PATCH 08/83] Comment unnecessary loop until underlying issue is resolved --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index ed2c67c0..92433457 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -715,17 +715,20 @@ namespace LCIO2EDM4hepConv { TypeMapT& SimCaloHitMap, const TypeMapT& mcparticlesMap) { - for (auto& [lcio, edm] : SimCaloHitMap) { - auto contributionLen = lcio->getNMCParticles(); - for (auto i = 0; i < contributionLen; i++) { - auto mcp = lcio->getParticleCont(i); - const auto it = mcparticlesMap.find(mcp); - // if (it != mcparticlesMap.end()) { - // edm.getContributions(i).setParticle(it->second); - // see https://github.com/AIDASoft/podio/issues/347 - // } - } - } + // TODO: Currently not doing anything here because we cannot get a mutable + // CaloHitContribution from the SimCalorimeterHit via the podio generated + // interface. The underlying issue is https://github.com/AIDASoft/podio/issues/347 + + // for (auto& [lcio, edm] : SimCaloHitMap) { + // auto contributionLen = lcio->getNMCParticles(); + // for (auto i = 0; i < contributionLen; i++) { + // auto mcp = lcio->getParticleCont(i); + // const auto it = mcparticlesMap.find(mcp); + // if (it != mcparticlesMap.end()) { + // edm.getContributions(i).setParticle(it->second); // This breaks + // } + // } + // } } void resolveRelationsCluster( From 4e42c41f0265a62dc7c2982ae552170ba9bcf583 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 10:53:57 +0100 Subject: [PATCH 09/83] Convert ParticleIDs as part of the ReconstructedParticles --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 31 +++-- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 108 +++++++++--------- 2 files changed, 70 insertions(+), 69 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index f3c045d6..f5b2269c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -124,6 +124,17 @@ namespace LCIO2EDM4hepConv { */ edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState); + /** + * Convert a ParticleID object. + * + * In LCIO ParticleIDs are persisted as part of the ReconstructedParticle they + * are attached to. There are no ParticleID collections in the event. Hence, + * this function converts single particle ID objects and the management of + * putting them into a collection and of creating the LCIO to EDM4hep mapping + * is done in the conversion of the ReconstructedParticles. + */ + edm4hep::MutableParticleID convertPaticleID(const EVENT::ParticleID* pid); + /** * Convert an MCParticle collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. @@ -136,11 +147,16 @@ namespace LCIO2EDM4hepConv { /** * Convert a ReconstructedParticle collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. + * + * NOTE: Also populates a ParticleID collection, as those are persisted as + * part of the ReconstructedParticles in LCIO. The name of this collection is + * _particleIDs */ - std::unique_ptr convertReconstructedParticle( + std::vector convertReconstructedParticle( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap); + TypeMapT& recoparticlesMap, + TypeMapT& particleIDMap); /** * Convert a Vertex collection and return the resulting collection. @@ -227,14 +243,6 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& caloHitMap); - /** - * Convert a ParticleID collection and return the resulting collection. - * Simultaneously populates the mapping from LCIO to EDM4hep objects. - */ - std::unique_ptr convertParticleID( - const std::string& name, - EVENT::LCCollection* LCCollection); - /** * Convert a Cluster collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. @@ -360,8 +368,7 @@ namespace LCIO2EDM4hepConv { TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, - const TypeMapT& tracksMap, - const TypeMapT& particleIDMap); + const TypeMapT& tracksMap); /** * Resolve the relations for SimCalorimeterHits diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 92433457..ea44c0d5 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -43,6 +43,21 @@ namespace LCIO2EDM4hepConv { return edmtrackState; } + edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID* pid) + { + auto result = edm4hep::MutableParticleID {}; + result.setType(pid->getType()); + result.setPDG(pid->getPDG()); + result.setAlgorithmType(pid->getAlgorithmType()); + result.setLikelihood(pid->getLikelihood()); + + for (auto v : pid->getParameters()) { + result.addToParameters(v); + } + + return result; + } + std::unique_ptr convertMCParticle( const std::string& name, EVENT::LCCollection* LCCollection, @@ -77,12 +92,14 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertReconstructedParticle( + std::vector convertReconstructedParticle( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap) + TypeMapT& recoparticlesMap, + TypeMapT& particleIDMap) { auto dest = std::make_unique(); + auto particleIDs = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); @@ -104,8 +121,36 @@ namespace LCIO2EDM4hepConv { std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; } + + // Need to convert the particle IDs here, since they are part of the reco + // particle collection in LCIO + for (const auto lcioPid : rval->getParticleIDs()) { + auto pid = convertParticleID(lcioPid); + const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); + if (pidInserted) { + lval.addToParticleIDs(pid); + } + else { + lval.addToParticleIDs(pidIt->second); + } + } + + const auto lcioPidUsed = rval->getParticleIDUsed(); + if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { + lval.setParticleIDUsed(it->second); + } + else { + auto pid = convertParticleID(lcioPidUsed); + particleIDMap.emplace(lcioPidUsed, pid); + lval.setParticleIDUsed(pid); + } } - return dest; + + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_particleIDs", std::move(particleIDs)); + return results; } std::unique_ptr convertVertex( @@ -423,29 +468,6 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertParticleID( - const std::string& name, - EVENT::LCCollection* LCCollection) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setType(rval->getType()); - lval.setPDG(rval->getPDG()); - lval.setAlgorithmType(rval->getAlgorithmType()); - lval.setLikelihood(rval->getLikelihood()); - - for (auto v : rval->getParameters()) { - lval.addToParameters(v); - } - } - - return dest; - } - std::unique_ptr convertCluster( const std::string& name, EVENT::LCCollection* LCCollection, @@ -488,7 +510,7 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); } else if (type == "ReconstructedParticle") { - retColls.emplace_back(name, convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles)); + return convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); } else if (type == "Vertex") { retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); @@ -520,9 +542,6 @@ namespace LCIO2EDM4hepConv { else if (type == "TrackerHitPlane") { retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); } - else if (type == "ParticleID") { - retColls.emplace_back(name, convertParticleID(name, LCCollection)); - } return retColls; } @@ -636,8 +655,7 @@ namespace LCIO2EDM4hepConv { TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, - const TypeMapT& tracksMap, - const TypeMapT& particleIDMap) + const TypeMapT& tracksMap) { int edmnum = 1; for (auto& [lcio, edm] : recoparticlesMap) { @@ -691,23 +709,6 @@ namespace LCIO2EDM4hepConv { << std::endl; } } - - auto particleIDUsed = lcio->getParticleIDUsed(); - if (const auto it = particleIDMap.find(particleIDUsed); it != particleIDMap.end()) { - edm.setParticleIDUsed(it->second); - } - else { - std::cerr << "Cannot find corresponding ParticleIDUsed for a LCIO RecoParticle" << std::endl; - } - - for (auto pid : lcio->getParticleIDs()) { - if (const auto it = particleIDMap.find(pid); it != particleIDMap.end()) { - edm.addToParticleIDs(it->second); - } - else { - std::cerr << "Cannot find corresponding ParticleID for a LCIO Recoparticle" << std::endl; - } - } } } @@ -845,11 +846,7 @@ namespace LCIO2EDM4hepConv { { resolveRelationsMCParticle(typeMapping.mcParticles); resolveRelationsRecoParticle( - typeMapping.recoParticles, - typeMapping.vertices, - typeMapping.clusters, - typeMapping.tracks, - typeMapping.particleIDs); + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); resolveRelationsSimCalorimeterHit(typeMapping.simCaloHits, typeMapping.mcParticles); resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); @@ -983,9 +980,6 @@ namespace LCIO2EDM4hepConv { else if (type == "TrackerHitPlane") { return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); } - else if (type == "ParticleID") { - return handleSubsetColl(LCCollection, typeMapping.particleIDs); - } else { return nullptr; } From d4c0e65b2efcf234484325feeb579b1d0d7913e1 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 10:56:21 +0100 Subject: [PATCH 10/83] Format commented code block --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index ea44c0d5..d4365139 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -791,35 +791,35 @@ namespace LCIO2EDM4hepConv { if (it != trackerHitMap.end()) { edm.addToTrackerHits(it->second); } - else { - // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" - // <<" This is due to it being a TrackerHitPlane or TPCHit" << std::endl; - - /* - // This Code looks for the trackerHit in the TPCHit Map aswell as the trackerHitPlane Map. Those relations can - not be set for a track in edm4HEP. In all tests the missing trackerHits were located in either of these maps. - const auto tpchit = dynamic_cast(th); - const auto trackerhitplane = dynamic_cast(th); - if (tpchit != nullptr){ - const auto it = TPCHitMap.find(tpchit); - if (it != TPCHitMap.end()) { - std::cout << "trackerHit found in TPCHit map !" << std::endl; - } - else { - std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; - } - } - else if(trackerhitplane != nullptr){ - const auto it = trackerhitplaneMap.find(trackerhitplane); - if (it != trackerhitplaneMap.end()) { - std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; - } - else { - std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; - } - } - */ - } + // else { + // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" + // << " This is due to it being a TrackerHitPlane or TPCHit" << std::endl; + + // // This Code looks for the trackerHit in the TPCHit Map aswell as the + // // trackerHitPlane Map. Those relations can not be set for a track in + // // edm4HEP. In all tests the missing trackerHits were located in + // // either of these maps. + // const auto tpchit = dynamic_cast(th); + // const auto trackerhitplane = dynamic_cast(th); + // if (tpchit != nullptr) { + // const auto it = TPCHitMap.find(tpchit); + // if (it != TPCHitMap.end()) { + // std::cout << "trackerHit found in TPCHit map !" << std::endl; + // } + // else { + // std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; + // } + // } + // else if (trackerhitplane != nullptr) { + // const auto it = trackerhitplaneMap.find(trackerhitplane); + // if (it != trackerhitplaneMap.end()) { + // std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; + // } + // else { + // std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; + // } + // } + // } } } } From 75bed64f6b0d34b5548812bacd3f9adcce15c070 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 11:27:01 +0100 Subject: [PATCH 11/83] Add include guards --- .../include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index f5b2269c..35e9111c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -1,3 +1,6 @@ +#ifndef K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H +#define K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H + // EDM4hep #include "edm4hep/CaloHitContributionCollection.h" #include "edm4hep/CalorimeterHitCollection.h" @@ -401,3 +404,5 @@ namespace LCIO2EDM4hepConv { const TypeMapT& recoparticleMap); } // namespace LCIO2EDM4hepConv + +#endif // K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H From 76e1296ac75f2f9dd1d5e1b8ae7cffa80e8104ca Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 11:48:12 +0100 Subject: [PATCH 12/83] Add library for basic comparisons of LCIO and EDM4hep --- CMakeLists.txt | 4 + tests/CMakeLists.txt | 3 + tests/CompareEDM4hepLCIO.cc | 300 ++++++++++++++++++++++++++++++++++++ tests/CompareEDM4hepLCIO.h | 95 ++++++++++++ tests/ComparisonUtils.h | 140 +++++++++++++++++ 5 files changed, 542 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/CompareEDM4hepLCIO.cc create mode 100644 tests/CompareEDM4hepLCIO.h create mode 100644 tests/ComparisonUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b44f14a6..e7c4a0f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,5 +26,9 @@ find_package(LCIO REQUIRED) find_package(EDM4HEP REQUIRED) add_subdirectory(k4EDM4hep2LcioConv) +include(CTest) +if (BUILD_TESTING) + add_subdirectory(tests) +endif() include(cmake/k4EDM4hep2LcioConvCreateConfig.cmake) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..d6272171 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(edmCompare SHARED CompareEDM4hepLCIO.cc) +target_link_libraries(edmCompare PUBLIC EDM4HEP::edm4hep ${LCIO_LIBRARIES}) +target_include_directories(edmCompare PUBLIC ${LCIO_INCLUDE_DIRS}) diff --git a/tests/CompareEDM4hepLCIO.cc b/tests/CompareEDM4hepLCIO.cc new file mode 100644 index 00000000..a7ef5e95 --- /dev/null +++ b/tests/CompareEDM4hepLCIO.cc @@ -0,0 +1,300 @@ +#include "CompareEDM4hepLCIO.h" +#include "ComparisonUtils.h" + +/** + * The basic implementation of the functionality has been generated via modified + * podio templates, employing some handwritten macros to facilitate the task. + * These macros and the code generation cover the data members of all involved + * data types but no relations. + * + * The basic working principle is to implement an overload that compares an LCIO + * and EDM4hep object for each corresponding type. A second overload for + * collections of the involved types simply forwards to the templated + * compareCollection function, which then does the looping and eventually calls + * the single object compare. All comparison functions return true if all + * comparisons succeeded or false in case any comparison failed returning as + * early as possible. + * + * TODO: Also compare relations + */ + +// ================= CalorimeterHit ================ + +bool compare(const EVENT::CalorimeterHit* lcioElem, const edm4hep::CalorimeterHit& edm4hepElem) +{ + // TODO: cellID vs. cellID0 and cellID1 in LCIO + // ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, "cellID in + // CalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEnergy, "energy in CalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEnergyError, "energyError in CalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTime, "time in CalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in CalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in CalorimeterHit"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::CalorimeterHitCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= Cluster ================ + +bool compare(const EVENT::Cluster* lcioElem, const edm4hep::Cluster& edm4hepElem) +{ + ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in Cluster"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEnergy, "energy in Cluster"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEnergyError, "energyError in Cluster"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in Cluster"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPositionError, "positionError in Cluster"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getITheta, "iTheta in Cluster"); + // TODO: LCIO has getIPhi not get Phi + // ASSERT_COMPARE(lcioElem, edm4hepElem, getPhi, "phi in Cluster"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getDirectionError, "directionError in Cluster"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::ClusterCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= MCParticle ================ + +bool compare(const EVENT::MCParticle* lcioElem, const edm4hep::MCParticle& edm4hepElem) +{ + static const std::vector BITno {23, 24, 25, 26, 27, 28, 29, 30, 31}; + + const std::vector BITname { + "BITOverlay", + "BITStopped", + "BITLeftDetector", + "BITDecayedInCalorimeter", + "BITDecayedInTracker", + "BITVertexIsNotEndpointOfParent", + "BITBackscatter", + "BITCreatedInSimulation", + "BITEndpoint"}; + ASSERT_COMPARE(lcioElem, edm4hepElem, getPDG, "PDG in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getGeneratorStatus, "generatorStatus in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, isCreatedInSimulation, "Created in Simulation"); + ASSERT_COMPARE(lcioElem, edm4hepElem, isBackscatter, "particle is from backscatter of calorimeter shower"); + ASSERT_COMPARE(lcioElem, edm4hepElem, vertexIsNotEndpointOfParent, "checks vertex, enpoint of parent"); + ASSERT_COMPARE(lcioElem, edm4hepElem, isDecayedInTracker, "isDecayedInTracker"); + ASSERT_COMPARE(lcioElem, edm4hepElem, isDecayedInCalorimeter, "isDecayedInCalorimeter,"); + ASSERT_COMPARE(lcioElem, edm4hepElem, hasLeftDetector, "hasLeftDetector,"); + ASSERT_COMPARE(lcioElem, edm4hepElem, isStopped, "isStopped,"); + ASSERT_COMPARE(lcioElem, edm4hepElem, isOverlay, "isOverlay,"); + + ASSERT_COMPARE(lcioElem, edm4hepElem, getCharge, "charge in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTime, "time in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getMass, "mass in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getVertex, "vertex in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEndpoint, "endpoint in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getMomentum, "momentum in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getMomentumAtEndpoint, "momentumAtEndpoint in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getSpin, "spin in MCParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getColorFlow, "colorFlow in MCParticle"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::MCParticleCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= ParticleID ================ + +// bool compare(const EVENT::ParticleID * lcioElem, const edm4hep::ParticleID & +// edm4hepElem) { +// ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in ParticleID"); +// ASSERT_COMPARE(lcioElem, edm4hepElem, getPDG, "PDG in ParticleID"); +// ASSERT_COMPARE(lcioElem, edm4hepElem, getAlgorithmType, "algorithmType in +// ParticleID"); ASSERT_COMPARE(lcioElem, edm4hepElem, getLikelihood, +// "likelihood in ParticleID"); return true; +// } +// +// bool compare(const lcio::LCCollection* lcioCollection, const +// edm4hep::ParticleIDCollection& edm4hepCollection) { +// return compareCollection(lcioCollection, +// edm4hepCollection); +// } + +// ================= RawCalorimeterHit ================ + +bool compare(const EVENT::RawCalorimeterHit* lcioElem, const edm4hep::RawCalorimeterHit& edm4hepElem) +{ + // TODO: LCIO has getCellID0 and getCellID1 + // ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, + // "cellID in RawCalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getAmplitude, "amplitude in RawCalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTimeStamp, "timeStamp in RawCalorimeterHit"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::RawCalorimeterHitCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= ReconstructedParticle ================ + +bool compare(const EVENT::ReconstructedParticle* lcioElem, const edm4hep::ReconstructedParticle& edm4hepElem) +{ + ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEnergy, "energy in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getMomentum, "momentum in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getReferencePoint, "referencePoint in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getCharge, "charge in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getMass, "mass in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getGoodnessOfPID, "goodnessOfPID in ReconstructedParticle"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getCovMatrix, "covMatrix in ReconstructedParticle"); + return true; +} + +bool compare( + const lcio::LCCollection* lcioCollection, + const edm4hep::ReconstructedParticleCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= SimCalorimeterHit ================ + +bool compare(const EVENT::SimCalorimeterHit* lcioElem, const edm4hep::SimCalorimeterHit& edm4hepElem) +{ + // TODO: LCIO has getCellID0 and getCellID1 + // ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, + // "cellID in SimCalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEnergy, "energy in SimCalorimeterHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in SimCalorimeterHit"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimCalorimeterHitCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= SimTrackerHit ================ + +bool compare(const EVENT::SimTrackerHit* lcioElem, const edm4hep::SimTrackerHit& edm4hepElem) +{ + ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, "cellID in SimTrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEDep, "EDep in SimTrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTime, "time in SimTrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPathLength, "pathLength in SimTrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getQuality, "quality in SimTrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in SimTrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getMomentum, "momentum in SimTrackerHit"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimTrackerHitCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= TPCHit ================ + +bool compare(const EVENT::TPCHit* lcioElem, const edm4hep::TPCHit& edm4hepElem) +{ + ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, "cellID in TPCHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getQuality, "quality in TPCHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTime, "time in TPCHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getCharge, "charge in TPCHit"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TPCHitCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= Track ================ + +bool compare(const EVENT::Track* lcioElem, const edm4hep::Track& edm4hepElem) +{ + ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in Track"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getChi2, "chi2 in Track"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getNdf, "ndf in Track"); + // TODO: LCIO has getdEdx instead of getDEdx + // ASSERT_COMPARE(lcioElem, edm4hepElem, getDEdx, "dEdx in Track"); + // ASSERT_COMPARE(lcioElem, edm4hepElem, getDEdxError, "dEdxError in Track"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getRadiusOfInnermostHit, "radiusOfInnermostHit in Track"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= TrackerHit ================ + +bool compare(const EVENT::TrackerHit* lcioElem, const edm4hep::TrackerHit& edm4hepElem) +{ + // TODO: LCIO has getCellID0 and getCellID1 + // ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, "cellID in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getQuality, "quality in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTime, "time in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEDep, "eDep in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEDepError, "eDepError in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in TrackerHit"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getCovMatrix, "covMatrix in TrackerHit"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackerHitCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= TrackerHitPlane ================ + +bool compare(const EVENT::TrackerHitPlane* lcioElem, const edm4hep::TrackerHitPlane& edm4hepElem) +{ + // TODO: LCIO has getCellID0 and getCellID1 + // ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, "cellID in + // TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getType, "type in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getQuality, "quality in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getTime, "time in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEDep, "eDep in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getEDepError, "eDepError in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getU, "u in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getV, "v in TrackerHitPlane"); + // TODO: LCIO has getdU and getdV instead of getDu and getDv + // ASSERT_COMPARE(lcioElem, edm4hepElem, getDu, "du in TrackerHitPlane"); + // ASSERT_COMPARE(lcioElem, edm4hepElem, getDv, "dv in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in TrackerHitPlane"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getCovMatrix, "covMatrix in TrackerHitPlane"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackerHitPlaneCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} + +// ================= Vertex ================ + +bool compare(const EVENT::Vertex* lcioElem, const edm4hep::Vertex& edm4hepElem) +{ + // TODO: LCIO has isPrimary + // ASSERT_COMPARE(lcioElem, edm4hepElem, getPrimary, "primary in Vertex"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getChi2, "chi2 in Vertex"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getProbability, "probability in Vertex"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getPosition, "position in Vertex"); + ASSERT_COMPARE(lcioElem, edm4hepElem, getCovMatrix, "covMatrix in Vertex"); + // TODO: LCIO with std::string vs. EDM4hep with int + // ASSERT_COMPARE(lcioElem, edm4hepElem, getAlgorithmType, + // "algorithmType in Vertex"); + return true; +} + +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::VertexCollection& edm4hepCollection) +{ + return compareCollection(lcioCollection, edm4hepCollection); +} diff --git a/tests/CompareEDM4hepLCIO.h b/tests/CompareEDM4hepLCIO.h new file mode 100644 index 00000000..aeb47859 --- /dev/null +++ b/tests/CompareEDM4hepLCIO.h @@ -0,0 +1,95 @@ +#ifndef K4EDM4HEP2LCIOCONV_TEST_COMPAREEDM4HEPLCIO_H +#define K4EDM4HEP2LCIOCONV_TEST_COMPAREEDM4HEPLCIO_H + +#include "edm4hep/CaloHitContributionCollection.h" +#include "edm4hep/CalorimeterHitCollection.h" +#include "edm4hep/ClusterCollection.h" +#include "edm4hep/EventHeaderCollection.h" +#include "edm4hep/MCParticleCollection.h" +#include "edm4hep/MCRecoCaloAssociationCollection.h" +#include "edm4hep/MCRecoCaloParticleAssociationCollection.h" +#include "edm4hep/MCRecoClusterParticleAssociationCollection.h" +#include "edm4hep/MCRecoParticleAssociationCollection.h" +#include "edm4hep/MCRecoTrackParticleAssociationCollection.h" +#include "edm4hep/MCRecoTrackerAssociationCollection.h" +#include "edm4hep/MCRecoTrackerHitPlaneAssociationCollection.h" +#include "edm4hep/ParticleIDCollection.h" +#include "edm4hep/RawCalorimeterHitCollection.h" +#include "edm4hep/RecoParticleVertexAssociationCollection.h" +#include "edm4hep/ReconstructedParticleCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" +#include "edm4hep/SimTrackerHitCollection.h" +#include "edm4hep/TPCHitCollection.h" +#include "edm4hep/TrackCollection.h" +#include "edm4hep/TrackerHitCollection.h" +#include "edm4hep/TrackerHitPlaneCollection.h" +#include "edm4hep/VertexCollection.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// bool compare(const EVENT::CaloHitContribution *lcio, +// const edm4hep::CaloHitContribution &edm4hep); +// bool compare(const lcio::LCCollection *lcioCollection, +// const edm4hep::CaloHitContributionCollection &edm4hepCollection); + +bool compare(const EVENT::CalorimeterHit* lcio, const edm4hep::CalorimeterHit& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::CalorimeterHitCollection& edm4hepCollection); + +bool compare(const EVENT::Cluster* lcio, const edm4hep::Cluster& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::ClusterCollection& edm4hepCollection); + +// bool compare(const EVENT::EventHeader *lcio, +// const edm4hep::EventHeader &edm4hep); +// bool compare(const lcio::LCCollection *lcioCollection, +// const edm4hep::EventHeaderCollection &edm4hepCollection); + +bool compare(const EVENT::MCParticle* lcio, const edm4hep::MCParticle& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::MCParticleCollection& edm4hepCollection); + +bool compare(const EVENT::RawCalorimeterHit* lcio, const edm4hep::RawCalorimeterHit& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::RawCalorimeterHitCollection& edm4hepCollection); + +bool compare(const EVENT::ReconstructedParticle* lcio, const edm4hep::ReconstructedParticle& edm4hep); +bool compare( + const lcio::LCCollection* lcioCollection, + const edm4hep::ReconstructedParticleCollection& edm4hepCollection); + +bool compare(const EVENT::SimCalorimeterHit* lcio, const edm4hep::SimCalorimeterHit& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimCalorimeterHitCollection& edm4hepCollection); + +bool compare(const EVENT::SimTrackerHit* lcio, const edm4hep::SimTrackerHit& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimTrackerHitCollection& edm4hepCollection); + +bool compare(const EVENT::TPCHit* lcio, const edm4hep::TPCHit& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TPCHitCollection& edm4hepCollection); + +bool compare(const EVENT::TrackerHit* lcio, const edm4hep::TrackerHit& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackerHitCollection& edm4hepCollection); + +bool compare(const EVENT::TrackerHitPlane* lcio, const edm4hep::TrackerHitPlane& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackerHitPlaneCollection& edm4hepCollection); + +bool compare(const EVENT::Track* lcio, const edm4hep::Track& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackCollection& edm4hepCollection); + +bool compare(const EVENT::Vertex* lcio, const edm4hep::Vertex& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::VertexCollection& edm4hepCollection); + +#endif // K4EDM4HEP2LCIOCONV_TEST_COMPAREEDM4HEPLCIO_H diff --git a/tests/ComparisonUtils.h b/tests/ComparisonUtils.h new file mode 100644 index 00000000..05646160 --- /dev/null +++ b/tests/ComparisonUtils.h @@ -0,0 +1,140 @@ +#ifndef K4EDM4HEP2LCIOCONV_TEST_COMPARISONUTILS_H +#define K4EDM4HEP2LCIOCONV_TEST_COMPARISONUTILS_H + +#include "edm4hep/Vector2f.h" +#include "edm4hep/Vector2i.h" +#include "edm4hep/Vector3d.h" +#include "edm4hep/Vector3f.h" + +#include "UTIL/LCIterator.h" +#include "EVENT/LCCollection.h" + +#include +#include +#include + +template +std::ostream& printContainer(std::ostream& os, const T& cont) +{ + os << "("; + if (!cont.empty()) { + os << cont[0]; + for (size_t i = 1; i < cont.size(); ++i) { + os << ", " << cont[i]; + } + } + + return os << ")"; +} + +template +std::ostream& operator<<(std::ostream& os, const std::vector& vec) +{ + return printContainer(os, vec); +} + +template +std::ostream& operator<<(std::ostream& os, const std::array& arr) +{ + return printContainer(os, arr); +} + +template +bool operator==(const std::vector& vec, const std::array& arr) +{ + if (vec.size() != N) { + return false; + } + for (size_t i = 0; i < N; ++i) { + if (vec[i] != arr[i]) { + return false; + } + } + return true; +} + +template +bool operator!=(const std::vector& vec, const std::array& arr) +{ + return !(vec == arr); +} + +// Macro for defining the comparison operators for edm4hep::Vector3X and +// different return types (X* or vector from LCIO) +#define VECTOR3_COMPARE(FT, VT) \ + bool operator==(const FT* vals, const VT& vec) \ + { \ + return vals[0] == vec[0] && vals[1] == vec[1] && vals[2] == vec[2]; \ + } \ + bool operator!=(const FT* vals, const VT& vec) { return !(vals == vec); } \ + bool operator==(const std::vector& vals, const VT& vec) \ + { \ + if (vals.size() != 3) { \ + return false; \ + } \ + return vals.data() == vec; \ + } \ + bool operator!=(const std::vector& vals, const VT& vec) { return !(vals == vec); } + +VECTOR3_COMPARE(float, edm4hep::Vector3f) +VECTOR3_COMPARE(double, edm4hep::Vector3d) +// Necessary in some MCParticle return types +VECTOR3_COMPARE(double, edm4hep::Vector3f) +#undef VECTOR3_COMPARE + +// Macro for defining the comparison operators for edm4hep::Vector3X and +// different return types (X* or vector from LCIO) +#define VECTOR2_COMPARE(FT, VT) \ + bool operator==(const FT* vals, const VT& vec) { return vals[0] == vec[0] && vals[1] == vec[1]; } \ + bool operator!=(const FT* vals, const VT& vec) { return !(vals == vec); } \ + bool operator==(const std::vector& vals, const VT& vec) \ + { \ + if (vals.size() != 2) { \ + return false; \ + } \ + return vals.data() == vec; \ + } \ + bool operator!=(const std::vector& vals, const VT& vec) { return !(vals == vec); } + +VECTOR2_COMPARE(int, edm4hep::Vector2i) +VECTOR2_COMPARE(float, edm4hep::Vector2f) +#undef VECTOR2_COMPARE + +// Macro for comparing the return types of the different functions and return +// false if they are not equal while also emitting a message +#define ASSERT_COMPARE_VALS(lcioV, edm4hepV, msg) \ + if ((lcioV) != (edm4hepV)) { \ + std::cerr << msg << " (LCIO: " << (lcioV) << ", EDM4hep: " << (edm4hepV) << ")" << std::endl; \ + return false; \ + } + +#define ASSERT_COMPARE(lcioE, edm4hepE, func, msg) \ + { \ + const auto lcioV = lcioE->func(); \ + const auto edm4hepV = edm4hepE.func(); \ + ASSERT_COMPARE_VALS(lcioV, edm4hepV, msg) \ + } + +// Compare an LCIO collection and an EDM4hep collection. Assumes that a compare +// function working with the element types is available +template +bool compareCollection(const lcio::LCCollection* lcioCollection, const EDM4hepCollT& edm4hepCollection) +{ + if (lcioCollection->getNumberOfElements() != edm4hepCollection.size()) { + return false; + } + + UTIL::LCIterator lcioIt(lcioCollection); + int counter = 0; + for (const auto edm4hepElem : edm4hepCollection) { + if (!compare(lcioIt.next(), edm4hepElem)) { + std::cerr << "in Element " << counter << std::endl; + return false; + } + counter++; + } + + return true; +} + +#endif // K4EDM4HEP2LCIOCONV_TEST_COMPARISONUTILS_H From 4523cb2cdc646f45f3e61a8124b69655c7d15949 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 13:53:07 +0100 Subject: [PATCH 13/83] Cleanup and clarification of simulator status --- tests/CompareEDM4hepLCIO.cc | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tests/CompareEDM4hepLCIO.cc b/tests/CompareEDM4hepLCIO.cc index a7ef5e95..20c84a26 100644 --- a/tests/CompareEDM4hepLCIO.cc +++ b/tests/CompareEDM4hepLCIO.cc @@ -63,20 +63,10 @@ bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::ClusterCol bool compare(const EVENT::MCParticle* lcioElem, const edm4hep::MCParticle& edm4hepElem) { - static const std::vector BITno {23, 24, 25, 26, 27, 28, 29, 30, 31}; - - const std::vector BITname { - "BITOverlay", - "BITStopped", - "BITLeftDetector", - "BITDecayedInCalorimeter", - "BITDecayedInTracker", - "BITVertexIsNotEndpointOfParent", - "BITBackscatter", - "BITCreatedInSimulation", - "BITEndpoint"}; ASSERT_COMPARE(lcioElem, edm4hepElem, getPDG, "PDG in MCParticle"); ASSERT_COMPARE(lcioElem, edm4hepElem, getGeneratorStatus, "generatorStatus in MCParticle"); + // LCIO changes the SimulatorStatus during I/O, so here we have to check the + // individual bits which are untouched instead of just doing one comparison for the SimulatorStatus ASSERT_COMPARE(lcioElem, edm4hepElem, isCreatedInSimulation, "Created in Simulation"); ASSERT_COMPARE(lcioElem, edm4hepElem, isBackscatter, "particle is from backscatter of calorimeter shower"); ASSERT_COMPARE(lcioElem, edm4hepElem, vertexIsNotEndpointOfParent, "checks vertex, enpoint of parent"); From 87155c9a1d062cc2ab1a47fd205987e4c1c77726 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 15:12:32 +0100 Subject: [PATCH 14/83] Fix typo in function name Discovered by linker --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index d4365139..d66fc49d 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -651,7 +651,7 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsRecocParticle( + void resolveRelationsRecoParticle( TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, From 232325bd137640b612ca06bfcfdc6128bd73c51e Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Thu, 23 Feb 2023 16:20:38 +0100 Subject: [PATCH 15/83] Add a standalone executable for converting LCIO to EDM4hep --- CMakeLists.txt | 2 + standalone/CMakeLists.txt | 6 +++ standalone/lcio2edm4hep.cpp | 103 ++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 standalone/CMakeLists.txt create mode 100644 standalone/lcio2edm4hep.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e7c4a0f7..8c5edd8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,8 @@ find_package(LCIO REQUIRED) find_package(EDM4HEP REQUIRED) add_subdirectory(k4EDM4hep2LcioConv) +add_subdirectory(standalone) + include(CTest) if (BUILD_TESTING) add_subdirectory(tests) diff --git a/standalone/CMakeLists.txt b/standalone/CMakeLists.txt new file mode 100644 index 00000000..395c8b77 --- /dev/null +++ b/standalone/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(lcio2edm4hep lcio2edm4hep.cpp) +target_link_libraries(lcio2edm4hep PRIVATE k4EDM4hep2LcioConv podio::podioRootIO) + +install(TARGETS lcio2edm4hep + DESTINATION ${CMAKE_INSTALL_BINDIR} + ) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp new file mode 100644 index 00000000..75b74e7e --- /dev/null +++ b/standalone/lcio2edm4hep.cpp @@ -0,0 +1,103 @@ +#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" + +#include +#include +#include + +#include "podio/ROOTFrameWriter.h" + +#include +#include +#include +#include +#include + +std::vector> getNamesAndTypes(const std::string& collTypeFile) +{ + std::ifstream input_file(collTypeFile); + std::vector> names_types; + + if (!input_file.is_open()) { + std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; + } + std::string line; + while (std::getline(input_file, line)) { + size_t delimiter_pos = line.find(' '); + if (delimiter_pos == std::string::npos) { + std::cerr << "Invalid input format" << std::endl; + input_file.close(); + return {}; + } + std::string name = line.substr(0, delimiter_pos); + std::string type = line.substr(delimiter_pos + 1); + names_types.emplace_back(name, type); + } + + input_file.close(); + + return names_types; +} + +constexpr auto usageMsg = R"(usage: lcio2edm4hep [-h] inputfile outputfile [colltypefile])"; +constexpr auto helpMsg = R"( +Convert an LCIO file to EDM4hep + +positional arguments: + inputfile the input LCIO file + outputfile the output EDM4hep file that will be created + colltypefile An optional input file that specifies the names and types of all + collections that should be present in the output. + +optional arguments: + -h, --help show this help message and exit +)"; + +int main(int argc, char* argv[]) +{ + if (argc == 2 && (argv[1] == std::string("-h") || argv[1] == std::string("--help"))) { + std::cerr << usageMsg << '\n' << helpMsg << std::endl; + return 0; + } + if (argc < 3) { + std::cerr << usageMsg << std::endl; + return 1; + } + const auto outputFile = std::string(argv[2]); + + bool patching = false; + UTIL::CheckCollections colPatcher {}; + if (argc == 4) { + const auto namesTypes = getNamesAndTypes(argv[3]); + if (namesTypes.empty()) { + std::cerr << "The provided list of collection names and types does not satisfy the required format: Pair of Name " + "and Type per line separated by space" + << std::endl; + return 1; + } + colPatcher.addPatchCollections(namesTypes); + patching = true; + } + + auto lcreader = IOIMPL::LCFactory::getInstance()->createLCReader(); + lcreader->open(argv[1]); + std::cout << "Number of events: " << lcreader->getNumberOfEvents() << '\n'; + + podio::ROOTFrameWriter writer(outputFile); + + std::cout << "starting Conversion" << std::endl; + for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { + // std::cout << "processing Event: " << i << std::endl; + + auto evt = lcreader->readNextEvent(); + // Patching the Event to make sure all events contain the same Collections. + if (patching == true) { + colPatcher.patchCollections(evt); + } + const auto edmEvent = LCIO2EDM4hepConv::convertEvent(evt); + writer.writeFrame(edmEvent, "events"); + } + + writer.finish(); + + return 0; +} From 17630775088203b9633c0e2f8392eab1d3de134a Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 17:28:38 +0100 Subject: [PATCH 16/83] Move comparison library to subfolder --- tests/CMakeLists.txt | 2 +- tests/{ => src}/CompareEDM4hepLCIO.cc | 0 tests/{ => src}/CompareEDM4hepLCIO.h | 0 tests/{ => src}/ComparisonUtils.h | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename tests/{ => src}/CompareEDM4hepLCIO.cc (100%) rename tests/{ => src}/CompareEDM4hepLCIO.h (100%) rename tests/{ => src}/ComparisonUtils.h (100%) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d6272171..a85d90de 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(edmCompare SHARED CompareEDM4hepLCIO.cc) +add_library(edmCompare SHARED src/CompareEDM4hepLCIO.cc) target_link_libraries(edmCompare PUBLIC EDM4HEP::edm4hep ${LCIO_LIBRARIES}) target_include_directories(edmCompare PUBLIC ${LCIO_INCLUDE_DIRS}) diff --git a/tests/CompareEDM4hepLCIO.cc b/tests/src/CompareEDM4hepLCIO.cc similarity index 100% rename from tests/CompareEDM4hepLCIO.cc rename to tests/src/CompareEDM4hepLCIO.cc diff --git a/tests/CompareEDM4hepLCIO.h b/tests/src/CompareEDM4hepLCIO.h similarity index 100% rename from tests/CompareEDM4hepLCIO.h rename to tests/src/CompareEDM4hepLCIO.h diff --git a/tests/ComparisonUtils.h b/tests/src/ComparisonUtils.h similarity index 100% rename from tests/ComparisonUtils.h rename to tests/src/ComparisonUtils.h From 581300872320d4d9cc2d38dfae0d27204a7d28dc Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Thu, 23 Feb 2023 17:29:01 +0100 Subject: [PATCH 17/83] Add executable for comparing lcio and edm4hep file contents --- tests/CMakeLists.txt | 5 +++ tests/compare_contents.cpp | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/compare_contents.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a85d90de..d1f7ed7e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,8 @@ add_library(edmCompare SHARED src/CompareEDM4hepLCIO.cc) target_link_libraries(edmCompare PUBLIC EDM4HEP::edm4hep ${LCIO_LIBRARIES}) target_include_directories(edmCompare PUBLIC ${LCIO_INCLUDE_DIRS}) + +add_executable(compare-contents compare_contents.cpp) +target_link_libraries(compare-contents PRIVATE edmCompare podio::podioRootIO) +target_include_directories(compare-contents PRIVATE + $) diff --git a/tests/compare_contents.cpp b/tests/compare_contents.cpp new file mode 100644 index 00000000..9ca4deae --- /dev/null +++ b/tests/compare_contents.cpp @@ -0,0 +1,77 @@ +#include "CompareEDM4hepLCIO.h" + +#include "podio/ROOTFrameReader.h" +#include "podio/Frame.h" + +#include + +#include + +#define ASSERT_COMPARE_OR_EXIT(collType) \ + if (type == #collType) { \ + auto& edmcoll = edmEvent.get(name); \ + if (!compare(lcioColl, edmcoll)) { \ + std::cerr << "in collection: " << name << std::endl; \ + return 1; \ + } \ + } + +constexpr auto usageMsg = R"(usage: compare-contents lciofile edm4hepfile)"; + +int main(int argc, char* argv[]) +{ + if (argc != 3) { + std::cerr << usageMsg << std::endl; + return 1; + } + + auto lcreader = IOIMPL::LCFactory::getInstance()->createLCReader(); + lcreader->open(argv[1]); + + auto edmreader = podio::ROOTFrameReader(); + edmreader.openFile(argv[2]); + + // loop going over every name of the lcio collection and checking if a + // collection with the same name exists in the edm file. + std::cout << "number of Events" << edmreader.getEntries("events") << std::endl; + for (size_t n = 0; n < edmreader.getEntries("events"); ++n) { + if (n % 1000 == 0) { + std::cout << "Event number: " << n << std::endl; + } + + auto lcEvent = lcreader->readNextEvent(); + auto edmEvent = podio::Frame(edmreader.readNextEntry("events")); + + for (const auto& name : *(lcEvent->getCollectionNames())) { + const auto lcioColl = lcEvent->getCollection(name); + // TODO: The Frame needs to improve here in order to get to the type + // without retrieving the collection + const auto& type = [&edmEvent, &name]() { + const auto coll = edmEvent.get(name); + if (coll) { + return coll->getTypeName(); + } + static std::string empty = ""; + return empty; + }(); + if (type.empty()) { + std::cerr << "Collection " << name << " not present in edm4hep file" << std::endl; + return 1; + } + + ASSERT_COMPARE_OR_EXIT(edm4hep::MCParticleCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::ReconstructedParticleCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::TrackCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::TrackerHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::SimTrackerHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::CalorimeterHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::RawCalorimeterHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::SimCalorimeterHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::TPCHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::ClusterCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::VertexCollection) + } + } + + return 0; +} From 77f734675c7335fe5bcaa08f8ff4288a48df89af Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Thu, 23 Feb 2023 17:40:38 +0100 Subject: [PATCH 18/83] Add nullptr check --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index d66fc49d..1e7b720e 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -136,6 +136,9 @@ namespace LCIO2EDM4hepConv { } const auto lcioPidUsed = rval->getParticleIDUsed(); + if (lcioPidUsed == nullptr) { + continue; + } if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { lval.setParticleIDUsed(it->second); } From b5aacff025f4c43f2ec03810fef8ba1c79cc10c6 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Mon, 6 Mar 2023 10:40:23 +0100 Subject: [PATCH 19/83] Change TPCHit -> RawTimeSeries --- .../include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 8 ++++---- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 12 ++++++------ tests/compare_contents.cpp | 2 +- tests/src/CompareEDM4hepLCIO.cc | 4 ++-- tests/src/CompareEDM4hepLCIO.h | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 35e9111c..75cd48e3 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -20,7 +20,7 @@ #include "edm4hep/ReconstructedParticleCollection.h" #include "edm4hep/SimCalorimeterHitCollection.h" #include "edm4hep/SimTrackerHitCollection.h" -#include "edm4hep/TPCHitCollection.h" +#include "edm4hep/RawTimeSeriesCollection.h" #include "edm4hep/TrackCollection.h" #include "edm4hep/TrackerHitCollection.h" #include "edm4hep/TrackerHitPlaneCollection.h" @@ -70,7 +70,7 @@ namespace LCIO2EDM4hepConv { TypeMapT caloHits {}; TypeMapT rawCaloHits {}; TypeMapT simCaloHits {}; - TypeMapT tpcHits {}; + TypeMapT tpcHits {}; TypeMapT clusters {}; TypeMapT vertices {}; TypeMapT recoParticles {}; @@ -183,10 +183,10 @@ namespace LCIO2EDM4hepConv { * Convert a TPCHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTPCHit( + std::unique_ptr convertTPCHit( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap); + TypeMapT& TPCHitMap); /** * Convert a TrackerHit collection and return the resulting collection. diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 1e7b720e..61a055d2 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -223,12 +223,12 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTPCHit( + std::unique_ptr convertTPCHit( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap) + TypeMapT& TPCHitMap) { - auto dest = std::make_unique(); + auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { const auto* rval = static_cast(LCCollection->getElementAt(i)); @@ -239,7 +239,7 @@ namespace LCIO2EDM4hepConv { lval.setCharge(rval->getCharge()); lval.setQuality(rval->getQuality()); for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { - lval.addToRawDataWords(rval->getRawDataWord(j)); + lval.addToAdcCounts(rval->getRawDataWord(j)); } const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); if (!inserted) { @@ -774,7 +774,7 @@ namespace LCIO2EDM4hepConv { void resolveRelationsTrack( TypeMapT& tracksMap, const TypeMapT& trackerHitMap, - const TypeMapT& TPCHitMap, + const TypeMapT& TPCHitMap, const TypeMapT& trackerhitplaneMap) { for (auto& [lcio, edm] : tracksMap) { @@ -975,7 +975,7 @@ namespace LCIO2EDM4hepConv { return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); } else if (type == "TPCHit") { - return handleSubsetColl(LCCollection, typeMapping.tpcHits); + return handleSubsetColl(LCCollection, typeMapping.tpcHits); } else if (type == "TrackerHit") { return handleSubsetColl(LCCollection, typeMapping.trackerHits); diff --git a/tests/compare_contents.cpp b/tests/compare_contents.cpp index 9ca4deae..605e99db 100644 --- a/tests/compare_contents.cpp +++ b/tests/compare_contents.cpp @@ -67,7 +67,7 @@ int main(int argc, char* argv[]) ASSERT_COMPARE_OR_EXIT(edm4hep::CalorimeterHitCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::RawCalorimeterHitCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::SimCalorimeterHitCollection) - ASSERT_COMPARE_OR_EXIT(edm4hep::TPCHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::RawTimeSeriesCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::ClusterCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::VertexCollection) } diff --git a/tests/src/CompareEDM4hepLCIO.cc b/tests/src/CompareEDM4hepLCIO.cc index 20c84a26..098addcb 100644 --- a/tests/src/CompareEDM4hepLCIO.cc +++ b/tests/src/CompareEDM4hepLCIO.cc @@ -187,7 +187,7 @@ bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimTracker // ================= TPCHit ================ -bool compare(const EVENT::TPCHit* lcioElem, const edm4hep::TPCHit& edm4hepElem) +bool compare(const EVENT::TPCHit* lcioElem, const edm4hep::RawTimeSeries& edm4hepElem) { ASSERT_COMPARE(lcioElem, edm4hepElem, getCellID, "cellID in TPCHit"); ASSERT_COMPARE(lcioElem, edm4hepElem, getQuality, "quality in TPCHit"); @@ -196,7 +196,7 @@ bool compare(const EVENT::TPCHit* lcioElem, const edm4hep::TPCHit& edm4hepElem) return true; } -bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TPCHitCollection& edm4hepCollection) +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::RawTimeSeriesCollection& edm4hepCollection) { return compareCollection(lcioCollection, edm4hepCollection); } diff --git a/tests/src/CompareEDM4hepLCIO.h b/tests/src/CompareEDM4hepLCIO.h index aeb47859..7f084555 100644 --- a/tests/src/CompareEDM4hepLCIO.h +++ b/tests/src/CompareEDM4hepLCIO.h @@ -19,7 +19,7 @@ #include "edm4hep/ReconstructedParticleCollection.h" #include "edm4hep/SimCalorimeterHitCollection.h" #include "edm4hep/SimTrackerHitCollection.h" -#include "edm4hep/TPCHitCollection.h" +#include "edm4hep/RawTimeSeriesCollection.h" #include "edm4hep/TrackCollection.h" #include "edm4hep/TrackerHitCollection.h" #include "edm4hep/TrackerHitPlaneCollection.h" @@ -77,8 +77,8 @@ bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimCalorim bool compare(const EVENT::SimTrackerHit* lcio, const edm4hep::SimTrackerHit& edm4hep); bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::SimTrackerHitCollection& edm4hepCollection); -bool compare(const EVENT::TPCHit* lcio, const edm4hep::TPCHit& edm4hep); -bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TPCHitCollection& edm4hepCollection); +bool compare(const EVENT::TPCHit* lcio, const edm4hep::RawTimeSeries& edm4hep); +bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::RawTimeSeriesCollection& edm4hepCollection); bool compare(const EVENT::TrackerHit* lcio, const edm4hep::TrackerHit& edm4hep); bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackerHitCollection& edm4hepCollection); From aa29c4c55db5fdc6553f79fd75ca1ad7e98d7335 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 27 Feb 2023 14:57:35 +0100 Subject: [PATCH 20/83] Adding ParticleID to Collections --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 61a055d2..711f16bb 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -129,6 +129,7 @@ namespace LCIO2EDM4hepConv { const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); if (pidInserted) { lval.addToParticleIDs(pid); + particleIDs->push_back(pid); } else { lval.addToParticleIDs(pidIt->second); @@ -144,6 +145,7 @@ namespace LCIO2EDM4hepConv { } else { auto pid = convertParticleID(lcioPidUsed); + particleIDs->push_back(pid); particleIDMap.emplace(lcioPidUsed, pid); lval.setParticleIDUsed(pid); } @@ -543,7 +545,7 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); } else if (type == "TrackerHitPlane") { - retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); + retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); } return retColls; } From 8b4bc015fefa3be64049c92e709a7a834a055e16 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 1 Mar 2023 10:26:08 +0100 Subject: [PATCH 21/83] Added a Nullptr check before every map lookup to make what we are looking for is actually in the file and not a removed object. --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 45 ++++++++++++++++--- standalone/lcio2edm4hep.cpp | 6 ++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 711f16bb..e8582828 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -545,7 +545,7 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); } else if (type == "TrackerHitPlane") { - retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); + retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); } return retColls; } @@ -613,6 +613,9 @@ namespace LCIO2EDM4hepConv { int dnum = 1; for (auto d : daughters) { + if (d == nullptr) { + continue; + } const auto it = mcparticlesMap.find(d); dnum++; if (it != mcparticlesMap.end()) { @@ -625,6 +628,9 @@ namespace LCIO2EDM4hepConv { } } for (auto p : parents) { + if (p == nullptr) { + continue; + } const auto it = mcparticlesMap.find(p); if (it != mcparticlesMap.end()) { edm.addToParents(it->second); @@ -644,6 +650,9 @@ namespace LCIO2EDM4hepConv { { for (auto& [lcio, edm] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); + if (mcps == nullptr) { + continue; + } const auto it = mcparticlesMap.find(mcps); if (it != mcparticlesMap.end()) { edm.setMCParticle(it->second); @@ -667,17 +676,23 @@ namespace LCIO2EDM4hepConv { edmnum++; auto vertex = lcio->getStartVertex(); + if (vertex == nullptr) { + continue; + } if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { edm.setStartVertex(it->second); } else { std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " - "while trying to resolve the ReconstructedParticle Relations" + "while trying to resolve the ReconstructedParticle Relations " << std::endl; } auto clusters = lcio->getClusters(); for (auto c : clusters) { + if (c == nullptr) { + continue; + } const auto it = clusterMap.find(c); if (it != clusterMap.end()) { edm.addToClusters(it->second); @@ -691,19 +706,25 @@ namespace LCIO2EDM4hepConv { auto tracks = lcio->getTracks(); for (auto t : tracks) { + if (t == nullptr) { + continue; + } const auto it = tracksMap.find(t); if (it != tracksMap.end()) { edm.addToTracks(it->second); } else { std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " - "while trying to resolve the ReconstructedParticle Relations" + "while trying to resolve the ReconstructedParticle Relations" << std::endl; } } auto parents = lcio->getParticles(); for (auto p : parents) { + if (p == nullptr) { + continue; + } const auto it = recoparticlesMap.find(p); if (it != recoparticlesMap.end()) { edm.addToParticles(it->second); @@ -747,6 +768,9 @@ namespace LCIO2EDM4hepConv { auto shape = lcio->getShape(); auto subdetectorEnergies = lcio->getSubdetectorEnergies(); for (auto c : clusters) { + if (c == nullptr) { + continue; + } const auto it = clustersMap.find(c); if (it != clustersMap.end()) { edm.addToClusters(it->second); @@ -756,6 +780,9 @@ namespace LCIO2EDM4hepConv { } } for (auto cal : calohits) { + if (cal == nullptr) { + continue; + } const auto it = caloHitMap.find(cal); if (it != caloHitMap.end()) { edm.addToHits(it->second); @@ -783,15 +810,21 @@ namespace LCIO2EDM4hepConv { auto tracks = lcio->getTracks(); auto trackerHits = lcio->getTrackerHits(); for (auto t : tracks) { + if (t == nullptr) { + continue; + } const auto it = tracksMap.find(t); if (it != tracksMap.end()) { edm.addToTracks(it->second); } else { - std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; + //std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; } } for (auto th : trackerHits) { + if (th == nullptr) { + continue; + } const auto it = trackerHitMap.find(th); if (it != trackerHitMap.end()) { edm.addToTrackerHits(it->second); @@ -835,7 +868,9 @@ namespace LCIO2EDM4hepConv { { for (auto& [lcio, edm] : vertexMap) { auto recoparticle = lcio->getAssociatedParticle(); - + if (recoparticle == nullptr) { + continue; + } const auto it = recoparticleMap.find(recoparticle); if (it != recoparticleMap.end()) { edm.setAssociatedParticle(it->second); diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 75b74e7e..ef5a0e2c 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -86,8 +86,10 @@ int main(int argc, char* argv[]) std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { - // std::cout << "processing Event: " << i << std::endl; - + //for (auto i = 0u; i < 10; ++i) { + if (i % 100 == 0){ + std::cout << "processing Event: " << i << std::endl; + } auto evt = lcreader->readNextEvent(); // Patching the Event to make sure all events contain the same Collections. if (patching == true) { From 4606481a8886db80f795f45b280e1349e058572e Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Tue, 7 Mar 2023 18:49:08 +0200 Subject: [PATCH 22/83] [clang-format] Format fixes --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 16 ++++++++-------- standalone/lcio2edm4hep.cpp | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index e8582828..68681fb7 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -651,8 +651,8 @@ namespace LCIO2EDM4hepConv { for (auto& [lcio, edm] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); if (mcps == nullptr) { - continue; - } + continue; + } const auto it = mcparticlesMap.find(mcps); if (it != mcparticlesMap.end()) { edm.setMCParticle(it->second); @@ -677,8 +677,8 @@ namespace LCIO2EDM4hepConv { auto vertex = lcio->getStartVertex(); if (vertex == nullptr) { - continue; - } + continue; + } if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { edm.setStartVertex(it->second); } @@ -715,7 +715,7 @@ namespace LCIO2EDM4hepConv { } else { std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " - "while trying to resolve the ReconstructedParticle Relations" + "while trying to resolve the ReconstructedParticle Relations" << std::endl; } } @@ -818,7 +818,7 @@ namespace LCIO2EDM4hepConv { edm.addToTracks(it->second); } else { - //std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; + // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; } } for (auto th : trackerHits) { @@ -869,8 +869,8 @@ namespace LCIO2EDM4hepConv { for (auto& [lcio, edm] : vertexMap) { auto recoparticle = lcio->getAssociatedParticle(); if (recoparticle == nullptr) { - continue; - } + continue; + } const auto it = recoparticleMap.find(recoparticle); if (it != recoparticleMap.end()) { edm.setAssociatedParticle(it->second); diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index ef5a0e2c..b3dc0f52 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -86,8 +86,8 @@ int main(int argc, char* argv[]) std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { - //for (auto i = 0u; i < 10; ++i) { - if (i % 100 == 0){ + // for (auto i = 0u; i < 10; ++i) { + if (i % 100 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); From 01ec81bf79205093ac276962752cb35f458eae53 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 13 Mar 2023 16:44:05 +0100 Subject: [PATCH 23/83] made a seperate function to create simCaloHitContributionCollections --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 68681fb7..679f0aeb 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -550,6 +550,45 @@ namespace LCIO2EDM4hepConv { return retColls; } + std::unique_ptr create_calo_contrColl( + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap){ + { + auto contrCollection = std::make_unique(); + for (auto& [lcioHit,edmHit] : SimCaloHitMap) { + + auto NMCParticle = lcioHit->getNMCParticles(); + for (unsigned j = 0; j < NMCParticle; j++) { + auto edm_contr = contrCollection->create(); + + edm_contr.setPDG(lcioHit->getPDGCont(j)); + edm_contr.setTime(lcioHit->getTimeCont(j)); + edm_contr.setEnergy(lcioHit->getEnergyCont(j)); + edm_contr.setStepPosition(lcioHit->getStepPosition(j)); + auto lcioParticle = (lcioHit -> getParticleCont(j)); + if (lcioParticle == nullptr){ + edm_contr.setParticle(nullptr); + } + else{ + const auto it = mcparticlesMap.find(lcioParticle); + if (it != mcparticlesMap.end()) { + edm_contr.setParticle(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the parents of MCParticles Collections" + << std::endl; + } + } + edmHit.addToContributions(edm_contr); + } + } + return contrCollection; + } + + + } + podio::Frame convertEvent(EVENT::LCEvent* evt) { auto typeMapping = LcioEdmTypeMapping {}; @@ -589,11 +628,13 @@ namespace LCIO2EDM4hepConv { } } // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. + auto calocontr = create_calo_contrColl(typeMapping.simCaloHits,typeMapping.mcParticles); resolveRelations(typeMapping); auto assoCollVec = createAssociations(typeMapping, LCRelations); podio::Frame event; // Now everything is done and we simply populate a Frame + event.put(std::move(calocontr),"CaloHitContribution"); for (auto& [name, coll] : edmevent) { event.put(std::move(coll), name); } @@ -603,6 +644,8 @@ namespace LCIO2EDM4hepConv { return event; } + + void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) { int edmnum = 1; From d529718bd03c73d0e75d2b72f5eb049552846b10 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 13 Mar 2023 17:01:47 +0100 Subject: [PATCH 24/83] changed the name of the createCaloHitContributions to be in line with other names and removed now redundant code. --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 17 ++++----- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 38 +------------------ 2 files changed, 10 insertions(+), 45 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 75cd48e3..cef81485 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -352,6 +352,14 @@ namespace LCIO2EDM4hepConv { return assocColl; } + /** + * Creates the CaloHitContributions for all SimCaloHits. + * has to be done this way, since the converted McParticles are needeed. + */ + std::unique_ptr createCaloHitContributions( + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap); + /** * Resolve the relations for the MCParticles. */ @@ -373,15 +381,6 @@ namespace LCIO2EDM4hepConv { const TypeMapT& clusterMap, const TypeMapT& tracksMap); - /** - * Resolve the relations for SimCalorimeterHits - * - * TODO: Handle the Contributions correctly - */ - void resolveRelationsSimCalorimeterHit( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap); - /** * Resolve the relations for Clusters */ diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 679f0aeb..5c434a27 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -375,7 +375,6 @@ namespace LCIO2EDM4hepConv { TypeMapT& SimCaloHitMap) { auto dest = std::make_unique(); - auto contr = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); @@ -386,17 +385,6 @@ namespace LCIO2EDM4hepConv { lval.setEnergy(rval->getEnergy()); lval.setPosition(rval->getPosition()); - auto NMCParticle = rval->getNMCParticles(); - for (unsigned j = 0; j < NMCParticle; j++) { - auto edm_contr = contr->create(); - - edm_contr.setPDG(rval->getPDGCont(j)); - edm_contr.setTime(rval->getTimeCont(j)); - edm_contr.setEnergy(rval->getEnergyCont(j)); - edm_contr.setStepPosition(rval->getStepPosition(j)); - - lval.addToContributions(edm_contr); - } const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; @@ -408,7 +396,6 @@ namespace LCIO2EDM4hepConv { std::vector results; results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "_contribution", std::move(contr)); return results; } @@ -550,7 +537,7 @@ namespace LCIO2EDM4hepConv { return retColls; } - std::unique_ptr create_calo_contrColl( + std::unique_ptr createCaloHitContributions( TypeMapT& SimCaloHitMap, const TypeMapT& mcparticlesMap){ { @@ -628,7 +615,7 @@ namespace LCIO2EDM4hepConv { } } // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. - auto calocontr = create_calo_contrColl(typeMapping.simCaloHits,typeMapping.mcParticles); + auto calocontr = createCaloHitContributions(typeMapping.simCaloHits,typeMapping.mcParticles); resolveRelations(typeMapping); auto assoCollVec = createAssociations(typeMapping, LCRelations); @@ -781,26 +768,6 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsSimCalorimeterHit( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap) - { - // TODO: Currently not doing anything here because we cannot get a mutable - // CaloHitContribution from the SimCalorimeterHit via the podio generated - // interface. The underlying issue is https://github.com/AIDASoft/podio/issues/347 - - // for (auto& [lcio, edm] : SimCaloHitMap) { - // auto contributionLen = lcio->getNMCParticles(); - // for (auto i = 0; i < contributionLen; i++) { - // auto mcp = lcio->getParticleCont(i); - // const auto it = mcparticlesMap.find(mcp); - // if (it != mcparticlesMap.end()) { - // edm.getContributions(i).setParticle(it->second); // This breaks - // } - // } - // } - } - void resolveRelationsCluster( TypeMapT& clustersMap, const TypeMapT& caloHitMap) @@ -931,7 +898,6 @@ namespace LCIO2EDM4hepConv { resolveRelationsRecoParticle( typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); - resolveRelationsSimCalorimeterHit(typeMapping.simCaloHits, typeMapping.mcParticles); resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); resolveRelationsTrack( typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); From ec4b3310ccf0069f78b38ae2adf6c0a8db04c2c3 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Mon, 13 Mar 2023 17:59:55 +0100 Subject: [PATCH 25/83] [clang-format] Fix formatting --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 10 +-- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 61 +++++++++---------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index cef81485..af4003aa 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -353,13 +353,13 @@ namespace LCIO2EDM4hepConv { } /** - * Creates the CaloHitContributions for all SimCaloHits. - * has to be done this way, since the converted McParticles are needeed. - */ - std::unique_ptr createCaloHitContributions( + * Creates the CaloHitContributions for all SimCaloHits. + * has to be done this way, since the converted McParticles are needeed. + */ + std::unique_ptr createCaloHitContributions( TypeMapT& SimCaloHitMap, const TypeMapT& mcparticlesMap); - + /** * Resolve the relations for the MCParticles. */ diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 5c434a27..47aedd57 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -539,41 +539,40 @@ namespace LCIO2EDM4hepConv { std::unique_ptr createCaloHitContributions( TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap){ + const TypeMapT& mcparticlesMap) { - auto contrCollection = std::make_unique(); - for (auto& [lcioHit,edmHit] : SimCaloHitMap) { - - auto NMCParticle = lcioHit->getNMCParticles(); - for (unsigned j = 0; j < NMCParticle; j++) { - auto edm_contr = contrCollection->create(); - - edm_contr.setPDG(lcioHit->getPDGCont(j)); - edm_contr.setTime(lcioHit->getTimeCont(j)); - edm_contr.setEnergy(lcioHit->getEnergyCont(j)); - edm_contr.setStepPosition(lcioHit->getStepPosition(j)); - auto lcioParticle = (lcioHit -> getParticleCont(j)); - if (lcioParticle == nullptr){ - edm_contr.setParticle(nullptr); - } - else{ - const auto it = mcparticlesMap.find(lcioParticle); - if (it != mcparticlesMap.end()) { - edm_contr.setParticle(it->second); + { + auto contrCollection = std::make_unique(); + for (auto& [lcioHit, edmHit] : SimCaloHitMap) { + + auto NMCParticle = lcioHit->getNMCParticles(); + for (unsigned j = 0; j < NMCParticle; j++) { + auto edm_contr = contrCollection->create(); + + edm_contr.setPDG(lcioHit->getPDGCont(j)); + edm_contr.setTime(lcioHit->getTimeCont(j)); + edm_contr.setEnergy(lcioHit->getEnergyCont(j)); + edm_contr.setStepPosition(lcioHit->getStepPosition(j)); + auto lcioParticle = (lcioHit->getParticleCont(j)); + if (lcioParticle == nullptr) { + edm_contr.setParticle(nullptr); } else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" - << std::endl; + const auto it = mcparticlesMap.find(lcioParticle); + if (it != mcparticlesMap.end()) { + edm_contr.setParticle(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the parents of MCParticles Collections" + << std::endl; + } } + edmHit.addToContributions(edm_contr); } - edmHit.addToContributions(edm_contr); } + return contrCollection; } - return contrCollection; - } - - } podio::Frame convertEvent(EVENT::LCEvent* evt) @@ -615,13 +614,13 @@ namespace LCIO2EDM4hepConv { } } // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. - auto calocontr = createCaloHitContributions(typeMapping.simCaloHits,typeMapping.mcParticles); + auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); resolveRelations(typeMapping); auto assoCollVec = createAssociations(typeMapping, LCRelations); podio::Frame event; // Now everything is done and we simply populate a Frame - event.put(std::move(calocontr),"CaloHitContribution"); + event.put(std::move(calocontr), "CaloHitContribution"); for (auto& [name, coll] : edmevent) { event.put(std::move(coll), name); } @@ -631,8 +630,6 @@ namespace LCIO2EDM4hepConv { return event; } - - void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) { int edmnum = 1; From 991cd4eafcad4f2f8934050c11e3cb846c846181 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 15 Mar 2023 14:18:35 +0100 Subject: [PATCH 26/83] some small adjustments to names and docstrings --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 4 - k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 1034 ----------------- 2 files changed, 1038 deletions(-) delete mode 100644 k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index af4003aa..8237012c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -218,10 +218,6 @@ namespace LCIO2EDM4hepConv { /** * Convert a SimCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. - * - * NOTE: Returns two collections, since the contributions are a separate - * CaloHitContributions are a separate collection in EDM4hep. The name for the - * contributions is simply _contributions */ std::vector convertSimCalorimeterHit( const std::string& name, diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp deleted file mode 100644 index 47aedd57..00000000 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ /dev/null @@ -1,1034 +0,0 @@ -#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" - -#include - -namespace LCIO2EDM4hepConv { - edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState) - { - auto edmtrackState = edm4hep::TrackState {}; - edmtrackState.location = trackState->getLocation(); - edmtrackState.D0 = trackState->getD0(); - edmtrackState.phi = trackState->getPhi(); - edmtrackState.omega = trackState->getOmega(); - edmtrackState.Z0 = trackState->getZ0(); - edmtrackState.tanLambda = trackState->getTanLambda(); - // not available in lcio - edmtrackState.time = -1; - const auto refPoint = trackState->getReferencePoint(); - edmtrackState.referencePoint = Vector3fFrom({refPoint[0], refPoint[1], refPoint[2]}); - const auto& covMatrix = trackState->getCovMatrix(); - edmtrackState.covMatrix = { - covMatrix[0], - covMatrix[1], - covMatrix[2], - covMatrix[3], - covMatrix[4], - covMatrix[5], - covMatrix[6], - covMatrix[7], - covMatrix[8], - covMatrix[9], - covMatrix[10], - covMatrix[11], - covMatrix[12], - covMatrix[13], - covMatrix[15], - 0, - 0, - 0, - 0, - 0, - 0}; - - return edmtrackState; - } - - edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID* pid) - { - auto result = edm4hep::MutableParticleID {}; - result.setType(pid->getType()); - result.setPDG(pid->getPDG()); - result.setAlgorithmType(pid->getAlgorithmType()); - result.setLikelihood(pid->getLikelihood()); - - for (auto v : pid->getParameters()) { - result.addToParameters(v); - } - - return result; - } - - std::unique_ptr convertMCParticle( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& mcparticlesMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setPDG(rval->getPDG()); - lval.setGeneratorStatus(rval->getGeneratorStatus()); - lval.setSimulatorStatus(rval->getSimulatorStatus()); - lval.setCharge(rval->getCharge()); - lval.setTime(rval->getTime()); - lval.setMass(rval->getMass()); - lval.setSpin(edm4hep::Vector3f(rval->getSpin())); - lval.setColorFlow(edm4hep::Vector2i(rval->getColorFlow())); - lval.setVertex(edm4hep::Vector3d(rval->getVertex())); - lval.setEndpoint(edm4hep::Vector3d(rval->getEndpoint())); - lval.setMomentum(Vector3fFrom(rval->getMomentum())); - lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); - - const auto [iterator, inserted] = mcparticlesMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element" << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - std::vector convertReconstructedParticle( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap, - TypeMapT& particleIDMap) - { - auto dest = std::make_unique(); - auto particleIDs = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setCharge(rval->getCharge()); - auto& m = rval->getCovMatrix(); // 10 parameters - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); - lval.setEnergy(rval->getEnergy()); - lval.setGoodnessOfPID(rval->getGoodnessOfPID()); - lval.setMass(rval->getMass()); - lval.setMomentum(Vector3fFrom(rval->getMomentum())); - lval.setReferencePoint(rval->getReferencePoint()); - lval.setType(rval->getType()); - - const auto [iterator, inserted] = recoparticlesMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - - // Need to convert the particle IDs here, since they are part of the reco - // particle collection in LCIO - for (const auto lcioPid : rval->getParticleIDs()) { - auto pid = convertParticleID(lcioPid); - const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); - if (pidInserted) { - lval.addToParticleIDs(pid); - particleIDs->push_back(pid); - } - else { - lval.addToParticleIDs(pidIt->second); - } - } - - const auto lcioPidUsed = rval->getParticleIDUsed(); - if (lcioPidUsed == nullptr) { - continue; - } - if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { - lval.setParticleIDUsed(it->second); - } - else { - auto pid = convertParticleID(lcioPidUsed); - particleIDs->push_back(pid); - particleIDMap.emplace(lcioPidUsed, pid); - lval.setParticleIDUsed(pid); - } - } - - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "_particleIDs", std::move(particleIDs)); - return results; - } - - std::unique_ptr convertVertex( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& vertexMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary - lval.setChi2(rval->getChi2()); - lval.setProbability(rval->getProbability()); - lval.setPosition(rval->getPosition()); - auto& m = rval->getCovMatrix(); // 6 parameters - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - // FIXME: the algorithm type in LCIO is a string, but an integer is expected - // lval.setAlgorithmType(rval->getAlgorithmType()); - // lval.setAssociatedParticle(); //set it when convert ReconstructedParticle - // - for (auto v : rval->getParameters()) { - lval.addToParameters(v); - } - - const auto [iterator, inserted] = vertexMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - std::unique_ptr convertSimTrackerHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimTrHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setEDep(rval->getEDep()); - lval.setTime(rval->getTime()); - lval.setPathLength(rval->getPathLength()); - lval.setQuality(rval->getQuality()); - lval.setPosition(rval->getPosition()); - lval.setMomentum(rval->getMomentum()); - - const auto [iterator, inserted] = SimTrHitMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - std::unique_ptr convertTPCHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setCellID(rval->getCellID()); - lval.setTime(rval->getTime()); - lval.setCharge(rval->getCharge()); - lval.setQuality(rval->getQuality()); - for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { - lval.addToAdcCounts(rval->getRawDataWord(j)); - } - const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - std::unique_ptr convertTrackerHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setType(rval->getType()); - lval.setQuality(rval->getQuality()); - lval.setTime(rval->getTime()); - lval.setEDep(rval->getEDep()); - lval.setEDepError(rval->getEDepError()); - lval.setPosition(rval->getPosition()); - auto& m = rval->getCovMatrix(); - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - - const auto [iterator, inserted] = TrackerHitMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - std::unique_ptr convertTrackerHitPlane( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitPlaneMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setType(rval->getType()); - lval.setQuality(rval->getQuality()); - lval.setTime(rval->getTime()); - lval.setEDep(rval->getEDep()); - lval.setEDepError(rval->getEDepError()); - lval.setPosition(rval->getPosition()); - lval.setU({rval->getU()[0], rval->getU()[1]}); - lval.setV({rval->getV()[0], rval->getV()[1]}); - lval.setDu(rval->getdU()); - lval.setDv(rval->getdV()); - auto& m = rval->getCovMatrix(); - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - - const auto [iterator, inserted] = TrackerHitPlaneMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - std::unique_ptr convertTrack( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setType(rval->getType()); - lval.setChi2(rval->getChi2()); - lval.setNdf(rval->getNdf()); - lval.setDEdx(rval->getdEdx()); - lval.setDEdxError(rval->getdEdxError()); - lval.setRadiusOfInnermostHit(rval->getRadiusOfInnermostHit()); - - auto subdetectorHitNum = rval->getSubdetectorHitNumbers(); - for (auto hitNum : subdetectorHitNum) { - lval.addToSubDetectorHitNumbers(hitNum); - } - auto& trackStates = rval->getTrackStates(); - for (auto& trackState : trackStates) { - lval.addToTrackStates(convertTrackState(trackState)); - } - auto quantities = edm4hep::Quantity {}; - quantities.value = rval->getdEdx(); - quantities.error = rval->getdEdxError(); - lval.addToDxQuantities(quantities); - const auto [iterator, inserted] = TrackMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - std::vector convertSimCalorimeterHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimCaloHitMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setEnergy(rval->getEnergy()); - lval.setPosition(rval->getPosition()); - - const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - std::vector results; - results.emplace_back(name, std::move(dest)); - - return results; - } - - std::unique_ptr convertRawCalorimeterHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& rawCaloHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setAmplitude(rval->getAmplitude()); - lval.setTimeStamp(rval->getTimeStamp()); - - const auto [iterator, inserted] = rawCaloHitMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - std::unique_ptr convertCalorimeterHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& caloHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setEnergy(rval->getEnergy()); - lval.setEnergyError(rval->getEnergyError()); - lval.setPosition(rval->getPosition()); - lval.setTime(rval->getTime()); - lval.setType(rval->getType()); - - const auto [iterator, inserted] = caloHitMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->second; - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - std::unique_ptr convertCluster( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setEnergy(rval->getEnergy()); - lval.setEnergyError(rval->getEnergyError()); - lval.setITheta(rval->getITheta()); - lval.setPhi(rval->getIPhi()); - lval.setPosition(rval->getPosition()); - auto& m = rval->getPositionError(); - lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); - lval.setType(rval->getType()); - lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); - - const auto [iterator, inserted] = clusterMap.emplace(rval, lval); - if (!inserted) { - auto existing = iterator->first; - const auto existingId = existing->id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - std::vector - convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) - { - const auto& type = LCCollection->getTypeName(); - std::vector retColls; - if (type == "MCParticle") { - retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); - } - else if (type == "ReconstructedParticle") { - return convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); - } - else if (type == "Vertex") { - retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); - } - else if (type == "Track") { - retColls.emplace_back(name, convertTrack(name, LCCollection, typeMapping.tracks)); - } - else if (type == "Cluster") { - retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); - } - else if (type == "SimCalorimeterHit") { - return convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits); - } - else if (type == "RawCalorimeterHit") { - retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); - } - else if (type == "CalorimeterHit") { - retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.caloHits)); - } - else if (type == "SimTrackerHit") { - retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simTrackerHits)); - } - else if (type == "TPCHit") { - retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpcHits)); - } - else if (type == "TrackerHit") { - retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); - } - else if (type == "TrackerHitPlane") { - retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); - } - return retColls; - } - - std::unique_ptr createCaloHitContributions( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap) - { - { - auto contrCollection = std::make_unique(); - for (auto& [lcioHit, edmHit] : SimCaloHitMap) { - - auto NMCParticle = lcioHit->getNMCParticles(); - for (unsigned j = 0; j < NMCParticle; j++) { - auto edm_contr = contrCollection->create(); - - edm_contr.setPDG(lcioHit->getPDGCont(j)); - edm_contr.setTime(lcioHit->getTimeCont(j)); - edm_contr.setEnergy(lcioHit->getEnergyCont(j)); - edm_contr.setStepPosition(lcioHit->getStepPosition(j)); - auto lcioParticle = (lcioHit->getParticleCont(j)); - if (lcioParticle == nullptr) { - edm_contr.setParticle(nullptr); - } - else { - const auto it = mcparticlesMap.find(lcioParticle); - if (it != mcparticlesMap.end()) { - edm_contr.setParticle(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" - << std::endl; - } - } - edmHit.addToContributions(edm_contr); - } - } - return contrCollection; - } - } - - podio::Frame convertEvent(EVENT::LCEvent* evt) - { - auto typeMapping = LcioEdmTypeMapping {}; - std::vector edmevent; - std::vector> LCRelations; - const auto& lcnames = evt->getCollectionNames(); - // In this loop the data gets converted. - for (const auto& lcioname : *lcnames) { - const auto& lcioColl = evt->getCollection(lcioname); - const auto& lciotype = lcioColl->getTypeName(); - if (lciotype == "LCRelations") { - LCRelations.push_back(std::make_pair(lcioname, lcioColl)); - // We handle Relations (aka Associations) once we have converted all the - // data parts. - continue; - } - - if (!lcioColl->isSubset()) { - for (auto&& [name, edmColl] : convertCollection(lcioname, lcioColl, typeMapping)) { - if (edmColl != nullptr) { - edmevent.emplace_back(std::move(name), std::move(edmColl)); - } - } - } - } - - // Filling of the Subset Colections - for (const auto& lcioname : *lcnames) { - - auto lcioColl = evt->getCollection(lcioname); - if (lcioColl->isSubset()) { - const auto& lciotype = lcioColl->getTypeName(); - auto edmColl = fillSubSet(lcioColl, typeMapping, lciotype); - if (edmColl != nullptr) { - edmevent.emplace_back(lcioname, std::move(edmColl)); - } - } - } - // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. - auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); - resolveRelations(typeMapping); - auto assoCollVec = createAssociations(typeMapping, LCRelations); - - podio::Frame event; - // Now everything is done and we simply populate a Frame - event.put(std::move(calocontr), "CaloHitContribution"); - for (auto& [name, coll] : edmevent) { - event.put(std::move(coll), name); - } - for (auto& [name, coll] : assoCollVec) { - event.put(std::move(coll), name); - } - return event; - } - - void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) - { - int edmnum = 1; - for (auto& [lcio, edm] : mcparticlesMap) { - edmnum++; - auto daughters = lcio->getDaughters(); - auto parents = lcio->getParents(); - - int dnum = 1; - for (auto d : daughters) { - if (d == nullptr) { - continue; - } - const auto it = mcparticlesMap.find(d); - dnum++; - if (it != mcparticlesMap.end()) { - edm.addToDaughters(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " - "while trying to resolve the daughters of MCParticles" - << std::endl; - } - } - for (auto p : parents) { - if (p == nullptr) { - continue; - } - const auto it = mcparticlesMap.find(p); - if (it != mcparticlesMap.end()) { - edm.addToParents(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" - << std::endl; - } - } - } - } - - void resolveRelationsSimTrackerHit( - TypeMapT& SimTrHitMap, - TypeMapT& mcparticlesMap) - { - for (auto& [lcio, edm] : SimTrHitMap) { - auto mcps = lcio->getMCParticle(); - if (mcps == nullptr) { - continue; - } - const auto it = mcparticlesMap.find(mcps); - if (it != mcparticlesMap.end()) { - edm.setMCParticle(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the SimTrackHit Relations" - << std::endl; - } - } - } - - void resolveRelationsRecoParticle( - TypeMapT& recoparticlesMap, - const TypeMapT& vertexMap, - const TypeMapT& clusterMap, - const TypeMapT& tracksMap) - { - int edmnum = 1; - for (auto& [lcio, edm] : recoparticlesMap) { - edmnum++; - - auto vertex = lcio->getStartVertex(); - if (vertex == nullptr) { - continue; - } - if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { - edm.setStartVertex(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " - "while trying to resolve the ReconstructedParticle Relations " - << std::endl; - } - - auto clusters = lcio->getClusters(); - for (auto c : clusters) { - if (c == nullptr) { - continue; - } - const auto it = clusterMap.find(c); - if (it != clusterMap.end()) { - edm.addToClusters(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; - } - } - - auto tracks = lcio->getTracks(); - for (auto t : tracks) { - if (t == nullptr) { - continue; - } - const auto it = tracksMap.find(t); - if (it != tracksMap.end()) { - edm.addToTracks(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; - } - } - - auto parents = lcio->getParticles(); - for (auto p : parents) { - if (p == nullptr) { - continue; - } - const auto it = recoparticlesMap.find(p); - if (it != recoparticlesMap.end()) { - edm.addToParticles(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " - "while trying to resolve the ReconstructedParticles parents Relations" - << std::endl; - } - } - } - } - - void resolveRelationsCluster( - TypeMapT& clustersMap, - const TypeMapT& caloHitMap) - { - for (auto& [lcio, edm] : clustersMap) { - auto clusters = lcio->getClusters(); - auto calohits = lcio->getCalorimeterHits(); - auto shape = lcio->getShape(); - auto subdetectorEnergies = lcio->getSubdetectorEnergies(); - for (auto c : clusters) { - if (c == nullptr) { - continue; - } - const auto it = clustersMap.find(c); - if (it != clustersMap.end()) { - edm.addToClusters(it->second); - } - else { - std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; - } - } - for (auto cal : calohits) { - if (cal == nullptr) { - continue; - } - const auto it = caloHitMap.find(cal); - if (it != caloHitMap.end()) { - edm.addToHits(it->second); - } - else { - std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; - } - } - for (auto s : shape) { - edm.addToShapeParameters(s); - } - for (auto subE : subdetectorEnergies) { - edm.addToSubdetectorEnergies(subE); - } - } - } - - void resolveRelationsTrack( - TypeMapT& tracksMap, - const TypeMapT& trackerHitMap, - const TypeMapT& TPCHitMap, - const TypeMapT& trackerhitplaneMap) - { - for (auto& [lcio, edm] : tracksMap) { - auto tracks = lcio->getTracks(); - auto trackerHits = lcio->getTrackerHits(); - for (auto t : tracks) { - if (t == nullptr) { - continue; - } - const auto it = tracksMap.find(t); - if (it != tracksMap.end()) { - edm.addToTracks(it->second); - } - else { - // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; - } - } - for (auto th : trackerHits) { - if (th == nullptr) { - continue; - } - const auto it = trackerHitMap.find(th); - if (it != trackerHitMap.end()) { - edm.addToTrackerHits(it->second); - } - // else { - // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" - // << " This is due to it being a TrackerHitPlane or TPCHit" << std::endl; - - // // This Code looks for the trackerHit in the TPCHit Map aswell as the - // // trackerHitPlane Map. Those relations can not be set for a track in - // // edm4HEP. In all tests the missing trackerHits were located in - // // either of these maps. - // const auto tpchit = dynamic_cast(th); - // const auto trackerhitplane = dynamic_cast(th); - // if (tpchit != nullptr) { - // const auto it = TPCHitMap.find(tpchit); - // if (it != TPCHitMap.end()) { - // std::cout << "trackerHit found in TPCHit map !" << std::endl; - // } - // else { - // std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; - // } - // } - // else if (trackerhitplane != nullptr) { - // const auto it = trackerhitplaneMap.find(trackerhitplane); - // if (it != trackerhitplaneMap.end()) { - // std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; - // } - // else { - // std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; - // } - // } - // } - } - } - } - - void resolveRelationsVertex( - TypeMapT& vertexMap, - const TypeMapT& recoparticleMap) - { - for (auto& [lcio, edm] : vertexMap) { - auto recoparticle = lcio->getAssociatedParticle(); - if (recoparticle == nullptr) { - continue; - } - const auto it = recoparticleMap.find(recoparticle); - if (it != recoparticleMap.end()) { - edm.setAssociatedParticle(it->second); - } - else { - std::cerr << "Couldn't find associated Particle to add to Vertex " - << "Relations in edm" << std::endl; - } - } - } - - void resolveRelations(LcioEdmTypeMapping& typeMapping) - { - resolveRelationsMCParticle(typeMapping.mcParticles); - resolveRelationsRecoParticle( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); - resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); - resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); - resolveRelationsTrack( - typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); - resolveRelationsVertex(typeMapping.vertices, typeMapping.recoParticles); - } - - std::vector createAssociations( - const LcioEdmTypeMapping& typeMapping, - const std::vector>& LCRelation) - { - std::vector assoCollVec; - for (const auto& [name, relations] : LCRelation) { - const auto& params = relations->getParameters(); - - const auto& fromType = params.getStringVal("FromType"); - const auto& toType = params.getStringVal("ToType"); - - if (fromType == "MCParticle" && toType == "ReconstructedParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.recoParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.caloHits, typeMapping.simCaloHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.simCaloHits, typeMapping.caloHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Cluster" && toType == "MCParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.clusters, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "MCParticle" && toType == "Cluster") { - auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.clusters); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "MCParticle" && toType == "Track") { - auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.tracks); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Track" && toType == "MCParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.tracks, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHits, typeMapping.simTrackerHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { - auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "ReconstructedParticle" && toType == "Vertex") { - auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.vertices); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Vertex" && toType == "reconstructedparticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.vertices, typeMapping.recoParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - } - - return assoCollVec; - } - - std::unique_ptr - fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) - { - if (type == "MCParticle") { - return handleSubsetColl(LCCollection, typeMapping.mcParticles); - } - else if (type == "ReconstructedParticle") { - return handleSubsetColl(LCCollection, typeMapping.recoParticles); - } - else if (type == "Vertex") { - return handleSubsetColl(LCCollection, typeMapping.vertices); - } - else if (type == "Track") { - return handleSubsetColl(LCCollection, typeMapping.tracks); - } - else if (type == "Cluster") { - return handleSubsetColl(LCCollection, typeMapping.clusters); - } - else if (type == "SimCalorimeterHit") { - return handleSubsetColl(LCCollection, typeMapping.simCaloHits); - } - else if (type == "RawCalorimeterHit") { - return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); - } - else if (type == "CalorimeterHit") { - return handleSubsetColl(LCCollection, typeMapping.caloHits); - } - else if (type == "SimTrackerHit") { - return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); - } - else if (type == "TPCHit") { - return handleSubsetColl(LCCollection, typeMapping.tpcHits); - } - else if (type == "TrackerHit") { - return handleSubsetColl(LCCollection, typeMapping.trackerHits); - } - else if (type == "TrackerHitPlane") { - return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); - } - else { - return nullptr; - } - } - -} // namespace LCIO2EDM4hepConv From 4ffaf81fb38e8f05ff2006dd1db18f4418b54444 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 15 Mar 2023 14:46:35 +0100 Subject: [PATCH 27/83] restoring file --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 1034 +++++++++++++++++ 1 file changed, 1034 insertions(+) create mode 100644 k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp new file mode 100644 index 00000000..47aedd57 --- /dev/null +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -0,0 +1,1034 @@ +#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" + +#include + +namespace LCIO2EDM4hepConv { + edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState) + { + auto edmtrackState = edm4hep::TrackState {}; + edmtrackState.location = trackState->getLocation(); + edmtrackState.D0 = trackState->getD0(); + edmtrackState.phi = trackState->getPhi(); + edmtrackState.omega = trackState->getOmega(); + edmtrackState.Z0 = trackState->getZ0(); + edmtrackState.tanLambda = trackState->getTanLambda(); + // not available in lcio + edmtrackState.time = -1; + const auto refPoint = trackState->getReferencePoint(); + edmtrackState.referencePoint = Vector3fFrom({refPoint[0], refPoint[1], refPoint[2]}); + const auto& covMatrix = trackState->getCovMatrix(); + edmtrackState.covMatrix = { + covMatrix[0], + covMatrix[1], + covMatrix[2], + covMatrix[3], + covMatrix[4], + covMatrix[5], + covMatrix[6], + covMatrix[7], + covMatrix[8], + covMatrix[9], + covMatrix[10], + covMatrix[11], + covMatrix[12], + covMatrix[13], + covMatrix[15], + 0, + 0, + 0, + 0, + 0, + 0}; + + return edmtrackState; + } + + edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID* pid) + { + auto result = edm4hep::MutableParticleID {}; + result.setType(pid->getType()); + result.setPDG(pid->getPDG()); + result.setAlgorithmType(pid->getAlgorithmType()); + result.setLikelihood(pid->getLikelihood()); + + for (auto v : pid->getParameters()) { + result.addToParameters(v); + } + + return result; + } + + std::unique_ptr convertMCParticle( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& mcparticlesMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setPDG(rval->getPDG()); + lval.setGeneratorStatus(rval->getGeneratorStatus()); + lval.setSimulatorStatus(rval->getSimulatorStatus()); + lval.setCharge(rval->getCharge()); + lval.setTime(rval->getTime()); + lval.setMass(rval->getMass()); + lval.setSpin(edm4hep::Vector3f(rval->getSpin())); + lval.setColorFlow(edm4hep::Vector2i(rval->getColorFlow())); + lval.setVertex(edm4hep::Vector3d(rval->getVertex())); + lval.setEndpoint(edm4hep::Vector3d(rval->getEndpoint())); + lval.setMomentum(Vector3fFrom(rval->getMomentum())); + lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); + + const auto [iterator, inserted] = mcparticlesMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element" << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::vector convertReconstructedParticle( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& recoparticlesMap, + TypeMapT& particleIDMap) + { + auto dest = std::make_unique(); + auto particleIDs = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setCharge(rval->getCharge()); + auto& m = rval->getCovMatrix(); // 10 parameters + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); + lval.setEnergy(rval->getEnergy()); + lval.setGoodnessOfPID(rval->getGoodnessOfPID()); + lval.setMass(rval->getMass()); + lval.setMomentum(Vector3fFrom(rval->getMomentum())); + lval.setReferencePoint(rval->getReferencePoint()); + lval.setType(rval->getType()); + + const auto [iterator, inserted] = recoparticlesMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + + // Need to convert the particle IDs here, since they are part of the reco + // particle collection in LCIO + for (const auto lcioPid : rval->getParticleIDs()) { + auto pid = convertParticleID(lcioPid); + const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); + if (pidInserted) { + lval.addToParticleIDs(pid); + particleIDs->push_back(pid); + } + else { + lval.addToParticleIDs(pidIt->second); + } + } + + const auto lcioPidUsed = rval->getParticleIDUsed(); + if (lcioPidUsed == nullptr) { + continue; + } + if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { + lval.setParticleIDUsed(it->second); + } + else { + auto pid = convertParticleID(lcioPidUsed); + particleIDs->push_back(pid); + particleIDMap.emplace(lcioPidUsed, pid); + lval.setParticleIDUsed(pid); + } + } + + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_particleIDs", std::move(particleIDs)); + return results; + } + + std::unique_ptr convertVertex( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& vertexMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary + lval.setChi2(rval->getChi2()); + lval.setProbability(rval->getProbability()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getCovMatrix(); // 6 parameters + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + // FIXME: the algorithm type in LCIO is a string, but an integer is expected + // lval.setAlgorithmType(rval->getAlgorithmType()); + // lval.setAssociatedParticle(); //set it when convert ReconstructedParticle + // + for (auto v : rval->getParameters()) { + lval.addToParameters(v); + } + + const auto [iterator, inserted] = vertexMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertSimTrackerHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimTrHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEDep(rval->getEDep()); + lval.setTime(rval->getTime()); + lval.setPathLength(rval->getPathLength()); + lval.setQuality(rval->getQuality()); + lval.setPosition(rval->getPosition()); + lval.setMomentum(rval->getMomentum()); + + const auto [iterator, inserted] = SimTrHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertTPCHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TPCHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setCellID(rval->getCellID()); + lval.setTime(rval->getTime()); + lval.setCharge(rval->getCharge()); + lval.setQuality(rval->getQuality()); + for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { + lval.addToAdcCounts(rval->getRawDataWord(j)); + } + const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertTrackerHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setType(rval->getType()); + lval.setQuality(rval->getQuality()); + lval.setTime(rval->getTime()); + lval.setEDep(rval->getEDep()); + lval.setEDepError(rval->getEDepError()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getCovMatrix(); + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + + const auto [iterator, inserted] = TrackerHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + std::unique_ptr convertTrackerHitPlane( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitPlaneMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setType(rval->getType()); + lval.setQuality(rval->getQuality()); + lval.setTime(rval->getTime()); + lval.setEDep(rval->getEDep()); + lval.setEDepError(rval->getEDepError()); + lval.setPosition(rval->getPosition()); + lval.setU({rval->getU()[0], rval->getU()[1]}); + lval.setV({rval->getV()[0], rval->getV()[1]}); + lval.setDu(rval->getdU()); + lval.setDv(rval->getdV()); + auto& m = rval->getCovMatrix(); + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + + const auto [iterator, inserted] = TrackerHitPlaneMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertTrack( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setType(rval->getType()); + lval.setChi2(rval->getChi2()); + lval.setNdf(rval->getNdf()); + lval.setDEdx(rval->getdEdx()); + lval.setDEdxError(rval->getdEdxError()); + lval.setRadiusOfInnermostHit(rval->getRadiusOfInnermostHit()); + + auto subdetectorHitNum = rval->getSubdetectorHitNumbers(); + for (auto hitNum : subdetectorHitNum) { + lval.addToSubDetectorHitNumbers(hitNum); + } + auto& trackStates = rval->getTrackStates(); + for (auto& trackState : trackStates) { + lval.addToTrackStates(convertTrackState(trackState)); + } + auto quantities = edm4hep::Quantity {}; + quantities.value = rval->getdEdx(); + quantities.error = rval->getdEdxError(); + lval.addToDxQuantities(quantities); + const auto [iterator, inserted] = TrackMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::vector convertSimCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimCaloHitMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEnergy(rval->getEnergy()); + lval.setPosition(rval->getPosition()); + + const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + std::vector results; + results.emplace_back(name, std::move(dest)); + + return results; + } + + std::unique_ptr convertRawCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& rawCaloHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setAmplitude(rval->getAmplitude()); + lval.setTimeStamp(rval->getTimeStamp()); + + const auto [iterator, inserted] = rawCaloHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertCalorimeterHit( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& caloHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEnergy(rval->getEnergy()); + lval.setEnergyError(rval->getEnergyError()); + lval.setPosition(rval->getPosition()); + lval.setTime(rval->getTime()); + lval.setType(rval->getType()); + + const auto [iterator, inserted] = caloHitMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->second; + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::unique_ptr convertCluster( + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& clusterMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setEnergy(rval->getEnergy()); + lval.setEnergyError(rval->getEnergyError()); + lval.setITheta(rval->getITheta()); + lval.setPhi(rval->getIPhi()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getPositionError(); + lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); + lval.setType(rval->getType()); + lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); + + const auto [iterator, inserted] = clusterMap.emplace(rval, lval); + if (!inserted) { + auto existing = iterator->first; + const auto existingId = existing->id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + std::vector + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) + { + const auto& type = LCCollection->getTypeName(); + std::vector retColls; + if (type == "MCParticle") { + retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); + } + else if (type == "ReconstructedParticle") { + return convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); + } + else if (type == "Vertex") { + retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); + } + else if (type == "Track") { + retColls.emplace_back(name, convertTrack(name, LCCollection, typeMapping.tracks)); + } + else if (type == "Cluster") { + retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); + } + else if (type == "SimCalorimeterHit") { + return convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits); + } + else if (type == "RawCalorimeterHit") { + retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); + } + else if (type == "CalorimeterHit") { + retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.caloHits)); + } + else if (type == "SimTrackerHit") { + retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simTrackerHits)); + } + else if (type == "TPCHit") { + retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpcHits)); + } + else if (type == "TrackerHit") { + retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); + } + else if (type == "TrackerHitPlane") { + retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); + } + return retColls; + } + + std::unique_ptr createCaloHitContributions( + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap) + { + { + auto contrCollection = std::make_unique(); + for (auto& [lcioHit, edmHit] : SimCaloHitMap) { + + auto NMCParticle = lcioHit->getNMCParticles(); + for (unsigned j = 0; j < NMCParticle; j++) { + auto edm_contr = contrCollection->create(); + + edm_contr.setPDG(lcioHit->getPDGCont(j)); + edm_contr.setTime(lcioHit->getTimeCont(j)); + edm_contr.setEnergy(lcioHit->getEnergyCont(j)); + edm_contr.setStepPosition(lcioHit->getStepPosition(j)); + auto lcioParticle = (lcioHit->getParticleCont(j)); + if (lcioParticle == nullptr) { + edm_contr.setParticle(nullptr); + } + else { + const auto it = mcparticlesMap.find(lcioParticle); + if (it != mcparticlesMap.end()) { + edm_contr.setParticle(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the parents of MCParticles Collections" + << std::endl; + } + } + edmHit.addToContributions(edm_contr); + } + } + return contrCollection; + } + } + + podio::Frame convertEvent(EVENT::LCEvent* evt) + { + auto typeMapping = LcioEdmTypeMapping {}; + std::vector edmevent; + std::vector> LCRelations; + const auto& lcnames = evt->getCollectionNames(); + // In this loop the data gets converted. + for (const auto& lcioname : *lcnames) { + const auto& lcioColl = evt->getCollection(lcioname); + const auto& lciotype = lcioColl->getTypeName(); + if (lciotype == "LCRelations") { + LCRelations.push_back(std::make_pair(lcioname, lcioColl)); + // We handle Relations (aka Associations) once we have converted all the + // data parts. + continue; + } + + if (!lcioColl->isSubset()) { + for (auto&& [name, edmColl] : convertCollection(lcioname, lcioColl, typeMapping)) { + if (edmColl != nullptr) { + edmevent.emplace_back(std::move(name), std::move(edmColl)); + } + } + } + } + + // Filling of the Subset Colections + for (const auto& lcioname : *lcnames) { + + auto lcioColl = evt->getCollection(lcioname); + if (lcioColl->isSubset()) { + const auto& lciotype = lcioColl->getTypeName(); + auto edmColl = fillSubSet(lcioColl, typeMapping, lciotype); + if (edmColl != nullptr) { + edmevent.emplace_back(lcioname, std::move(edmColl)); + } + } + } + // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. + auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); + resolveRelations(typeMapping); + auto assoCollVec = createAssociations(typeMapping, LCRelations); + + podio::Frame event; + // Now everything is done and we simply populate a Frame + event.put(std::move(calocontr), "CaloHitContribution"); + for (auto& [name, coll] : edmevent) { + event.put(std::move(coll), name); + } + for (auto& [name, coll] : assoCollVec) { + event.put(std::move(coll), name); + } + return event; + } + + void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) + { + int edmnum = 1; + for (auto& [lcio, edm] : mcparticlesMap) { + edmnum++; + auto daughters = lcio->getDaughters(); + auto parents = lcio->getParents(); + + int dnum = 1; + for (auto d : daughters) { + if (d == nullptr) { + continue; + } + const auto it = mcparticlesMap.find(d); + dnum++; + if (it != mcparticlesMap.end()) { + edm.addToDaughters(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " + "while trying to resolve the daughters of MCParticles" + << std::endl; + } + } + for (auto p : parents) { + if (p == nullptr) { + continue; + } + const auto it = mcparticlesMap.find(p); + if (it != mcparticlesMap.end()) { + edm.addToParents(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the parents of MCParticles Collections" + << std::endl; + } + } + } + } + + void resolveRelationsSimTrackerHit( + TypeMapT& SimTrHitMap, + TypeMapT& mcparticlesMap) + { + for (auto& [lcio, edm] : SimTrHitMap) { + auto mcps = lcio->getMCParticle(); + if (mcps == nullptr) { + continue; + } + const auto it = mcparticlesMap.find(mcps); + if (it != mcparticlesMap.end()) { + edm.setMCParticle(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the SimTrackHit Relations" + << std::endl; + } + } + } + + void resolveRelationsRecoParticle( + TypeMapT& recoparticlesMap, + const TypeMapT& vertexMap, + const TypeMapT& clusterMap, + const TypeMapT& tracksMap) + { + int edmnum = 1; + for (auto& [lcio, edm] : recoparticlesMap) { + edmnum++; + + auto vertex = lcio->getStartVertex(); + if (vertex == nullptr) { + continue; + } + if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { + edm.setStartVertex(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " + "while trying to resolve the ReconstructedParticle Relations " + << std::endl; + } + + auto clusters = lcio->getClusters(); + for (auto c : clusters) { + if (c == nullptr) { + continue; + } + const auto it = clusterMap.find(c); + if (it != clusterMap.end()) { + edm.addToClusters(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + + auto tracks = lcio->getTracks(); + for (auto t : tracks) { + if (t == nullptr) { + continue; + } + const auto it = tracksMap.find(t); + if (it != tracksMap.end()) { + edm.addToTracks(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + + auto parents = lcio->getParticles(); + for (auto p : parents) { + if (p == nullptr) { + continue; + } + const auto it = recoparticlesMap.find(p); + if (it != recoparticlesMap.end()) { + edm.addToParticles(it->second); + } + else { + std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " + "while trying to resolve the ReconstructedParticles parents Relations" + << std::endl; + } + } + } + } + + void resolveRelationsCluster( + TypeMapT& clustersMap, + const TypeMapT& caloHitMap) + { + for (auto& [lcio, edm] : clustersMap) { + auto clusters = lcio->getClusters(); + auto calohits = lcio->getCalorimeterHits(); + auto shape = lcio->getShape(); + auto subdetectorEnergies = lcio->getSubdetectorEnergies(); + for (auto c : clusters) { + if (c == nullptr) { + continue; + } + const auto it = clustersMap.find(c); + if (it != clustersMap.end()) { + edm.addToClusters(it->second); + } + else { + std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; + } + } + for (auto cal : calohits) { + if (cal == nullptr) { + continue; + } + const auto it = caloHitMap.find(cal); + if (it != caloHitMap.end()) { + edm.addToHits(it->second); + } + else { + std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; + } + } + for (auto s : shape) { + edm.addToShapeParameters(s); + } + for (auto subE : subdetectorEnergies) { + edm.addToSubdetectorEnergies(subE); + } + } + } + + void resolveRelationsTrack( + TypeMapT& tracksMap, + const TypeMapT& trackerHitMap, + const TypeMapT& TPCHitMap, + const TypeMapT& trackerhitplaneMap) + { + for (auto& [lcio, edm] : tracksMap) { + auto tracks = lcio->getTracks(); + auto trackerHits = lcio->getTrackerHits(); + for (auto t : tracks) { + if (t == nullptr) { + continue; + } + const auto it = tracksMap.find(t); + if (it != tracksMap.end()) { + edm.addToTracks(it->second); + } + else { + // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; + } + } + for (auto th : trackerHits) { + if (th == nullptr) { + continue; + } + const auto it = trackerHitMap.find(th); + if (it != trackerHitMap.end()) { + edm.addToTrackerHits(it->second); + } + // else { + // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" + // << " This is due to it being a TrackerHitPlane or TPCHit" << std::endl; + + // // This Code looks for the trackerHit in the TPCHit Map aswell as the + // // trackerHitPlane Map. Those relations can not be set for a track in + // // edm4HEP. In all tests the missing trackerHits were located in + // // either of these maps. + // const auto tpchit = dynamic_cast(th); + // const auto trackerhitplane = dynamic_cast(th); + // if (tpchit != nullptr) { + // const auto it = TPCHitMap.find(tpchit); + // if (it != TPCHitMap.end()) { + // std::cout << "trackerHit found in TPCHit map !" << std::endl; + // } + // else { + // std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; + // } + // } + // else if (trackerhitplane != nullptr) { + // const auto it = trackerhitplaneMap.find(trackerhitplane); + // if (it != trackerhitplaneMap.end()) { + // std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; + // } + // else { + // std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; + // } + // } + // } + } + } + } + + void resolveRelationsVertex( + TypeMapT& vertexMap, + const TypeMapT& recoparticleMap) + { + for (auto& [lcio, edm] : vertexMap) { + auto recoparticle = lcio->getAssociatedParticle(); + if (recoparticle == nullptr) { + continue; + } + const auto it = recoparticleMap.find(recoparticle); + if (it != recoparticleMap.end()) { + edm.setAssociatedParticle(it->second); + } + else { + std::cerr << "Couldn't find associated Particle to add to Vertex " + << "Relations in edm" << std::endl; + } + } + } + + void resolveRelations(LcioEdmTypeMapping& typeMapping) + { + resolveRelationsMCParticle(typeMapping.mcParticles); + resolveRelationsRecoParticle( + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); + resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); + resolveRelationsTrack( + typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); + resolveRelationsVertex(typeMapping.vertices, typeMapping.recoParticles); + } + + std::vector createAssociations( + const LcioEdmTypeMapping& typeMapping, + const std::vector>& LCRelation) + { + std::vector assoCollVec; + for (const auto& [name, relations] : LCRelation) { + const auto& params = relations->getParameters(); + + const auto& fromType = params.getStringVal("FromType"); + const auto& toType = params.getStringVal("ToType"); + + if (fromType == "MCParticle" && toType == "ReconstructedParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.recoParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.recoParticles, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.caloHits, typeMapping.simCaloHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simCaloHits, typeMapping.caloHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Cluster" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.clusters, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "MCParticle" && toType == "Cluster") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.clusters); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "MCParticle" && toType == "Track") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.tracks); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Track" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.tracks, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.trackerHits, typeMapping.simTrackerHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simTrackerHits, typeMapping.trackerHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "ReconstructedParticle" && toType == "Vertex") { + auto mc_a = createAssociationCollection( + relations, typeMapping.recoParticles, typeMapping.vertices); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Vertex" && toType == "reconstructedparticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.vertices, typeMapping.recoParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + } + + return assoCollVec; + } + + std::unique_ptr + fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) + { + if (type == "MCParticle") { + return handleSubsetColl(LCCollection, typeMapping.mcParticles); + } + else if (type == "ReconstructedParticle") { + return handleSubsetColl(LCCollection, typeMapping.recoParticles); + } + else if (type == "Vertex") { + return handleSubsetColl(LCCollection, typeMapping.vertices); + } + else if (type == "Track") { + return handleSubsetColl(LCCollection, typeMapping.tracks); + } + else if (type == "Cluster") { + return handleSubsetColl(LCCollection, typeMapping.clusters); + } + else if (type == "SimCalorimeterHit") { + return handleSubsetColl(LCCollection, typeMapping.simCaloHits); + } + else if (type == "RawCalorimeterHit") { + return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); + } + else if (type == "CalorimeterHit") { + return handleSubsetColl(LCCollection, typeMapping.caloHits); + } + else if (type == "SimTrackerHit") { + return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); + } + else if (type == "TPCHit") { + return handleSubsetColl(LCCollection, typeMapping.tpcHits); + } + else if (type == "TrackerHit") { + return handleSubsetColl(LCCollection, typeMapping.trackerHits); + } + else if (type == "TrackerHitPlane") { + return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); + } + else { + return nullptr; + } + } + +} // namespace LCIO2EDM4hepConv From 1351d582f8419ae25d5a849558dec2a011438cc5 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 15 Mar 2023 14:50:35 +0100 Subject: [PATCH 28/83] some small changes to names and error messages --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 47aedd57..5f3da499 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,7 +564,7 @@ namespace LCIO2EDM4hepConv { } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" + "while trying to build CaloHitContributions " << std::endl; } } @@ -614,13 +614,15 @@ namespace LCIO2EDM4hepConv { } } // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. - auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); + resolveRelations(typeMapping); auto assoCollVec = createAssociations(typeMapping, LCRelations); + // creating the CaloHitContributions to fill them into the Frame + auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); podio::Frame event; // Now everything is done and we simply populate a Frame - event.put(std::move(calocontr), "CaloHitContribution"); + event.put(std::move(calocontr), "combinedCaloHitContribution"); for (auto& [name, coll] : edmevent) { event.put(std::move(coll), name); } From 8ca79c421c08882f084acb616e713beb23fc1a37 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 15 Mar 2023 18:05:59 +0100 Subject: [PATCH 29/83] Fixing a typo making the Associations not work --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 5f3da499..96f74f11 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -585,7 +585,7 @@ namespace LCIO2EDM4hepConv { for (const auto& lcioname : *lcnames) { const auto& lcioColl = evt->getCollection(lcioname); const auto& lciotype = lcioColl->getTypeName(); - if (lciotype == "LCRelations") { + if (lciotype == "LCRelation") { LCRelations.push_back(std::make_pair(lcioname, lcioColl)); // We handle Relations (aka Associations) once we have converted all the // data parts. From 76f0d57d619b9b64796c2e61fedf55836a1d91fb Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 20 Mar 2023 11:36:49 +0100 Subject: [PATCH 30/83] slight change to the interface of convertSimCalorimeterHit to make it more inline with the other methods --- .../include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 3 ++- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 10 +++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 8237012c..e16d21c6 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -219,7 +219,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::vector convertSimCalorimeterHit( + std::unique_ptr convertSimCalorimeterHit( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& SimCaloHitMap); @@ -351,6 +351,7 @@ namespace LCIO2EDM4hepConv { /** * Creates the CaloHitContributions for all SimCaloHits. * has to be done this way, since the converted McParticles are needeed. + * The contributions are also attached to their corresponding SimCalorimeterHits. */ std::unique_ptr createCaloHitContributions( TypeMapT& SimCaloHitMap, diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 96f74f11..973f3c00 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -369,7 +369,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::vector convertSimCalorimeterHit( + std::unique_ptr convertSimCalorimeterHit( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& SimCaloHitMap) @@ -394,10 +394,7 @@ namespace LCIO2EDM4hepConv { } } - std::vector results; - results.emplace_back(name, std::move(dest)); - - return results; + return dest; } std::unique_ptr convertRawCalorimeterHit( @@ -514,7 +511,7 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); } else if (type == "SimCalorimeterHit") { - return convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits); + retColls.emplace_back(name, convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits)); } else if (type == "RawCalorimeterHit") { retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); @@ -614,7 +611,6 @@ namespace LCIO2EDM4hepConv { } } // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. - resolveRelations(typeMapping); auto assoCollVec = createAssociations(typeMapping, LCRelations); // creating the CaloHitContributions to fill them into the Frame From 987af8a390ed17b8afe0872e3d64921c4d9adfd6 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Mon, 20 Mar 2023 15:45:48 +0100 Subject: [PATCH 31/83] Only add MCParticle if there is one to add Remove unnecessary scope --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 973f3c00..a0f18c3a 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -538,38 +538,32 @@ namespace LCIO2EDM4hepConv { TypeMapT& SimCaloHitMap, const TypeMapT& mcparticlesMap) { - { - auto contrCollection = std::make_unique(); - for (auto& [lcioHit, edmHit] : SimCaloHitMap) { - - auto NMCParticle = lcioHit->getNMCParticles(); - for (unsigned j = 0; j < NMCParticle; j++) { - auto edm_contr = contrCollection->create(); - - edm_contr.setPDG(lcioHit->getPDGCont(j)); - edm_contr.setTime(lcioHit->getTimeCont(j)); - edm_contr.setEnergy(lcioHit->getEnergyCont(j)); - edm_contr.setStepPosition(lcioHit->getStepPosition(j)); - auto lcioParticle = (lcioHit->getParticleCont(j)); - if (lcioParticle == nullptr) { - edm_contr.setParticle(nullptr); + auto contrCollection = std::make_unique(); + for (auto& [lcioHit, edmHit] : SimCaloHitMap) { + auto NMCParticle = lcioHit->getNMCParticles(); + for (unsigned j = 0; j < NMCParticle; j++) { + auto edm_contr = contrCollection->create(); + + edm_contr.setPDG(lcioHit->getPDGCont(j)); + edm_contr.setTime(lcioHit->getTimeCont(j)); + edm_contr.setEnergy(lcioHit->getEnergyCont(j)); + edm_contr.setStepPosition(lcioHit->getStepPosition(j)); + auto lcioParticle = (lcioHit->getParticleCont(j)); + if (lcioParticle != nullptr) { + const auto it = mcparticlesMap.find(lcioParticle); + if (it != mcparticlesMap.end()) { + edm_contr.setParticle(it->second); } else { - const auto it = mcparticlesMap.find(lcioParticle); - if (it != mcparticlesMap.end()) { - edm_contr.setParticle(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to build CaloHitContributions " - << std::endl; - } + std::cerr << "Cannot find corresponding EDM4hep MCParticle for a LCIO MCParticle, " + << "while trying to build CaloHitContributions " << std::endl; } - edmHit.addToContributions(edm_contr); } + + edmHit.addToContributions(edm_contr); } - return contrCollection; } + return contrCollection; } podio::Frame convertEvent(EVENT::LCEvent* evt) From bc369f01031a73cc87af1d7bee98f8e58b6f43ce Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Mon, 20 Mar 2023 15:48:12 +0100 Subject: [PATCH 32/83] Rename output collection --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index a0f18c3a..50a043b3 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -543,6 +543,7 @@ namespace LCIO2EDM4hepConv { auto NMCParticle = lcioHit->getNMCParticles(); for (unsigned j = 0; j < NMCParticle; j++) { auto edm_contr = contrCollection->create(); + edmHit.addToContributions(edm_contr); edm_contr.setPDG(lcioHit->getPDGCont(j)); edm_contr.setTime(lcioHit->getTimeCont(j)); @@ -559,8 +560,6 @@ namespace LCIO2EDM4hepConv { << "while trying to build CaloHitContributions " << std::endl; } } - - edmHit.addToContributions(edm_contr); } } return contrCollection; @@ -612,7 +611,7 @@ namespace LCIO2EDM4hepConv { podio::Frame event; // Now everything is done and we simply populate a Frame - event.put(std::move(calocontr), "combinedCaloHitContribution"); + event.put(std::move(calocontr), "AllCaloHitContributionsCombined"); for (auto& [name, coll] : edmevent) { event.put(std::move(coll), name); } From a058fa2569777e37da3d085fd85b225f24206e06 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 22 Mar 2023 13:57:15 +0100 Subject: [PATCH 33/83] Add .vscode folder to .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 56a69053..61f90178 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,7 @@ install* *.app # CMake generated -Testing \ No newline at end of file +Testing + +# Tooling +.vscode \ No newline at end of file From 9cb6427602c469592885e6ddc408642c18705388 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 22 Mar 2023 14:03:12 +0100 Subject: [PATCH 34/83] Change to the tests to handle LCrelations without from type. --- tests/compare_contents.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/compare_contents.cpp b/tests/compare_contents.cpp index 605e99db..5fe6324d 100644 --- a/tests/compare_contents.cpp +++ b/tests/compare_contents.cpp @@ -35,7 +35,7 @@ int main(int argc, char* argv[]) // collection with the same name exists in the edm file. std::cout << "number of Events" << edmreader.getEntries("events") << std::endl; for (size_t n = 0; n < edmreader.getEntries("events"); ++n) { - if (n % 1000 == 0) { + if (n % 10 == 0) { std::cout << "Event number: " << n << std::endl; } @@ -46,6 +46,15 @@ int main(int argc, char* argv[]) const auto lcioColl = lcEvent->getCollection(name); // TODO: The Frame needs to improve here in order to get to the type // without retrieving the collection + if (lcioColl->getTypeName() == "LCRelation"){ + + const auto& params = lcioColl->getParameters(); + const auto& fromType = params.getStringVal("FromType"); + if (fromType.length() == 0){ + //std::cout<<"WARNING: LCRelations "<< name <<" has no 'to' or 'from' set!"<< std::endl; + continue; + } + } const auto& type = [&edmEvent, &name]() { const auto coll = edmEvent.get(name); if (coll) { @@ -63,6 +72,7 @@ int main(int argc, char* argv[]) ASSERT_COMPARE_OR_EXIT(edm4hep::ReconstructedParticleCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::TrackCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::TrackerHitCollection) + ASSERT_COMPARE_OR_EXIT(edm4hep::TrackerHitPlaneCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::SimTrackerHitCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::CalorimeterHitCollection) ASSERT_COMPARE_OR_EXIT(edm4hep::RawCalorimeterHitCollection) From 379afeefa13212a868bbc31cb40b7425b8c2c067 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 22 Mar 2023 14:33:41 +0100 Subject: [PATCH 35/83] Chnage in compare for Vectors to make it nan safe. --- tests/src/ComparisonUtils.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/src/ComparisonUtils.h b/tests/src/ComparisonUtils.h index 05646160..96d1a267 100644 --- a/tests/src/ComparisonUtils.h +++ b/tests/src/ComparisonUtils.h @@ -9,6 +9,7 @@ #include "UTIL/LCIterator.h" #include "EVENT/LCCollection.h" +#include #include #include #include @@ -59,12 +60,17 @@ bool operator!=(const std::vector& vec, const std::array& arr) return !(vec == arr); } +template +bool nanSafeComp(T x,U y){ + return (x == y) || (std::isnan(x) && std::isnan(y)); +} + // Macro for defining the comparison operators for edm4hep::Vector3X and // different return types (X* or vector from LCIO) #define VECTOR3_COMPARE(FT, VT) \ bool operator==(const FT* vals, const VT& vec) \ { \ - return vals[0] == vec[0] && vals[1] == vec[1] && vals[2] == vec[2]; \ + return nanSafeComp(vals[0],vec[0]) && nanSafeComp(vals[1],vec[1]) && nanSafeComp(vals[2],vec[2]); \ } \ bool operator!=(const FT* vals, const VT& vec) { return !(vals == vec); } \ bool operator==(const std::vector& vals, const VT& vec) \ From 76216a96f67ba340acb15246981fc2e4000c1359 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 3 Apr 2023 11:48:28 +0200 Subject: [PATCH 36/83] ome debug messages --- standalone/lcio2edm4hep.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index b3dc0f52..04b85110 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -86,14 +86,17 @@ int main(int argc, char* argv[]) std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { - // for (auto i = 0u; i < 10; ++i) { - if (i % 100 == 0) { + //for (auto i = 0u; i < 4; ++i) { + if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); + std::cout<<"read Event"< Date: Mon, 3 Apr 2023 13:06:58 +0200 Subject: [PATCH 37/83] removing some debug prints --- standalone/lcio2edm4hep.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 04b85110..0443df2a 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -94,9 +94,7 @@ int main(int argc, char* argv[]) std::cout<<"read Event"< Date: Mon, 3 Apr 2023 14:16:54 +0200 Subject: [PATCH 38/83] adding a conversion of the EventHeader and its data --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 7 +++++++ k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index e16d21c6..431b3f36 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -251,6 +251,13 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& clusterMap); + /** + * Create an EventHeaderCollection and fills it with the Metadata. + */ + + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt); + + /** * Helper function to create a subset collection from an existing (LCIO) * subset collection. Needs the object mapping as input to resolve to the diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 50a043b3..f833ca42 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,6 +564,17 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt){ + auto headerColl = std::make_unique(); + auto header = headerColl -> create(); + + header.setEventNumber(evt->getEventNumber()); + header.setRunNumber(evt->getRunNumber()); + header.setTimeStamp(evt->getTimeStamp()); + header.setWeight(evt->getWeight()); + return headerColl; + } + podio::Frame convertEvent(EVENT::LCEvent* evt) { @@ -608,10 +619,11 @@ namespace LCIO2EDM4hepConv { auto assoCollVec = createAssociations(typeMapping, LCRelations); // creating the CaloHitContributions to fill them into the Frame auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); - + auto headerColl = createEventHeader(evt); podio::Frame event; // Now everything is done and we simply populate a Frame event.put(std::move(calocontr), "AllCaloHitContributionsCombined"); + event.put(std::move(headerColl), "EventHeader"); for (auto& [name, coll] : edmevent) { event.put(std::move(coll), name); } From a9c125fb7f51ceb57cc3158bdc39d13bf07a6098 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 5 Apr 2023 14:17:46 +0200 Subject: [PATCH 39/83] changed the tests to also check for same EventHeader information. --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 2 +- tests/compare_contents.cpp | 5 +++++ tests/src/CompareEDM4hepLCIO.cc | 12 ++++++++++++ tests/src/CompareEDM4hepLCIO.h | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index f833ca42..f00f8e82 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -568,7 +568,7 @@ namespace LCIO2EDM4hepConv { auto headerColl = std::make_unique(); auto header = headerColl -> create(); - header.setEventNumber(evt->getEventNumber()); + header.setEventNumber(evt->getEventNumber()+1); header.setRunNumber(evt->getRunNumber()); header.setTimeStamp(evt->getTimeStamp()); header.setWeight(evt->getWeight()); diff --git a/tests/compare_contents.cpp b/tests/compare_contents.cpp index 5fe6324d..63e2e920 100644 --- a/tests/compare_contents.cpp +++ b/tests/compare_contents.cpp @@ -18,6 +18,7 @@ constexpr auto usageMsg = R"(usage: compare-contents lciofile edm4hepfile)"; + int main(int argc, char* argv[]) { if (argc != 3) { @@ -42,6 +43,10 @@ int main(int argc, char* argv[]) auto lcEvent = lcreader->readNextEvent(); auto edmEvent = podio::Frame(edmreader.readNextEntry("events")); + if (!compareEventHeader(lcEvent,&edmEvent)){ + return 1; + } + for (const auto& name : *(lcEvent->getCollectionNames())) { const auto lcioColl = lcEvent->getCollection(name); // TODO: The Frame needs to improve here in order to get to the type diff --git a/tests/src/CompareEDM4hepLCIO.cc b/tests/src/CompareEDM4hepLCIO.cc index 098addcb..becb679a 100644 --- a/tests/src/CompareEDM4hepLCIO.cc +++ b/tests/src/CompareEDM4hepLCIO.cc @@ -288,3 +288,15 @@ bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::VertexColl { return compareCollection(lcioCollection, edm4hepCollection); } + +bool compareEventHeader(const EVENT::LCEvent* lcevt, const podio::Frame* edmEvent){ + + const auto& edmEventHeader = edmEvent->get("EventHeader")[0]; + + ASSERT_COMPARE(lcevt,edmEventHeader,getEventNumber, "Event Number is not the same"); + ASSERT_COMPARE(lcevt,edmEventHeader,getRunNumber, "Run Number is not the same"); + ASSERT_COMPARE(lcevt,edmEventHeader,getTimeStamp, "TimeStamp in EventHeader is not the same"); + ASSERT_COMPARE(lcevt,edmEventHeader,getWeight, "Weight in EventHeader is not the same"); + + return true; +} diff --git a/tests/src/CompareEDM4hepLCIO.h b/tests/src/CompareEDM4hepLCIO.h index 7f084555..baa9219b 100644 --- a/tests/src/CompareEDM4hepLCIO.h +++ b/tests/src/CompareEDM4hepLCIO.h @@ -24,6 +24,7 @@ #include "edm4hep/TrackerHitCollection.h" #include "edm4hep/TrackerHitPlaneCollection.h" #include "edm4hep/VertexCollection.h" +#include "podio/Frame.h" #include #include @@ -44,6 +45,8 @@ #include #include + + // bool compare(const EVENT::CaloHitContribution *lcio, // const edm4hep::CaloHitContribution &edm4hep); // bool compare(const lcio::LCCollection *lcioCollection, @@ -92,4 +95,5 @@ bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::TrackColle bool compare(const EVENT::Vertex* lcio, const edm4hep::Vertex& edm4hep); bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::VertexCollection& edm4hepCollection); +bool compareEventHeader(const EVENT::LCEvent* lcevt, const podio::Frame* edmEvent); #endif // K4EDM4HEP2LCIOCONV_TEST_COMPAREEDM4HEPLCIO_H From 6132b0550df758b89968b5fc795b60b2741a01d0 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 12 Apr 2023 11:53:59 +0200 Subject: [PATCH 40/83] [clang-format] Fix style --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 7 ++--- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 8 ++--- standalone/lcio2edm4hep.cpp | 4 +-- tests/compare_contents.cpp | 11 ++++--- tests/src/CompareEDM4hepLCIO.cc | 13 +++++---- tests/src/CompareEDM4hepLCIO.h | 2 -- tests/src/ComparisonUtils.h | 29 ++++++++++--------- 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 431b3f36..4cd4ea04 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -252,12 +252,11 @@ namespace LCIO2EDM4hepConv { TypeMapT& clusterMap); /** - * Create an EventHeaderCollection and fills it with the Metadata. - */ + * Create an EventHeaderCollection and fills it with the Metadata. + */ std::unique_ptr createEventHeader(const EVENT::LCEvent* evt); - - + /** * Helper function to create a subset collection from an existing (LCIO) * subset collection. Needs the object mapping as input to resolve to the diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index f00f8e82..38fdbcd4 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,18 +564,18 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt){ + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) + { auto headerColl = std::make_unique(); - auto header = headerColl -> create(); + auto header = headerColl->create(); - header.setEventNumber(evt->getEventNumber()+1); + header.setEventNumber(evt->getEventNumber() + 1); header.setRunNumber(evt->getRunNumber()); header.setTimeStamp(evt->getTimeStamp()); header.setWeight(evt->getWeight()); return headerColl; } - podio::Frame convertEvent(EVENT::LCEvent* evt) { auto typeMapping = LcioEdmTypeMapping {}; diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 0443df2a..e055cb79 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -86,12 +86,12 @@ int main(int argc, char* argv[]) std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { - //for (auto i = 0u; i < 4; ++i) { + // for (auto i = 0u; i < 4; ++i) { if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); - std::cout<<"read Event"<readNextEvent(); auto edmEvent = podio::Frame(edmreader.readNextEntry("events")); - if (!compareEventHeader(lcEvent,&edmEvent)){ + if (!compareEventHeader(lcEvent, &edmEvent)) { return 1; } @@ -51,14 +50,14 @@ int main(int argc, char* argv[]) const auto lcioColl = lcEvent->getCollection(name); // TODO: The Frame needs to improve here in order to get to the type // without retrieving the collection - if (lcioColl->getTypeName() == "LCRelation"){ + if (lcioColl->getTypeName() == "LCRelation") { const auto& params = lcioColl->getParameters(); const auto& fromType = params.getStringVal("FromType"); - if (fromType.length() == 0){ - //std::cout<<"WARNING: LCRelations "<< name <<" has no 'to' or 'from' set!"<< std::endl; + if (fromType.length() == 0) { + // std::cout<<"WARNING: LCRelations "<< name <<" has no 'to' or 'from' set!"<< std::endl; continue; - } + } } const auto& type = [&edmEvent, &name]() { const auto coll = edmEvent.get(name); diff --git a/tests/src/CompareEDM4hepLCIO.cc b/tests/src/CompareEDM4hepLCIO.cc index becb679a..e9490548 100644 --- a/tests/src/CompareEDM4hepLCIO.cc +++ b/tests/src/CompareEDM4hepLCIO.cc @@ -289,14 +289,15 @@ bool compare(const lcio::LCCollection* lcioCollection, const edm4hep::VertexColl return compareCollection(lcioCollection, edm4hepCollection); } -bool compareEventHeader(const EVENT::LCEvent* lcevt, const podio::Frame* edmEvent){ +bool compareEventHeader(const EVENT::LCEvent* lcevt, const podio::Frame* edmEvent) +{ const auto& edmEventHeader = edmEvent->get("EventHeader")[0]; - ASSERT_COMPARE(lcevt,edmEventHeader,getEventNumber, "Event Number is not the same"); - ASSERT_COMPARE(lcevt,edmEventHeader,getRunNumber, "Run Number is not the same"); - ASSERT_COMPARE(lcevt,edmEventHeader,getTimeStamp, "TimeStamp in EventHeader is not the same"); - ASSERT_COMPARE(lcevt,edmEventHeader,getWeight, "Weight in EventHeader is not the same"); - + ASSERT_COMPARE(lcevt, edmEventHeader, getEventNumber, "Event Number is not the same"); + ASSERT_COMPARE(lcevt, edmEventHeader, getRunNumber, "Run Number is not the same"); + ASSERT_COMPARE(lcevt, edmEventHeader, getTimeStamp, "TimeStamp in EventHeader is not the same"); + ASSERT_COMPARE(lcevt, edmEventHeader, getWeight, "Weight in EventHeader is not the same"); + return true; } diff --git a/tests/src/CompareEDM4hepLCIO.h b/tests/src/CompareEDM4hepLCIO.h index baa9219b..19f5c809 100644 --- a/tests/src/CompareEDM4hepLCIO.h +++ b/tests/src/CompareEDM4hepLCIO.h @@ -45,8 +45,6 @@ #include #include - - // bool compare(const EVENT::CaloHitContribution *lcio, // const edm4hep::CaloHitContribution &edm4hep); // bool compare(const lcio::LCCollection *lcioCollection, diff --git a/tests/src/ComparisonUtils.h b/tests/src/ComparisonUtils.h index 96d1a267..8525ae9a 100644 --- a/tests/src/ComparisonUtils.h +++ b/tests/src/ComparisonUtils.h @@ -61,25 +61,26 @@ bool operator!=(const std::vector& vec, const std::array& arr) } template -bool nanSafeComp(T x,U y){ +bool nanSafeComp(T x, U y) +{ return (x == y) || (std::isnan(x) && std::isnan(y)); } // Macro for defining the comparison operators for edm4hep::Vector3X and // different return types (X* or vector from LCIO) -#define VECTOR3_COMPARE(FT, VT) \ - bool operator==(const FT* vals, const VT& vec) \ - { \ - return nanSafeComp(vals[0],vec[0]) && nanSafeComp(vals[1],vec[1]) && nanSafeComp(vals[2],vec[2]); \ - } \ - bool operator!=(const FT* vals, const VT& vec) { return !(vals == vec); } \ - bool operator==(const std::vector& vals, const VT& vec) \ - { \ - if (vals.size() != 3) { \ - return false; \ - } \ - return vals.data() == vec; \ - } \ +#define VECTOR3_COMPARE(FT, VT) \ + bool operator==(const FT* vals, const VT& vec) \ + { \ + return nanSafeComp(vals[0], vec[0]) && nanSafeComp(vals[1], vec[1]) && nanSafeComp(vals[2], vec[2]); \ + } \ + bool operator!=(const FT* vals, const VT& vec) { return !(vals == vec); } \ + bool operator==(const std::vector& vals, const VT& vec) \ + { \ + if (vals.size() != 3) { \ + return false; \ + } \ + return vals.data() == vec; \ + } \ bool operator!=(const std::vector& vals, const VT& vec) { return !(vals == vec); } VECTOR3_COMPARE(float, edm4hep::Vector3f) From ea8f727f56c5befda37e9a2d974f515d7bdfd332 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 17 Apr 2023 13:43:08 +0200 Subject: [PATCH 41/83] change to the reading of patch Files to ignore spaces infront of type names --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 15 ++++++--- standalone/lcio2edm4hep.cpp | 32 +++++++++++++++++-- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 38fdbcd4..c3d9daef 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,12 +564,11 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) - { + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt){ auto headerColl = std::make_unique(); auto header = headerColl->create(); - header.setEventNumber(evt->getEventNumber() + 1); + header.setEventNumber(evt->getEventNumber()); header.setRunNumber(evt->getRunNumber()); header.setTimeStamp(evt->getTimeStamp()); header.setWeight(evt->getWeight()); @@ -914,6 +913,10 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); + if (fromType.empty() or toType.empty()){ + //std::cout << "From type or to type is not set in relation "<< name <<", while converting LCRelations" << std::endl; + continue; + } if (fromType == "MCParticle" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( @@ -980,11 +983,15 @@ namespace LCIO2EDM4hepConv { relations, typeMapping.recoParticles, typeMapping.vertices); assoCollVec.emplace_back(name, std::move(mc_a)); } - else if (fromType == "Vertex" && toType == "reconstructedparticle") { + else if (fromType == "Vertex" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( relations, typeMapping.vertices, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } + else { + std::cout << "Relation from: "<> getNamesAndTypes(const std::str std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; } std::string line; + /** while (std::getline(input_file, line)) { size_t delimiter_pos = line.find(' '); if (delimiter_pos == std::string::npos) { @@ -29,9 +30,29 @@ std::vector> getNamesAndTypes(const std::str return {}; } std::string name = line.substr(0, delimiter_pos); - std::string type = line.substr(delimiter_pos + 1); + std::string type_space = line.substr(delimiter_pos + 1); + int first_letter; + for (auto elem :line.substr(delimiter_pos + 1)){ + if (elem == ' '){ + first_letter ++; + } + else{ + continue; + } + } + std::string type line.substr(first_letter+delimiter_pos); names_types.emplace_back(name, type); } + */ + while(std::getline(input_file, line)) { + std::stringstream sline(std::move(line)); + std::string name, type; + if (!(sline >> name >> type && sline.eof())) { + std::cerr << "need a name and a type per line" << std::endl; + return{}; + } + names_types.emplace_back(name, type); +} input_file.close(); @@ -84,18 +105,23 @@ int main(int argc, char* argv[]) podio::ROOTFrameWriter writer(outputFile); - std::cout << "starting Conversion" << std::endl; + //std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { // for (auto i = 0u; i < 4; ++i) { if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); - std::cout << "read Event" << std::endl; + //std::cout << "read Event" << std::endl; // Patching the Event to make sure all events contain the same Collections. + try {//std::cout<<"EcalEndcapRingRelationsSimRec before patching: "<< evt->getCollection("EcalEndcapRingRelationsSimRec")<getCollection("EcalEndcapRingRelationsSimRec")< Date: Mon, 17 Apr 2023 14:18:00 +0200 Subject: [PATCH 42/83] removal of some debug output --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 1 - standalone/lcio2edm4hep.cpp | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index c3d9daef..337642f7 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -914,7 +914,6 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); if (fromType.empty() or toType.empty()){ - //std::cout << "From type or to type is not set in relation "<< name <<", while converting LCRelations" << std::endl; continue; } diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 9cea2741..e25c2cb7 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -105,23 +105,18 @@ int main(int argc, char* argv[]) podio::ROOTFrameWriter writer(outputFile); - //std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { - // for (auto i = 0u; i < 4; ++i) { if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); - //std::cout << "read Event" << std::endl; // Patching the Event to make sure all events contain the same Collections. - try {//std::cout<<"EcalEndcapRingRelationsSimRec before patching: "<< evt->getCollection("EcalEndcapRingRelationsSimRec")<getCollection("EcalEndcapRingRelationsSimRec")< Date: Mon, 17 Apr 2023 14:21:15 +0200 Subject: [PATCH 43/83] removing alternative reading function --- standalone/lcio2edm4hep.cpp | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index e25c2cb7..a7618d8b 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -21,29 +21,6 @@ std::vector> getNamesAndTypes(const std::str std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; } std::string line; - /** - while (std::getline(input_file, line)) { - size_t delimiter_pos = line.find(' '); - if (delimiter_pos == std::string::npos) { - std::cerr << "Invalid input format" << std::endl; - input_file.close(); - return {}; - } - std::string name = line.substr(0, delimiter_pos); - std::string type_space = line.substr(delimiter_pos + 1); - int first_letter; - for (auto elem :line.substr(delimiter_pos + 1)){ - if (elem == ' '){ - first_letter ++; - } - else{ - continue; - } - } - std::string type line.substr(first_letter+delimiter_pos); - names_types.emplace_back(name, type); - } - */ while(std::getline(input_file, line)) { std::stringstream sline(std::move(line)); std::string name, type; From ef708df703259e6e4ef504a1df9629cd4e4c914e Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Mon, 17 Apr 2023 14:25:12 +0200 Subject: [PATCH 44/83] Avoid unnecessary string copies --- standalone/lcio2edm4hep.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index a7618d8b..7bce7389 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -28,7 +28,7 @@ std::vector> getNamesAndTypes(const std::str std::cerr << "need a name and a type per line" << std::endl; return{}; } - names_types.emplace_back(name, type); + names_types.emplace_back(std::move(name), std::move(type)); } input_file.close(); From 3e46e9bfe8378bcfc93fea834f4ee0fe76a4a92f Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 17 Apr 2023 14:31:10 +0200 Subject: [PATCH 45/83] Remove debug try-catch block --- standalone/lcio2edm4hep.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 7bce7389..452de6b1 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -88,9 +88,6 @@ int main(int argc, char* argv[]) } auto evt = lcreader->readNextEvent(); // Patching the Event to make sure all events contain the same Collections. - try { - } - catch(...){std::cout<<"EcalEndcapRingRelationsSimRec is not present"< Date: Mon, 17 Apr 2023 14:31:37 +0200 Subject: [PATCH 46/83] [clang-format] Fix formatting --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 9 +++++---- standalone/lcio2edm4hep.cpp | 16 ++++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 337642f7..2b79bbf1 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,7 +564,8 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt){ + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) + { auto headerColl = std::make_unique(); auto header = headerColl->create(); @@ -913,7 +914,7 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); - if (fromType.empty() or toType.empty()){ + if (fromType.empty() or toType.empty()) { continue; } @@ -988,9 +989,9 @@ namespace LCIO2EDM4hepConv { assoCollVec.emplace_back(name, std::move(mc_a)); } else { - std::cout << "Relation from: "<> getNamesAndTypes(const std::str std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; } std::string line; - while(std::getline(input_file, line)) { - std::stringstream sline(std::move(line)); - std::string name, type; - if (!(sline >> name >> type && sline.eof())) { - std::cerr << "need a name and a type per line" << std::endl; - return{}; + while (std::getline(input_file, line)) { + std::stringstream sline(std::move(line)); + std::string name, type; + if (!(sline >> name >> type && sline.eof())) { + std::cerr << "need a name and a type per line" << std::endl; + return {}; + } + names_types.emplace_back(std::move(name), std::move(type)); } - names_types.emplace_back(std::move(name), std::move(type)); -} input_file.close(); From b7b81ef0bdc18a7048d889a04a786361f586742a Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 17 Apr 2023 15:20:41 +0200 Subject: [PATCH 47/83] Add a warning for missing From/ToType when converting relations --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 2b79bbf1..4e11fb4a 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -914,7 +914,9 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); - if (fromType.empty() or toType.empty()) { + if (fromType.empty() || toType.empty()) { + std::cerr << "LCRelation collection has missing FromType or ToType parameters. " + << "Cannot convert it without this information." << std::endl; continue; } From 1b08060e4ef22c95456af7daa006189f449c398b Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 17 Apr 2023 13:43:08 +0200 Subject: [PATCH 48/83] change to the reading of patch Files to ignore spaces infront of type names --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 15 ++++++--- standalone/lcio2edm4hep.cpp | 32 +++++++++++++++++-- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 38fdbcd4..c3d9daef 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,12 +564,11 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) - { + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt){ auto headerColl = std::make_unique(); auto header = headerColl->create(); - header.setEventNumber(evt->getEventNumber() + 1); + header.setEventNumber(evt->getEventNumber()); header.setRunNumber(evt->getRunNumber()); header.setTimeStamp(evt->getTimeStamp()); header.setWeight(evt->getWeight()); @@ -914,6 +913,10 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); + if (fromType.empty() or toType.empty()){ + //std::cout << "From type or to type is not set in relation "<< name <<", while converting LCRelations" << std::endl; + continue; + } if (fromType == "MCParticle" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( @@ -980,11 +983,15 @@ namespace LCIO2EDM4hepConv { relations, typeMapping.recoParticles, typeMapping.vertices); assoCollVec.emplace_back(name, std::move(mc_a)); } - else if (fromType == "Vertex" && toType == "reconstructedparticle") { + else if (fromType == "Vertex" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( relations, typeMapping.vertices, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } + else { + std::cout << "Relation from: "<> getNamesAndTypes(const std::str std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; } std::string line; + /** while (std::getline(input_file, line)) { size_t delimiter_pos = line.find(' '); if (delimiter_pos == std::string::npos) { @@ -29,9 +30,29 @@ std::vector> getNamesAndTypes(const std::str return {}; } std::string name = line.substr(0, delimiter_pos); - std::string type = line.substr(delimiter_pos + 1); + std::string type_space = line.substr(delimiter_pos + 1); + int first_letter; + for (auto elem :line.substr(delimiter_pos + 1)){ + if (elem == ' '){ + first_letter ++; + } + else{ + continue; + } + } + std::string type line.substr(first_letter+delimiter_pos); names_types.emplace_back(name, type); } + */ + while(std::getline(input_file, line)) { + std::stringstream sline(std::move(line)); + std::string name, type; + if (!(sline >> name >> type && sline.eof())) { + std::cerr << "need a name and a type per line" << std::endl; + return{}; + } + names_types.emplace_back(name, type); +} input_file.close(); @@ -84,18 +105,23 @@ int main(int argc, char* argv[]) podio::ROOTFrameWriter writer(outputFile); - std::cout << "starting Conversion" << std::endl; + //std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { // for (auto i = 0u; i < 4; ++i) { if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); - std::cout << "read Event" << std::endl; + //std::cout << "read Event" << std::endl; // Patching the Event to make sure all events contain the same Collections. + try {//std::cout<<"EcalEndcapRingRelationsSimRec before patching: "<< evt->getCollection("EcalEndcapRingRelationsSimRec")<getCollection("EcalEndcapRingRelationsSimRec")< Date: Mon, 17 Apr 2023 14:18:00 +0200 Subject: [PATCH 49/83] removal of some debug output --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 1 - standalone/lcio2edm4hep.cpp | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index c3d9daef..337642f7 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -914,7 +914,6 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); if (fromType.empty() or toType.empty()){ - //std::cout << "From type or to type is not set in relation "<< name <<", while converting LCRelations" << std::endl; continue; } diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 9cea2741..e25c2cb7 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -105,23 +105,18 @@ int main(int argc, char* argv[]) podio::ROOTFrameWriter writer(outputFile); - //std::cout << "starting Conversion" << std::endl; for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { - // for (auto i = 0u; i < 4; ++i) { if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; } auto evt = lcreader->readNextEvent(); - //std::cout << "read Event" << std::endl; // Patching the Event to make sure all events contain the same Collections. - try {//std::cout<<"EcalEndcapRingRelationsSimRec before patching: "<< evt->getCollection("EcalEndcapRingRelationsSimRec")<getCollection("EcalEndcapRingRelationsSimRec")< Date: Mon, 17 Apr 2023 14:21:15 +0200 Subject: [PATCH 50/83] removing alternative reading function --- standalone/lcio2edm4hep.cpp | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index e25c2cb7..a7618d8b 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -21,29 +21,6 @@ std::vector> getNamesAndTypes(const std::str std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; } std::string line; - /** - while (std::getline(input_file, line)) { - size_t delimiter_pos = line.find(' '); - if (delimiter_pos == std::string::npos) { - std::cerr << "Invalid input format" << std::endl; - input_file.close(); - return {}; - } - std::string name = line.substr(0, delimiter_pos); - std::string type_space = line.substr(delimiter_pos + 1); - int first_letter; - for (auto elem :line.substr(delimiter_pos + 1)){ - if (elem == ' '){ - first_letter ++; - } - else{ - continue; - } - } - std::string type line.substr(first_letter+delimiter_pos); - names_types.emplace_back(name, type); - } - */ while(std::getline(input_file, line)) { std::stringstream sline(std::move(line)); std::string name, type; From 8a7bd10cd8fc8b6bf2e387898ad627211a0e14a8 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Mon, 17 Apr 2023 14:25:12 +0200 Subject: [PATCH 51/83] Avoid unnecessary string copies --- standalone/lcio2edm4hep.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index a7618d8b..7bce7389 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -28,7 +28,7 @@ std::vector> getNamesAndTypes(const std::str std::cerr << "need a name and a type per line" << std::endl; return{}; } - names_types.emplace_back(name, type); + names_types.emplace_back(std::move(name), std::move(type)); } input_file.close(); From d30b363f9b00252ec72c6bdc643f13fbc700e345 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 17 Apr 2023 14:31:10 +0200 Subject: [PATCH 52/83] Remove debug try-catch block --- standalone/lcio2edm4hep.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 7bce7389..452de6b1 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -88,9 +88,6 @@ int main(int argc, char* argv[]) } auto evt = lcreader->readNextEvent(); // Patching the Event to make sure all events contain the same Collections. - try { - } - catch(...){std::cout<<"EcalEndcapRingRelationsSimRec is not present"< Date: Mon, 17 Apr 2023 14:31:37 +0200 Subject: [PATCH 53/83] [clang-format] Fix formatting --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 9 +++++---- standalone/lcio2edm4hep.cpp | 16 ++++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 337642f7..2b79bbf1 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -564,7 +564,8 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt){ + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) + { auto headerColl = std::make_unique(); auto header = headerColl->create(); @@ -913,7 +914,7 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); - if (fromType.empty() or toType.empty()){ + if (fromType.empty() or toType.empty()) { continue; } @@ -988,9 +989,9 @@ namespace LCIO2EDM4hepConv { assoCollVec.emplace_back(name, std::move(mc_a)); } else { - std::cout << "Relation from: "<> getNamesAndTypes(const std::str std::cerr << "Failed to open file countaining the names and types of the LCIO Collections." << std::endl; } std::string line; - while(std::getline(input_file, line)) { - std::stringstream sline(std::move(line)); - std::string name, type; - if (!(sline >> name >> type && sline.eof())) { - std::cerr << "need a name and a type per line" << std::endl; - return{}; + while (std::getline(input_file, line)) { + std::stringstream sline(std::move(line)); + std::string name, type; + if (!(sline >> name >> type && sline.eof())) { + std::cerr << "need a name and a type per line" << std::endl; + return {}; + } + names_types.emplace_back(std::move(name), std::move(type)); } - names_types.emplace_back(std::move(name), std::move(type)); -} input_file.close(); From da2d34a68d04f96f3c4dc633dfc7e4aa0c48c338 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 17 Apr 2023 15:20:41 +0200 Subject: [PATCH 54/83] Add a warning for missing From/ToType when converting relations --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 2b79bbf1..4e11fb4a 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -914,7 +914,9 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); - if (fromType.empty() or toType.empty()) { + if (fromType.empty() || toType.empty()) { + std::cerr << "LCRelation collection has missing FromType or ToType parameters. " + << "Cannot convert it without this information." << std::endl; continue; } From 8643c126e7f706cda10c98d538a915c79e619b42 Mon Sep 17 00:00:00 2001 From: FinnJohannsen <57949495+FinnJohannsen@users.noreply.github.com> Date: Mon, 24 Apr 2023 14:29:49 +0200 Subject: [PATCH 55/83] Create README.md --- standalone/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 standalone/README.md diff --git a/standalone/README.md b/standalone/README.md new file mode 100644 index 00000000..ea21dec3 --- /dev/null +++ b/standalone/README.md @@ -0,0 +1,16 @@ +To convert an lcio file to edm4HEP either a lcio file where all collections are present in every event +or a file containing a list of all collections in the file are needed. +This list can be obtained by executing check_missing_cols from the lcio package with the --minimal flag. + +Example: +./lcio/install/bin/check_missing_cols /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio > patch.txt + +This file will be the third argument when the converter ist called. + +./standalone/lcio2edm4hep /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio Output.root patch.txt + +The resulting root file will be "Output.root" + + +A few things to note: +ralations that do not have a "toType" and "fromType" set are currently not beeing converted. If check_missing_cols is run without the minimal flag a warning will be given for each Ralations this applies to. From fa31c892e09f0aafe693e5eedd1421ac3425a4ed Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Mon, 17 Apr 2023 14:25:12 +0200 Subject: [PATCH 56/83] Avoid unnecessary string copies --- standalone/lcio2edm4hep.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 5c7514e5..d422cdde 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -30,6 +30,8 @@ std::vector> getNamesAndTypes(const std::str } names_types.emplace_back(std::move(name), std::move(type)); } + names_types.emplace_back(std::move(name), std::move(type)); +} input_file.close(); From 49be81c604c56bc3ad230bae857ecfec4a41530b Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 17 Apr 2023 14:31:37 +0200 Subject: [PATCH 57/83] [clang-format] Fix formatting --- standalone/lcio2edm4hep.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index d422cdde..5c7514e5 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -30,8 +30,6 @@ std::vector> getNamesAndTypes(const std::str } names_types.emplace_back(std::move(name), std::move(type)); } - names_types.emplace_back(std::move(name), std::move(type)); -} input_file.close(); From e45bf00e34439b2b4dca8023d0c0a2440ad36ec2 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 26 Apr 2023 13:50:09 +0200 Subject: [PATCH 58/83] fixing of some typos --- standalone/README.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/standalone/README.md b/standalone/README.md index ea21dec3..38390132 100644 --- a/standalone/README.md +++ b/standalone/README.md @@ -1,16 +1,27 @@ -To convert an lcio file to edm4HEP either a lcio file where all collections are present in every event +To convert aa LCIO file to EDM4hep either a LCIO file where all collections are present in every event or a file containing a list of all collections in the file are needed. -This list can be obtained by executing check_missing_cols from the lcio package with the --minimal flag. +This list can be obtained by executing 'check_missing_cols' from the LCIO package with the '--minimal' flag. Example: -./lcio/install/bin/check_missing_cols /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio > patch.txt + + +```bash +check_missing_cols --minimal \ + /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ + > patch.txt +``` This file will be the third argument when the converter ist called. -./standalone/lcio2edm4hep /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio Output.root patch.txt +```bash +lcio2edm4hep \ + /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ + Output.root \ + patch.txt +``` The resulting root file will be "Output.root" A few things to note: -ralations that do not have a "toType" and "fromType" set are currently not beeing converted. If check_missing_cols is run without the minimal flag a warning will be given for each Ralations this applies to. +- LCRelations that do not have a `"ToType"` and `"FromType"` as collection parameters set are currently not being converted. If `check_missing_cols` is run without the `--minimal` flag a warning will be given for each Relation this applies to. From e1f5394be0a94aa095a6ede4410d3870dd0b8c07 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 3 May 2023 13:54:02 +0200 Subject: [PATCH 59/83] fixing an error in the reading of files that had trailing whitespaces as well as changing a Warning. --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 4 ++-- standalone/lcio2edm4hep.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 4e11fb4a..ecdd7fad 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -915,7 +915,7 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); if (fromType.empty() || toType.empty()) { - std::cerr << "LCRelation collection has missing FromType or ToType parameters. " + std::cerr << "LCRelation collection "<< name <<" has missing FromType or ToType parameters. " << "Cannot convert it without this information." << std::endl; continue; } @@ -992,7 +992,7 @@ namespace LCIO2EDM4hepConv { } else { std::cout << "Relation from: " << fromType << " to: " << toType - << " is not beeing handled during creation of associations" << std::endl; + << " ("<> getNamesAndTypes(const std::str while (std::getline(input_file, line)) { std::stringstream sline(std::move(line)); std::string name, type; - if (!(sline >> name >> type && sline.eof())) { + //This only looks for the first two words in the line and ignores everything that comes after that. + if (!(sline >> name >> type)) { std::cerr << "need a name and a type per line" << std::endl; return {}; } From 3a177dd011bd0e7fafbc41d49eb80b27a26f7d06 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 3 May 2023 14:18:50 +0200 Subject: [PATCH 60/83] updating the README --- standalone/README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/standalone/README.md b/standalone/README.md index 38390132..6e39651e 100644 --- a/standalone/README.md +++ b/standalone/README.md @@ -1,4 +1,4 @@ -To convert aa LCIO file to EDM4hep either a LCIO file where all collections are present in every event +To convert a LCIO file to EDM4hep either a LCIO file where all collections are present in every event or a file containing a list of all collections in the file are needed. This list can be obtained by executing 'check_missing_cols' from the LCIO package with the '--minimal' flag. @@ -11,6 +11,16 @@ check_missing_cols --minimal \ > patch.txt ``` +The format will look like: +SETSpacePoints TrackerHit +SETSpacePointRelations LCRelation[,] +RecoMCTruthLink LCRelation[ReconstructedParticle,MCParticle] + +All ollections, apart from LCRelations, have the same format of <"Name" "Type"> as seen at the TrackerHit example. LCRelations get [,] behind them. +The first word in the bracket is where the Relations is comming from (FromType) and the second where it is pointing to (ToType). +Only the first two columns are read and everything behind it aswell as excess whitespaces are ignored. +This file is used to patch missing collections and can not be used to choose which collections to convert! + This file will be the third argument when the converter ist called. ```bash From 3a68e67a37e7faab4373d13c2850423103160967 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 3 May 2023 15:11:45 +0200 Subject: [PATCH 61/83] [clang-format] Fix formatting --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 6 +++--- standalone/lcio2edm4hep.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index ecdd7fad..81952ed6 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -915,7 +915,7 @@ namespace LCIO2EDM4hepConv { const auto& fromType = params.getStringVal("FromType"); const auto& toType = params.getStringVal("ToType"); if (fromType.empty() || toType.empty()) { - std::cerr << "LCRelation collection "<< name <<" has missing FromType or ToType parameters. " + std::cerr << "LCRelation collection " << name << " has missing FromType or ToType parameters. " << "Cannot convert it without this information." << std::endl; continue; } @@ -991,8 +991,8 @@ namespace LCIO2EDM4hepConv { assoCollVec.emplace_back(name, std::move(mc_a)); } else { - std::cout << "Relation from: " << fromType << " to: " << toType - << " ("<> getNamesAndTypes(const std::str while (std::getline(input_file, line)) { std::stringstream sline(std::move(line)); std::string name, type; - //This only looks for the first two words in the line and ignores everything that comes after that. + // This only looks for the first two words in the line and ignores everything that comes after that. if (!(sline >> name >> type)) { std::cerr << "need a name and a type per line" << std::endl; return {}; From daf488f289a7d986c0a5c363b8a791b6f2479866 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 3 May 2023 15:27:05 +0200 Subject: [PATCH 62/83] Update documentation --- standalone/README.md | 62 ++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/standalone/README.md b/standalone/README.md index 6e39651e..90b974bf 100644 --- a/standalone/README.md +++ b/standalone/README.md @@ -1,28 +1,43 @@ -To convert a LCIO file to EDM4hep either a LCIO file where all collections are present in every event -or a file containing a list of all collections in the file are needed. -This list can be obtained by executing 'check_missing_cols' from the LCIO package with the '--minimal' flag. - -Example: - +# Standalone conversion from LCIO to EDM4hep +The `lcio2edm4hep` executable reads LCIO (`.slcio`) files and converts its +contents into EDM4hep. Each `LCEvent` of the input file will be put into a +`podio::Frame` in the output file (under the `events` category). The most basic +usage is simply ```bash -check_missing_cols --minimal \ - /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ - > patch.txt +lcio2edm4hep ``` -The format will look like: +## Patching missing collections on the fly +A major difference between LCIO and EDM4hep is that in LCIO an `LCEvent` can +effectively have arbitrary contents, whereas in EDM4hep the assumption is that +each event consists of the same collections (even if some of them are empty). +Hence, it is necessary to either ensure that all events in the LCIO file have +the same contents or or to give `lcio2edm4hep` some additional information such +that it can patch in potentially missing collections on the fly. This additional +information comes in the form of a third argument to `lcio2edm4hep` and is +effectively a list of collection names and their types that comprise the +superset of all collectoins appearing in at least one event in the input LCIO +file. The format looks like this, where each collection is a single line +containing the name first and than its type, e.g. + +``` SETSpacePoints TrackerHit -SETSpacePointRelations LCRelation[,] RecoMCTruthLink LCRelation[ReconstructedParticle,MCParticle] +``` -All ollections, apart from LCRelations, have the same format of <"Name" "Type"> as seen at the TrackerHit example. LCRelations get [,] behind them. -The first word in the bracket is where the Relations is comming from (FromType) and the second where it is pointing to (ToType). -Only the first two columns are read and everything behind it aswell as excess whitespaces are ignored. -This file is used to patch missing collections and can not be used to choose which collections to convert! - -This file will be the third argument when the converter ist called. +The easiest way to obtain such a file is to use the `check_missing_cols` +executable that comes with LCIO using the `--minimal` flag. The output of this +can be directly consumed by `lcio2edm4hep` +Example: +1. Get the patch file +```bash +check_missing_cols --minimal \ + /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ + > patch.txt +``` +2. Pass it to `lcio2edm4hep` ```bash lcio2edm4hep \ /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ @@ -30,8 +45,11 @@ lcio2edm4hep \ patch.txt ``` -The resulting root file will be "Output.root" - - -A few things to note: -- LCRelations that do not have a `"ToType"` and `"FromType"` as collection parameters set are currently not being converted. If `check_missing_cols` is run without the `--minimal` flag a warning will be given for each Relation this applies to. +### Converting `LCRelation` collections +For collections of `LCRelation` type it is necessary to define the `From` and +`To` type as well, as otherwise the converter will not be able to create the +correct edm4hep file. The `check_missing_cols` executable will try to determine +these types from the collection parameters and will warn if it cannot do it for +certain collections. In this case it is the **users responsibility to provide +the missing types** as otherwise the conversion will simply skip these +collections, or potentially even crash. From 0c69f86ea4e53612bbe767bb35da9bcd9ef17cbe Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 15 May 2023 13:36:53 +0200 Subject: [PATCH 63/83] more documentation --- doc/LCIO2EDM4hep.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 doc/LCIO2EDM4hep.md diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md new file mode 100644 index 00000000..dca8c753 --- /dev/null +++ b/doc/LCIO2EDM4hep.md @@ -0,0 +1,11 @@ +There exists a convert function for every data collection type (e.g. convertReconstructedParticleconvert). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their edm4HEP equivalents. Every type has a seperate map. The maps are grouped in a struct for ease of use. They can be defined and stored seperatly. +The order in which the data is converted does not matter because converting data and resolving relations are two differet steps that are carried out in sequence. +Subset collections are also handled at the same step as relations using the fuction "fillSubset()". Alternatively "handleSubsetColl" can also be called to convert a Subset Collection. This way the unique pointers can be obtained directly. +The OneToMany and OnToOne Relations can be resolved using "resolveRelations()". There is a resolveRelations function for each type. +The AssociationCollections in EDM4hep are then created using "createAssociations()", the exception here are the CaloHitContributions. Since they are part of the SimCalorimeterHits in LCIO while being a seperate Association in EDM4hep. They are created by "createCaloHitContributions()". +The EventHeader Colletion can be created using "EventHeaderCollection()". + +Particle IDs are converted during the conversion of the the reconstructed Particle collection. + + +Converting an entire event can be done calling the "convertEvent()". \ No newline at end of file From 6fba9452124f796ca0f3838e764f6a88621d40a5 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 15 May 2023 15:11:30 +0200 Subject: [PATCH 64/83] added an example to the doc and merged two documentations. --- doc/LCIO2EDM4hep.md | 89 +++++++++++++++++++++++++++++++++++++++++--- standalone/README.md | 55 --------------------------- 2 files changed, 83 insertions(+), 61 deletions(-) delete mode 100644 standalone/README.md diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index dca8c753..998a926f 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -1,11 +1,88 @@ -There exists a convert function for every data collection type (e.g. convertReconstructedParticleconvert). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their edm4HEP equivalents. Every type has a seperate map. The maps are grouped in a struct for ease of use. They can be defined and stored seperatly. +# Standalone conversion from LCIO to EDM4hep +The `lcio2edm4hep` executable reads LCIO (`.slcio`) files and converts its +contents into EDM4hep. Each `LCEvent` of the input file will be put into a +`podio::Frame` in the output file (under the `events` category). The most basic +usage is simply + +```bash +lcio2edm4hep +``` + +## Patching missing collections on the fly +A major difference between LCIO and EDM4hep is that in LCIO an `LCEvent` can +effectively have arbitrary contents, whereas in EDM4hep the assumption is that +each event consists of the same collections (even if some of them are empty). +Hence, it is necessary to either ensure that all events in the LCIO file have +the same contents or or to give `lcio2edm4hep` some additional information such +that it can patch in potentially missing collections on the fly. This additional +information comes in the form of a third argument to `lcio2edm4hep` and is +effectively a list of collection names and their types that comprise the +superset of all collectoins appearing in at least one event in the input LCIO +file. The format looks like this, where each collection is a single line +containing the name first and than its type, e.g. + +``` +SETSpacePoints TrackerHit +RecoMCTruthLink LCRelation[ReconstructedParticle,MCParticle] +``` + +The easiest way to obtain such a file is to use the `check_missing_cols` +executable that comes with LCIO using the `--minimal` flag. The output of this +can be directly consumed by `lcio2edm4hep` + +Example: +1. Get the patch file +```bash +check_missing_cols --minimal \ + /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ + > patch.txt +``` +2. Pass it to `lcio2edm4hep` +```bash +lcio2edm4hep \ + /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ + Output.root \ + patch.txt +``` + +### Converting `LCRelation` collections +For collections of `LCRelation` type it is necessary to define the `From` and +`To` type as well, as otherwise the converter will not be able to create the +correct edm4hep file. The `check_missing_cols` executable will try to determine +these types from the collection parameters and will warn if it cannot do it for +certain collections. In this case it is the **users responsibility to provide +the missing types** as otherwise the conversion will simply skip these +collections, or potentially even crash. + + +# Integrated use of conversion +The functions can be used integrated, making the conversion a two step process. Step one is converting the data and step two being the resolving of therelations and filling of subset collection. +There exists a convert function for every collection not of `LCRelation` type (e.g. [`convertReconstructedParticle`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h)). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their edm4HEP equivalents. Every type has a seperate map. The maps are grouped in a struct for ease of use. They can be defined and stored seperatly. The order in which the data is converted does not matter because converting data and resolving relations are two differet steps that are carried out in sequence. -Subset collections are also handled at the same step as relations using the fuction "fillSubset()". Alternatively "handleSubsetColl" can also be called to convert a Subset Collection. This way the unique pointers can be obtained directly. -The OneToMany and OnToOne Relations can be resolved using "resolveRelations()". There is a resolveRelations function for each type. -The AssociationCollections in EDM4hep are then created using "createAssociations()", the exception here are the CaloHitContributions. Since they are part of the SimCalorimeterHits in LCIO while being a seperate Association in EDM4hep. They are created by "createCaloHitContributions()". -The EventHeader Colletion can be created using "EventHeaderCollection()". +Subset collections are also handled at the same step as relations using the fuction [`fillSubset`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Alternatively [`handleSubsetColl`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) can also be called to convert a Subset Collection. This way the unique pointers can be obtained directly. +The OneToMany and OnToOne Relations can be resolved using [`resolveRelations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). There is a resolveRelations function for each type. +The AssociationCollections in EDM4hep are then created using [`createAssociations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h), the exception here are the CaloHitContributions. Since they are part of the SimCalorimeterHits in LCIO while being a seperate Association in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). +The EventHeader Colletion can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Particle IDs are converted during the conversion of the the reconstructed Particle collection. -Converting an entire event can be done calling the "convertEvent()". \ No newline at end of file +Converting an entire event can be done calling the [`convertEvent`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). + +Example for a ReconstructedParticle Collection +```cpp +//the structs defined in the header file are used for the maps linking Lcio particles to there EDM counterparts. + +#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" + +auto convertedReconstructedParticleCollection = convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs) + +//If the relations to other data types are supposed to be converted it is necessary that these are converted aswell by calling their convert function. This needs to be done in order to fill the maps used in setting the relations. +//For a collction of the type reconstructedparticle those are the vertex, cluster and track collections containing the data related to the reconstructed particles. + +//next step is resolving the relations. +resolveRelationsRecoParticle( + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + +//after this the reconstructed particles in `convertedReconstructedParticleCollection` that was created earlier got their vertecies, clusters and tracks attached. +``` \ No newline at end of file diff --git a/standalone/README.md b/standalone/README.md deleted file mode 100644 index 90b974bf..00000000 --- a/standalone/README.md +++ /dev/null @@ -1,55 +0,0 @@ -# Standalone conversion from LCIO to EDM4hep -The `lcio2edm4hep` executable reads LCIO (`.slcio`) files and converts its -contents into EDM4hep. Each `LCEvent` of the input file will be put into a -`podio::Frame` in the output file (under the `events` category). The most basic -usage is simply - -```bash -lcio2edm4hep -``` - -## Patching missing collections on the fly -A major difference between LCIO and EDM4hep is that in LCIO an `LCEvent` can -effectively have arbitrary contents, whereas in EDM4hep the assumption is that -each event consists of the same collections (even if some of them are empty). -Hence, it is necessary to either ensure that all events in the LCIO file have -the same contents or or to give `lcio2edm4hep` some additional information such -that it can patch in potentially missing collections on the fly. This additional -information comes in the form of a third argument to `lcio2edm4hep` and is -effectively a list of collection names and their types that comprise the -superset of all collectoins appearing in at least one event in the input LCIO -file. The format looks like this, where each collection is a single line -containing the name first and than its type, e.g. - -``` -SETSpacePoints TrackerHit -RecoMCTruthLink LCRelation[ReconstructedParticle,MCParticle] -``` - -The easiest way to obtain such a file is to use the `check_missing_cols` -executable that comes with LCIO using the `--minimal` flag. The output of this -can be directly consumed by `lcio2edm4hep` - -Example: -1. Get the patch file -```bash -check_missing_cols --minimal \ - /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ - > patch.txt -``` -2. Pass it to `lcio2edm4hep` -```bash -lcio2edm4hep \ - /pnfs/desy.de/ilc/prod/ilc/mc-2020/ild/rec/250-SetA/higgs/ILD_l5_o2_v02/v02-02-01/00015671/000/rv02-02-01.sv02-02-01.mILD_l5_o2_v02.E250-SetA.I402005.Pe3e3h.eL.pR.n000_002.d_rec_00015671_493.slcio \ - Output.root \ - patch.txt -``` - -### Converting `LCRelation` collections -For collections of `LCRelation` type it is necessary to define the `From` and -`To` type as well, as otherwise the converter will not be able to create the -correct edm4hep file. The `check_missing_cols` executable will try to determine -these types from the collection parameters and will warn if it cannot do it for -certain collections. In this case it is the **users responsibility to provide -the missing types** as otherwise the conversion will simply skip these -collections, or potentially even crash. From 65affeba81b2cfba0b6c61e3342458eee7dc57fd Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 15 May 2023 15:45:13 +0200 Subject: [PATCH 65/83] fixing some typos and changing SubSet into Subset for consistency. --- doc/LCIO2EDM4hep.md | 21 ++++++++++++------- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 6 +++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index 998a926f..db0d0187 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -56,12 +56,18 @@ collections, or potentially even crash. # Integrated use of conversion -The functions can be used integrated, making the conversion a two step process. Step one is converting the data and step two being the resolving of therelations and filling of subset collection. -There exists a convert function for every collection not of `LCRelation` type (e.g. [`convertReconstructedParticle`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h)). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their edm4HEP equivalents. Every type has a seperate map. The maps are grouped in a struct for ease of use. They can be defined and stored seperatly. +The functions can be used integrated, making the conversion a two step process. Step one is converting the data and step two being the resolving of the relations and filling of subset collection. + +There exists a conversion function for every collection that is not of `LCRelation` type (e.g. [`convertReconstructedParticle`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h)). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their EDM4hep equivalents. Every type has a separate map. The maps are grouped in the `LcioEdmTypeMapping` struct for ease of use. It is also possible to use them directly without the helper struct. The order in which the data is converted does not matter because converting data and resolving relations are two differet steps that are carried out in sequence. -Subset collections are also handled at the same step as relations using the fuction [`fillSubset`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Alternatively [`handleSubsetColl`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) can also be called to convert a Subset Collection. This way the unique pointers can be obtained directly. -The OneToMany and OnToOne Relations can be resolved using [`resolveRelations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). There is a resolveRelations function for each type. -The AssociationCollections in EDM4hep are then created using [`createAssociations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h), the exception here are the CaloHitContributions. Since they are part of the SimCalorimeterHits in LCIO while being a seperate Association in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). +Subset collections are handled similar to relations using the fuction [`fillSubset`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Internally this simply forwards to [`handleSubsetColl`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) which handles all the type details and can obviously also be used directly. + +The OneToMany and OneToOne Relations can be resolved using [`resolveRelations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). There is a resolveRelations function for each type. `LCRelation` only exist in LCIOand there conversion is limited to what is available in EDM4hep. They use the `"FromType"` and `"ToType"` collection parameters to get the necessary type information. + +The AssociationCollections in EDM4hep are then created using [`createAssociations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). + +`CaloHitContributions` are treated separately, since they are part of the SimCalorimeterHits in LCIO while being a their own data type in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). + The EventHeader Colletion can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Particle IDs are converted during the conversion of the the reconstructed Particle collection. @@ -69,7 +75,7 @@ Particle IDs are converted during the conversion of the the reconstructed Partic Converting an entire event can be done calling the [`convertEvent`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). -Example for a ReconstructedParticle Collection +## Example for a ReconstructedParticle Collection ```cpp //the structs defined in the header file are used for the maps linking Lcio particles to there EDM counterparts. @@ -77,7 +83,8 @@ Example for a ReconstructedParticle Collection auto convertedReconstructedParticleCollection = convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs) -//If the relations to other data types are supposed to be converted it is necessary that these are converted aswell by calling their convert function. This needs to be done in order to fill the maps used in setting the relations. +//If the relations to other data types are supposed to be converted it is necessary that these are converted aswell by calling their convert function. +//This needs to be done in order to fill the maps used in setting the relations. //For a collction of the type reconstructedparticle those are the vertex, cluster and track collections containing the data related to the reconstructed particles. //next step is resolving the relations. diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 81952ed6..3dceeba9 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -608,13 +608,13 @@ namespace LCIO2EDM4hepConv { auto lcioColl = evt->getCollection(lcioname); if (lcioColl->isSubset()) { const auto& lciotype = lcioColl->getTypeName(); - auto edmColl = fillSubSet(lcioColl, typeMapping, lciotype); + auto edmColl = fillSubset(lcioColl, typeMapping, lciotype); if (edmColl != nullptr) { edmevent.emplace_back(lcioname, std::move(edmColl)); } } } - // Filling all the OneToMany and OnToOne Relations and creating the AssociationCollections. + // Filling all the OneToMany and OneToOne Relations and creating the AssociationCollections. resolveRelations(typeMapping); auto assoCollVec = createAssociations(typeMapping, LCRelations); // creating the CaloHitContributions to fill them into the Frame @@ -1000,7 +1000,7 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr - fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) + fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) { if (type == "MCParticle") { return handleSubsetColl(LCCollection, typeMapping.mcParticles); From f2c03f7156b1d361d459bdb958fd1efa2d99cc27 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 15 May 2023 15:56:12 +0200 Subject: [PATCH 66/83] changing SubSet to Subset for consistency --- .../include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 4cd4ea04..e3395d6f 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -116,7 +116,7 @@ namespace LCIO2EDM4hepConv { * type of the input collection */ std::unique_ptr - fillSubSet(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type); + fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type); inline edm4hep::Vector3f Vector3fFrom(const double* v) { return edm4hep::Vector3f(v[0], v[1], v[2]); } From 614f15fbf684c01632a1a9c4e54bf4402e247bc8 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 17 May 2023 13:45:00 +0200 Subject: [PATCH 67/83] adjustments to the documentation file --- doc/LCIO2EDM4hep.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index db0d0187..3dc5c533 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -60,15 +60,25 @@ The functions can be used integrated, making the conversion a two step process. There exists a conversion function for every collection that is not of `LCRelation` type (e.g. [`convertReconstructedParticle`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h)). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their EDM4hep equivalents. Every type has a separate map. The maps are grouped in the `LcioEdmTypeMapping` struct for ease of use. It is also possible to use them directly without the helper struct. The order in which the data is converted does not matter because converting data and resolving relations are two differet steps that are carried out in sequence. -Subset collections are handled similar to relations using the fuction [`fillSubset`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Internally this simply forwards to [`handleSubsetColl`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) which handles all the type details and can obviously also be used directly. -The OneToMany and OneToOne Relations can be resolved using [`resolveRelations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). There is a resolveRelations function for each type. `LCRelation` only exist in LCIOand there conversion is limited to what is available in EDM4hep. They use the `"FromType"` and `"ToType"` collection parameters to get the necessary type information. +## Handling of subset collections +Subset collections are handled similar to relations using the function [`fillSubset`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Internally this simply forwards to [`handleSubsetColl`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) which handles all the type details and can obviously also be used directly. + +## Handling relations +The OneToMany and OneToOne Relations can be resolved using [`resolveRelations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). There is a resolveRelations function for each type. +## Handling of `LCRelation`s +`LCRelation` only exist in LCIO and their conversion is limited to what is available in EDM4hep. They use the `"FromType"` and `"ToType"` collection parameters to get the necessary type information. The AssociationCollections in EDM4hep are then created using [`createAssociations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). -`CaloHitContributions` are treated separately, since they are part of the SimCalorimeterHits in LCIO while being a their own data type in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). +## Subtle differences between LCIO and EDM4hep +There are a few small differences between LCIO and EDM4hep that shine through in the conversion, these are: + +`CaloHitContributions` are part of the SimCalorimeterHits in LCIO while being their own data type in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). + +The event informaton like is part of the `LCEvent` in LCIO. In EDM4hep there is a separate EventHeader Collection. +It can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). -The EventHeader Colletion can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Particle IDs are converted during the conversion of the the reconstructed Particle collection. @@ -77,19 +87,21 @@ Converting an entire event can be done calling the [`convertEvent`](../k4EDM4hep ## Example for a ReconstructedParticle Collection ```cpp -//the structs defined in the header file are used for the maps linking Lcio particles to there EDM counterparts. + #include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" +//the structs defined in the header file is used for the maps linking Lcio particles to there EDM counterparts. +auto typeMapping = LcioEdmTypeMapping {}; +LCEVENT::LCCollection* LCCollection; -auto convertedReconstructedParticleCollection = convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs) +auto EDMCollection = convertReconstructedParticle("name", LCCollection, typeMapping.recoParticles, typeMapping.particleIDs) //If the relations to other data types are supposed to be converted it is necessary that these are converted aswell by calling their convert function. //This needs to be done in order to fill the maps used in setting the relations. //For a collction of the type reconstructedparticle those are the vertex, cluster and track collections containing the data related to the reconstructed particles. //next step is resolving the relations. -resolveRelationsRecoParticle( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); +resolveRelations(typeMapping); //after this the reconstructed particles in `convertedReconstructedParticleCollection` that was created earlier got their vertecies, clusters and tracks attached. ``` \ No newline at end of file From 7cf119d474134c12790b7333cd62a1a4aa963637 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 17 May 2023 18:11:11 +0200 Subject: [PATCH 68/83] first attempt of converting LCIntVec collection, compiles and runs after some weird adjustment. Seg-faults in podio-dump --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 3 + k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index e3395d6f..d23f79c8 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -44,10 +44,13 @@ #include #include #include +#include +#include #include #include #include "podio/Frame.h" +#include "podio/UserDataCollection.h" #include #include diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 3dceeba9..5630d184 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -287,6 +287,55 @@ namespace LCIO2EDM4hepConv { } return dest; } + + std::unique_ptr> convertLCIntVec( + const std::string& name, + EVENT::LCCollection* LCCollection) + { + auto dest = std::make_unique>(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i){ + const auto* rval = static_cast(LCCollection->getElementAt(i)); + for (unsigned j = 0; jsize();j++){ + + //const auto* value = &rval[i][j]; + //for(unsigned k = 0; ksize();k++){ + //int value = veclayer1[k][0]; + + + //gives syntax error if I just try to push rval[j], apperently still a LCIntVec. + //Need to find a resonable solution of access. + dest->push_back(rval[0][j]); + //} + } + } + return dest; + } + + + /* + std::unique_ptr convertLCFloatVec( + const std::string& name, + EVENT::LCCollection* LCCollection) + { + + } + */ + /* + std::unique_ptr convertTrackerPulse( + const std::string& name, + EVENT::LCCollection* LCCollection) + { + auto dest = std::make_unique(); + + + + //needs Body + + + + return dest; + } + */ std::unique_ptr convertTrackerHitPlane( const std::string& name, @@ -531,6 +580,17 @@ namespace LCIO2EDM4hepConv { else if (type == "TrackerHitPlane") { retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); } + else if (type == "LCIntVec") { + retColls.emplace_back(name, convertLCIntVec(name, LCCollection)); + } + //else if (type == "LCFloatVec") { + // retColls.emplace_back(name, convertLCFloatVec(name, LCCollection)); + //} + else if(type != "LCRelation"){ + std::cerr << type<<" is not a collction type that is not beein handled during data conversion." + << std::endl; + + } return retColls; } From d14632cfa779bf9093ddc846c475de420b4664bf Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 22 May 2023 14:56:41 +0200 Subject: [PATCH 69/83] Added a conversion for LCFloatVec and LCIntVec Collections. --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 26 +++++ k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 98 +++++++++---------- 2 files changed, 73 insertions(+), 51 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index d23f79c8..97bcb2f4 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -164,6 +164,32 @@ namespace LCIO2EDM4hepConv { TypeMapT& recoparticlesMap, TypeMapT& particleIDMap); + /** + * Converts a LCIntVec Collection. + + * NOTE: Since podio doesnt have a structure for vector of vector of int or + *a collection that can be filled with multiple vector, + *a workaround had to be found to convert the LCIO data. + *All the data gets put into one Collection with an additional Collection + *holding the beginnings and ends of the original vectors. + */ + std::vector convertLCIntVec( + const std::string& name, EVENT::LCCollection* LCCollection); + + + + /** + * Converts a LCFloatVec Collection. + + * NOTE: Since podio doesnt have a structure for vector of vector of float or + *a collection that can be filled with multiple vector, + *a workaround had to be found to convert the LCIO data. + *All the data gets put into one Collection with an additional Collection + *holding the beginnings and ends of the original vectors. + */ + std::vector convertLCFloatVec( + const std::string& name, EVENT::LCCollection* LCCollection); + /** * Convert a Vertex collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 5630d184..cb206162 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -287,55 +287,53 @@ namespace LCIO2EDM4hepConv { } return dest; } - - std::unique_ptr> convertLCIntVec( - const std::string& name, - EVENT::LCCollection* LCCollection) - { - auto dest = std::make_unique>(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i){ - const auto* rval = static_cast(LCCollection->getElementAt(i)); - for (unsigned j = 0; jsize();j++){ - //const auto* value = &rval[i][j]; - //for(unsigned k = 0; ksize();k++){ - //int value = veclayer1[k][0]; - - - //gives syntax error if I just try to push rval[j], apperently still a LCIntVec. - //Need to find a resonable solution of access. - dest->push_back(rval[0][j]); - //} + std::vector convertLCIntVec(const std::string& name, EVENT::LCCollection* LCCollection) + { + auto dest = std::make_unique>(); + auto vecSizes = std::make_unique>(); + + if (LCCollection->getNumberOfElements() > 0) { + vecSizes->push_back(0); } + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + for (unsigned j = 0; j < rval->size(); j++) { + dest->push_back((*rval)[j]); + } + vecSizes->push_back(dest->size()); + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "VecLenghts", std::move(vecSizes)); + return results; } - return dest; - } - - /* - std::unique_ptr convertLCFloatVec( + + std::vector convertLCFloatVec( const std::string& name, - EVENT::LCCollection* LCCollection) - { - - } - */ - /* - std::unique_ptr convertTrackerPulse( - const std::string& name, - EVENT::LCCollection* LCCollection) + EVENT::LCCollection* LCCollection) { - auto dest = std::make_unique(); - - - - //needs Body - - - - return dest; + auto dest = std::make_unique>(); + auto vecSizes = std::make_unique>(); + if (LCCollection->getNumberOfElements() > 0) { + vecSizes->push_back(0); + } + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + for (unsigned j = 0; j < rval->size(); j++) { + dest->push_back((*rval)[j]); + } + vecSizes->push_back(dest->size()); + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "VecLenghts", std::move(vecSizes)); + return results; } - */ + std::unique_ptr convertTrackerHitPlane( const std::string& name, @@ -581,15 +579,13 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); } else if (type == "LCIntVec") { - retColls.emplace_back(name, convertLCIntVec(name, LCCollection)); + return convertLCIntVec(name, LCCollection); } - //else if (type == "LCFloatVec") { - // retColls.emplace_back(name, convertLCFloatVec(name, LCCollection)); - //} - else if(type != "LCRelation"){ - std::cerr << type<<" is not a collction type that is not beein handled during data conversion." - << std::endl; - + else if (type == "LCFloatVec") { + return convertLCFloatVec(name, LCCollection); + } + else if (type != "LCRelation") { + std::cerr << type << " is not a collction type that is not beein handled during data conversion." << std::endl; } return retColls; } @@ -661,7 +657,6 @@ namespace LCIO2EDM4hepConv { } } } - // Filling of the Subset Colections for (const auto& lcioname : *lcnames) { @@ -691,6 +686,7 @@ namespace LCIO2EDM4hepConv { event.put(std::move(coll), name); } return event; + } void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) From ed6bf4e5fee9c2d29536e677733e9ce1bac952c1 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Mon, 22 May 2023 16:41:46 +0200 Subject: [PATCH 70/83] conversion of the event parameters --- doc/LCIO2EDM4hep.md | 3 + .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 6 + k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 582 ++++++++---------- 3 files changed, 268 insertions(+), 323 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index 3dc5c533..7ab3203a 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -82,6 +82,9 @@ It can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/ Particle IDs are converted during the conversion of the the reconstructed Particle collection. +## Converting Event parameters +This can be done by calling `convertEventParameters`. + Converting an entire event can be done calling the [`convertEvent`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 97bcb2f4..478d848d 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -89,6 +89,12 @@ namespace LCIO2EDM4hepConv { */ podio::Frame convertEvent(EVENT::LCEvent* evt); + /* + * Putting all the parameters of the event into the podio frame + */ + void convertEventParameters(EVENT::LCEvent* evt,podio::Frame*event); + + /** * Convert an LCIOCollection by dispatching to the specific conversion * function for the corresponding type (after querying the input collection). diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index cb206162..1a062ea9 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -3,9 +3,8 @@ #include namespace LCIO2EDM4hepConv { - edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState) - { - auto edmtrackState = edm4hep::TrackState {}; + edm4hep::TrackState convertTrackState(const EVENT::TrackState *trackState) { + auto edmtrackState = edm4hep::TrackState{}; edmtrackState.location = trackState->getLocation(); edmtrackState.D0 = trackState->getD0(); edmtrackState.phi = trackState->getPhi(); @@ -16,36 +15,17 @@ namespace LCIO2EDM4hepConv { edmtrackState.time = -1; const auto refPoint = trackState->getReferencePoint(); edmtrackState.referencePoint = Vector3fFrom({refPoint[0], refPoint[1], refPoint[2]}); - const auto& covMatrix = trackState->getCovMatrix(); - edmtrackState.covMatrix = { - covMatrix[0], - covMatrix[1], - covMatrix[2], - covMatrix[3], - covMatrix[4], - covMatrix[5], - covMatrix[6], - covMatrix[7], - covMatrix[8], - covMatrix[9], - covMatrix[10], - covMatrix[11], - covMatrix[12], - covMatrix[13], - covMatrix[15], - 0, - 0, - 0, - 0, - 0, - 0}; + const auto &covMatrix = trackState->getCovMatrix(); + edmtrackState.covMatrix = {covMatrix[0], covMatrix[1], covMatrix[2], covMatrix[3], covMatrix[4], covMatrix[5], + covMatrix[6], covMatrix[7], covMatrix[8], covMatrix[9], covMatrix[10], covMatrix[11], + covMatrix[12], covMatrix[13], covMatrix[15], 0, 0, 0, + 0, 0, 0}; return edmtrackState; } - edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID* pid) - { - auto result = edm4hep::MutableParticleID {}; + edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID *pid) { + auto result = edm4hep::MutableParticleID{}; result.setType(pid->getType()); result.setPDG(pid->getPDG()); result.setAlgorithmType(pid->getAlgorithmType()); @@ -59,13 +39,12 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertMCParticle( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& mcparticlesMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &mcparticlesMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setPDG(rval->getPDG()); @@ -81,7 +60,7 @@ namespace LCIO2EDM4hepConv { lval.setMomentum(Vector3fFrom(rval->getMomentum())); lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); - const auto [iterator, inserted] = mcparticlesMap.emplace(rval, lval); + const auto[ iterator, inserted ] = mcparticlesMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -93,19 +72,18 @@ namespace LCIO2EDM4hepConv { } std::vector convertReconstructedParticle( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap, - TypeMapT& particleIDMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &recoparticlesMap, + TypeMapT &particleIDMap) { auto dest = std::make_unique(); auto particleIDs = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setCharge(rval->getCharge()); - auto& m = rval->getCovMatrix(); // 10 parameters + auto &m = rval->getCovMatrix(); // 10 parameters lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); lval.setEnergy(rval->getEnergy()); lval.setGoodnessOfPID(rval->getGoodnessOfPID()); @@ -114,7 +92,7 @@ namespace LCIO2EDM4hepConv { lval.setReferencePoint(rval->getReferencePoint()); lval.setType(rval->getType()); - const auto [iterator, inserted] = recoparticlesMap.emplace(rval, lval); + const auto[ iterator, inserted ] = recoparticlesMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -126,12 +104,11 @@ namespace LCIO2EDM4hepConv { // particle collection in LCIO for (const auto lcioPid : rval->getParticleIDs()) { auto pid = convertParticleID(lcioPid); - const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); + const auto[ pidIt, pidInserted ] = particleIDMap.emplace(lcioPid, pid); if (pidInserted) { lval.addToParticleIDs(pid); particleIDs->push_back(pid); - } - else { + } else { lval.addToParticleIDs(pidIt->second); } } @@ -142,8 +119,7 @@ namespace LCIO2EDM4hepConv { } if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { lval.setParticleIDUsed(it->second); - } - else { + } else { auto pid = convertParticleID(lcioPidUsed); particleIDs->push_back(pid); particleIDMap.emplace(lcioPidUsed, pid); @@ -159,20 +135,19 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertVertex( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& vertexMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &vertexMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary lval.setChi2(rval->getChi2()); lval.setProbability(rval->getProbability()); lval.setPosition(rval->getPosition()); - auto& m = rval->getCovMatrix(); // 6 parameters + auto &m = rval->getCovMatrix(); // 6 parameters lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); // FIXME: the algorithm type in LCIO is a string, but an integer is expected // lval.setAlgorithmType(rval->getAlgorithmType()); @@ -182,7 +157,7 @@ namespace LCIO2EDM4hepConv { lval.addToParameters(v); } - const auto [iterator, inserted] = vertexMap.emplace(rval, lval); + const auto[ iterator, inserted ] = vertexMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -194,14 +169,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertSimTrackerHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimTrHitMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &SimTrHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -214,7 +188,7 @@ namespace LCIO2EDM4hepConv { lval.setPosition(rval->getPosition()); lval.setMomentum(rval->getMomentum()); - const auto [iterator, inserted] = SimTrHitMap.emplace(rval, lval); + const auto[ iterator, inserted ] = SimTrHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -226,14 +200,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertTPCHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &TPCHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setCellID(rval->getCellID()); @@ -243,7 +216,7 @@ namespace LCIO2EDM4hepConv { for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { lval.addToAdcCounts(rval->getRawDataWord(j)); } - const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); + const auto[ iterator, inserted ] = TPCHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -256,13 +229,12 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertTrackerHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &TrackerHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -274,10 +246,10 @@ namespace LCIO2EDM4hepConv { lval.setEDep(rval->getEDep()); lval.setEDepError(rval->getEDepError()); lval.setPosition(rval->getPosition()); - auto& m = rval->getCovMatrix(); + auto &m = rval->getCovMatrix(); lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - const auto [iterator, inserted] = TrackerHitMap.emplace(rval, lval); + const auto[ iterator, inserted ] = TrackerHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -288,16 +260,20 @@ namespace LCIO2EDM4hepConv { return dest; } - std::vector convertLCIntVec(const std::string& name, EVENT::LCCollection* LCCollection) - { + std::vector convertLCIntVec(const std::string &name, EVENT::LCCollection *LCCollection) { + // Since podio doesnt have a structure for vector of vector of int or + // a collection that can be filled with multiple vector, + // a workaround had to be found to convert the LCIO data. + // All the data gets put into one Collection with an additional Collection + // holding the beginnings and ends of the original vectors. auto dest = std::make_unique>(); auto vecSizes = std::make_unique>(); - + if (LCCollection->getNumberOfElements() > 0) { vecSizes->push_back(0); } for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); for (unsigned j = 0; j < rval->size(); j++) { dest->push_back((*rval)[j]); } @@ -310,18 +286,19 @@ namespace LCIO2EDM4hepConv { return results; } - - std::vector convertLCFloatVec( - const std::string& name, - EVENT::LCCollection* LCCollection) - { + std::vector convertLCFloatVec(const std::string &name, EVENT::LCCollection *LCCollection) { + // Since podio doesnt have a structure for vector of vector of int or + // a collection that can be filled with multiple vector, + // a workaround had to be found to convert the LCIO data. + // All the data gets put into one Collection with an additional Collection + // holding the beginnings and ends of the original vectors. auto dest = std::make_unique>(); auto vecSizes = std::make_unique>(); if (LCCollection->getNumberOfElements() > 0) { vecSizes->push_back(0); } for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); for (unsigned j = 0; j < rval->size(); j++) { dest->push_back((*rval)[j]); } @@ -333,17 +310,15 @@ namespace LCIO2EDM4hepConv { results.emplace_back(name + "VecLenghts", std::move(vecSizes)); return results; } - std::unique_ptr convertTrackerHitPlane( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitPlaneMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &TrackerHitPlaneMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -359,10 +334,10 @@ namespace LCIO2EDM4hepConv { lval.setV({rval->getV()[0], rval->getV()[1]}); lval.setDu(rval->getdU()); lval.setDv(rval->getdV()); - auto& m = rval->getCovMatrix(); + auto &m = rval->getCovMatrix(); lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - const auto [iterator, inserted] = TrackerHitPlaneMap.emplace(rval, lval); + const auto[ iterator, inserted ] = TrackerHitPlaneMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -375,14 +350,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertTrack( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &TrackMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setType(rval->getType()); @@ -396,15 +370,15 @@ namespace LCIO2EDM4hepConv { for (auto hitNum : subdetectorHitNum) { lval.addToSubDetectorHitNumbers(hitNum); } - auto& trackStates = rval->getTrackStates(); - for (auto& trackState : trackStates) { + auto &trackStates = rval->getTrackStates(); + for (auto &trackState : trackStates) { lval.addToTrackStates(convertTrackState(trackState)); } - auto quantities = edm4hep::Quantity {}; + auto quantities = edm4hep::Quantity{}; quantities.value = rval->getdEdx(); quantities.error = rval->getdEdxError(); lval.addToDxQuantities(quantities); - const auto [iterator, inserted] = TrackMap.emplace(rval, lval); + const auto[ iterator, inserted ] = TrackMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -417,13 +391,12 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertSimCalorimeterHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimCaloHitMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &SimCaloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -432,7 +405,7 @@ namespace LCIO2EDM4hepConv { lval.setEnergy(rval->getEnergy()); lval.setPosition(rval->getPosition()); - const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); + const auto[ iterator, inserted ] = SimCaloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -445,14 +418,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertRawCalorimeterHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& rawCaloHitMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &rawCaloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -461,7 +433,7 @@ namespace LCIO2EDM4hepConv { lval.setAmplitude(rval->getAmplitude()); lval.setTimeStamp(rval->getTimeStamp()); - const auto [iterator, inserted] = rawCaloHitMap.emplace(rval, lval); + const auto[ iterator, inserted ] = rawCaloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -474,14 +446,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertCalorimeterHit( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& caloHitMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &caloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); cellID = (cellID << 32) | rval->getCellID0(); @@ -492,7 +463,7 @@ namespace LCIO2EDM4hepConv { lval.setTime(rval->getTime()); lval.setType(rval->getType()); - const auto [iterator, inserted] = caloHitMap.emplace(rval, lval); + const auto[ iterator, inserted ] = caloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -505,14 +476,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertCluster( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap) - { + const std::string &name, + EVENT::LCCollection *LCCollection, + TypeMapT &clusterMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + const auto *rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setEnergy(rval->getEnergy()); @@ -520,12 +490,12 @@ namespace LCIO2EDM4hepConv { lval.setITheta(rval->getITheta()); lval.setPhi(rval->getIPhi()); lval.setPosition(rval->getPosition()); - auto& m = rval->getPositionError(); + auto &m = rval->getPositionError(); lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); lval.setType(rval->getType()); lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); - const auto [iterator, inserted] = clusterMap.emplace(rval, lval); + const auto[ iterator, inserted ] = clusterMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->first; const auto existingId = existing->id(); @@ -537,65 +507,50 @@ namespace LCIO2EDM4hepConv { return dest; } - std::vector - convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) - { - const auto& type = LCCollection->getTypeName(); + std::vector convertCollection(const std::string &name, + EVENT::LCCollection *LCCollection, + LcioEdmTypeMapping &typeMapping) { + const auto &type = LCCollection->getTypeName(); std::vector retColls; if (type == "MCParticle") { retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); - } - else if (type == "ReconstructedParticle") { + } else if (type == "ReconstructedParticle") { return convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); - } - else if (type == "Vertex") { + } else if (type == "Vertex") { retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); - } - else if (type == "Track") { + } else if (type == "Track") { retColls.emplace_back(name, convertTrack(name, LCCollection, typeMapping.tracks)); - } - else if (type == "Cluster") { + } else if (type == "Cluster") { retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); - } - else if (type == "SimCalorimeterHit") { + } else if (type == "SimCalorimeterHit") { retColls.emplace_back(name, convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits)); - } - else if (type == "RawCalorimeterHit") { + } else if (type == "RawCalorimeterHit") { retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); - } - else if (type == "CalorimeterHit") { + } else if (type == "CalorimeterHit") { retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.caloHits)); - } - else if (type == "SimTrackerHit") { + } else if (type == "SimTrackerHit") { retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simTrackerHits)); - } - else if (type == "TPCHit") { + } else if (type == "TPCHit") { retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpcHits)); - } - else if (type == "TrackerHit") { + } else if (type == "TrackerHit") { retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); - } - else if (type == "TrackerHitPlane") { + } else if (type == "TrackerHitPlane") { retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); - } - else if (type == "LCIntVec") { + } else if (type == "LCIntVec") { return convertLCIntVec(name, LCCollection); - } - else if (type == "LCFloatVec") { + } else if (type == "LCFloatVec") { return convertLCFloatVec(name, LCCollection); - } - else if (type != "LCRelation") { + } else if (type != "LCRelation") { std::cerr << type << " is not a collction type that is not beein handled during data conversion." << std::endl; } return retColls; } std::unique_ptr createCaloHitContributions( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap) - { + TypeMapT &SimCaloHitMap, + const TypeMapT &mcparticlesMap) { auto contrCollection = std::make_unique(); - for (auto& [lcioHit, edmHit] : SimCaloHitMap) { + for (auto & [ lcioHit, edmHit ] : SimCaloHitMap) { auto NMCParticle = lcioHit->getNMCParticles(); for (unsigned j = 0; j < NMCParticle; j++) { auto edm_contr = contrCollection->create(); @@ -610,8 +565,7 @@ namespace LCIO2EDM4hepConv { const auto it = mcparticlesMap.find(lcioParticle); if (it != mcparticlesMap.end()) { edm_contr.setParticle(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for a LCIO MCParticle, " << "while trying to build CaloHitContributions " << std::endl; } @@ -620,8 +574,7 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) - { + std::unique_ptr createEventHeader(const EVENT::LCEvent *evt) { auto headerColl = std::make_unique(); auto header = headerColl->create(); @@ -632,16 +585,15 @@ namespace LCIO2EDM4hepConv { return headerColl; } - podio::Frame convertEvent(EVENT::LCEvent* evt) - { - auto typeMapping = LcioEdmTypeMapping {}; + podio::Frame convertEvent(EVENT::LCEvent *evt) { + auto typeMapping = LcioEdmTypeMapping{}; std::vector edmevent; - std::vector> LCRelations; - const auto& lcnames = evt->getCollectionNames(); + std::vector> LCRelations; + const auto &lcnames = evt->getCollectionNames(); // In this loop the data gets converted. - for (const auto& lcioname : *lcnames) { - const auto& lcioColl = evt->getCollection(lcioname); - const auto& lciotype = lcioColl->getTypeName(); + for (const auto &lcioname : *lcnames) { + const auto &lcioColl = evt->getCollection(lcioname); + const auto &lciotype = lcioColl->getTypeName(); if (lciotype == "LCRelation") { LCRelations.push_back(std::make_pair(lcioname, lcioColl)); // We handle Relations (aka Associations) once we have converted all the @@ -650,7 +602,7 @@ namespace LCIO2EDM4hepConv { } if (!lcioColl->isSubset()) { - for (auto&& [name, edmColl] : convertCollection(lcioname, lcioColl, typeMapping)) { + for (auto && [ name, edmColl ] : convertCollection(lcioname, lcioColl, typeMapping)) { if (edmColl != nullptr) { edmevent.emplace_back(std::move(name), std::move(edmColl)); } @@ -658,11 +610,11 @@ namespace LCIO2EDM4hepConv { } } // Filling of the Subset Colections - for (const auto& lcioname : *lcnames) { + for (const auto &lcioname : *lcnames) { auto lcioColl = evt->getCollection(lcioname); if (lcioColl->isSubset()) { - const auto& lciotype = lcioColl->getTypeName(); + const auto &lciotype = lcioColl->getTypeName(); auto edmColl = fillSubset(lcioColl, typeMapping, lciotype); if (edmColl != nullptr) { edmevent.emplace_back(lcioname, std::move(edmColl)); @@ -676,23 +628,59 @@ namespace LCIO2EDM4hepConv { auto calocontr = createCaloHitContributions(typeMapping.simCaloHits, typeMapping.mcParticles); auto headerColl = createEventHeader(evt); podio::Frame event; + // convert put the event parameters into the frame + convertEventParameters(evt, &event); // Now everything is done and we simply populate a Frame event.put(std::move(calocontr), "AllCaloHitContributionsCombined"); event.put(std::move(headerColl), "EventHeader"); - for (auto& [name, coll] : edmevent) { + for (auto & [ name, coll ] : edmevent) { event.put(std::move(coll), name); } - for (auto& [name, coll] : assoCollVec) { + for (auto & [ name, coll ] : assoCollVec) { event.put(std::move(coll), name); } return event; + } + void convertEventParameters(EVENT::LCEvent *evt, podio::Frame *event) { + const auto ¶ms = evt->getParameters(); + // handle srting params + EVENT::StringVec keys; + const auto stringKeys = params.getStringKeys(keys); + for (int i = 0; i < stringKeys.size(); i++) { + EVENT::StringVec sValues; + const auto stringVals = params.getStringVals(stringKeys[i], sValues); + event->putParameter(stringKeys[i], stringVals); + } + // handle float params + EVENT::StringVec fkeys; + const auto floatKeys = params.getFloatKeys(fkeys); + for (int i = 0; i < floatKeys.size(); i++) { + EVENT::FloatVec fValues; + const auto floatVals = params.getFloatVals(floatKeys[i], fValues); + event->putParameter(floatKeys[i], floatVals); + } + // handle int params + EVENT::StringVec ikeys; + const auto intKeys = params.getIntKeys(ikeys); + for (int i = 0; i < intKeys.size(); i++) { + EVENT::IntVec iValues; + const auto intVals = params.getIntVals(intKeys[i], iValues); + event->putParameter(intKeys[i], intVals); + } + // handle double params + EVENT::StringVec dkeys; + const auto dKeys = params.getDoubleKeys(dkeys); + for (int i = 0; i < dKeys.size(); i++) { + EVENT::DoubleVec dValues; + const auto dVals = params.getDoubleVals(dKeys[i], dValues); + event->putParameter(dKeys[i], dVals); + } } - void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) - { + void resolveRelationsMCParticle(TypeMapT &mcparticlesMap) { int edmnum = 1; - for (auto& [lcio, edm] : mcparticlesMap) { + for (auto & [ lcio, edm ] : mcparticlesMap) { edmnum++; auto daughters = lcio->getDaughters(); auto parents = lcio->getParents(); @@ -706,11 +694,9 @@ namespace LCIO2EDM4hepConv { dnum++; if (it != mcparticlesMap.end()) { edm.addToDaughters(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " - "while trying to resolve the daughters of MCParticles" - << std::endl; + "while trying to resolve the daughters of MCParticles" << std::endl; } } for (auto p : parents) { @@ -720,21 +706,17 @@ namespace LCIO2EDM4hepConv { const auto it = mcparticlesMap.find(p); if (it != mcparticlesMap.end()) { edm.addToParents(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" - << std::endl; + "while trying to resolve the parents of MCParticles Collections" << std::endl; } } } } - void resolveRelationsSimTrackerHit( - TypeMapT& SimTrHitMap, - TypeMapT& mcparticlesMap) - { - for (auto& [lcio, edm] : SimTrHitMap) { + void resolveRelationsSimTrackerHit(TypeMapT &SimTrHitMap, + TypeMapT &mcparticlesMap) { + for (auto & [ lcio, edm ] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); if (mcps == nullptr) { continue; @@ -742,23 +724,20 @@ namespace LCIO2EDM4hepConv { const auto it = mcparticlesMap.find(mcps); if (it != mcparticlesMap.end()) { edm.setMCParticle(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the SimTrackHit Relations" - << std::endl; + "while trying to resolve the SimTrackHit Relations" << std::endl; } } } void resolveRelationsRecoParticle( - TypeMapT& recoparticlesMap, - const TypeMapT& vertexMap, - const TypeMapT& clusterMap, - const TypeMapT& tracksMap) - { + TypeMapT &recoparticlesMap, + const TypeMapT &vertexMap, + const TypeMapT &clusterMap, + const TypeMapT &tracksMap) { int edmnum = 1; - for (auto& [lcio, edm] : recoparticlesMap) { + for (auto & [ lcio, edm ] : recoparticlesMap) { edmnum++; auto vertex = lcio->getStartVertex(); @@ -767,11 +746,9 @@ namespace LCIO2EDM4hepConv { } if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { edm.setStartVertex(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " - "while trying to resolve the ReconstructedParticle Relations " - << std::endl; + "while trying to resolve the ReconstructedParticle Relations " << std::endl; } auto clusters = lcio->getClusters(); @@ -782,11 +759,9 @@ namespace LCIO2EDM4hepConv { const auto it = clusterMap.find(c); if (it != clusterMap.end()) { edm.addToClusters(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; + "while trying to resolve the ReconstructedParticle Relations" << std::endl; } } @@ -798,11 +773,9 @@ namespace LCIO2EDM4hepConv { const auto it = tracksMap.find(t); if (it != tracksMap.end()) { edm.addToTracks(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; + "while trying to resolve the ReconstructedParticle Relations" << std::endl; } } @@ -814,21 +787,18 @@ namespace LCIO2EDM4hepConv { const auto it = recoparticlesMap.find(p); if (it != recoparticlesMap.end()) { edm.addToParticles(it->second); - } - else { + } else { std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " - "while trying to resolve the ReconstructedParticles parents Relations" - << std::endl; + "while trying to resolve the ReconstructedParticles parents Relations" << std::endl; } } } } void resolveRelationsCluster( - TypeMapT& clustersMap, - const TypeMapT& caloHitMap) - { - for (auto& [lcio, edm] : clustersMap) { + TypeMapT &clustersMap, + const TypeMapT &caloHitMap) { + for (auto & [ lcio, edm ] : clustersMap) { auto clusters = lcio->getClusters(); auto calohits = lcio->getCalorimeterHits(); auto shape = lcio->getShape(); @@ -840,8 +810,7 @@ namespace LCIO2EDM4hepConv { const auto it = clustersMap.find(c); if (it != clustersMap.end()) { edm.addToClusters(it->second); - } - else { + } else { std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; } } @@ -852,8 +821,7 @@ namespace LCIO2EDM4hepConv { const auto it = caloHitMap.find(cal); if (it != caloHitMap.end()) { edm.addToHits(it->second); - } - else { + } else { std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; } } @@ -867,12 +835,11 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsTrack( - TypeMapT& tracksMap, - const TypeMapT& trackerHitMap, - const TypeMapT& TPCHitMap, - const TypeMapT& trackerhitplaneMap) - { - for (auto& [lcio, edm] : tracksMap) { + TypeMapT &tracksMap, + const TypeMapT &trackerHitMap, + const TypeMapT &TPCHitMap, + const TypeMapT &trackerhitplaneMap) { + for (auto & [ lcio, edm ] : tracksMap) { auto tracks = lcio->getTracks(); auto trackerHits = lcio->getTrackerHits(); for (auto t : tracks) { @@ -882,8 +849,7 @@ namespace LCIO2EDM4hepConv { const auto it = tracksMap.find(t); if (it != tracksMap.end()) { edm.addToTracks(it->second); - } - else { + } else { // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; } } @@ -929,10 +895,9 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsVertex( - TypeMapT& vertexMap, - const TypeMapT& recoparticleMap) - { - for (auto& [lcio, edm] : vertexMap) { + TypeMapT &vertexMap, + const TypeMapT &recoparticleMap) { + for (auto & [ lcio, edm ] : vertexMap) { auto recoparticle = lcio->getAssociatedParticle(); if (recoparticle == nullptr) { continue; @@ -940,36 +905,33 @@ namespace LCIO2EDM4hepConv { const auto it = recoparticleMap.find(recoparticle); if (it != recoparticleMap.end()) { edm.setAssociatedParticle(it->second); - } - else { + } else { std::cerr << "Couldn't find associated Particle to add to Vertex " << "Relations in edm" << std::endl; } } } - void resolveRelations(LcioEdmTypeMapping& typeMapping) - { + void resolveRelations(LcioEdmTypeMapping &typeMapping) { resolveRelationsMCParticle(typeMapping.mcParticles); resolveRelationsRecoParticle( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); resolveRelationsTrack( - typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); + typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); resolveRelationsVertex(typeMapping.vertices, typeMapping.recoParticles); } std::vector createAssociations( - const LcioEdmTypeMapping& typeMapping, - const std::vector>& LCRelation) - { + const LcioEdmTypeMapping &typeMapping, + const std::vector> &LCRelation) { std::vector assoCollVec; - for (const auto& [name, relations] : LCRelation) { - const auto& params = relations->getParameters(); + for (const auto & [ name, relations ] : LCRelation) { + const auto ¶ms = relations->getParameters(); - const auto& fromType = params.getStringVal("FromType"); - const auto& toType = params.getStringVal("ToType"); + const auto &fromType = params.getStringVal("FromType"); + const auto &toType = params.getStringVal("ToType"); if (fromType.empty() || toType.empty()) { std::cerr << "LCRelation collection " << name << " has missing FromType or ToType parameters. " << "Cannot convert it without this information." << std::endl; @@ -978,75 +940,61 @@ namespace LCIO2EDM4hepConv { if (fromType == "MCParticle" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.recoParticles); + relations, typeMapping.mcParticles, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { + } else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.mcParticles); + relations, typeMapping.recoParticles, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { + } else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.caloHits, typeMapping.simCaloHits); + relations, typeMapping.caloHits, typeMapping.simCaloHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { + } else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.simCaloHits, typeMapping.caloHits); + relations, typeMapping.simCaloHits, typeMapping.caloHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Cluster" && toType == "MCParticle") { + } else if (fromType == "Cluster" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.clusters, typeMapping.mcParticles); + relations, typeMapping.clusters, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "MCParticle" && toType == "Cluster") { + } else if (fromType == "MCParticle" && toType == "Cluster") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.clusters); + relations, typeMapping.mcParticles, typeMapping.clusters); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "MCParticle" && toType == "Track") { + } else if (fromType == "MCParticle" && toType == "Track") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.tracks); + relations, typeMapping.mcParticles, typeMapping.tracks); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Track" && toType == "MCParticle") { + } else if (fromType == "Track" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.tracks, typeMapping.mcParticles); + relations, typeMapping.tracks, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { + } else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHits, typeMapping.simTrackerHits); + relations, typeMapping.trackerHits, typeMapping.simTrackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { + } else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHits); + relations, typeMapping.simTrackerHits, typeMapping.trackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { + } else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); + relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { + } else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); + relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "ReconstructedParticle" && toType == "Vertex") { + } else if (fromType == "ReconstructedParticle" && toType == "Vertex") { auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.vertices); + relations, typeMapping.recoParticles, typeMapping.vertices); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Vertex" && toType == "ReconstructedParticle") { + } else if (fromType == "Vertex" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.vertices, typeMapping.recoParticles); + relations, typeMapping.vertices, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } - else { + } else { std::cout << "Relation from: " << fromType << " to: " << toType << " (" << name << ") is not beeing handled during creation of associations" << std::endl; } @@ -1055,46 +1003,34 @@ namespace LCIO2EDM4hepConv { return assoCollVec; } - std::unique_ptr - fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) - { + std::unique_ptr fillSubset(EVENT::LCCollection *LCCollection, + const LcioEdmTypeMapping &typeMapping, + const std::string &type) { if (type == "MCParticle") { return handleSubsetColl(LCCollection, typeMapping.mcParticles); - } - else if (type == "ReconstructedParticle") { + } else if (type == "ReconstructedParticle") { return handleSubsetColl(LCCollection, typeMapping.recoParticles); - } - else if (type == "Vertex") { + } else if (type == "Vertex") { return handleSubsetColl(LCCollection, typeMapping.vertices); - } - else if (type == "Track") { + } else if (type == "Track") { return handleSubsetColl(LCCollection, typeMapping.tracks); - } - else if (type == "Cluster") { + } else if (type == "Cluster") { return handleSubsetColl(LCCollection, typeMapping.clusters); - } - else if (type == "SimCalorimeterHit") { + } else if (type == "SimCalorimeterHit") { return handleSubsetColl(LCCollection, typeMapping.simCaloHits); - } - else if (type == "RawCalorimeterHit") { + } else if (type == "RawCalorimeterHit") { return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); - } - else if (type == "CalorimeterHit") { + } else if (type == "CalorimeterHit") { return handleSubsetColl(LCCollection, typeMapping.caloHits); - } - else if (type == "SimTrackerHit") { + } else if (type == "SimTrackerHit") { return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); - } - else if (type == "TPCHit") { + } else if (type == "TPCHit") { return handleSubsetColl(LCCollection, typeMapping.tpcHits); - } - else if (type == "TrackerHit") { + } else if (type == "TrackerHit") { return handleSubsetColl(LCCollection, typeMapping.trackerHits); - } - else if (type == "TrackerHitPlane") { + } else if (type == "TrackerHitPlane") { return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); - } - else { + } else { return nullptr; } } From e377bdc3f518e6731bf8f1c11be8747b6fd70aa8 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 24 May 2023 11:31:15 +0200 Subject: [PATCH 71/83] changed the conversion of LCVec's to a template --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 56 ++++++++++--------- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 53 +----------------- 2 files changed, 32 insertions(+), 77 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 478d848d..dc796690 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -83,6 +83,36 @@ namespace LCIO2EDM4hepConv { }; using CollNamePair = std::tuple>; + + /* + * Converts a LCIntVec or LCFloatVec Collection. + + * NOTE: Since podio doesnt have a structure for vector of vector of or + *a collection that can be filled with multiple vector or vector, + *a workaround had to be found to convert the LCIO data. + *All the data gets put into one Collection with an additional Collection + *holding the beginnings and ends of the original vectors. + */ + template + std::vector convertLCVec(const std::string &name, EVENT::LCCollection *LCCollection) { + auto dest = std::make_unique>(); + auto vecSizes = std::make_unique>(); + if (LCCollection->getNumberOfElements() > 0) { + vecSizes->push_back(0); + } + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + for (unsigned j = 0; j < rval->size(); j++) { + dest->push_back((*rval)[j]); + } + vecSizes->push_back(dest->size()); + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_VecLenghts", std::move(vecSizes)); + return results; +} /** * Convert a complete LCEvent from LCIO to EDM4hep @@ -169,32 +199,6 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& recoparticlesMap, TypeMapT& particleIDMap); - - /** - * Converts a LCIntVec Collection. - - * NOTE: Since podio doesnt have a structure for vector of vector of int or - *a collection that can be filled with multiple vector, - *a workaround had to be found to convert the LCIO data. - *All the data gets put into one Collection with an additional Collection - *holding the beginnings and ends of the original vectors. - */ - std::vector convertLCIntVec( - const std::string& name, EVENT::LCCollection* LCCollection); - - - - /** - * Converts a LCFloatVec Collection. - - * NOTE: Since podio doesnt have a structure for vector of vector of float or - *a collection that can be filled with multiple vector, - *a workaround had to be found to convert the LCIO data. - *All the data gets put into one Collection with an additional Collection - *holding the beginnings and ends of the original vectors. - */ - std::vector convertLCFloatVec( - const std::string& name, EVENT::LCCollection* LCCollection); /** * Convert a Vertex collection and return the resulting collection. diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 1a062ea9..925cea2d 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -260,56 +260,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::vector convertLCIntVec(const std::string &name, EVENT::LCCollection *LCCollection) { - // Since podio doesnt have a structure for vector of vector of int or - // a collection that can be filled with multiple vector, - // a workaround had to be found to convert the LCIO data. - // All the data gets put into one Collection with an additional Collection - // holding the beginnings and ends of the original vectors. - auto dest = std::make_unique>(); - auto vecSizes = std::make_unique>(); - - if (LCCollection->getNumberOfElements() > 0) { - vecSizes->push_back(0); - } - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); - for (unsigned j = 0; j < rval->size(); j++) { - dest->push_back((*rval)[j]); - } - vecSizes->push_back(dest->size()); - } - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "VecLenghts", std::move(vecSizes)); - return results; - } - std::vector convertLCFloatVec(const std::string &name, EVENT::LCCollection *LCCollection) { - // Since podio doesnt have a structure for vector of vector of int or - // a collection that can be filled with multiple vector, - // a workaround had to be found to convert the LCIO data. - // All the data gets put into one Collection with an additional Collection - // holding the beginnings and ends of the original vectors. - auto dest = std::make_unique>(); - auto vecSizes = std::make_unique>(); - if (LCCollection->getNumberOfElements() > 0) { - vecSizes->push_back(0); - } - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); - for (unsigned j = 0; j < rval->size(); j++) { - dest->push_back((*rval)[j]); - } - vecSizes->push_back(dest->size()); - } - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "VecLenghts", std::move(vecSizes)); - return results; - } std::unique_ptr convertTrackerHitPlane( const std::string &name, @@ -537,9 +488,9 @@ namespace LCIO2EDM4hepConv { } else if (type == "TrackerHitPlane") { retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); } else if (type == "LCIntVec") { - return convertLCIntVec(name, LCCollection); + return convertLCVec(name, LCCollection); } else if (type == "LCFloatVec") { - return convertLCFloatVec(name, LCCollection); + return convertLCVec(name, LCCollection); } else if (type != "LCRelation") { std::cerr << type << " is not a collction type that is not beein handled during data conversion." << std::endl; } From 4c28fe0a61f9611be5aa4cd1016f7c1038f2b9ad Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 24 May 2023 11:59:13 +0200 Subject: [PATCH 72/83] change of convertEventParameters function to be more generall --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 48 ++++++++++++++++--- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 39 +-------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index dc796690..f8f16786 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -112,19 +112,53 @@ namespace LCIO2EDM4hepConv { results.emplace_back(name, std::move(dest)); results.emplace_back(name + "_VecLenghts", std::move(vecSizes)); return results; -} + } + + /* + Converting all parameters of an LCIO Object and attaching them to the current podio Frame. + */ + template + void convertObjectParameters(LCIOType *lcioobj, podio::Frame &event) { + const auto ¶ms = lcioobj->getParameters(); + // handle srting params + EVENT::StringVec keys; + const auto stringKeys = params.getStringKeys(keys); + for (int i = 0; i < stringKeys.size(); i++) { + EVENT::StringVec sValues; + const auto stringVals = params.getStringVals(stringKeys[i], sValues); + event.putParameter(stringKeys[i], stringVals); + } + // handle float params + EVENT::StringVec fkeys; + const auto floatKeys = params.getFloatKeys(fkeys); + for (int i = 0; i < floatKeys.size(); i++) { + EVENT::FloatVec fValues; + const auto floatVals = params.getFloatVals(floatKeys[i], fValues); + event.putParameter(floatKeys[i], floatVals); + } + // handle int params + EVENT::StringVec ikeys; + const auto intKeys = params.getIntKeys(ikeys); + for (int i = 0; i < intKeys.size(); i++) { + EVENT::IntVec iValues; + const auto intVals = params.getIntVals(intKeys[i], iValues); + event.putParameter(intKeys[i], intVals); + } + // handle double params + EVENT::StringVec dkeys; + const auto dKeys = params.getDoubleKeys(dkeys); + for (int i = 0; i < dKeys.size(); i++) { + EVENT::DoubleVec dValues; + const auto dVals = params.getDoubleVals(dKeys[i], dValues); + event.putParameter(dKeys[i], dVals); + } + } /** * Convert a complete LCEvent from LCIO to EDM4hep */ podio::Frame convertEvent(EVENT::LCEvent* evt); - /* - * Putting all the parameters of the event into the podio frame - */ - void convertEventParameters(EVENT::LCEvent* evt,podio::Frame*event); - - /** * Convert an LCIOCollection by dispatching to the specific conversion * function for the corresponding type (after querying the input collection). diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 925cea2d..1b3c27f1 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -525,6 +525,7 @@ namespace LCIO2EDM4hepConv { } return contrCollection; } + std::unique_ptr createEventHeader(const EVENT::LCEvent *evt) { auto headerColl = std::make_unique(); auto header = headerColl->create(); @@ -580,7 +581,7 @@ namespace LCIO2EDM4hepConv { auto headerColl = createEventHeader(evt); podio::Frame event; // convert put the event parameters into the frame - convertEventParameters(evt, &event); + convertObjectParameters(evt, event); // Now everything is done and we simply populate a Frame event.put(std::move(calocontr), "AllCaloHitContributionsCombined"); event.put(std::move(headerColl), "EventHeader"); @@ -593,42 +594,6 @@ namespace LCIO2EDM4hepConv { return event; } - void convertEventParameters(EVENT::LCEvent *evt, podio::Frame *event) { - const auto ¶ms = evt->getParameters(); - // handle srting params - EVENT::StringVec keys; - const auto stringKeys = params.getStringKeys(keys); - for (int i = 0; i < stringKeys.size(); i++) { - EVENT::StringVec sValues; - const auto stringVals = params.getStringVals(stringKeys[i], sValues); - event->putParameter(stringKeys[i], stringVals); - } - // handle float params - EVENT::StringVec fkeys; - const auto floatKeys = params.getFloatKeys(fkeys); - for (int i = 0; i < floatKeys.size(); i++) { - EVENT::FloatVec fValues; - const auto floatVals = params.getFloatVals(floatKeys[i], fValues); - event->putParameter(floatKeys[i], floatVals); - } - // handle int params - EVENT::StringVec ikeys; - const auto intKeys = params.getIntKeys(ikeys); - for (int i = 0; i < intKeys.size(); i++) { - EVENT::IntVec iValues; - const auto intVals = params.getIntVals(intKeys[i], iValues); - event->putParameter(intKeys[i], intVals); - } - // handle double params - EVENT::StringVec dkeys; - const auto dKeys = params.getDoubleKeys(dkeys); - for (int i = 0; i < dKeys.size(); i++) { - EVENT::DoubleVec dValues; - const auto dVals = params.getDoubleVals(dKeys[i], dValues); - event->putParameter(dKeys[i], dVals); - } - } - void resolveRelationsMCParticle(TypeMapT &mcparticlesMap) { int edmnum = 1; for (auto & [ lcio, edm ] : mcparticlesMap) { From 0e04d40504b537d584bcc6955a8938fc28330e7f Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 24 May 2023 16:04:13 +0200 Subject: [PATCH 73/83] Update k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h --- .../include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index f8f16786..0b28de3f 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -85,14 +85,13 @@ namespace LCIO2EDM4hepConv { using CollNamePair = std::tuple>; /* - * Converts a LCIntVec or LCFloatVec Collection. + * Converts a LCIntVec or LCFloatVec Collection into a podio::UserDataCollection of the appropriate type. - * NOTE: Since podio doesnt have a structure for vector of vector of or - *a collection that can be filled with multiple vector or vector, - *a workaround had to be found to convert the LCIO data. - *All the data gets put into one Collection with an additional Collection - *holding the beginnings and ends of the original vectors. - */ + * NOTE: LC[Int|Float]Vec are nested, but podio::UserDataCollection are flat. Hence, this will put all + * contents into one collection, and the [begin, end) indices in this collection into a second (flat) + * collection (with the suffix "_VecLengths" added to its name), such that the elements at position i, + * resp. (i + 1) form the [begin, end) indices for each of the original vector collections. + */ template std::vector convertLCVec(const std::string &name, EVENT::LCCollection *LCCollection) { auto dest = std::make_unique>(); From 04435195b65aa5ce6c7c0cd3178d464d3311a278 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 24 May 2023 16:04:59 +0200 Subject: [PATCH 74/83] Update doc/LCIO2EDM4hep.md --- doc/LCIO2EDM4hep.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index 7ab3203a..71fa9428 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -74,13 +74,9 @@ The AssociationCollections in EDM4hep are then created using [`createAssociation ## Subtle differences between LCIO and EDM4hep There are a few small differences between LCIO and EDM4hep that shine through in the conversion, these are: -`CaloHitContributions` are part of the SimCalorimeterHits in LCIO while being their own data type in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). - -The event informaton like is part of the `LCEvent` in LCIO. In EDM4hep there is a separate EventHeader Collection. -It can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). - - -Particle IDs are converted during the conversion of the the reconstructed Particle collection. +- `CaloHitContributions` are part of the SimCalorimeterHits in LCIO while being their own data type in EDM4hep. They are created by [`createCaloHitContributions`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). +- The event informaton like is part of the `LCEvent` in LCIO. In EDM4hep there is a separate EventHeader Collection. It can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) which is stored under the name `"EventHeader"`. +- Particle IDs are converted during the conversion of the the reconstructed Particle collection. ## Converting Event parameters This can be done by calling `convertEventParameters`. From 19348fbca8177254a96cd1a8597c77cbab56b6ef Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 24 May 2023 16:14:11 +0200 Subject: [PATCH 75/83] [format] Format cleanup, small re-org and README clarifications --- doc/LCIO2EDM4hep.md | 27 +- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 150 ++--- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 521 +++++++++++------- 3 files changed, 409 insertions(+), 289 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index 71fa9428..d77d1c7b 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -86,21 +86,24 @@ Converting an entire event can be done calling the [`convertEvent`](../k4EDM4hep ## Example for a ReconstructedParticle Collection ```cpp +#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" +// the struct defined in the header file is used for the maps linking Lcio particles +// to their EDM counterparts. -#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" -//the structs defined in the header file is used for the maps linking Lcio particles to there EDM counterparts. -auto typeMapping = LcioEdmTypeMapping {}; -LCEVENT::LCCollection* LCCollection; +auto typeMapping = LcioEdmTypeMapping{}; -auto EDMCollection = convertReconstructedParticle("name", LCCollection, typeMapping.recoParticles, typeMapping.particleIDs) +// We assume that this is a collection of ReconstructedParticles! +LCEVENT::LCCollection* lcCollection; -//If the relations to other data types are supposed to be converted it is necessary that these are converted aswell by calling their convert function. -//This needs to be done in order to fill the maps used in setting the relations. -//For a collction of the type reconstructedparticle those are the vertex, cluster and track collections containing the data related to the reconstructed particles. +// Convert the data +auto edmCollections = convertReconstructedParticle("name", + lcCollection, + typeMapping.recoParticles, + typeMapping.particleIDs); -//next step is resolving the relations. +// Resolve relations (only converted objects will be available) +// This has to be called at the very end, after all collection data has been +// converted resolveRelations(typeMapping); - -//after this the reconstructed particles in `convertedReconstructedParticleCollection` that was created earlier got their vertecies, clusters and tracks attached. -``` \ No newline at end of file +``` diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 0b28de3f..43065668 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -83,75 +83,11 @@ namespace LCIO2EDM4hepConv { }; using CollNamePair = std::tuple>; - - /* - * Converts a LCIntVec or LCFloatVec Collection into a podio::UserDataCollection of the appropriate type. - - * NOTE: LC[Int|Float]Vec are nested, but podio::UserDataCollection are flat. Hence, this will put all - * contents into one collection, and the [begin, end) indices in this collection into a second (flat) - * collection (with the suffix "_VecLengths" added to its name), such that the elements at position i, - * resp. (i + 1) form the [begin, end) indices for each of the original vector collections. - */ - template - std::vector convertLCVec(const std::string &name, EVENT::LCCollection *LCCollection) { - auto dest = std::make_unique>(); - auto vecSizes = std::make_unique>(); - if (LCCollection->getNumberOfElements() > 0) { - vecSizes->push_back(0); - } - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - for (unsigned j = 0; j < rval->size(); j++) { - dest->push_back((*rval)[j]); - } - vecSizes->push_back(dest->size()); - } - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "_VecLenghts", std::move(vecSizes)); - return results; - } /* - Converting all parameters of an LCIO Object and attaching them to the current podio Frame. - */ - template - void convertObjectParameters(LCIOType *lcioobj, podio::Frame &event) { - const auto ¶ms = lcioobj->getParameters(); - // handle srting params - EVENT::StringVec keys; - const auto stringKeys = params.getStringKeys(keys); - for (int i = 0; i < stringKeys.size(); i++) { - EVENT::StringVec sValues; - const auto stringVals = params.getStringVals(stringKeys[i], sValues); - event.putParameter(stringKeys[i], stringVals); - } - // handle float params - EVENT::StringVec fkeys; - const auto floatKeys = params.getFloatKeys(fkeys); - for (int i = 0; i < floatKeys.size(); i++) { - EVENT::FloatVec fValues; - const auto floatVals = params.getFloatVals(floatKeys[i], fValues); - event.putParameter(floatKeys[i], floatVals); - } - // handle int params - EVENT::StringVec ikeys; - const auto intKeys = params.getIntKeys(ikeys); - for (int i = 0; i < intKeys.size(); i++) { - EVENT::IntVec iValues; - const auto intVals = params.getIntVals(intKeys[i], iValues); - event.putParameter(intKeys[i], intVals); - } - // handle double params - EVENT::StringVec dkeys; - const auto dKeys = params.getDoubleKeys(dkeys); - for (int i = 0; i < dKeys.size(); i++) { - EVENT::DoubleVec dValues; - const auto dVals = params.getDoubleVals(dKeys[i], dValues); - event.putParameter(dKeys[i], dVals); - } - } + * Convert a LCRunHeader to EDM4hep as a frame. + */ + podio::Frame convertRunHeader(EVENT::LCRunHeader* rheader); /** * Convert a complete LCEvent from LCIO to EDM4hep @@ -190,6 +126,24 @@ namespace LCIO2EDM4hepConv { std::unique_ptr fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type); + /* + * Converts a LCIntVec or LCFloatVec Collection into a podio::UserDataCollection of the appropriate type. + * + * NOTE: LC[Int|Float]Vec are nested, but podio::UserDataCollection are flat. Hence, this will put all + * contents into one collection, and the [begin, end) indices in this collection into a second (flat) + * collection (with the suffix "_VecLengths" added to its name), such that the elements at position i, + * resp. (i + 1) form the [begin, end) indices for each of the original vector collections. + */ + template + std::vector convertLCVec(const std::string& name, EVENT::LCCollection* LCCollection); + + /** + * Converting all parameters of an LCIO Object and attaching them to the + * passed podio::Frame. + */ + template + void convertObjectParameters(LCIOType* lcioobj, podio::Frame& event); + inline edm4hep::Vector3f Vector3fFrom(const double* v) { return edm4hep::Vector3f(v[0], v[1], v[2]); } inline edm4hep::Vector3f Vector3fFrom(const EVENT::FloatVec& v) { return edm4hep::Vector3f(v[0], v[1], v[2]); } @@ -232,7 +186,7 @@ namespace LCIO2EDM4hepConv { EVENT::LCCollection* LCCollection, TypeMapT& recoparticlesMap, TypeMapT& particleIDMap); - + /** * Convert a Vertex collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. @@ -477,6 +431,66 @@ namespace LCIO2EDM4hepConv { TypeMapT& vertexMap, const TypeMapT& recoparticleMap); + template + void convertObjectParameters(LCIOType* lcioobj, podio::Frame& event) + { + const auto& params = lcioobj->getParameters(); + // handle srting params + EVENT::StringVec keys; + const auto stringKeys = params.getStringKeys(keys); + for (int i = 0; i < stringKeys.size(); i++) { + EVENT::StringVec sValues; + const auto stringVals = params.getStringVals(stringKeys[i], sValues); + event.putParameter(stringKeys[i], stringVals); + } + // handle float params + EVENT::StringVec fkeys; + const auto floatKeys = params.getFloatKeys(fkeys); + for (int i = 0; i < floatKeys.size(); i++) { + EVENT::FloatVec fValues; + const auto floatVals = params.getFloatVals(floatKeys[i], fValues); + event.putParameter(floatKeys[i], floatVals); + } + // handle int params + EVENT::StringVec ikeys; + const auto intKeys = params.getIntKeys(ikeys); + for (int i = 0; i < intKeys.size(); i++) { + EVENT::IntVec iValues; + const auto intVals = params.getIntVals(intKeys[i], iValues); + event.putParameter(intKeys[i], intVals); + } + // handle double params + EVENT::StringVec dkeys; + const auto dKeys = params.getDoubleKeys(dkeys); + for (int i = 0; i < dKeys.size(); i++) { + EVENT::DoubleVec dValues; + const auto dVals = params.getDoubleVals(dKeys[i], dValues); + event.putParameter(dKeys[i], dVals); + } + } + + template + std::vector convertLCVec(const std::string& name, EVENT::LCCollection* LCCollection) + { + auto dest = std::make_unique>(); + auto vecSizes = std::make_unique>(); + if (LCCollection->getNumberOfElements() > 0) { + vecSizes->push_back(0); + } + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + for (unsigned j = 0; j < rval->size(); j++) { + dest->push_back((*rval)[j]); + } + vecSizes->push_back(dest->size()); + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_VecLenghts", std::move(vecSizes)); + return results; + } + } // namespace LCIO2EDM4hepConv #endif // K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 1b3c27f1..f5055638 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -3,8 +3,9 @@ #include namespace LCIO2EDM4hepConv { - edm4hep::TrackState convertTrackState(const EVENT::TrackState *trackState) { - auto edmtrackState = edm4hep::TrackState{}; + edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState) + { + auto edmtrackState = edm4hep::TrackState {}; edmtrackState.location = trackState->getLocation(); edmtrackState.D0 = trackState->getD0(); edmtrackState.phi = trackState->getPhi(); @@ -15,17 +16,36 @@ namespace LCIO2EDM4hepConv { edmtrackState.time = -1; const auto refPoint = trackState->getReferencePoint(); edmtrackState.referencePoint = Vector3fFrom({refPoint[0], refPoint[1], refPoint[2]}); - const auto &covMatrix = trackState->getCovMatrix(); - edmtrackState.covMatrix = {covMatrix[0], covMatrix[1], covMatrix[2], covMatrix[3], covMatrix[4], covMatrix[5], - covMatrix[6], covMatrix[7], covMatrix[8], covMatrix[9], covMatrix[10], covMatrix[11], - covMatrix[12], covMatrix[13], covMatrix[15], 0, 0, 0, - 0, 0, 0}; + const auto& covMatrix = trackState->getCovMatrix(); + edmtrackState.covMatrix = { + covMatrix[0], + covMatrix[1], + covMatrix[2], + covMatrix[3], + covMatrix[4], + covMatrix[5], + covMatrix[6], + covMatrix[7], + covMatrix[8], + covMatrix[9], + covMatrix[10], + covMatrix[11], + covMatrix[12], + covMatrix[13], + covMatrix[15], + 0, + 0, + 0, + 0, + 0, + 0}; return edmtrackState; } - edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID *pid) { - auto result = edm4hep::MutableParticleID{}; + edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID* pid) + { + auto result = edm4hep::MutableParticleID {}; result.setType(pid->getType()); result.setPDG(pid->getPDG()); result.setAlgorithmType(pid->getAlgorithmType()); @@ -39,12 +59,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertMCParticle( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &mcparticlesMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& mcparticlesMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setPDG(rval->getPDG()); @@ -60,7 +81,7 @@ namespace LCIO2EDM4hepConv { lval.setMomentum(Vector3fFrom(rval->getMomentum())); lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); - const auto[ iterator, inserted ] = mcparticlesMap.emplace(rval, lval); + const auto [iterator, inserted] = mcparticlesMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -72,18 +93,19 @@ namespace LCIO2EDM4hepConv { } std::vector convertReconstructedParticle( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &recoparticlesMap, - TypeMapT &particleIDMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& recoparticlesMap, + TypeMapT& particleIDMap) + { auto dest = std::make_unique(); auto particleIDs = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setCharge(rval->getCharge()); - auto &m = rval->getCovMatrix(); // 10 parameters + auto& m = rval->getCovMatrix(); // 10 parameters lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); lval.setEnergy(rval->getEnergy()); lval.setGoodnessOfPID(rval->getGoodnessOfPID()); @@ -92,7 +114,7 @@ namespace LCIO2EDM4hepConv { lval.setReferencePoint(rval->getReferencePoint()); lval.setType(rval->getType()); - const auto[ iterator, inserted ] = recoparticlesMap.emplace(rval, lval); + const auto [iterator, inserted] = recoparticlesMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -104,11 +126,12 @@ namespace LCIO2EDM4hepConv { // particle collection in LCIO for (const auto lcioPid : rval->getParticleIDs()) { auto pid = convertParticleID(lcioPid); - const auto[ pidIt, pidInserted ] = particleIDMap.emplace(lcioPid, pid); + const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); if (pidInserted) { lval.addToParticleIDs(pid); particleIDs->push_back(pid); - } else { + } + else { lval.addToParticleIDs(pidIt->second); } } @@ -119,7 +142,8 @@ namespace LCIO2EDM4hepConv { } if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { lval.setParticleIDUsed(it->second); - } else { + } + else { auto pid = convertParticleID(lcioPidUsed); particleIDs->push_back(pid); particleIDMap.emplace(lcioPidUsed, pid); @@ -135,19 +159,20 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertVertex( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &vertexMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& vertexMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary lval.setChi2(rval->getChi2()); lval.setProbability(rval->getProbability()); lval.setPosition(rval->getPosition()); - auto &m = rval->getCovMatrix(); // 6 parameters + auto& m = rval->getCovMatrix(); // 6 parameters lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); // FIXME: the algorithm type in LCIO is a string, but an integer is expected // lval.setAlgorithmType(rval->getAlgorithmType()); @@ -157,7 +182,7 @@ namespace LCIO2EDM4hepConv { lval.addToParameters(v); } - const auto[ iterator, inserted ] = vertexMap.emplace(rval, lval); + const auto [iterator, inserted] = vertexMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -169,13 +194,14 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertSimTrackerHit( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &SimTrHitMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimTrHitMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -188,7 +214,7 @@ namespace LCIO2EDM4hepConv { lval.setPosition(rval->getPosition()); lval.setMomentum(rval->getMomentum()); - const auto[ iterator, inserted ] = SimTrHitMap.emplace(rval, lval); + const auto [iterator, inserted] = SimTrHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -200,13 +226,14 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertTPCHit( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &TPCHitMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TPCHitMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setCellID(rval->getCellID()); @@ -216,7 +243,7 @@ namespace LCIO2EDM4hepConv { for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { lval.addToAdcCounts(rval->getRawDataWord(j)); } - const auto[ iterator, inserted ] = TPCHitMap.emplace(rval, lval); + const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -229,12 +256,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertTrackerHit( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &TrackerHitMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -246,10 +274,10 @@ namespace LCIO2EDM4hepConv { lval.setEDep(rval->getEDep()); lval.setEDepError(rval->getEDepError()); lval.setPosition(rval->getPosition()); - auto &m = rval->getCovMatrix(); + auto& m = rval->getCovMatrix(); lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - const auto[ iterator, inserted ] = TrackerHitMap.emplace(rval, lval); + const auto [iterator, inserted] = TrackerHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -260,16 +288,15 @@ namespace LCIO2EDM4hepConv { return dest; } - - std::unique_ptr convertTrackerHitPlane( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &TrackerHitPlaneMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackerHitPlaneMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -285,10 +312,10 @@ namespace LCIO2EDM4hepConv { lval.setV({rval->getV()[0], rval->getV()[1]}); lval.setDu(rval->getdU()); lval.setDv(rval->getdV()); - auto &m = rval->getCovMatrix(); + auto& m = rval->getCovMatrix(); lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - const auto[ iterator, inserted ] = TrackerHitPlaneMap.emplace(rval, lval); + const auto [iterator, inserted] = TrackerHitPlaneMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -301,13 +328,14 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertTrack( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &TrackMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& TrackMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setType(rval->getType()); @@ -321,15 +349,15 @@ namespace LCIO2EDM4hepConv { for (auto hitNum : subdetectorHitNum) { lval.addToSubDetectorHitNumbers(hitNum); } - auto &trackStates = rval->getTrackStates(); - for (auto &trackState : trackStates) { + auto& trackStates = rval->getTrackStates(); + for (auto& trackState : trackStates) { lval.addToTrackStates(convertTrackState(trackState)); } - auto quantities = edm4hep::Quantity{}; + auto quantities = edm4hep::Quantity {}; quantities.value = rval->getdEdx(); quantities.error = rval->getdEdxError(); lval.addToDxQuantities(quantities); - const auto[ iterator, inserted ] = TrackMap.emplace(rval, lval); + const auto [iterator, inserted] = TrackMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -342,12 +370,13 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertSimCalorimeterHit( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &SimCaloHitMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& SimCaloHitMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -356,7 +385,7 @@ namespace LCIO2EDM4hepConv { lval.setEnergy(rval->getEnergy()); lval.setPosition(rval->getPosition()); - const auto[ iterator, inserted ] = SimCaloHitMap.emplace(rval, lval); + const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -369,13 +398,14 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertRawCalorimeterHit( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &rawCaloHitMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& rawCaloHitMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -384,7 +414,7 @@ namespace LCIO2EDM4hepConv { lval.setAmplitude(rval->getAmplitude()); lval.setTimeStamp(rval->getTimeStamp()); - const auto[ iterator, inserted ] = rawCaloHitMap.emplace(rval, lval); + const auto [iterator, inserted] = rawCaloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -397,13 +427,14 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertCalorimeterHit( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &caloHitMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& caloHitMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); cellID = (cellID << 32) | rval->getCellID0(); @@ -414,7 +445,7 @@ namespace LCIO2EDM4hepConv { lval.setTime(rval->getTime()); lval.setType(rval->getType()); - const auto[ iterator, inserted ] = caloHitMap.emplace(rval, lval); + const auto [iterator, inserted] = caloHitMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->second; const auto existingId = existing.id(); @@ -427,13 +458,14 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr convertCluster( - const std::string &name, - EVENT::LCCollection *LCCollection, - TypeMapT &clusterMap) { + const std::string& name, + EVENT::LCCollection* LCCollection, + TypeMapT& clusterMap) + { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto *rval = static_cast(LCCollection->getElementAt(i)); + const auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setEnergy(rval->getEnergy()); @@ -441,12 +473,12 @@ namespace LCIO2EDM4hepConv { lval.setITheta(rval->getITheta()); lval.setPhi(rval->getIPhi()); lval.setPosition(rval->getPosition()); - auto &m = rval->getPositionError(); + auto& m = rval->getPositionError(); lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); lval.setType(rval->getType()); lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); - const auto[ iterator, inserted ] = clusterMap.emplace(rval, lval); + const auto [iterator, inserted] = clusterMap.emplace(rval, lval); if (!inserted) { auto existing = iterator->first; const auto existingId = existing->id(); @@ -458,50 +490,65 @@ namespace LCIO2EDM4hepConv { return dest; } - std::vector convertCollection(const std::string &name, - EVENT::LCCollection *LCCollection, - LcioEdmTypeMapping &typeMapping) { - const auto &type = LCCollection->getTypeName(); + std::vector + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) + { + const auto& type = LCCollection->getTypeName(); std::vector retColls; if (type == "MCParticle") { retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); - } else if (type == "ReconstructedParticle") { + } + else if (type == "ReconstructedParticle") { return convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); - } else if (type == "Vertex") { + } + else if (type == "Vertex") { retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); - } else if (type == "Track") { + } + else if (type == "Track") { retColls.emplace_back(name, convertTrack(name, LCCollection, typeMapping.tracks)); - } else if (type == "Cluster") { + } + else if (type == "Cluster") { retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); - } else if (type == "SimCalorimeterHit") { + } + else if (type == "SimCalorimeterHit") { retColls.emplace_back(name, convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits)); - } else if (type == "RawCalorimeterHit") { + } + else if (type == "RawCalorimeterHit") { retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); - } else if (type == "CalorimeterHit") { + } + else if (type == "CalorimeterHit") { retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.caloHits)); - } else if (type == "SimTrackerHit") { + } + else if (type == "SimTrackerHit") { retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simTrackerHits)); - } else if (type == "TPCHit") { + } + else if (type == "TPCHit") { retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpcHits)); - } else if (type == "TrackerHit") { + } + else if (type == "TrackerHit") { retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); - } else if (type == "TrackerHitPlane") { + } + else if (type == "TrackerHitPlane") { retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); - } else if (type == "LCIntVec") { + } + else if (type == "LCIntVec") { return convertLCVec(name, LCCollection); - } else if (type == "LCFloatVec") { + } + else if (type == "LCFloatVec") { return convertLCVec(name, LCCollection); - } else if (type != "LCRelation") { + } + else if (type != "LCRelation") { std::cerr << type << " is not a collction type that is not beein handled during data conversion." << std::endl; } return retColls; } std::unique_ptr createCaloHitContributions( - TypeMapT &SimCaloHitMap, - const TypeMapT &mcparticlesMap) { + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap) + { auto contrCollection = std::make_unique(); - for (auto & [ lcioHit, edmHit ] : SimCaloHitMap) { + for (auto& [lcioHit, edmHit] : SimCaloHitMap) { auto NMCParticle = lcioHit->getNMCParticles(); for (unsigned j = 0; j < NMCParticle; j++) { auto edm_contr = contrCollection->create(); @@ -516,7 +563,8 @@ namespace LCIO2EDM4hepConv { const auto it = mcparticlesMap.find(lcioParticle); if (it != mcparticlesMap.end()) { edm_contr.setParticle(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for a LCIO MCParticle, " << "while trying to build CaloHitContributions " << std::endl; } @@ -526,7 +574,8 @@ namespace LCIO2EDM4hepConv { return contrCollection; } - std::unique_ptr createEventHeader(const EVENT::LCEvent *evt) { + std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) + { auto headerColl = std::make_unique(); auto header = headerColl->create(); @@ -537,15 +586,16 @@ namespace LCIO2EDM4hepConv { return headerColl; } - podio::Frame convertEvent(EVENT::LCEvent *evt) { - auto typeMapping = LcioEdmTypeMapping{}; + podio::Frame convertEvent(EVENT::LCEvent* evt) + { + auto typeMapping = LcioEdmTypeMapping {}; std::vector edmevent; - std::vector> LCRelations; - const auto &lcnames = evt->getCollectionNames(); + std::vector> LCRelations; + const auto& lcnames = evt->getCollectionNames(); // In this loop the data gets converted. - for (const auto &lcioname : *lcnames) { - const auto &lcioColl = evt->getCollection(lcioname); - const auto &lciotype = lcioColl->getTypeName(); + for (const auto& lcioname : *lcnames) { + const auto& lcioColl = evt->getCollection(lcioname); + const auto& lciotype = lcioColl->getTypeName(); if (lciotype == "LCRelation") { LCRelations.push_back(std::make_pair(lcioname, lcioColl)); // We handle Relations (aka Associations) once we have converted all the @@ -554,7 +604,7 @@ namespace LCIO2EDM4hepConv { } if (!lcioColl->isSubset()) { - for (auto && [ name, edmColl ] : convertCollection(lcioname, lcioColl, typeMapping)) { + for (auto&& [name, edmColl] : convertCollection(lcioname, lcioColl, typeMapping)) { if (edmColl != nullptr) { edmevent.emplace_back(std::move(name), std::move(edmColl)); } @@ -562,11 +612,11 @@ namespace LCIO2EDM4hepConv { } } // Filling of the Subset Colections - for (const auto &lcioname : *lcnames) { + for (const auto& lcioname : *lcnames) { auto lcioColl = evt->getCollection(lcioname); if (lcioColl->isSubset()) { - const auto &lciotype = lcioColl->getTypeName(); + const auto& lciotype = lcioColl->getTypeName(); auto edmColl = fillSubset(lcioColl, typeMapping, lciotype); if (edmColl != nullptr) { edmevent.emplace_back(lcioname, std::move(edmColl)); @@ -585,18 +635,19 @@ namespace LCIO2EDM4hepConv { // Now everything is done and we simply populate a Frame event.put(std::move(calocontr), "AllCaloHitContributionsCombined"); event.put(std::move(headerColl), "EventHeader"); - for (auto & [ name, coll ] : edmevent) { + for (auto& [name, coll] : edmevent) { event.put(std::move(coll), name); } - for (auto & [ name, coll ] : assoCollVec) { + for (auto& [name, coll] : assoCollVec) { event.put(std::move(coll), name); } return event; } - void resolveRelationsMCParticle(TypeMapT &mcparticlesMap) { + void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) + { int edmnum = 1; - for (auto & [ lcio, edm ] : mcparticlesMap) { + for (auto& [lcio, edm] : mcparticlesMap) { edmnum++; auto daughters = lcio->getDaughters(); auto parents = lcio->getParents(); @@ -610,9 +661,11 @@ namespace LCIO2EDM4hepConv { dnum++; if (it != mcparticlesMap.end()) { edm.addToDaughters(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " - "while trying to resolve the daughters of MCParticles" << std::endl; + "while trying to resolve the daughters of MCParticles" + << std::endl; } } for (auto p : parents) { @@ -622,17 +675,21 @@ namespace LCIO2EDM4hepConv { const auto it = mcparticlesMap.find(p); if (it != mcparticlesMap.end()) { edm.addToParents(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" << std::endl; + "while trying to resolve the parents of MCParticles Collections" + << std::endl; } } } } - void resolveRelationsSimTrackerHit(TypeMapT &SimTrHitMap, - TypeMapT &mcparticlesMap) { - for (auto & [ lcio, edm ] : SimTrHitMap) { + void resolveRelationsSimTrackerHit( + TypeMapT& SimTrHitMap, + TypeMapT& mcparticlesMap) + { + for (auto& [lcio, edm] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); if (mcps == nullptr) { continue; @@ -640,20 +697,23 @@ namespace LCIO2EDM4hepConv { const auto it = mcparticlesMap.find(mcps); if (it != mcparticlesMap.end()) { edm.setMCParticle(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the SimTrackHit Relations" << std::endl; + "while trying to resolve the SimTrackHit Relations" + << std::endl; } } } void resolveRelationsRecoParticle( - TypeMapT &recoparticlesMap, - const TypeMapT &vertexMap, - const TypeMapT &clusterMap, - const TypeMapT &tracksMap) { + TypeMapT& recoparticlesMap, + const TypeMapT& vertexMap, + const TypeMapT& clusterMap, + const TypeMapT& tracksMap) + { int edmnum = 1; - for (auto & [ lcio, edm ] : recoparticlesMap) { + for (auto& [lcio, edm] : recoparticlesMap) { edmnum++; auto vertex = lcio->getStartVertex(); @@ -662,9 +722,11 @@ namespace LCIO2EDM4hepConv { } if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { edm.setStartVertex(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " - "while trying to resolve the ReconstructedParticle Relations " << std::endl; + "while trying to resolve the ReconstructedParticle Relations " + << std::endl; } auto clusters = lcio->getClusters(); @@ -675,9 +737,11 @@ namespace LCIO2EDM4hepConv { const auto it = clusterMap.find(c); if (it != clusterMap.end()) { edm.addToClusters(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " - "while trying to resolve the ReconstructedParticle Relations" << std::endl; + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; } } @@ -689,9 +753,11 @@ namespace LCIO2EDM4hepConv { const auto it = tracksMap.find(t); if (it != tracksMap.end()) { edm.addToTracks(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " - "while trying to resolve the ReconstructedParticle Relations" << std::endl; + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; } } @@ -703,18 +769,21 @@ namespace LCIO2EDM4hepConv { const auto it = recoparticlesMap.find(p); if (it != recoparticlesMap.end()) { edm.addToParticles(it->second); - } else { + } + else { std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " - "while trying to resolve the ReconstructedParticles parents Relations" << std::endl; + "while trying to resolve the ReconstructedParticles parents Relations" + << std::endl; } } } } void resolveRelationsCluster( - TypeMapT &clustersMap, - const TypeMapT &caloHitMap) { - for (auto & [ lcio, edm ] : clustersMap) { + TypeMapT& clustersMap, + const TypeMapT& caloHitMap) + { + for (auto& [lcio, edm] : clustersMap) { auto clusters = lcio->getClusters(); auto calohits = lcio->getCalorimeterHits(); auto shape = lcio->getShape(); @@ -726,7 +795,8 @@ namespace LCIO2EDM4hepConv { const auto it = clustersMap.find(c); if (it != clustersMap.end()) { edm.addToClusters(it->second); - } else { + } + else { std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; } } @@ -737,7 +807,8 @@ namespace LCIO2EDM4hepConv { const auto it = caloHitMap.find(cal); if (it != caloHitMap.end()) { edm.addToHits(it->second); - } else { + } + else { std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; } } @@ -751,11 +822,12 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsTrack( - TypeMapT &tracksMap, - const TypeMapT &trackerHitMap, - const TypeMapT &TPCHitMap, - const TypeMapT &trackerhitplaneMap) { - for (auto & [ lcio, edm ] : tracksMap) { + TypeMapT& tracksMap, + const TypeMapT& trackerHitMap, + const TypeMapT& TPCHitMap, + const TypeMapT& trackerhitplaneMap) + { + for (auto& [lcio, edm] : tracksMap) { auto tracks = lcio->getTracks(); auto trackerHits = lcio->getTrackerHits(); for (auto t : tracks) { @@ -765,7 +837,8 @@ namespace LCIO2EDM4hepConv { const auto it = tracksMap.find(t); if (it != tracksMap.end()) { edm.addToTracks(it->second); - } else { + } + else { // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; } } @@ -811,9 +884,10 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsVertex( - TypeMapT &vertexMap, - const TypeMapT &recoparticleMap) { - for (auto & [ lcio, edm ] : vertexMap) { + TypeMapT& vertexMap, + const TypeMapT& recoparticleMap) + { + for (auto& [lcio, edm] : vertexMap) { auto recoparticle = lcio->getAssociatedParticle(); if (recoparticle == nullptr) { continue; @@ -821,33 +895,36 @@ namespace LCIO2EDM4hepConv { const auto it = recoparticleMap.find(recoparticle); if (it != recoparticleMap.end()) { edm.setAssociatedParticle(it->second); - } else { + } + else { std::cerr << "Couldn't find associated Particle to add to Vertex " << "Relations in edm" << std::endl; } } } - void resolveRelations(LcioEdmTypeMapping &typeMapping) { + void resolveRelations(LcioEdmTypeMapping& typeMapping) + { resolveRelationsMCParticle(typeMapping.mcParticles); resolveRelationsRecoParticle( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); resolveRelationsTrack( - typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); + typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); resolveRelationsVertex(typeMapping.vertices, typeMapping.recoParticles); } std::vector createAssociations( - const LcioEdmTypeMapping &typeMapping, - const std::vector> &LCRelation) { + const LcioEdmTypeMapping& typeMapping, + const std::vector>& LCRelation) + { std::vector assoCollVec; - for (const auto & [ name, relations ] : LCRelation) { - const auto ¶ms = relations->getParameters(); + for (const auto& [name, relations] : LCRelation) { + const auto& params = relations->getParameters(); - const auto &fromType = params.getStringVal("FromType"); - const auto &toType = params.getStringVal("ToType"); + const auto& fromType = params.getStringVal("FromType"); + const auto& toType = params.getStringVal("ToType"); if (fromType.empty() || toType.empty()) { std::cerr << "LCRelation collection " << name << " has missing FromType or ToType parameters. " << "Cannot convert it without this information." << std::endl; @@ -856,61 +933,75 @@ namespace LCIO2EDM4hepConv { if (fromType == "MCParticle" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.recoParticles); + relations, typeMapping.mcParticles, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { + } + else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.mcParticles); + relations, typeMapping.recoParticles, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { + } + else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.caloHits, typeMapping.simCaloHits); + relations, typeMapping.caloHits, typeMapping.simCaloHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { + } + else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.simCaloHits, typeMapping.caloHits); + relations, typeMapping.simCaloHits, typeMapping.caloHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "Cluster" && toType == "MCParticle") { + } + else if (fromType == "Cluster" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.clusters, typeMapping.mcParticles); + relations, typeMapping.clusters, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "MCParticle" && toType == "Cluster") { + } + else if (fromType == "MCParticle" && toType == "Cluster") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.clusters); + relations, typeMapping.mcParticles, typeMapping.clusters); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "MCParticle" && toType == "Track") { + } + else if (fromType == "MCParticle" && toType == "Track") { auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.tracks); + relations, typeMapping.mcParticles, typeMapping.tracks); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "Track" && toType == "MCParticle") { + } + else if (fromType == "Track" && toType == "MCParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.tracks, typeMapping.mcParticles); + relations, typeMapping.tracks, typeMapping.mcParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { + } + else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHits, typeMapping.simTrackerHits); + relations, typeMapping.trackerHits, typeMapping.simTrackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHits); + relations, typeMapping.simTrackerHits, typeMapping.trackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); + relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { + } + else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); + relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "ReconstructedParticle" && toType == "Vertex") { + } + else if (fromType == "ReconstructedParticle" && toType == "Vertex") { auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.vertices); + relations, typeMapping.recoParticles, typeMapping.vertices); assoCollVec.emplace_back(name, std::move(mc_a)); - } else if (fromType == "Vertex" && toType == "ReconstructedParticle") { + } + else if (fromType == "Vertex" && toType == "ReconstructedParticle") { auto mc_a = createAssociationCollection( - relations, typeMapping.vertices, typeMapping.recoParticles); + relations, typeMapping.vertices, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); - } else { + } + else { std::cout << "Relation from: " << fromType << " to: " << toType << " (" << name << ") is not beeing handled during creation of associations" << std::endl; } @@ -919,34 +1010,46 @@ namespace LCIO2EDM4hepConv { return assoCollVec; } - std::unique_ptr fillSubset(EVENT::LCCollection *LCCollection, - const LcioEdmTypeMapping &typeMapping, - const std::string &type) { + std::unique_ptr + fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) + { if (type == "MCParticle") { return handleSubsetColl(LCCollection, typeMapping.mcParticles); - } else if (type == "ReconstructedParticle") { + } + else if (type == "ReconstructedParticle") { return handleSubsetColl(LCCollection, typeMapping.recoParticles); - } else if (type == "Vertex") { + } + else if (type == "Vertex") { return handleSubsetColl(LCCollection, typeMapping.vertices); - } else if (type == "Track") { + } + else if (type == "Track") { return handleSubsetColl(LCCollection, typeMapping.tracks); - } else if (type == "Cluster") { + } + else if (type == "Cluster") { return handleSubsetColl(LCCollection, typeMapping.clusters); - } else if (type == "SimCalorimeterHit") { + } + else if (type == "SimCalorimeterHit") { return handleSubsetColl(LCCollection, typeMapping.simCaloHits); - } else if (type == "RawCalorimeterHit") { + } + else if (type == "RawCalorimeterHit") { return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); - } else if (type == "CalorimeterHit") { + } + else if (type == "CalorimeterHit") { return handleSubsetColl(LCCollection, typeMapping.caloHits); - } else if (type == "SimTrackerHit") { + } + else if (type == "SimTrackerHit") { return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); - } else if (type == "TPCHit") { + } + else if (type == "TPCHit") { return handleSubsetColl(LCCollection, typeMapping.tpcHits); - } else if (type == "TrackerHit") { + } + else if (type == "TrackerHit") { return handleSubsetColl(LCCollection, typeMapping.trackerHits); - } else if (type == "TrackerHitPlane") { + } + else if (type == "TrackerHitPlane") { return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); - } else { + } + else { return nullptr; } } From f1bfca3861729535a3a00ba5c1aba7650a786ca8 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 24 May 2023 16:28:00 +0200 Subject: [PATCH 76/83] Minor overhaul of documentation --- doc/LCIO2EDM4hep.md | 52 +++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index d77d1c7b..c552df9e 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -30,7 +30,7 @@ The easiest way to obtain such a file is to use the `check_missing_cols` executable that comes with LCIO using the `--minimal` flag. The output of this can be directly consumed by `lcio2edm4hep` -Example: +#### Example: 1. Get the patch file ```bash check_missing_cols --minimal \ @@ -46,8 +46,8 @@ lcio2edm4hep \ ``` ### Converting `LCRelation` collections -For collections of `LCRelation` type it is necessary to define the `From` and -`To` type as well, as otherwise the converter will not be able to create the +For collections of `LCRelation` type it is necessary to define the `FromType` and +`ToType` as well, as otherwise the converter will not be able to create the correct edm4hep file. The `check_missing_cols` executable will try to determine these types from the collection parameters and will warn if it cannot do it for certain collections. In this case it is the **users responsibility to provide @@ -55,21 +55,43 @@ the missing types** as otherwise the conversion will simply skip these collections, or potentially even crash. -# Integrated use of conversion -The functions can be used integrated, making the conversion a two step process. Step one is converting the data and step two being the resolving of the relations and filling of subset collection. +# Library usage of the conversion functions +The conversion functions are designed to also be usable as a library. The overall design is to make the conversion a two step process. Step one is converting the data and step two being the resolving of the relations and filling of subset collection. -There exists a conversion function for every collection that is not of `LCRelation` type (e.g. [`convertReconstructedParticle`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h)). These need to be called before the relations can be handled, since they fill the maps linking the particle in LCIO to their EDM4hep equivalents. Every type has a separate map. The maps are grouped in the `LcioEdmTypeMapping` struct for ease of use. It is also possible to use them directly without the helper struct. -The order in which the data is converted does not matter because converting data and resolving relations are two differet steps that are carried out in sequence. +## Converting collection (data) +The main entry point is `convertCollection` which will automatically dispatch to +the correct conversion function depending on the type information that is stored +in the input `LCCollection`. It is also possible to access the individual +conversion functions for each type. All of the conversion functions take a map +of LCIO to EDM4hep objects of their specific type that will be filled during the +conversion. for convenience all necessary maps are bundled in the +`LcioEdmTypeMapping` struct. + +## Handling relations +**Once all necessary collections have been converted, it is necessary to resolve +the relations between the obects.** This is done using the `resolveRelations` +function. This will again dispatch to the correct relation resolving function +for the corresponding types, which can obviously also be invoked directly. ## Handling of subset collections -Subset collections are handled similar to relations using the function [`fillSubset`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). Internally this simply forwards to [`handleSubsetColl`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) which handles all the type details and can obviously also be used directly. +Subset collections are handled similar to relations using the function +`fillSubset`. Internally this simply forwards to `handleSubsetColl` which +handles all the type details and can obviously also be used directly. -## Handling relations -The OneToMany and OneToOne Relations can be resolved using [`resolveRelations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). There is a resolveRelations function for each type. ## Handling of `LCRelation`s -`LCRelation` only exist in LCIO and their conversion is limited to what is available in EDM4hep. They use the `"FromType"` and `"ToType"` collection parameters to get the necessary type information. +`LCRelation` only exist in LCIO and their conversion is limited to what is +available in EDM4hep. They use the `"FromType"` and `"ToType"` collection +parameters to get the necessary type information. + +The AssociationCollections in EDM4hep are then created using `createAssociations`. + +## Converting entire events +Converting an entire event can be done calling the `convertEvent`. This can also +be used as an example to guide the implementation of custom conversions using +the available functionality. -The AssociationCollections in EDM4hep are then created using [`createAssociations`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). +## Converting Event parameters +This can be done by calling `convertObjectParameters` that will put all the event parameters into the passed `podio::Frame`. ## Subtle differences between LCIO and EDM4hep There are a few small differences between LCIO and EDM4hep that shine through in the conversion, these are: @@ -78,12 +100,6 @@ There are a few small differences between LCIO and EDM4hep that shine through in - The event informaton like is part of the `LCEvent` in LCIO. In EDM4hep there is a separate EventHeader Collection. It can be created using [`EventHeaderCollection`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h) which is stored under the name `"EventHeader"`. - Particle IDs are converted during the conversion of the the reconstructed Particle collection. -## Converting Event parameters -This can be done by calling `convertEventParameters`. - - -Converting an entire event can be done calling the [`convertEvent`](../k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h). - ## Example for a ReconstructedParticle Collection ```cpp #include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" From 6ae562358f2c6e4619e54bb98af68824f9974f6e Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 24 May 2023 16:45:17 +0200 Subject: [PATCH 77/83] [format] Whitespace cleanup --- doc/LCIO2EDM4hep.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/LCIO2EDM4hep.md b/doc/LCIO2EDM4hep.md index c552df9e..48867e41 100644 --- a/doc/LCIO2EDM4hep.md +++ b/doc/LCIO2EDM4hep.md @@ -65,7 +65,7 @@ in the input `LCCollection`. It is also possible to access the individual conversion functions for each type. All of the conversion functions take a map of LCIO to EDM4hep objects of their specific type that will be filled during the conversion. for convenience all necessary maps are bundled in the -`LcioEdmTypeMapping` struct. +`LcioEdmTypeMapping` struct. ## Handling relations **Once all necessary collections have been converted, it is necessary to resolve @@ -83,7 +83,7 @@ handles all the type details and can obviously also be used directly. available in EDM4hep. They use the `"FromType"` and `"ToType"` collection parameters to get the necessary type information. -The AssociationCollections in EDM4hep are then created using `createAssociations`. +The AssociationCollections in EDM4hep are then created using `createAssociations`. ## Converting entire events Converting an entire event can be done calling the `convertEvent`. This can also From a6ce31e155e134845cfbfdc884a3666eb678ce09 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 26 May 2023 15:38:24 +0200 Subject: [PATCH 78/83] Rename conversion functions for less ambiguity --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 36 +++++----- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 72 +++++++++---------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 43065668..dc94db91 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -168,7 +168,7 @@ namespace LCIO2EDM4hepConv { * Convert an MCParticle collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertMCParticle( + std::unique_ptr convertMCParticles( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& mcparticlesMap); @@ -181,7 +181,7 @@ namespace LCIO2EDM4hepConv { * part of the ReconstructedParticles in LCIO. The name of this collection is * _particleIDs */ - std::vector convertReconstructedParticle( + std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& recoparticlesMap, @@ -191,7 +191,7 @@ namespace LCIO2EDM4hepConv { * Convert a Vertex collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertVertex( + std::unique_ptr convertVertices( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& vertexMap); @@ -200,7 +200,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimTrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertSimTrackerHit( + std::unique_ptr convertSimTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& SimTrHitMap); @@ -209,7 +209,7 @@ namespace LCIO2EDM4hepConv { * Convert a TPCHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTPCHit( + std::unique_ptr convertTPCHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TPCHitMap); @@ -218,7 +218,7 @@ namespace LCIO2EDM4hepConv { * Convert a TrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTrackerHit( + std::unique_ptr convertTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TrackerHitMap); @@ -227,7 +227,7 @@ namespace LCIO2EDM4hepConv { * Convert a TrackerHitPlane collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTrackerHitPlane( + std::unique_ptr convertTrackerHitPlanes( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TrackerHitPlaneMap); @@ -236,7 +236,7 @@ namespace LCIO2EDM4hepConv { * Convert a Track collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTrack( + std::unique_ptr convertTracks( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TrackMap); @@ -245,7 +245,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertSimCalorimeterHit( + std::unique_ptr convertSimCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& SimCaloHitMap); @@ -254,7 +254,7 @@ namespace LCIO2EDM4hepConv { * Convert a RawCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertRawCalorimeterHit( + std::unique_ptr convertRawCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& rawCaloHitMap); @@ -263,7 +263,7 @@ namespace LCIO2EDM4hepConv { * Convert a CalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertCalorimeterHit( + std::unique_ptr convertCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& caloHitMap); @@ -272,7 +272,7 @@ namespace LCIO2EDM4hepConv { * Convert a Cluster collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertCluster( + std::unique_ptr convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& clusterMap); @@ -392,19 +392,19 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for the MCParticles. */ - void resolveRelationsMCParticle(TypeMapT& mcparticlesMap); + void resolveRelationsMCParticles(TypeMapT& mcparticlesMap); /** * Resolve the relations for SimTrackerHits */ - void resolveRelationsSimTrackerHit( + void resolveRelationsSimTrackerHits( TypeMapT& SimTrHitMap, const TypeMapT& mcparticlesMap); /** * Resolve the relations for ReconstructedParticles */ - void resolveRelationsRecoParticle( + void resolveRelationsRecoParticles( TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, @@ -413,21 +413,21 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for Clusters */ - void resolveRelationsCluster( + void resolveRelationsClusters( TypeMapT& clustersMap, const TypeMapT& caloHitMap); /** * Resolve the relations for Tracks */ - void resolveRelationsTrack( + void resolveRelationsTracks( TypeMapT& tracksMap, const TypeMapT& trackerHitMap); /** * Resolve the relations for Vertices */ - void resolveRelationsVertex( + void resolveRelationsVertices( TypeMapT& vertexMap, const TypeMapT& recoparticleMap); diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index f5055638..51925e2d 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -58,7 +58,7 @@ namespace LCIO2EDM4hepConv { return result; } - std::unique_ptr convertMCParticle( + std::unique_ptr convertMCParticles( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& mcparticlesMap) @@ -92,7 +92,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::vector convertReconstructedParticle( + std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& recoparticlesMap, @@ -158,7 +158,7 @@ namespace LCIO2EDM4hepConv { return results; } - std::unique_ptr convertVertex( + std::unique_ptr convertVertices( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& vertexMap) @@ -193,7 +193,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertSimTrackerHit( + std::unique_ptr convertSimTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& SimTrHitMap) @@ -225,7 +225,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTPCHit( + std::unique_ptr convertTPCHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TPCHitMap) @@ -255,7 +255,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTrackerHit( + std::unique_ptr convertTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TrackerHitMap) @@ -288,7 +288,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTrackerHitPlane( + std::unique_ptr convertTrackerHitPlanes( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TrackerHitPlaneMap) @@ -327,7 +327,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTrack( + std::unique_ptr convertTracks( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& TrackMap) @@ -369,7 +369,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertSimCalorimeterHit( + std::unique_ptr convertSimCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& SimCaloHitMap) @@ -397,7 +397,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertRawCalorimeterHit( + std::unique_ptr convertRawCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& rawCaloHitMap) @@ -426,7 +426,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertCalorimeterHit( + std::unique_ptr convertCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& caloHitMap) @@ -457,7 +457,7 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertCluster( + std::unique_ptr convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, TypeMapT& clusterMap) @@ -496,40 +496,40 @@ namespace LCIO2EDM4hepConv { const auto& type = LCCollection->getTypeName(); std::vector retColls; if (type == "MCParticle") { - retColls.emplace_back(name, convertMCParticle(name, LCCollection, typeMapping.mcParticles)); + retColls.emplace_back(name, convertMCParticles(name, LCCollection, typeMapping.mcParticles)); } else if (type == "ReconstructedParticle") { - return convertReconstructedParticle(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); + return convertReconstructedParticles(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); } else if (type == "Vertex") { - retColls.emplace_back(name, convertVertex(name, LCCollection, typeMapping.vertices)); + retColls.emplace_back(name, convertVertices(name, LCCollection, typeMapping.vertices)); } else if (type == "Track") { - retColls.emplace_back(name, convertTrack(name, LCCollection, typeMapping.tracks)); + retColls.emplace_back(name, convertTracks(name, LCCollection, typeMapping.tracks)); } else if (type == "Cluster") { - retColls.emplace_back(name, convertCluster(name, LCCollection, typeMapping.clusters)); + retColls.emplace_back(name, convertClusters(name, LCCollection, typeMapping.clusters)); } else if (type == "SimCalorimeterHit") { - retColls.emplace_back(name, convertSimCalorimeterHit(name, LCCollection, typeMapping.simCaloHits)); + retColls.emplace_back(name, convertSimCalorimeterHits(name, LCCollection, typeMapping.simCaloHits)); } else if (type == "RawCalorimeterHit") { - retColls.emplace_back(name, convertRawCalorimeterHit(name, LCCollection, typeMapping.rawCaloHits)); + retColls.emplace_back(name, convertRawCalorimeterHits(name, LCCollection, typeMapping.rawCaloHits)); } else if (type == "CalorimeterHit") { - retColls.emplace_back(name, convertCalorimeterHit(name, LCCollection, typeMapping.caloHits)); + retColls.emplace_back(name, convertCalorimeterHits(name, LCCollection, typeMapping.caloHits)); } else if (type == "SimTrackerHit") { - retColls.emplace_back(name, convertSimTrackerHit(name, LCCollection, typeMapping.simTrackerHits)); + retColls.emplace_back(name, convertSimTrackerHits(name, LCCollection, typeMapping.simTrackerHits)); } else if (type == "TPCHit") { - retColls.emplace_back(name, convertTPCHit(name, LCCollection, typeMapping.tpcHits)); + retColls.emplace_back(name, convertTPCHits(name, LCCollection, typeMapping.tpcHits)); } else if (type == "TrackerHit") { - retColls.emplace_back(name, convertTrackerHit(name, LCCollection, typeMapping.trackerHits)); + retColls.emplace_back(name, convertTrackerHits(name, LCCollection, typeMapping.trackerHits)); } else if (type == "TrackerHitPlane") { - retColls.emplace_back(name, convertTrackerHitPlane(name, LCCollection, typeMapping.trackerHitPlanes)); + retColls.emplace_back(name, convertTrackerHitPlanes(name, LCCollection, typeMapping.trackerHitPlanes)); } else if (type == "LCIntVec") { return convertLCVec(name, LCCollection); @@ -644,7 +644,7 @@ namespace LCIO2EDM4hepConv { return event; } - void resolveRelationsMCParticle(TypeMapT& mcparticlesMap) + void resolveRelationsMCParticles(TypeMapT& mcparticlesMap) { int edmnum = 1; for (auto& [lcio, edm] : mcparticlesMap) { @@ -685,7 +685,7 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsSimTrackerHit( + void resolveRelationsSimTrackerHits( TypeMapT& SimTrHitMap, TypeMapT& mcparticlesMap) { @@ -706,7 +706,7 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsRecoParticle( + void resolveRelationsRecoParticles( TypeMapT& recoparticlesMap, const TypeMapT& vertexMap, const TypeMapT& clusterMap, @@ -779,7 +779,7 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsCluster( + void resolveRelationsClusters( TypeMapT& clustersMap, const TypeMapT& caloHitMap) { @@ -821,7 +821,7 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsTrack( + void resolveRelationsTracks( TypeMapT& tracksMap, const TypeMapT& trackerHitMap, const TypeMapT& TPCHitMap, @@ -883,7 +883,7 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsVertex( + void resolveRelationsVertices( TypeMapT& vertexMap, const TypeMapT& recoparticleMap) { @@ -905,14 +905,14 @@ namespace LCIO2EDM4hepConv { void resolveRelations(LcioEdmTypeMapping& typeMapping) { - resolveRelationsMCParticle(typeMapping.mcParticles); - resolveRelationsRecoParticle( + resolveRelationsMCParticles(typeMapping.mcParticles); + resolveRelationsRecoParticles( typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); - resolveRelationsSimTrackerHit(typeMapping.simTrackerHits, typeMapping.mcParticles); - resolveRelationsCluster(typeMapping.clusters, typeMapping.caloHits); - resolveRelationsTrack( + resolveRelationsSimTrackerHits(typeMapping.simTrackerHits, typeMapping.mcParticles); + resolveRelationsClusters(typeMapping.clusters, typeMapping.caloHits); + resolveRelationsTracks( typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); - resolveRelationsVertex(typeMapping.vertices, typeMapping.recoParticles); + resolveRelationsVertices(typeMapping.vertices, typeMapping.recoParticles); } std::vector createAssociations( From bb79f5373b4acbba902a646fff4803dfebb81a7f Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 2 Jun 2023 11:05:33 +0200 Subject: [PATCH 79/83] Add conversion of MCRecoCaloParticleAssociations --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 51925e2d..0554e9b1 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -1001,6 +1001,16 @@ namespace LCIO2EDM4hepConv { relations, typeMapping.vertices, typeMapping.recoParticles); assoCollVec.emplace_back(name, std::move(mc_a)); } + else if (fromType == "CalorimeterHit" && toType == "MCParticle") { + auto assoc = createAssociationCollection( + relations, typeMapping.caloHits, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(assoc)); + } + else if (fromType == "MCParticle" && toType == "CalorimeterHit") { + auto assoc = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.caloHits); + assoCollVec.emplace_back(name, std::move(assoc)); + } else { std::cout << "Relation from: " << fromType << " to: " << toType << " (" << name << ") is not beeing handled during creation of associations" << std::endl; From 366411d4f0d6129bdfabba17bd16089f16ce3315 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 24 May 2023 15:30:50 +0200 Subject: [PATCH 80/83] Conversion of information in LCRunHeader --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 15 +++++++++++++++ standalone/lcio2edm4hep.cpp | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 0554e9b1..281a9bb6 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -1064,4 +1064,19 @@ namespace LCIO2EDM4hepConv { } } + podio::Frame convertRunHeader(EVENT::LCRunHeader* rheader) + { + podio::Frame runHeaderFrame; + runHeaderFrame.putParameter("runNumber", rheader->getRunNumber()); + runHeaderFrame.putParameter("detectoName", rheader->getDetectorName()); + runHeaderFrame.putParameter("description", rheader->getDescription()); + auto subdetectors = rheader->getActiveSubdetectors(); + runHeaderFrame.putParameter("activeSubdetectors", *subdetectors); + + // convert everything set as a parameter + convertObjectParameters(rheader, runHeaderFrame); + + return runHeaderFrame; + } + } // namespace LCIO2EDM4hepConv diff --git a/standalone/lcio2edm4hep.cpp b/standalone/lcio2edm4hep.cpp index 0f0a1fb8..2193b8bf 100644 --- a/standalone/lcio2edm4hep.cpp +++ b/standalone/lcio2edm4hep.cpp @@ -80,9 +80,20 @@ int main(int argc, char* argv[]) auto lcreader = IOIMPL::LCFactory::getInstance()->createLCReader(); lcreader->open(argv[1]); std::cout << "Number of events: " << lcreader->getNumberOfEvents() << '\n'; + std::cout << "Number of runs: " << lcreader->getNumberOfRuns() << '\n'; podio::ROOTFrameWriter writer(outputFile); + for (auto j = 0u; j < lcreader->getNumberOfRuns(); ++j) { + if (j % 1 == 0) { + std::cout << "processing RunHeader: " << j << std::endl; + } + auto rhead = lcreader->readNextRunHeader(); + + const auto edmRunHeader = LCIO2EDM4hepConv::convertRunHeader(rhead); + writer.writeFrame(edmRunHeader, "runs"); + } + for (auto i = 0u; i < lcreader->getNumberOfEvents(); ++i) { if (i % 10 == 0) { std::cout << "processing Event: " << i << std::endl; From 44e8ae0c48a21a1dd45f399fbf062d017d536b16 Mon Sep 17 00:00:00 2001 From: Finn Johannsen Date: Wed, 31 May 2023 15:03:35 +0200 Subject: [PATCH 81/83] Conversion of ParticleIDs of the Cluster Collection --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 9 ++++-- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 29 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index dc94db91..b2d9b7da 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -271,11 +271,16 @@ namespace LCIO2EDM4hepConv { /** * Convert a Cluster collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. + * + * NOTE: Also populates a ParticleID collection, as those are persisted as + * part of the Cluster collection in LCIO. The name of this collection is + * _particleIDs */ - std::unique_ptr convertClusters( + std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap); + TypeMapT& clusterMap, + TypeMapT& particleIDMap); /** * Create an EventHeaderCollection and fills it with the Metadata. diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 281a9bb6..02a9d767 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -457,11 +457,13 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertClusters( + std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap) + TypeMapT& clusterMap, + TypeMapT& particleIDMap) { + auto particleIDs = std::make_unique(); auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -485,9 +487,26 @@ namespace LCIO2EDM4hepConv { std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; } - } - return dest; + // Need to convert the particle IDs here, since they are part of the cluster + // collection in LCIO + for (const auto lcioPid : rval->getParticleIDs()) { + auto pid = convertParticleID(lcioPid); + const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); + if (pidInserted) { + lval.addToParticleIDs(pid); + particleIDs->push_back(pid); + } + else { + lval.addToParticleIDs(pidIt->second); + } + } + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_particleIDs", std::move(particleIDs)); + return results; } std::vector @@ -508,7 +527,7 @@ namespace LCIO2EDM4hepConv { retColls.emplace_back(name, convertTracks(name, LCCollection, typeMapping.tracks)); } else if (type == "Cluster") { - retColls.emplace_back(name, convertClusters(name, LCCollection, typeMapping.clusters)); + return convertClusters(name, LCCollection, typeMapping.clusters, typeMapping.particleIDs); } else if (type == "SimCalorimeterHit") { retColls.emplace_back(name, convertSimCalorimeterHits(name, LCCollection, typeMapping.simCaloHits)); From 189cddbe68cbe9284a5158e53fb691b41fb32d19 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 7 Jun 2023 10:51:11 +0200 Subject: [PATCH 82/83] Fix capitalization of subdetectorHitNumbers --- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 02a9d767..5e0d482e 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -347,7 +347,7 @@ namespace LCIO2EDM4hepConv { auto subdetectorHitNum = rval->getSubdetectorHitNumbers(); for (auto hitNum : subdetectorHitNum) { - lval.addToSubDetectorHitNumbers(hitNum); + lval.addToSubdetectorHitNumbers(hitNum); } auto& trackStates = rval->getTrackStates(); for (auto& trackState : trackStates) { From 93012b437ca939446a60831ed974d7829cd5b6ef Mon Sep 17 00:00:00 2001 From: tmadlener Date: Tue, 13 Jun 2023 15:54:46 +0200 Subject: [PATCH 83/83] Adapt to podio change to string_views --- tests/compare_contents.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/compare_contents.cpp b/tests/compare_contents.cpp index 1495f0f0..cc2f7e20 100644 --- a/tests/compare_contents.cpp +++ b/tests/compare_contents.cpp @@ -6,6 +6,7 @@ #include #include +#include #define ASSERT_COMPARE_OR_EXIT(collType) \ if (type == #collType) { \ @@ -59,12 +60,12 @@ int main(int argc, char* argv[]) continue; } } - const auto& type = [&edmEvent, &name]() { + const auto type = [&edmEvent, &name]() { const auto coll = edmEvent.get(name); if (coll) { return coll->getTypeName(); } - static std::string empty = ""; + static constexpr std::string_view empty = ""; return empty; }(); if (type.empty()) {