Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:15:35

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 <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Geometry/GeometryContext.hpp"
0014 #include "Acts/Surfaces/CylinderBounds.hpp"
0015 #include "Acts/Surfaces/CylinderSurface.hpp"
0016 #include "Acts/Surfaces/DiscBounds.hpp"
0017 #include "Acts/Surfaces/DiscSurface.hpp"
0018 #include "Acts/Surfaces/PlanarBounds.hpp"
0019 #include "Acts/Surfaces/PlaneSurface.hpp"
0020 #include "Acts/Surfaces/RadialBounds.hpp"
0021 #include "Acts/Surfaces/RectangleBounds.hpp"
0022 #include "Acts/Surfaces/Surface.hpp"
0023 #include "Acts/Utilities/IAxis.hpp"
0024 #include "Acts/Utilities/IMultiAxis.hpp"
0025 #include "ActsFatras/Digitization/Segmentizer.hpp"
0026 
0027 #include <cmath>
0028 #include <fstream>
0029 #include <memory>
0030 #include <numbers>
0031 #include <string>
0032 #include <tuple>
0033 #include <utility>
0034 #include <vector>
0035 
0036 #include "DigitizationCsvOutput.hpp"
0037 #include "PlanarSurfaceTestBeds.hpp"
0038 
0039 namespace bdata = boost::unit_test::data;
0040 
0041 using namespace Acts;
0042 using namespace ActsFatras;
0043 
0044 namespace ActsTests {
0045 
0046 BOOST_AUTO_TEST_SUITE(DigitizationSuite)
0047 
0048 BOOST_AUTO_TEST_CASE(SegmentizerCartesian) {
0049   auto geoCtx = GeometryContext::dangerouslyDefaultConstruct();
0050 
0051   auto rectangleBounds = std::make_shared<RectangleBounds>(1., 1.);
0052   auto planeSurface = Surface::makeShared<PlaneSurface>(Transform3::Identity(),
0053                                                         rectangleBounds);
0054 
0055   // The segmentation
0056   const auto segmentation = IMultiAxis::create(
0057       *IAxis::createEquidistant(AxisBoundaryType::Bound, -1., 1., 20,
0058                                 AxisDirection::AxisX),
0059       *IAxis::createEquidistant(AxisBoundaryType::Bound, -1., 1., 20,
0060                                 AxisDirection::AxisY));
0061 
0062   Segmentizer cl;
0063 
0064   // Test: Normal hit into the surface
0065   Vector2 nPosition(0.37, 0.76);
0066   auto nSegments =
0067       cl.segments(geoCtx, *planeSurface, *segmentation, {nPosition, nPosition});
0068   BOOST_CHECK_EQUAL(nSegments.size(), 1);
0069   BOOST_CHECK_EQUAL(nSegments[0].bin[0], 13);
0070   BOOST_CHECK_EQUAL(nSegments[0].bin[1], 17);
0071 
0072   // Test: Inclined hit into the surface - negative x direction
0073   Vector2 ixPositionS(0.37, 0.76);
0074   Vector2 ixPositionE(0.02, 0.73);
0075   auto ixSegments = cl.segments(geoCtx, *planeSurface, *segmentation,
0076                                 {ixPositionS, ixPositionE});
0077   BOOST_CHECK_EQUAL(ixSegments.size(), 4);
0078 
0079   // Test: Inclined hit into the surface - positive y direction
0080   Vector2 iyPositionS(0.37, 0.76);
0081   Vector2 iyPositionE(0.39, 0.91);
0082   auto iySegments = cl.segments(geoCtx, *planeSurface, *segmentation,
0083                                 {iyPositionS, iyPositionE});
0084   BOOST_CHECK_EQUAL(iySegments.size(), 3);
0085 
0086   // Test: Inclined hit into the surface - x/y direction
0087   Vector2 ixyPositionS(-0.27, 0.76);
0088   Vector2 ixyPositionE(-0.02, -0.73);
0089   auto ixySegments = cl.segments(geoCtx, *planeSurface, *segmentation,
0090                                  {ixyPositionS, ixyPositionE});
0091   BOOST_CHECK_EQUAL(ixySegments.size(), 18);
0092 }
0093 
0094 BOOST_AUTO_TEST_CASE(SegmentizerPolarRadial) {
0095   auto geoCtx = GeometryContext::dangerouslyDefaultConstruct();
0096 
0097   auto radialBounds = std::make_shared<const RadialBounds>(5., 10., 0.25, 0.);
0098   auto radialDisc =
0099       Surface::makeShared<DiscSurface>(Transform3::Identity(), radialBounds);
0100 
0101   // The segmentation
0102   const auto segmentation = IMultiAxis::create(
0103       *IAxis::createEquidistant(AxisBoundaryType::Bound, 5., 10., 2,
0104                                 AxisDirection::AxisR),
0105       *IAxis::createEquidistant(AxisBoundaryType::Bound, -0.25, 0.25, 250,
0106                                 AxisDirection::AxisPhi));
0107 
0108   Segmentizer cl;
0109 
0110   // Test: Normal hit into the surface
0111   Vector2 nPosition(6.76, 0.5);
0112   auto nSegments =
0113       cl.segments(geoCtx, *radialDisc, *segmentation, {nPosition, nPosition});
0114   BOOST_CHECK_EQUAL(nSegments.size(), 1);
0115   BOOST_CHECK_EQUAL(nSegments[0].bin[0], 0);
0116   BOOST_CHECK_EQUAL(nSegments[0].bin[1], 161);
0117 
0118   // Test: now opver more phi strips
0119   Vector2 sPositionS(6.76, 0.5);
0120   Vector2 sPositionE(7.03, -0.3);
0121   auto sSegment =
0122       cl.segments(geoCtx, *radialDisc, *segmentation, {sPositionS, sPositionE});
0123   BOOST_CHECK_EQUAL(sSegment.size(), 59);
0124 
0125   // Test: jump over R boundary, but stay in phi bin
0126   sPositionS = Vector2(6.76, 0.);
0127   sPositionE = Vector2(7.83, 0.);
0128   sSegment =
0129       cl.segments(geoCtx, *radialDisc, *segmentation, {sPositionS, sPositionE});
0130   BOOST_CHECK_EQUAL(sSegment.size(), 2);
0131 }
0132 
0133 BOOST_AUTO_TEST_CASE(SegmentizerCylinderPhiSeam) {
0134   auto geoCtx = GeometryContext::dangerouslyDefaultConstruct();
0135 
0136   const double radius = 5.;
0137   const double halfZ = 10.;
0138   const double halfRPhi = std::numbers::pi * radius;
0139   auto cylinder = Surface::makeShared<CylinderSurface>(
0140       Transform3::Identity(), std::make_shared<CylinderBounds>(radius, halfZ));
0141 
0142   // Unrolled (rPhi, z) readout frame, closed in rPhi
0143   const std::size_t nRPhi = 100;
0144   const auto segmentation = IMultiAxis::create(
0145       *IAxis::createEquidistant(AxisBoundaryType::Closed, -halfRPhi, halfRPhi,
0146                                 nRPhi, AxisDirection::AxisRPhi),
0147       *IAxis::createEquidistant(AxisBoundaryType::Bound, -halfZ, halfZ, 20,
0148                                 AxisDirection::AxisZ));
0149   const auto openSegmentation = IMultiAxis::create(
0150       *IAxis::createEquidistant(AxisBoundaryType::Bound, -halfRPhi, halfRPhi,
0151                                 nRPhi, AxisDirection::AxisRPhi),
0152       *IAxis::createEquidistant(AxisBoundaryType::Bound, -halfZ, halfZ, 20,
0153                                 AxisDirection::AxisZ));
0154 
0155   Segmentizer cl;
0156 
0157   auto totalPath = [](const auto& segments) {
0158     double path = 0.;
0159     for (const auto& s : segments) {
0160       path += s.activation;
0161     }
0162     return path;
0163   };
0164 
0165   // Test: away from the seam, the closed axis behaves like an open one
0166   Vector2 aStart(0.1, 0.3);
0167   Vector2 aEnd(1.4, 0.9);
0168   auto aSegments =
0169       cl.segments(geoCtx, *cylinder, *segmentation, {aStart, aEnd});
0170   auto aOpenSegments =
0171       cl.segments(geoCtx, *cylinder, *openSegmentation, {aStart, aEnd});
0172   BOOST_REQUIRE_EQUAL(aSegments.size(), aOpenSegments.size());
0173   for (std::size_t i = 0; i < aSegments.size(); ++i) {
0174     BOOST_CHECK_EQUAL(aSegments[i].bin[0], aOpenSegments[i].bin[0]);
0175     BOOST_CHECK_EQUAL(aSegments[i].bin[1], aOpenSegments[i].bin[1]);
0176     BOOST_CHECK_EQUAL(aSegments[i].activation, aOpenSegments[i].activation);
0177   }
0178 
0179   // Test: crossing the seam at +pi R in rPhi gives two channels, not the ring
0180   Vector2 sStart(halfRPhi - 0.05, 0.3);
0181   Vector2 sEnd(halfRPhi + 0.05, 0.3);
0182   auto sSegments =
0183       cl.segments(geoCtx, *cylinder, *segmentation, {sStart, sEnd});
0184   BOOST_REQUIRE_EQUAL(sSegments.size(), 2);
0185   BOOST_CHECK_EQUAL(sSegments[0].bin[0], nRPhi - 1);
0186   BOOST_CHECK_EQUAL(sSegments[1].bin[0], 0);
0187   BOOST_CHECK_CLOSE(totalPath(sSegments), 0.1, 1e-6);
0188 
0189   // Test: the same in the other direction
0190   auto rSegments =
0191       cl.segments(geoCtx, *cylinder, *segmentation, {sEnd, sStart});
0192   BOOST_REQUIRE_EQUAL(rSegments.size(), 2);
0193   BOOST_CHECK_EQUAL(rSegments[0].bin[0], 0);
0194   BOOST_CHECK_EQUAL(rSegments[1].bin[0], nRPhi - 1);
0195 
0196   // Test: crossing the seam at -pi R in rPhi
0197   Vector2 nStart(-halfRPhi + 0.05, 0.3);
0198   Vector2 nEnd(-halfRPhi - 0.05, 0.3);
0199   auto nSegments =
0200       cl.segments(geoCtx, *cylinder, *segmentation, {nStart, nEnd});
0201   BOOST_REQUIRE_EQUAL(nSegments.size(), 2);
0202   BOOST_CHECK_EQUAL(nSegments[0].bin[0], 0);
0203   BOOST_CHECK_EQUAL(nSegments[1].bin[0], nRPhi - 1);
0204 
0205   // Test: an inclined segment across the seam is channelised like the same
0206   // segment rotated by half a turn, with the rPhi bins shifted by nRPhi / 2
0207   Vector2 iStart(halfRPhi - 0.83, -0.47);
0208   Vector2 iEnd(halfRPhi + 0.61, 1.38);
0209   Vector2 shift(halfRPhi, 0.);
0210   auto iSegments =
0211       cl.segments(geoCtx, *cylinder, *segmentation, {iStart, iEnd});
0212   auto iShiftedSegments = cl.segments(geoCtx, *cylinder, *segmentation,
0213                                       {iStart - shift, iEnd - shift});
0214   BOOST_REQUIRE_EQUAL(iSegments.size(), iShiftedSegments.size());
0215   BOOST_CHECK_LT(iSegments.size(), 15u);
0216   for (std::size_t i = 0; i < iSegments.size(); ++i) {
0217     BOOST_CHECK_EQUAL(iSegments[i].bin[0],
0218                       (iShiftedSegments[i].bin[0] + nRPhi / 2) % nRPhi);
0219     BOOST_CHECK_EQUAL(iSegments[i].bin[1], iShiftedSegments[i].bin[1]);
0220     BOOST_CHECK_CLOSE(iSegments[i].activation, iShiftedSegments[i].activation,
0221                       1e-6);
0222   }
0223   BOOST_CHECK_CLOSE(totalPath(iSegments), (iEnd - iStart).norm(), 1e-6);
0224 }
0225 
0226 /// Unit test for testing the Segmentizer
0227 BOOST_DATA_TEST_CASE(
0228     RandomSegmentizerTest,
0229     bdata::random((
0230         bdata::engine = std::mt19937(), bdata::seed = 1,
0231         bdata::distribution = std::uniform_real_distribution<double>(0., 1.))) ^
0232         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
0233                        bdata::distribution =
0234                            std::uniform_real_distribution<double>(0., 1.))) ^
0235         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
0236                        bdata::distribution =
0237                            std::uniform_real_distribution<double>(0., 1.))) ^
0238         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
0239                        bdata::distribution =
0240                            std::uniform_real_distribution<double>(0., 1.))) ^
0241         bdata::xrange(25),
0242     startR0, startR1, endR0, endR1, index) {
0243   auto geoCtx = GeometryContext::dangerouslyDefaultConstruct();
0244   Segmentizer cl;
0245 
0246   // Test beds with random numbers generated inside
0247   PlanarSurfaceTestBeds pstd;
0248   auto testBeds = pstd(1.);
0249 
0250   DigitizationCsvOutput csvHelper;
0251 
0252   for (const auto& tb : testBeds) {
0253     const auto& name = std::get<0>(tb);
0254     const auto* surface = (std::get<1>(tb)).get();
0255     const auto& segmentation = std::get<2>(tb);
0256     const auto& randomizer = std::get<3>(tb);
0257 
0258     if (index == 0) {
0259       std::ofstream shape;
0260       std::ofstream grid;
0261       const auto centerXY = surface->center(geoCtx).segment<2>(0);
0262       // 0 - write the shape
0263       shape.open("Segmentizer" + name + "Borders.csv");
0264       if (surface->type() == Surface::Plane) {
0265         const auto* pBounds =
0266             static_cast<const PlanarBounds*>(&(surface->bounds()));
0267         csvHelper.writePolygon(shape, pBounds->vertices(1), -centerXY);
0268       } else if (surface->type() == Surface::Disc) {
0269         const auto* dBounds =
0270             static_cast<const DiscBounds*>(&(surface->bounds()));
0271         csvHelper.writePolygon(shape, dBounds->vertices(72), -centerXY);
0272       }
0273       // 1 - write the grid
0274       grid.open("Segmentizer" + name + "Grid.csv");
0275       const IAxis& axis0 = segmentation->getAxis(0);
0276       const IAxis& axis1 = segmentation->getAxis(1);
0277       if (axis0.getDirection() == AxisDirection::AxisX &&
0278           axis1.getDirection() == AxisDirection::AxisY) {
0279         double bxmin = axis0.getMin();
0280         double bxmax = axis0.getMax();
0281         double bymin = axis1.getMin();
0282         double bymax = axis1.getMax();
0283         const std::vector<double> xboundaries = axis0.getBinEdges();
0284         const std::vector<double> yboundaries = axis1.getBinEdges();
0285         for (const double xval : xboundaries) {
0286           csvHelper.writeLine(grid, {xval, bymin}, {xval, bymax});
0287         }
0288         for (const double yval : yboundaries) {
0289           csvHelper.writeLine(grid, {bxmin, yval}, {bxmax, yval});
0290         }
0291       } else if (axis0.getDirection() == AxisDirection::AxisR &&
0292                  axis1.getDirection() == AxisDirection::AxisPhi) {
0293         double brmin = axis0.getMin();
0294         double brmax = axis0.getMax();
0295         double bphimin = axis1.getMin();
0296         double bphimax = axis1.getMax();
0297         const std::vector<double> rboundaries = axis0.getBinEdges();
0298         const std::vector<double> phiboundaries = axis1.getBinEdges();
0299         for (const double r : rboundaries) {
0300           csvHelper.writeArc(grid, r, bphimin, bphimax);
0301         }
0302         for (const double phi : phiboundaries) {
0303           double cphi = std::cos(phi);
0304           double sphi = std::sin(phi);
0305           csvHelper.writeLine(grid, {brmin * cphi, brmin * sphi},
0306                               {brmax * cphi, brmax * sphi});
0307         }
0308       }
0309     }
0310 
0311     auto start = randomizer(startR0, startR1);
0312     auto end = randomizer(endR0, endR1);
0313 
0314     std::ofstream segments;
0315     segments.open("Segmentizer" + name + "Segments_n" + std::to_string(index) +
0316                   ".csv");
0317 
0318     std::ofstream cluster;
0319     cluster.open("Segmentizer" + name + "Cluster_n" + std::to_string(index) +
0320                  ".csv");
0321 
0322     /// Run the Segmentizer
0323     auto cSegments = cl.segments(geoCtx, *surface, *segmentation, {start, end});
0324 
0325     for (const auto& cs : cSegments) {
0326       csvHelper.writeLine(segments, cs.path2D[0], cs.path2D[1]);
0327     }
0328 
0329     segments.close();
0330     cluster.close();
0331   }
0332 }
0333 
0334 BOOST_AUTO_TEST_SUITE_END()
0335 
0336 }  // namespace ActsTests