Skip to content

Commit

Permalink
feat: Variable size measurement for Examples (acts-project#3107)
Browse files Browse the repository at this point in the history
This is an attempt to implement a variable size measurement as an
alternative to the fixed size measurement. Currently our Examples
framework is using a variant over fixed size measurements to cope with
measurements of different dimensions. This obfuscates the code a bit
since accessing the actual measurement requires a visitor pattern to
acquire the dimensional typed measurement.

Here I propose to switch this out for a variable size measurement which
allocates the same amount of memory but stores the dimension as a direct
member instead of pushing this detail to `std::variant`.

blocked by
- acts-project#3192
- acts-project#3331
  • Loading branch information
andiwand authored Aug 8, 2024
1 parent 223cba2 commit 3a8a0e3
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 668 deletions.
16 changes: 16 additions & 0 deletions Core/include/Acts/EventData/MultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ class MultiTrajectory {
return self().template calibratedCovariance_impl<measdim>(istate);
}

/// Retrieve a calibrated measurement covariance proxy instance for a
/// measurement at a given index
/// @param istate The track state
/// @return Mutable proxy
typename TrackStateProxy::EffectiveCalibrated effectiveCalibrated(
IndexType istate) requires(!ReadOnly) {
// This abuses an incorrectly sized vector / matrix to access the
Expand All @@ -508,6 +512,10 @@ class MultiTrajectory {
calibrated<eBoundSize>(istate).data(), calibratedSize(istate)};
}

/// Retrieve a calibrated measurement covariance proxy instance for a
/// measurement at a given index
/// @param istate The track state
/// @return Const proxy
typename ConstTrackStateProxy::EffectiveCalibrated effectiveCalibrated(
IndexType istate) const {
// This abuses an incorrectly sized vector / matrix to access the
Expand All @@ -517,6 +525,10 @@ class MultiTrajectory {
calibrated<eBoundSize>(istate).data(), calibratedSize(istate)};
}

/// Retrieve a calibrated measurement covariance proxy instance for a
/// measurement at a given index
/// @param istate The track state
/// @return Mutable proxy
typename TrackStateProxy::EffectiveCalibratedCovariance
effectiveCalibratedCovariance(IndexType istate) requires(!ReadOnly) {
// This abuses an incorrectly sized vector / matrix to access the
Expand All @@ -527,6 +539,10 @@ class MultiTrajectory {
calibratedSize(istate)};
}

/// Retrieve a calibrated measurement covariance proxy instance for a
/// measurement at a given index
/// @param istate The track state
/// @return Const proxy
typename ConstTrackStateProxy::EffectiveCalibratedCovariance
effectiveCalibratedCovariance(IndexType istate) const {
// This abuses an incorrectly sized vector / matrix to access the
Expand Down
52 changes: 18 additions & 34 deletions Core/include/Acts/Utilities/detail/Subspace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ class FixedSizeSubspace {
/// @return Axis index in the full space
constexpr std::size_t operator[](std::size_t i) const { return m_axes[i]; }

std::size_t indexOf(std::size_t axis) const {
for (std::size_t i = 0; i < kSize; ++i) {
if (m_axes[i] == axis) {
return i;
}
}
return kSize;
}

/// Axis indices that comprise the subspace.
///
/// The specific container and index type should be considered an
Expand Down Expand Up @@ -266,6 +275,15 @@ class VariableSizeSubspace {
return m_axes[i];
}

std::size_t indexOf(std::size_t axis) const {
for (std::size_t i = 0; i < m_size; ++i) {
if (m_axes[i] == axis) {
return i;
}
}
return m_size;
}

/// Check if the given axis index in the full space is part of the subspace.
constexpr bool contains(std::size_t index) const {
bool isContained = false;
Expand Down Expand Up @@ -302,40 +320,6 @@ class VariableSizeSubspace {
}
return expn;
}

std::uint64_t projectorBits() const {
std::uint64_t result = 0;

for (std::size_t i = 0; i < m_size; ++i) {
for (std::size_t j = 0; j < kFullSize; ++j) {
// the bit order is defined in `Acts/Utilities/AlgebraHelpers.hpp`
// in `matrixToBitset`
std::size_t index = m_size * kFullSize - 1 - (i + j * m_size);
if (m_axes[i] == j) {
result |= (1ull << index);
}
}
}

return result;
}

std::uint64_t fullProjectorBits() const {
std::uint64_t result = 0;

for (std::size_t i = 0; i < kFullSize; ++i) {
for (std::size_t j = 0; j < kFullSize; ++j) {
// the bit order is defined in `Acts/Utilities/AlgebraHelpers.hpp`
// in `matrixToBitset`
std::size_t index = kFullSize * kFullSize - 1 - (i + j * kFullSize);
if (i < m_size && m_axes[i] == j) {
result |= (1ull << index);
}
}
}

return result;
}
};

/// @}
Expand Down
12 changes: 4 additions & 8 deletions Examples/Algorithms/Digitization/src/MeasurementCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ ActsExamples::Measurement ActsExamples::createMeasurement(
switch (dParams.indices.size()) {
case 1u: {
auto [indices, par, cov] = measurementConstituents<1>(dParams);
return FixedSizeMeasurement<Acts::BoundIndices, 1>(std::move(sl), indices,
par, cov);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
}
case 2u: {
auto [indices, par, cov] = measurementConstituents<2>(dParams);
return FixedSizeMeasurement<Acts::BoundIndices, 2>(std::move(sl), indices,
par, cov);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
};
case 3u: {
auto [indices, par, cov] = measurementConstituents<3>(dParams);
return FixedSizeMeasurement<Acts::BoundIndices, 3>(std::move(sl), indices,
par, cov);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
};
case 4u: {
auto [indices, par, cov] = measurementConstituents<4>(dParams);
return FixedSizeMeasurement<Acts::BoundIndices, 4>(std::move(sl), indices,
par, cov);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
};
default:
std::string errorMsg = "Invalid/mismatching measurement dimension: " +
Expand Down
30 changes: 14 additions & 16 deletions Examples/Algorithms/TrackFinding/src/HoughTransformSeeder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,20 +525,18 @@ void ActsExamples::HoughTransformSeeder::addMeasurements(
// are transformed to the bound space where we do know their location.
// if the local parameters are not measured, this results in a
// zero location, which is a reasonable default fall-back.
auto [localPos, localCov] = std::visit(
[](const auto& meas) {
auto expander = meas.expander();
Acts::BoundVector par = expander * meas.parameters();
Acts::BoundSquareMatrix cov =
expander * meas.covariance() * expander.transpose();
// extract local position
Acts::Vector2 lpar(par[Acts::eBoundLoc0], par[Acts::eBoundLoc1]);
// extract local position covariance.
Acts::SquareMatrix2 lcov =
cov.block<2, 2>(Acts::eBoundLoc0, Acts::eBoundLoc0);
return std::make_pair(lpar, lcov);
},
measurements[sourceLink.index()]);
const auto& measurement = measurements[sourceLink.index()];

assert(measurement.contains(Acts::eBoundLoc0) &&
"Measurement does not contain the required bound loc0");
assert(measurement.contains(Acts::eBoundLoc1) &&
"Measurement does not contain the required bound loc1");

auto boundLoc0 = measurement.subspace().indexOf(Acts::eBoundLoc0);
auto boundLoc1 = measurement.subspace().indexOf(Acts::eBoundLoc1);

Acts::Vector2 localPos{measurement.effectiveParameters()[boundLoc0],
measurement.effectiveParameters()[boundLoc1]};

// transform local position to global coordinates
Acts::Vector3 globalFakeMom(1, 1, 1);
Expand All @@ -551,10 +549,10 @@ void ActsExamples::HoughTransformSeeder::addMeasurements(
if (hitlayer.ok()) {
std::vector<Index> index;
index.push_back(sourceLink.index());
auto meas = std::shared_ptr<HoughMeasurementStruct>(
auto houghMeas = std::shared_ptr<HoughMeasurementStruct>(
new HoughMeasurementStruct(hitlayer.value(), phi, r, z, index,
HoughHitType::MEASUREMENT));
houghMeasurementStructs.push_back(meas);
houghMeasurementStructs.push_back(houghMeas);
}
}
}
Expand Down
10 changes: 1 addition & 9 deletions Examples/Algorithms/TrackFinding/src/SpacePointMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,7 @@ ActsExamples::ProcessCode ActsExamples::SpacePointMaker::execute(
const auto islink = slink.get<IndexSourceLink>();
const auto& meas = measurements[islink.index()];

return std::visit(
[](const auto& measurement) {
auto expander = measurement.expander();
Acts::BoundVector par = expander * measurement.parameters();
Acts::BoundSquareMatrix cov =
expander * measurement.covariance() * expander.transpose();
return std::make_pair(par, cov);
},
meas);
return std::make_pair(meas.fullParameters(), meas.fullCovariance());
};

SimSpacePointContainer spacePoints;
Expand Down
11 changes: 3 additions & 8 deletions Examples/Algorithms/Utilities/src/PrototracksToTracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ ProcessCode PrototracksToTracks::execute(const AlgorithmContext& ctx) const {
TrackContainer tracks(trackContainer, mtj);

boost::container::flat_map<Index, Acts::SourceLink> slMap;
for (const auto& varm : m_inputMeasurements(ctx)) {
std::visit(
[&](const auto& m) {
const auto idx =
m.sourceLink().template get<IndexSourceLink>().index();
slMap.insert(std::pair<Index, Acts::SourceLink>{idx, m.sourceLink()});
},
varm);
for (const auto& m : m_inputMeasurements(ctx)) {
const auto idx = m.sourceLink().template get<IndexSourceLink>().index();
slMap.insert(std::pair<Index, Acts::SourceLink>{idx, m.sourceLink()});
}

const auto& prototracks = m_inputProtoTracks(ctx);
Expand Down
Loading

0 comments on commit 3a8a0e3

Please sign in to comment.