Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackContainer.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackContainer.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
17 #include "Acts/EventData/Types.hpp"
18 #include "Acts/EventData/Utils.hpp"
22 
23 #include <any>
24 #include <cstddef>
25 #include <iterator>
26 
27 namespace Acts {
28 
29 template <typename T>
31 
38 template <ACTS_CONCEPT(TrackContainerBackend) track_container_t,
39  typename traj_t,
40  template <typename> class holder_t = detail::RefHolder>
42  public:
43  static constexpr bool ReadOnly =
45  static constexpr bool TrackStateReadOnly =
47 
48  static_assert(ReadOnly == TrackStateReadOnly,
49  "Either both track container and track state container need to "
50  "be readonly or both have to be readwrite");
51 
54 
55  using TrackProxy =
57  using ConstTrackProxy =
59 
60 #ifndef DOXYGEN
61  friend TrackProxy;
63 #endif
64 
69  TrackContainer(holder_t<track_container_t> container, holder_t<traj_t> traj)
70  : m_container{std::move(container)}, m_traj{std::move(traj)} {}
71 
78  template <template <typename> class H = holder_t,
79  typename = std::enable_if_t<
81  TrackContainer(track_container_t& container, traj_t& traj)
82  : m_container{&container}, m_traj{&traj} {}
83 
90  template <
91  template <typename> class H = holder_t,
94  typename = std::enable_if_t<
96  TrackContainer(const track_container_t& container, const traj_t& traj)
97  : m_container{&container}, m_traj{&traj} {}
98 
102  ConstTrackProxy getTrack(IndexType itrack) const {
103  return {*this, itrack};
104  }
105 
109  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
110  TrackProxy getTrack(IndexType itrack) {
111  return {*this, itrack};
112  }
113 
116  constexpr IndexType size() const {
117  return m_container->size_impl();
118  }
119 
123  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
124  IndexType addTrack() {
125  auto track = getTrack(m_container->addTrack_impl());
126  track.tipIndex() = kInvalid;
127  return track.index();
128  }
129 
133  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
134  void removeTrack(IndexType itrack) {
135  m_container->removeTrack_impl(itrack);
136  }
137 
140  template <typename T, bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
141  constexpr void addColumn(const std::string& key) {
142  m_container->template addColumn_impl<T>(key);
143  }
144 
147  constexpr bool hasColumn(const std::string& key) const {
148  return m_container->hasColumn_impl(hashString(key));
149  }
150 
153  constexpr bool hasColumn(HashedString key) const {
154  return m_container->hasColumn_impl(key);
155  }
156 
159  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
160  auto& container() {
161  return *m_container;
162  }
163 
166  const auto& container() const {
167  return *m_container;
168  }
169 
172  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
173  auto& trackStateContainer() {
174  return *m_traj;
175  }
176 
179  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
180  auto& trackStateContainerHolder() {
181  return m_traj;
182  }
183 
186  const auto& trackStateContainer() const {
187  return *m_traj;
188  }
189 
192  const auto& trackStateContainerHolder() const {
193  return m_traj;
194  }
195 
198  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
199  auto begin() {
200  return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
201  TrackProxy, false>{*this, 0};
202  }
203 
206  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
207  auto end() {
208  return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
209  TrackProxy, false>{*this, size()};
210  }
211 
214  auto begin() const {
215  return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
216  ConstTrackProxy, true>{*this, 0};
217  }
218 
221  auto end() const {
222  return detail_tc::TrackProxyIterator<std::decay_t<decltype(*this)>,
223  ConstTrackProxy, true>{*this, size()};
224  }
225 
232  template <typename other_track_container_t, bool RO = ReadOnly,
233  typename = std::enable_if_t<!RO>>
234  void ensureDynamicColumns(const other_track_container_t& other) {
235  container().ensureDynamicColumns_impl(other.container());
236  }
237 
239  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
240  void clear() {
241  m_container->clear();
242  m_traj->clear();
243  }
244 
245  protected:
246  template <typename T, HashedString key, bool RO = ReadOnly,
247  typename = std::enable_if_t<!RO>>
248  constexpr T& component(IndexType itrack) {
249  return *std::any_cast<T*>(container().component_impl(key, itrack));
250  }
251 
252  template <typename T, bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
253  constexpr T& component(HashedString key, IndexType itrack) {
254  return *std::any_cast<T*>(container().component_impl(key, itrack));
255  }
256 
257  template <typename T, HashedString key>
258  constexpr const T& component(IndexType itrack) const {
259  return *std::any_cast<const T*>(container().component_impl(key, itrack));
260  }
261 
262  template <typename T>
263  constexpr const T& component(HashedString key, IndexType itrack) const {
264  return *std::any_cast<const T*>(container().component_impl(key, itrack));
265  }
266 
267  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
268  constexpr typename TrackProxy::Parameters parameters(IndexType itrack) {
269  return container().parameters(itrack);
270  }
271 
272  constexpr typename ConstTrackProxy::ConstParameters parameters(
273  IndexType itrack) const {
274  return container().parameters(itrack);
275  }
276 
277  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
278  constexpr typename TrackProxy::Covariance covariance(IndexType itrack) {
279  return container().covariance(itrack);
280  }
281 
282  constexpr typename ConstTrackProxy::ConstCovariance covariance(
283  IndexType itrack) const {
284  return container().covariance(itrack);
285  }
286 
287  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
288  auto reverseTrackStateRange(IndexType itrack) {
289  auto tip = component<IndexType, hashString("tipIndex")>(itrack);
290  return m_traj->reverseTrackStateRange(tip);
291  }
292 
293  auto reverseTrackStateRange(IndexType itrack) const {
294  auto tip = component<IndexType, hashString("tipIndex")>(itrack);
295  return m_traj->reverseTrackStateRange(tip);
296  }
297 
298  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
299  auto forwardTrackStateRange(IndexType itrack) {
300  auto stem = component<IndexType, hashString("stemIndex")>(itrack);
301  if (stem == kInvalid) {
302  throw std::invalid_argument{"Track has no stem index"};
303  }
304  return m_traj->forwardTrackStateRange(stem);
305  }
306 
307  auto forwardTrackStateRange(IndexType itrack) const {
308  auto stem = component<IndexType, hashString("stemIndex")>(itrack);
309  if (stem == kInvalid) {
310  throw std::invalid_argument{"Track has no stem index"};
311  }
312  return m_traj->forwardTrackStateRange(stem);
313  }
314 
315  private:
316  template <typename T, bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
317  void copyDynamicFrom(IndexType dstIdx, const T& src, IndexType srcIdx) {
318  container().copyDynamicFrom_impl(dstIdx, src, srcIdx);
319  }
320 
321  detail_tc::ConstIf<holder_t<track_container_t>, ReadOnly> m_container;
323 };
324 
325 template <ACTS_CONCEPT(TrackContainerBackend) track_container_t,
326  typename traj_t>
327 TrackContainer(track_container_t& container, traj_t& traj)
328  -> TrackContainer<track_container_t, traj_t, detail::RefHolder>;
329 
330 template <ACTS_CONCEPT(TrackContainerBackend) track_container_t,
331  typename traj_t>
332 TrackContainer(const track_container_t& container, const traj_t& traj)
333  -> TrackContainer<track_container_t, traj_t, detail::ConstRefHolder>;
334 
335 template <ACTS_CONCEPT(TrackContainerBackend) track_container_t,
336  typename traj_t>
337 TrackContainer(track_container_t&& container, traj_t&& traj)
338  -> TrackContainer<track_container_t, traj_t, detail::ValueHolder>;
339 
343 template <typename T, bool ReadOnly>
346 
349  TrackAccessorBase(HashedString _key) : key{_key} {}
352  TrackAccessorBase(const std::string& _key) : key{hashString(_key)} {}
353 
358  template <typename track_proxy_t, bool RO = ReadOnly,
359  typename = std::enable_if_t<!RO>>
360  T& operator()(track_proxy_t track) const {
361  static_assert(!track_proxy_t::ReadOnly,
362  "Cannot get mutable ref for const track proxy");
363  return track.template component<T>(key);
364  }
365 
370  template <typename track_proxy_t, bool RO = ReadOnly,
371  typename = std::enable_if_t<RO>>
372  const T& operator()(track_proxy_t track) const {
373  if constexpr (track_proxy_t::ReadOnly) {
374  return track.template component<T>(key);
375  } else {
376  typename track_proxy_t::ConstTrackProxy ctrack{track};
377  return ctrack.template component<T>(key);
378  }
379  }
380 };
381 
382 template <typename T>
384 template <typename T>
386 
387 } // namespace Acts