-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the possibility to set edm4hep::utils::ParticleIDMeta
via the IMetadataSvc
#273
Open
tmadlener
wants to merge
11
commits into
main
Choose a base branch
from
add-pid-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
74a1456
Add overloads for handling ParticleIDMeta
tmadlener 255c581
Add a producer and a consumer for PID with metadata
tmadlener 0f660eb
Add a test sequence for PID with metadata
tmadlener 8b45d86
Fix a few minor slips
tmadlener c3cb331
Add a check for checking the standalone output
tmadlener 60c885a
Move configuration into global variables
tmadlener 3803077
Make test functions consistent and check specific values
tmadlener e4a1ff4
Add license header
tmadlener cc8a2af
Fix unrelated test failures
tmadlener 50d288a
Find fmtlib in cmake if we build the tests
tmadlener 842a5d5
Find fmtlib unconditionally
tmadlener File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include <edm4hep/ReconstructedParticleCollection.h> | ||
#include <edm4hep/utils/ParticleIDUtils.h> | ||
|
||
#include <podio/Reader.h> | ||
|
||
#include <fmt/format.h> | ||
#include <fmt/ranges.h> | ||
|
||
#include <algorithm> | ||
|
||
bool checkPIDForAlgo(const edm4hep::utils::PIDHandler& pidHandler, const edm4hep::ReconstructedParticle& reco, | ||
const edm4hep::utils::ParticleIDMeta& pidMeta, const int paramIndex) { | ||
auto maybePID = pidHandler.getPID(reco, pidMeta.algoType()); | ||
if (!maybePID) { | ||
fmt::print("Could not retrieve the {} PID object for reco particle {}", pidMeta.algoName, reco.id().index); | ||
return false; | ||
} | ||
auto pid = maybePID.value(); | ||
auto paramVal = pid.getParameters()[paramIndex]; | ||
|
||
// As set in the producer | ||
if (paramVal != paramIndex * 0.5f) { | ||
fmt::print("Could not retrieve the correct parameter value for param {} (expected {}, actual {})", | ||
pidMeta.paramNames[paramIndex], paramIndex * 0.5f, paramVal); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool checkAlgoMetadata(const edm4hep::utils::ParticleIDMeta& pidMeta, const std::string& algoName, | ||
const std::vector<std::string>& paramNames) { | ||
if (pidMeta.algoName != algoName) { | ||
fmt::print( | ||
jmcarcell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"The PID algorithm name from metadata does not match the expected one from the properties: (expected {}, " | ||
"actual {})\n", | ||
algoName, pidMeta.algoName); | ||
return false; | ||
} | ||
|
||
if (!std::ranges::equal(pidMeta.paramNames, paramNames)) { | ||
fmt::print( | ||
"The PID parameter names retrieved from metadata does not match the expected ones from the properties: " | ||
"(expected {}, actual {})\n", | ||
paramNames, pidMeta.paramNames); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// Test configuration (this needs to match the settings in | ||
// options/ExampleParticleIDMetadata.py) | ||
constexpr auto pidCollectionName1 = "RecoParticlesPIDs_1"; | ||
constexpr auto pidCollectionName2 = "RecoParticlesPIDs_2"; | ||
constexpr auto pidAlgo1 = "PIDAlgo1"; | ||
constexpr auto pidAlgo2 = "PIDAlgo2"; | ||
constexpr auto pidParam1 = "single_param"; | ||
constexpr auto pidParam2 = "param_2"; | ||
const std::vector<std::string> paramNames1 = {"single_param"}; | ||
const std::vector<std::string> paramNames2 = {"param_1", "param_2", "param_3"}; | ||
|
||
int main(int, char* argv[]) { | ||
auto reader = podio::makeReader(argv[1]); | ||
const auto metadata = reader.readFrame(podio::Category::Metadata, 0); | ||
|
||
const auto pidMeta1 = edm4hep::utils::PIDHandler::getAlgoInfo(metadata, pidCollectionName1).value(); | ||
const auto pidMeta2 = edm4hep::utils::PIDHandler::getAlgoInfo(metadata, pidCollectionName2).value(); | ||
|
||
if (!checkAlgoMetadata(pidMeta1, pidAlgo1, paramNames1) || !checkAlgoMetadata(pidMeta2, pidAlgo2, paramNames2)) { | ||
return 1; | ||
} | ||
|
||
const auto paramIndex1 = edm4hep::utils::getParamIndex(pidMeta1, pidParam1).value_or(-1); | ||
const auto paramIndex2 = edm4hep::utils::getParamIndex(pidMeta2, pidParam2).value_or(-1); | ||
if (paramIndex1 == -1 || paramIndex2 == -1) { | ||
fmt::print("Could not get a parameter index for '{}' (got {}) or '{}' (got {})\n", pidParam1, paramIndex1, | ||
pidParam2, paramIndex2); | ||
} | ||
|
||
const auto event = reader.readEvent(0); | ||
const auto pidHandler = edm4hep::utils::PIDHandler::from(event, metadata); | ||
|
||
const auto& recos = event.get<edm4hep::ReconstructedParticleCollection>("RecoParticles"); | ||
for (const auto r : recos) { | ||
auto pids = pidHandler.getPIDs(r); | ||
if (pids.size() != 2) { | ||
fmt::print("Failed to retrieve the two expected ParticlID objects related to reco particle {}", r.id().index); | ||
return 1; | ||
} | ||
|
||
if (!checkPIDForAlgo(pidHandler, r, pidMeta1, paramIndex1) || | ||
!checkPIDForAlgo(pidHandler, r, pidMeta2, paramIndex2)) { | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be good to have a
find_package(fmt)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good idea. I was relying on
find_package(Gaudi)
bringing it in. Should I put it into the top levelCMakeLists.txt
or in the one intests
? Currently only the tests seem to be using fmtlib, but that might change(?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well with #275 it will change, so it may be at the top level anyway.