File indexing completed on 2025-11-11 09:18:02
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();
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 std::shared_ptr<DD4hepFieldAdapter> DD4hepDetectorBase::field() const {
0049 throw_assert(m_detector != nullptr, "Detector not initialized");
0050 return std::make_shared<DD4hepFieldAdapter>(m_detector->field());
0051 }
0052
0053 TGeoNode& DD4hepDetectorBase::tgeoGeometry() {
0054 return *m_detector->world().placement().ptr();
0055 }
0056
0057 std::unique_ptr<dd4hep::Detector> DD4hepDetectorBase::buildDD4hepGeometry(
0058 const Config& cfg) const {
0059 const int old_gErrorIgnoreLevel = gErrorIgnoreLevel;
0060
0061 switch (cfg.dd4hepLogLevel) {
0062 case Logging::Level::VERBOSE:
0063 dd4hep::setPrintLevel(dd4hep::PrintLevel::VERBOSE);
0064 break;
0065 case Logging::Level::DEBUG:
0066 dd4hep::setPrintLevel(dd4hep::PrintLevel::DEBUG);
0067 break;
0068 case Logging::Level::INFO:
0069 dd4hep::setPrintLevel(dd4hep::PrintLevel::INFO);
0070 break;
0071 case Logging::Level::WARNING:
0072 dd4hep::setPrintLevel(dd4hep::PrintLevel::WARNING);
0073 gErrorIgnoreLevel = kWarning;
0074 break;
0075 case Logging::Level::ERROR:
0076 dd4hep::setPrintLevel(dd4hep::PrintLevel::ERROR);
0077 gErrorIgnoreLevel = kError;
0078 break;
0079 case Logging::Level::FATAL:
0080 dd4hep::setPrintLevel(dd4hep::PrintLevel::FATAL);
0081 gErrorIgnoreLevel = kFatal;
0082 break;
0083 case Logging::Level::MAX:
0084 dd4hep::setPrintLevel(dd4hep::PrintLevel::ALWAYS);
0085 break;
0086 }
0087
0088 if (cfg.dd4hepLogLevel >= Logging::Level::WARNING) {
0089 std::cout.setstate(std::ios_base::failbit);
0090 }
0091
0092 std::unique_ptr<dd4hep::Detector> detector =
0093 dd4hep::Detector::make_unique(cfg.name);
0094 for (const auto& file : cfg.xmlFileNames) {
0095 detector->fromCompact(file);
0096 }
0097 detector->volumeManager();
0098 detector->apply("DD4hepVolumeManager", 0, nullptr);
0099
0100
0101 gErrorIgnoreLevel = old_gErrorIgnoreLevel;
0102 std::cout.clear();
0103
0104 return detector;
0105 }
0106
0107 DD4hepDetector::DD4hepDetector(const Config& cfg)
0108 : DD4hepDetectorBase{cfg}, m_cfg{cfg} {
0109 m_trackingGeometry = convertDD4hepDetector(
0110 m_detector->world(), logger(), m_cfg.bTypePhi, m_cfg.bTypeR, m_cfg.bTypeZ,
0111 m_cfg.envelopeR, m_cfg.envelopeZ, m_cfg.defaultLayerThickness,
0112 m_cfg.sortDetectors, m_nominalGeometryContext, m_cfg.materialDecorator,
0113 m_cfg.geometryIdentifierHook, m_cfg.detectorElementFactory);
0114 }
0115
0116 auto DD4hepDetector::config() const -> const Config& {
0117 return m_cfg;
0118 }
0119
0120 }
0121
0122 void ActsExamples::sortFCChhDetElements(std::vector<dd4hep::DetElement>& det) {
0123 std::vector<dd4hep::DetElement> tracker;
0124 std::vector<dd4hep::DetElement> eCal;
0125 std::vector<dd4hep::DetElement> hCal;
0126 std::vector<dd4hep::DetElement> muon;
0127 for (const auto& detElement : det) {
0128 std::string detName = detElement.name();
0129 if (detName.find("Muon") != std::string::npos) {
0130 muon.push_back(detElement);
0131 } else if (detName.find("ECal") != std::string::npos) {
0132 eCal.push_back(detElement);
0133 } else if (detName.find("HCal") != std::string::npos) {
0134 hCal.push_back(detElement);
0135 } else {
0136 tracker.push_back(detElement);
0137 }
0138 }
0139 std::ranges::sort(
0140 muon, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0141 return (a.id() < b.id());
0142 });
0143 std::ranges::sort(
0144 eCal, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0145 return (a.id() < b.id());
0146 });
0147 std::ranges::sort(
0148 hCal, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0149 return (a.id() < b.id());
0150 });
0151 std::ranges::sort(
0152 tracker, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0153 return (a.id() < b.id());
0154 });
0155 det.clear();
0156 det = tracker;
0157
0158 det.insert(det.end(), eCal.begin(), eCal.end());
0159 det.insert(det.end(), hCal.begin(), hCal.end());
0160 det.insert(det.end(), muon.begin(), muon.end());
0161 }