Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 07:50:28

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/DD4hepDetector/DD4hepDetector.hpp"
0010 
0011 #include "Acts/Plugins/DD4hep/ConvertDD4hepDetector.hpp"
0012 #include "Acts/Utilities/Logger.hpp"
0013 #include "Acts/Utilities/ThrowAssert.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 namespace ActsExamples {
0027 
0028 DD4hepDetector::DD4hepDetector(const Config& cfg)
0029     : Detector(Acts::getDefaultLogger("DD4hepDetector", cfg.logLevel)),
0030       m_cfg(cfg) {
0031   if (m_cfg.xmlFileNames.empty()) {
0032     throw std::invalid_argument("Missing DD4hep XML filenames");
0033   }
0034 
0035   m_nominalGeometryContext = Acts::GeometryContext();
0036 
0037   m_detector = buildDD4hepGeometry();
0038 
0039   if (m_cfg.alignmentDecorator != nullptr) {
0040     m_contextDecorators = {m_cfg.alignmentDecorator};
0041   }
0042 
0043   auto logger = Acts::getDefaultLogger("DD4hepConversion", m_cfg.logLevel);
0044   m_trackingGeometry = Acts::convertDD4hepDetector(
0045       m_detector->world(), *logger, m_cfg.bTypePhi, m_cfg.bTypeR, m_cfg.bTypeZ,
0046       m_cfg.envelopeR, m_cfg.envelopeZ, m_cfg.defaultLayerThickness,
0047       m_cfg.sortDetectors, m_nominalGeometryContext, m_cfg.materialDecorator,
0048       m_cfg.geometryIdentifierHook, m_cfg.detectorElementFactory);
0049 }
0050 
0051 dd4hep::Detector& DD4hepDetector::dd4hepDetector() {
0052   return *m_detector;
0053 }
0054 
0055 std::shared_ptr<Acts::DD4hepFieldAdapter> DD4hepDetector::field() const {
0056   throw_assert(m_detector != nullptr, "Detector not initialized");
0057   return std::make_shared<Acts::DD4hepFieldAdapter>(m_detector->field());
0058 }
0059 
0060 TGeoNode& DD4hepDetector::tgeoGeometry() {
0061   return *m_detector->world().placement().ptr();
0062 }
0063 
0064 std::unique_ptr<dd4hep::Detector> DD4hepDetector::buildDD4hepGeometry() const {
0065   const int old_gErrorIgnoreLevel = gErrorIgnoreLevel;
0066   switch (m_cfg.dd4hepLogLevel) {
0067     case Acts::Logging::Level::VERBOSE:
0068       dd4hep::setPrintLevel(dd4hep::PrintLevel::VERBOSE);
0069       break;
0070     case Acts::Logging::Level::DEBUG:
0071       dd4hep::setPrintLevel(dd4hep::PrintLevel::DEBUG);
0072       break;
0073     case Acts::Logging::Level::INFO:
0074       dd4hep::setPrintLevel(dd4hep::PrintLevel::INFO);
0075       break;
0076     case Acts::Logging::Level::WARNING:
0077       dd4hep::setPrintLevel(dd4hep::PrintLevel::WARNING);
0078       gErrorIgnoreLevel = kWarning;
0079       break;
0080     case Acts::Logging::Level::ERROR:
0081       dd4hep::setPrintLevel(dd4hep::PrintLevel::ERROR);
0082       gErrorIgnoreLevel = kError;
0083       break;
0084     case Acts::Logging::Level::FATAL:
0085       dd4hep::setPrintLevel(dd4hep::PrintLevel::FATAL);
0086       gErrorIgnoreLevel = kFatal;
0087       break;
0088     case Acts::Logging::Level::MAX:
0089       dd4hep::setPrintLevel(dd4hep::PrintLevel::ALWAYS);
0090       break;
0091   }
0092   // completely silence std::cout as DD4HEP is using it for logging
0093   if (m_cfg.dd4hepLogLevel >= Acts::Logging::Level::WARNING) {
0094     std::cout.setstate(std::ios_base::failbit);
0095   }
0096 
0097   std::unique_ptr<dd4hep::Detector> detector =
0098       dd4hep::Detector::make_unique(m_cfg.name);
0099   for (const auto& file : m_cfg.xmlFileNames) {
0100     detector->fromCompact(file);
0101   }
0102   detector->volumeManager();
0103   detector->apply("DD4hepVolumeManager", 0, nullptr);
0104 
0105   // restore the logging
0106   gErrorIgnoreLevel = old_gErrorIgnoreLevel;
0107   std::cout.clear();
0108 
0109   return detector;
0110 }
0111 
0112 }  // namespace ActsExamples
0113 
0114 void ActsExamples::sortFCChhDetElements(std::vector<dd4hep::DetElement>& det) {
0115   std::vector<dd4hep::DetElement> tracker;
0116   std::vector<dd4hep::DetElement> eCal;
0117   std::vector<dd4hep::DetElement> hCal;
0118   std::vector<dd4hep::DetElement> muon;
0119   for (const auto& detElement : det) {
0120     std::string detName = detElement.name();
0121     if (detName.find("Muon") != std::string::npos) {
0122       muon.push_back(detElement);
0123     } else if (detName.find("ECal") != std::string::npos) {
0124       eCal.push_back(detElement);
0125     } else if (detName.find("HCal") != std::string::npos) {
0126       hCal.push_back(detElement);
0127     } else {
0128       tracker.push_back(detElement);
0129     }
0130   }
0131   std::ranges::sort(
0132       muon, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0133         return (a.id() < b.id());
0134       });
0135   std::ranges::sort(
0136       eCal, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0137         return (a.id() < b.id());
0138       });
0139   std::ranges::sort(
0140       hCal, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0141         return (a.id() < b.id());
0142       });
0143   std::ranges::sort(
0144       tracker, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0145         return (a.id() < b.id());
0146       });
0147   det.clear();
0148   det = tracker;
0149 
0150   det.insert(det.end(), eCal.begin(), eCal.end());
0151   det.insert(det.end(), hCal.begin(), hCal.end());
0152   det.insert(det.end(), muon.begin(), muon.end());
0153 }