Skip to content

Commit

Permalink
refactor!: make trkGridSize non-template parameter in AdaptiveGridTra…
Browse files Browse the repository at this point in the history
…ckDensity (#2745)

BREAKING CHANGE: `spatialTrkGridSize` and `temporalTrkGridSize` need to be set in `Config` instead of being passed as template parameters. Concerns `AdaptiveGridTrackDensity` and `AdaptiveGridDensityVertexFinder`.
  • Loading branch information
felix-russo authored Dec 12, 2023
1 parent cf30700 commit 15203d1
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 190 deletions.
23 changes: 3 additions & 20 deletions Core/include/Acts/Vertexing/AdaptiveGridDensityVertexFinder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,17 @@ namespace Acts {
/// Unlike the GridDensityVertexFinder, this seeder implements an adaptive
/// version where the density grid grows bigger with added tracks.
///
/// @tparam trkGridSize The 2-dim grid size of a single track, i.e.
/// a single track is modelled as a (trkGridSize x trkGridSize) grid
/// in the d0-z0 plane. Note: trkGridSize has to be an odd value.
template <int spatialTrkGridSize = 15, int temporalTrkGridSize = 1,
typename vfitter_t = DummyVertexFitter<>>
/// @tparam vfitter_t Vertex fitter type
template <typename vfitter_t = DummyVertexFitter<>>
class AdaptiveGridDensityVertexFinder {
// Assert odd grid sizes
static_assert(spatialTrkGridSize % 2);
static_assert(temporalTrkGridSize % 2);

using InputTrack_t = typename vfitter_t::InputTrack_t;
using GridDensity =
AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>;
using GridDensity = AdaptiveGridTrackDensity;

public:
using DensityMap = typename GridDensity::DensityMap;

/// @brief The Config struct
struct Config {
/// @param spatialBinExtent The spatial extent of a bin in mm
Config(float spatialBinExtent = 0.1)
: gridDensity(typename GridDensity::Config(spatialBinExtent)) {}
/// @param spatialBinExtent The spatial extent of a bin in mm
/// @param temporalBinExtent The temporal extent of a bin in mm
/// @note The speed of light is set to 1, hence the unit.
Config(float spatialBinExtent, float temporalBinExtent)
: gridDensity(typename GridDensity::Config(spatialBinExtent,
temporalBinExtent)) {}
///@param gDensity The grid density
Config(const GridDensity& gDensity) : gridDensity(gDensity) {}

Expand Down
18 changes: 7 additions & 11 deletions Core/include/Acts/Vertexing/AdaptiveGridDensityVertexFinder.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
// 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/.

template <int spatialTrkGridSize, int temporalTrkGridSize, typename vfitter_t>
auto Acts::AdaptiveGridDensityVertexFinder<
spatialTrkGridSize, temporalTrkGridSize,
vfitter_t>::find(const std::vector<const InputTrack_t*>& trackVector,
const VertexingOptions<InputTrack_t>& vertexingOptions,
State& state) const
template <typename vfitter_t>
auto Acts::AdaptiveGridDensityVertexFinder<vfitter_t>::find(
const std::vector<const InputTrack_t*>& trackVector,
const VertexingOptions<InputTrack_t>& vertexingOptions, State& state) const
-> Result<std::vector<Vertex<InputTrack_t>>> {
// Remove density contributions from tracks removed from track collection
if (m_cfg.cacheGridStateForTrackRemoval && state.isInitialized &&
Expand Down Expand Up @@ -104,11 +102,9 @@ auto Acts::AdaptiveGridDensityVertexFinder<
return seedVec;
}

template <int spatialTrkGridSize, int temporalTrkGridSize, typename vfitter_t>
auto Acts::AdaptiveGridDensityVertexFinder<
spatialTrkGridSize, temporalTrkGridSize,
vfitter_t>::doesPassTrackSelection(const BoundTrackParameters& trk) const
-> bool {
template <typename vfitter_t>
auto Acts::AdaptiveGridDensityVertexFinder<vfitter_t>::doesPassTrackSelection(
const BoundTrackParameters& trk) const -> bool {
// Get required track parameters
const double d0 = trk.parameters()[BoundIndices::eBoundLoc0];
const double z0 = trk.parameters()[BoundIndices::eBoundLoc1];
Expand Down
48 changes: 17 additions & 31 deletions Core/include/Acts/Vertexing/AdaptiveGridTrackDensity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@ namespace Acts {
/// Single tracks can be cached and removed from the overall density.
/// Unlike in the GaussianGridTrackDensity, the overall density map
/// grows adaptively when tracks densities are added to the grid.
///
/// @tparam spatialTrkGridSize Number of bins per track in z direction
/// @tparam temporalTrkGridSize Number of bins per track in t direction
/// @note In total, a track is represented by a grid of size
/// spatialTrkGridSize * temporalTrkGridSize
template <int spatialTrkGridSize = 15, int temporalTrkGridSize = 1>
class AdaptiveGridTrackDensity {
// Assert odd spatial and temporal track grid size
static_assert(spatialTrkGridSize % 2);
static_assert(temporalTrkGridSize % 2);

public:
// The first (second) integer indicates the bin's z (t) position
Expand All @@ -52,32 +44,21 @@ class AdaptiveGridTrackDensity {

/// The configuration struct
struct Config {
/// @param spatialBinExtent_ The spatial extent of a bin in mm
Config(float spatialBinExtent_) : spatialBinExtent(spatialBinExtent_) {
if constexpr (temporalTrkGridSize > 1) {
throw std::invalid_argument(
"temporalBinExtent must be provided if temporalTrkGridSize > 1 "
"(i.e., if time vertex seeding is enabled).");
}
}
/// Default constructor
Config() = default;

/// @param spatialBinExtent_ The spatial extent of a bin in mm
/// @param temporalBinExtent_ The temporal extent of a bin in mm
/// @note The speed of light is set to 1, hence the unit.
Config(float spatialBinExtent_, float temporalBinExtent_)
: spatialBinExtent(spatialBinExtent_),
temporalBinExtent(temporalBinExtent_) {
if constexpr (temporalTrkGridSize == 1) {
throw std::invalid_argument(
"temporalBinExtent must not be provided if temporalTrkGridSize == "
"1 (i.e., if time vertex seeding is disabled).");
}
}
// In total, a track is represented by a grid of size
// spatialTrkGridSize * temporalTrkGridSize
// Number of bins per track in z direction
unsigned int spatialTrkGridSize = 15;

// Spatial extent of a bin in d0 and z0 direction, should always be set to a
// positive value
float spatialBinExtent = 0.; // mm

// Number of bins per track in t direction
unsigned int temporalTrkGridSize = 1;

// Temporal extent of a bin, should be set to 0 if time vertex seeding is
// disabled (i.e., if temporalTrkGridSize = 1)
float temporalBinExtent = 0.; // mm
Expand All @@ -96,7 +77,14 @@ class AdaptiveGridTrackDensity {
float maxRelativeDensityDev = 0.01;
};

AdaptiveGridTrackDensity(const Config& cfg) : m_cfg(cfg) {}
AdaptiveGridTrackDensity(const Config& cfg) : m_cfg(cfg) {
// Check that spatial and temporal track grid size are odd
if (!bool(m_cfg.spatialTrkGridSize % 2) ||
!bool(m_cfg.temporalTrkGridSize % 2)) {
throw std::invalid_argument(
"Please choose an odd spatialTrkGridSize and temporalTrkGridSize.");
}
}

/// @brief Calculates the bin center from the bin number
/// @param bin Bin number
Expand Down Expand Up @@ -219,5 +207,3 @@ class AdaptiveGridTrackDensity {
};

} // namespace Acts

#include "Acts/Vertexing/AdaptiveGridTrackDensity.ipp"
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,24 @@
// 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 "Acts/Vertexing/AdaptiveGridTrackDensity.hpp"

#include "Acts/Utilities/AlgebraHelpers.hpp"
#include "Acts/Vertexing/VertexingError.hpp"

#include <algorithm>

template <int spatialTrkGridSize, int temporalTrkGridSize>
float Acts::AdaptiveGridTrackDensity<
spatialTrkGridSize, temporalTrkGridSize>::getBinCenter(int bin,
float binExtent) {
float Acts::AdaptiveGridTrackDensity::getBinCenter(int bin, float binExtent) {
return bin * binExtent;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
int Acts::AdaptiveGridTrackDensity<
spatialTrkGridSize, temporalTrkGridSize>::getBin(float value,
float binExtent) {
int Acts::AdaptiveGridTrackDensity::getBin(float value, float binExtent) {
return static_cast<int>(std::floor(value / binExtent - 0.5) + 1);
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
typename Acts::AdaptiveGridTrackDensity<
spatialTrkGridSize, temporalTrkGridSize>::DensityMap::const_iterator
Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
highestDensityEntry(const DensityMap& densityMap) const {
typename Acts::AdaptiveGridTrackDensity::DensityMap::const_iterator
Acts::AdaptiveGridTrackDensity::highestDensityEntry(
const DensityMap& densityMap) const {
auto maxEntry = std::max_element(
std::begin(densityMap), std::end(densityMap),
[](const auto& densityEntry1, const auto& densityEntry2) {
Expand All @@ -37,11 +31,8 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
return maxEntry;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
Acts::Result<typename Acts::AdaptiveGridTrackDensity<
spatialTrkGridSize, temporalTrkGridSize>::ZTPosition>
Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
getMaxZTPosition(DensityMap& densityMap) const {
Acts::Result<typename Acts::AdaptiveGridTrackDensity::ZTPosition>
Acts::AdaptiveGridTrackDensity::getMaxZTPosition(DensityMap& densityMap) const {
if (densityMap.empty()) {
return VertexingError::EmptyInput;
}
Expand All @@ -61,19 +52,17 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
ZTPosition maxValues = std::make_pair(maxZ, 0.);

// Get t value of the maximum if we do time vertex seeding
if constexpr (temporalTrkGridSize > 1) {
if (m_cfg.temporalTrkGridSize > 1) {
float maxT = getBinCenter(bin.second, m_cfg.temporalBinExtent);
maxValues.second = maxT;
}

return maxValues;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
Acts::Result<typename Acts::AdaptiveGridTrackDensity<
spatialTrkGridSize, temporalTrkGridSize>::ZTPositionAndWidth>
Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
getMaxZTPositionAndWidth(DensityMap& densityMap) const {
Acts::Result<typename Acts::AdaptiveGridTrackDensity::ZTPositionAndWidth>
Acts::AdaptiveGridTrackDensity::getMaxZTPositionAndWidth(
DensityMap& densityMap) const {
// Get z value where the density is the highest
auto maxZTRes = getMaxZTPosition(densityMap);
if (!maxZTRes.ok()) {
Expand All @@ -91,19 +80,16 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
return maxZTAndWidth;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
typename Acts::AdaptiveGridTrackDensity<spatialTrkGridSize,
temporalTrkGridSize>::DensityMap
Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
addTrack(const Acts::BoundTrackParameters& trk,
DensityMap& mainDensityMap) const {
typename Acts::AdaptiveGridTrackDensity::DensityMap
Acts::AdaptiveGridTrackDensity::addTrack(const Acts::BoundTrackParameters& trk,
DensityMap& mainDensityMap) const {
ActsVector<3> impactParams = trk.impactParameters();
ActsSquareMatrix<3> cov = trk.impactParameterCovariance().value();

// Calculate bin in d direction
int centralDBin = getBin(impactParams(0), m_cfg.spatialBinExtent);
// Check if current track affects grid density
if (std::abs(centralDBin) > (spatialTrkGridSize - 1) / 2.) {
if (std::abs(centralDBin) > (m_cfg.spatialTrkGridSize - 1) / 2.) {
DensityMap emptyTrackDensityMap;
return emptyTrackDensityMap;
}
Expand All @@ -114,7 +100,7 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
Bin centralBin = std::make_pair(centralZBin, 0.);

// Calculate bin in t direction if we do time vertex seeding
if constexpr (temporalTrkGridSize > 1) {
if (m_cfg.temporalTrkGridSize > 1) {
int centralTBin = getBin(impactParams(2), m_cfg.temporalBinExtent);
centralBin.second = centralTBin;
}
Expand All @@ -135,48 +121,44 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
return trackDensityMap;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
void Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
subtractTrack(const DensityMap& trackDensityMap,
DensityMap& mainDensityMap) const {
void Acts::AdaptiveGridTrackDensity::subtractTrack(
const DensityMap& trackDensityMap, DensityMap& mainDensityMap) const {
for (auto it = trackDensityMap.begin(); it != trackDensityMap.end(); it++) {
mainDensityMap.at(it->first) -= it->second;
}
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
typename Acts::AdaptiveGridTrackDensity<spatialTrkGridSize,
temporalTrkGridSize>::DensityMap
Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
createTrackGrid(const Acts::Vector3& impactParams, const Bin& centralBin,
const Acts::SquareMatrix3& cov) const {
typename Acts::AdaptiveGridTrackDensity::DensityMap
Acts::AdaptiveGridTrackDensity::createTrackGrid(
const Acts::Vector3& impactParams, const Bin& centralBin,
const Acts::SquareMatrix3& cov) const {
DensityMap trackDensityMap;

int halfSpatialTrkGridSize = (spatialTrkGridSize - 1) / 2;
int halfSpatialTrkGridSize = (m_cfg.spatialTrkGridSize - 1) / 2;
int firstZBin = centralBin.first - halfSpatialTrkGridSize;

// If we don't do time vertex seeding, firstTBin will be 0.
int halfTemporalTrkGridSize = (temporalTrkGridSize - 1) / 2;
int halfTemporalTrkGridSize = (m_cfg.temporalTrkGridSize - 1) / 2;
int firstTBin = centralBin.second - halfTemporalTrkGridSize;

// Loop over bins
for (int i = 0; i < temporalTrkGridSize; i++) {
for (unsigned int i = 0; i < m_cfg.temporalTrkGridSize; i++) {
int tBin = firstTBin + i;
// If we don't do vertex time seeding, we set the time to 0 since it will be
// discarded in the for loop below anyways
float t = 0;
if constexpr (temporalTrkGridSize > 1) {
if (m_cfg.temporalTrkGridSize > 1) {
t = getBinCenter(tBin, m_cfg.temporalBinExtent);
}
for (int j = 0; j < spatialTrkGridSize; j++) {
for (unsigned int j = 0; j < m_cfg.spatialTrkGridSize; j++) {
int zBin = firstZBin + j;
float z = getBinCenter(zBin, m_cfg.spatialBinExtent);
// Bin coordinates in the d-z-t plane
Acts::Vector3 binCoords(0., z, t);
// Transformation to coordinate system with origin at the track center
binCoords -= impactParams;
Bin bin = std::make_pair(zBin, tBin);
if constexpr (temporalTrkGridSize == 1) {
if (m_cfg.temporalTrkGridSize == 1) {
trackDensityMap[bin] = multivariateGaussian<2>(
binCoords.head<2>(), cov.topLeftCorner<2, 2>());
} else {
Expand All @@ -187,11 +169,8 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
return trackDensityMap;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
Acts::Result<float> Acts::AdaptiveGridTrackDensity<
spatialTrkGridSize,
temporalTrkGridSize>::estimateSeedWidth(const DensityMap& densityMap,
const ZTPosition& maxZT) const {
Acts::Result<float> Acts::AdaptiveGridTrackDensity::estimateSeedWidth(
const DensityMap& densityMap, const ZTPosition& maxZT) const {
if (densityMap.empty()) {
return VertexingError::EmptyInput;
}
Expand All @@ -200,7 +179,7 @@ Acts::Result<float> Acts::AdaptiveGridTrackDensity<
int zMaxBin = getBin(maxZT.first, m_cfg.spatialBinExtent);
int tMaxBin = 0;
// Fill the time bin with a non-zero value if we do time vertex seeding
if constexpr (temporalTrkGridSize > 1) {
if (m_cfg.temporalTrkGridSize > 1) {
tMaxBin = getBin(maxZT.second, m_cfg.temporalBinExtent);
}
const float maxValue = densityMap.at(std::make_pair(zMaxBin, tMaxBin));
Expand Down Expand Up @@ -260,21 +239,18 @@ Acts::Result<float> Acts::AdaptiveGridTrackDensity<
return std::isnormal(width) ? width : 0.0f;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
template <unsigned int nDim>
float Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
multivariateGaussian(const Acts::ActsVector<nDim>& args,
const Acts::ActsSquareMatrix<nDim>& cov) {
float Acts::AdaptiveGridTrackDensity::multivariateGaussian(
const Acts::ActsVector<nDim>& args,
const Acts::ActsSquareMatrix<nDim>& cov) {
float expo = -0.5 * args.transpose().dot(cov.inverse() * args);
float gaussianDensity = safeExp(expo) / std::sqrt(cov.determinant());
return gaussianDensity;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
typename Acts::AdaptiveGridTrackDensity<spatialTrkGridSize,
temporalTrkGridSize>::Bin
Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
highestDensitySumBin(DensityMap& densityMap) const {
typename Acts::AdaptiveGridTrackDensity::Bin
Acts::AdaptiveGridTrackDensity::highestDensitySumBin(
DensityMap& densityMap) const {
// The global maximum
auto firstMax = highestDensityEntry(densityMap);
Bin binFirstMax = firstMax->first;
Expand Down Expand Up @@ -323,9 +299,8 @@ Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
return binFirstMax;
}

template <int spatialTrkGridSize, int temporalTrkGridSize>
float Acts::AdaptiveGridTrackDensity<spatialTrkGridSize, temporalTrkGridSize>::
getDensitySum(const DensityMap& densityMap, const Bin& bin) const {
float Acts::AdaptiveGridTrackDensity::getDensitySum(
const DensityMap& densityMap, const Bin& bin) const {
// Add density from the bin.
float sum = densityMap.at(bin);
// Check if neighboring bins are part of the densityMap and add them (if they
Expand Down
1 change: 1 addition & 0 deletions Core/src/Vertexing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target_sources(
ActsCore
PRIVATE
AdaptiveGridTrackDensity.cpp
FsmwMode1dFinder.cpp
VertexingError.cpp
)
Loading

0 comments on commit 15203d1

Please sign in to comment.