File indexing completed on 2026-05-24 07:35:22
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Definitions/TrackParametrization.hpp"
0012 #include "Acts/EventData/TransformationHelpers.hpp"
0013 #include "Acts/Surfaces/Surface.hpp"
0014 #include "Acts/Surfaces/SurfacePlacementBase.hpp"
0015 #include "Acts/Utilities/Result.hpp"
0016 #include "ActsFatras/Digitization/DigitizationError.hpp"
0017 #include "ActsFatras/EventData/Hit.hpp"
0018
0019 #include <array>
0020 #include <functional>
0021 #include <utility>
0022
0023 namespace ActsFatras {
0024
0025
0026
0027
0028
0029
0030
0031 template <typename generator_t>
0032 using SingleParameterSmearFunction =
0033 std::function<Acts::Result<std::pair<double, double>>(double,
0034 generator_t&)>;
0035
0036
0037
0038
0039
0040
0041
0042
0043 template <typename generator_t, std::size_t kSize>
0044 struct BoundParametersSmearer {
0045
0046 using ParametersVector = Acts::Vector<kSize>;
0047
0048 using CovarianceMatrix = Acts::SquareMatrix<kSize>;
0049
0050 using Result = Acts::Result<std::pair<ParametersVector, CovarianceMatrix>>;
0051
0052
0053 std::array<Acts::BoundIndices, kSize> indices{};
0054
0055 std::array<SingleParameterSmearFunction<generator_t>, kSize> smearFunctions{};
0056
0057 std::array<bool, kSize> forcePositive = {};
0058
0059 std::size_t maxRetries = 0;
0060
0061
0062
0063 static constexpr std::size_t size() { return kSize; }
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 Result operator()(generator_t& rng, const Hit& hit,
0074 const Acts::Surface& surface,
0075 const Acts::GeometryContext& geoCtx) const {
0076
0077
0078
0079 const auto tolerance = surface.isSensitive() ? surface.thickness()
0080 : Acts::s_onSurfaceTolerance;
0081
0082
0083
0084 Acts::Result<Acts::BoundVector> boundParamsRes =
0085 Acts::transformFreeToBoundParameters(hit.position(), hit.time(),
0086 hit.direction(), 0, surface,
0087 geoCtx, tolerance);
0088
0089 if (!boundParamsRes.ok()) {
0090 return boundParamsRes.error();
0091 }
0092
0093 const auto& boundParams = *boundParamsRes;
0094 Acts::BoundVector smearedBoundParams = boundParams;
0095
0096 for (std::size_t k = 0; k < maxRetries + 1; ++k) {
0097 ParametersVector par = ParametersVector::Zero();
0098 CovarianceMatrix cov = CovarianceMatrix::Zero();
0099 for (std::size_t i = 0; i < kSize; ++i) {
0100 auto res = smearFunctions[i](boundParams[indices[i]], rng);
0101 if (!res.ok()) {
0102 return Result::failure(res.error());
0103 }
0104 auto [value, stddev] = res.value();
0105 par[i] = value;
0106 if (forcePositive[i]) {
0107 par[i] = std::abs(value);
0108 }
0109 smearedBoundParams[indices[i]] = par[i];
0110 cov(i, i) = stddev * stddev;
0111 }
0112
0113 if (!surface.insideBounds(smearedBoundParams.head<2>())) {
0114 continue;
0115 }
0116
0117 return Result::success(std::make_pair(par, cov));
0118 }
0119
0120 return Result::failure(DigitizationError::MaximumRetriesExceeded);
0121 }
0122 };
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 template <typename generator_t, std::size_t kSize>
0135 struct FreeParametersSmearer {
0136
0137 using ParametersVector = Acts::Vector<kSize>;
0138
0139 using CovarianceMatrix = Acts::SquareMatrix<kSize>;
0140
0141 using Result = Acts::Result<std::pair<ParametersVector, CovarianceMatrix>>;
0142
0143
0144 std::array<Acts::FreeIndices, kSize> indices{};
0145
0146 std::array<SingleParameterSmearFunction<generator_t>, kSize> smearFunctions;
0147
0148
0149
0150 static constexpr std::size_t size() { return kSize; }
0151
0152
0153
0154
0155
0156
0157
0158
0159 Result operator()(generator_t& rng, const Hit& hit) const {
0160
0161
0162 Acts::FreeVector freeParams;
0163 freeParams.segment<3>(Acts::eFreePos0) = hit.position();
0164 freeParams[Acts::eFreeTime] = hit.time();
0165 freeParams.segment<3>(Acts::eFreeDir0) = hit.direction();
0166 freeParams[Acts::eFreeQOverP] = 0;
0167
0168 ParametersVector par = ParametersVector::Zero();
0169 CovarianceMatrix cov = CovarianceMatrix::Zero();
0170 for (std::size_t i = 0; i < kSize; ++i) {
0171 auto res = smearFunctions[i](freeParams[indices[i]], rng);
0172 if (!res.ok()) {
0173 return Result::failure(res.error());
0174 }
0175 auto [value, stddev] = res.value();
0176 par[i] = value;
0177 cov(i, i) = stddev * stddev;
0178 }
0179
0180 return Result::success(std::make_pair(par, cov));
0181 }
0182 };
0183
0184 }