From 1030e5ba8cae096239bee9dd214cb344ab720525 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 20 Dec 2024 15:16:22 +0100 Subject: [PATCH] Add a variadic overload for multiple metadata objects --- test/utils/test_PIDHandler.cpp | 22 +++++++++++++++++++ utils/include/edm4hep/utils/ParticleIDUtils.h | 8 +++++++ 2 files changed, 30 insertions(+) diff --git a/test/utils/test_PIDHandler.cpp b/test/utils/test_PIDHandler.cpp index 9e60195d5..a9f138153 100644 --- a/test/utils/test_PIDHandler.cpp +++ b/test/utils/test_PIDHandler.cpp @@ -161,6 +161,28 @@ TEST_CASE("PIDHandler w/ addMetaInfo", "[pid_utils]") { REQUIRE(handler.getParamIndex(42, "p2").value_or(-1) == 1); } +TEST_CASE("PIDHandler add multiple meta info objects", "[pid_utils]") { + using namespace edm4hep::utils; + auto handler = PIDHandler(); + + const auto pidInfo1 = ParticleIDMeta{"fancyAlgo", {"param1", "param2"}}; + const auto pidInfo2 = ParticleIDMeta{"fancyAlgo2", {"p1", "p2"}}; + const auto pidInfo3 = ParticleIDMeta{"fancyAlgo3", {"p1", "p2"}}; + + // Can add all of them at once or do it in steps + handler.addMetaInfos(pidInfo1); + handler.addMetaInfos(pidInfo2, pidInfo3); + + REQUIRE(handler.getParamIndex(pidInfo1.algoType(), "param2").value() == 1); + REQUIRE(handler.getParamIndex(pidInfo2.algoType(), "p1").value() == 0); + REQUIRE(handler.getParamIndex(pidInfo3.algoType(), "p2").value() == 1); + REQUIRE(handler.getAlgoType("fancyAlgo").value() == pidInfo1.algoType()); + REQUIRE(handler.getAlgoType("fancyAlgo3").value() == pidInfo3.algoType()); + + const auto duplicate = ParticleIDMeta{"fancyAlgo", {}}; + REQUIRE_THROWS_AS(handler.addMetaInfos(duplicate), std::runtime_error); +} + TEST_CASE("PIDHandler from Frame w/ metadata", "[pid_utils]") { using namespace edm4hep; const auto& [event, metadata] = createEventAndMetadata(); diff --git a/utils/include/edm4hep/utils/ParticleIDUtils.h b/utils/include/edm4hep/utils/ParticleIDUtils.h index d9c2791f6..ae3b54d36 100644 --- a/utils/include/edm4hep/utils/ParticleIDUtils.h +++ b/utils/include/edm4hep/utils/ParticleIDUtils.h @@ -85,6 +85,14 @@ class PIDHandler { /// Add meta information for a collection void addMetaInfo(const edm4hep::utils::ParticleIDMeta& pidInfo); + /// Add several meta informations simultaneously + template + void addMetaInfos(const PIDMetas&... pidMetas) { + static_assert((std::is_same_v && ...), + "Only ParticleIDMeta can be used to add metadata to a PIDHandler"); + (addMetaInfo(pidMetas), ...); + } + /// Retrieve all ParticleIDs that are related to the passed /// ReconstructedParticle std::vector getPIDs(const edm4hep::ReconstructedParticle& reco) const;