Skip to content

Commit

Permalink
feat: Dense MeasurementContainer for Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Aug 21, 2024
1 parent 202fd53 commit a3245df
Show file tree
Hide file tree
Showing 16 changed files with 482 additions and 293 deletions.
47 changes: 46 additions & 1 deletion Core/include/Acts/EventData/SubspaceHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 All @@ -49,6 +49,7 @@ class SubspaceHelperBase {
public:
static constexpr std::size_t kFullSize = FullSize;

using FullVector = ActsVector<kFullSize>;
using FullSquareMatrix = ActsSquareMatrix<kFullSize>;

bool empty() const { return self().empty(); }
Expand All @@ -59,6 +60,19 @@ class SubspaceHelperBase {
auto begin() const { return self().begin(); }
auto end() const { return self().end(); }

template <typename indices_t>
bool contains(indices_t i) const {
return std::find(begin(), end(), static_cast<Derived::IndexType>(i)) !=
end();
}

template <typename indices_t>
std::size_t indexOf(indices_t i) const {
auto it = std::find(begin(), end(), static_cast<Derived::IndexType>(i));
assert(it != end());
return std::distance(begin(), it);
}

FullSquareMatrix fullProjector() const {
FullSquareMatrix result = FullSquareMatrix::Zero();
for (auto [i, index] : enumerate(*this)) {
Expand All @@ -81,6 +95,33 @@ class SubspaceHelperBase {
return matrixToBitset(fullProjector()).to_ullong();
}

template <typename EigenDerived>
FullVector expandVector(
const Eigen::DenseBase<EigenDerived>& subspaceVector) const {
assert(static_cast<std::size_t>(subspaceVector.size()) == size() &&
"Invalid subspace vector size");
FullVector result = FullVector::Zero();
for (auto [i, index] : enumerate(*this)) {
result(index) = subspaceVector(i);
}
return result;
}

template <typename EigenDerived>
FullSquareMatrix expandMatrix(
const Eigen::DenseBase<EigenDerived>& subspaceMatrix) const {
assert(static_cast<std::size_t>(subspaceMatrix.rows()) == size() &&
static_cast<std::size_t>(subspaceMatrix.cols()) == size() &&
"Invalid subspace matrix size");
FullSquareMatrix result = FullSquareMatrix::Zero();
for (auto [i, indexI] : enumerate(*this)) {
for (auto [j, indexJ] : enumerate(*this)) {
result(indexI, indexJ) = subspaceMatrix(i, j);
}
}
return result;
}

private:
const Derived& self() const { return static_cast<const Derived&>(*this); }
};
Expand Down Expand Up @@ -112,6 +153,8 @@ class VariableSubspaceHelper
auto begin() const { return m_indices.begin(); }
auto end() const { return m_indices.end(); }

const Container& indices() const { return m_indices; }

private:
Container m_indices;
};
Expand Down Expand Up @@ -154,6 +197,8 @@ class FixedSubspaceHelper
auto begin() const { return m_indices.begin(); }
auto end() const { return m_indices.end(); }

const Container& indices() const { return m_indices; }

Projector projector() const {
Projector result = Projector::Zero();
for (auto [i, index] : enumerate(*this)) {
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
46 changes: 22 additions & 24 deletions Examples/Algorithms/Digitization/src/MeasurementCreation.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
// 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.setSubspaceIndices(indices);
measurement.parameters() = par;
measurement.covariance() = cov;
return measurement;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,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
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
Loading

0 comments on commit a3245df

Please sign in to comment.