Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:14:41

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/tools/output_test_stream.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Definitions/Alignment.hpp"
0014 #include "Acts/Definitions/TrackParametrization.hpp"
0015 #include "Acts/EventData/ParticleHypothesis.hpp"
0016 #include "Acts/EventData/TrackParameters.hpp"
0017 #include "Acts/EventData/detail/GenerateParameters.hpp"
0018 #include "Acts/Geometry/GeometryContext.hpp"
0019 #include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
0020 #include "Acts/Propagator/Propagator.hpp"
0021 #include "Acts/Propagator/StraightLineStepper.hpp"
0022 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0023 #include "Acts/Surfaces/LineBounds.hpp"
0024 #include "Acts/Surfaces/Surface.hpp"
0025 #include "Acts/Tests/CommonHelpers/DetectorElementStub.hpp"
0026 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0027 #include "Acts/Tests/CommonHelpers/LineSurfaceStub.hpp"
0028 #include "Acts/Tests/CommonHelpers/PredefinedMaterials.hpp"
0029 #include "Acts/Utilities/Intersection.hpp"
0030 #include "Acts/Utilities/Result.hpp"
0031 #include "Acts/Utilities/ThrowAssert.hpp"
0032 #include "Acts/Utilities/UnitVectors.hpp"
0033 #include "Acts/Utilities/VectorHelpers.hpp"
0034 
0035 #include <cmath>
0036 #include <memory>
0037 #include <numbers>
0038 #include <optional>
0039 #include <tuple>
0040 #include <vector>
0041 
0042 namespace Acts::Test {
0043 
0044 // Create a test context
0045 GeometryContext tgContext = GeometryContext();
0046 
0047 BOOST_AUTO_TEST_SUITE(Surfaces)
0048 
0049 /// Unit test for creating compliant/non-compliant LineSurface object
0050 BOOST_AUTO_TEST_CASE(LineSurface_Constructors_test) {
0051   /// Test default construction
0052   // default construction is deleted
0053 
0054   Translation3 translation{0., 1., 2.};
0055   Transform3 transform(translation);
0056   auto pTransform = Transform3(translation);
0057   const double radius = 2.;
0058   const double halfZ = 20.;
0059   BOOST_CHECK(LineSurfaceStub(pTransform, radius, halfZ).constructedOk());
0060 
0061   /// ctor with nullptr for LineBounds
0062   BOOST_CHECK(LineSurfaceStub(pTransform).constructedOk());
0063 
0064   /// ctor with LineBounds
0065   auto pLineBounds = std::make_shared<const LineBounds>(2., 10.);
0066   BOOST_CHECK(LineSurfaceStub(pTransform, pLineBounds).constructedOk());
0067 
0068   /// ctor with LineBounds, detector element, Identifier
0069   auto pMaterial =
0070       std::make_shared<const HomogeneousSurfaceMaterial>(makePercentSlab());
0071   DetectorElementStub detElement{pTransform, pLineBounds, 0.2, pMaterial};
0072   BOOST_CHECK(LineSurfaceStub(pLineBounds, detElement).constructedOk());
0073   LineSurfaceStub lineToCopy(pTransform, 2., 20.);
0074 
0075   /// Copy ctor
0076   BOOST_CHECK(LineSurfaceStub(lineToCopy).constructedOk());
0077 
0078   /// Copied and transformed ctor
0079   BOOST_CHECK(
0080       LineSurfaceStub(tgContext, lineToCopy, transform).constructedOk());
0081 
0082   /// Construct with nullptr bounds
0083   DetectorElementStub detElem;
0084   BOOST_CHECK_THROW(LineSurfaceStub nullBounds(nullptr, detElem),
0085                     AssertionFailureException);
0086 
0087   BOOST_TEST_MESSAGE(
0088       "All LineSurface constructors are callable without problem");
0089 }
0090 
0091 /// Unit tests of all named methods
0092 BOOST_AUTO_TEST_CASE(LineSurface_allNamedMethods_test) {
0093   // referencePosition()
0094   Translation3 translation{0., 1., 2.};
0095   Transform3 transform(translation);
0096   LineSurfaceStub line(transform, 2., 20.);
0097   Vector3 referencePosition{0., 1., 2.};
0098   CHECK_CLOSE_ABS(referencePosition,
0099                   line.referencePosition(tgContext, AxisDirection::AxisX),
0100                   1e-6);
0101 
0102   // bounds()
0103   auto pLineBounds = std::make_shared<const LineBounds>(2., 10.);
0104   LineSurfaceStub boundedLine(transform, pLineBounds);
0105   const LineBounds& bounds =
0106       dynamic_cast<const LineBounds&>(boundedLine.bounds());
0107   BOOST_CHECK_EQUAL(bounds, LineBounds(2., 10.));
0108 
0109   // globalToLocal()
0110   Vector3 gpos{0., 1., 0.};
0111   const Vector3 mom{20., 0., 0.};  // needs more realistic parameters
0112   Vector2 localPosition =
0113       line.globalToLocal(tgContext, gpos, mom.normalized()).value();
0114   const Vector2 expectedResult{0, -2};
0115   CHECK_CLOSE_ABS(expectedResult, localPosition, 1e-6);
0116 
0117   // intersection
0118   {
0119     const Vector3 direction{0., 1., 2.};
0120     auto sfIntersection =
0121         line.intersect(tgContext, {0., 0., 0.}, direction.normalized(),
0122                        BoundaryTolerance::Infinite())
0123             .closest();
0124     BOOST_CHECK(sfIntersection.isValid());
0125     Vector3 expectedIntersection(0, 1., 2.);
0126     CHECK_CLOSE_ABS(sfIntersection.position(), expectedIntersection,
0127                     1e-6);  // need more tests..
0128     BOOST_CHECK_EQUAL(&sfIntersection.surface(), &line);
0129   }
0130 
0131   // isOnSurface
0132   const Vector3 insidePosition{0., 2.5, 0.};
0133   BOOST_CHECK(line.isOnSurface(
0134       tgContext, insidePosition, mom,
0135       BoundaryTolerance::Infinite()));  // need better test here
0136   const Vector3 outsidePosition{100., 100., 200.};
0137   BOOST_CHECK(!line.isOnSurface(tgContext, outsidePosition, mom,
0138                                 BoundaryTolerance::None()));
0139 
0140   // localToGlobal
0141   Vector3 returnedGlobalPosition{0., 0., 0.};
0142   // Vector2 localPosition{0., 0.};
0143   const Vector3 momentum{300., 200., 0.};  // find better values!
0144   returnedGlobalPosition =
0145       line.localToGlobal(tgContext, localPosition, momentum.normalized());
0146   const Vector3 expectedGlobalPosition{0, 1, 0};
0147   CHECK_CLOSE_ABS(returnedGlobalPosition, expectedGlobalPosition, 1e-6);
0148 
0149   // referenceFrame
0150   Vector3 globalPosition{0., 0., 0.};
0151   auto returnedRotationMatrix =
0152       line.referenceFrame(tgContext, globalPosition, momentum.normalized());
0153   double v0 = std::cos(std::atan(2. / 3.));
0154   double v1 = std::sin(std::atan(2. / 3.));
0155   RotationMatrix3 expectedRotationMatrix;
0156   expectedRotationMatrix << -v1, 0., v0, v0, 0., v1, 0., 1., -0.;
0157   CHECK_CLOSE_OR_SMALL(returnedRotationMatrix, expectedRotationMatrix, 1e-6,
0158                        1e-9);
0159 
0160   // name()
0161   boost::test_tools::output_test_stream output;
0162   output << line.name();
0163   BOOST_CHECK(output.is_equal("Acts::LineSurface"));
0164 
0165   // normal
0166   // arbitrary position, because should be irrelevant
0167   Vector3 position{5, 5, 5};  // should be irrelevant
0168   {
0169     Vector3 direction{1, 0, 0};
0170     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction), direction,
0171                     1e-6);
0172   }
0173   {
0174     Vector3 direction = Vector3{1, 0, 0.1}.normalized();
0175     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction),
0176                     Vector3::UnitX(), 1e-6);
0177   }
0178   {
0179     Vector3 direction{-1, 0, 0};
0180     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction), direction,
0181                     1e-6);
0182   }
0183   {
0184     Vector3 direction{0, 1, 0};
0185     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction), direction,
0186                     1e-6);
0187   }
0188 
0189   // pathCorrection
0190   Vector3 any3DVector = Vector3::Random();
0191   CHECK_CLOSE_REL(line.pathCorrection(tgContext, any3DVector, any3DVector), 1.,
0192                   1e-6);
0193 }
0194 
0195 /// Unit test for testing LineSurface assignment
0196 BOOST_AUTO_TEST_CASE(LineSurface_assignment_test) {
0197   Translation3 translation{0., 1., 2.};
0198   Transform3 transform(translation);
0199   LineSurfaceStub originalLine(transform, 2., 20.);
0200   LineSurfaceStub assignedLine(transform, 1., 1.);
0201   BOOST_CHECK(assignedLine != originalLine);  // operator != from base
0202   assignedLine = originalLine;
0203   BOOST_CHECK(assignedLine == originalLine);  // operator == from base
0204 }
0205 
0206 /// Unit test for testing LineSurface alignment derivatives
0207 BOOST_AUTO_TEST_CASE(LineSurfaceAlignment) {
0208   Translation3 translation{0., 1., 2.};
0209   Transform3 transform(translation);
0210   LineSurfaceStub line(transform, 2., 20.);
0211 
0212   const auto& rotation = transform.rotation();
0213   // The local frame z axis
0214   const Vector3 localZAxis = rotation.col(2);
0215   // Check the local z axis is aligned to global z axis
0216   CHECK_CLOSE_ABS(localZAxis, Vector3(0., 0., 1.), 1e-15);
0217 
0218   // Define the track (global) position and direction
0219   Vector3 globalPosition{1, 2, 4};
0220   Vector3 momentum{-1, 1, 1};
0221   Vector3 direction = momentum.normalized();
0222 
0223   // (a) Test the derivative of path length w.r.t. alignment parameters
0224   const AlignmentToPathMatrix& alignToPath =
0225       line.alignmentToPathDerivative(tgContext, globalPosition, direction);
0226   // The expected results
0227   AlignmentToPathMatrix expAlignToPath = AlignmentToPathMatrix::Zero();
0228   const double value = std::numbers::sqrt3 / 2;
0229   expAlignToPath << -value, value, 0, -3 * value, -value, 0;
0230   // Check if the calculated derivative is as expected
0231   CHECK_CLOSE_ABS(alignToPath, expAlignToPath, 1e-10);
0232 
0233   // (b) Test the derivative of bound track parameters local position w.r.t.
0234   // position in local 3D Cartesian coordinates
0235   const auto& loc3DToLocBound =
0236       line.localCartesianToBoundLocalDerivative(tgContext, globalPosition);
0237   // Check if the result is as expected
0238   ActsMatrix<2, 3> expLoc3DToLocBound = ActsMatrix<2, 3>::Zero();
0239   expLoc3DToLocBound << 1 / std::numbers::sqrt2, 1 / std::numbers::sqrt2, 0, 0,
0240       0, 1;
0241   CHECK_CLOSE_ABS(loc3DToLocBound, expLoc3DToLocBound, 1e-10);
0242 }
0243 
0244 BOOST_AUTO_TEST_CASE(LineSurfaceTransformRoundTrip) {
0245   LineSurfaceStub surface(Transform3::Identity());
0246 
0247   auto roundTrip = [&surface](const Vector3& pos, const Vector3& dir) {
0248     auto intersection = surface.intersect(tgContext, pos, dir).closest();
0249     Vector3 global = intersection.position();
0250     Vector2 local = *surface.globalToLocal(tgContext, global, dir);
0251     Vector3 global2 = surface.localToGlobal(tgContext, local, dir);
0252     return std::make_tuple(global, local, global2);
0253   };
0254 
0255   {
0256     Vector3 pos = {-0.02801, 0.00475611, 0.285106};
0257     Vector3 dir = Vector3(-0.03951, -0.221457, -0.564298).normalized();
0258 
0259     auto [global, local, global2] = roundTrip(pos, dir);
0260 
0261     CHECK_CLOSE_ABS(global, global2, 1e-10);
0262   }
0263 
0264   {
0265     Vector3 pos = {-64.2892, 65.2697, -0.839014};
0266     Vector3 dir = Vector3(-0.236602, -0.157616, 0.956786).normalized();
0267 
0268     auto [global, local, global2] = roundTrip(pos, dir);
0269 
0270     CHECK_CLOSE_ABS(global, global2, 1e-10);
0271   }
0272 }
0273 
0274 BOOST_AUTO_TEST_CASE(LineSurfaceTransformRoundTripEtaStability) {
0275   LineSurfaceStub surface(Transform3::Identity());
0276 
0277   // eta=6 is already crashing
0278   const std::vector<double> etas = {0, 1, 2, 3, 4, 5};
0279 
0280   for (double eta : etas) {
0281     Vector3 pca = {5, 0, 0};
0282     Vector3 dir = makeDirectionFromPhiEta(std::numbers::pi / 2., eta);
0283     Vector3 pos = pca + dir;
0284 
0285     auto intersection = surface.intersect(tgContext, pos, dir).closest();
0286 
0287     Vector3 global = intersection.position();
0288     Vector2 local = *surface.globalToLocal(tgContext, global, dir);
0289     Vector3 global2 = surface.localToGlobal(tgContext, local, dir);
0290 
0291     CHECK_CLOSE_ABS(global, global2, 1e-10);
0292     CHECK_CLOSE_ABS(pca, global2, 1e-10);
0293   }
0294 }
0295 
0296 BOOST_AUTO_TEST_CASE(LineSurfaceIntersection) {
0297   using namespace Acts::UnitLiterals;
0298 
0299   double eps = 1e-10;
0300 
0301   Vector3 direction = Vector3(1, 1, 100).normalized();
0302   BoundVector boundVector;
0303   boundVector << 1_cm, 1_cm, VectorHelpers::phi(direction),
0304       VectorHelpers::theta(direction), 1, 0;
0305   double pathLimit = 1_cm;
0306 
0307   auto surface = std::make_shared<LineSurfaceStub>(Transform3::Identity());
0308 
0309   BoundTrackParameters initialParams{surface, boundVector, std::nullopt,
0310                                      ParticleHypothesis::pion()};
0311 
0312   using Propagator = Propagator<StraightLineStepper>;
0313   using PropagatorOptions = Propagator::Options<>;
0314 
0315   Propagator propagator({});
0316 
0317   BoundTrackParameters displacedParameters =
0318       BoundTrackParameters::createCurvilinear(Vector4::Zero(), Vector3::Zero(),
0319                                               1, std::nullopt,
0320                                               ParticleHypothesis::pion());
0321   {
0322     PropagatorOptions options(tgContext, {});
0323     options.direction = Acts::Direction::Backward();
0324     options.pathLimit = pathLimit;
0325 
0326     auto result = propagator.propagate(initialParams, options);
0327     BOOST_CHECK(result.ok());
0328     BOOST_CHECK(result.value().endParameters);
0329 
0330     displacedParameters = result.value().endParameters.value();
0331   }
0332 
0333   auto intersection =
0334       surface
0335           ->intersect(tgContext, displacedParameters.position(tgContext),
0336                       displacedParameters.direction())
0337           .closest();
0338   CHECK_CLOSE_ABS(intersection.pathLength(), pathLimit, eps);
0339 
0340   BoundTrackParameters endParameters{surface,
0341                                      detail::Test::someBoundParametersA(),
0342                                      std::nullopt, ParticleHypothesis::pion()};
0343   {
0344     PropagatorOptions options(tgContext, {});
0345     options.direction = Acts::Direction::Forward();
0346     options.stepping.maxStepSize = 1_mm;
0347 
0348     auto result = propagator.propagate(displacedParameters, *surface, options);
0349     BOOST_CHECK(result.ok());
0350     BOOST_CHECK(result.value().endParameters);
0351     CHECK_CLOSE_ABS(result.value().pathLength, pathLimit, eps);
0352     endParameters = result.value().endParameters.value();
0353   }
0354 
0355   CHECK_CLOSE_ABS(initialParams.parameters(), endParameters.parameters(), eps);
0356 }
0357 
0358 BOOST_AUTO_TEST_SUITE_END()
0359 
0360 }  // namespace Acts::Test