Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Core/src/Geometry/SurfaceArrayCreator.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/SurfaceArrayCreator.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Tolerance.hpp"
0013 #include "Acts/Surfaces/CylinderSurface.hpp"
0014 #include "Acts/Surfaces/DiscSurface.hpp"
0015 #include "Acts/Surfaces/PlanarBounds.hpp"
0016 #include "Acts/Surfaces/PlaneSurface.hpp"
0017 #include "Acts/Surfaces/RectangleBounds.hpp"
0018 #include "Acts/Surfaces/Surface.hpp"
0019 #include "Acts/Surfaces/SurfaceArray.hpp"
0020 #include "Acts/Utilities/AxisDefinitions.hpp"
0021 #include "Acts/Utilities/BinningType.hpp"
0022 #include "Acts/Utilities/Helpers.hpp"
0023 
0024 #include <algorithm>
0025 #include <numbers>
0026 #include <stdexcept>
0027 
0028 namespace Acts {
0029 
0030 using VectorHelpers::perp;
0031 using VectorHelpers::phi;
0032 
0033 namespace {
0034 
0035 /// Bin edges are derived from the surfaces but consumed as local coordinates of
0036 /// the representative surface. Subtracting its translation puts them in the
0037 /// same frame. Phi is rotated by @c createEquidistantAxis instead, and R is
0038 /// invariant under the translations applied here.
0039 double axisFrameOffset(AxisDirection aDir, const Transform3& transform) {
0040   using enum AxisDirection;
0041   switch (aDir) {
0042     case AxisX:
0043       return transform.translation().x();
0044     case AxisY:
0045       return transform.translation().y();
0046     case AxisZ:
0047       return transform.translation().z();
0048     default:
0049       return 0.;
0050   }
0051 }
0052 
0053 }  // namespace
0054 
0055 SurfaceArray SurfaceArrayCreator::surfaceArrayOnCylinder(
0056     const GeometryContext& gctx,
0057     std::vector<std::shared_ptr<const Surface>> surfaces, std::size_t binsPhi,
0058     std::size_t binsZ, std::optional<ProtoLayer> protoLayerOpt,
0059     const Transform3& transform, SurfaceArray::NeighborWindow neighborWindow,
0060     std::uint8_t overfill) const {
0061   using enum AxisDirection;
0062 
0063   const std::vector<const Surface*> surfacesRaw = unpackSmartPointers(surfaces);
0064   // Check if we have proto layer, else build it
0065   const ProtoLayer protoLayer =
0066       protoLayerOpt ? *protoLayerOpt : ProtoLayer(gctx, surfacesRaw);
0067 
0068   ACTS_VERBOSE("Creating a SurfaceArray on a cylinder");
0069   ACTS_VERBOSE(" -- with " << surfaces.size() << " surfaces.");
0070   ACTS_VERBOSE(" -- with phi x z  = " << binsPhi << " x " << binsZ << " = "
0071                                       << binsPhi * binsZ << " bins.");
0072 
0073   Transform3 fullTransform = transform;
0074   const auto pAxisPhi =
0075       createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0076                             AxisPhi, protoLayer, fullTransform, binsPhi);
0077   const auto pAxisZ =
0078       createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound, AxisZ,
0079                             protoLayer, fullTransform, binsZ);
0080 
0081   const double R = protoLayer.medium(AxisR, true);
0082   const double halfZ = protoLayer.range(AxisZ, true) * 0.5;
0083   const double layerTolerance = protoLayer.range(AxisR) * 0.5;
0084 
0085   auto surface = Surface::makeShared<CylinderSurface>(fullTransform, R, halfZ);
0086   ACTS_VERBOSE("- projection surface is: " << surface->toString(gctx));
0087 
0088   return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0089                       layerTolerance, {*pAxisPhi, *pAxisZ}, neighborWindow,
0090                       overfill);
0091 }
0092 
0093 SurfaceArray SurfaceArrayCreator::surfaceArrayOnCylinder(
0094     const GeometryContext& gctx,
0095     std::vector<std::shared_ptr<const Surface>> surfaces, BinningType bTypePhi,
0096     BinningType bTypeZ, std::optional<ProtoLayer> protoLayerOpt,
0097     const Transform3& transform, SurfaceArray::NeighborWindow neighborWindow,
0098     std::uint8_t overfill) const {
0099   using enum AxisDirection;
0100 
0101   const std::vector<const Surface*> surfacesRaw = unpackSmartPointers(surfaces);
0102   // check if we have proto layer, else build it
0103   const ProtoLayer protoLayer =
0104       protoLayerOpt ? *protoLayerOpt : ProtoLayer(gctx, surfacesRaw);
0105 
0106   const double R = protoLayer.medium(AxisR, true);
0107   const double halfZ = protoLayer.range(AxisZ, true) * 0.5;
0108   const double layerTolerance = protoLayer.range(AxisR) * 0.5;
0109 
0110   std::unique_ptr<const IAxis> pAxisPhi;
0111   std::unique_ptr<const IAxis> pAxisZ;
0112 
0113   Transform3 fullTransform = transform;
0114 
0115   if (bTypePhi == equidistant) {
0116     pAxisPhi =
0117         createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0118                               AxisPhi, protoLayer, fullTransform, 0);
0119   } else {
0120     pAxisPhi = createVariableAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0121                                   AxisPhi, protoLayer, fullTransform);
0122   }
0123 
0124   if (bTypeZ == equidistant) {
0125     pAxisZ = createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0126                                    AxisZ, protoLayer, fullTransform);
0127   } else {
0128     pAxisZ = createVariableAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0129                                 AxisZ, protoLayer, fullTransform);
0130   }
0131 
0132   auto surface = Surface::makeShared<CylinderSurface>(fullTransform, R, halfZ);
0133 
0134   const std::size_t bins0 = pAxisPhi->getNBins();
0135   const std::size_t bins1 = pAxisZ->getNBins();
0136   ACTS_VERBOSE("Creating a SurfaceArray on a cylinder");
0137   ACTS_VERBOSE(" -- with " << surfaces.size() << " surfaces.");
0138   ACTS_VERBOSE(" -- with phi x z  = " << bins0 << " x " << bins1 << " = "
0139                                       << bins0 * bins1 << " bins.");
0140 
0141   return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0142                       layerTolerance, {*pAxisPhi, *pAxisZ}, neighborWindow,
0143                       overfill);
0144 }
0145 
0146 SurfaceArray SurfaceArrayCreator::surfaceArrayOnDisc(
0147     const GeometryContext& gctx,
0148     std::vector<std::shared_ptr<const Surface>> surfaces, std::size_t binsR,
0149     std::size_t binsPhi, std::optional<ProtoLayer> protoLayerOpt,
0150     const Transform3& transform, SurfaceArray::NeighborWindow neighborWindow,
0151     std::uint8_t overfill) const {
0152   using enum AxisDirection;
0153 
0154   const std::vector<const Surface*> surfacesRaw = unpackSmartPointers(surfaces);
0155   // check if we have proto layer, else build it
0156   const ProtoLayer protoLayer =
0157       protoLayerOpt ? *protoLayerOpt : ProtoLayer(gctx, surfacesRaw);
0158 
0159   ACTS_VERBOSE("Creating a SurfaceArray on a disc");
0160 
0161   Transform3 fullTransform = transform;
0162   const auto pAxisR =
0163       createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound, AxisR,
0164                             protoLayer, fullTransform, binsR);
0165   const auto pAxisPhi =
0166       createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0167                             AxisPhi, protoLayer, fullTransform, binsPhi);
0168 
0169   const double Z = protoLayer.medium(AxisZ, true);
0170   const double Rmin = protoLayer.min(AxisR, true);
0171   const double Rmax = protoLayer.max(AxisR, true);
0172   const double layerThickness = protoLayer.range(AxisZ) * 0.5;
0173   ACTS_VERBOSE("- z-position of disc estimated as " << Z);
0174   ACTS_VERBOSE("- full transform is \n" << fullTransform.matrix());
0175 
0176   if (fullTransform.translation().norm() < s_transformEquivalentTolerance) {
0177     ACTS_VERBOSE(
0178         "input transform does not have translation: putting projection surface "
0179         "at center of gravity in z");
0180     fullTransform.translate(Vector3::UnitZ() * Z);
0181   }
0182 
0183   auto surface = Surface::makeShared<DiscSurface>(fullTransform, Rmin, Rmax);
0184   ACTS_VERBOSE("- projection surface is: " << surface->toString(gctx));
0185 
0186   const std::size_t bins0 = pAxisR->getNBins();
0187   const std::size_t bins1 = pAxisPhi->getNBins();
0188   ACTS_VERBOSE(" -- with " << surfaces.size() << " surfaces.");
0189   ACTS_VERBOSE(" -- with r x phi  = " << bins0 << " x " << bins1 << " = "
0190                                       << bins0 * bins1 << " bins.");
0191 
0192   return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0193                       layerThickness, {*pAxisR, *pAxisPhi}, neighborWindow,
0194                       overfill);
0195 }
0196 
0197 SurfaceArray SurfaceArrayCreator::surfaceArrayOnDisc(
0198     const GeometryContext& gctx,
0199     std::vector<std::shared_ptr<const Surface>> surfaces, BinningType bTypeR,
0200     BinningType bTypePhi, std::optional<ProtoLayer> protoLayerOpt,
0201     const Transform3& transform, SurfaceArray::NeighborWindow neighborWindow,
0202     std::uint8_t overfill) const {
0203   using enum AxisDirection;
0204 
0205   const std::vector<const Surface*> surfacesRaw = unpackSmartPointers(surfaces);
0206   // check if we have proto layer, else build it
0207   const ProtoLayer protoLayer =
0208       protoLayerOpt ? *protoLayerOpt : ProtoLayer(gctx, surfacesRaw);
0209 
0210   ACTS_VERBOSE("Creating a SurfaceArray on a disc");
0211 
0212   std::unique_ptr<const IAxis> pAxisPhi;
0213   std::unique_ptr<const IAxis> pAxisR;
0214 
0215   Transform3 fullTransform = transform;
0216   Transform3 inverseTransform = transform.inverse();
0217 
0218   if (bTypeR == equidistant) {
0219     pAxisR = createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0220                                    AxisR, protoLayer, fullTransform);
0221   } else {
0222     pAxisR = createVariableAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0223                                 AxisR, protoLayer, fullTransform);
0224   }
0225 
0226   // if we have more than one R ring, we need to figure out
0227   // the number of phi bins.
0228   if (pAxisR->getNBins() > 1) {
0229     // more than one R-Ring, we need to adjust
0230     // this FORCES equidistant binning
0231     std::vector<std::vector<const Surface*>> phiModules(pAxisR->getNBins());
0232     for (const auto& srf : surfacesRaw) {
0233       const Vector3 bpos =
0234           inverseTransform * srf->referencePosition(gctx, AxisR);
0235       const std::size_t bin =
0236           pAxisR->getBin(perp(bpos)) - 1;  // subtract underflow bin
0237       phiModules.at(bin).push_back(srf);
0238     }
0239 
0240     std::vector<std::size_t> nPhiModules;
0241     const auto& matcher = m_cfg.surfaceMatcher;
0242     const auto equal = [&gctx, &matcher](const Surface* a, const Surface* b) {
0243       return matcher(gctx, AxisPhi, a, b);
0244     };
0245 
0246     std::transform(
0247         phiModules.begin(), phiModules.end(), std::back_inserter(nPhiModules),
0248         [&equal,
0249          this](const std::vector<const Surface*>& surfaces_) -> std::size_t {
0250           return this->findKeySurfaces(surfaces_, equal).size();
0251         });
0252 
0253     // @FIXME: Problem: phi binning runs rotation to optimize
0254     // for bin edges. This FAILS after this modification, since
0255     // the bin count is the one from the lowest module-count bin,
0256     // but the rotation is done considering all bins.
0257     // This might be resolved through bin completion, but not sure.
0258     // @TODO: check in extrapolation
0259     const std::size_t nBinsPhi =
0260         *std::min_element(nPhiModules.begin(), nPhiModules.end());
0261     pAxisPhi =
0262         createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0263                               AxisPhi, protoLayer, fullTransform, nBinsPhi);
0264 
0265   } else {
0266     // use regular determination
0267     if (bTypePhi == equidistant) {
0268       pAxisPhi =
0269           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0270                                 AxisPhi, protoLayer, fullTransform, 0);
0271     } else {
0272       pAxisPhi = createVariableAxis(gctx, surfacesRaw, AxisBoundaryType::Closed,
0273                                     AxisPhi, protoLayer, fullTransform);
0274     }
0275   }
0276 
0277   const double Z = protoLayer.medium(AxisZ, true);
0278   const double Rmin = protoLayer.min(AxisR, true);
0279   const double Rmax = protoLayer.max(AxisR, true);
0280   const double layerThickness = protoLayer.range(AxisZ) * 0.5;
0281   ACTS_VERBOSE("- z-position of disc estimated as " << Z);
0282 
0283   if (fullTransform.translation().norm() < s_transformEquivalentTolerance) {
0284     ACTS_VERBOSE(
0285         "input transform does not have translation: putting projection surface "
0286         "at center of gravity in z");
0287     fullTransform.translate(Vector3::UnitZ() * Z);
0288   }
0289 
0290   auto surface = Surface::makeShared<DiscSurface>(fullTransform, Rmin, Rmax);
0291 
0292   // get the number of bins
0293   const std::size_t bins0 = pAxisR->getNBins();
0294   const std::size_t bins1 = pAxisPhi->getNBins();
0295   ACTS_VERBOSE(" -- with " << surfaces.size() << " surfaces.");
0296   ACTS_VERBOSE(" -- with r x phi  = " << bins0 << " x " << bins1 << " = "
0297                                       << bins0 * bins1 << " bins.");
0298 
0299   return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0300                       layerThickness, {*pAxisR, *pAxisPhi}, neighborWindow,
0301                       overfill);
0302 }
0303 
0304 /// SurfaceArrayCreator interface method - create an array on a plane
0305 SurfaceArray SurfaceArrayCreator::surfaceArrayOnPlane(
0306     const GeometryContext& gctx,
0307     std::vector<std::shared_ptr<const Surface>> surfaces, std::size_t bins1,
0308     std::size_t bins2, AxisDirection aDir,
0309     std::optional<ProtoLayer> protoLayerOpt, const Transform3& transform,
0310     SurfaceArray::NeighborWindow neighborWindow, std::uint8_t overfill) const {
0311   using enum AxisDirection;
0312 
0313   const std::vector<const Surface*> surfacesRaw = unpackSmartPointers(surfaces);
0314   // check if we have proto layer, else build it
0315   const ProtoLayer protoLayer =
0316       protoLayerOpt ? *protoLayerOpt : ProtoLayer(gctx, surfacesRaw);
0317 
0318   ACTS_VERBOSE("Creating a SurfaceArray on a plance");
0319   ACTS_VERBOSE(" -- with " << surfaces.size() << " surfaces.");
0320   ACTS_VERBOSE(" -- with " << bins1 << " x " << bins2 << " = " << bins1 * bins2
0321                            << " bins.");
0322   Transform3 fullTransform = transform;
0323 
0324   const double layerTolerance = protoLayer.range(aDir) * 0.5;
0325 
0326   // Axis along the binning
0327   switch (aDir) {
0328     case AxisX: {
0329       const auto pAxis1 =
0330           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0331                                 AxisY, protoLayer, fullTransform, bins1);
0332       const auto pAxis2 =
0333           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0334                                 AxisZ, protoLayer, fullTransform, bins2);
0335       const Vector3 shift = fullTransform.translation();
0336       auto surface = Surface::makeShared<PlaneSurface>(
0337           fullTransform, std::make_shared<RectangleBounds>(
0338                              Vector2(protoLayer.min(AxisY) - shift.y(),
0339                                      protoLayer.min(AxisZ) - shift.z()),
0340                              Vector2(protoLayer.max(AxisY) - shift.y(),
0341                                      protoLayer.max(AxisZ) - shift.z())));
0342       return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0343                           layerTolerance, {*pAxis1, *pAxis2}, neighborWindow,
0344                           overfill);
0345     }
0346     case AxisY: {
0347       const auto pAxis1 =
0348           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0349                                 AxisX, protoLayer, fullTransform, bins1);
0350       const auto pAxis2 =
0351           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0352                                 AxisZ, protoLayer, fullTransform, bins2);
0353       const Vector3 shift = fullTransform.translation();
0354       auto surface = Surface::makeShared<PlaneSurface>(
0355           fullTransform, std::make_shared<RectangleBounds>(
0356                              Vector2(protoLayer.min(AxisX) - shift.x(),
0357                                      protoLayer.min(AxisZ) - shift.z()),
0358                              Vector2(protoLayer.max(AxisX) - shift.x(),
0359                                      protoLayer.max(AxisZ) - shift.z())));
0360       return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0361                           layerTolerance, {*pAxis1, *pAxis2}, neighborWindow,
0362                           overfill);
0363     }
0364     case AxisZ: {
0365       const auto pAxis1 =
0366           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0367                                 AxisX, protoLayer, fullTransform, bins1);
0368       const auto pAxis2 =
0369           createEquidistantAxis(gctx, surfacesRaw, AxisBoundaryType::Bound,
0370                                 AxisY, protoLayer, fullTransform, bins2);
0371       const Vector3 shift = fullTransform.translation();
0372       auto surface = Surface::makeShared<PlaneSurface>(
0373           fullTransform, std::make_shared<RectangleBounds>(
0374                              Vector2(protoLayer.min(AxisX) - shift.x(),
0375                                      protoLayer.min(AxisY) - shift.y()),
0376                              Vector2(protoLayer.max(AxisX) - shift.x(),
0377                                      protoLayer.max(AxisY) - shift.y())));
0378       return SurfaceArray(gctx, std::move(surfaces), std::move(surface),
0379                           layerTolerance, {*pAxis1, *pAxis2}, neighborWindow,
0380                           overfill);
0381     }
0382     default:
0383       break;
0384   }
0385 
0386   throw std::invalid_argument(
0387       "SurfaceArrayCreator::surfaceArrayOnPlane: Invalid binning direction");
0388 }
0389 
0390 std::vector<const Surface*> SurfaceArrayCreator::findKeySurfaces(
0391     const std::vector<const Surface*>& surfaces,
0392     const std::function<bool(const Surface*, const Surface*)>& equal) const {
0393   std::vector<const Surface*> keys;
0394   for (const auto& srfA : surfaces) {
0395     bool exists = false;
0396     for (const auto& srfB : keys) {
0397       if (equal(srfA, srfB)) {
0398         exists = true;
0399         break;
0400       }
0401     }
0402     if (!exists) {
0403       keys.push_back(srfA);
0404     }
0405   }
0406 
0407   return keys;
0408 }
0409 
0410 std::size_t SurfaceArrayCreator::determineBinCount(
0411     const GeometryContext& gctx, const std::vector<const Surface*>& surfaces,
0412     AxisDirection aDir) const {
0413   const auto& matcher = m_cfg.surfaceMatcher;
0414   const auto equal = [&gctx, &aDir, &matcher](const Surface* a,
0415                                               const Surface* b) {
0416     return matcher(gctx, aDir, a, b);
0417   };
0418   const std::vector<const Surface*> keys = findKeySurfaces(surfaces, equal);
0419 
0420   return keys.size();
0421 }
0422 
0423 std::unique_ptr<const IAxis> SurfaceArrayCreator::createVariableAxis(
0424     const GeometryContext& gctx, const std::vector<const Surface*>& surfaces,
0425     AxisBoundaryType aBoundaryType, AxisDirection aDir,
0426     const ProtoLayer& protoLayer, Transform3& transform) const {
0427   using enum AxisDirection;
0428 
0429   if (surfaces.empty()) {
0430     throw std::logic_error(
0431         "No surfaces handed over for creating arbitrary bin utility!");
0432   }
0433   // BinningOption is open for z and r, in case of phi binning reset later
0434   // the vector with the binning Values (boundaries for each bin)
0435 
0436   // bind matcher with binning type
0437   const auto& matcher = m_cfg.surfaceMatcher;
0438   // find the key surfaces
0439   const auto equal = [&gctx, &aDir, &matcher](const Surface* a,
0440                                               const Surface* b) {
0441     return matcher(gctx, aDir, a, b);
0442   };
0443   std::vector<const Surface*> keys = findKeySurfaces(surfaces, equal);
0444 
0445   std::vector<double> binEdges;
0446   if (aDir == AxisPhi) {
0447     std::stable_sort(keys.begin(), keys.end(),
0448                      [&gctx](const Surface* a, const Surface* b) {
0449                        return (phi(a->referencePosition(gctx, AxisPhi)) <
0450                                phi(b->referencePosition(gctx, AxisPhi)));
0451                      });
0452 
0453     const double maxPhi =
0454         0.5 * (phi(keys.at(0)->referencePosition(gctx, AxisPhi)) +
0455                phi(keys.at(1)->referencePosition(gctx, AxisPhi)));
0456 
0457     // create rotation, so that maxPhi is +pi
0458     const double angle = -(std::numbers::pi + maxPhi);
0459     transform = transform * AngleAxis3(angle, Vector3::UnitZ());
0460 
0461     // iterate over all key surfaces, and use their mean position as aDirs,
0462     // but
0463     // rotate using transform from before
0464     double previous = phi(keys.at(0)->referencePosition(gctx, AxisPhi));
0465     // go through key surfaces
0466     for (std::size_t i = 1; i < keys.size(); i++) {
0467       const Surface* surface = keys.at(i);
0468       // create central binning values which is the mean of the center
0469       // positions in the binning direction of the current and previous
0470       // surface
0471       const double edge =
0472           0.5 * (previous + phi(surface->referencePosition(gctx, AxisPhi))) +
0473           angle;
0474       binEdges.push_back(edge);
0475       previous = phi(surface->referencePosition(gctx, AxisPhi));
0476     }
0477 
0478     // segments
0479     constexpr unsigned int segments = 72;
0480 
0481     // get the bounds of the last surfaces
0482     const Surface* backSurface = keys.back();
0483     const PlanarBounds* backBounds =
0484         dynamic_cast<const PlanarBounds*>(&(backSurface->bounds()));
0485     if (backBounds == nullptr) {
0486       ACTS_ERROR(
0487           "Given SurfaceBounds are not planar - not implemented for "
0488           "other bounds yet! ");
0489     }
0490     // get the global vertices
0491     const std::vector<Vector3> backVertices =
0492         makeGlobalVertices(gctx, *backSurface, backBounds->vertices(segments));
0493     const double maxBValue = phi(*std::ranges::max_element(
0494         backVertices, {}, [](const Vector3& v) { return phi(v); }));
0495 
0496     binEdges.push_back(maxBValue);
0497 
0498     binEdges.push_back(std::numbers::pi);
0499 
0500   } else if (aDir == AxisZ) {
0501     std::ranges::stable_sort(keys, {}, [&gctx](const Surface* s) {
0502       return s->referencePosition(gctx, AxisZ).z();
0503     });
0504 
0505     const double frameOffset = axisFrameOffset(AxisZ, transform);
0506 
0507     binEdges.push_back(protoLayer.min(AxisZ) - frameOffset);
0508     binEdges.push_back(protoLayer.max(AxisZ) - frameOffset);
0509 
0510     // the z-center position of the previous surface
0511     double previous = keys.front()->referencePosition(gctx, AxisZ).z();
0512     // go through key surfaces
0513     for (auto surface = keys.begin() + 1; surface != keys.end(); surface++) {
0514       // create central binning values which is the mean of the center
0515       // positions in the binning direction of the current and previous
0516       // surface
0517       binEdges.push_back(
0518           0.5 * (previous + (*surface)->referencePosition(gctx, AxisZ).z()) -
0519           frameOffset);
0520       previous = (*surface)->referencePosition(gctx, AxisZ).z();
0521     }
0522   } else {  // AxisR
0523     std::ranges::stable_sort(keys, {}, [&gctx](const Surface* s) {
0524       return perp(s->referencePosition(gctx, AxisR));
0525     });
0526 
0527     binEdges.push_back(protoLayer.min(AxisR));
0528     binEdges.push_back(protoLayer.max(AxisR));
0529 
0530     // the r-center position of the previous surface
0531     double previous = perp(keys.front()->referencePosition(gctx, AxisR));
0532 
0533     // go through key surfaces
0534     for (auto surface = keys.begin() + 1; surface != keys.end(); surface++) {
0535       // create central binning values which is the mean of the center
0536       // positions in the binning direction of the current and previous
0537       // surface
0538       binEdges.push_back(
0539           0.5 * (previous + perp((*surface)->referencePosition(gctx, AxisR))));
0540       previous = perp((*surface)->referencePosition(gctx, AxisR));
0541     }
0542   }
0543   std::ranges::sort(binEdges);
0544   ACTS_VERBOSE("Create variable binning Axis for binned SurfaceArray");
0545   ACTS_VERBOSE("    AxisDirection: " << aDir);
0546   ACTS_VERBOSE("    Number of bins: " << (binEdges.size() - 1));
0547   ACTS_VERBOSE("    (Min/Max) = (" << binEdges.front() << "/"
0548                                        << binEdges.back() << ")");
0549 
0550   return IAxis::createVariable(aBoundaryType, binEdges, aDir);
0551 }
0552 
0553 std::unique_ptr<const IAxis> SurfaceArrayCreator::createEquidistantAxis(
0554     const GeometryContext& gctx, const std::vector<const Surface*>& surfaces,
0555     AxisBoundaryType aBoundaryType, AxisDirection aDir,
0556     const ProtoLayer& protoLayer, Transform3& transform,
0557     std::size_t nBins) const {
0558   using enum AxisDirection;
0559 
0560   if (surfaces.empty()) {
0561     throw std::logic_error(
0562         "No surfaces handed over for creating equidistant axis!");
0563   }
0564   // check the binning type first
0565 
0566   const double frameOffset = axisFrameOffset(aDir, transform);
0567   double minimum = protoLayer.min(aDir, false) - frameOffset;
0568   double maximum = protoLayer.max(aDir, false) - frameOffset;
0569 
0570   std::size_t binNumber = 0;
0571   if (nBins == 0) {
0572     // determine bin count
0573     binNumber = determineBinCount(gctx, surfaces, aDir);
0574   } else {
0575     // use bin count
0576     binNumber = nBins;
0577   }
0578 
0579   // now check the binning value
0580   if (aDir == AxisPhi) {
0581     minimum = protoLayer.min(AxisPhi, true);
0582     maximum = protoLayer.max(AxisPhi, true);
0583 
0584     if (m_cfg.doPhiBinningOptimization) {
0585       minimum = -std::numbers::pi;
0586       maximum = std::numbers::pi;
0587 
0588       // Phi binning
0589       // set the binning option for phi
0590       // sort first in phi
0591       const Surface* maxElem =
0592           *std::ranges::max_element(surfaces, {}, [&gctx](const Surface* s) {
0593             return phi(s->referencePosition(gctx, AxisR));
0594           });
0595 
0596       // Rotate the highest-phi module centre onto the last bin centre, so the
0597       // wrap-around at +-pi never falls on a module centre. @p transform is
0598       // the representative surface's local-to-global map, so the lookup sees
0599       // `localPhi = globalPhi - angle`.
0600       const double surfaceMax = phi(maxElem->referencePosition(gctx, AxisR));
0601       const double gridStep =
0602           2 * std::numbers::pi / static_cast<double>(binNumber);
0603       const double gridMax = std::numbers::pi - 0.5 * gridStep;
0604       const double angle = surfaceMax - gridMax;
0605 
0606       // replace given transform ref
0607       transform = transform * AngleAxis3(angle, Vector3::UnitZ());
0608     }
0609   }
0610 
0611   // assign the bin size
0612   ACTS_VERBOSE("Create equidistant binning Axis for binned SurfaceArray");
0613   ACTS_VERBOSE("    AxisDirection: " << aDir);
0614   ACTS_VERBOSE("    Number of bins: " << binNumber);
0615   ACTS_VERBOSE("    (Min/Max) = (" << minimum << "/" << maximum << ")");
0616 
0617   return IAxis::createEquidistant(aBoundaryType, minimum, maximum, binNumber,
0618                                   aDir);
0619 }
0620 
0621 std::vector<Vector3> SurfaceArrayCreator::makeGlobalVertices(
0622     const GeometryContext& gctx, const Surface& surface,
0623     const std::vector<Vector2>& locVertices) const {
0624   std::vector<Vector3> globVertices;
0625   for (auto& vertex : locVertices) {
0626     Vector3 globVertex = surface.localToGlobal(gctx, vertex, Vector3());
0627     globVertices.push_back(globVertex);
0628   }
0629   return globVertices;
0630 }
0631 
0632 }  // namespace Acts