From e8b92c450c0b2a7a4cbf681bfb5b7a502c97a446 Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Fri, 14 Jul 2023 13:35:34 +0200 Subject: [PATCH] Replacing legacy Podio service with the frame Podio service --- .../DetComponents/src/RedoSegmentation.cpp | 23 +----- Detector/DetComponents/src/RedoSegmentation.h | 33 ++++---- SimG4Components/src/SimG4SaveCalHits.cpp | 80 +++++++++++------- SimG4Components/src/SimG4SaveCalHits.h | 35 ++++---- SimG4Components/src/SimG4SaveTrackerHits.cpp | 81 +++++++++++-------- SimG4Components/src/SimG4SaveTrackerHits.h | 38 +++++---- SimG4Components/tests/options/xAngleBoost.py | 10 +-- 7 files changed, 170 insertions(+), 130 deletions(-) diff --git a/Detector/DetComponents/src/RedoSegmentation.cpp b/Detector/DetComponents/src/RedoSegmentation.cpp index d795e2e..a8d1a3d 100644 --- a/Detector/DetComponents/src/RedoSegmentation.cpp +++ b/Detector/DetComponents/src/RedoSegmentation.cpp @@ -1,19 +1,11 @@ #include "RedoSegmentation.h" -// FCCSW -#include "k4Interface/IGeoSvc.h" - -// datamodel -#include "edm4hep/CalorimeterHitCollection.h" -#include "edm4hep/SimCalorimeterHitCollection.h" - // DD4hep #include "DD4hep/Detector.h" -#include "DDSegmentation/Segmentation.h" DECLARE_COMPONENT(RedoSegmentation) -RedoSegmentation::RedoSegmentation(const std::string& aName, ISvcLocator* aSvcLoc) : GaudiAlgorithm(aName, aSvcLoc), m_geoSvc("GeoSvc", aName), m_eventDataSvc("EventDataSvc", "RedoSegmentation") { +RedoSegmentation::RedoSegmentation(const std::string& aName, ISvcLocator* aSvcLoc) : GaudiAlgorithm(aName, aSvcLoc), m_geoSvc("GeoSvc", aName) { declareProperty("inhits", m_inHits, "Hit collection with old segmentation (input)"); declareProperty("outhits", m_outHits, "Hit collection with modified segmentation (output)"); } @@ -22,7 +14,7 @@ RedoSegmentation::~RedoSegmentation() {} StatusCode RedoSegmentation::initialize() { if (GaudiAlgorithm::initialize().isFailure()) return StatusCode::FAILURE; - + if (!m_geoSvc) { error() << "Unable to locate Geometry Service. " << "Make sure you have GeoSvc and SimSvc in the right order in the configuration." << endmsg; @@ -39,12 +31,6 @@ StatusCode RedoSegmentation::initialize() { } // Take readout, bitfield from GeoSvc m_oldDecoder = m_geoSvc->lcdd()->readout(m_oldReadoutName).idSpec().decoder(); - StatusCode sc_dataSvc = m_eventDataSvc.retrieve(); - m_podioDataSvc = dynamic_cast(m_eventDataSvc.get()); - if (sc_dataSvc == StatusCode::FAILURE) { - error() << "Error retrieving Event Data Service" << endmsg; - return StatusCode::FAILURE; - } // segmentation identifiers to be overwritten if (m_oldIdentifiers.size() == 0) { // it is not an error, maybe no segmentation was used previously @@ -77,6 +63,7 @@ StatusCode RedoSegmentation::initialize() { info() << "Old bitfield:\t" << m_oldDecoder->fieldDescription() << endmsg; info() << "New bitfield:\t" << m_segmentation->decoder()->fieldDescription() << endmsg; info() << "New segmentation is of type:\t" << m_segmentation->type() << endmsg; + m_outHitsCellIDEncoding.put(m_segmentation->decoder()->fieldDescription()); return StatusCode::SUCCESS; } @@ -113,9 +100,7 @@ StatusCode RedoSegmentation::execute() { debugIter++; } } - // Define the metadata - auto& coll_md = m_podioDataSvc->getProvider().getCollectionMetaData(m_outHits.get()->getID()); - coll_md.setValue("CellIDEncodingString", m_segmentation->decoder()->fieldDescription()); + return StatusCode::SUCCESS; } diff --git a/Detector/DetComponents/src/RedoSegmentation.h b/Detector/DetComponents/src/RedoSegmentation.h index 994576f..e22d833 100644 --- a/Detector/DetComponents/src/RedoSegmentation.h +++ b/Detector/DetComponents/src/RedoSegmentation.h @@ -5,23 +5,18 @@ #include "GaudiAlg/GaudiAlgorithm.h" #include "GaudiKernel/ToolHandle.h" -// FCCSW +// k4FWCore #include "k4FWCore/DataHandle.h" -class IGeoSvc; +#include "k4FWCore/MetaDataHandle.h" +#include "k4Interface/IGeoSvc.h" // DD4hep #include "DD4hep/Readout.h" -namespace dd4hep { -namespace DDSegmentation { -class Segmentation; -} -} +#include "DD4hep/Segmentations.h" -// datamodel -namespace edm4hep { -class CalorimeterHitCollection; -class SimCalorimeterHitCollection; -} +// EDM4hep +#include "edm4hep/CalorimeterHitCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" /** @class RedoSegmentation Detector/DetComponents/src/RedoSegmentation.h RedoSegmentation.h * @@ -63,12 +58,18 @@ class RedoSegmentation : public GaudiAlgorithm { uint64_t volumeID(uint64_t aCellId) const; /// Pointer to the geometry service ServiceHandle m_geoSvc; - PodioLegacyDataSvc* m_podioDataSvc; - ServiceHandle m_eventDataSvc; /// Handle for the EDM positioned hits to be read - DataHandle m_inHits{"hits/caloInHits", Gaudi::DataHandle::Reader, this}; + DataHandle m_inHits{ + "hits/caloInHits", Gaudi::DataHandle::Reader, this}; + /// Handle for the input hits cell id encoding + MetaDataHandle m_inHitsCellIDEncoding{ + m_inHits,"CellIDEncodingString", Gaudi::DataHandle::Reader}; /// Handle for the EDM hits to be written - DataHandle m_outHits{"hits/caloOutHits", Gaudi::DataHandle::Writer, this}; + DataHandle m_outHits{ + "hits/caloOutHits", Gaudi::DataHandle::Writer, this}; + /// Handle for the output hits cell id encoding + MetaDataHandle m_outHitsCellIDEncoding{ + m_outHits,"CellIDEncodingString", Gaudi::DataHandle::Writer}; /// New segmentation dd4hep::DDSegmentation::Segmentation* m_segmentation; /// Name of the detector readout used in simulation diff --git a/SimG4Components/src/SimG4SaveCalHits.cpp b/SimG4Components/src/SimG4SaveCalHits.cpp index 9a87482..408a5aa 100644 --- a/SimG4Components/src/SimG4SaveCalHits.cpp +++ b/SimG4Components/src/SimG4SaveCalHits.cpp @@ -1,16 +1,12 @@ #include "SimG4SaveCalHits.h" -// FCCSW +// k4SimGeant4 #include "SimG4Common/Geant4CaloHit.h" -#include "SimG4Interface/IGeoSvc.h" #include "SimG4Common/Units.h" // Geant4 #include "G4Event.hh" -// datamodel -#include "edm4hep/SimCalorimeterHitCollection.h" - // DD4hep #include "DD4hep/Detector.h" #include "DD4hep/Segmentations.h" @@ -18,8 +14,10 @@ DECLARE_COMPONENT(SimG4SaveCalHits) -SimG4SaveCalHits::SimG4SaveCalHits(const std::string& aType, const std::string& aName, const IInterface* aParent) - : GaudiTool(aType, aName, aParent), m_geoSvc("GeoSvc", aName), m_eventDataSvc("EventDataSvc", "SimG4SaveCalHits") { +SimG4SaveCalHits::SimG4SaveCalHits(const std::string& aType, + const std::string& aName, + const IInterface* aParent) : + GaudiTool(aType, aName, aParent), m_geoSvc("GeoSvc", aName) { declareInterface(this); declareProperty("CaloHits", m_caloHits, "Handle for calo hits"); declareProperty("GeoSvc", m_geoSvc); @@ -31,29 +29,62 @@ StatusCode SimG4SaveCalHits::initialize() { if (GaudiTool::initialize().isFailure()) { return StatusCode::FAILURE; } + if (!m_geoSvc) { error() << "Unable to locate Geometry Service. " << "Make sure you have GeoSvc and SimSvc in the right order in the configuration." << endmsg; return StatusCode::FAILURE; } - auto lcdd = m_geoSvc->lcdd(); - auto allReadouts = lcdd->readouts(); - for (auto& readoutName : m_readoutNames) { - if (allReadouts.find(readoutName) == allReadouts.end()) { - error() << "Readout " << readoutName << " not found! Please check tool configuration." << endmsg; - return StatusCode::FAILURE; - } else { - debug() << "Hits will be saved to EDM from the collection " << readoutName << endmsg; + + if (m_readoutName.empty() && m_readoutNames.empty()) { + error() << "No readout name provided. Exiting..." << endmsg; + return StatusCode::FAILURE; + } + + if (!m_readoutName.empty() && !m_readoutNames.empty()) { + error() << "Readout name provided through \"readoutName\" parameter, " + << "but also through deprecated \"readoutNames\" vector " + << "parameter." << endmsg; + error() << "Please use only the \"readoutName\" parameter. Exiting..." + << endmsg; + return StatusCode::FAILURE; + } + + if (!m_readoutNames.empty()) { + warning() << "Providing multiple readout names deprecated." << endmsg; + warning() << "Please use \"readoutName\" parameter instead." << endmsg; + + if (m_readoutNames.size() > 1) { + error() << "More than one readout name provided. Exiting..." << endmsg; } + + m_readoutName = m_readoutNames[0]; } - StatusCode sc = m_eventDataSvc.retrieve(); - m_podioDataSvc = dynamic_cast(m_eventDataSvc.get()); - if (sc == StatusCode::FAILURE) { - error() << "Error retrieving Event Data Service" << endmsg; + if (m_readoutName.empty()) { + error() << "No readout name provided. Exiting..." << endmsg; return StatusCode::FAILURE; } + auto lcdd = m_geoSvc->lcdd(); + auto allReadouts = lcdd->readouts(); + if (allReadouts.find(m_readoutName) == allReadouts.end()) { + error() << "Readout " << m_readoutName << " not found! " + << "Please check tool configuration. Exiting..." << endmsg; + return StatusCode::FAILURE; + } else { + info() << "Hits from readout \"" << m_readoutName.value() + << "\" will be saved in the collection \"" + << m_caloHits.objKey() << "\"." << endmsg; + } + + // Add CellID encoding string to hit collection metadata + auto idspec = lcdd->idSpecification(m_readoutName); + auto field_str = idspec.fieldDescription(); + m_cellIDEncoding.put(field_str); + debug() << "Storing cell ID encoding string: \"" << field_str << "\"." + << endmsg; + return StatusCode::SUCCESS; } @@ -67,15 +98,7 @@ StatusCode SimG4SaveCalHits::saveOutput(const G4Event& aEvent) { auto edmHits = m_caloHits.createAndPut(); for (int iter_coll = 0; iter_coll < collections->GetNumberOfCollections(); iter_coll++) { collect = collections->GetHC(iter_coll); - if (std::find(m_readoutNames.begin(), m_readoutNames.end(), collect->GetName()) != m_readoutNames.end()) { - // Add CellID encoding string to collection metadata - auto lcdd = m_geoSvc->lcdd(); - auto allReadouts = lcdd->readouts(); - auto idspec = lcdd->idSpecification(collect->GetName()); - auto field_str = idspec.fieldDescription(); - auto& coll_md = m_podioDataSvc->getProvider().getCollectionMetaData( m_caloHits.get()->getID() ); - coll_md.setValue("CellIDEncodingString", field_str); - + if (m_readoutName == collect->GetName()) { size_t n_hit = collect->GetSize(); debug() << "\t" << n_hit << " hits are stored in a collection #" << iter_coll << ": " << collect->GetName() << endmsg; @@ -95,5 +118,6 @@ StatusCode SimG4SaveCalHits::saveOutput(const G4Event& aEvent) { } } } + return StatusCode::SUCCESS; } diff --git a/SimG4Components/src/SimG4SaveCalHits.h b/SimG4Components/src/SimG4SaveCalHits.h index e5599b3..14f31e5 100644 --- a/SimG4Components/src/SimG4SaveCalHits.h +++ b/SimG4Components/src/SimG4SaveCalHits.h @@ -1,18 +1,21 @@ #ifndef SIMG4COMPONENTS_G4SAVECALHITS_H #define SIMG4COMPONENTS_G4SAVECALHITS_H +// STL +#include +#include + // Gaudi #include "GaudiAlg/GaudiTool.h" -// FCCSW +// k4FWCore #include "k4FWCore/DataHandle.h" -#include "SimG4Interface/ISimG4SaveOutputTool.h" -class IGeoSvc; +#include "k4FWCore/MetaDataHandle.h" +#include "k4Interface/IGeoSvc.h" +#include "k4Interface/ISimG4SaveOutputTool.h" -// datamodel -namespace edm4hep { -class SimCalorimeterHitCollection; -} +// EDM4hep +#include "edm4hep/SimCalorimeterHitCollection.h" /** @class SimG4SaveCalHits SimG4Components/src/SimG4SaveCalHits.h SimG4SaveCalHits.h * @@ -48,14 +51,18 @@ class SimG4SaveCalHits : public GaudiTool, virtual public ISimG4SaveOutputTool { private: /// Pointer to the geometry service ServiceHandle m_geoSvc; - /// Pointer to Podio and Event Data Services - PodioLegacyDataSvc* m_podioDataSvc; - ServiceHandle m_eventDataSvc; - /// Handle for calo hits - DataHandle m_caloHits{"CaloHits", Gaudi::DataHandle::Writer, this}; - /// Name of the readouts (hits collections) to save + /// Output handle for calo hits + DataHandle m_caloHits{ + "CaloHits", Gaudi::DataHandle::Writer, this}; + /// Output handle for cell ID encoding string + MetaDataHandle m_cellIDEncoding{ + m_caloHits, "CellIDEncodingString", Gaudi::DataHandle::Writer}; + /// Name of the readouts (hits collections) to save, deprecated Gaudi::Property> m_readoutNames{ - this, "readoutNames", {}, "Name of the readouts (hits collections) to save"}; + this, "readoutNames", {}, "[Deprecated] Names of the readouts (hits collections) to save"}; + /// Name of the readout (hits collection) to save + Gaudi::Property m_readoutName { + this, "readoutName", {}, "Name of the readout (hits collection) to save"}; }; #endif /* SIMG4COMPONENTS_G4SAVECALHITS_H */ diff --git a/SimG4Components/src/SimG4SaveTrackerHits.cpp b/SimG4Components/src/SimG4SaveTrackerHits.cpp index 2ce3089..ca7d344 100644 --- a/SimG4Components/src/SimG4SaveTrackerHits.cpp +++ b/SimG4Components/src/SimG4SaveTrackerHits.cpp @@ -1,28 +1,23 @@ #include "SimG4SaveTrackerHits.h" -// FCCSW -#include "SimG4Interface/IGeoSvc.h" +// k4SimGeant4 #include "SimG4Common/Units.h" #include "SimG4Common/Geant4PreDigiTrackHit.h" // Geant4 #include "G4Event.hh" -// datamodel -#include "edm4hep/SimTrackerHitCollection.h" - // DD4hep #include "DD4hep/Detector.h" DECLARE_COMPONENT(SimG4SaveTrackerHits) -SimG4SaveTrackerHits::SimG4SaveTrackerHits(const std::string& aType, const std::string& aName, +SimG4SaveTrackerHits::SimG4SaveTrackerHits(const std::string& aType, + const std::string& aName, const IInterface* aParent) : GaudiTool(aType, aName, aParent), - m_geoSvc("GeoSvc", aName), - m_eventDataSvc("EventDataSvc", "SimG4SaveTrackerHits") - { + m_geoSvc("GeoSvc", aName) { declareInterface(this); declareProperty("SimTrackHits", m_trackHits, "Handle for tracker hits"); declareProperty("GeoSvc", m_geoSvc); @@ -39,24 +34,56 @@ StatusCode SimG4SaveTrackerHits::initialize() { << "Make sure you have GeoSvc and SimSvc in the right order in the configuration." << endmsg; return StatusCode::FAILURE; } - auto lcdd = m_geoSvc->lcdd(); - auto allReadouts = lcdd->readouts(); - for (auto& readoutName : m_readoutNames) { - if (allReadouts.find(readoutName) == allReadouts.end()) { - error() << "Readout " << readoutName << " not found! Please check tool configuration." << endmsg; - return StatusCode::FAILURE; - } else { - debug() << "Hits will be saved to EDM from the collection " << readoutName << endmsg; + + if (m_readoutName.empty() && m_readoutNames.empty()) { + error() << "No readout name provided. Exiting..." << endmsg; + return StatusCode::FAILURE; + } + + if (!m_readoutName.empty() && !m_readoutNames.empty()) { + error() << "Readout name provided through \"readoutName\" parameter, " + << "but also through deprecated \"readoutNames\" vector " + << "parameter." << endmsg; + error() << "Please use only the \"readoutName\" parameter. Exiting..." + << endmsg; + return StatusCode::FAILURE; + } + + if (!m_readoutNames.empty()) { + warning() << "Providing multiple readout names deprecated." << endmsg; + warning() << "Please use \"readoutName\" parameter instead." << endmsg; + + if (m_readoutNames.size() > 1) { + error() << "More than one readout name provided. Exiting..." << endmsg; } + + m_readoutName = m_readoutNames[0]; } - StatusCode sc = m_eventDataSvc.retrieve(); - m_podioDataSvc = dynamic_cast(m_eventDataSvc.get()); - if (sc == StatusCode::FAILURE) { - error() << "Error retrieving Event Data Service" << endmsg; + if (m_readoutName.empty()) { + error() << "No readout name provided. Exiting..." << endmsg; return StatusCode::FAILURE; } + auto lcdd = m_geoSvc->lcdd(); + auto allReadouts = lcdd->readouts(); + if (allReadouts.find(m_readoutName) == allReadouts.end()) { + error() << "Readout " << m_readoutName << " not found! " + << "Please check tool configuration. Exiting..." << endmsg; + return StatusCode::FAILURE; + } else { + info() << "Hits from readout \"" << m_readoutName.value() + << "\" will be saved in the collection \"" + << m_trackHits.objKey() << "\"." << endmsg; + } + + // Add CellID encoding string to hit collection metadata + auto idspec = lcdd->idSpecification(m_readoutName); + auto field_str = idspec.fieldDescription(); + m_cellIDEncoding.put(field_str); + debug() << "Storing cell ID encoding string: \"" << field_str << "\"." + << endmsg; + return StatusCode::SUCCESS; } @@ -71,15 +98,6 @@ StatusCode SimG4SaveTrackerHits::saveOutput(const G4Event& aEvent) { for (int iter_coll = 0; iter_coll < collections->GetNumberOfCollections(); iter_coll++) { collect = collections->GetHC(iter_coll); if (std::find(m_readoutNames.begin(), m_readoutNames.end(), collect->GetName()) != m_readoutNames.end()) { - - // Add CellID encoding string to collection metadata - auto lcdd = m_geoSvc->lcdd(); - auto allReadouts = lcdd->readouts(); - auto idspec = lcdd->idSpecification(collect->GetName()); - auto field_str = idspec.fieldDescription(); - auto& coll_md = m_podioDataSvc->getProvider().getCollectionMetaData( m_trackHits.get()->getID() ); - coll_md.setValue("CellIDEncodingString", field_str); - size_t n_hit = collect->GetSize(); verbose() << "\t" << n_hit << " hits are stored in a tracker collection #" << iter_coll << ": " << collect->GetName() << endmsg; @@ -103,11 +121,10 @@ StatusCode SimG4SaveTrackerHits::saveOutput(const G4Event& aEvent) { (float) (diff.z() * sim::g42edm::length), }); edmHit.setPathLength(diff.mag()); - - } } } } + return StatusCode::SUCCESS; } diff --git a/SimG4Components/src/SimG4SaveTrackerHits.h b/SimG4Components/src/SimG4SaveTrackerHits.h index fe1f200..bd9c9b4 100644 --- a/SimG4Components/src/SimG4SaveTrackerHits.h +++ b/SimG4Components/src/SimG4SaveTrackerHits.h @@ -1,18 +1,21 @@ #ifndef SIMG4COMPONENTS_G4SAVETRACKERHITS_H #define SIMG4COMPONENTS_G4SAVETRACKERHITS_H +// STL +#include +#include + // Gaudi #include "GaudiAlg/GaudiTool.h" -// FCCSW +// k4FWCore #include "k4FWCore/DataHandle.h" -#include "SimG4Interface/ISimG4SaveOutputTool.h" -class IGeoSvc; +#include "k4FWCore/MetaDataHandle.h" +#include "k4Interface/IGeoSvc.h" +#include "k4Interface/ISimG4SaveOutputTool.h" -// datamodel -namespace edm4hep { -class SimTrackerHitCollection; -} +// EDM4hep +#include "edm4hep/SimTrackerHitCollection.h" /** @class SimG4SaveTrackerHits SimG4Components/src/SimG4SaveTrackerHits.h SimG4SaveTrackerHits.h * @@ -49,15 +52,18 @@ class SimG4SaveTrackerHits : public GaudiTool, virtual public ISimG4SaveOutputTo private: /// Pointer to the geometry service ServiceHandle m_geoSvc; - /// Pointer to Podio and Event Data Services - PodioLegacyDataSvc* m_podioDataSvc; - ServiceHandle m_eventDataSvc; - /// Handle for tracker hits - DataHandle m_trackHits{"TrackerHits", Gaudi::DataHandle::Writer, this}; - /// Handle for tracker hits including position information - /// Name of the readouts (hits collections) to save - Gaudi::Property> m_readoutNames{ - this, "readoutNames", {}, "Name of the readouts (hits collections) to save"}; + /// Handle for output tracker hits + DataHandle m_trackHits { + "TrackerHits", Gaudi::DataHandle::Writer, this}; + /// Output handle for cell ID encoding string + MetaDataHandle m_cellIDEncoding { + m_trackHits, "CellIDEncodingString", Gaudi::DataHandle::Writer}; + /// Names of the readouts (hits collections) to save + Gaudi::Property> m_readoutNames { + this, "readoutNames", {}, "[Deprecated] Name of the readouts (hits collections) to save"}; + /// Name of the readout (hits collection) to save + Gaudi::Property m_readoutName { + this, "readoutName", {}, "Name of the readout (hit collection) to save"}; }; #endif /* SIMG4COMPONENTS_G4SAVETRACKERHITS_H */ diff --git a/SimG4Components/tests/options/xAngleBoost.py b/SimG4Components/tests/options/xAngleBoost.py index 6aa4da3..c57ec80 100644 --- a/SimG4Components/tests/options/xAngleBoost.py +++ b/SimG4Components/tests/options/xAngleBoost.py @@ -7,8 +7,8 @@ ApplicationMgr().EvtMax = 2 ApplicationMgr().OutputLevel = INFO -from Configurables import k4LegacyDataSvc -podioevent = k4LegacyDataSvc("EventDataSvc") +from Configurables import k4DataSvc +podioevent = k4DataSvc("EventDataSvc") ApplicationMgr().ExtSvc += [podioevent] @@ -45,8 +45,8 @@ ApplicationMgr().TopAlg += [xAngleBoost] -from Configurables import PodioLegacyOutput -output = PodioLegacyOutput("output") +from Configurables import PodioOutput +output = PodioOutput("output") output.filename = "output_xAngleBoost.root" output.outputCommands = ["keep *"] -ApplicationMgr().TopAlg += [output] \ No newline at end of file +ApplicationMgr().TopAlg += [output]