Skip to content

Commit

Permalink
Add support for running together with functional algorithms (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Jul 9, 2024
1 parent eeab932 commit af28b7d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 121 deletions.
26 changes: 21 additions & 5 deletions k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,27 @@ void EDM4hep2LcioTool::convertEventHeader(const std::string& e4h_coll_name, lcio
void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::string& lcio_coll_name,
lcio::LCEventImpl* lcio_event, CollectionPairMappings& collection_pairs,
std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections) {
const auto& evtFrame = m_podioDataSvc->getEventFrame();
const auto& metadata = m_podioDataSvc->getMetaDataFrame();
const auto collPtr = evtFrame.get(e4h_coll_name);
if (!collPtr) {
error() << "No collection with name: " << e4h_coll_name << " available for conversion" << endmsg;
const auto& metadata = m_podioDataSvc->getMetaDataFrame();
podio::CollectionBase* collPtr = nullptr;
DataObject* p;
auto sc = m_podioDataSvc->retrieveObject(e4h_coll_name, p);
if (sc.isFailure()) {
throw GaudiException("Collection not found", name(), StatusCode::FAILURE);
}
auto ptr = dynamic_cast<DataWrapperBase*>(p);
if (ptr) {
collPtr = ptr->collectionBase();
}
// When the collection can't be retrieved from the frame
// there is still the possibility that it was generated
// from a functional algorithm
else {
auto ptr = dynamic_cast<AnyDataWrapper<std::shared_ptr<podio::CollectionBase>>*>(p);
if (!ptr) {
throw GaudiException("Collection could not be casted to the expected type", name(), StatusCode::FAILURE);
} else {
collPtr = dynamic_cast<podio::CollectionBase*>(ptr->getData().get());
}
}
const auto fulltype = collPtr->getValueTypeName();

Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
gaudi_add_module(GaudiTestAlgorithms
SOURCES
src/MCRecoLinkChecker.cc
src/PseudoRecoAlgorithm.cc
LINK
Gaudi::GaudiAlgLib
Gaudi::GaudiKernel
Expand All @@ -37,7 +38,7 @@ set(ExternalData_URL_TEMPLATES
# Compile the Marlin test processors into a shared library
find_package(Marlin REQUIRED)

add_library(MarlinTestProcessors SHARED src/PseudoRecoProcessor.cc src/TrivialMCTruthLinkerProcessor.cc)
add_library(MarlinTestProcessors SHARED src/TrivialMCTruthLinkerProcessor.cc)
target_link_libraries(MarlinTestProcessors PUBLIC ${Marlin_LIBRARIES})
target_include_directories(MarlinTestProcessors PUBLIC ${Marlin_INCLUDE_DIRS})

Expand Down
14 changes: 7 additions & 7 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ The test is using the
[`global_converter_maps.sh`](./scripts/global_converter_maps.sh) script which
effectively simply runs the
[`test_global_converter_maps.py`](./gaudi_opts/test_global_converter_maps.py)
options file after downloading some input data. This options file uses two
wrapped MarlinProcessors and one GaudiAlgorithm plus some converters inbetween
them:
options file after downloading some input data. This options file uses one
wrapped MarlinProcessor, one GaudiAlgorithm and one Gaudi Functional algorithm
plus some converters inbetween them:
- `PodioInput` to read the *MCParticles* collection from the input file (in
EDM4hep format)
- [`PseudoRecoProcessor`](./src/PseudoRecoProcessor.cc) creates a reco particle
- [`PseudoRecoAlgorithm`](./src/PseudoRecoAlgorithm.cc) creates a reco particle
for every MC particle in the input collection
- An EDM4hep to LCIO converter converts the input MC particles up front
- An LCIO to EDM4hep converter converts the output reco particles afterwards
- An EDM4hep to LCIO converter converts the input MC particles and the
reconstructed particles that are created by the algorithm
- [`TrivalMCTruthLinkerProcessor`](./src/TrivialMCTruthLinkerProcessor.cc)
creates trivial links from the MC particles to the reco particles.
- An LCIO to EDM4hep converter converts the output collction to EDM4hep
- An LCIO to EDM4hep converter converts the output collection to EDM4hep
- [`MCRecoLinkChecker`](./src/MCRecoLinkChecker.cc) is a Gaudi algorithm that
simply checks whether the MC-reco links are as expected.

Expand Down
31 changes: 12 additions & 19 deletions test/gaudi_opts/test_global_converter_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
EDM4hep2LcioTool,
MCRecoLinkChecker,
ApplicationMgr,
PseudoRecoAlgorithm,
)

evtsvc = k4DataSvc("EventDataSvc")
Expand All @@ -37,35 +38,27 @@
podioInput.collections = ["MCParticles"]
podioInput.OutputLevel = INFO

PseudoRecoProc = MarlinProcessorWrapper("PseudoReco")
PseudoRecoProc.ProcessorType = "PseudoRecoProcessor"
PseudoRecoProc.Parameters = {
"InputMCs": ["MCParticles"],
"OutputRecos": ["PseudoRecoParticles"],
}
PseudoRecoAlg = PseudoRecoAlgorithm("PseudoRecoAlgorithm",
InputMCs=["MCParticles"],
OutputRecos=["PseudoRecoParticles"])

inputConverter = EDM4hep2LcioTool("InputConverter")
inputConverter.convertAll = False
inputConverter.collNameMapping = {"MCParticles": "MCParticles"}
inputConverter.collNameMapping = {"MCParticles": "MCParticles",
"PseudoRecoParticles": "PseudoRecoParticles"
}
inputConverter.OutputLevel = DEBUG

PseudoRecoProc.EDM4hep2LcioTool = inputConverter

pseudoRecConverter = Lcio2EDM4hepTool("PseudoRecoConverter")
pseudoRecConverter.convertAll = False
pseudoRecConverter.collNameMapping = {"PseudoRecoParticles": "PseudoRecoParticles"}
pseudoRecConverter.OutputLevel = DEBUG

PseudoRecoProc.Lcio2EDM4hepTool = pseudoRecConverter

TrivialMCTruthLinkerProc = MarlinProcessorWrapper("TvivialMCTruthLinker")
TrivialMCTruthLinkerProc = MarlinProcessorWrapper("TrivialMCTruthLinker")
TrivialMCTruthLinkerProc.ProcessorType = "TrivialMCTruthLinkerProcessor"
TrivialMCTruthLinkerProc.Parameters = {
"InputMCs": ["MCParticles"],
"InputRecos": ["PseudoRecoParticles"],
"OutputMCRecoLinks": ["TrivialMCRecoLinks"],
}

TrivialMCTruthLinkerProc.EDM4hep2LcioTool = inputConverter

mcTruthConverter = Lcio2EDM4hepTool("TrivialMCTruthLinkerConverter")
mcTruthConverter.convertAll = False
mcTruthConverter.collNameMapping = {"TrivialMCRecoLinks": "TrivialMCRecoLinks"}
Expand All @@ -81,11 +74,11 @@

algList = [
podioInput,
PseudoRecoProc,
PseudoRecoAlg,
TrivialMCTruthLinkerProc,
mcLinkChecker,
]

ApplicationMgr(
TopAlg=algList, EvtSel="NONE", EvtMax=3, ExtSvc=[evtsvc], OutputLevel=INFO
TopAlg=algList, EvtSel="NONE", EvtMax=3, ExtSvc=[evtsvc], OutputLevel=DEBUG
)
47 changes: 47 additions & 0 deletions test/src/PseudoRecoAlgorithm.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 "edm4hep/MCParticleCollection.h"
#include "edm4hep/ReconstructedParticleCollection.h"

#include "k4FWCore/Transformer.h"

#include <string>

struct PseudoRecoAlgorithm final
: k4FWCore::Transformer<edm4hep::ReconstructedParticleCollection(const edm4hep::MCParticleCollection&)> {
PseudoRecoAlgorithm(const std::string& name, ISvcLocator* svcLoc)
: Transformer(name, svcLoc, {KeyValues("InputMCs", {"MCParticles"})},
{KeyValues("OutputRecos", {"PseudoRecoParticles"})}) {}

edm4hep::ReconstructedParticleCollection operator()(const edm4hep::MCParticleCollection& input) const override {
auto coll_out = edm4hep::ReconstructedParticleCollection();
for (const auto& particle : input) {
auto new_particle = coll_out.create();
new_particle.setCharge(particle.getCharge());
new_particle.setMomentum({static_cast<float>(particle.getMomentum()[0]),
static_cast<float>(particle.getMomentum()[1]),
static_cast<float>(particle.getMomentum()[2])});
new_particle.setEnergy(particle.getEnergy());
}
return coll_out;
}
};

DECLARE_COMPONENT(PseudoRecoAlgorithm)
52 changes: 0 additions & 52 deletions test/src/PseudoRecoProcessor.cc

This file was deleted.

37 changes: 0 additions & 37 deletions test/src/PseudoRecoProcessor.h

This file was deleted.

0 comments on commit af28b7d

Please sign in to comment.