Skip to content

Commit

Permalink
feat: Enable removing tracks from container (#1870)
Browse files Browse the repository at this point in the history
This also moves some method definitions to a cpp file for VectorTrackContainer, and adds the ability to copy a const track vector container to a mutable one (for modification).
  • Loading branch information
paulgessinger committed Feb 17, 2023
1 parent 2c2c0fc commit bcc917d
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Core/include/Acts/EventData/Track.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@ class TrackContainer {
return m_container->addTrack_impl();
}

/// Remove a track at index @p itrack from the container
/// @note This invalidates all track proxies!
/// @param itrack The index of the track to remmove
template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
void removeTrack(IndexType itrack) {
m_container->removeTrack_impl(itrack);
}

/// Add a dymanic column to the track container
/// @param key the name of the column to be added
template <typename T, bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
Expand Down
76 changes: 48 additions & 28 deletions Core/include/Acts/EventData/VectorTrackContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ class VectorTrackContainerBase {
protected:
VectorTrackContainerBase() = default;

VectorTrackContainerBase(const VectorTrackContainerBase& other)
: m_tipIndex{other.m_tipIndex},
m_params{other.m_params},
m_cov{other.m_cov},
m_referenceSurfaces{other.m_referenceSurfaces} {
for (const auto& [key, value] : other.m_dynamic) {
m_dynamic.insert({key, value->clone()});
}
};
VectorTrackContainerBase(const VectorTrackContainerBase& other);

VectorTrackContainerBase(VectorTrackContainerBase&& other) = default;

Expand Down Expand Up @@ -88,6 +80,31 @@ class VectorTrackContainerBase {
}
}

bool checkConsistency() const {
size_t size = m_tipIndex.size();
(void)size;

bool result = true;
result = result && m_tipIndex.size() == size;
assert(result);
result = result && m_params.size() == size;
assert(result);
result = result && m_cov.size() == size;
assert(result);
result = result && m_referenceSurfaces.size() == size;
assert(result);
result = result && m_nMeasurements.size() == size;
assert(result);
result = result && m_nHoles.size() == size;

for (const auto& [key, col] : m_dynamic) {
(void)key;
result = result && col->size() == size;
}

return result;
}

public:
constexpr bool hasColumn_impl(HashedString key) const {
using namespace Acts::HashedStringLiteral;
Expand All @@ -97,7 +114,10 @@ class VectorTrackContainerBase {
}
}

std::size_t size_impl() const { return m_tipIndex.size(); }
std::size_t size_impl() const {
assert(checkConsistency());
return m_tipIndex.size();
}
// END INTERFACE HELPER

std::vector<IndexType> m_tipIndex;
Expand All @@ -115,6 +135,8 @@ class VectorTrackContainerBase {
} // namespace detail_vtc

class VectorTrackContainer;
class ConstVectorTrackContainer;

template <>
struct IsReadOnlyTrackContainer<VectorTrackContainer> : std::false_type {};

Expand All @@ -124,6 +146,8 @@ class VectorTrackContainer final : public detail_vtc::VectorTrackContainerBase {
VectorTrackContainer(const VectorTrackContainer& other) = default;
VectorTrackContainer(VectorTrackContainer&&) = default;

VectorTrackContainer(const ConstVectorTrackContainer& other);

public:
// BEGIN INTERFACE

Expand All @@ -137,23 +161,9 @@ class VectorTrackContainer final : public detail_vtc::VectorTrackContainerBase {
*this, key, itrack);
}

IndexType addTrack_impl() {
m_tipIndex.emplace_back();
IndexType addTrack_impl();

m_params.emplace_back();
m_cov.emplace_back();
m_referenceSurfaces.emplace_back();

m_nMeasurements.emplace_back();
m_nHoles.emplace_back();

// dynamic columns
for (auto& [key, vec] : m_dynamic) {
vec->add();
}

return m_tipIndex.size() - 1;
}
void removeTrack_impl(IndexType itrack);

template <typename T>
constexpr void addColumn_impl(const std::string& key) {
Expand Down Expand Up @@ -191,11 +201,15 @@ class ConstVectorTrackContainer final

ConstVectorTrackContainer(const ConstVectorTrackContainer& other) = default;
ConstVectorTrackContainer(const VectorTrackContainer& other)
: VectorTrackContainerBase{other} {}
: VectorTrackContainerBase{other} {
assert(checkConsistency());
}

ConstVectorTrackContainer(ConstVectorTrackContainer&&) = default;
ConstVectorTrackContainer(VectorTrackContainer&& other)
: VectorTrackContainerBase{std::move(other)} {}
: VectorTrackContainerBase{std::move(other)} {
assert(checkConsistency());
}

public:
// BEGIN INTERFACE
Expand All @@ -216,4 +230,10 @@ class ConstVectorTrackContainer final
// END INTERFACE
};

inline VectorTrackContainer::VectorTrackContainer(
const ConstVectorTrackContainer& other)
: VectorTrackContainerBase{other} {
assert(checkConsistency());
}

} // namespace Acts
6 changes: 6 additions & 0 deletions Core/include/Acts/EventData/detail/DynamicColumn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct DynamicColumnBase {

virtual void add() = 0;
virtual void clear() = 0;
virtual void erase(size_t i) = 0;
virtual size_t size() const = 0;

virtual std::unique_ptr<DynamicColumnBase> clone() const = 0;
};
Expand All @@ -39,6 +41,8 @@ struct DynamicColumn : public DynamicColumnBase {

void add() override { m_vector.emplace_back(); }
void clear() override { m_vector.clear(); }
void erase(size_t i) override { m_vector.erase(m_vector.begin() + i); }
size_t size() const override { return m_vector.size(); }

std::unique_ptr<DynamicColumnBase> clone() const override {
return std::make_unique<DynamicColumn<T>>(*this);
Expand All @@ -65,6 +69,8 @@ struct DynamicColumn<bool> : public DynamicColumnBase {

void add() override { m_vector.emplace_back(); }
void clear() override { m_vector.clear(); }
void erase(size_t i) override { m_vector.erase(m_vector.begin() + i); }
size_t size() const override { return m_vector.size(); }

std::unique_ptr<DynamicColumnBase> clone() const override {
return std::make_unique<DynamicColumn<bool>>(*this);
Expand Down
1 change: 1 addition & 0 deletions Core/src/EventData/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ target_sources(
CorrectedTransformationFreeToBound.cpp
TrackStatePropMask.cpp
VectorMultiTrajectory.cpp
VectorTrackContainer.cpp
)
75 changes: 75 additions & 0 deletions Core/src/EventData/VectorTrackContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 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 "Acts/EventData/VectorTrackContainer.hpp"

namespace Acts {

namespace detail_vtc {

VectorTrackContainerBase::VectorTrackContainerBase(
const VectorTrackContainerBase& other)
: m_tipIndex{other.m_tipIndex},
m_params{other.m_params},
m_cov{other.m_cov},
m_referenceSurfaces{other.m_referenceSurfaces},
m_nMeasurements{other.m_nMeasurements},
m_nHoles{other.m_nHoles} {
for (const auto& [key, value] : other.m_dynamic) {
m_dynamic.insert({key, value->clone()});
}

assert(checkConsistency());
}
} // namespace detail_vtc

VectorTrackContainer::IndexType VectorTrackContainer::addTrack_impl() {
assert(checkConsistency());

m_tipIndex.emplace_back();

m_params.emplace_back();
m_cov.emplace_back();
m_referenceSurfaces.emplace_back();

m_nMeasurements.emplace_back();
m_nHoles.emplace_back();

// dynamic columns
for (auto& [key, vec] : m_dynamic) {
vec->add();
}

assert(checkConsistency());

return m_tipIndex.size() - 1;
}

void VectorTrackContainer::removeTrack_impl(IndexType itrack) {
auto erase = [&](auto& vec) {
assert(itrack < vec.size() && "Index is out of range");
auto it = vec.begin();
std::advance(it, itrack);
vec.erase(it);
};

erase(m_tipIndex);

erase(m_params);
erase(m_cov);
erase(m_referenceSurfaces);

erase(m_nMeasurements);
erase(m_nHoles);

for (auto& [key, vec] : m_dynamic) {
vec->erase(itrack);
}
}

} // namespace Acts

0 comments on commit bcc917d

Please sign in to comment.