From 6b4e0aeee6008b7c9250ce4bb2e09f08b4fb0572 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Thu, 17 Oct 2024 13:31:37 +0200 Subject: [PATCH 1/5] Add an option to copy CellID metadata when overlaying --- k4Reco/Overlay/components/OverlayTiming.cpp | 23 +++++++++++++++++++++ k4Reco/Overlay/components/OverlayTiming.h | 4 ++++ k4Reco/Overlay/options/runOverlayTiming.py | 1 + 3 files changed, 28 insertions(+) diff --git a/k4Reco/Overlay/components/OverlayTiming.cpp b/k4Reco/Overlay/components/OverlayTiming.cpp index 27003c4..510302c 100644 --- a/k4Reco/Overlay/components/OverlayTiming.cpp +++ b/k4Reco/Overlay/components/OverlayTiming.cpp @@ -21,8 +21,11 @@ #include "podio/Frame.h" +#include "edm4hep/Constants.h" #include "edm4hep/MutableCaloHitContribution.h" +#include "k4FWCore/MetadataUtils.h" + #include #include @@ -378,4 +381,24 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection& std::move(ocaloHitContribs)); } +StatusCode OverlayTiming::finalize() { + if (m_copyCellIDMetadata) { + for (auto& [input, output] : + {std::make_pair(inputLocations(SIMTRACKERHIT_INDEX_POSITION), outputLocations("OutputSimTrackerHits")), + std::make_pair(inputLocations(SIMCALOHIT_INDEX_POSITION), outputLocations("OutputSimCalorimeterHits"))}) { + for (size_t i = 0; i < input.size(); ++i) { + auto name = input[i]; + auto value = k4FWCore::getParameter(name + "__" + edm4hep::labels::CellIDEncoding, this); + if (value.has_value()) { + k4FWCore::putParameter(output[i] + "__" + edm4hep::labels::CellIDEncoding, value.value(), this); + } else { + warning() << "No metadata found for " << name << endmsg; + } + } + } + } + + return Gaudi::Algorithm::finalize(); +} + DECLARE_COMPONENT(OverlayTiming) diff --git a/k4Reco/Overlay/components/OverlayTiming.h b/k4Reco/Overlay/components/OverlayTiming.h index cfb49cf..a9b3911 100644 --- a/k4Reco/Overlay/components/OverlayTiming.h +++ b/k4Reco/Overlay/components/OverlayTiming.h @@ -94,6 +94,7 @@ struct OverlayTiming : public k4FWCore::MultiTransformer void overlayCollection(std::string collName, const podio::CollectionBase& inColl); virtual StatusCode initialize() final; + virtual StatusCode finalize() final; retType virtual operator()( const edm4hep::EventHeaderCollection& headers, const edm4hep::MCParticleCollection& mcParticles, @@ -140,6 +141,9 @@ struct OverlayTiming : public k4FWCore::MultiTransformer>(), "Time windows for the different collections"}; Gaudi::Property m_allowReusingBackgroundFiles{ this, "AllowReusingBackgroundFiles", false, "If true the same background file can be used for the same event"}; + Gaudi::Property m_copyCellIDMetadata{this, "CopyCellIDMetadata", false, + "If metadata is found in the signal file, copy it to the output file, " + "replacing the old names with the new names"}; // Gaudi::Property m_maxCachedFrames{ // this, "MaxCachedFrames", 0, "Maximum number of frames cached from background files"}; diff --git a/k4Reco/Overlay/options/runOverlayTiming.py b/k4Reco/Overlay/options/runOverlayTiming.py index 27514fb..b4a2172 100644 --- a/k4Reco/Overlay/options/runOverlayTiming.py +++ b/k4Reco/Overlay/options/runOverlayTiming.py @@ -57,6 +57,7 @@ ["background_group2_1.root"], ] overlay.TimeWindows = {"MCParticle": [0, 23.5], "VertexBarrelCollection": [0, 23.5], "VertexEndcapCollection": [0, 23.5], "HCalRingCollection": [0, 23.5]} +overlay.CopyCellIDMetadata = True ApplicationMgr(TopAlg=[overlay], EvtSel="NONE", From 0a92534f6532bebde0c8406a1eb34d318e7d0e2b Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Thu, 17 Oct 2024 13:36:29 +0200 Subject: [PATCH 2/5] Improve warning message --- k4Reco/Overlay/components/OverlayTiming.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k4Reco/Overlay/components/OverlayTiming.cpp b/k4Reco/Overlay/components/OverlayTiming.cpp index 510302c..d4b0ded 100644 --- a/k4Reco/Overlay/components/OverlayTiming.cpp +++ b/k4Reco/Overlay/components/OverlayTiming.cpp @@ -392,7 +392,7 @@ StatusCode OverlayTiming::finalize() { if (value.has_value()) { k4FWCore::putParameter(output[i] + "__" + edm4hep::labels::CellIDEncoding, value.value(), this); } else { - warning() << "No metadata found for " << name << endmsg; + warning() << "No metadata found for " << name << " when copying CellID metadata was requested" << endmsg; } } } From 83eeaf4762d6bd3852c13dcc13cac8b8f7424fc9 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Thu, 17 Oct 2024 14:18:09 +0200 Subject: [PATCH 3/5] Find the locations by name --- k4Reco/Overlay/components/OverlayTiming.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/k4Reco/Overlay/components/OverlayTiming.cpp b/k4Reco/Overlay/components/OverlayTiming.cpp index d4b0ded..48802ed 100644 --- a/k4Reco/Overlay/components/OverlayTiming.cpp +++ b/k4Reco/Overlay/components/OverlayTiming.cpp @@ -384,8 +384,8 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection& StatusCode OverlayTiming::finalize() { if (m_copyCellIDMetadata) { for (auto& [input, output] : - {std::make_pair(inputLocations(SIMTRACKERHIT_INDEX_POSITION), outputLocations("OutputSimTrackerHits")), - std::make_pair(inputLocations(SIMCALOHIT_INDEX_POSITION), outputLocations("OutputSimCalorimeterHits"))}) { + {std::make_pair(inputLocations("SimTrackerHits"), outputLocations("OutputSimTrackerHits")), + std::make_pair(inputLocations("SimCalorimeterHits"), outputLocations("OutputSimCalorimeterHits"))}) { for (size_t i = 0; i < input.size(); ++i) { auto name = input[i]; auto value = k4FWCore::getParameter(name + "__" + edm4hep::labels::CellIDEncoding, this); From 75efadc8a62e9112977d2292ea9169c28640b11f Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Thu, 17 Oct 2024 14:48:27 +0200 Subject: [PATCH 4/5] Use podio::collMetadataParamName and fix a few headers --- k4Reco/Overlay/components/OverlayTiming.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/k4Reco/Overlay/components/OverlayTiming.cpp b/k4Reco/Overlay/components/OverlayTiming.cpp index 48802ed..609c51c 100644 --- a/k4Reco/Overlay/components/OverlayTiming.cpp +++ b/k4Reco/Overlay/components/OverlayTiming.cpp @@ -19,10 +19,14 @@ #include "OverlayTiming.h" #include -#include "podio/Frame.h" +#include "podio/FrameCategories.h" +#include "edm4hep/CaloHitContributionCollection.h" #include "edm4hep/Constants.h" -#include "edm4hep/MutableCaloHitContribution.h" +#include "edm4hep/EventHeaderCollection.h" +#include "edm4hep/MCParticleCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" +#include "edm4hep/SimTrackerHitCollection.h" #include "k4FWCore/MetadataUtils.h" @@ -30,6 +34,8 @@ #include #include +#include +#include template inline float time_of_flight(const T& pos) { // Returns the time of flight to the radius in ns @@ -387,12 +393,13 @@ StatusCode OverlayTiming::finalize() { {std::make_pair(inputLocations("SimTrackerHits"), outputLocations("OutputSimTrackerHits")), std::make_pair(inputLocations("SimCalorimeterHits"), outputLocations("OutputSimCalorimeterHits"))}) { for (size_t i = 0; i < input.size(); ++i) { - auto name = input[i]; - auto value = k4FWCore::getParameter(name + "__" + edm4hep::labels::CellIDEncoding, this); + auto value = k4FWCore::getParameter( + podio::collMetadataParamName(input[i], edm4hep::labels::CellIDEncoding), this); if (value.has_value()) { - k4FWCore::putParameter(output[i] + "__" + edm4hep::labels::CellIDEncoding, value.value(), this); + k4FWCore::putParameter(podio::collMetadataParamName(output[i], edm4hep::labels::CellIDEncoding), + value.value(), this); } else { - warning() << "No metadata found for " << name << " when copying CellID metadata was requested" << endmsg; + warning() << "No metadata found for " << input[i] << " when copying CellID metadata was requested" << endmsg; } } } From 4e0356bb889cc4035a5fdc9288aae61914dbc0db Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Thu, 17 Oct 2024 16:24:31 +0200 Subject: [PATCH 5/5] Add a comment about dropping the original collections --- k4Reco/Overlay/components/OverlayTiming.h | 3 +++ k4Reco/Overlay/options/runOverlayTiming.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/k4Reco/Overlay/components/OverlayTiming.h b/k4Reco/Overlay/components/OverlayTiming.h index a9b3911..01ba76d 100644 --- a/k4Reco/Overlay/components/OverlayTiming.h +++ b/k4Reco/Overlay/components/OverlayTiming.h @@ -48,7 +48,10 @@ #include "Gaudi/Parsers/Factory.h" #include "Gaudi/Property.h" +#include #include +#include +#include struct EventHolder { std::vector> m_fileNames; diff --git a/k4Reco/Overlay/options/runOverlayTiming.py b/k4Reco/Overlay/options/runOverlayTiming.py index b4a2172..e4d6f2c 100644 --- a/k4Reco/Overlay/options/runOverlayTiming.py +++ b/k4Reco/Overlay/options/runOverlayTiming.py @@ -59,6 +59,9 @@ overlay.TimeWindows = {"MCParticle": [0, 23.5], "VertexBarrelCollection": [0, 23.5], "VertexEndcapCollection": [0, 23.5], "HCalRingCollection": [0, 23.5]} overlay.CopyCellIDMetadata = True +# In case the original collections should be dropped +# iosvc.outputCommands = ["drop MCParticles", "drop VertexBarrelCollection"] + ApplicationMgr(TopAlg=[overlay], EvtSel="NONE", EvtMax=10,