Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-25 07:57:17

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/Definitions/Tolerance.hpp"
0014 #include "Acts/Geometry/GeometryContext.hpp"
0015 #include "Acts/Surfaces/DiscBounds.hpp"
0016 #include "Acts/Surfaces/DiscSurface.hpp"
0017 #include "Acts/Surfaces/PlanarBounds.hpp"
0018 #include "Acts/Surfaces/PlaneSurface.hpp"
0019 #include "Acts/Surfaces/RadialBounds.hpp"
0020 #include "Acts/Surfaces/RectangleBounds.hpp"
0021 #include "Acts/Surfaces/Surface.hpp"
0022 #include "Acts/Utilities/Helpers.hpp"
0023 #include "Acts/Utilities/Result.hpp"
0024 #include "ActsFatras/Digitization/PlanarSurfaceMask.hpp"
0025 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0026 
0027 #include <array>
0028 #include <cmath>
0029 #include <fstream>
0030 #include <memory>
0031 #include <numbers>
0032 #include <string>
0033 #include <tuple>
0034 #include <utility>
0035 #include <vector>
0036 
0037 #include "DigitizationCsvOutput.hpp"
0038 #include "PlanarSurfaceTestBeds.hpp"
0039 
0040 namespace bdata = boost::unit_test::data;
0041 
0042 using namespace Acts;
0043 using namespace ActsFatras;
0044 
0045 namespace ActsTests {
0046 
0047 std::vector<std::array<std::ofstream, 3>> out;
0048 
0049 BOOST_AUTO_TEST_SUITE(DigitizationSuite)
0050 
0051 BOOST_AUTO_TEST_CASE(PlaneMaskRectangleBounds) {
0052   auto rectangleBounds = std::make_shared<RectangleBounds>(2., 3.5);
0053   auto planeSurface = Surface::makeShared<PlaneSurface>(Transform3::Identity(),
0054                                                         rectangleBounds);
0055 
0056   PlanarSurfaceMask psm;
0057 
0058   /// Case one : one outside
0059   std::array<Vector2, 2> segment = {Vector2(2.5, -4.5), Vector2(-1., -1.)};
0060   auto clipped = psm.apply(*planeSurface, segment).value();
0061 
0062   CHECK_CLOSE_ABS(clipped[1].x(), segment[1].x(), s_epsilon);
0063   CHECK_CLOSE_ABS(clipped[1].y(), segment[1].y(), s_epsilon);
0064   CHECK_CLOSE_ABS(clipped[0].x(), 1.5, s_epsilon);
0065   CHECK_CLOSE_ABS(clipped[0].y(), -3.5, s_epsilon);
0066 
0067   /// Case two : two outside
0068   segment = {Vector2(1., 4.), Vector2(3., 2.)};
0069   clipped = psm.apply(*planeSurface, segment).value();
0070 
0071   CHECK_CLOSE_ABS(clipped[1].x(), 2., s_epsilon);
0072   CHECK_CLOSE_ABS(clipped[1].y(), 3., s_epsilon);
0073   CHECK_CLOSE_ABS(clipped[0].x(), 1.5, s_epsilon);
0074   CHECK_CLOSE_ABS(clipped[0].y(), 3.5, s_epsilon);
0075 
0076   /// Case two : both inside (most likely case, untouched)
0077   segment = {Vector2(-1., 0.5), Vector2(0., 2.)};
0078   clipped = psm.apply(*planeSurface, segment).value();
0079 
0080   CHECK_CLOSE_ABS(clipped[0].x(), segment[0].x(), s_epsilon);
0081   CHECK_CLOSE_ABS(clipped[0].y(), segment[0].y(), s_epsilon);
0082   CHECK_CLOSE_ABS(clipped[1].x(), segment[1].x(), s_epsilon);
0083   CHECK_CLOSE_ABS(clipped[1].y(), segment[1].y(), s_epsilon);
0084 }
0085 
0086 BOOST_AUTO_TEST_CASE(DiscMaskRadialBounds) {
0087   auto discRadial = std::make_shared<RadialBounds>(
0088       2., 7.5, std::numbers::pi / 4., std::numbers::pi / 2.);
0089   auto discSurface =
0090       Surface::makeShared<DiscSurface>(Transform3::Identity(), discRadial);
0091 
0092   PlanarSurfaceMask psm;
0093 
0094   /// Case one : one outside R min
0095   std::array<Vector2, 2> segment = {Vector2(0.5, 1.8), Vector2(0.9, 6.)};
0096   auto clipped = psm.apply(*discSurface, segment).value();
0097 
0098   CHECK_CLOSE_ABS(clipped[1].x(), segment[1].x(), s_epsilon);
0099   CHECK_CLOSE_ABS(clipped[1].y(), segment[1].y(), s_epsilon);
0100   CHECK_CLOSE_ABS(VectorHelpers::perp(clipped[0]), 2., 5 * s_epsilon);
0101 
0102   /// Case two : one outside R max
0103   segment = {Vector2(0.5, 2.8), Vector2(0.9, 8.5)};
0104   clipped = psm.apply(*discSurface, segment).value();
0105 
0106   CHECK_CLOSE_ABS(clipped[0].x(), segment[0].x(), s_epsilon);
0107   CHECK_CLOSE_ABS(clipped[0].y(), segment[0].y(), s_epsilon);
0108   CHECK_CLOSE_ABS(VectorHelpers::perp(clipped[1]), 7.5, 5 * s_epsilon);
0109 
0110   /// Case three : both outside R min / max
0111   segment = {Vector2(0.5, 1.8), Vector2(0.9, 8.5)};
0112   clipped = psm.apply(*discSurface, segment).value();
0113   CHECK_CLOSE_ABS(VectorHelpers::perp(clipped[0]), 2., 5 * s_epsilon);
0114   CHECK_CLOSE_ABS(VectorHelpers::perp(clipped[1]), 7.5, 5 * s_epsilon);
0115   /// Case four: outside phi min
0116   segment = {Vector2(2.8, 2.5), Vector2(0., 3.5)};
0117   clipped = psm.apply(*discSurface, segment).value();
0118   CHECK_CLOSE_ABS(VectorHelpers::phi(clipped[0]), std::numbers::pi / 4.,
0119                   s_epsilon);
0120 
0121   /// Case five: outside phi max
0122   segment = {Vector2(0., 3.5), Vector2(-8., 5.)};
0123   clipped = psm.apply(*discSurface, segment).value();
0124   CHECK_CLOSE_ABS(VectorHelpers::phi(clipped[1]),
0125                   std::numbers::pi / 2. + std::numbers::pi / 4., s_epsilon);
0126 }
0127 
0128 std::vector<std::array<std::ofstream, 3>> segmentOutput;
0129 int ntests = 100;
0130 
0131 /// Unit test for testing the Surface mask
0132 BOOST_DATA_TEST_CASE(
0133     RandomPlanarSurfaceMask,
0134     bdata::random((
0135         bdata::engine = std::mt19937(), bdata::seed = 1,
0136         bdata::distribution = std::uniform_real_distribution<double>(0., 1.))) ^
0137         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
0138                        bdata::distribution =
0139                            std::uniform_real_distribution<double>(0., 1.))) ^
0140         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
0141                        bdata::distribution =
0142                            std::uniform_real_distribution<double>(0., 1.))) ^
0143         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
0144                        bdata::distribution =
0145                            std::uniform_real_distribution<double>(0., 1.))) ^
0146         bdata::xrange(ntests),
0147     startR0, startR1, endR0, endR1, index) {
0148   GeometryContext geoCtx;
0149 
0150   PlanarSurfaceMask psm;
0151 
0152   // Test beds with random numbers generated inside
0153   PlanarSurfaceTestBeds pstd;
0154   // Smearing 10 percent outside
0155   auto testBeds = pstd(1.1);
0156 
0157   DigitizationCsvOutput csvHelper;
0158 
0159   int itb = 0;
0160   for (const auto& tb : testBeds) {
0161     const auto& name = std::get<0>(tb);
0162     const auto* surface = (std::get<1>(tb)).get();
0163     const auto& randomizer = std::get<3>(tb);
0164 
0165     if (index == 0) {
0166       std::ofstream shape;
0167       const Vector2 centerXY = surface->center(geoCtx).segment<2>(0);
0168 
0169       // 0 - write the shape
0170       shape.open("PlanarSurfaceMask" + name + "Borders.csv");
0171       if (surface->type() == Surface::Plane) {
0172         const auto* pBounds =
0173             static_cast<const PlanarBounds*>(&(surface->bounds()));
0174         csvHelper.writePolygon(shape, pBounds->vertices(1), -centerXY);
0175       } else if (surface->type() == Surface::Disc) {
0176         const auto* dBounds =
0177             static_cast<const DiscBounds*>(&(surface->bounds()));
0178         csvHelper.writePolygon(shape, dBounds->vertices(72), -centerXY);
0179       }
0180 
0181       segmentOutput.push_back(std::array<std::ofstream, 3>());
0182       segmentOutput[itb][0].open("PlanarSurfaceMask" + name + "Inside.csv");
0183       segmentOutput[itb][1].open("PlanarSurfaceMask" + name + "Clipped.csv");
0184       segmentOutput[itb][2].open("PlanarSurfaceMask" + name + "Outside.csv");
0185     }
0186 
0187     auto start = randomizer(startR0, startR1);
0188     auto end = randomizer(endR0, endR1);
0189 
0190     std::array<Vector2, 2> segment = {start, end};
0191     auto clippedTest = psm.apply(*surface, segment);
0192     if (clippedTest.ok()) {
0193       auto clipped = clippedTest.value();
0194       if (segment == clipped) {
0195         csvHelper.writeLine(segmentOutput[itb][0], start, end);
0196       } else {
0197         csvHelper.writeLine(segmentOutput[itb][1], clipped[0], clipped[1]);
0198       }
0199     } else {
0200       csvHelper.writeLine(segmentOutput[itb][2], start, end);
0201     }
0202     ++itb;
0203   }
0204 
0205   // close the lines
0206   if (itb == ntests - 1) {
0207     segmentOutput[itb][0].close();
0208     segmentOutput[itb][1].close();
0209     segmentOutput[itb][2].close();
0210   }
0211 }
0212 
0213 BOOST_AUTO_TEST_SUITE_END()
0214 
0215 }  // namespace ActsTests