Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Memory dense MeasurementContainer for Examples #3528

Merged
merged 23 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion Core/include/Acts/EventData/SubspaceHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ inline static bool checkSubspaceIndices(const Container& container,
if (subspaceSize > fullSize) {
return false;
}
if (container.size() != subspaceSize) {
if (static_cast<std::size_t>(container.size()) != subspaceSize) {
return false;
}
for (auto it = container.begin(); it != container.end();) {
Expand Down Expand Up @@ -115,6 +115,36 @@ class SubspaceHelperBase {
auto begin() const { return self().begin(); }
auto end() const { return self().end(); }

bool contains(std::uint8_t index) const {
return std::find(begin(), end(), index) != end();
}
std::size_t indexOf(std::uint8_t index) const {
auto it = std::find(begin(), end(), index);
return it != end() ? std::distance(begin(), it) : kFullSize;
}

template <typename EigenDerived>
ActsVector<kFullSize> expandVector(
const Eigen::DenseBase<EigenDerived>& vector) const {
ActsVector<kFullSize> result = ActsVector<kFullSize>::Zero();
for (auto [i, index] : enumerate(*this)) {
result(index) = vector(i);
}
return result;
}

template <typename EigenDerived>
FullSquareMatrix expandMatrix(
const Eigen::DenseBase<EigenDerived>& matrix) const {
FullSquareMatrix result = FullSquareMatrix::Zero();
for (auto [i, indexI] : enumerate(*this)) {
for (auto [j, indexJ] : enumerate(*this)) {
result(indexI, indexJ) = matrix(i, j);
}
}
return result;
}

FullSquareMatrix fullProjector() const {
FullSquareMatrix result = FullSquareMatrix::Zero();
for (auto [i, index] : enumerate(*this)) {
Expand Down Expand Up @@ -168,6 +198,7 @@ class VariableSubspaceHelper

bool empty() const { return m_indices.empty(); }
std::size_t size() const { return m_indices.size(); }
const Container& indices() const { return m_indices; }

IndexType operator[](std::size_t i) const { return m_indices[i]; }

Expand Down Expand Up @@ -215,6 +246,7 @@ class FixedSubspaceHelper

bool empty() const { return m_indices.empty(); }
std::size_t size() const { return m_indices.size(); }
const Container& indices() const { return m_indices; }

IndexType operator[](std::uint32_t i) const { return m_indices[i]; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2021 CERN for the benefit of the Acts project
// Copyright (C) 2021-2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -40,9 +40,10 @@ struct DigitizedParameters {
///
/// To be used also by the e I/O system
///
/// @return a variant measurement
Measurement createMeasurement(const DigitizedParameters& dParams,
const IndexSourceLink& isl) noexcept(false);
/// @return the measurement proxy
ActsExamples::VariableBoundMeasurementProxy createMeasurement(
MeasurementContainer& container, const DigitizedParameters& dParams,
const IndexSourceLink& isl) noexcept(false);

/// Construct the constituents of a measurement.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2021 CERN for the benefit of the Acts project
// Copyright (C) 2021-2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -268,8 +268,7 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
// be added at the end.
sourceLinks.insert(sourceLinks.end(), sourceLink);

measurements.emplace_back(
createMeasurement(dParameters, sourceLink));
createMeasurement(measurements, dParameters, sourceLink);
clusters.emplace_back(std::move(dParameters.cluster));
// this digitization does hit merging so there can be more than one
// mapping entry for each digitized hit.
Expand Down
47 changes: 23 additions & 24 deletions Examples/Algorithms/Digitization/src/MeasurementCreation.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
// This file is part of the Acts project.
//
// Copyright (C) 2021 CERN for the benefit of the Acts project
// Copyright (C) 2021-2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "ActsExamples/Digitization/MeasurementCreation.hpp"

#include "Acts/EventData/MeasurementHelpers.hpp"
#include "Acts/EventData/SourceLink.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"

#include <stdexcept>
#include <string>
#include <utility>

ActsExamples::Measurement ActsExamples::createMeasurement(
const DigitizedParameters& dParams, const IndexSourceLink& isl) {
ActsExamples::VariableBoundMeasurementProxy ActsExamples::createMeasurement(
MeasurementContainer& container, const DigitizedParameters& dParams,
const IndexSourceLink& isl) {
Acts::SourceLink sl{isl};
switch (dParams.indices.size()) {
case 1u: {
auto [indices, par, cov] = measurementConstituents<1>(dParams);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
}
case 2u: {
auto [indices, par, cov] = measurementConstituents<2>(dParams);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
};
case 3u: {
auto [indices, par, cov] = measurementConstituents<3>(dParams);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
};
case 4u: {
auto [indices, par, cov] = measurementConstituents<4>(dParams);
return ActsExamples::Measurement(std::move(sl), indices, par, cov);
};
default:
std::string errorMsg = "Invalid/mismatching measurement dimension: " +
std::to_string(dParams.indices.size());
throw std::runtime_error(errorMsg.c_str());

if (dParams.indices.size() > 4u) {
std::string errorMsg = "Invalid/mismatching measurement dimension: " +
std::to_string(dParams.indices.size());
throw std::runtime_error(errorMsg.c_str());
}

return Acts::visit_measurement(
dParams.indices.size(), [&](auto dim) -> VariableBoundMeasurementProxy {
auto [indices, par, cov] = measurementConstituents<dim>(dParams);
FixedBoundMeasurementProxy<dim> measurement =
container.makeMeasurement<dim>();
measurement.setSourceLink(sl);
measurement.setSubspaceIndices(indices);
measurement.parameters() = par;
measurement.covariance() = cov;
return measurement;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ 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.
const auto& measurement = measurements[sourceLink.index()];
const ConstVariableBoundMeasurementProxy measurement =
measurements.getMeasurement(sourceLink.index());

assert(measurement.contains(Acts::eBoundLoc0) &&
"Measurement does not contain the required bound loc0");
Expand Down
3 changes: 2 additions & 1 deletion Examples/Algorithms/TrackFinding/src/SpacePointMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ ActsExamples::ProcessCode ActsExamples::SpacePointMaker::execute(

spOpt.paramCovAccessor = [&measurements](Acts::SourceLink slink) {
const auto islink = slink.get<IndexSourceLink>();
const auto& meas = measurements[islink.index()];
const ConstVariableBoundMeasurementProxy meas =
measurements.getMeasurement(islink.index());

return std::make_pair(meas.fullParameters(), meas.fullCovariance());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Acts/Surfaces/PerigeeSurface.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/Result.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/MeasurementCalibration.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"
Expand Down Expand Up @@ -126,7 +127,8 @@ ActsExamples::ProcessCode ActsExamples::TrackFittingAlgorithm::execute(

// Fill the source links via their indices from the container
for (auto hitIndex : protoTrack) {
const auto& measurement = measurements.at(hitIndex);
ConstVariableBoundMeasurementProxy measurement =
measurements.getMeasurement(hitIndex);
trackSourceLinks.push_back(measurement.sourceLink());
}

Expand Down
1 change: 1 addition & 0 deletions Examples/Framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(ActsExamplesFramework_SOURCES)
add_library(
ActsExamplesFramework
SHARED
src/EventData/Measurement.cpp
src/EventData/MeasurementCalibration.cpp
src/EventData/ScalingCalibrator.cpp
src/Framework/IAlgorithm.cpp
Expand Down
32 changes: 18 additions & 14 deletions Examples/Framework/ML/src/NeuralCalibrator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 CERN for the benefit of the Acts project
// Copyright (C) 2023-2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -106,7 +106,8 @@ void ActsExamples::NeuralCalibrator::calibrate(
const Acts::Surface& referenceSurface = trackState.referenceSurface();
auto trackParameters = trackState.parameters();

const auto& measurement = measurements[idxSourceLink.index()];
const ConstVariableBoundMeasurementProxy measurement =
measurements.getMeasurement(idxSourceLink.index());

assert(measurement.contains(Acts::eBoundLoc0) &&
"Measurement does not contain the required bound loc0");
Expand Down Expand Up @@ -171,21 +172,24 @@ void ActsExamples::NeuralCalibrator::calibrate(
std::size_t iLoc0 = m_nComponents + iMax * 2;
std::size_t iVar0 = 3 * m_nComponents + iMax * 2;

Measurement measurementCopy = measurement;
measurementCopy.parameters()[boundLoc0] = output[iLoc0];
measurementCopy.parameters()[boundLoc1] = output[iLoc0 + 1];
measurementCopy.covariance()(boundLoc0, boundLoc0) = output[iVar0];
measurementCopy.covariance()(boundLoc1, boundLoc1) = output[iVar0 + 1];

Acts::visit_measurement(measurement.size(), [&](auto N) -> void {
constexpr std::size_t kMeasurementSize = decltype(N)::value;
const ConstFixedBoundMeasurementProxy<kMeasurementSize> fixedMeasurement =
measurement;

Acts::ActsVector<kMeasurementSize> calibratedParameters =
fixedMeasurement.parameters();
Acts::ActsSquareMatrix<kMeasurementSize> calibratedCovariance =
fixedMeasurement.covariance();

calibratedParameters[boundLoc0] = output[iLoc0];
calibratedParameters[boundLoc1] = output[iLoc0 + 1];
calibratedCovariance(boundLoc0, boundLoc0) = output[iVar0];
calibratedCovariance(boundLoc1, boundLoc1) = output[iVar0 + 1];

trackState.allocateCalibrated(kMeasurementSize);
trackState.calibrated<kMeasurementSize>() =
measurementCopy.parameters<kMeasurementSize>();
trackState.calibratedCovariance<kMeasurementSize>() =
measurementCopy.covariance<kMeasurementSize>();
trackState.setSubspaceIndices(
measurementCopy.subspaceIndices<kMeasurementSize>());
trackState.calibrated<kMeasurementSize>() = calibratedParameters;
trackState.calibratedCovariance<kMeasurementSize>() = calibratedCovariance;
trackState.setSubspaceIndices(fixedMeasurement.subspaceIndices());
});
}
Loading
Loading