Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:44

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 
0014 #include <algorithm>
0015 #include <memory>
0016 #include <stdexcept>
0017 
0018 #include <DD4hep/DetElement.h>
0019 #include <DD4hep/Detector.h>
0020 #include <DD4hep/Handle.h>
0021 #include <DD4hep/Volumes.h>
0022 #include <Parsers/Printout.h>
0023 #include <TError.h>
0024 
0025 namespace ActsExamples {
0026 
0027 DD4hepDetector::DD4hepDetector(const Config& cfg)
0028     : Detector(Acts::getDefaultLogger("DD4hepDetector", cfg.logLevel)),
0029       m_cfg(cfg) {
0030   if (m_cfg.xmlFileNames.empty()) {
0031     throw std::invalid_argument("Missing DD4hep XML filenames");
0032   }
0033 
0034   m_nominalGeometryContext = Acts::GeometryContext();
0035 
0036   m_detector = buildDD4hepGeometry();
0037 
0038   auto logger = Acts::getDefaultLogger("DD4hepConversion", m_cfg.logLevel);
0039   m_trackingGeometry = Acts::convertDD4hepDetector(
0040       m_detector->world(), *logger, m_cfg.bTypePhi, m_cfg.bTypeR, m_cfg.bTypeZ,
0041       m_cfg.envelopeR, m_cfg.envelopeZ, m_cfg.defaultLayerThickness,
0042       m_cfg.sortDetectors, m_nominalGeometryContext, m_cfg.materialDecorator,
0043       m_cfg.geometryIdentifierHook);
0044 }
0045 
0046 dd4hep::Detector& DD4hepDetector::dd4hepDetector() {
0047   return *m_detector;
0048 }
0049 
0050 TGeoNode& DD4hepDetector::tgeoGeometry() {
0051   return *m_detector->world().placement().ptr();
0052 }
0053 
0054 std::unique_ptr<dd4hep::Detector> DD4hepDetector::buildDD4hepGeometry() const {
0055   const int old_gErrorIgnoreLevel = gErrorIgnoreLevel;
0056   switch (m_cfg.dd4hepLogLevel) {
0057     case Acts::Logging::Level::VERBOSE:
0058       dd4hep::setPrintLevel(dd4hep::PrintLevel::VERBOSE);
0059       break;
0060     case Acts::Logging::Level::DEBUG:
0061       dd4hep::setPrintLevel(dd4hep::PrintLevel::DEBUG);
0062       break;
0063     case Acts::Logging::Level::INFO:
0064       dd4hep::setPrintLevel(dd4hep::PrintLevel::INFO);
0065       break;
0066     case Acts::Logging::Level::WARNING:
0067       dd4hep::setPrintLevel(dd4hep::PrintLevel::WARNING);
0068       gErrorIgnoreLevel = kWarning;
0069       break;
0070     case Acts::Logging::Level::ERROR:
0071       dd4hep::setPrintLevel(dd4hep::PrintLevel::ERROR);
0072       gErrorIgnoreLevel = kError;
0073       break;
0074     case Acts::Logging::Level::FATAL:
0075       dd4hep::setPrintLevel(dd4hep::PrintLevel::FATAL);
0076       gErrorIgnoreLevel = kFatal;
0077       break;
0078     case Acts::Logging::Level::MAX:
0079       dd4hep::setPrintLevel(dd4hep::PrintLevel::ALWAYS);
0080       break;
0081   }
0082   // completely silence std::cout as DD4HEP is using it for logging
0083   if (m_cfg.dd4hepLogLevel >= Acts::Logging::Level::WARNING) {
0084     std::cout.setstate(std::ios_base::failbit);
0085   }
0086 
0087   std::unique_ptr<dd4hep::Detector> detector =
0088       dd4hep::Detector::make_unique(m_cfg.name);
0089   for (const auto& file : m_cfg.xmlFileNames) {
0090     detector->fromCompact(file);
0091   }
0092   detector->volumeManager();
0093   detector->apply("DD4hepVolumeManager", 0, nullptr);
0094 
0095   // restore the logging
0096   gErrorIgnoreLevel = old_gErrorIgnoreLevel;
0097   std::cout.clear();
0098 
0099   return detector;
0100 }
0101 
0102 }  // namespace ActsExamples
0103 
0104 void ActsExamples::sortFCChhDetElements(std::vector<dd4hep::DetElement>& det) {
0105   std::vector<dd4hep::DetElement> tracker;
0106   std::vector<dd4hep::DetElement> eCal;
0107   std::vector<dd4hep::DetElement> hCal;
0108   std::vector<dd4hep::DetElement> muon;
0109   for (const auto& detElement : det) {
0110     std::string detName = detElement.name();
0111     if (detName.find("Muon") != std::string::npos) {
0112       muon.push_back(detElement);
0113     } else if (detName.find("ECal") != std::string::npos) {
0114       eCal.push_back(detElement);
0115     } else if (detName.find("HCal") != std::string::npos) {
0116       hCal.push_back(detElement);
0117     } else {
0118       tracker.push_back(detElement);
0119     }
0120   }
0121   std::ranges::sort(
0122       muon, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0123         return (a.id() < b.id());
0124       });
0125   std::ranges::sort(
0126       eCal, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0127         return (a.id() < b.id());
0128       });
0129   std::ranges::sort(
0130       hCal, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0131         return (a.id() < b.id());
0132       });
0133   std::ranges::sort(
0134       tracker, [](const dd4hep::DetElement& a, const dd4hep::DetElement& b) {
0135         return (a.id() < b.id());
0136       });
0137   det.clear();
0138   det = tracker;
0139 
0140   det.insert(det.end(), eCal.begin(), eCal.end());
0141   det.insert(det.end(), hCal.begin(), hCal.end());
0142   det.insert(det.end(), muon.begin(), muon.end());
0143 }