-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from association to link (#200)
* Switch from assocation to link Accomodate for the new naming * Switch to non-deprecated function * Add tests for converting links from EDM4hep to LCIO * Remove leftover docstring * Make comparisons a bit easier to read
- Loading branch information
Showing
6 changed files
with
243 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright (c) 2019-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. | ||
# | ||
|
||
from Gaudi.Configuration import INFO, DEBUG | ||
|
||
from Configurables import ( | ||
PodioInput, | ||
k4DataSvc, | ||
ApplicationMgr, | ||
PseudoRecoAlgorithm, | ||
TrivialMCRecoLinker, | ||
MarlinProcessorWrapper, | ||
EDM4hep2LcioTool, | ||
) | ||
|
||
from k4FWCore.parseArgs import parser | ||
|
||
parser.add_argument("--inputfile", help="Input file") | ||
my_args = parser.parse_known_args()[0] | ||
|
||
evtsvc = k4DataSvc("EventDataSvc") | ||
evtsvc.input = my_args.inputfile | ||
|
||
podioInput = PodioInput("InputReader") | ||
podioInput.collections = ["MCParticles"] | ||
podioInput.OutputLevel = INFO | ||
|
||
|
||
PseudoRecoAlg = PseudoRecoAlgorithm( | ||
"PseudoRecoAlgorithm", InputMCs=["MCParticles"], OutputRecos=["PseudoRecoParticles"] | ||
) | ||
|
||
MCRecoLinker = TrivialMCRecoLinker( | ||
InputMCs=["MCParticles"], | ||
InputRecos=["PseudoRecoParticles"], | ||
OutputLinks=["TrivialMCRecoLinks"], | ||
) | ||
|
||
|
||
MarlinMCLinkChecker = MarlinProcessorWrapper( | ||
"MarlinMCRecoLinkChecker", | ||
ProcessorType="MarlinMCRecoLinkChecker", | ||
Parameters={ | ||
"MCRecoLinks": ["TrivialMCRecoLinks"], | ||
"InputMCs": ["MCParticles"], | ||
"InputRecos": ["PseudoRecoParticles"], | ||
}, | ||
) | ||
|
||
mcLinkConverter = EDM4hep2LcioTool("MCLinkConverterToEDM4hep") | ||
mcLinkConverter.convertAll = False | ||
mcLinkConverter.collNameMapping = { | ||
"TrivialMCRecoLinks": "TrivialMCRecoLinks", | ||
"MCParticles": "MCParticles", | ||
"PseudoRecoParticles": "PseudoRecoParticles", | ||
} | ||
mcLinkConverter.OutputLevel = DEBUG | ||
MarlinMCLinkChecker.EDM4hep2LcioTool = mcLinkConverter | ||
|
||
|
||
ApplicationMgr( | ||
TopAlg=[podioInput, PseudoRecoAlg, MCRecoLinker, MarlinMCLinkChecker], | ||
ExtSvc=[evtsvc], | ||
EvtMax=-1, | ||
EvtSel="NONE", | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2019-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 "marlin/Processor.h" | ||
|
||
#include <EVENT/LCCollection.h> | ||
#include <EVENT/LCEvent.h> | ||
#include <EVENT/LCRelation.h> | ||
#include <EVENT/MCParticle.h> | ||
#include <EVENT/ReconstructedParticle.h> | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
|
||
class MarlinMCRecoLinkChecker : public marlin::Processor { | ||
public: | ||
MarlinMCRecoLinkChecker(); | ||
|
||
marlin::Processor* newProcessor() override { return new MarlinMCRecoLinkChecker; } | ||
|
||
void processEvent(LCEvent* evt) override; | ||
|
||
private: | ||
std::string m_mcCollName{}; | ||
std::string m_recoCollName{}; | ||
std::string m_relCollName{}; | ||
}; | ||
|
||
MarlinMCRecoLinkChecker::MarlinMCRecoLinkChecker() : marlin::Processor("MarlinMCRecoLinkChecker") { | ||
registerInputCollection(LCIO::MCPARTICLE, "InputMCs", "Name of the input MCParticle collection", m_mcCollName, | ||
std::string("MCParticles")); | ||
|
||
registerInputCollection(LCIO::RECONSTRUCTEDPARTICLE, "InputRecos", | ||
"Name of the input ReconstructedParticle collection", m_recoCollName, | ||
std::string("PseudoRecoParticles")); | ||
registerInputCollection(LCIO::LCRELATION, "MCRecoLinks", "Name of the input Reco - MC Truth link collection", | ||
m_relCollName, std::string("TrivialMCRecoLinks")); | ||
} | ||
|
||
void MarlinMCRecoLinkChecker::processEvent(LCEvent* evt) { | ||
const auto mcColl = evt->getCollection(m_mcCollName); | ||
const auto recoColl = evt->getCollection(m_recoCollName); | ||
const auto relColl = evt->getCollection(m_relCollName); | ||
|
||
if (relColl->getNumberOfElements() != mcColl->getNumberOfElements()) { | ||
throw std::runtime_error("The LCRelation collection does not have the expected number of elements: (expected " + | ||
std::to_string(mcColl->getNumberOfElements()) + ", actual " + | ||
std::to_string(relColl->getNumberOfElements()) + ")"); | ||
} | ||
|
||
for (size_t i = 0; i < mcColl->getNumberOfElements(); ++i) { | ||
const auto mc = static_cast<EVENT::MCParticle*>(mcColl->getElementAt(i)); | ||
const auto reco = static_cast<EVENT::ReconstructedParticle*>(recoColl->getElementAt(i)); | ||
const auto rel = static_cast<EVENT::LCRelation*>(relColl->getElementAt(i)); | ||
|
||
if (rel->getWeight() != i) { | ||
throw std::runtime_error("Relation " + std::to_string(i) + " does not have the correct weight (expected: " + | ||
std::to_string(i) + ", actual: " + std::to_string(rel->getWeight()) + ")"); | ||
} | ||
|
||
if (rel->getTo() != mc) { | ||
throw std::runtime_error("Relation " + std::to_string(i) + " does not point to the correct MCParticle"); | ||
} | ||
|
||
if (rel->getFrom() != reco) { | ||
throw std::runtime_error("Relation " + std::to_string(i) + " does not point to the correct MCParticle"); | ||
} | ||
} | ||
} | ||
|
||
MarlinMCRecoLinkChecker aMarlinMCRecoLinkChecker{}; |
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,56 @@ | ||
/* | ||
* Copyright (c) 2019-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 <GaudiKernel/ISvcLocator.h> | ||
#include "edm4hep/MCParticleCollection.h" | ||
#include "edm4hep/RecoMCParticleLinkCollection.h" | ||
#include "edm4hep/ReconstructedParticleCollection.h" | ||
|
||
#include "k4FWCore/Transformer.h" | ||
|
||
#include <string> | ||
|
||
struct TrivialMCRecoLinker final | ||
: k4FWCore::Transformer<edm4hep::RecoMCParticleLinkCollection(const edm4hep::MCParticleCollection&, | ||
const edm4hep::ReconstructedParticleCollection&)> { | ||
TrivialMCRecoLinker(const std::string& name, ISvcLocator* svcLoc) | ||
: Transformer(name, svcLoc, | ||
{KeyValues("InputMCs", {"MCParticles"}), KeyValues("InputRecos", {"PseudoRecoParticles"})}, | ||
KeyValues("OutputLinks", {"TrivialMCRecoLinks"})) {} | ||
|
||
edm4hep::RecoMCParticleLinkCollection operator()( | ||
const edm4hep::MCParticleCollection& mcParticles, | ||
const edm4hep::ReconstructedParticleCollection& recoParticles) const override { | ||
auto links = edm4hep::RecoMCParticleLinkCollection{}; | ||
|
||
for (size_t i = 0; i < mcParticles.size(); ++i) { | ||
const auto mc = mcParticles[i]; | ||
const auto reco = recoParticles[i]; | ||
|
||
auto link = links.create(); | ||
link.setFrom(reco); | ||
link.setTo(mc); | ||
link.setWeight(i); | ||
} | ||
|
||
return links; | ||
} | ||
}; | ||
|
||
DECLARE_COMPONENT(TrivialMCRecoLinker) |