diff --git a/Examples/CMakeLists.txt b/Examples/CMakeLists.txt index a53bc0fa1c8f8b543f93397650fdc4e829977268..8a0ba74979b16bc41d1e26dee535ac00fd12f51f 100644 --- a/Examples/CMakeLists.txt +++ b/Examples/CMakeLists.txt @@ -6,6 +6,11 @@ find_package(podio REQUIRED) find_package(LCIO REQUIRED) find_package(EDM4HEP REQUIRED) find_package(K4FWCore REQUIRED) +find_package(DD4hep COMPONENTS DDRec DDParsers REQUIRED) + +gaudi_depends_on_subdirs( + Detector/DetInterface +) set(Examples_srcs src/HelloWorld/*.cpp @@ -13,6 +18,7 @@ set(Examples_srcs src/SecondAlg/*.cpp # src/PlcioTest/*.cpp src/Edm4hepTest/*.cpp + src/DumpIDAlg/*.cpp ) # Headers and Libraries @@ -23,6 +29,7 @@ gaudi_install_headers(Examples) gaudi_add_module(Examples ${Examples_srcs} INCLUDE_DIRS K4FWCore GaudiAlgLib GaudiKernel ${podio_INCLUDE_DIRS} ${LCIO_INCLUDE_DIRS} LINK_LIBRARIES K4FWCore GaudiAlgLib GaudiKernel ${LCIO_LIBRARIES} + DD4hep ${DD4hep_COMPONENT_LIBRARIES} # Force loading the libraries. -Wl,--no-as-needed EDM4HEP::edm4hep EDM4HEP::edm4hepDict ${podio_LIBRARIES} podio::podioRootIO -Wl,--as-needed ) diff --git a/Examples/src/DumpIDAlg/DumpIDAlg.cpp b/Examples/src/DumpIDAlg/DumpIDAlg.cpp new file mode 100644 index 0000000000000000000000000000000000000000..627227d651357afa792fc207485a670d97430e37 --- /dev/null +++ b/Examples/src/DumpIDAlg/DumpIDAlg.cpp @@ -0,0 +1,94 @@ +#include "DumpIDAlg.h" +#include "edm4hep/EventHeaderCollection.h" +#include "edm4hep/MCParticleCollection.h" +#include "edm4hep/SimCalorimeterHitCollection.h" +#include "edm4hep/CaloHitContributionCollection.h" + +#include "DD4hep/Detector.h" +#include "DD4hep/IDDescriptor.h" +#include "DD4hep/Plugins.h" + +DECLARE_COMPONENT(DumpIDAlg) + +DumpIDAlg::DumpIDAlg(const std::string& name, ISvcLocator* svcLoc) +: GaudiAlgorithm(name, svcLoc), m_dd4hep_geo(nullptr), m_decoder(nullptr) +{ + +} + +StatusCode DumpIDAlg::initialize() +{ + m_geosvc = service<IGeoSvc>("GeoSvc"); + if (!m_geosvc) { + error() << "Failed to find GeoSvc." << endmsg; + return StatusCode::FAILURE; + } + m_dd4hep_geo = m_geosvc->lcdd(); + if (!m_dd4hep_geo) { + error() << "failed to retrieve dd4hep_geo: " << m_dd4hep_geo << endmsg; + return StatusCode::FAILURE; + } + + // get the DD4hep readout + auto readouts = m_dd4hep_geo->readouts(); + const std::string name_readout = "EcalBarrelCollection"; + if (readouts.find(name_readout) != readouts.end()) { + dd4hep::Readout readout = m_dd4hep_geo->readout(name_readout); + + auto m_idspec = readout.idSpec(); + + info() << "The idspec is " << m_idspec.fieldDescription() << " for " << name_readout << endmsg; + + dd4hep::DDSegmentation::BitFieldCoder* decoder = m_idspec.decoder(); + + m_decoder = decoder; + } + + if (!m_decoder) { + error() << "Failed to get the decoder. " << endmsg; + return StatusCode::FAILURE; + } + + return GaudiAlgorithm::initialize(); +} + +StatusCode DumpIDAlg::execute() +{ + + + auto ecalBarrelCol = m_EcalBarrelCol.get(); + for (auto calohit: *ecalBarrelCol) { + auto cellid = calohit.getCellID(); + + int id_system = m_decoder->get(cellid, "system"); + int id_module = m_decoder->get(cellid, "module"); + int id_stave = m_decoder->get(cellid, "stave"); + int id_tower = m_decoder->get(cellid, "tower"); + int id_layer = m_decoder->get(cellid, "layer"); + int id_wafer = m_decoder->get(cellid, "wafer"); + int id_cellX = m_decoder->get(cellid, "cellX"); + int id_cellY = m_decoder->get(cellid, "cellY"); + + info() << "Calo hit cellid: " << cellid + << " system: " << id_system + << " module: " << id_module + << " stave: " << id_stave + << " tower: " << id_tower + << " layer: " << id_layer + << " wafer: " << id_wafer + << " cellX: " << id_cellX + << " cellY: " << id_cellY + << endmsg; + } + return StatusCode::SUCCESS; +} + +StatusCode DumpIDAlg::finalize() +{ + + return GaudiAlgorithm::finalize(); +} + + + + diff --git a/Examples/src/DumpIDAlg/DumpIDAlg.h b/Examples/src/DumpIDAlg/DumpIDAlg.h new file mode 100644 index 0000000000000000000000000000000000000000..04277437b42c3f5457379d37c0a363b686014239 --- /dev/null +++ b/Examples/src/DumpIDAlg/DumpIDAlg.h @@ -0,0 +1,39 @@ +#ifndef DumpIDAlg_h +#define DumpIDAlg_h + +#include "FWCore/DataHandle.h" +#include "GaudiAlg/GaudiAlgorithm.h" + +#include "DetInterface/IGeoSvc.h" + +#include "DD4hep/Detector.h" + +namespace edm4hep { + class EventHeaderCollection; + class MCParticleCollection; + class SimCalorimeterHitCollection; + class CaloHitContributionCollection; +} + +class DumpIDAlg: public GaudiAlgorithm +{ +public: + + DumpIDAlg(const std::string& name, ISvcLocator* svcLoc); + + virtual StatusCode initialize(); + virtual StatusCode execute(); + virtual StatusCode finalize(); + +private: + SmartIF<IGeoSvc> m_geosvc; + dd4hep::Detector* m_dd4hep_geo; + dd4hep::DDSegmentation::BitFieldCoder* m_decoder; + + DataHandle<edm4hep::SimCalorimeterHitCollection> m_EcalBarrelCol{"EcalBarrelCollection", + Gaudi::DataHandle::Reader, this}; + +}; + + +#endif