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