File indexing completed on 2025-08-06 08:10:41
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/DD4hepDetector/DD4hepGeometryService.hpp"
0010
0011 #include "Acts/Geometry/TrackingGeometry.hpp"
0012 #include "Acts/Plugins/DD4hep/ConvertDD4hepDetector.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014
0015 #include <algorithm>
0016 #include <memory>
0017 #include <stdexcept>
0018 #include <utility>
0019
0020 #include <DD4hep/Detector.h>
0021 #include <DD4hep/Handle.h>
0022 #include <DD4hep/Volumes.h>
0023 #include <Parsers/Printout.h>
0024
0025 class TGeoNode;
0026
0027 ActsExamples::DD4hep::DD4hepGeometryService::DD4hepGeometryService(
0028 const ActsExamples::DD4hep::DD4hepGeometryService::Config& cfg)
0029 : m_cfg(cfg),
0030 m_logger{Acts::getDefaultLogger("DD4hepGeometryService", cfg.logLevel)} {
0031 if (m_cfg.xmlFileNames.empty()) {
0032 throw std::invalid_argument("Missing DD4hep XML filenames");
0033 }
0034 }
0035
0036 ActsExamples::DD4hep::DD4hepGeometryService::~DD4hepGeometryService() {
0037 if (m_detector != nullptr) {
0038 m_detector->destroyInstance();
0039 }
0040 }
0041
0042 ActsExamples::ProcessCode
0043 ActsExamples::DD4hep::DD4hepGeometryService::buildDD4hepGeometry() {
0044 switch (m_cfg.dd4hepLogLevel) {
0045 case Acts::Logging::Level::VERBOSE:
0046 dd4hep::setPrintLevel(dd4hep::PrintLevel::VERBOSE);
0047 break;
0048 case Acts::Logging::Level::DEBUG:
0049 dd4hep::setPrintLevel(dd4hep::PrintLevel::DEBUG);
0050 break;
0051 case Acts::Logging::Level::INFO:
0052 dd4hep::setPrintLevel(dd4hep::PrintLevel::INFO);
0053 break;
0054 case Acts::Logging::Level::WARNING:
0055 dd4hep::setPrintLevel(dd4hep::PrintLevel::WARNING);
0056 break;
0057 case Acts::Logging::Level::ERROR:
0058 dd4hep::setPrintLevel(dd4hep::PrintLevel::ERROR);
0059 break;
0060 case Acts::Logging::Level::FATAL:
0061 dd4hep::setPrintLevel(dd4hep::PrintLevel::FATAL);
0062 break;
0063 case Acts::Logging::Level::MAX:
0064 dd4hep::setPrintLevel(dd4hep::PrintLevel::ALWAYS);
0065 break;
0066 }
0067 m_detector = &dd4hep::Detector::getInstance();
0068 for (auto& file : m_cfg.xmlFileNames) {
0069 m_detector->fromCompact(file.c_str());
0070 }
0071 m_detector->volumeManager();
0072 m_detector->apply("DD4hepVolumeManager", 0, nullptr);
0073 m_geometry = m_detector->world();
0074
0075 return ActsExamples::ProcessCode::SUCCESS;
0076 }
0077
0078 dd4hep::Detector&
0079 ActsExamples::DD4hep::DD4hepGeometryService::DD4hepGeometryService::detector() {
0080 if (m_detector == nullptr) {
0081 buildDD4hepGeometry();
0082 }
0083 return *m_detector;
0084 }
0085
0086 dd4hep::DetElement& ActsExamples::DD4hep::DD4hepGeometryService::geometry() {
0087 if (!m_geometry) {
0088 buildDD4hepGeometry();
0089 }
0090 return m_geometry;
0091 }
0092
0093 TGeoNode& ActsExamples::DD4hep::DD4hepGeometryService::tgeoGeometry() {
0094 if (!m_geometry) {
0095 buildDD4hepGeometry();
0096 }
0097 return *m_geometry.placement().ptr();
0098 }
0099
0100 ActsExamples::ProcessCode
0101 ActsExamples::DD4hep::DD4hepGeometryService::buildTrackingGeometry(
0102 const Acts::GeometryContext& gctx) {
0103
0104 auto logger = Acts::getDefaultLogger("DD4hepConversion", m_cfg.logLevel);
0105 m_trackingGeometry = Acts::convertDD4hepDetector(
0106 geometry(), *logger, m_cfg.bTypePhi, m_cfg.bTypeR, m_cfg.bTypeZ,
0107 m_cfg.envelopeR, m_cfg.envelopeZ, m_cfg.defaultLayerThickness,
0108 m_cfg.sortDetectors, gctx, m_cfg.matDecorator,
0109 m_cfg.geometryIdentifierHook);
0110 return ActsExamples::ProcessCode::SUCCESS;
0111 }
0112
0113 std::shared_ptr<const Acts::TrackingGeometry>
0114 ActsExamples::DD4hep::DD4hepGeometryService::trackingGeometry(
0115 const Acts::GeometryContext& gctx) {
0116 if (!m_trackingGeometry) {
0117 buildTrackingGeometry(gctx);
0118 }
0119 return m_trackingGeometry;
0120 }
0121
0122 void ActsExamples::DD4hep::sortFCChhDetElements(
0123 std::vector<dd4hep::DetElement>& det) {
0124 std::vector<dd4hep::DetElement> tracker;
0125 std::vector<dd4hep::DetElement> eCal;
0126 std::vector<dd4hep::DetElement> hCal;
0127 std::vector<dd4hep::DetElement> muon;
0128 for (auto& detElement : det) {
0129 std::string detName = detElement.name();
0130 if (detName.find("Muon") != std::string::npos) {
0131 muon.push_back(detElement);
0132 } else if (detName.find("ECal") != std::string::npos) {
0133 eCal.push_back(detElement);
0134 } else if (detName.find("HCal") != std::string::npos) {
0135 hCal.push_back(detElement);
0136 } else {
0137 tracker.push_back(detElement);
0138 }
0139 }
0140 sort(muon.begin(), muon.end(),
0141 [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0142 return (a.id() < b.id());
0143 });
0144 sort(eCal.begin(), eCal.end(),
0145 [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0146 return (a.id() < b.id());
0147 });
0148 sort(hCal.begin(), hCal.end(),
0149 [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0150 return (a.id() < b.id());
0151 });
0152 sort(tracker.begin(), tracker.end(),
0153 [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0154 return (a.id() < b.id());
0155 });
0156 det.clear();
0157 det = tracker;
0158
0159 det.insert(det.end(), eCal.begin(), eCal.end());
0160 det.insert(det.end(), hCal.begin(), hCal.end());
0161 det.insert(det.end(), muon.begin(), muon.end());
0162 }