Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-22 09:24:22

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