Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:02:46

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