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

Replacing legacy Podio service with the frame Podio service #40

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 4 additions & 19 deletions Detector/DetComponents/src/RedoSegmentation.cpp
Original file line number Diff line number Diff line change
@@ -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)");
}
Expand All @@ -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;
Expand All @@ -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<PodioLegacyDataSvc*>(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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
33 changes: 17 additions & 16 deletions Detector/DetComponents/src/RedoSegmentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -63,12 +58,18 @@ class RedoSegmentation : public GaudiAlgorithm {
uint64_t volumeID(uint64_t aCellId) const;
/// Pointer to the geometry service
ServiceHandle<IGeoSvc> m_geoSvc;
PodioLegacyDataSvc* m_podioDataSvc;
ServiceHandle<IDataProviderSvc> m_eventDataSvc;
/// Handle for the EDM positioned hits to be read
DataHandle<edm4hep::CalorimeterHitCollection> m_inHits{"hits/caloInHits", Gaudi::DataHandle::Reader, this};
DataHandle<edm4hep::CalorimeterHitCollection> m_inHits{
"hits/caloInHits", Gaudi::DataHandle::Reader, this};
/// Handle for the input hits cell id encoding
MetaDataHandle<std::string> m_inHitsCellIDEncoding{
m_inHits,"CellIDEncodingString", Gaudi::DataHandle::Reader};
/// Handle for the EDM hits to be written
DataHandle<edm4hep::SimCalorimeterHitCollection> m_outHits{"hits/caloOutHits", Gaudi::DataHandle::Writer, this};
DataHandle<edm4hep::SimCalorimeterHitCollection> m_outHits{
"hits/caloOutHits", Gaudi::DataHandle::Writer, this};
/// Handle for the output hits cell id encoding
MetaDataHandle<std::string> m_outHitsCellIDEncoding{
m_outHits,"CellIDEncodingString", Gaudi::DataHandle::Writer};
/// New segmentation
dd4hep::DDSegmentation::Segmentation* m_segmentation;
/// Name of the detector readout used in simulation
Expand Down
80 changes: 52 additions & 28 deletions SimG4Components/src/SimG4SaveCalHits.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#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"


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<ISimG4SaveOutputTool>(this);
declareProperty("CaloHits", m_caloHits, "Handle for calo hits");
declareProperty("GeoSvc", m_geoSvc);
Expand All @@ -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<PodioLegacyDataSvc*>(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;
}

Expand All @@ -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;
Expand All @@ -95,5 +118,6 @@ StatusCode SimG4SaveCalHits::saveOutput(const G4Event& aEvent) {
}
}
}

return StatusCode::SUCCESS;
}
35 changes: 21 additions & 14 deletions SimG4Components/src/SimG4SaveCalHits.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#ifndef SIMG4COMPONENTS_G4SAVECALHITS_H
#define SIMG4COMPONENTS_G4SAVECALHITS_H

// STL
#include <vector>
#include <string>

// 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
*
Expand Down Expand Up @@ -48,14 +51,18 @@ class SimG4SaveCalHits : public GaudiTool, virtual public ISimG4SaveOutputTool {
private:
/// Pointer to the geometry service
ServiceHandle<IGeoSvc> m_geoSvc;
/// Pointer to Podio and Event Data Services
PodioLegacyDataSvc* m_podioDataSvc;
ServiceHandle<IDataProviderSvc> m_eventDataSvc;
/// Handle for calo hits
DataHandle<edm4hep::SimCalorimeterHitCollection> m_caloHits{"CaloHits", Gaudi::DataHandle::Writer, this};
/// Name of the readouts (hits collections) to save
/// Output handle for calo hits
DataHandle<edm4hep::SimCalorimeterHitCollection> m_caloHits{
"CaloHits", Gaudi::DataHandle::Writer, this};
/// Output handle for cell ID encoding string
MetaDataHandle<std::string> m_cellIDEncoding{
m_caloHits, "CellIDEncodingString", Gaudi::DataHandle::Writer};
/// Name of the readouts (hits collections) to save, deprecated
Gaudi::Property<std::vector<std::string>> 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<std::string> m_readoutName {
this, "readoutName", {}, "Name of the readout (hits collection) to save"};
};

#endif /* SIMG4COMPONENTS_G4SAVECALHITS_H */
Loading
Loading