File indexing completed on 2025-07-02 07:54:33
0001
0002
0003
0004 #include <Acts/Geometry/DetectorElementBase.hpp>
0005 #include <Acts/Geometry/GeometryIdentifier.hpp>
0006 #include <Acts/Geometry/TrackingGeometry.hpp>
0007 #include <Acts/Geometry/TrackingVolume.hpp>
0008 #include <Acts/MagneticField/MagneticFieldContext.hpp>
0009 #include <Acts/Material/IMaterialDecorator.hpp>
0010 #include <Acts/Plugins/DD4hep/ConvertDD4hepDetector.hpp>
0011 #include <Acts/Plugins/DD4hep/DD4hepDetectorElement.hpp>
0012 #include <Acts/Plugins/Json/JsonMaterialDecorator.hpp>
0013 #include <Acts/Plugins/Json/MaterialMapJsonConverter.hpp>
0014 #include <Acts/Surfaces/Surface.hpp>
0015 #include <Acts/Utilities/BinningType.hpp>
0016 #include <Acts/Utilities/Result.hpp>
0017 #include <Acts/Visualization/GeometryView3D.hpp>
0018 #include <Acts/Visualization/ObjVisualization3D.hpp>
0019 #include <Acts/Visualization/PlyVisualization3D.hpp>
0020 #include <DD4hep/DetElement.h>
0021 #include <DD4hep/VolumeManager.h>
0022 #include <JANA/JException.h>
0023 #include <TGeoManager.h>
0024 #include <fmt/core.h>
0025 #include <fmt/ostream.h>
0026 #include <spdlog/common.h>
0027 #include <exception>
0028 #include <initializer_list>
0029 #include <type_traits>
0030
0031 #include "ActsGeometryProvider.h"
0032 #include "DD4hepBField.h"
0033 #include "extensions/spdlog/SpdlogToActs.h"
0034
0035
0036 #if FMT_VERSION >= 90000
0037 #include <Eigen/Core>
0038
0039 template <typename T>
0040 struct fmt::formatter<T, std::enable_if_t<std::is_base_of_v<Eigen::MatrixBase<T>, T>, char>>
0041 : fmt::ostream_formatter {};
0042 #endif
0043
0044 void ActsGeometryProvider::initialize(const dd4hep::Detector* dd4hep_geo, std::string material_file,
0045 std::shared_ptr<spdlog::logger> log,
0046 std::shared_ptr<spdlog::logger> init_log) {
0047
0048 m_log = log;
0049 m_init_log = init_log;
0050
0051 m_init_log->debug("ActsGeometryProvider initializing...");
0052
0053 m_init_log->debug("Set TGeoManager and acts_init_log_level log levels");
0054
0055 if (m_log->level() >= (int)spdlog::level::info) {
0056 TGeoManager::SetVerboseLevel(0);
0057 }
0058
0059
0060 auto acts_init_log_level = eicrecon::SpdlogToActsLevel(m_init_log->level());
0061
0062 m_dd4hepDetector = dd4hep_geo;
0063
0064
0065 std::shared_ptr<const Acts::IMaterialDecorator> materialDeco{nullptr};
0066 if (!material_file.empty()) {
0067 m_init_log->info("loading materials map from file: '{}'", material_file);
0068
0069 Acts::MaterialMapJsonConverter::Config jsonGeoConvConfig;
0070
0071 materialDeco = std::make_shared<const Acts::JsonMaterialDecorator>(
0072 jsonGeoConvConfig, material_file, acts_init_log_level);
0073 }
0074
0075
0076 class ConvertDD4hepDetectorGeometryIdentifierHook : public Acts::GeometryIdentifierHook {
0077 Acts::GeometryIdentifier decorateIdentifier(Acts::GeometryIdentifier identifier,
0078 const Acts::Surface& surface) const override {
0079 const auto* dd4hep_det_element =
0080 dynamic_cast<const Acts::DD4hepDetectorElement*>(surface.associatedDetectorElement());
0081 if (dd4hep_det_element == nullptr) {
0082 return identifier;
0083 }
0084
0085 #if Acts_VERSION_MAJOR >= 40
0086 return identifier.withExtra(0xff & dd4hep_det_element->identifier());
0087 #else
0088 return identifier.setExtra(0xff & dd4hep_det_element->identifier());
0089 #endif
0090 };
0091 };
0092 auto geometryIdHook = std::make_shared<ConvertDD4hepDetectorGeometryIdentifierHook>();
0093
0094
0095 m_init_log->info("Converting DD4Hep geometry to ACTS...");
0096 auto logger = eicrecon::getSpdlogLogger("CONV", m_log);
0097 Acts::BinningType bTypePhi = Acts::equidistant;
0098 Acts::BinningType bTypeR = Acts::equidistant;
0099 Acts::BinningType bTypeZ = Acts::equidistant;
0100 double layerEnvelopeR = Acts::UnitConstants::mm;
0101 double layerEnvelopeZ = Acts::UnitConstants::mm;
0102 double defaultLayerThickness = Acts::UnitConstants::fm;
0103 using Acts::sortDetElementsByID;
0104
0105 try {
0106 m_trackingGeo = Acts::convertDD4hepDetector(m_dd4hepDetector->world(), *logger, bTypePhi,
0107 bTypeR, bTypeZ, layerEnvelopeR, layerEnvelopeZ,
0108 defaultLayerThickness, sortDetElementsByID,
0109 m_trackingGeoCtx, materialDeco, geometryIdHook);
0110 } catch (std::exception& ex) {
0111 m_init_log->error("Error during DD4Hep -> ACTS geometry conversion: {}", ex.what());
0112 m_init_log->info("Set parameter acts::InitLogLevel=trace to see conversion info and possibly "
0113 "identify failing geometry");
0114 throw JException(ex.what());
0115 }
0116
0117 m_init_log->info("DD4Hep geometry converted!");
0118
0119
0120 m_init_log->info("Checking surfaces...");
0121 if (m_trackingGeo) {
0122
0123 const Acts::TrackingVolume* world = m_trackingGeo->highestTrackingVolume();
0124 if (m_objWriteIt) {
0125 m_init_log->info("Writing obj files to {}...", m_outputDir);
0126 Acts::ObjVisualization3D objVis;
0127 Acts::GeometryView3D::drawTrackingVolume(objVis, *world, m_trackingGeoCtx, m_containerView,
0128 m_volumeView, m_passiveView, m_sensitiveView,
0129 m_gridView, m_objWriteIt, m_outputTag, m_outputDir);
0130 }
0131 if (m_plyWriteIt) {
0132 m_init_log->info("Writing ply files to {}...", m_outputDir);
0133 Acts::PlyVisualization3D plyVis;
0134 Acts::GeometryView3D::drawTrackingVolume(plyVis, *world, m_trackingGeoCtx, m_containerView,
0135 m_volumeView, m_passiveView, m_sensitiveView,
0136 m_gridView, m_plyWriteIt, m_outputTag, m_outputDir);
0137 }
0138
0139 m_init_log->debug("visiting all the surfaces ");
0140 m_trackingGeo->visitSurfaces([this](const Acts::Surface* surface) {
0141
0142 if (surface == nullptr) {
0143 m_init_log->info("no surface??? ");
0144 return;
0145 }
0146 const auto* det_element =
0147 dynamic_cast<const Acts::DD4hepDetectorElement*>(surface->associatedDetectorElement());
0148
0149 if (det_element == nullptr) {
0150 m_init_log->error("invalid det_element!!! det_element == nullptr ");
0151 return;
0152 }
0153
0154
0155 m_init_log->debug(" det_element->identifier() = {} ", det_element->identifier());
0156 auto volman = m_dd4hepDetector->volumeManager();
0157 auto* vol_ctx = volman.lookupContext(det_element->identifier());
0158 auto vol_id = vol_ctx->identifier;
0159
0160 if (m_init_log->level() <= spdlog::level::debug) {
0161 auto de = vol_ctx->element;
0162 m_init_log->debug(" de.path = {}", de.path());
0163 m_init_log->debug(" de.placementPath = {}", de.placementPath());
0164 }
0165
0166 this->m_surfaces.insert_or_assign(vol_id, surface);
0167 });
0168 } else {
0169 m_init_log->error("m_trackingGeo==null why am I still alive???");
0170 }
0171
0172
0173 m_init_log->info("Loading magnetic field...");
0174 m_magneticField = std::make_shared<const eicrecon::BField::DD4hepBField>(m_dd4hepDetector);
0175 Acts::MagneticFieldContext m_fieldctx{eicrecon::BField::BFieldVariant(m_magneticField)};
0176 auto bCache = m_magneticField->makeCache(m_fieldctx);
0177 for (int z : {0, 500, 1000, 1500, 2000, 3000, 4000}) {
0178 auto b = m_magneticField->getField({0.0, 0.0, double(z)}, bCache).value();
0179 m_init_log->debug("B(z = {:>5} [mm]) = {} T", z, b.transpose() / Acts::UnitConstants::T);
0180 }
0181
0182 m_init_log->info("ActsGeometryProvider initialization complete");
0183 }
0184
0185 std::shared_ptr<const Acts::MagneticFieldProvider> ActsGeometryProvider::getFieldProvider() const {
0186 return m_magneticField;
0187 }