Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 09:05:32

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/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/EventData/BoundTrackParameters.hpp"
0014 #include "Acts/EventData/SourceLink.hpp"
0015 #include "Acts/Geometry/GeometryContext.hpp"
0016 #include "Acts/Geometry/GeometryIdentifier.hpp"
0017 #include "Acts/Surfaces/RectangleBounds.hpp"
0018 #include "Acts/Surfaces/detail/AlignmentHelper.hpp"
0019 #include "Acts/Utilities/Logger.hpp"
0020 #include "Acts/Utilities/Result.hpp"
0021 #include "ActsAlignment/Geometry/AlignableStructure.hpp"
0022 #include "ActsAlignment/Geometry/AlignmentHierarchy.hpp"
0023 #include "ActsAlignment/Kernel/Alignment.hpp"
0024 #include "ActsAlignment/Kernel/detail/AlignmentEngine.hpp"
0025 #include "ActsTests/CommonHelpers/DetectorElementStub.hpp"
0026 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0027 
0028 #include <array>
0029 #include <memory>
0030 #include <stdexcept>
0031 #include <vector>
0032 
0033 namespace {
0034 
0035 using namespace Acts;
0036 using namespace ActsAlignment;
0037 using namespace Acts::UnitLiterals;
0038 
0039 // Utility to create a dummy surface and detector element
0040 auto makeDummyElement(const Acts::Transform3& trafo) {
0041   auto bounds = std::make_shared<const Acts::RectangleBounds>(10_mm, 10_mm);
0042   return std::make_shared<ActsTests::DetectorElementStub>(trafo, bounds, 1_um);
0043 }
0044 
0045 // Dummy options for the test
0046 struct DummyFitOptions {
0047   Acts::GeometryContext geoContext =
0048       Acts::GeometryContext::dangerouslyDefaultConstruct();
0049   const Acts::Surface* referenceSurface = nullptr;
0050 };
0051 
0052 // Unique track type for hierarchy validation tests to avoid name collisions
0053 struct HierarchyValidationTestTrack {
0054   std::size_t tipIndex() const { return 0; }
0055 };
0056 
0057 // Dummy fitter for alignment
0058 struct DummyFitter {
0059   template <typename it_t, typename start_parameters_t, typename fit_options_t,
0060             typename track_container_t>
0061   Acts::Result<HierarchyValidationTestTrack> fit(
0062       it_t /*begin*/, it_t /*end*/,
0063       const start_parameters_t& /*startParameters*/,
0064       const fit_options_t& /*fitOptions*/,
0065       track_container_t& /*tracks*/) const {
0066     return Acts::Result<HierarchyValidationTestTrack>::success(
0067         HierarchyValidationTestTrack{});
0068   }
0069 };
0070 
0071 struct DerivativeFrames {
0072   Transform3 structure;
0073   Transform3 surface;
0074 };
0075 
0076 class ContextualDerivativeElement final
0077     : public ActsTests::DetectorElementStub {
0078  public:
0079   ContextualDerivativeElement()
0080       : DetectorElementStub(
0081             Transform3::Identity(),
0082             std::make_shared<const RectangleBounds>(1000_mm, 1000_mm), 1_um) {}
0083 
0084   const Transform3& localToGlobalTransform(
0085       const GeometryContext& gctx) const override {
0086     return gctx.get<DerivativeFrames>().surface;
0087   }
0088 };
0089 
0090 struct StructureDerivativeFixture {
0091   DerivativeFrames frames;
0092   ContextualDerivativeElement element;
0093   std::shared_ptr<AlignableStructure> structure;
0094   Vector3 trackOrigin = Vector3::Zero();
0095   Vector3 direction = Vector3::Zero();
0096   FreeVector pathDerivative = FreeVector::Zero();
0097   Matrix<2, eBoundSize> projector = Matrix<2, eBoundSize>::Zero();
0098 
0099   StructureDerivativeFixture() {
0100     frames.structure = Translation3(100_mm, -40_mm, 300_mm) *
0101                        AngleAxis3(0.6_rad, Vector3(1., 2., -1.).normalized());
0102     frames.surface = frames.structure * Translation3(30_mm, -20_mm, 50_mm) *
0103                      AngleAxis3(-0.8_rad, Vector3(2., -1., 3.).normalized());
0104     structure = std::make_shared<AlignableStructure>(
0105         GeometryIdentifier().withVolume(1), [](const GeometryContext& gctx) {
0106           return gctx.get<DerivativeFrames>().structure;
0107         });
0108     structure->addSurface(element.surface().getSharedPtr());
0109     structure->alignmentMask() = AlignmentMask::All;
0110     trackOrigin = frames.surface * Vector3(3_mm, -7_mm, 0.);
0111     direction =
0112         (frames.surface.rotation() * Vector3(0.3, -0.2, 1.)).normalized();
0113     pathDerivative.segment<3>(eFreePos0) = direction;
0114     // Reverse measurement order to exercise the bound-to-measurement
0115     // projection.
0116     projector(0, eBoundLoc1) = 1.;
0117     projector(1, eBoundLoc0) = 1.;
0118   }
0119 
0120   Vector3 intersection(const DerivativeFrames& state) const {
0121     const Vector3 normal = state.surface.rotation().col(2);
0122     const double path = normal.dot(state.surface.translation() - trackOrigin) /
0123                         normal.dot(direction);
0124     return trackOrigin + path * direction;
0125   }
0126 
0127   Vector2 residual(const DerivativeFrames& state) const {
0128     const GeometryContext gctx{state};
0129     BoundVector prediction = BoundVector::Zero();
0130     prediction.segment<2>(eBoundLoc0) =
0131         element.surface()
0132             .globalToLocal(gctx, intersection(state), direction)
0133             .value();
0134     return Vector2(1_mm, -2_mm) - projector * prediction;
0135   }
0136 
0137   DerivativeFrames moved(const DerivativeFrames& state, std::size_t parameter,
0138                          double step) const {
0139     Transform3 delta = Transform3::Identity();
0140     if (parameter < eAlignmentRotation0) {
0141       delta.translation()[parameter] = step;
0142     } else {
0143       delta.linear() =
0144           AngleAxis3(step, Vector3::Unit(parameter - eAlignmentRotation0))
0145               .toRotationMatrix();
0146     }
0147     const Transform3 current = state.structure * delta;
0148     return {current, current * state.structure.inverse() * state.surface};
0149   }
0150 };
0151 
0152 }  // namespace
0153 
0154 BOOST_AUTO_TEST_CASE(StructureResidualDerivative) {
0155   const StructureDerivativeFixture fixture;
0156   const AlignmentHierarchy hierarchy({fixture.structure});
0157   BOOST_REQUIRE(hierarchy.validate().ok());
0158   const auto* owner = hierarchy.structureFor(fixture.element.surface());
0159   BOOST_REQUIRE_EQUAL(owner, fixture.structure.get());
0160 
0161   // Both contexts are displaced from the nominal element and have different
0162   // structure/member poses. Hold the same global straight track in both.
0163   const Transform3 motion =
0164       Translation3(-12_mm, 8_mm, 5_mm) * AngleAxis3(-0.2_rad, Vector3::UnitY());
0165   const std::array states{fixture.frames,
0166                           DerivativeFrames{motion * fixture.frames.structure,
0167                                            motion * fixture.frames.surface}};
0168   for (std::size_t contextIndex = 0; contextIndex < states.size();
0169        ++contextIndex) {
0170     const auto& state = states[contextIndex];
0171     const GeometryContext gctx{state};
0172     const Transform3 frame = owner->localToGlobalTransform(gctx);
0173     const Matrix<2, eAlignmentSize> analytical =
0174         -fixture.projector * ActsAlignment::detail::alignmentToBoundDerivative(
0175                                  gctx, fixture.element.surface(),
0176                                  fixture.intersection(state), fixture.direction,
0177                                  fixture.pathDerivative, owner->alignmentMask(),
0178                                  &frame);
0179     for (double scale : std::array{1e-5, 2e-6}) {
0180       for (std::size_t parameter = 0; parameter < eAlignmentSize; ++parameter) {
0181         BOOST_TEST_CONTEXT("context=" << contextIndex << ", parameter="
0182                                       << parameter << ", scale=" << scale) {
0183           const double step =
0184               scale * (parameter < eAlignmentRotation0 ? 1_mm : 1_rad);
0185           const Vector2 numerical =
0186               (fixture.residual(fixture.moved(state, parameter, step)) -
0187                fixture.residual(fixture.moved(state, parameter, -step))) /
0188               (2. * step);
0189           CHECK_CLOSE_ABS(analytical.col(parameter), numerical, 1e-6);
0190         }
0191       }
0192     }
0193   }
0194 }
0195 
0196 BOOST_AUTO_TEST_CASE(StructureDerivativeMaskAfterConversion) {
0197   const StructureDerivativeFixture fixture;
0198   const GeometryContext gctx{fixture.frames};
0199   const Transform3 frame = fixture.structure->localToGlobalTransform(gctx);
0200   const auto& surface = fixture.element.surface();
0201   const Vector3 position = fixture.intersection(fixture.frames);
0202   const AlignmentMask mask = AlignmentMask::Center0 | AlignmentMask::Rotation2;
0203   const AlignmentToBoundMatrix masked =
0204       ActsAlignment::detail::alignmentToBoundDerivative(
0205           gctx, surface, position, fixture.direction, fixture.pathDerivative,
0206           mask, &frame);
0207   for (std::size_t parameter = 0; parameter < eAlignmentSize; ++parameter) {
0208     if (parameter == eAlignmentCenter0 || parameter == eAlignmentRotation2) {
0209       const double step =
0210           2e-6 * (parameter < eAlignmentRotation0 ? 1_mm : 1_rad);
0211       const Vector2 numerical =
0212           (fixture.residual(fixture.moved(fixture.frames, parameter, step)) -
0213            fixture.residual(fixture.moved(fixture.frames, parameter, -step))) /
0214           (2. * step);
0215       const Vector2 analytical = -fixture.projector * masked.col(parameter);
0216       CHECK_CLOSE_ABS(analytical, numerical, 1e-6);
0217     } else {
0218       BOOST_CHECK_EQUAL(masked.col(parameter).squaredNorm(), 0.);
0219     }
0220   }
0221   const AlignmentToBoundMatrix fixed =
0222       ActsAlignment::detail::alignmentToBoundDerivative(
0223           gctx, surface, position, fixture.direction, fixture.pathDerivative,
0224           AlignmentMask::None, &frame);
0225   BOOST_CHECK_EQUAL(fixed.squaredNorm(), 0.);
0226 
0227   // Verify that this rotated geometry distinguishes premature masking and
0228   // the obsolete conversion of surface translations to global coordinates.
0229   const AlignmentToBoundMatrix raw = surface.alignmentToBoundDerivative(
0230       gctx, position, fixture.direction, fixture.pathDerivative);
0231   AlignmentToBoundMatrix prematureMask = raw;
0232   ActsAlignment::detail::resetAlignmentDerivative(prematureMask, mask);
0233   const AlignmentMatrix composite =
0234       Acts::detail::compositeToComponentJacobian(frame, fixture.frames.surface);
0235   const Vector2 wronglyMasked =
0236       -fixture.projector * prematureMask * composite.col(eAlignmentCenter0);
0237   AlignmentMatrix obsoleteAdapter = AlignmentMatrix::Identity();
0238   obsoleteAdapter.block<3, 3>(eAlignmentCenter0, eAlignmentCenter0) =
0239       fixture.frames.surface.rotation();
0240   const Vector2 wronglyAdapted = -fixture.projector * raw * obsoleteAdapter *
0241                                  composite.col(eAlignmentCenter0);
0242   const Vector2 correct = -fixture.projector * masked.col(eAlignmentCenter0);
0243   BOOST_CHECK_GT((wronglyMasked - correct).norm(), 1e-2);
0244   BOOST_CHECK_GT((wronglyAdapted - correct).norm(), 1e-2);
0245 }
0246 
0247 BOOST_AUTO_TEST_CASE(StandaloneDerivativeCompatibility) {
0248   const StructureDerivativeFixture fixture;
0249   const GeometryContext gctx{fixture.frames};
0250   const auto& surface = fixture.element.surface();
0251   const Vector3 position = fixture.intersection(fixture.frames);
0252   for (AlignmentMask mask :
0253        {AlignmentMask::All, AlignmentMask::None,
0254         AlignmentMask::Center1 | AlignmentMask::Rotation0}) {
0255     AlignmentToBoundMatrix expected = surface.alignmentToBoundDerivative(
0256         gctx, position, fixture.direction, fixture.pathDerivative);
0257     ActsAlignment::detail::resetAlignmentDerivative(expected, mask);
0258     const AlignmentToBoundMatrix actual =
0259         ActsAlignment::detail::alignmentToBoundDerivative(
0260             gctx, surface, position, fixture.direction, fixture.pathDerivative,
0261             mask);
0262     BOOST_CHECK_EQUAL((actual - expected).squaredNorm(), 0.);
0263   }
0264 }
0265 
0266 BOOST_AUTO_TEST_CASE(StructureWithoutFrame) {
0267   const auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0268   const AlignableStructure structure(GeometryIdentifier().withVolume(1));
0269   BOOST_CHECK(!structure.hasTransform());
0270   BOOST_CHECK_THROW(structure.localToGlobalTransform(gctx), std::logic_error);
0271   BOOST_CHECK_THROW(AlignableStructure(GeometryIdentifier().withVolume(1),
0272                                        AlignableStructure::TransformProvider{}),
0273                     std::invalid_argument);
0274 }
0275 
0276 BOOST_AUTO_TEST_CASE(FixedStructureFrame) {
0277   const auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0278   const Transform3 expected = Translation3(100_mm, -30_mm, 200_mm) *
0279                               AngleAxis3(0.7_rad, Vector3::UnitY());
0280   Transform3 supplied = expected;
0281   auto parent = std::make_shared<AlignableStructure>(
0282       GeometryIdentifier().withVolume(1), supplied);
0283   supplied.setIdentity();
0284   BOOST_CHECK(parent->hasTransform());
0285   CHECK_CLOSE_ABS(parent->localToGlobalTransform(gctx).matrix(),
0286                   expected.matrix(), 1e-14);
0287 
0288   // An explicitly global child frame is not composed with its parent's frame.
0289   const Transform3 childFrame = Translation3(110_mm, -20_mm, 220_mm) *
0290                                 AngleAxis3(-0.3_rad, Vector3::UnitX());
0291   auto child = std::make_shared<AlignableStructure>(
0292       GeometryIdentifier().withVolume(2), childFrame);
0293   parent->addChild(child);
0294   auto element = makeDummyElement(Transform3::Identity());
0295   child->addSurface(element->surface().getSharedPtr());
0296   const AlignmentHierarchy hierarchy({parent});
0297   BOOST_REQUIRE(hierarchy.validate().ok());
0298   BOOST_REQUIRE_EQUAL(hierarchy.structureFor(element->surface()), child.get());
0299   CHECK_CLOSE_ABS(child->localToGlobalTransform(gctx).matrix(),
0300                   childFrame.matrix(), 1e-14);
0301   // Declaring or querying a frame does not move the nominal member surface.
0302   CHECK_CLOSE_ABS(element->surface().localToGlobalTransform(gctx).matrix(),
0303                   Transform3::Identity().matrix(), 1e-14);
0304 }
0305 
0306 BOOST_AUTO_TEST_CASE(ContextualStructureFrame) {
0307   struct FrameContext {
0308     Transform3 transform;
0309   };
0310   const AlignableStructure structure(
0311       GeometryIdentifier().withVolume(1), [](const GeometryContext& gctx) {
0312         return gctx.get<FrameContext>().transform;
0313       });
0314   const Transform3 first =
0315       Translation3(12_mm, -5_mm, 70_mm) * AngleAxis3(0.4_rad, Vector3::UnitZ());
0316   const Transform3 second = Translation3(-18_mm, 9_mm, 80_mm) *
0317                             AngleAxis3(-0.2_rad, Vector3::UnitY());
0318   const GeometryContext firstContext{FrameContext{first}};
0319   const GeometryContext secondContext{FrameContext{second}};
0320   BOOST_CHECK(structure.hasTransform());
0321   auto returned = structure.localToGlobalTransform(firstContext);
0322   CHECK_CLOSE_ABS(returned.matrix(), first.matrix(), 1e-14);
0323   returned.setIdentity();
0324   CHECK_CLOSE_ABS(structure.localToGlobalTransform(secondContext).matrix(),
0325                   second.matrix(), 1e-14);
0326   // A query in another context, or mutation of a returned value, must not
0327   // replace or modify the frame seen by the first context.
0328   CHECK_CLOSE_ABS(structure.localToGlobalTransform(firstContext).matrix(),
0329                   first.matrix(), 1e-14);
0330 }
0331 
0332 BOOST_AUTO_TEST_CASE(StructureFrameReplacement) {
0333   struct FrameContext {
0334     Transform3 transform;
0335   };
0336   const Transform3 first =
0337       Translation3(12_mm, -5_mm, 70_mm) * AngleAxis3(0.4_rad, Vector3::UnitZ());
0338   const Transform3 second = Translation3(-18_mm, 9_mm, 80_mm) *
0339                             AngleAxis3(-0.2_rad, Vector3::UnitY());
0340   const GeometryContext firstContext{FrameContext{first}};
0341   const GeometryContext secondContext{FrameContext{second}};
0342   auto structure =
0343       std::make_shared<AlignableStructure>(GeometryIdentifier().withVolume(1));
0344   auto element = makeDummyElement(Transform3::Identity());
0345   structure->addSurface(element->surface().getSharedPtr());
0346   const AlignmentHierarchy hierarchy({structure});
0347   BOOST_REQUIRE(hierarchy.validate().ok());
0348 
0349   BOOST_CHECK_THROW(
0350       structure->setTransform(AlignableStructure::TransformProvider{}),
0351       std::invalid_argument);
0352   BOOST_CHECK(!structure->hasTransform());
0353 
0354   Transform3 supplied = first;
0355   structure->setTransform(supplied);
0356   supplied.setIdentity();
0357   BOOST_REQUIRE(structure->hasTransform());
0358   CHECK_CLOSE_ABS(structure->localToGlobalTransform(firstContext).matrix(),
0359                   first.matrix(), 1e-14);
0360 
0361   const auto provider = [](const GeometryContext& gctx) {
0362     return gctx.get<FrameContext>().transform;
0363   };
0364   structure->setTransform(provider);
0365   CHECK_CLOSE_ABS(structure->localToGlobalTransform(firstContext).matrix(),
0366                   first.matrix(), 1e-14);
0367   CHECK_CLOSE_ABS(structure->localToGlobalTransform(secondContext).matrix(),
0368                   second.matrix(), 1e-14);
0369   BOOST_CHECK_THROW(
0370       structure->setTransform(AlignableStructure::TransformProvider{}),
0371       std::invalid_argument);
0372   CHECK_CLOSE_ABS(structure->localToGlobalTransform(secondContext).matrix(),
0373                   second.matrix(), 1e-14);
0374 
0375   structure->setTransform(first);
0376   CHECK_CLOSE_ABS(structure->localToGlobalTransform(secondContext).matrix(),
0377                   first.matrix(), 1e-14);
0378   BOOST_CHECK_THROW(
0379       structure->setTransform(AlignableStructure::TransformProvider{}),
0380       std::invalid_argument);
0381   CHECK_CLOSE_ABS(structure->localToGlobalTransform(secondContext).matrix(),
0382                   first.matrix(), 1e-14);
0383 
0384   // Frames can be attached after building the hierarchy without changing its
0385   // membership lookup or moving a member surface.
0386   BOOST_CHECK_EQUAL(hierarchy.structureFor(element->surface()),
0387                     structure.get());
0388   CHECK_CLOSE_ABS(
0389       element->surface().localToGlobalTransform(firstContext).matrix(),
0390       Transform3::Identity().matrix(), 1e-14);
0391 
0392   AlignableStructure lateContextual(GeometryIdentifier().withVolume(2));
0393   lateContextual.setTransform(provider);
0394   BOOST_CHECK(lateContextual.hasTransform());
0395   CHECK_CLOSE_ABS(lateContextual.localToGlobalTransform(secondContext).matrix(),
0396                   second.matrix(), 1e-14);
0397 }
0398 
0399 BOOST_AUTO_TEST_CASE(AlignmentRejectsOverlappingStructures) {
0400   auto element = makeDummyElement(Acts::Transform3::Identity());
0401   auto struct1 = std::make_shared<ActsAlignment::AlignableStructure>(
0402       Acts::GeometryIdentifier().withVolume(1));
0403   auto struct2 = std::make_shared<ActsAlignment::AlignableStructure>(
0404       Acts::GeometryIdentifier().withVolume(2));
0405   struct1->addSurface(element->surface().getSharedPtr());
0406   struct2->addSurface(element->surface().getSharedPtr());
0407 
0408   // Suppress the expected error log for this negative test only. This works
0409   // with both disabled and enabled log-failure thresholds, without changing
0410   // the global threshold used by other tests.
0411   ActsAlignment::Alignment alignEngine(DummyFitter{},
0412                                        Acts::getDummyLogger().clone());
0413 
0414   // Validation must reject the hierarchy even with no solver iterations.
0415   // Empty trajectories are never passed to the numerical solve.
0416   ActsAlignment::AlignmentOptions<DummyFitOptions> options(
0417       DummyFitOptions{}, {}, {element.get()}, 0.5, {5, 0.01}, 0, {},
0418       {struct1, struct2});
0419   const std::vector<std::vector<Acts::SourceLink>> trajs;
0420   const std::vector<Acts::BoundTrackParameters> params;
0421 
0422   const auto result = alignEngine.align(trajs, params, options);
0423   BOOST_REQUIRE(!result.ok());
0424   BOOST_CHECK_EQUAL(result.error(),
0425                     ActsAlignment::AlignmentError::HierarchyValidationFailure);
0426 }
0427 
0428 BOOST_AUTO_TEST_CASE(AlignmentHierarchyHelper) {
0429   // Direct test of AlignmentHierarchy without standing up Alignment<Fitter>.
0430   auto el1 = makeDummyElement(Acts::Translation3(0, 0, 10_mm) *
0431                               Acts::Transform3::Identity());
0432   auto el2 = makeDummyElement(Acts::Translation3(0, 0, 20_mm) *
0433                               Acts::Transform3::Identity());
0434   auto el3 = makeDummyElement(Acts::Translation3(0, 0, 30_mm) *
0435                               Acts::Transform3::Identity());
0436 
0437   auto structA = std::make_shared<ActsAlignment::AlignableStructure>(
0438       Acts::GeometryIdentifier().withVolume(10));
0439   auto structB = std::make_shared<ActsAlignment::AlignableStructure>(
0440       Acts::GeometryIdentifier().withVolume(20));
0441 
0442   // structA owns el1 + el2; el3 floats as a standalone module.
0443   structA->addSurface(el1->surface().getSharedPtr());
0444   structA->addSurface(el2->surface().getSharedPtr());
0445 
0446   // --- Clean hierarchy ---
0447   {
0448     ActsAlignment::AlignmentHierarchy hierarchy({structA, structB});
0449     BOOST_CHECK(hierarchy.validate().ok());
0450     BOOST_CHECK_EQUAL(hierarchy.structureFor(*el1), structA.get());
0451     BOOST_CHECK_EQUAL(hierarchy.structureFor(*el2), structA.get());
0452     // el3 is a standalone floating module
0453     BOOST_CHECK_EQUAL(hierarchy.structureFor(*el3), nullptr);
0454   }
0455 
0456   // --- Surface-level lookup ---
0457   {
0458     ActsAlignment::AlignmentHierarchy hierarchy({structA, structB});
0459     BOOST_CHECK_EQUAL(hierarchy.structureFor(el1->surface()), structA.get());
0460     BOOST_CHECK_EQUAL(hierarchy.structureFor(el2->surface()), structA.get());
0461     BOOST_CHECK_EQUAL(hierarchy.structureFor(el3->surface()), nullptr);
0462   }
0463 
0464   // --- Overlap detected ---
0465   structB->addSurface(el1->surface().getSharedPtr());
0466   {
0467     ActsAlignment::AlignmentHierarchy hierarchy({structA, structB});
0468     const auto result = hierarchy.validate();
0469     BOOST_CHECK(!result.ok());
0470     BOOST_REQUIRE_EQUAL(result.overlapping.size(), 1u);
0471     BOOST_CHECK_EQUAL(result.overlapping.front(), &el1->surface());
0472   }
0473 
0474   // --- Empty hierarchy ---
0475   {
0476     ActsAlignment::AlignmentHierarchy hierarchy({});
0477     BOOST_CHECK(hierarchy.validate().ok());
0478     BOOST_CHECK_EQUAL(hierarchy.structureFor(*el1), nullptr);
0479   }
0480 }
0481 
0482 BOOST_AUTO_TEST_CASE(NestedHierarchyRegistration) {
0483   auto el1 = makeDummyElement(Acts::Translation3(0, 0, 10_mm) *
0484                               Acts::Transform3::Identity());
0485   auto el2 = makeDummyElement(Acts::Translation3(0, 0, 20_mm) *
0486                               Acts::Transform3::Identity());
0487   auto el3 = makeDummyElement(Acts::Translation3(0, 0, 30_mm) *
0488                               Acts::Transform3::Identity());
0489   auto parent = std::make_shared<ActsAlignment::AlignableStructure>(
0490       Acts::GeometryIdentifier().withVolume(1));
0491   auto child = std::make_shared<ActsAlignment::AlignableStructure>(
0492       Acts::GeometryIdentifier().withVolume(2));
0493   auto grandchild = std::make_shared<ActsAlignment::AlignableStructure>(
0494       Acts::GeometryIdentifier().withVolume(3));
0495   parent->addSurface(el1->surface().getSharedPtr());
0496   child->addSurface(el2->surface().getSharedPtr());
0497   grandchild->addSurface(el3->surface().getSharedPtr());
0498   parent->addChild(child);
0499   child->addChild(grandchild);
0500 
0501   // Register only the root. Descendants must be visited recursively, and
0502   // each surface must retain its immediate owner instead of the root.
0503   {
0504     ActsAlignment::AlignmentHierarchy hierarchy({parent});
0505     BOOST_CHECK(hierarchy.validate().ok());
0506     BOOST_CHECK_EQUAL(hierarchy.structureFor(el1->surface()), parent.get());
0507     BOOST_CHECK_EQUAL(hierarchy.structureFor(el2->surface()), child.get());
0508     BOOST_CHECK_EQUAL(hierarchy.structureFor(el3->surface()), grandchild.get());
0509   }
0510 
0511   // Duplicate ownership must also be detected across hierarchy levels.
0512   grandchild->addSurface(el1->surface().getSharedPtr());
0513   {
0514     ActsAlignment::AlignmentHierarchy hierarchy({parent});
0515     const auto result = hierarchy.validate();
0516     BOOST_CHECK(!result.ok());
0517     BOOST_REQUIRE_EQUAL(result.overlapping.size(), 1u);
0518     BOOST_CHECK_EQUAL(result.overlapping.front(), &el1->surface());
0519   }
0520 }
0521 
0522 // TODO: re-enable once detectMaskConflicts() is implemented. The function is
0523 // currently stubbed to return empty (see AlignmentHierarchy.hpp) because a
0524 // correct conflict check requires the chain-rule Jacobian to compare DoFs
0525 // across coordinate frames.
0526 BOOST_AUTO_TEST_CASE(MaskConflictDetection, *boost::unit_test::disabled()) {
0527   using ActsAlignment::AlignmentMask;
0528 
0529   auto el1 = makeDummyElement(Acts::Translation3(0, 0, 10_mm) *
0530                               Acts::Transform3::Identity());
0531   auto el2 = makeDummyElement(Acts::Translation3(0, 0, 20_mm) *
0532                               Acts::Transform3::Identity());
0533   auto el3 = makeDummyElement(Acts::Translation3(0, 0, 30_mm) *
0534                               Acts::Transform3::Identity());
0535 
0536   auto parent = std::make_shared<ActsAlignment::AlignableStructure>(
0537       Acts::GeometryIdentifier().withVolume(1));
0538   parent->addSurface(el1->surface().getSharedPtr());
0539   parent->addSurface(el2->surface().getSharedPtr());
0540 
0541   std::vector<Acts::SurfacePlacementBase*> floatingModules = {
0542       el1.get(), el2.get(), el3.get()};
0543 
0544   ActsAlignment::AlignmentHierarchy hierarchy({parent});
0545 
0546   // Parent floats Center0. Module iteration mask = All → conflict on el1, el2.
0547   parent->alignmentMask() = AlignmentMask::Center0;
0548   {
0549     const auto conflicts =
0550         hierarchy.detectMaskConflicts(AlignmentMask::All, floatingModules);
0551     BOOST_CHECK_EQUAL(conflicts.size(), 2u);
0552     for (const auto& c : conflicts) {
0553       BOOST_CHECK_EQUAL(c.structure, parent.get());
0554       BOOST_CHECK(c.conflictingBits == AlignmentMask::Center0);
0555     }
0556   }
0557 
0558   // Disjoint masks: parent = Center0, module mask = Rotation0 → no conflict.
0559   {
0560     const auto conflicts = hierarchy.detectMaskConflicts(
0561         AlignmentMask::Rotation0, floatingModules);
0562     BOOST_CHECK(conflicts.empty());
0563   }
0564 
0565   // Parent with None mask → no conflict regardless of module mask.
0566   parent->alignmentMask() = AlignmentMask::None;
0567   {
0568     const auto conflicts =
0569         hierarchy.detectMaskConflicts(AlignmentMask::All, floatingModules);
0570     BOOST_CHECK(conflicts.empty());
0571   }
0572 
0573   // Standalone module (el3) is never in a structure → never a conflict.
0574   parent->alignmentMask() = AlignmentMask::All;
0575   {
0576     const auto conflicts =
0577         hierarchy.detectMaskConflicts(AlignmentMask::All, {el3.get()});
0578     BOOST_CHECK(conflicts.empty());
0579   }
0580 }