From ff46b52f41974df9cdd877f84f23fe2ebe364585 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 20 Dec 2024 15:05:44 +0100 Subject: [PATCH] Add a test sequence for PID with metadata --- test/k4FWCoreTest/CMakeLists.txt | 2 + .../options/ExampleParticleIDMetadata.py | 80 +++++++++++++++++++ .../ExampleFunctionalProducerMultiple.cpp | 27 ++++--- 3 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 test/k4FWCoreTest/options/ExampleParticleIDMetadata.py diff --git a/test/k4FWCoreTest/CMakeLists.txt b/test/k4FWCoreTest/CMakeLists.txt index 0e7291e4..f956b698 100644 --- a/test/k4FWCoreTest/CMakeLists.txt +++ b/test/k4FWCoreTest/CMakeLists.txt @@ -190,6 +190,8 @@ add_test_with_env(FunctionalProducerRNTuple options/ExampleFunctionalProducerRNT add_test_with_env(FunctionalTTreeToRNTuple options/ExampleFunctionalTTreeToRNTuple.py PROPERTIES DEPENDS FunctionalProducer ADD_TO_CHECK_FILES) add_test_with_env(GaudiFunctional options/ExampleGaudiFunctional.py PROPERTIES DEPENDS FunctionalProducer ADD_TO_CHECK_FILES) +add_test_with_env(ParticleIDMetadataFramework options/ExampleParticleIDMetadata.py) + # The following is done to make the tests work without installing the files in # the installation directory. The k4FWCore in the build directory is populated by diff --git a/test/k4FWCoreTest/options/ExampleParticleIDMetadata.py b/test/k4FWCoreTest/options/ExampleParticleIDMetadata.py new file mode 100644 index 00000000..a7a22ffb --- /dev/null +++ b/test/k4FWCoreTest/options/ExampleParticleIDMetadata.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2014-2024 Key4hep-Project. +# +# This file is part of Key4hep. +# See https://key4hep.github.io/key4hep-doc/ for further info. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Example showcasing how to use ParticleID related metadata""" + +from Gaudi.Configuration import INFO +from Configurables import ( + ExampleParticleIDProducer, + ExampleParticleIDConsumer, + ExampleFunctionalProducerMultiple, + EventDataSvc, +) +from k4FWCore import ApplicationMgr, IOSvc + +# NOTE: If you are not using the IOSvc (e.g. because you don't need I/O), make +# sure to add the MetadataSvc to the ExtSvc as that is necessary to store / +# retrieve the metadata for ParticleIDs +iosvc = IOSvc() +iosvc.Output = "example_with_particleids.root" +iosvc.outputCommands = ["drop *", "keep RecoParticles*"] + +reco_producer = ExampleFunctionalProducerMultiple( + "RecoProducer", OutputCollectionRecoParticles=["RecoParticles"] +) + +pid_producer1 = ExampleParticleIDProducer( + "PIDProducer1", + InputCollection=["RecoParticles"], + ParticleIDCollection=["RecoParticlesPIDs_1"], + PIDAlgoName="PIDAlgo1", + PIDParamNames=["single_param"], +) + +pid_producer2 = ExampleParticleIDProducer( + "PIDProducer2", + InputCollection=["RecoParticles"], + ParticleIDCollection=["RecoParticlesPIDs_2"], + PIDAlgoName="PIDAlgo2", + PIDParamNames=["param_1", "param_2", "param_3"], +) + +pid_consumer = ExampleParticleIDConsumer( + "PIDConsumer", + RecoParticleCollection=reco_producer.OutputCollectionRecoParticles, + # From first producer + ParticleIDCollection1=pid_producer1.ParticleIDCollection, + PIDAlgoName1=pid_producer1.PIDAlgoName, + PIDParamNames1=pid_producer1.PIDParamNames, + ParamName1="single_param", + # From second producer + ParticleIDCollection2=pid_producer2.ParticleIDCollection, + PIDAlgoName2=pid_producer2.PIDAlgoName, + PIDParamNames2=pid_producer2.PIDParamNames, + ParamName2="param_2", +) + +ApplicationMgr( + TopAlg=[reco_producer, pid_producer1, pid_producer2, pid_consumer], + EvtSel="NONE", + EvtMax=10, + ExtSvc=[EventDataSvc("EventDataSvc")], + OutputLevel=INFO, +) diff --git a/test/k4FWCoreTest/src/components/ExampleFunctionalProducerMultiple.cpp b/test/k4FWCoreTest/src/components/ExampleFunctionalProducerMultiple.cpp index 96a6dcb2..a56ec7bc 100644 --- a/test/k4FWCoreTest/src/components/ExampleFunctionalProducerMultiple.cpp +++ b/test/k4FWCoreTest/src/components/ExampleFunctionalProducerMultiple.cpp @@ -22,6 +22,7 @@ #include "k4FWCore/Producer.h" #include "edm4hep/MCParticleCollection.h" +#include "edm4hep/ReconstructedParticleCollection.h" #include "edm4hep/SimTrackerHitCollection.h" #include "edm4hep/TrackCollection.h" #include "edm4hep/TrackerHit3DCollection.h" @@ -33,19 +34,21 @@ using retType = std::tuple, edm4hep::MCParticleCollection, edm4hep::MCParticleCollection, - edm4hep::SimTrackerHitCollection, edm4hep::TrackerHit3DCollection, edm4hep::TrackCollection>; + edm4hep::SimTrackerHitCollection, edm4hep::TrackerHit3DCollection, edm4hep::TrackCollection, + edm4hep::ReconstructedParticleCollection>; struct ExampleFunctionalProducerMultiple final : k4FWCore::Producer { // The pairs in KeyValue can be changed from python and they correspond // to the names of the output collections ExampleFunctionalProducerMultiple(const std::string& name, ISvcLocator* svcLoc) - : Producer(name, svcLoc, {}, - {KeyValues("OutputCollectionFloat", {"VectorFloat"}), - KeyValues("OutputCollectionParticles1", {"MCParticles1"}), - KeyValues("OutputCollectionParticles2", {"MCParticles2"}), - KeyValues("OutputCollectionSimTrackerHits", {"SimTrackerHits"}), - KeyValues("OutputCollectionTrackerHits", {"TrackerHits"}), - KeyValues("OutputCollectionTracks", {"Tracks"})}) {} + : Producer( + name, svcLoc, {}, + {KeyValues("OutputCollectionFloat", {"VectorFloat"}), + KeyValues("OutputCollectionParticles1", {"MCParticles1"}), + KeyValues("OutputCollectionParticles2", {"MCParticles2"}), + KeyValues("OutputCollectionSimTrackerHits", {"SimTrackerHits"}), + KeyValues("OutputCollectionTrackerHits", {"TrackerHits"}), KeyValues("OutputCollectionTracks", {"Tracks"}), + KeyValues("OutputCollectionRecoParticles", {"RecoParticles"})}) {} // This is the function that will be called to produce the data retType operator()() const override { @@ -84,8 +87,14 @@ struct ExampleFunctionalProducerMultiple final : k4FWCore::Producer { track.addToTrackerHits(trackerHit); track.addToTracks(track2); + auto recos = edm4hep::ReconstructedParticleCollection(); + for (int i = 1; i < 5; ++i) { + auto reco = recos.create(); + reco.setPDG(i); + } + return std::make_tuple(std::move(floatVector), std::move(particles), edm4hep::MCParticleCollection(), - std::move(simTrackerHits), std::move(trackerHits), std::move(tracks)); + std::move(simTrackerHits), std::move(trackerHits), std::move(tracks), std::move(recos)); } private: