Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Core/src/Geometry/CuboidVolumeBuilder.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "Acts/Geometry/CuboidVolumeBuilder.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/BoundarySurfaceFace.hpp"
0013 #include "Acts/Geometry/CuboidVolumeBounds.hpp"
0014 #include "Acts/Geometry/Extent.hpp"
0015 #include "Acts/Geometry/LayerArrayCreator.hpp"
0016 #include "Acts/Geometry/LayerCreator.hpp"
0017 #include "Acts/Geometry/ProtoLayer.hpp"
0018 #include "Acts/Geometry/SurfaceArrayCreator.hpp"
0019 #include "Acts/Geometry/TrackingGeometry.hpp"
0020 #include "Acts/Geometry/TrackingVolume.hpp"
0021 #include "Acts/Geometry/Volume.hpp"
0022 #include "Acts/Surfaces/PlaneSurface.hpp"
0023 #include "Acts/Surfaces/RectangleBounds.hpp"
0024 #include "Acts/Surfaces/Surface.hpp"
0025 #include "Acts/Utilities/BinUtility.hpp"
0026 #include "Acts/Utilities/BinnedArrayXD.hpp"
0027 #include "Acts/Utilities/BinningData.hpp"
0028 #include "Acts/Utilities/Logger.hpp"
0029 
0030 #include <algorithm>
0031 #include <limits>
0032 #include <stdexcept>
0033 
0034 namespace Acts {
0035 
0036 std::shared_ptr<const Surface> CuboidVolumeBuilder::buildSurface(
0037     const GeometryContext& /*gctx*/,
0038     const CuboidVolumeBuilder::SurfaceConfig& cfg) const {
0039   std::shared_ptr<PlaneSurface> surface;
0040 
0041   // Build transformation
0042   Transform3 trafo(Transform3::Identity() * cfg.rotation);
0043   trafo.translation() = cfg.position;
0044 
0045   // Create and store surface
0046   if (cfg.detElementConstructor) {
0047     surface = Surface::makeShared<PlaneSurface>(
0048         cfg.rBounds,
0049         *(cfg.detElementConstructor(trafo, cfg.rBounds, cfg.thickness)));
0050   } else {
0051     surface = Surface::makeShared<PlaneSurface>(trafo, cfg.rBounds);
0052   }
0053   surface->assignSurfaceMaterial(cfg.surMat);
0054   return surface;
0055 }
0056 
0057 std::shared_ptr<const Layer> CuboidVolumeBuilder::buildLayer(
0058     const GeometryContext& gctx, CuboidVolumeBuilder::LayerConfig& cfg) const {
0059   if (cfg.surfaces.empty() && cfg.surfaceCfg.empty()) {
0060     throw std::runtime_error{
0061         "Neither surfaces nor config to build surfaces was provided. Cannot "
0062         "proceed"};
0063   }
0064 
0065   // Build the surface
0066   if (cfg.surfaces.empty()) {
0067     for (const auto& sCfg : cfg.surfaceCfg) {
0068       cfg.surfaces.push_back(buildSurface(gctx, sCfg));
0069     }
0070   }
0071   // Build transformation centered at the surface position
0072   Vector3 centroid{0., 0., 0.};
0073 
0074   for (const auto& surface : cfg.surfaces) {
0075     centroid += surface->transform(gctx).translation();
0076   }
0077 
0078   centroid /= cfg.surfaces.size();
0079 
0080   // In the case the layer configuration doesn't define the rotation of the
0081   // layer use the orientation of the first surface to define the layer rotation
0082   // in space.
0083   Transform3 trafo = Transform3::Identity();
0084   trafo.translation() = centroid;
0085   if (cfg.rotation) {
0086     trafo.linear() = *cfg.rotation;
0087   } else {
0088     trafo.linear() = cfg.surfaces.front()->transform(gctx).rotation();
0089   }
0090 
0091   LayerCreator::Config lCfg;
0092   lCfg.surfaceArrayCreator = std::make_shared<const SurfaceArrayCreator>();
0093   LayerCreator layerCreator(lCfg);
0094   ProtoLayer pl{gctx, cfg.surfaces};
0095   pl.envelope[AxisDirection::AxisX] = cfg.envelopeX;
0096   pl.envelope[AxisDirection::AxisY] = cfg.envelopeY;
0097   pl.envelope[AxisDirection::AxisZ] = cfg.envelopeZ;
0098   return layerCreator.planeLayer(gctx, cfg.surfaces, cfg.binsY, cfg.binsZ,
0099                                  cfg.binningDimension, pl, trafo);
0100 }
0101 
0102 std::pair<double, double> CuboidVolumeBuilder::binningRange(
0103     const GeometryContext& gctx,
0104     const CuboidVolumeBuilder::VolumeConfig& cfg) const {
0105   using namespace UnitLiterals;
0106   // Construct return value
0107   std::pair<double, double> minMax = std::make_pair(
0108       std::numeric_limits<double>::max(), -std::numeric_limits<double>::max());
0109 
0110   // Compute the min volume boundaries for computing the binning start
0111   // See
0112   // https://acts.readthedocs.io/en/latest/core/geometry/legacy/building.html
0113   // !! IMPORTANT !! The volume is assumed to be already rotated into the
0114   // telescope geometry
0115   Vector3 minVolumeBoundaries = cfg.position - 0.5 * cfg.length;
0116   Vector3 maxVolumeBoundaries = cfg.position + 0.5 * cfg.length;
0117 
0118   // Compute first the min-max from the layers
0119 
0120   for (const auto& layercfg : cfg.layerCfg) {
0121     // recreating the protolayer for each layer => slow, but only few sensors
0122     ProtoLayer pl{gctx, layercfg.surfaces};
0123     pl.envelope[cfg.binningDimension] = layercfg.envelopeX;
0124 
0125     double surfacePosMin = pl.min(cfg.binningDimension);
0126     double surfacePosMax = pl.max(cfg.binningDimension);
0127 
0128     // Test if new extreme is found and set it
0129     if (surfacePosMin < minMax.first) {
0130       minMax.first = surfacePosMin;
0131     }
0132     if (surfacePosMax > minMax.second) {
0133       minMax.second = surfacePosMax;
0134     }
0135   }
0136 
0137   // Use the volume boundaries as limits for the binning
0138   minMax.first = std::min(
0139       minMax.first, minVolumeBoundaries(toUnderlying(cfg.binningDimension)));
0140   minMax.second = std::max(
0141       minMax.second, maxVolumeBoundaries(toUnderlying(cfg.binningDimension)));
0142 
0143   return minMax;
0144 }
0145 
0146 std::shared_ptr<TrackingVolume> CuboidVolumeBuilder::buildVolume(
0147     const GeometryContext& gctx, CuboidVolumeBuilder::VolumeConfig& cfg) const {
0148   // Build transformation
0149   Transform3 trafo(Transform3::Identity());
0150   trafo.translation() = cfg.position;
0151   // Set bounds
0152   auto bounds = std::make_shared<CuboidVolumeBounds>(
0153       cfg.length.x() * 0.5, cfg.length.y() * 0.5, cfg.length.z() * 0.5);
0154 
0155   // Gather the layers
0156   LayerVector layVec;
0157   if (cfg.layers.empty()) {
0158     cfg.layers.reserve(cfg.layerCfg.size());
0159 
0160     for (auto& layerCfg : cfg.layerCfg) {
0161       cfg.layers.push_back(buildLayer(gctx, layerCfg));
0162       layVec.push_back(cfg.layers.back());
0163     }
0164   } else {
0165     for (auto& lay : cfg.layers) {
0166       layVec.push_back(lay);
0167     }
0168   }
0169 
0170   // Build layer array
0171   std::pair<double, double> minMax = binningRange(gctx, cfg);
0172   LayerArrayCreator::Config lacCnf;
0173   LayerArrayCreator layArrCreator(
0174       lacCnf, getDefaultLogger("LayerArrayCreator", Logging::INFO));
0175   std::unique_ptr<const LayerArray> layArr(
0176       layArrCreator.layerArray(gctx, layVec, minMax.first, minMax.second,
0177                                BinningType::arbitrary, cfg.binningDimension));
0178 
0179   // Build confined volumes
0180   if (cfg.trackingVolumes.empty()) {
0181     for (VolumeConfig vc : cfg.volumeCfg) {
0182       cfg.trackingVolumes.push_back(buildVolume(gctx, vc));
0183     }
0184   }
0185 
0186   std::shared_ptr<TrackingVolume> trackVolume;
0187   if (layVec.empty()) {
0188     // Build TrackingVolume
0189     trackVolume = std::make_shared<TrackingVolume>(
0190         trafo, bounds, cfg.volumeMaterial, nullptr, nullptr,
0191         cfg.trackingVolumes, cfg.name);
0192   } else {
0193     // Build TrackingVolume
0194     trackVolume = std::make_shared<TrackingVolume>(
0195         trafo, bounds, cfg.volumeMaterial, std::move(layArr), nullptr,
0196         cfg.trackingVolumes, cfg.name);
0197   }
0198   return trackVolume;
0199 }
0200 
0201 MutableTrackingVolumePtr CuboidVolumeBuilder::trackingVolume(
0202     const GeometryContext& gctx, TrackingVolumePtr /*gctx*/,
0203     std::shared_ptr<const VolumeBounds> /*bounds*/) const {
0204   // Build volumes
0205   std::vector<std::shared_ptr<TrackingVolume>> volumes;
0206   volumes.reserve(m_cfg.volumeCfg.size());
0207   for (VolumeConfig volCfg : m_cfg.volumeCfg) {
0208     volumes.push_back(buildVolume(gctx, volCfg));
0209   }
0210 
0211   // Sort the volumes vectors according to the center location, otherwise the
0212   // binning boundaries will fail
0213   std::ranges::sort(volumes, {}, [](const auto& v) { return v->center().x(); });
0214 
0215   // Glue volumes
0216   for (unsigned int i = 0; i < volumes.size() - 1; i++) {
0217     volumes[i + 1]->glueTrackingVolume(
0218         gctx, BoundarySurfaceFace::negativeFaceYZ, volumes[i].get(),
0219         BoundarySurfaceFace::positiveFaceYZ);
0220     volumes[i]->glueTrackingVolume(gctx, BoundarySurfaceFace::positiveFaceYZ,
0221                                    volumes[i + 1].get(),
0222                                    BoundarySurfaceFace::negativeFaceYZ);
0223   }
0224 
0225   // Translation
0226   Transform3 trafo(Transform3::Identity());
0227   trafo.translation() = m_cfg.position;
0228 
0229   // Size of the volume
0230   auto volumeBounds = std::make_shared<CuboidVolumeBounds>(
0231       m_cfg.length.x() * 0.5, m_cfg.length.y() * 0.5, m_cfg.length.z() * 0.5);
0232 
0233   // Build vector of confined volumes
0234   std::vector<std::pair<TrackingVolumePtr, Vector3>> tapVec;
0235   tapVec.reserve(m_cfg.volumeCfg.size());
0236   for (auto& tVol : volumes) {
0237     tapVec.push_back(std::make_pair(tVol, tVol->center()));
0238   }
0239 
0240   // Set bin boundaries along binning
0241   std::vector<float> binBoundaries;
0242   binBoundaries.push_back(volumes[0]->center().x() -
0243                           m_cfg.volumeCfg[0].length.x() * 0.5);
0244   for (std::size_t i = 0; i < volumes.size(); i++) {
0245     binBoundaries.push_back(volumes[i]->center().x() +
0246                             m_cfg.volumeCfg[i].length.x() * 0.5);
0247   }
0248 
0249   // Build binning
0250   BinningData binData(BinningOption::open, AxisDirection::AxisX, binBoundaries);
0251   auto bu = std::make_unique<const BinUtility>(binData);
0252 
0253   // Build TrackingVolume array
0254   std::shared_ptr<const TrackingVolumeArray> trVolArr(
0255       new BinnedArrayXD<TrackingVolumePtr>(tapVec, std::move(bu)));
0256 
0257   // Create world volume
0258   MutableTrackingVolumePtr mtvp(std::make_shared<TrackingVolume>(
0259       trafo, volumeBounds, nullptr, nullptr, trVolArr,
0260       MutableTrackingVolumeVector{}, "World"));
0261 
0262   return mtvp;
0263 }
0264 
0265 }  // namespace Acts