Skip to content

Commit

Permalink
Use a podio::Reader for edm4hep2json
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Jul 23, 2024
1 parent 4e5191a commit 7949413
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
17 changes: 7 additions & 10 deletions tools/include/edm4hep2json.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

// podio specific includes
#include "podio/Frame.h"
#include "podio/Reader.h"
#include "podio/UserDataCollection.h"
#include "podio/podioVersion.h"

Expand All @@ -43,7 +44,6 @@
// STL
#include <cassert>
#include <cstdint>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -174,16 +174,13 @@ std::vector<std::string> splitString(const std::string& inString) {
return outString;
}

template <typename ReaderT>
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);
const std::string& requestedEvents, int nEventsMax = -1, bool verboser = false) {
podio::Reader reader = podio::makeReader(filename);

nlohmann::json allEventsDict;

unsigned nEvents = reader.getEntries(frameName);
unsigned nEvents = reader.getEvents();
if (nEvents < 1) {
std::cout << "WARNING: Input file contains no events!" << std::endl;
return EXIT_SUCCESS;
Expand All @@ -196,7 +193,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.readEvent(0));
collList = frame.getAvailableCollections();
}
if (collList.empty()) {
Expand Down Expand Up @@ -272,13 +269,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.readEvent(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.readEvent(i));
auto eventDict = processEvent(frame, collList, reader.currentFileVersion());
allEventsDict["Event " + std::to_string(i)] = eventDict;
}
Expand Down
14 changes: 1 addition & 13 deletions tools/src/edm4hep2json.cxx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// EDM4hep
#include "edm4hep2json.hxx"

// ROOT
#include "TFile.h"

// podio
#include "podio/ROOTReader.h"

// std
#include <filesystem>

Expand Down Expand Up @@ -34,7 +28,6 @@ int main(int argc, char** argv) {
std::filesystem::path outFilePath;
std::string requestedCollections;
std::string requestedEvents;
std::string frameName = "events";
bool verboser = false;
int nEventsMax = -1;

Expand All @@ -43,7 +36,6 @@ int main(int argc, char** argv) {
{"coll-list", required_argument, nullptr, 'l'},
{"events", required_argument, nullptr, 'e'},
{"nevents", required_argument, nullptr, 'n'},
{"frame-name", required_argument, nullptr, 'f'},
{"verbose", no_argument, nullptr, 'v'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}};
Expand All @@ -68,9 +60,6 @@ int main(int argc, char** argv) {
case 'e':
requestedEvents = std::string(optarg);
break;
case 'f':
frameName = std::string(optarg);
break;
case 'n':
try {
nEventsMax = std::stoi(optarg);
Expand Down Expand Up @@ -119,8 +108,7 @@ int main(int argc, char** argv) {
outFilePath = std::filesystem::path(outFileStr + ".edm4hep.json");
}

return read_frames<podio::ROOTReader>(inFilePath, outFilePath, requestedCollections, requestedEvents, frameName,
nEventsMax, verboser);
return read_frames(inFilePath, outFilePath, requestedCollections, requestedEvents, nEventsMax, verboser);

return EXIT_SUCCESS;
}

0 comments on commit 7949413

Please sign in to comment.