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";