From 8dc4bab3d52e7f5211f4d5bab4ea039ae27265f4 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Wed, 20 Sep 2023 11:22:04 +0200 Subject: [PATCH 1/9] All EDM4hep collections + ROOT legacy reader --- tools/include/edm4hep2json.hxx | 279 +++++++++++++++++++++++++++++---- tools/src/edm4hep2json.cxx | 60 ++++--- 2 files changed, 282 insertions(+), 57 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index a90fad2a6..9d61ac01d 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -2,18 +2,35 @@ #define EDM4HEP_TO_JSON_H // EDM4hep event data model -#include "edm4hep/CalorimeterHitCollection.h" -#include "edm4hep/ClusterCollection.h" #include "edm4hep/EventHeaderCollection.h" #include "edm4hep/MCParticleCollection.h" -#include "edm4hep/MCRecoParticleAssociationCollection.h" -#include "edm4hep/ParticleIDCollection.h" -#include "edm4hep/ReconstructedParticleCollection.h" -#include "edm4hep/SimCalorimeterHitCollection.h" #include "edm4hep/SimTrackerHitCollection.h" -#include "edm4hep/TrackCollection.h" +#include "edm4hep/CaloHitContributionCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" +#include "edm4hep/RawCalorimeterHitCollection.h" +#include "edm4hep/CalorimeterHitCollection.h" +#include "edm4hep/ParticleIDCollection.h" +#include "edm4hep/ClusterCollection.h" #include "edm4hep/TrackerHitCollection.h" +#include "edm4hep/TrackerHitPlaneCollection.h" +#include "edm4hep/RawTimeSeriesCollection.h" +#include "edm4hep/TrackCollection.h" #include "edm4hep/VertexCollection.h" +#include "edm4hep/ReconstructedParticleCollection.h" +#include "edm4hep/SimPrimaryIonizationClusterCollection.h" +#include "edm4hep/TrackerPulseCollection.h" +#include "edm4hep/RecIonizationClusterCollection.h" +#include "edm4hep/TimeSeriesCollection.h" +#include "edm4hep/RecDqdxCollection.h" + +#include "edm4hep/MCRecoParticleAssociationCollection.h" +#include "edm4hep/MCRecoCaloAssociationCollection.h" +#include "edm4hep/MCRecoTrackerAssociationCollection.h" +#include "edm4hep/MCRecoTrackerHitPlaneAssociationCollection.h" +#include "edm4hep/MCRecoCaloParticleAssociationCollection.h" +#include "edm4hep/MCRecoClusterParticleAssociationCollection.h" +#include "edm4hep/MCRecoTrackParticleAssociationCollection.h" +#include "edm4hep/RecoParticleVertexAssociationCollection.h" // podio specific includes #include "podio/Frame.h" @@ -43,6 +60,7 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& continue; } + // Datatypes if (coll->getTypeName() == "edm4hep::EventHeaderCollection") { auto& eventCollection = frame.get(collList[i]); nlohmann::json jsonColl{{ @@ -57,32 +75,23 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& {{"collection", particleCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, }}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::MCRecoParticleAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::VertexCollection") { - auto& vertexCollection = frame.get(collList[i]); + } else if (coll->getTypeName() == "edm4hep::SimTrackerHitCollection") { + auto& hitCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], - {{"collection", vertexCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::TrackCollection") { - auto& trackCollection = frame.get(collList[i]); + } else if (coll->getTypeName() == "edm4hep::CaloHitContributionCollection") { + auto& hitContribCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], - {{"collection", trackCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], {{"collection", hitContribCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::TrackerHitCollection") { - auto& hitCollection = frame.get(collList[i]); + } else if (coll->getTypeName() == "edm4hep::SimCalorimeterHitCollection") { + auto& hitCollection = frame.get(collList[i]); nlohmann::json jsonColl{ {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::SimTrackerHitCollection") { - auto& hitCollection = frame.get(collList[i]); + } else if (coll->getTypeName() == "edm4hep::RawCalorimeterHitCollection") { + auto& hitCollection = frame.get(collList[i]); nlohmann::json jsonColl{ {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); @@ -91,10 +100,11 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& nlohmann::json jsonColl{ {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::SimCalorimeterHitCollection") { - auto& hitCollection = frame.get(collList[i]); + } else if (coll->getTypeName() == "edm4hep::ParticleIDCollection") { + auto& particleIDCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], + {{"collection", particleIDCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::ClusterCollection") { auto& clusterCollection = frame.get(collList[i]); @@ -102,17 +112,198 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& {collList[i], {{"collection", clusterCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::TrackerHitCollection") { + auto& hitCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::TrackerHitPlaneCollection") { + auto& hitPlaneCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], {{"collection", hitPlaneCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::RawTimeSeries") { + auto& rtsCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], {{"collection", rtsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::TrackCollection") { + auto& trackCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], + {{"collection", trackCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::VertexCollection") { + auto& vertexCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], + {{"collection", vertexCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::ReconstructedParticleCollection") { auto& recoParticleCollection = frame.get(collList[i]); nlohmann::json jsonColl{ {collList[i], {{"collection", recoParticleCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::ParticleIDCollection") { - auto& particleIDCollection = frame.get(collList[i]); + } else if (coll->getTypeName() == "edm4hep::SimPrimaryIonizationClusterCollection") { + auto& spicCollection = frame.get(collList[i]); nlohmann::json jsonColl{ {collList[i], - {{"collection", particleIDCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {{"collection", spicCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::TrackerPulseCollection") { + auto& tpCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], + {{"collection", tpCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::RecIonizationClusterCollection") { + auto& ricCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], + {{"collection", ricCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::TimeSeriesCollection") { + auto& tsCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], + {{"collection", tsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::RecDqdxCollection") { + auto& recCollection = frame.get(collList[i]); + nlohmann::json jsonColl{ + {collList[i], + {{"collection", recCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } + // Associations + else if (coll->getTypeName() == "edm4hep::MCRecoParticleAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::MCRecoCaloAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::MCRecoTrackerAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::MCRecoTrackerHitPlaneAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::MCRecoCaloParticleAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::MCRecoClusterParticleAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::MCRecoTrackParticleAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "edm4hep::RecoParticleVertexAssociationCollection") { + auto& assocCollection = frame.get(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } + // Podio user data + else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); + } else if (coll->getTypeName() == "podio::UserDataCollection") { + auto& userCollection = frame.get>(collList[i]); + nlohmann::json jsonColl{{ + collList[i], + {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, + }}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else { std::cout << "WARNING: Collection type not recognized!\n" @@ -130,7 +321,9 @@ std::vector splitString(const std::string& inString) { while (sStream.good()) { std::string subString; getline(sStream, subString, ','); - outString.emplace_back(subString); + if (!subString.empty()) { + outString.emplace_back(subString); + } } return outString; @@ -146,6 +339,10 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const nlohmann::json allEventsDict; unsigned nEvents = reader.getEntries(frameName); + if (nEvents < 1) { + std::cout << "WARNING: Input file contains no events!" << std::endl; + return EXIT_SUCCESS; + } if (nEventsMax > 0) { if ((unsigned)nEventsMax < nEvents) { nEvents = nEventsMax; @@ -153,6 +350,20 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const } auto collList = splitString(requestedCollections); + if (collList.empty()) { + auto frame = podio::Frame(reader.readEntry(frameName, 0)); + collList = frame.getAvailableCollections(); + } + if (collList.empty()) { + std::cout << "WARNING: Input file does not contain any collections!" << std::endl; + return EXIT_SUCCESS; + } + if (verboser) { + std::cout << "INFO: Following collections will be converted:" << std::endl; + for (const auto& collName: collList) { + std::cout << " * " << collName << std::endl; + } + } std::vector eventVec; if (!requestedEvents.empty()) { @@ -210,7 +421,7 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const } if (eventVec.empty()) { - unsigned modInfo = nEvents / 10; + const unsigned modInfo = nEvents > 10 ? nEvents / 10 : 1; for (unsigned i = 0; i < nEvents; ++i) { if (verboser && i % modInfo == 0) { std::cout << "INFO: Reading event " << i << std::endl; diff --git a/tools/src/edm4hep2json.cxx b/tools/src/edm4hep2json.cxx index cdd3e6460..384e4a8cd 100644 --- a/tools/src/edm4hep2json.cxx +++ b/tools/src/edm4hep2json.cxx @@ -2,7 +2,11 @@ #include "edm4hep2json.hxx" // ROOT +#include "TFile.h" + +// podio #include "podio/ROOTFrameReader.h" +#include "podio/ROOTLegacyReader.h" // std #include @@ -27,8 +31,8 @@ void printHelp() { } int main(int argc, char** argv) { - std::filesystem::path inFile; - std::filesystem::path outFile; + std::filesystem::path inFilePath; + std::filesystem::path outFilePath; std::string requestedCollections; std::string requestedEvents; std::string frameName = "events"; @@ -54,10 +58,10 @@ int main(int argc, char** argv) { switch (opt) { case 'i': - inFile = std::filesystem::path(optarg); + inFilePath = std::filesystem::path(optarg); break; case 'o': - outFile = std::filesystem::path(optarg); + outFilePath = std::filesystem::path(optarg); break; case 'l': requestedCollections = std::string(optarg); @@ -92,39 +96,49 @@ int main(int argc, char** argv) { } for (int i = optind; i < argc; ++i) { - inFile = std::string(argv[i]); + inFilePath = std::string(argv[i]); } - if (inFile.empty()) { - std::cout << "ERROR: Input .root file not provided!" << std::endl; + if (inFilePath.empty()) { + std::cerr << "ERROR: Input .root file not provided!" << std::endl; return EXIT_FAILURE; } - if (!std::filesystem::exists(inFile)) { - std::cout << "ERROR: Input .root file can't be read!" << std::endl; + if (!std::filesystem::exists(inFilePath)) { + std::cerr << "ERROR: Input .root file can't be read!" << std::endl; return EXIT_FAILURE; } - if (requestedCollections.empty()) { - requestedCollections = "GenParticles,BuildUpVertices,SiTracks," - "PandoraClusters,VertexJets,EventHeader"; - if (verboser) { - std::cout << "DEBUG: Using default collection to convert:\n" - << " " << requestedCollections << std::endl; - } - } - - if (outFile.empty()) { - std::string outFileStr = inFile.stem().string(); + if (outFilePath.empty()) { + std::string outFileStr = inFilePath.stem().string(); if (outFileStr.find(".edm4hep") != std::string::npos) { outFileStr = outFileStr.erase(outFileStr.find(".edm4hep"), 8); } if (outFileStr.find("_edm4hep") != std::string::npos) { outFileStr = outFileStr.erase(outFileStr.find("_edm4hep"), 8); } - outFile = std::filesystem::path(outFileStr + ".edm4hep.json"); + outFilePath = std::filesystem::path(outFileStr + ".edm4hep.json"); + } + + bool legacyReader = false; + { + std::unique_ptr inFile(TFile::Open(inFilePath.c_str(), "READ")); + legacyReader = !inFile->GetListOfKeys()->FindObject("podio_metadata"); + } + + if (legacyReader) { + std::cout << "WARNING: Reading legacy file, some collections might not be recognized!" << std::endl; + return read_frames(inFilePath, + outFilePath, + requestedCollections, + requestedEvents, + frameName, + nEventsMax, + verboser); + } else { + return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, frameName, + nEventsMax, verboser); } - return read_frames(inFile, outFile, requestedCollections, requestedEvents, frameName, - nEventsMax, verboser); + return EXIT_SUCCESS; } From cbc8422dae48ae698615a5ec3558bcf319fd1553 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Wed, 20 Sep 2023 11:26:59 +0200 Subject: [PATCH 2/9] Formatting --- tools/include/edm4hep2json.hxx | 52 ++++++++++++++++------------------ tools/src/edm4hep2json.cxx | 13 +++------ 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index 9d61ac01d..c5e03b2dd 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -2,34 +2,34 @@ #define EDM4HEP_TO_JSON_H // EDM4hep event data model -#include "edm4hep/EventHeaderCollection.h" -#include "edm4hep/MCParticleCollection.h" -#include "edm4hep/SimTrackerHitCollection.h" #include "edm4hep/CaloHitContributionCollection.h" -#include "edm4hep/SimCalorimeterHitCollection.h" -#include "edm4hep/RawCalorimeterHitCollection.h" #include "edm4hep/CalorimeterHitCollection.h" -#include "edm4hep/ParticleIDCollection.h" #include "edm4hep/ClusterCollection.h" -#include "edm4hep/TrackerHitCollection.h" -#include "edm4hep/TrackerHitPlaneCollection.h" +#include "edm4hep/EventHeaderCollection.h" +#include "edm4hep/MCParticleCollection.h" +#include "edm4hep/ParticleIDCollection.h" +#include "edm4hep/RawCalorimeterHitCollection.h" #include "edm4hep/RawTimeSeriesCollection.h" -#include "edm4hep/TrackCollection.h" -#include "edm4hep/VertexCollection.h" +#include "edm4hep/RecDqdxCollection.h" +#include "edm4hep/RecIonizationClusterCollection.h" #include "edm4hep/ReconstructedParticleCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" #include "edm4hep/SimPrimaryIonizationClusterCollection.h" -#include "edm4hep/TrackerPulseCollection.h" -#include "edm4hep/RecIonizationClusterCollection.h" +#include "edm4hep/SimTrackerHitCollection.h" #include "edm4hep/TimeSeriesCollection.h" -#include "edm4hep/RecDqdxCollection.h" +#include "edm4hep/TrackCollection.h" +#include "edm4hep/TrackerHitCollection.h" +#include "edm4hep/TrackerHitPlaneCollection.h" +#include "edm4hep/TrackerPulseCollection.h" +#include "edm4hep/VertexCollection.h" -#include "edm4hep/MCRecoParticleAssociationCollection.h" #include "edm4hep/MCRecoCaloAssociationCollection.h" -#include "edm4hep/MCRecoTrackerAssociationCollection.h" -#include "edm4hep/MCRecoTrackerHitPlaneAssociationCollection.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/RecoParticleVertexAssociationCollection.h" // podio specific includes @@ -83,7 +83,8 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& } else if (coll->getTypeName() == "edm4hep::CaloHitContributionCollection") { auto& hitContribCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], {{"collection", hitContribCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], + {{"collection", hitContribCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::SimCalorimeterHitCollection") { auto& hitCollection = frame.get(collList[i]); @@ -120,7 +121,8 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& } else if (coll->getTypeName() == "edm4hep::TrackerHitPlaneCollection") { auto& hitPlaneCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], {{"collection", hitPlaneCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], + {{"collection", hitPlaneCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::RawTimeSeries") { auto& rtsCollection = frame.get(collList[i]); @@ -154,26 +156,22 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& } else if (coll->getTypeName() == "edm4hep::TrackerPulseCollection") { auto& tpCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], - {{"collection", tpCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], {{"collection", tpCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::RecIonizationClusterCollection") { auto& ricCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], - {{"collection", ricCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], {{"collection", ricCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::TimeSeriesCollection") { auto& tsCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], - {{"collection", tsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], {{"collection", tsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } else if (coll->getTypeName() == "edm4hep::RecDqdxCollection") { auto& recCollection = frame.get(collList[i]); nlohmann::json jsonColl{ - {collList[i], - {{"collection", recCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {collList[i], {{"collection", recCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } // Associations @@ -360,7 +358,7 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const } if (verboser) { std::cout << "INFO: Following collections will be converted:" << std::endl; - for (const auto& collName: collList) { + for (const auto& collName : collList) { std::cout << " * " << collName << std::endl; } } diff --git a/tools/src/edm4hep2json.cxx b/tools/src/edm4hep2json.cxx index 384e4a8cd..a502e215f 100644 --- a/tools/src/edm4hep2json.cxx +++ b/tools/src/edm4hep2json.cxx @@ -128,16 +128,11 @@ int main(int argc, char** argv) { if (legacyReader) { std::cout << "WARNING: Reading legacy file, some collections might not be recognized!" << std::endl; - return read_frames(inFilePath, - outFilePath, - requestedCollections, - requestedEvents, - frameName, - nEventsMax, - verboser); + return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, + frameName, nEventsMax, verboser); } else { - return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, frameName, - nEventsMax, verboser); + return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, + frameName, nEventsMax, verboser); } return EXIT_SUCCESS; From aec4e6d2b3c89d94bee844cbb498e5e92fe3457f Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Thu, 21 Sep 2023 10:40:28 +0200 Subject: [PATCH 3/9] RawTimeSeriesCollection --- tools/include/edm4hep2json.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index c5e03b2dd..5bfe205a5 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -124,7 +124,7 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& {collList[i], {{"collection", hitPlaneCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); - } else if (coll->getTypeName() == "edm4hep::RawTimeSeries") { + } else if (coll->getTypeName() == "edm4hep::RawTimeSeriesCollection") { auto& rtsCollection = frame.get(collList[i]); nlohmann::json jsonColl{ {collList[i], {{"collection", rtsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; @@ -425,7 +425,7 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const std::cout << "INFO: Reading event " << i << std::endl; } - auto frame = podio::Frame(reader.readNextEntry(frameName)); + auto frame = podio::Frame(reader.readEntry(frameName, i)); auto eventDict = processEvent(frame, collList, verboser, reader.currentFileVersion()); allEventsDict["Event " + std::to_string(i)] = eventDict; } From 43b187ba177e0825dcc4f835fcb7027d25d516db Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Thu, 21 Sep 2023 11:43:32 +0200 Subject: [PATCH 4/9] Add EDM4hep version to the output --- tools/include/edm4hep2json.hxx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index 5bfe205a5..6abbbbaaa 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -32,6 +32,8 @@ #include "edm4hep/MCRecoTrackerHitPlaneAssociationCollection.h" #include "edm4hep/RecoParticleVertexAssociationCollection.h" +#include "edm4hep/EDM4hepVersion.h" + // podio specific includes #include "podio/Frame.h" #include "podio/UserDataCollection.h" @@ -48,11 +50,13 @@ #include #include -nlohmann::json processEvent(const podio::Frame& frame, std::vector& collList, bool verboser, +nlohmann::json processEvent(const podio::Frame& frame, std::vector& collList, podio::version::Version podioVersion) { std::stringstream podioVersionStr; podioVersionStr << podioVersion; - nlohmann::json jsonDict = {{"podioVersion", podioVersionStr.str()}}; + std::stringstream e4hVersionStr; + e4hVersionStr << edm4hep::version::build_version; + nlohmann::json jsonDict = {{"podioVersion", podioVersionStr.str()}, {"edm4hepVersion", e4hVersionStr.str()}}; for (unsigned i = 0; i < collList.size(); ++i) { auto coll = frame.get(collList[i]); @@ -426,13 +430,13 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const } auto frame = podio::Frame(reader.readEntry(frameName, i)); - auto eventDict = processEvent(frame, collList, verboser, reader.currentFileVersion()); + auto eventDict = processEvent(frame, collList, reader.currentFileVersion()); allEventsDict["Event " + std::to_string(i)] = eventDict; } } else { for (auto& i : eventVec) { auto frame = podio::Frame(reader.readEntry(frameName, i)); - auto eventDict = processEvent(frame, collList, verboser, reader.currentFileVersion()); + auto eventDict = processEvent(frame, collList, reader.currentFileVersion()); allEventsDict["Event " + std::to_string(i)] = eventDict; } } From bd1275c06901d776fed5c87a87aa5144918daf71 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Tue, 31 Oct 2023 17:53:19 +0100 Subject: [PATCH 5/9] Using teplate function insertIntoJson --- tools/include/edm4hep2json.hxx | 250 +++++++-------------------------- 1 file changed, 50 insertions(+), 200 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index 6abbbbaaa..a165a116e 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -44,12 +44,24 @@ // STL #include +#include #include #include #include #include #include +template +void insertIntoJson(nlohmann::json& jsonDict, + const podio::CollectionBase* coll, + const std::string& name) { + const auto* typedColl = static_cast(coll); // safe to cast, since we have queried the type before + nlohmann::json jsonColl{ + {name, + {{"collection", *typedColl}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + jsonDict.insert(jsonColl.begin(), jsonColl.end()); +} + nlohmann::json processEvent(const podio::Frame& frame, std::vector& collList, podio::version::Version podioVersion) { std::stringstream podioVersionStr; @@ -66,247 +78,85 @@ nlohmann::json processEvent(const podio::Frame& frame, std::vector& // Datatypes if (coll->getTypeName() == "edm4hep::EventHeaderCollection") { - auto& eventCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", eventCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCParticleCollection") { - auto& particleCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", particleCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::SimTrackerHitCollection") { - auto& hitCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::CaloHitContributionCollection") { - auto& hitContribCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", hitContribCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::SimCalorimeterHitCollection") { - auto& hitCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::RawCalorimeterHitCollection") { - auto& hitCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::CalorimeterHitCollection") { - auto& hitCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::ParticleIDCollection") { - auto& particleIDCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", particleIDCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::ClusterCollection") { - auto& clusterCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", clusterCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::TrackerHitCollection") { - auto& hitCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", hitCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::TrackerHitPlaneCollection") { - auto& hitPlaneCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", hitPlaneCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::RawTimeSeriesCollection") { - auto& rtsCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", rtsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::TrackCollection") { - auto& trackCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", trackCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::VertexCollection") { - auto& vertexCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", vertexCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::ReconstructedParticleCollection") { - auto& recoParticleCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", recoParticleCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::SimPrimaryIonizationClusterCollection") { - auto& spicCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], - {{"collection", spicCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::TrackerPulseCollection") { - auto& tpCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", tpCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::RecIonizationClusterCollection") { - auto& ricCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", ricCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::TimeSeriesCollection") { - auto& tsCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", tsCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::RecDqdxCollection") { - auto& recCollection = frame.get(collList[i]); - nlohmann::json jsonColl{ - {collList[i], {{"collection", recCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } // Associations else if (coll->getTypeName() == "edm4hep::MCRecoParticleAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCRecoCaloAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCRecoTrackerAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCRecoTrackerHitPlaneAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCRecoCaloParticleAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCRecoClusterParticleAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::MCRecoTrackParticleAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "edm4hep::RecoParticleVertexAssociationCollection") { - auto& assocCollection = frame.get(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", assocCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson(jsonDict, coll, collList[i]); } // Podio user data else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else if (coll->getTypeName() == "podio::UserDataCollection") { - auto& userCollection = frame.get>(collList[i]); - nlohmann::json jsonColl{{ - collList[i], - {{"collection", userCollection}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}, - }}; - jsonDict.insert(jsonColl.begin(), jsonColl.end()); + insertIntoJson>(jsonDict, coll, collList[i]); } else { std::cout << "WARNING: Collection type not recognized!\n" << " " << coll->getTypeName() << "\n"; From 5633e277f1a1b82f773c52a871876128b244347e Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Tue, 31 Oct 2023 18:05:43 +0100 Subject: [PATCH 6/9] Correct formatting --- tools/include/edm4hep2json.hxx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index a165a116e..50dbe0f3f 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -51,14 +51,11 @@ #include #include -template -void insertIntoJson(nlohmann::json& jsonDict, - const podio::CollectionBase* coll, - const std::string& name) { +template +void insertIntoJson(nlohmann::json& jsonDict, const podio::CollectionBase* coll, const std::string& name) { const auto* typedColl = static_cast(coll); // safe to cast, since we have queried the type before nlohmann::json jsonColl{ - {name, - {{"collection", *typedColl}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; + {name, {{"collection", *typedColl}, {"collID", coll->getID()}, {"collType", coll->getTypeName()}}}}; jsonDict.insert(jsonColl.begin(), jsonColl.end()); } From c0b3324892de191a51d3f85068e11cdaeddd1d37 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Wed, 1 Nov 2023 10:46:41 +0100 Subject: [PATCH 7/9] Adding test which checks for all the collections --- test/CMakeLists.txt | 7 +---- test/tools/CMakeLists.txt | 12 ++++++++ test/tools/test_all_collections.py | 44 ++++++++++++++++++++++++++++++ tools/src/edm4hep2json.cxx | 3 +- 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 test/tools/CMakeLists.txt create mode 100644 test/tools/test_all_collections.py diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1ee3c3c41..11eec0b14 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -79,10 +79,5 @@ if(HepMC3_FOUND AND HepPDT_FOUND ) ) endif() -if (nlohmann_json_FOUND) - add_test(NAME convert_events COMMAND edm4hep2json edm4hep_events.root) - set_property(TEST convert_events PROPERTY DEPENDS write_events) - set_test_env(convert_events) -endif() - add_subdirectory(utils) +add_subdirectory(tools) diff --git a/test/tools/CMakeLists.txt b/test/tools/CMakeLists.txt new file mode 100644 index 000000000..2d29513f2 --- /dev/null +++ b/test/tools/CMakeLists.txt @@ -0,0 +1,12 @@ +if (nlohmann_json_FOUND) + add_test(NAME convert_events + COMMAND edm4hep2json ${CMAKE_CURRENT_BINARY_DIR}/../edm4hep_events.root) + set_property(TEST convert_events PROPERTY DEPENDS write_events) + set_test_env(convert_events) + + add_test(NAME test_convert_all_collections + COMMAND python ${CMAKE_CURRENT_LIST_DIR}/test_all_collections.py + ${PROJECT_SOURCE_DIR}/edm4hep.yaml + ${PROJECT_SOURCE_DIR}/tools/include/edm4hep2json.hxx) + set_test_env(test_convert_all_collections) +endif() diff --git a/test/tools/test_all_collections.py b/test/tools/test_all_collections.py new file mode 100644 index 000000000..870ceeb67 --- /dev/null +++ b/test/tools/test_all_collections.py @@ -0,0 +1,44 @@ +''' +Tests if all collection header files are included in the cxx file. +''' + +import sys +import argparse +import yaml + + +def test(yamlfile_path, cxxfile_path): + ''' + Test itself. + Takes two parameters, Podio YAML file location and cxx file to be checked. + ''' + + with open(yamlfile_path, mode='r', encoding="utf-8") as yamlfile: + datamodel = yaml.safe_load(yamlfile) + + with open(cxxfile_path, mode='r', encoding="utf-8") as cxxfile: + cxxfile_lines = cxxfile.readlines() + + datatypes = datamodel['datatypes'] + + for collname in datatypes: + include_string = '#include "' + collname.replace('::', '/') + \ + 'Collection.h"' + include_found = False + for line in cxxfile_lines: + if include_string in line: + include_found = True + + if not include_found: + print('ERROR: Following collection not included in the cxx file.') + print(' ' + collname) + sys.exit(2) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Test all collections') + parser.add_argument('yamlfile') + parser.add_argument('cxxfile') + args = parser.parse_args() + + test(args.yamlfile, args.cxxfile) diff --git a/tools/src/edm4hep2json.cxx b/tools/src/edm4hep2json.cxx index a502e215f..075a656e4 100644 --- a/tools/src/edm4hep2json.cxx +++ b/tools/src/edm4hep2json.cxx @@ -105,7 +105,8 @@ int main(int argc, char** argv) { } if (!std::filesystem::exists(inFilePath)) { - std::cerr << "ERROR: Input .root file can't be read!" << std::endl; + std::cerr << "ERROR: Input .root file can't be read!\n " + << inFilePath << std::endl; return EXIT_FAILURE; } From cacd21bd4eeba481747ca256246f9dee01900822 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Wed, 1 Nov 2023 10:47:10 +0100 Subject: [PATCH 8/9] Formatting fix --- tools/src/edm4hep2json.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/src/edm4hep2json.cxx b/tools/src/edm4hep2json.cxx index 075a656e4..affbf2b4a 100644 --- a/tools/src/edm4hep2json.cxx +++ b/tools/src/edm4hep2json.cxx @@ -105,8 +105,7 @@ int main(int argc, char** argv) { } if (!std::filesystem::exists(inFilePath)) { - std::cerr << "ERROR: Input .root file can't be read!\n " - << inFilePath << std::endl; + std::cerr << "ERROR: Input .root file can't be read!\n " << inFilePath << std::endl; return EXIT_FAILURE; } From bd30bbd946a23c60cb9ab60061a49720e76d26e5 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Wed, 1 Nov 2023 19:42:40 +0100 Subject: [PATCH 9/9] Checking for actual conversion --- test/tools/test_all_collections.py | 39 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/test/tools/test_all_collections.py b/test/tools/test_all_collections.py index 870ceeb67..58ff2fc0e 100644 --- a/test/tools/test_all_collections.py +++ b/test/tools/test_all_collections.py @@ -1,8 +1,9 @@ ''' -Tests if all collection header files are included in the cxx file. +Tests if all datatypes are used in the cxx file. ''' import sys +import re import argparse import yaml @@ -16,23 +17,27 @@ def test(yamlfile_path, cxxfile_path): with open(yamlfile_path, mode='r', encoding="utf-8") as yamlfile: datamodel = yaml.safe_load(yamlfile) + # List stores lines of cxx code on which `insertToJson` is used + datatypes_found = [] + with open(cxxfile_path, mode='r', encoding="utf-8") as cxxfile: - cxxfile_lines = cxxfile.readlines() - - datatypes = datamodel['datatypes'] - - for collname in datatypes: - include_string = '#include "' + collname.replace('::', '/') + \ - 'Collection.h"' - include_found = False - for line in cxxfile_lines: - if include_string in line: - include_found = True - - if not include_found: - print('ERROR: Following collection not included in the cxx file.') - print(' ' + collname) - sys.exit(2) + for cxxline in cxxfile: + cxxline = cxxfile.readline() + result = re.search('insertIntoJson', + cxxline) + if result: + datatypes_found += ['edm4hep::' + result.group(1)] + + datatypes_found = set(datatypes_found) + + datatypes = set(datamodel['datatypes']) + + if not datatypes.issubset(datatypes_found): + missing_datatypes = datatypes - datatypes_found + print('ERROR: One or more datatypes are not being converted:') + for datatype in missing_datatypes: + print(' ' + datatype) + sys.exit(2) if __name__ == "__main__":