Skip to content
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 support for running with IOSvc #216

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5967d0b
Add support for running with IOSvc
jmcarcell Jan 14, 2025
80e92fc
Fix rebase
jmcarcell Jan 14, 2025
b6cee81
Use the new overload of convertObjectParameters in k4EDM4hep2LcioConv
jmcarcell Jan 17, 2025
3c45aeb
Remove unused header
jmcarcell Jan 17, 2025
add8c80
Use the metadata service to put the PID parameters
jmcarcell Jan 22, 2025
f7c9223
Don't store the metadata frame in an optional
jmcarcell Jan 22, 2025
2f2504e
Fix pre-commit
jmcarcell Jan 22, 2025
4897a60
Use the metadata svc in EDM4hep2LCIO
jmcarcell Jan 22, 2025
27baccb
Fix pre-commit
jmcarcell Jan 22, 2025
8cbbdc6
Only complain about the metadata service when using IOSvc
jmcarcell Jan 24, 2025
6462700
Add shared utilities to retrieve collections from the store
jmcarcell Jan 24, 2025
7b41e44
Fix
jmcarcell Jan 24, 2025
ce365c8
Fix commands
jmcarcell Jan 24, 2025
7764db1
Fix pre-commit
jmcarcell Jan 24, 2025
b4794a7
Remove TODO and add a comment
jmcarcell Jan 24, 2025
5e85df2
Address comments in the PR
jmcarcell Jan 30, 2025
db7165f
Fix pre-commit
jmcarcell Jan 30, 2025
38b167e
Use a different Gaudi output file with IOSvc and without
jmcarcell Jan 30, 2025
7f7e4de
Add default arguments for clicRec_e4h_input
jmcarcell Jan 30, 2025
62689ed
Fix parameter conversion
jmcarcell Jan 30, 2025
9ac303b
Fix pre-commit and add a new test
jmcarcell Jan 30, 2025
1940dd2
Fix syntax
jmcarcell Jan 30, 2025
cf18701
Add comments, improve warning and add a throw
jmcarcell Jan 30, 2025
f62a519
Improve warning
jmcarcell Jan 30, 2025
a1c7d66
Add a new test with a functional taking input from a processor
jmcarcell Jan 30, 2025
0030ee8
Add missing files
jmcarcell Jan 31, 2025
61cbb2e
Fix pre-commit
jmcarcell Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions k4MarlinWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ target_include_directories(MarlinWrapper PUBLIC
${LCIO_INCLUDE_DIRS}
)

# k4MarlinWrapperUtils
gaudi_add_library(k4MarlinWrapperUtils SHARED
andresailer marked this conversation as resolved.
Show resolved Hide resolved
SOURCES
src/components/StoreUtils.cpp
LINK k4FWCore::k4FWCore
)

# EDM4hep2lcio
gaudi_add_module(EDM4hep2Lcio
SOURCES
Expand All @@ -64,6 +71,7 @@ gaudi_add_module(EDM4hep2Lcio
${LCIO_LIBRARIES}
${Marlin_LIBRARIES}
EDM4HEP::edm4hep
k4MarlinWrapperUtils
)

target_include_directories(EDM4hep2Lcio PUBLIC
Expand All @@ -80,6 +88,7 @@ gaudi_add_module(Lcio2EDM4hep
EDM4HEP::edm4hep
k4FWCore::k4FWCore
k4EDM4hep2LcioConv::k4EDM4hep2LcioConv
k4MarlinWrapperUtils
)

target_include_directories(Lcio2EDM4hep PUBLIC
Expand Down
71 changes: 43 additions & 28 deletions k4MarlinWrapper/examples/clicRec_e4h_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@

import os

from Gaudi.Configuration import *
from Gaudi.Configuration import DEBUG, WARNING

from Configurables import LcioEvent, MarlinProcessorWrapper
from k4MarlinWrapper.parseConstants import *
from Configurables import MarlinProcessorWrapper
from k4MarlinWrapper.parseConstants import parseConstants

from Configurables import Lcio2EDM4hepTool, EDM4hep2LcioTool
from Configurables import k4DataSvc, PodioInput, PodioOutput, EventDataSvc

from k4FWCore import ApplicationMgr, IOSvc
from k4FWCore.parseArgs import parser

algList = []

Expand All @@ -33,22 +39,40 @@

parseConstants(CONSTANTS)


# For converters
from Configurables import ToolSvc, Lcio2EDM4hepTool, EDM4hep2LcioTool


from Configurables import k4DataSvc, PodioInput

evtsvc = k4DataSvc("EventDataSvc")
evtsvc.input = os.path.join(
"$TEST_DIR/inputFiles/", os.environ.get("INPUTFILE", "ttbar_edm4hep_frame.root")
parser.add_argument(
"--iosvc", action="store_true", default=False, help="Use IOSvc instead of PodioDataSvc"
)
parser.add_argument(
"--rec-output", default="Output_REC_e4h_input.slcio", help="Output file name for the REC file"
)
parser.add_argument(
"--dst-output", default="Output_DST_e4h_input.slcio", help="Output file name for the DST file"
)
parser.add_argument(
"--gaudi-output", default="my_output.root", help="Output file name for the Gaudi file"
)

args = parser.parse_known_args()[0]

inp = PodioInput("InputReader")
inp.OutputLevel = DEBUG
if args.iosvc:
evtsvc = EventDataSvc("EventDataSvc")
iosvc = IOSvc()
iosvc.Input = os.path.join(
"$TEST_DIR/inputFiles/", os.environ.get("INPUTFILE", "ttbar_edm4hep_frame.root")
)
iosvc.Output = args.gaudi_output
iosvc.outputCommands = ["keep *", "drop RefinedVertexJets_PID_RefinedVertex"]
else:
evtsvc = k4DataSvc("EventDataSvc")
evtsvc.input = os.path.join(
"$TEST_DIR/inputFiles/", os.environ.get("INPUTFILE", "ttbar_edm4hep_frame.root")
)

inp = PodioInput("InputReader")
inp.OutputLevel = DEBUG

out = PodioOutput("PodioOutput", filename=args.gaudi_output)
out.outputCommands = ["keep *", "drop RefinedVertexJets_PID_RefinedVertex"]

MyAIDAProcessor = MarlinProcessorWrapper("MyAIDAProcessor")
MyAIDAProcessor.OutputLevel = WARNING
Expand Down Expand Up @@ -1167,7 +1191,7 @@
"DropCollectionTypes": [],
"FullSubsetCollections": ["EfficientMCParticles", "InefficientMCParticles"],
"KeepCollectionNames": [],
"LCIOOutputFile": ["Output_REC_e4h_input.slcio"],
"LCIOOutputFile": [args.rec_output],
"LCIOWriteMode": ["WRITE_NEW"],
}

Expand Down Expand Up @@ -1233,7 +1257,7 @@
"RefinedVertices",
"RefinedVertices_RP",
],
"LCIOOutputFile": ["Output_DST_e4h_input.slcio"],
"LCIOOutputFile": [args.dst_output],
"LCIOWriteMode": ["WRITE_NEW"],
}

Expand Down Expand Up @@ -2428,15 +2452,6 @@
"UseMCP": ["0"],
}


# Write output to EDM4hep
from Configurables import PodioOutput

out = PodioOutput("PodioOutput", filename="my_output.root")
out.outputCommands = ["keep *", "drop RefinedVertexJets_PID_RefinedVertex"]


algList.append(inp)
algList.append(MyAIDAProcessor)
algList.append(EventNumber)
algList.append(InitDD4hep)
Expand Down Expand Up @@ -2486,8 +2501,8 @@
# # algList.append(VertexFinderUnconstrained) # Config.VertexUnconstrainedON
algList.append(Output_REC)
algList.append(Output_DST)
algList.append(out)

from Configurables import ApplicationMgr
if not args.iosvc:
algList = [inp] + algList + [out]

ApplicationMgr(TopAlg=algList, EvtSel="NONE", EvtMax=3, ExtSvc=[evtsvc], OutputLevel=WARNING)
9 changes: 8 additions & 1 deletion k4MarlinWrapper/k4MarlinWrapper/converters/EDM4hep2Lcio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <vector>

class PodioDataSvc;
class IDataProviderSvc;
class IMetadataSvc;

template <typename K, typename V> using ObjMapT = k4EDM4hep2LcioConv::VecMapT<K, V>;

Expand Down Expand Up @@ -62,8 +64,13 @@ class EDM4hep2LcioTool : public AlgTool, virtual public IEDMConverter {
Gaudi::Property<std::map<std::string, std::string>> m_collNames{this, "collNameMapping", {}};
Gaudi::Property<bool> m_convertAll{this, "convertAll", true};

PodioDataSvc* m_podioDataSvc;
PodioDataSvc* m_podioDataSvc;
// EventDataSvc that is used together with IOSvc
ServiceHandle<IDataProviderSvc> m_eventDataSvc;
// Metadata service from k4FWCore that is used together with IOSvc
SmartIF<IMetadataSvc> m_metadataSvc;
std::vector<std::string> m_collectionNames;
std::map<uint32_t, std::string> m_idToName;

void convertTracks(TrackMap& tracks_vec, const std::string& e4h_coll_name, const std::string& lcio_coll_name,
lcio::LCEventImpl* lcio_event);
Expand Down
5 changes: 4 additions & 1 deletion k4MarlinWrapper/k4MarlinWrapper/converters/Lcio2EDM4hep.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <Gaudi/Property.h>
#include <GaudiKernel/AlgTool.h>

#include "k4FWCore/IMetadataSvc.h"

#include "k4MarlinWrapper/converters/IEDMConverter.h"

#include <lcio.h>
Expand Down Expand Up @@ -58,7 +60,8 @@ class Lcio2EDM4hepTool : public AlgTool, virtual public IEDMConverter {
Gaudi::Property<std::map<std::string, std::string>> m_collNames{this, "collNameMapping", {}};
Gaudi::Property<bool> m_convertAll{this, "convertAll", true};

ServiceHandle<IDataProviderSvc> m_eds;
ServiceHandle<IDataProviderSvc> m_eventDataSvc;
SmartIF<IMetadataSvc> m_metadataSvc;
PodioDataSvc* m_podioDataSvc;

// **********************************
Expand Down
81 changes: 68 additions & 13 deletions k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@
* limitations under the License.
*/
#include "k4MarlinWrapper/converters/EDM4hep2Lcio.h"
#include <GaudiKernel/StatusCode.h>
#include "GlobalConvertedObjectsMap.h"
#include "StoreUtils.h"

#include "UTIL/PIDHandler.h"

#include "edm4hep/Constants.h"
#include "edm4hep/utils/ParticleIDUtils.h"

#include "k4FWCore/DataHandle.h"
#include "k4FWCore/FunctionalUtils.h"
#include "k4FWCore/MetaDataHandle.h"
#include "k4FWCore/PodioDataSvc.h"

#include "GaudiKernel/AnyDataWrapper.h"
#include "GaudiKernel/IDataManagerSvc.h"
#include "GaudiKernel/IDataProviderSvc.h"

#include <functional>
#include <memory>

DECLARE_COMPONENT(EDM4hep2LcioTool);
Expand Down Expand Up @@ -55,11 +64,17 @@ EDM4hep2LcioTool::EDM4hep2LcioTool(const std::string& type, const std::string& n
}

StatusCode EDM4hep2LcioTool::initialize() {
StatusCode sc = m_eventDataSvc.retrieve();
StatusCode sc = m_eventDataSvc.retrieve();
if (sc == StatusCode::FAILURE) {
error() << "Could not retrieve the EventDataSvc;" << endmsg;
return StatusCode::FAILURE;
}

m_podioDataSvc = dynamic_cast<PodioDataSvc*>(m_eventDataSvc.get());

if (sc == StatusCode::FAILURE) {
error() << "Error retrieving Event Data Service" << endmsg;
m_metadataSvc = service("MetadataSvc", false);
if (!m_podioDataSvc && !m_metadataSvc) {
error() << "Could not retrieve MetadataSvc" << endmsg;
return StatusCode::FAILURE;
}

Expand Down Expand Up @@ -277,7 +292,7 @@ void EDM4hep2LcioTool::convertEventHeader(const std::string& e4h_coll_name, lcio

podio::CollectionBase* EDM4hep2LcioTool::getEDM4hepCollection(const std::string& collName) const {
DataObject* p;
auto sc = m_podioDataSvc->retrieveObject(collName, p);
auto sc = m_eventDataSvc->retrieveObject(collName, p);
if (sc.isFailure()) {
throw GaudiException("Collection not found", name(), StatusCode::FAILURE);
}
Expand Down Expand Up @@ -340,8 +355,14 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
} else if (fulltype == "edm4hep::EventHeader") {
convertEventHeader(e4h_coll_name, lcio_event);
} else if (fulltype == "edm4hep::ParticleID") {
pidCollections.emplace_back(e4h_coll_name, static_cast<const edm4hep::ParticleIDCollection*>(collPtr),
edm4hep::utils::PIDHandler::getAlgoInfo(metadata, e4h_coll_name));
std::optional<edm4hep::utils::ParticleIDMeta> pidInfo;
if (m_podioDataSvc) {
andresailer marked this conversation as resolved.
Show resolved Hide resolved
pidInfo = edm4hep::utils::PIDHandler::getAlgoInfo(metadata, e4h_coll_name);
} else {
pidInfo = m_metadataSvc->get<edm4hep::utils::ParticleIDMeta>(e4h_coll_name);
}

pidCollections.emplace_back(e4h_coll_name, static_cast<const edm4hep::ParticleIDCollection*>(collPtr), pidInfo);
} else if (fulltype == "edm4hep::RecDqDx") {
dQdxCollections.emplace_back(e4h_coll_name, static_cast<const edm4hep::RecDqdxCollection*>(collPtr));
}
Expand All @@ -362,8 +383,16 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
// Parse property parameters and convert the indicated collections.
// Use the collection names in the parameters to read and write them
StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
const auto& edmEvent = m_podioDataSvc->getEventFrame();
const auto collections = edmEvent.getAvailableCollections();
std::optional<std::reference_wrapper<const podio::Frame>> edmEvent;
if (m_collectionNames.empty() && m_podioDataSvc) {
edmEvent = m_podioDataSvc->getEventFrame();
m_collectionNames = edmEvent.value().get().getAvailableCollections();
} else if (m_collectionNames.empty()) {
std::optional<std::map<uint32_t, std::string>> idToNameOpt(std::move(m_idToName));
auto collections = getAvailableCollectionsFromStore(this, idToNameOpt);
m_idToName = std::move(idToNameOpt.value());
m_collectionNames.insert(m_collectionNames.end(), collections.begin(), collections.end());
}
// Start off with the pre-defined collection name mappings
auto collsToConvert{m_collNames.value()};
// We *always* want to convert the EventHeader
Expand All @@ -372,7 +401,7 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
info() << "Converting all collections from EDM4hep to LCIO" << endmsg;
// And simply add the rest, exploiting the fact that emplace will not
// replace existing entries with the same key
for (const auto& name : collections) {
for (const auto& name : m_collectionNames) {
collsToConvert.emplace(name, name);
}
}
Expand All @@ -399,13 +428,39 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
}
debug() << "Event: " << lcio_event->getEventNumber() << " Run: " << lcio_event->getRunNumber() << endmsg;

// Deal with
EDM4hep2LCIOConv::sortParticleIDs(pidCollections);
if (!m_podioDataSvc) {
DataObject* p;
StatusCode code = m_eventDataSvc->retrieveObject("/Event" + k4FWCore::frameLocation, p);
if (code.isSuccess()) {
auto* frame = dynamic_cast<AnyDataWrapper<podio::Frame>*>(p);
edmEvent = std::cref(frame->getData());
} else {
auto frame = podio::Frame{};
edmEvent = frame;
jmcarcell marked this conversation as resolved.
Show resolved Hide resolved
}
}
for (const auto& pidCollMeta : pidCollections) {
const auto algoId = attachParticleIDMetaData(lcio_event, edmEvent, pidCollMeta);
auto algoId = attachParticleIDMetaData(lcio_event, edmEvent.value(), pidCollMeta);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this also needs a similar treatment as convertObjectParameters in k4EDM4hep2LcioConv? That would make it possible to simplify the code below a bit, but I am not sure if it's worth the effort.

if (!algoId.has_value()) {
warning() << "Could not determine algorithm type for ParticleID collection " << pidCollMeta.name
<< " for setting consistent metadata" << endmsg;
// Now go over the collections that have been produced in a functional algorithm (if any)
bool found = false;
if (!m_podioDataSvc) {
const auto id = (*pidCollMeta.coll)[0].getParticle().id().collectionID;
if (auto it = m_idToName.find(id); it != m_idToName.end()) {
auto name = it->second;
if (pidCollMeta.metadata.has_value()) {
UTIL::PIDHandler pidHandler(lcio_event->getCollection(name));
algoId =
pidHandler.addAlgorithm(pidCollMeta.metadata.value().algoName, pidCollMeta.metadata.value().paramNames);
found = true;
}
}
}
if (!found) {
warning() << "Could not determine algorithm type for ParticleID collection " << pidCollMeta.name
<< " for setting consistent metadata" << endmsg;
}
}
convertParticleIDs(collection_pairs.particleIDs, pidCollMeta.name, algoId.value_or(-1));
}
Expand Down
Loading
Loading