Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-20 08:56:08

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/Utilities/Intersection.hpp"
0026 #include "Acts/Utilities/Result.hpp"
0027 #include "Acts/Utilities/ThrowAssert.hpp"
0028 #include "Acts/Utilities/UnitVectors.hpp"
0029 #include "Acts/Utilities/VectorHelpers.hpp"
0030 #include "ActsTests/CommonHelpers/DetectorElementStub.hpp"
0031 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0032 #include "ActsTests/CommonHelpers/LineSurfaceStub.hpp"
0033 #include "ActsTests/CommonHelpers/PredefinedMaterials.hpp"
0034 
0035 #include <cmath>
0036 #include <memory>
0037 #include <numbers>
0038 #include <optional>
0039 #include <vector>
0040 
0041 using namespace Acts;
0042 
0043 namespace ActsTests {
0044 
0045 // Create a test context
0046 GeometryContext tgContext = GeometryContext();
0047 
0048 BOOST_AUTO_TEST_SUITE(SurfacesSuite)
0049 
0050 /// Unit test for creating compliant/non-compliant LineSurface object
0051 BOOST_AUTO_TEST_CASE(LineSurface_Constructors_test) {
0052   /// Test default construction
0053   // default construction is deleted
0054 
0055   Translation3 translation{0., 1., 2.};
0056   Transform3 transform(translation);
0057   auto pTransform = Transform3(translation);
0058   const double radius = 2.;
0059   const double halfZ = 20.;
0060   BOOST_CHECK(LineSurfaceStub(pTransform, radius, halfZ).constructedOk());
0061 
0062   /// ctor with nullptr for LineBounds
0063   BOOST_CHECK(LineSurfaceStub(pTransform).constructedOk());
0064 
0065   /// ctor with LineBounds
0066   auto pLineBounds = std::make_shared<const LineBounds>(2., 10.);
0067   BOOST_CHECK(LineSurfaceStub(pTransform, pLineBounds).constructedOk());
0068 
0069   /// ctor with LineBounds, detector element, Identifier
0070   auto pMaterial =
0071       std::make_shared<const HomogeneousSurfaceMaterial>(makePercentSlab());
0072   DetectorElementStub detElement{pTransform, pLineBounds, 0.2, pMaterial};
0073   BOOST_CHECK(LineSurfaceStub(pLineBounds, detElement).constructedOk());
0074   LineSurfaceStub lineToCopy(pTransform, 2., 20.);
0075 
0076   /// Copy ctor
0077   BOOST_CHECK(LineSurfaceStub(lineToCopy).constructedOk());
0078 
0079   /// Copied and transformed ctor
0080   BOOST_CHECK(
0081       LineSurfaceStub(tgContext, lineToCopy, transform).constructedOk());
0082 
0083   /// Construct with nullptr bounds
0084   DetectorElementStub detElem;
0085   BOOST_CHECK_THROW(LineSurfaceStub nullBounds(nullptr, detElem),
0086                     AssertionFailureException);
0087 
0088   BOOST_TEST_MESSAGE(
0089       "All LineSurface constructors are callable without problem");
0090 }
0091 
0092 /// Unit tests of all named methods
0093 BOOST_AUTO_TEST_CASE(LineSurface_allNamedMethods_test) {
0094   // referencePosition()
0095   Translation3 translation{0., 1., 2.};
0096   Transform3 transform(translation);
0097   LineSurfaceStub line(transform, 2., 20.);
0098   Vector3 referencePosition{0., 1., 2.};
0099   CHECK_CLOSE_ABS(referencePosition,
0100                   line.referencePosition(tgContext, AxisDirection::AxisX),
0101                   1e-6);
0102 
0103   // bounds()
0104   auto pLineBounds = std::make_shared<const LineBounds>(2., 10.);
0105   LineSurfaceStub boundedLine(transform, pLineBounds);
0106   const LineBounds& bounds =
0107       dynamic_cast<const LineBounds&>(boundedLine.bounds());
0108   BOOST_CHECK_EQUAL(bounds, LineBounds(2., 10.));
0109 
0110   // globalToLocal()
0111   Vector3 gpos{0., 1., 0.};
0112   const Vector3 mom{20., 0., 0.};  // needs more realistic parameters
0113   Vector2 localPosition =
0114       line.globalToLocal(tgContext, gpos, mom.normalized()).value();
0115   const Vector2 expectedResult{0, -2};
0116   CHECK_CLOSE_ABS(expectedResult, localPosition, 1e-6);
0117 
0118   // intersection
0119   {
0120     const Vector3 direction{0., 1., 2.};
0121     Intersection3D sfIntersection =
0122         line.intersect(tgContext, {0., 0., 0.}, direction.normalized(),
0123                        BoundaryTolerance::Infinite())
0124             .closest();
0125     BOOST_CHECK(sfIntersection.isValid());
0126     Vector3 expectedIntersection(0, 1., 2.);
0127     CHECK_CLOSE_ABS(sfIntersection.position(), expectedIntersection,
0128                     1e-6);  // need more tests..
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     Intersection3D intersection =
0249         surface.intersect(tgContext, pos, dir).closest();
0250     Vector3 global = intersection.position();
0251     Vector2 local = *surface.globalToLocal(tgContext, global, dir);
0252     Vector3 global2 = surface.localToGlobal(tgContext, local, dir);
0253     return std::make_tuple(global, local, global2);
0254   };
0255 
0256   {
0257     Vector3 pos = {-0.02801, 0.00475611, 0.285106};
0258     Vector3 dir = Vector3(-0.03951, -0.221457, -0.564298).normalized();
0259 
0260     auto [global, local, global2] = roundTrip(pos, dir);
0261 
0262     CHECK_CLOSE_ABS(global, global2, 1e-10);
0263   }
0264 
0265   {
0266     Vector3 pos = {-64.2892, 65.2697, -0.839014};
0267     Vector3 dir = Vector3(-0.236602, -0.157616, 0.956786).normalized();
0268 
0269     auto [global, local, global2] = roundTrip(pos, dir);
0270 
0271     CHECK_CLOSE_ABS(global, global2, 1e-10);
0272   }
0273 }
0274 
0275 BOOST_AUTO_TEST_CASE(LineSurfaceTransformRoundTripEtaStability) {
0276   LineSurfaceStub surface(Transform3::Identity());
0277 
0278   // eta=6 is already crashing
0279   const std::vector<double> etas = {0, 1, 2, 3, 4, 5};
0280 
0281   for (double eta : etas) {
0282     Vector3 pca = {5, 0, 0};
0283     Vector3 dir = makeDirectionFromPhiEta(std::numbers::pi / 2., eta);
0284     Vector3 pos = pca + dir;
0285 
0286     Intersection3D intersection =
0287         surface.intersect(tgContext, pos, dir).closest();
0288 
0289     Vector3 global = intersection.position();
0290     Vector2 local = *surface.globalToLocal(tgContext, global, dir);
0291     Vector3 global2 = surface.localToGlobal(tgContext, local, dir);
0292 
0293     CHECK_CLOSE_ABS(global, global2, 1e-10);
0294     CHECK_CLOSE_ABS(pca, global2, 1e-10);
0295   }
0296 }
0297 
0298 BOOST_AUTO_TEST_CASE(LineSurfaceIntersection) {
0299   using namespace Acts::UnitLiterals;
0300 
0301   double eps = 1e-10;
0302 
0303   Vector3 direction = Vector3(1, 1, 100).normalized();
0304   BoundVector boundVector;
0305   boundVector << 1_cm, 1_cm, VectorHelpers::phi(direction),
0306       VectorHelpers::theta(direction), 1, 0;
0307   double pathLimit = 1_cm;
0308 
0309   auto surface = std::make_shared<LineSurfaceStub>(Transform3::Identity());
0310 
0311   BoundTrackParameters initialParams{surface, boundVector, std::nullopt,
0312                                      ParticleHypothesis::pion()};
0313 
0314   using Propagator = Propagator<StraightLineStepper>;
0315   using PropagatorOptions = Propagator::Options<>;
0316 
0317   Propagator propagator({});
0318 
0319   BoundTrackParameters displacedParameters =
0320       BoundTrackParameters::createCurvilinear(Vector4::Zero(), Vector3::Zero(),
0321                                               1, std::nullopt,
0322                                               ParticleHypothesis::pion());
0323   {
0324     PropagatorOptions options(tgContext, {});
0325     options.direction = Acts::Direction::Backward();
0326     options.pathLimit = pathLimit;
0327 
0328     auto result = propagator.propagate(initialParams, options);
0329     BOOST_CHECK(result.ok());
0330     BOOST_CHECK(result.value().endParameters);
0331 
0332     displacedParameters = result.value().endParameters.value();
0333   }
0334 
0335   Intersection3D intersection =
0336       surface
0337           ->intersect(tgContext, displacedParameters.position(tgContext),
0338                       displacedParameters.direction())
0339           .closest();
0340   CHECK_CLOSE_ABS(intersection.pathLength(), pathLimit, eps);
0341 
0342   BoundTrackParameters endParameters{surface,
0343                                      detail::Test::someBoundParametersA(),
0344                                      std::nullopt, ParticleHypothesis::pion()};
0345   {
0346     PropagatorOptions options(tgContext, {});
0347     options.direction = Acts::Direction::Forward();
0348     options.stepping.maxStepSize = 1_mm;
0349 
0350     auto result = propagator.propagate(displacedParameters, *surface, options);
0351     BOOST_CHECK(result.ok());
0352     BOOST_CHECK(result.value().endParameters);
0353     CHECK_CLOSE_ABS(result.value().pathLength, pathLimit, eps);
0354     endParameters = result.value().endParameters.value();
0355   }
0356 
0357   CHECK_CLOSE_ABS(initialParams.parameters(), endParameters.parameters(), eps);
0358 }
0359 
0360 BOOST_AUTO_TEST_SUITE_END()
0361 
0362 }  // namespace ActsTests