Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-25 07:35:39

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/detail/GenerateParameters.hpp"
0017 #include "Acts/Geometry/GeometryContext.hpp"
0018 #include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
0019 #include "Acts/Propagator/Propagator.hpp"
0020 #include "Acts/Propagator/StraightLineStepper.hpp"
0021 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0022 #include "Acts/Surfaces/LineBounds.hpp"
0023 #include "Acts/Surfaces/Surface.hpp"
0024 #include "Acts/Utilities/Intersection.hpp"
0025 #include "Acts/Utilities/Result.hpp"
0026 #include "Acts/Utilities/ThrowAssert.hpp"
0027 #include "Acts/Utilities/UnitVectors.hpp"
0028 #include "Acts/Utilities/VectorHelpers.hpp"
0029 #include "ActsTests/CommonHelpers/DetectorElementStub.hpp"
0030 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0031 #include "ActsTests/CommonHelpers/LineSurfaceStub.hpp"
0032 #include "ActsTests/CommonHelpers/PredefinedMaterials.hpp"
0033 
0034 #include <cmath>
0035 #include <memory>
0036 #include <numbers>
0037 #include <optional>
0038 #include <vector>
0039 
0040 using namespace Acts;
0041 
0042 namespace ActsTests {
0043 
0044 // Create a test context
0045 GeometryContext tgContext = GeometryContext::dangerouslyDefaultConstruct();
0046 
0047 BOOST_AUTO_TEST_SUITE(SurfacesSuite)
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     Intersection3D 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   }
0129 
0130   // isOnSurface
0131   const Vector3 insidePosition{0., 2.5, 0.};
0132   BOOST_CHECK(line.isOnSurface(
0133       tgContext, insidePosition, mom,
0134       BoundaryTolerance::Infinite()));  // need better test here
0135   const Vector3 outsidePosition{100., 100., 200.};
0136   BOOST_CHECK(!line.isOnSurface(tgContext, outsidePosition, mom,
0137                                 BoundaryTolerance::None()));
0138 
0139   // localToGlobal
0140   Vector3 returnedGlobalPosition{0., 0., 0.};
0141   // Vector2 localPosition{0., 0.};
0142   const Vector3 momentum{300., 200., 0.};  // find better values!
0143   returnedGlobalPosition =
0144       line.localToGlobal(tgContext, localPosition, momentum.normalized());
0145   const Vector3 expectedGlobalPosition{0, 1, 0};
0146   CHECK_CLOSE_ABS(returnedGlobalPosition, expectedGlobalPosition, 1e-6);
0147 
0148   // referenceFrame
0149   Vector3 globalPosition{0., 0., 0.};
0150   auto returnedRotationMatrix =
0151       line.referenceFrame(tgContext, globalPosition, momentum.normalized());
0152   double v0 = std::cos(std::atan(2. / 3.));
0153   double v1 = std::sin(std::atan(2. / 3.));
0154   RotationMatrix3 expectedRotationMatrix;
0155   expectedRotationMatrix << -v1, 0., v0, v0, 0., v1, 0., 1., -0.;
0156   CHECK_CLOSE_OR_SMALL(returnedRotationMatrix, expectedRotationMatrix, 1e-6,
0157                        1e-9);
0158 
0159   // name()
0160   boost::test_tools::output_test_stream output;
0161   output << line.name();
0162   BOOST_CHECK(output.is_equal("Acts::LineSurface"));
0163 
0164   // normal
0165   // arbitrary position, because should be irrelevant
0166   Vector3 position{5, 5, 5};  // should be irrelevant
0167   {
0168     Vector3 direction{1, 0, 0};
0169     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction), direction,
0170                     1e-6);
0171   }
0172   {
0173     Vector3 direction = Vector3{1, 0, 0.1}.normalized();
0174     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction),
0175                     Vector3::UnitX(), 1e-6);
0176   }
0177   {
0178     Vector3 direction{-1, 0, 0};
0179     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction), direction,
0180                     1e-6);
0181   }
0182   {
0183     Vector3 direction{0, 1, 0};
0184     CHECK_CLOSE_ABS(line.normal(tgContext, position, direction), direction,
0185                     1e-6);
0186   }
0187 
0188   // pathCorrection
0189   Vector3 any3DVector = Vector3::Random();
0190   CHECK_CLOSE_REL(line.pathCorrection(tgContext, any3DVector, any3DVector), 1.,
0191                   1e-6);
0192 }
0193 
0194 /// Unit test for testing LineSurface assignment
0195 BOOST_AUTO_TEST_CASE(LineSurface_assignment_test) {
0196   Translation3 translation{0., 1., 2.};
0197   Transform3 transform(translation);
0198   LineSurfaceStub originalLine(transform, 2., 20.);
0199   LineSurfaceStub assignedLine(transform, 1., 1.);
0200   BOOST_CHECK(assignedLine != originalLine);  // operator != from base
0201   assignedLine = originalLine;
0202   BOOST_CHECK(assignedLine == originalLine);  // operator == from base
0203 }
0204 
0205 /// Unit test for testing LineSurface alignment derivatives
0206 BOOST_AUTO_TEST_CASE(LineSurfaceAlignment) {
0207   Translation3 translation{0., 1., 2.};
0208   Transform3 transform(translation);
0209   LineSurfaceStub line(transform, 2., 20.);
0210 
0211   const auto& rotation = transform.rotation();
0212   // The local frame z axis
0213   const Vector3 localZAxis = rotation.col(2);
0214   // Check the local z axis is aligned to global z axis
0215   CHECK_CLOSE_ABS(localZAxis, Vector3(0., 0., 1.), 1e-15);
0216 
0217   // Define the track (global) position and direction
0218   Vector3 globalPosition{1, 2, 4};
0219   Vector3 momentum{-1, 1, 1};
0220   Vector3 direction = momentum.normalized();
0221 
0222   // (a) Test the derivative of path length w.r.t. alignment parameters
0223   const AlignmentToPathMatrix& alignToPath =
0224       line.alignmentToPathDerivative(tgContext, globalPosition, direction);
0225   // The expected results
0226   AlignmentToPathMatrix expAlignToPath = AlignmentToPathMatrix::Zero();
0227   const double value = std::numbers::sqrt3 / 2;
0228   expAlignToPath << -value, value, 0, -3 * value, -value, 0;
0229   // Check if the calculated derivative is as expected
0230   CHECK_CLOSE_ABS(alignToPath, expAlignToPath, 1e-10);
0231 
0232   // (b) Test the derivative of bound track parameters local position w.r.t.
0233   // position in local 3D Cartesian coordinates
0234   const auto& loc3DToLocBound =
0235       line.localCartesianToBoundLocalDerivative(tgContext, globalPosition);
0236   // Check if the result is as expected
0237   Matrix<2, 3> expLoc3DToLocBound = Matrix<2, 3>::Zero();
0238   expLoc3DToLocBound << 1 / std::numbers::sqrt2, 1 / std::numbers::sqrt2, 0, 0,
0239       0, 1;
0240   CHECK_CLOSE_ABS(loc3DToLocBound, expLoc3DToLocBound, 1e-10);
0241 }
0242 
0243 BOOST_AUTO_TEST_CASE(LineSurfaceTransformRoundTrip) {
0244   LineSurfaceStub surface(Transform3::Identity());
0245 
0246   auto roundTrip = [&surface](const Vector3& pos, const Vector3& dir) {
0247     Intersection3D intersection =
0248         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     Intersection3D intersection =
0286         surface.intersect(tgContext, pos, dir).closest();
0287 
0288     Vector3 global = intersection.position();
0289     Vector2 local = *surface.globalToLocal(tgContext, global, dir);
0290     Vector3 global2 = surface.localToGlobal(tgContext, local, dir);
0291 
0292     CHECK_CLOSE_ABS(global, global2, 1e-10);
0293     CHECK_CLOSE_ABS(pca, global2, 1e-10);
0294   }
0295 }
0296 
0297 BOOST_AUTO_TEST_CASE(LineSurfaceIntersection) {
0298   using namespace Acts::UnitLiterals;
0299 
0300   double eps = 1e-10;
0301 
0302   Vector3 direction = Vector3(1, 1, 100).normalized();
0303   BoundVector boundVector;
0304   boundVector << 1_cm, 1_cm, VectorHelpers::phi(direction),
0305       VectorHelpers::theta(direction), 1, 0;
0306   double pathLimit = 1_cm;
0307 
0308   auto surface = std::make_shared<LineSurfaceStub>(Transform3::Identity());
0309 
0310   BoundTrackParameters initialParams{surface, boundVector, std::nullopt,
0311                                      ParticleHypothesis::pion()};
0312 
0313   using Propagator = Propagator<StraightLineStepper>;
0314   using PropagatorOptions = Propagator::Options<>;
0315 
0316   Propagator propagator({});
0317 
0318   BoundTrackParameters displacedParameters =
0319       BoundTrackParameters::createCurvilinear(Vector4::Zero(), Vector3::Zero(),
0320                                               1, std::nullopt,
0321                                               ParticleHypothesis::pion());
0322   {
0323     PropagatorOptions options(tgContext, {});
0324     options.direction = Acts::Direction::Backward();
0325     options.pathLimit = pathLimit;
0326 
0327     auto result = propagator.propagate(initialParams, options);
0328     BOOST_CHECK(result.ok());
0329     BOOST_CHECK(result.value().endParameters);
0330 
0331     displacedParameters = result.value().endParameters.value();
0332   }
0333 
0334   Intersection3D intersection =
0335       surface
0336           ->intersect(tgContext, displacedParameters.position(tgContext),
0337                       displacedParameters.direction())
0338           .closest();
0339   CHECK_CLOSE_ABS(intersection.pathLength(), pathLimit, eps);
0340 
0341   BoundTrackParameters endParameters{surface,
0342                                      detail::Test::someBoundParametersA(),
0343                                      std::nullopt, ParticleHypothesis::pion()};
0344   {
0345     PropagatorOptions options(tgContext, {});
0346     options.direction = Acts::Direction::Forward();
0347     options.stepping.maxStepSize = 1_mm;
0348 
0349     auto result = propagator.propagate(displacedParameters, *surface, options);
0350     BOOST_CHECK(result.ok());
0351     BOOST_CHECK(result.value().endParameters);
0352     CHECK_CLOSE_ABS(result.value().pathLength, pathLimit, eps);
0353     endParameters = result.value().endParameters.value();
0354   }
0355 
0356   CHECK_CLOSE_ABS(initialParams.parameters(), endParameters.parameters(), eps);
0357 }
0358 
0359 BOOST_AUTO_TEST_SUITE_END()
0360 
0361 }  // namespace ActsTests