From 0035f905ce53b250cc6d6e57767568bc0c1d7871 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Wed, 24 Jul 2024 14:42:27 +0200 Subject: [PATCH 1/4] Drop support for the legacy reader and use a podio::Reader instead --- tools/include/edm4hep2json.hxx | 12 +++++------- tools/src/edm4hep2json.cxx | 26 ++++---------------------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index b4fbb3452..440b5391e 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -34,6 +34,7 @@ // podio specific includes #include "podio/Frame.h" +#include "podio/Reader.h" #include "podio/UserDataCollection.h" #include "podio/podioVersion.h" @@ -170,13 +171,10 @@ std::vector splitString(const std::string& inString) { return outString; } - -template int read_frames(const std::string& filename, const std::string& jsonFile, const std::string& requestedCollections, const std::string& requestedEvents, const std::string& frameName, int nEventsMax = -1, bool verboser = false) { - ReaderT reader; - reader.openFile(filename); + podio::Reader reader = podio::makeReader(filename); nlohmann::json allEventsDict; @@ -193,7 +191,7 @@ 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)); + auto frame = podio::Frame(reader.readFrame(frameName, 0)); collList = frame.getAvailableCollections(); } if (collList.empty()) { @@ -269,13 +267,13 @@ 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.readEntry(frameName, i)); + auto frame = podio::Frame(reader.readFrame(frameName, i)); 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 frame = podio::Frame(reader.readFrame(frameName, i)); auto eventDict = processEvent(frame, collList, reader.currentFileVersion()); allEventsDict["Event " + std::to_string(i)] = eventDict; } diff --git a/tools/src/edm4hep2json.cxx b/tools/src/edm4hep2json.cxx index c6e1f1686..461b33cbf 100644 --- a/tools/src/edm4hep2json.cxx +++ b/tools/src/edm4hep2json.cxx @@ -1,13 +1,8 @@ +#include "podio/FrameCategories.h" + // EDM4hep #include "edm4hep2json.hxx" -// ROOT -#include "TFile.h" - -// podio -#include "podio/ROOTLegacyReader.h" -#include "podio/ROOTReader.h" - // std #include @@ -35,7 +30,7 @@ int main(int argc, char** argv) { std::filesystem::path outFilePath; std::string requestedCollections; std::string requestedEvents; - std::string frameName = "events"; + std::string frameName = podio::Category::Event; bool verboser = false; int nEventsMax = -1; @@ -120,20 +115,7 @@ int main(int argc, char** argv) { 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(inFilePath, outFilePath, requestedCollections, requestedEvents, frameName, nEventsMax, verboser); return EXIT_SUCCESS; } From 2425b55ed59e7cfecf2213e21b15c357b2283c61 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Wed, 24 Jul 2024 16:59:15 +0200 Subject: [PATCH 2/4] Link to podioIO --- tools/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index f27b41c1d..0f79fbe32 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -12,7 +12,7 @@ if (nlohmann_json_FOUND) target_link_libraries(edm4hep2json PUBLIC ROOT::Core ROOT::Tree podio::podio - podio::podioRootIO + podio::podioIO edm4hep nlohmann_json::nlohmann_json) From 4f6fe0ccf10e1abf5a6f733ba442a09e32b31315 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Fri, 9 Aug 2024 14:32:10 +0200 Subject: [PATCH 3/4] Remove unnecessary copies using `podio::Frame` --- tools/include/edm4hep2json.hxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/include/edm4hep2json.hxx b/tools/include/edm4hep2json.hxx index 440b5391e..799e839fc 100644 --- a/tools/include/edm4hep2json.hxx +++ b/tools/include/edm4hep2json.hxx @@ -191,7 +191,7 @@ int read_frames(const std::string& filename, const std::string& jsonFile, const auto collList = splitString(requestedCollections); if (collList.empty()) { - auto frame = podio::Frame(reader.readFrame(frameName, 0)); + auto frame = reader.readFrame(frameName, 0); collList = frame.getAvailableCollections(); } if (collList.empty()) { @@ -267,13 +267,13 @@ 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.readFrame(frameName, i)); + auto frame = reader.readFrame(frameName, i); auto eventDict = processEvent(frame, collList, reader.currentFileVersion()); allEventsDict["Event " + std::to_string(i)] = eventDict; } } else { for (auto& i : eventVec) { - auto frame = podio::Frame(reader.readFrame(frameName, i)); + auto frame = reader.readFrame(frameName, i); auto eventDict = processEvent(frame, collList, reader.currentFileVersion()); allEventsDict["Event " + std::to_string(i)] = eventDict; } From 3901e6dc94b510970b66ea475d754ddd7c0249ec Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Fri, 9 Aug 2024 15:14:57 +0200 Subject: [PATCH 4/4] Add an error message for edm4hep2json when the file is incompatible --- tools/src/edm4hep2json.cxx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/src/edm4hep2json.cxx b/tools/src/edm4hep2json.cxx index 461b33cbf..399196d5b 100644 --- a/tools/src/edm4hep2json.cxx +++ b/tools/src/edm4hep2json.cxx @@ -9,6 +9,8 @@ // *nix #include +#include "TFile.h" + void printHelp() { std::cout << "Usage: edm4hep2json [olenfvh] FILEPATH\n" << " -o/--out-file output file path\n" @@ -115,7 +117,14 @@ int main(int argc, char** argv) { outFilePath = std::filesystem::path(outFileStr + ".edm4hep.json"); } - return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, frameName, nEventsMax, verboser); + { + std::unique_ptr inFile(TFile::Open(inFilePath.c_str(), "READ")); + if (!inFile->GetListOfKeys()->FindObject("podio_metadata")) { + std::cout << "ERROR: Reading file produced with an incompatible version of EDM4hep. Aborting..." << std::endl; + return 1; + } + inFile->Close(); + } - return EXIT_SUCCESS; + return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, frameName, nEventsMax, verboser); }