Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:04:04

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2020-2024 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/Detector/Detector.hpp"
0014 #include "Acts/EventData/MultiTrajectory.hpp"
0015 #include "Acts/EventData/SourceLink.hpp"
0016 #include "Acts/Geometry/GeometryContext.hpp"
0017 #include "Acts/Geometry/GeometryIdentifier.hpp"
0018 #include "Acts/Geometry/TrackingGeometry.hpp"
0019 #include "Acts/Utilities/CalibrationContext.hpp"
0020 
0021 #include <algorithm>
0022 #include <array>
0023 #include <cassert>
0024 #include <cstddef>
0025 #include <iosfwd>
0026 #include <stdexcept>
0027 
0028 namespace Acts::detail::Test {
0029 
0030 struct TestSourceLinkSurfaceAccessor;
0031 
0032 /// A minimal source link implementation for testing.
0033 ///
0034 /// Instead of storing a reference to a measurement or raw data, the measurement
0035 /// data is stored inline directly in the source link. Only 1d or 2d
0036 /// measurements are supported to limit the overhead. Additionally, a source
0037 /// identifier is stored that can be used to store additional information. How
0038 /// this is interpreted depends on the specific tests.
0039 struct TestSourceLink final {
0040   using SurfaceAccessor = TestSourceLinkSurfaceAccessor;
0041 
0042   GeometryIdentifier m_geometryId{};
0043   std::size_t sourceId = 0u;
0044   // use eBoundSize to indicate unused indices
0045   std::array<BoundIndices, 2> indices = {eBoundSize, eBoundSize};
0046   Acts::ActsVector<2> parameters;
0047   Acts::ActsSquareMatrix<2> covariance;
0048 
0049   /// Construct a source link for a 1d measurement.
0050   TestSourceLink(BoundIndices idx, ActsScalar val, ActsScalar var,
0051                  GeometryIdentifier gid = GeometryIdentifier(),
0052                  std::size_t sid = 0u)
0053       : m_geometryId(gid),
0054         sourceId(sid),
0055         indices{idx, eBoundSize},
0056         parameters(val, 0),
0057         covariance(Acts::ActsVector<2>(var, 0).asDiagonal()) {}
0058   /// Construct a source link for a 2d measurement.
0059   TestSourceLink(BoundIndices idx0, BoundIndices idx1,
0060                  const Acts::ActsVector<2>& params,
0061                  const Acts::ActsSquareMatrix<2>& cov,
0062                  GeometryIdentifier gid = GeometryIdentifier(),
0063                  std::size_t sid = 0u)
0064       : m_geometryId(gid),
0065         sourceId(sid),
0066         indices{idx0, idx1},
0067         parameters(params),
0068         covariance(cov) {}
0069   /// Default-construct an invalid source link to satisfy SourceLinkConcept.
0070   TestSourceLink() = default;
0071   TestSourceLink(const TestSourceLink&) = default;
0072   TestSourceLink(TestSourceLink&&) = default;
0073   TestSourceLink& operator=(const TestSourceLink&) = default;
0074   TestSourceLink& operator=(TestSourceLink&&) = default;
0075   bool operator==(const TestSourceLink& rhs) const {
0076     return (m_geometryId == rhs.m_geometryId) && (sourceId == rhs.sourceId) &&
0077            (indices == rhs.indices) && (parameters == rhs.parameters) &&
0078            (covariance == rhs.covariance);
0079   }
0080   bool operator!=(const TestSourceLink& rhs) const { return !(*this == rhs); }
0081   std::ostream& print(std::ostream& os) const {
0082     os << "TestsSourceLink(geometryId=" << m_geometryId
0083        << ",sourceId=" << sourceId;
0084     if (indices[0] != eBoundSize) {
0085       os << ",index0=" << indices[0];
0086     }
0087     if (indices[1] != eBoundSize) {
0088       os << ",index1=" << indices[1];
0089     }
0090     os << ")";
0091     return os;
0092   }
0093   constexpr std::size_t index() const { return sourceId; }
0094 };
0095 
0096 struct TestSourceLinkSurfaceAccessor {
0097   const TrackingGeometry& geometry;
0098 
0099   const Acts::Surface* operator()(const Acts::SourceLink& sourceLink) const {
0100     const auto& testSourceLink = sourceLink.get<TestSourceLink>();
0101     return geometry.findSurface(testSourceLink.m_geometryId);
0102   }
0103 };
0104 
0105 namespace Experimental {
0106 
0107 struct TestSourceLinkSurfaceAccessor {
0108   const Acts::Experimental::Detector& geometry;
0109 
0110   const Acts::Surface* operator()(const Acts::SourceLink& sourceLink) const {
0111     const auto& testSourceLink = sourceLink.get<TestSourceLink>();
0112     return geometry.findSurface(testSourceLink.m_geometryId);
0113   }
0114 };
0115 
0116 }  // namespace Experimental
0117 
0118 inline std::ostream& operator<<(std::ostream& os,
0119                                 const TestSourceLink& sourceLink) {
0120   return sourceLink.print(os);
0121 }
0122 
0123 /// Extract the measurement from a TestSourceLink.
0124 ///
0125 /// @param gctx Unused
0126 /// @param trackState TrackState to calibrated
0127 template <typename trajectory_t>
0128 void testSourceLinkCalibratorReturn(
0129     const GeometryContext& /*gctx*/, const CalibrationContext& /*cctx*/,
0130     const SourceLink& sourceLink,
0131     typename trajectory_t::TrackStateProxy trackState) {
0132   TestSourceLink sl = sourceLink.template get<TestSourceLink>();
0133 
0134   trackState.setUncalibratedSourceLink(SourceLink{sourceLink});
0135 
0136   if ((sl.indices[0] != Acts::eBoundSize) &&
0137       (sl.indices[1] != Acts::eBoundSize)) {
0138     trackState.allocateCalibrated(2);
0139     trackState.template calibrated<2>() = sl.parameters;
0140     trackState.template calibratedCovariance<2>() = sl.covariance;
0141     trackState.template setSubspaceIndices(
0142         std::array{sl.indices[0], sl.indices[1]});
0143   } else if (sl.indices[0] != Acts::eBoundSize) {
0144     trackState.allocateCalibrated(1);
0145     trackState.template calibrated<1>() = sl.parameters.head<1>();
0146     trackState.template calibratedCovariance<1>() =
0147         sl.covariance.topLeftCorner<1, 1>();
0148     trackState.template setSubspaceIndices(std::array{sl.indices[0]});
0149   } else {
0150     throw std::runtime_error(
0151         "Tried to extract measurement from invalid TestSourceLink");
0152   }
0153 }
0154 
0155 /// Extract the measurement from a TestSourceLink.
0156 ///
0157 /// @param gctx Unused
0158 /// @param trackState TrackState to calibrated
0159 template <typename trajectory_t>
0160 void testSourceLinkCalibrator(
0161     const GeometryContext& gctx, const CalibrationContext& cctx,
0162     const SourceLink& sourceLink,
0163     typename trajectory_t::TrackStateProxy trackState) {
0164   testSourceLinkCalibratorReturn<trajectory_t>(gctx, cctx, sourceLink,
0165                                                trackState);
0166 }
0167 
0168 }  // namespace Acts::detail::Test