Skip to content

Commit

Permalink
Merge pull request #47 from andresailer/addAccessor
Browse files Browse the repository at this point in the history
GeoSvc: Add constantAsString, EnableGeant4Geo, getDetector, deprecated lcdd
  • Loading branch information
kjvbrt authored Sep 12, 2023
2 parents 90f21af + 3847296 commit 07b4981
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 76 deletions.
16 changes: 8 additions & 8 deletions Detector/DetComponents/src/GeoConstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace det {

GeoConstruction::GeoConstruction(dd4hep::Detector& lcdd, std::map<std::string, std::string> sensitive_types) : m_lcdd(lcdd), m_sensitive_types(sensitive_types) {}
GeoConstruction::GeoConstruction(dd4hep::Detector& detector, std::map<std::string, std::string> sensitive_types) : m_detector(detector), m_sensitive_types(sensitive_types) {}

GeoConstruction::~GeoConstruction() {}

Expand All @@ -35,15 +35,15 @@ void GeoConstruction::ConstructSDandField() {
typ = m_sensitive_types[typ];
}
// Sensitive detectors are deleted in ~G4SDManager
G4VSensitiveDetector* g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, &m_lcdd);
G4VSensitiveDetector* g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, &m_detector);
if (g4sd == nullptr) {
std::string tmp = typ;
tmp[0] = ::toupper(tmp[0]);
typ = "Geant4" + tmp;
g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, &m_lcdd);
g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, &m_detector);
if (g4sd == nullptr) {
dd4hep::PluginDebug dbg;
g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, &m_lcdd);
g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, &m_detector);
if (g4sd == nullptr) {
throw std::runtime_error("ConstructSDandField: FATAL Failed to "
"create Geant4 sensitive detector " +
Expand All @@ -70,14 +70,14 @@ void GeoConstruction::ConstructSDandField() {
// method borrowed from dd4hep::sim::Geant4DetectorConstruction::Construct()
G4VPhysicalVolume* GeoConstruction::Construct() {
dd4hep::sim::Geant4Mapping& g4map = dd4hep::sim::Geant4Mapping::instance();
dd4hep::DetElement world = m_lcdd.world();
dd4hep::sim::Geant4Converter conv(m_lcdd, dd4hep::DEBUG);
dd4hep::DetElement world = m_detector.world();
dd4hep::sim::Geant4Converter conv(m_detector, dd4hep::DEBUG);
dd4hep::sim::Geant4GeometryInfo* geo_info = conv.create(world).detach();
g4map.attach(geo_info);
// All volumes are deleted in ~G4PhysicalVolumeStore()
G4VPhysicalVolume* m_world = geo_info->world();
if(not m_lcdd.volumeManager().isValid()) {
m_lcdd.apply("DD4hepVolumeManager", 0, 0);
if(not m_detector.volumeManager().isValid()) {
m_detector.apply("DD4hepVolumeManager", 0, 0);
}
// Create Geant4 volume manager
g4map.volumeManager();
Expand Down
4 changes: 2 additions & 2 deletions Detector/DetComponents/src/GeoConstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace det {
class GeoConstruction : public G4VUserDetectorConstruction {
public:
/// Constructor
GeoConstruction(dd4hep::Detector& lcdd, std::map<std::string, std::string> sensitive_types);
GeoConstruction(dd4hep::Detector& detector, std::map<std::string, std::string> sensitive_types);
/// Default destructor
virtual ~GeoConstruction();
/// Geometry construction callback: Invoke the conversion to Geant4
Expand All @@ -35,7 +35,7 @@ class GeoConstruction : public G4VUserDetectorConstruction {

private:
/// Reference to geometry object
dd4hep::Detector& m_lcdd;
dd4hep::Detector& m_detector;
std::map<std::string, std::string> m_sensitive_types;
};
}
Expand Down
20 changes: 15 additions & 5 deletions Detector/DetComponents/src/GeoSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ StatusCode GeoSvc::initialize() {
}

// Build Geant4 Geometry
{
if(m_buildGeant4Geo) {
StatusCode sc = buildGeant4Geo();
if (sc.isFailure()) {
error() << "Could not build Geant4 geometry!" << endmsg;
} else {
info() << "Geant4 geometry SUCCESSFULLY built." << endmsg;
}
} else {
debug() << "Conversion to Geant4 Geometry is disabled" << endmsg;
}

return StatusCode::SUCCESS;
Expand Down Expand Up @@ -82,13 +84,17 @@ StatusCode GeoSvc::buildDD4HepGeo() {
return StatusCode::SUCCESS;
}

dd4hep::Detector* GeoSvc::lcdd() { return (m_dd4hepgeo); }
dd4hep::Detector* GeoSvc::lcdd() { return m_dd4hepgeo; }

dd4hep::Detector* GeoSvc::getDetector() { return m_dd4hepgeo; }

dd4hep::DetElement GeoSvc::getDD4HepGeo() { return (lcdd()->world()); }
dd4hep::DetElement GeoSvc::getDD4HepGeo() { return m_dd4hepgeo->world(); }

StatusCode GeoSvc::buildGeant4Geo() {
std::shared_ptr<G4VUserDetectorConstruction> detector(new det::GeoConstruction(*lcdd(), m_sensitive_types));
m_geant4geo = detector;
if (not m_buildGeant4Geo) {
return StatusCode::SUCCESS;
}
m_geant4geo = std::make_shared<det::GeoConstruction>(*m_dd4hepgeo, m_sensitive_types);
if (nullptr != m_geant4geo) {
return StatusCode::SUCCESS;
}
Expand All @@ -97,3 +103,7 @@ StatusCode GeoSvc::buildGeant4Geo() {
}

G4VUserDetectorConstruction* GeoSvc::getGeant4Geo() { return (m_geant4geo.get()); }

std::string GeoSvc::constantAsString(std::string const& name) {
return m_dd4hepgeo->constantAsString(name);
}
6 changes: 6 additions & 0 deletions Detector/DetComponents/src/GeoSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class GeoSvc : public extends<Service, IGeoSvc> {
StatusCode buildGeant4Geo();
// receive DD4hep Geometry
virtual dd4hep::DetElement getDD4HepGeo() override;
[[deprecated("Use getDetector() instead")]]
virtual dd4hep::Detector* lcdd() override;
virtual dd4hep::Detector* getDetector() override;
virtual std::string constantAsString(std::string const& name) override;
// receive Geant4 Geometry
virtual G4VUserDetectorConstruction* getGeant4Geo() override;

Expand All @@ -50,6 +53,9 @@ class GeoSvc : public extends<Service, IGeoSvc> {
Gaudi::Property<std::vector<std::string>> m_xmlFileNames{this, "detectors", {}, "Detector descriptions XML-files"};
/// mapping of sensitive detector names
Gaudi::Property<std::map<std::string, std::string>> m_sensitive_types{this, "sensitiveTypes", {{"tracker", "SimpleTrackerSD"}, {"calorimeter", "SimpleCalorimeterSD"}}};
/// Whether to create the geant4 geometry or not
Gaudi::Property<bool> m_buildGeant4Geo{this, "EnableGeant4Geo", true, "If True the DD4hep geometry is converted for Geant4 Simulations"};

};

#endif // GEOSVC_H
2 changes: 1 addition & 1 deletion Detector/DetComponents/src/MaterialScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ StatusCode MaterialScan::initialize() {
tree->Branch("matDepth", &matDepthPtr);
tree->Branch("material", &materialPtr);

auto lcdd = m_geoSvc->lcdd();
auto lcdd = m_geoSvc->getDetector();
dd4hep::rec::MaterialManager matMgr(lcdd->detector(m_envelopeName).volume());
dd4hep::rec::Vector3D beginning(0, 0, 0);
auto boundaryVol = lcdd->detector(m_envelopeName).volume()->GetShape();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ StatusCode MaterialScan_2D_genericAngle::initialize() {
tree->Branch("matDepth", &matDepthPtr);
tree->Branch("material", &materialPtr);

auto lcdd = m_geoSvc->lcdd();
auto lcdd = m_geoSvc->getDetector();
dd4hep::rec::MaterialManager matMgr(lcdd->detector(m_envelopeName).volume());
dd4hep::rec::Vector3D beginning(0, 0, 0);
auto boundaryVol = lcdd->detector(m_envelopeName).volume()->GetShape();
Expand Down
2 changes: 1 addition & 1 deletion Detector/DetComponents/src/MaterialScan_genericAngle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ StatusCode MaterialScan_genericAngle::initialize() {
tree->Branch("matDepth", &matDepthPtr);
tree->Branch("material", &materialPtr);

auto lcdd = m_geoSvc->lcdd();
auto lcdd = m_geoSvc->getDetector();
dd4hep::rec::MaterialManager matMgr(lcdd->detector(m_envelopeName).volume());
dd4hep::rec::Vector3D beginning(0, 0, 0);
auto boundaryVol = lcdd->detector(m_envelopeName).volume()->GetShape();
Expand Down
4 changes: 2 additions & 2 deletions Detector/DetComponents/src/MergeCells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ StatusCode MergeCells::initialize() {
return StatusCode::FAILURE;
}
// check if readout exists
if (m_geoSvc->lcdd()->readouts().find(m_readoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_readoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_readoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
auto readout = m_geoSvc->lcdd()->readout(m_readoutName);
auto readout = m_geoSvc->getDetector()->readout(m_readoutName);
m_descriptor = readout.idSpec();
// check if identifier exists in the decoder
auto itIdentifier = std::find_if(m_descriptor.fields().begin(),
Expand Down
4 changes: 2 additions & 2 deletions Detector/DetComponents/src/MergeLayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ StatusCode MergeLayers::initialize() {
return StatusCode::FAILURE;
}
// check if readout exists
if (m_geoSvc->lcdd()->readouts().find(m_readoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_readoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_readoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
auto readout = m_geoSvc->lcdd()->readout(m_readoutName);
auto readout = m_geoSvc->getDetector()->readout(m_readoutName);
m_descriptor = readout.idSpec();
// check if identifier exists in the decoder
auto itIdentifier = std::find_if(m_descriptor.fields().begin(),
Expand Down
8 changes: 4 additions & 4 deletions Detector/DetComponents/src/RedoSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ StatusCode RedoSegmentation::initialize() {
return StatusCode::FAILURE;
}
// check if readouts exist
if (m_geoSvc->lcdd()->readouts().find(m_oldReadoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_oldReadoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_oldReadoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
if (m_geoSvc->lcdd()->readouts().find(m_newReadoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_newReadoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_newReadoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
// Take readout, bitfield from GeoSvc
m_oldDecoder = m_geoSvc->lcdd()->readout(m_oldReadoutName).idSpec().decoder();
m_oldDecoder = m_geoSvc->getDetector()->readout(m_oldReadoutName).idSpec().decoder();
// segmentation identifiers to be overwritten
if (m_oldIdentifiers.size() == 0) {
// it is not an error, maybe no segmentation was used previously
Expand All @@ -45,7 +45,7 @@ StatusCode RedoSegmentation::initialize() {
}
}
// Take new segmentation from geometry service
m_segmentation = m_geoSvc->lcdd()->readout(m_newReadoutName).segmentation().segmentation();
m_segmentation = m_geoSvc->getDetector()->readout(m_newReadoutName).segmentation().segmentation();
// check if detector identifiers (old and new) agree
std::vector<std::string> newFields;
for (uint itField = 0; itField < m_segmentation->decoder()->size(); itField++) {
Expand Down
8 changes: 4 additions & 4 deletions Detector/DetComponents/src/RewriteBitfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ StatusCode RewriteBitfield::initialize() {
return StatusCode::FAILURE;
}
// check if readouts exist
if (m_geoSvc->lcdd()->readouts().find(m_oldReadoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_oldReadoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_oldReadoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
if (m_geoSvc->lcdd()->readouts().find(m_newReadoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_newReadoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_newReadoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
// Take readout, bitfield from GeoSvc
m_oldDecoder = m_geoSvc->lcdd()->readout(m_oldReadoutName).idSpec().decoder();
m_oldDecoder = m_geoSvc->getDetector()->readout(m_oldReadoutName).idSpec().decoder();
// segmentation identifiers to be overwritten
if (m_oldIdentifiers.size() == 0) {
// it is not an error, maybe no segmentation was used previously
Expand All @@ -52,7 +52,7 @@ StatusCode RewriteBitfield::initialize() {
}
}
std::vector<std::string> newFields;
m_newDecoder = m_geoSvc->lcdd()->readout(m_newReadoutName).idSpec().decoder();
m_newDecoder = m_geoSvc->getDetector()->readout(m_newReadoutName).idSpec().decoder();
for (uint itField = 0; itField < m_newDecoder->size(); itField++) {
newFields.push_back((*m_newDecoder)[itField].name());
}
Expand Down
4 changes: 2 additions & 2 deletions Detector/DetStudies/src/components/EnergyInCaloLayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ StatusCode EnergyInCaloLayers::initialize() {
}

// Check if readout exists
if (m_geoSvc->lcdd()->readouts().find(m_readoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_readoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Can't find readout <<" << m_readoutName << ">>!" << endmsg;
return StatusCode::FAILURE;
}
Expand All @@ -64,7 +64,7 @@ StatusCode EnergyInCaloLayers::initialize() {


StatusCode EnergyInCaloLayers::execute() {
auto decoder = m_geoSvc->lcdd()->readout(m_readoutName).idSpec().decoder();
auto decoder = m_geoSvc->getDetector()->readout(m_readoutName).idSpec().decoder();

// Initialize output variables
std::vector<double>* energyInLayer = m_energyInLayer.createAndPut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ StatusCode SamplingFractionInLayers::initialize() {
return StatusCode::FAILURE;
}
// check if readouts exist
if (m_geoSvc->lcdd()->readouts().find(m_readoutName) == m_geoSvc->lcdd()->readouts().end()) {
if (m_geoSvc->getDetector()->readouts().find(m_readoutName) == m_geoSvc->getDetector()->readouts().end()) {
error() << "Readout <<" << m_readoutName << ">> does not exist." << endmsg;
return StatusCode::FAILURE;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ StatusCode SamplingFractionInLayers::initialize() {
}

StatusCode SamplingFractionInLayers::execute() {
auto decoder = m_geoSvc->lcdd()->readout(m_readoutName).idSpec().decoder();
auto decoder = m_geoSvc->getDetector()->readout(m_readoutName).idSpec().decoder();
double sumE = 0.;
std::vector<double> sumElayers;
double sumEactive = 0.;
Expand Down
4 changes: 3 additions & 1 deletion SimG4Components/src/InspectHitsCollectionsTool.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "InspectHitsCollectionsTool.h"

// k4FWCore
#include "k4Interface/IGeoSvc.h"

// FCCSW
#include "SimG4Interface/IGeoSvc.h"
#include "SimG4Common/Geant4CaloHit.h"
#include "SimG4Common/Geant4PreDigiTrackHit.h"

Expand Down
4 changes: 2 additions & 2 deletions SimG4Components/src/SimG4DD4hepDetector.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "SimG4DD4hepDetector.h"

// FCCSW
#include "SimG4Interface/IGeoSvc.h"
// k4FWCore
#include "k4Interface/IGeoSvc.h"

// Geant4
#include "G4VUserDetectorConstruction.hh"
Expand Down
2 changes: 1 addition & 1 deletion SimG4Components/src/SimG4MagneticFieldTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ StatusCode SimG4MagneticFieldTool::initialize() {
}

info() << "Adding following field(s):" << endmsg;
dd4hep::Detector* detDescription = m_geoSvc->lcdd();
dd4hep::Detector* detDescription = m_geoSvc->getDetector();
auto fields = detDescription->fields();
for (const auto& field : fields) {
info() << " - " << field.first << ": " << field.second->type << endmsg;
Expand Down
2 changes: 1 addition & 1 deletion SimG4Components/src/SimG4SaveCalHits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ StatusCode SimG4SaveCalHits::initialize() {
return StatusCode::FAILURE;
}

auto lcdd = m_geoSvc->lcdd();
auto lcdd = m_geoSvc->getDetector();
auto allReadouts = lcdd->readouts();
if (allReadouts.find(m_readoutName) == allReadouts.end()) {
error() << "Readout " << m_readoutName << " not found! "
Expand Down
2 changes: 1 addition & 1 deletion SimG4Components/src/SimG4SaveTrackerHits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ StatusCode SimG4SaveTrackerHits::initialize() {
return StatusCode::FAILURE;
}

auto lcdd = m_geoSvc->lcdd();
auto lcdd = m_geoSvc->getDetector();
auto allReadouts = lcdd->readouts();
if (allReadouts.find(m_readoutName) == allReadouts.end()) {
error() << "Readout " << m_readoutName << " not found! "
Expand Down
4 changes: 3 additions & 1 deletion SimG4Components/src/SimG4SaveTrajectory.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "SimG4SaveTrajectory.h"

// k4FwCore
#include "k4Interface/IGeoSvc.h"

// FCCSW
#include "SimG4Interface/IGeoSvc.h"
#include "SimG4Common/Units.h"

// Geant4
Expand Down
35 changes: 0 additions & 35 deletions SimG4Interface/include/SimG4Interface/IGeoSvc.h

This file was deleted.

0 comments on commit 07b4981

Please sign in to comment.