LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Track.h
Go to the documentation of this file.
1 
229 #ifndef LARDATA_RECOBASEPROXY_TRACK_H
230 #define LARDATA_RECOBASEPROXY_TRACK_H
231 
232 // LArSoft libraries
233 #include "lardata/RecoBaseProxy/ProxyBase.h" // proxy namespace
235 #include "lardataobj/RecoBase/Hit.h"
239 
240 // framework libraries
242 
243 #include <limits>
244 #include <tuple>
245 #include <vector>
246 
247 namespace proxy {
248 
249  //----------------------------------------------------------------------------
250  // forward declarations
251  template <typename TrackProxy>
253 
254  template <typename Data>
256 
257  //----------------------------------------------------------------------------
258  namespace details {
259 
260  template <typename CollProxy>
262 
263  template <typename T>
264  struct isTrackProxy;
265 
266  template <typename Obj>
268 
269  template <typename Data>
270  struct StaticAsserts<TrackPointWrapper<Data>> : public std::true_type {
272 
273  static_assert(sizeof(Wrapper_t) == 1U, "Wrapper carries data!");
274 
275  static_assert(std::is_same<std::decay_t<decltype(std::declval<Wrapper_t>().position())>,
277  "position() is not a recob::Track::Point_t");
278  static_assert(std::is_same<std::decay_t<decltype(std::declval<Wrapper_t>().momentum())>,
280  "momentum() is not a recob::Track::Vector_t");
281  static_assert(std::is_same<std::decay_t<decltype(std::declval<Wrapper_t>().flags())>,
283  "flags() is not a recob::Track::PointFlags_t");
284  static_assert(std::is_same<std::decay_t<decltype(std::declval<Wrapper_t>().hitPtr())>,
286  "hit() is not a art::Ptr<recob::Hit>");
287  static_assert(
288  std::is_same<std::decay_t<decltype(std::declval<Wrapper_t>().index())>, std::size_t>(),
289  "index() is not a std::size_t");
290 
291  }; // StaticAsserts<TrackPointWrapper<Data>>
292 
293  } // namespace details
294 
295  //----------------------------------------------------------------------------
296 
481  struct Tracks {
482 
484  using TrackDataProduct_t = std::vector<recob::Track>;
485 
488 
491 
494 
496  typedef enum {
499  NTypes
500  } TrackType_t;
501 
502  }; // struct Tracks
503 
504  //----------------------------------------------------------------------------
505  //--- track point information
506  //---
532  using TrackPointData = std::
533  tuple<recob::Track const*, art::Ptr<recob::Hit>, recob::TrackFitHitInfo const*, std::size_t>;
534 
563  template <typename Data>
564  class TrackPointWrapper {
566  using Wrapped_t = std::add_const_t<Data>;
567 
568  static constexpr std::size_t TrackIndex = 0;
569  static constexpr std::size_t HitIndex = 1;
570  static constexpr std::size_t FitHitInfoIndex = 2;
571  static constexpr std::size_t IndexIndex = 3;
572  static constexpr std::size_t NIndices = 4;
573 
574  static_assert(std::tuple_size<Data>::value == NIndices, "Unexpected data size.");
575 
576  Wrapped_t const& base() const { return reinterpret_cast<Wrapped_t const&>(*this); }
577 
578  template <std::size_t N>
579  auto get() const -> decltype(auto)
580  {
581  return std::get<N>(base());
582  }
583 
584  protected:
585  TrackPointWrapper() = default;
586  TrackPointWrapper(TrackPointWrapper const&) = default;
588  TrackPointWrapper& operator=(TrackPointWrapper const&) = default;
589  TrackPointWrapper& operator=(TrackPointWrapper&&) = default;
590 
591  public:
593  recob::Track const& track() const { return *get<TrackIndex>(); }
594 
597  auto position() const -> decltype(auto)
598  {
599  return track().Trajectory().LocationAtPoint(index());
600  }
601 
604  auto momentum() const -> decltype(auto)
605  {
606  return track().Trajectory().MomentumVectorAtPoint(index());
607  }
608 
614  auto flags() const -> decltype(auto) { return track().Trajectory().FlagsAtPoint(index()); }
617 
624  bool isPointValid() const { return flags().isPointValid(); }
625 
627 
632  art::Ptr<recob::Hit> hitPtr() const { return get<HitIndex>(); }
633 
643  recob::TrackFitHitInfo const* fitInfoPtr() const { return get<FitHitInfoIndex>(); }
644 
646  auto index() const -> decltype(auto) { return get<IndexIndex>(); }
647 
649  recob::Hit const* hit() const
650  {
651  decltype(auto) ptr = hitPtr();
652  return ptr ? ptr.get() : nullptr;
653  }
654 
655  }; // TrackPointWrapper<>
656 
664  struct TrackPoint : private TrackPointData, public TrackPointWrapper<TrackPointData> {
666  TrackPoint(TrackPointData const& data) : TrackPointData(data) {}
667  TrackPoint(TrackPointData&& data) : TrackPointData(std::move(data)) {}
668 
669  private:
670  static constexpr bool asserts =
672  }; // class TrackPoint
673 
683  template <typename TrackProxy>
684  TrackPointData makeTrackPointData(TrackProxy const& track, std::size_t index)
685  {
686  static_assert(details::isTrackProxy<TrackProxy>(), "Not a proxy::Track!");
687  return {&(track.track()), track.hitAtPoint(index), track.fitInfoAtPoint(index), index};
688  } // makeTrackPointData()
689 
690  //--------------------------------------------------------------------------
699  template <typename CollProxy>
702  using base_t::base_t; // inherit constructors
703 
706 
707  public:
710 
712  recob::Track const& track() const { return base_t::operator*(); }
713 
720  recob::TrackTrajectory const* operator()(proxy::Tracks::TrackType_t type) const noexcept;
721 
722  // --- BEGIN Direct hit interface ------------------------------------------
748 
758  auto hits() const -> decltype(auto) { return base_t::template get<Tracks::HitTag>(); }
759 
761  auto hitAtPoint(std::size_t index) const -> decltype(auto) { return hits()[index]; }
762 
764  std::size_t nHits() const { return hits().size(); }
765 
767  // --- END Direct hit interface --------------------------------------------
768 
770  recob::TrackFitHitInfo const* fitInfoAtPoint(std::size_t index) const;
771 
772  // --- BEGIN Direct track trajectory interface -----------------------------
797 
800  bool hasOriginalTrajectory() const { return !originalTrajectoryPtr().isNull(); }
801 
805  {
806  return base_t::template get<Tracks::TrackTrajectoryTag>();
807  }
808 
818  recob::TrackTrajectory const& originalTrajectory() const { return *originalTrajectoryPtr(); }
819 
821  // --- END Direct track trajectory interface -------------------------------
822 
823  // --- BEGIN Point-by-point iteration interface ----------------------------
848 
870  auto points() const { return details::TrackPointIteratorBox<CollProxy>(*this); }
871 
910  template <typename Pred>
911  auto selectPoints(Pred&& pred) const;
912 
941  auto pointsWithFlags(recob::TrackTrajectory::PointFlags_t::Mask_t mask) const;
942 
944  std::size_t nPoints() const { return track().NPoints(); }
945 
947  point_iterator beginPoint() const { return {*this, 0}; }
948 
950  point_iterator endPoint() const { return {*this, nPoints()}; }
951 
954  TrackPoint point(std::size_t index) const { return {makeTrackPointData(track(), index)}; }
955 
956  TrackPoint operator[](std::size_t index) const { return point(index); }
957 
959  // --- END Point-by-point iteration interface ------------------------------
960 
961  // --- BEGIN Additional utilities ------------------------------------------
964 
966  // --- END Additional utilities --------------------------------------------
967 
968  private:
970  {
971  return hasOriginalTrajectory() ? &originalTrajectory() : nullptr;
972  }
973 
974  }; // TrackCollectionProxyElement<>
975 
991  template <typename TrackCollProxy>
993 
994  // --- BEGIN Auxiliary data --------------------------------------------------
1018  inline auto withOriginalTrajectory(art::InputTag const& inputTag)
1019  {
1020  return proxy::withZeroOrOneAs<recob::TrackTrajectory, Tracks::TrackTrajectoryTag>(inputTag);
1021  }
1022 
1069  {
1070  return proxy::withZeroOrOneAs<recob::TrackTrajectory, Tracks::TrackTrajectoryTag>();
1071  }
1072 
1073  //----------------------------------------------------------------------------
1085  inline auto withFitHitInfo(art::InputTag const& inputTag)
1086  {
1087  return proxy::withParallelDataAs<std::vector<recob::TrackFitHitInfo>,
1088  Tracks::TrackFitHitInfoTag>(inputTag);
1089  }
1090 
1142  inline auto withFitHitInfo()
1143  {
1144  return proxy::withParallelDataAs<std::vector<recob::TrackFitHitInfo>,
1146  }
1147 
1149  // --- END Auxiliary data ----------------------------------------------------
1150 
1151  //----------------------------------------------------------------------------
1153  template <>
1155  : public CollectionProxyMakerTraits<Tracks::TrackDataProduct_t> {
1156  // default traits, plus a collection proxy class with a custom element:
1157  template <typename MainColl, typename... AuxColl>
1158  using collection_proxy_impl_t = CollectionProxyBase<Track, MainColl, AuxColl...>;
1159  };
1160 
1161  //----------------------------------------------------------------------------
1163  template <>
1165 
1168 
1184  template <typename Event, typename... WithArgs>
1185  static auto make(Event const& event, art::InputTag const& tag, WithArgs&&... withArgs)
1186  {
1187  // automatically add associated hits with the same input tag;
1188  // IDEA: allow a withAssociated<recob::Hit>() from withArgs to override
1189  // this one; the pattern may be:
1190  // - if withArgs contains a withAssociated<recob::Hit>(), produce a new
1191  // withArgs with that one pushed first
1192  // - otherwise, produce a new withArgs with a new
1193  // withAssociated<recob::Hit>(tag) as first element
1194  // In principle there is no need for these hits to be first; code might
1195  // be simpler when assuming that though.
1196  return maker_base_t::make(event,
1197  tag,
1198  withAssociatedAs<recob::Hit, Tracks::HitTag>(),
1199  std::forward<WithArgs>(withArgs)...);
1200  } // make()
1201 
1202  }; // struct CollectionProxyMaker<>
1203 
1205  template <typename Data>
1206  auto wrapTrackPoint(Data const& wrappedData)
1207  {
1209  return reinterpret_cast<TrackPointWrapper<Data> const&>(wrappedData);
1210  }
1211 
1214  template <typename TrackProxy>
1215  class TrackPointIterator {
1216 
1217  /*
1218  * So, let's go through the list of iterator traits from cppreference.com:
1219  * [x] Iterator
1220  * [x] CopyConstructible
1221  * [x] CopyAssignable
1222  * [x] Destructible
1223  * [x] lvalues are Swappable
1224  * [x] value_type
1225  * [x] difference_type
1226  * [x] reference
1227  * [x] pointer
1228  * [x] iterator_category
1229  * [x] operator*()
1230  * [x] operator++()
1231  * [ ] InputIterator
1232  * [x] Iterator (above)
1233  * [x] EqualityComparable (operator== (A, B))
1234  * [x] operator!= ()
1235  * [ ] reference operator*() (convertible to value_type)
1236  * [ ] operator->()
1237  * [x] It& operator++()
1238  * [x] operator++(int)
1239  * [x] *i++ equivalent to { auto v = *i; ++i; return v; }
1240  * [ ] Forward Iterator
1241  * [ ] InputIterator (above)
1242  * [x] DefaultConstructible
1243  * [x] multipass guarantee: a == b => ++a == ++b
1244  * [ ] reference = value_type const&
1245  * [x] It operator++(int)
1246  * [ ] *i++ returns reference
1247  * That's it! :-|
1248  */
1249 
1250  using track_proxy_t = TrackProxy;
1251 
1252  track_proxy_t const* track = nullptr;
1253  std::size_t index = std::numeric_limits<std::size_t>::max();
1254 
1255  public:
1258  using difference_type = std::ptrdiff_t;
1260  using pointer = TrackPoint const*;
1261  using reference = TrackPoint; // booo!
1262  // not quite an input iterator (see above)
1263  using iterator_category = std::input_iterator_tag;
1265 
1266  TrackPointIterator() = default;
1267 
1268  TrackPointIterator(track_proxy_t const& track, std::size_t index) : track(&track), index(index)
1269  {}
1270 
1272  {
1273  ++index;
1274  return *this;
1275  }
1276 
1278  {
1279  auto it = *this;
1280  this->operator++();
1281  return it;
1282  }
1283 
1284  // we make sure the return value is a temporary
1286  {
1287  return static_cast<value_type>(makeTrackPointData(*track, index));
1288  }
1289 
1291  {
1292  return (index == other.index) && (track == other.track);
1293  }
1294 
1296  {
1297  return (index != other.index) || (track != other.track);
1298  }
1299 
1300  }; // class TrackPointIterator
1301 
1302 } // namespace proxy
1303 
1304 namespace proxy {
1305 
1306  //----------------------------------------------------------------------------
1307  namespace details {
1308 
1309  //--------------------------------------------------------------------------
1310  template <typename T>
1311  struct isTrackProxy : public std::false_type {};
1312 
1313  template <typename TrackCollProxy>
1314  struct isTrackProxy<Track<TrackCollProxy>> : public std::true_type {};
1315 
1316  //--------------------------------------------------------------------------
1318  template <typename CollProxy>
1319  struct TrackPointIteratorBox {
1322 
1323  TrackPointIteratorBox(track_proxy_t const& track) : track(&track) {}
1324 
1325  const_iterator begin() const { return track->beginPoint(); }
1326 
1327  const_iterator end() const { return track->endPoint(); }
1328 
1329  private:
1330  track_proxy_t const* track = nullptr;
1331 
1332  }; // TrackPointIteratorBox<>
1333 
1334  //--------------------------------------------------------------------------
1335 
1336  } // namespace details
1337 
1338  //----------------------------------------------------------------------------
1339  template <typename CollProxy>
1341  proxy::Tracks::TrackType_t type) const noexcept
1342  {
1343  switch (type) {
1344  case proxy::Tracks::Fitted: return &(track().Trajectory());
1345  case proxy::Tracks::Unfitted: return originalTrajectoryCPtr();
1346  default: return nullptr;
1347  } // switch
1348  } // TrackCollectionProxyElement<>::operator()
1349 
1350  //----------------------------------------------------------------------------
1351  template <typename CollProxy>
1353  std::size_t index) const
1354  {
1355  if constexpr (base_t::template has<Tracks::TrackFitHitInfoTag>()) {
1356  auto const& fitInfo = base_t::template get<Tracks::TrackFitHitInfoTag>();
1357  return &(fitInfo[index]);
1358  }
1359  else
1360  return nullptr;
1361  } // TrackCollectionProxyElement<>::fitInfoAtPoint()
1362 
1363  //----------------------------------------------------------------------------
1364  template <typename CollProxy>
1365  template <typename Pred>
1367  {
1368  return util::filterRangeFor(points(), std::forward<Pred>(pred));
1369  }
1370 
1371  //----------------------------------------------------------------------------
1372  template <typename CollProxy>
1375  {
1376  return selectPoints([mask](auto&& point) { return point.flags().match(mask); });
1377  } // TrackCollectionProxyElement<>::pointsWithFlags()
1378 
1379  //----------------------------------------------------------------------------
1380 
1381 } // namespace proxy
1382 
1383 #endif // LARDATA_RECOBASEPROXY_TRACK_H
TrackPointIterator operator++(int)
Definition: Track.h:1277
TrackProxy track_proxy_t
Definition: Track.h:1250
auto withOriginalTrajectory(art::InputTag const &inputTag)
Adds recob::TrackTrajectory information to the proxy.
Definition: Track.h:1018
std::size_t nPoints() const
Returns the number of trajectory points in the track.
Definition: Track.h:944
Collection of data type definitions for collection proxies.
const_iterator begin() const
Definition: Track.h:1325
auto selectPoints(Pred &&pred) const
Returns an iterable range with only points matching the mask.
Definition: Track.h:1366
auto position() const -> decltype(auto)
Definition: Track.h:597
std::vector< recob::Track > TrackDataProduct_t
Type of the main collection.
Definition: Track.h:484
std::ptrdiff_t difference_type
Definition: Track.h:1258
art::Ptr< recob::TrackTrajectory > const & originalTrajectoryPtr() const
Definition: Track.h:804
art::Ptr< recob::Hit > hitPtr() const
Returns the hit associated with the trajectory point.
Definition: Track.h:632
Class to assemble the required proxy.
std::input_iterator_tag iterator_category
Definition: Track.h:1263
recob::TrackFitHitInfo const * fitInfoAtPoint(std::size_t index) const
Returns fit info for the specified point (nullptr if not available).
Definition: Track.h:1352
std::unique_ptr< InputSource > make(fhicl::ParameterSet const &conf, InputSourceDescription &desc)
auto hitAtPoint(std::size_t index) const -> decltype(auto)
Returns an art pointer to the hit associated with the specified point.
Definition: Track.h:761
Declaration of signal hit object.
Proxy tag for a recob::Track collection proxy.
Definition: Track.h:481
bool isPointValid() const
Returns whether the trajectory point is valid.
Definition: Track.h:624
STL namespace.
Structure for range-for iteration.
Definition: Track.h:261
auto filterRangeFor(Range &&range, Pred &&pred) -> decltype(auto)
Provides iteration only through elements passing a condition.
Base utilities for the implementation of data product facades.
bool operator==(TrackPointIterator const &other) const
Definition: Track.h:1290
Class for track proxy elements.
Definition: Track.h:700
Base representation of a collection of proxied objects.
TrackPointData makeTrackPointData(TrackProxy const &track, std::size_t index)
Returns an object with information about the specified track point.
Definition: Track.h:684
tracking::Vector_t Vector_t
Definition: Track.h:53
Type of track point information.
Definition: Track.h:664
A class containing a set of flags.
Definition: BitMask.h:407
Wrapper for a track data proxy.
Definition: Track.h:255
TrackPointIteratorBox(track_proxy_t const &track)
Definition: Track.h:1323
void hits()
Definition: readHits.C:15
TrackPoint(TrackPointData const &data)
Definition: Track.h:666
TrackPointIterator(track_proxy_t const &track, std::size_t index)
Definition: Track.h:1268
recob::TrackTrajectory const * originalTrajectoryCPtr() const noexcept
Definition: Track.h:969
A trajectory in space reconstructed from hits.
Object storing per-hit information from a track fit.
TrackPoint(TrackPointData &&data)
Definition: Track.h:667
const_iterator end() const
Definition: Track.h:1327
std::add_const_t< TrackPointData > Wrapped_t
Definition: Track.h:566
Class to assemble the required proxy.
TrackPoint operator[](std::size_t index) const
Returns an iterable range with point-by-point information.
Definition: Track.h:956
auto wrapTrackPoint(Data const &wrappedData)
"Converts" point data into a proxy::TrackPointWrapper.
Definition: Track.h:1206
Encloses LArSoft data product proxy objects and utilities.See this doxygen module for an introduction...
Provides recob::Track data product.
Data product for reconstructed trajectory in space.
recob::TrackTrajectory const * operator()(proxy::Tracks::TrackType_t type) const noexcept
Returns the requested trajectory from the proxy.
Definition: Track.h:1340
std::tuple< recob::Track const *, art::Ptr< recob::Hit >, recob::TrackFitHitInfo const *, std::size_t > TrackPointData
Container of track point information.
Definition: Track.h:533
auto index() const -> decltype(auto)
Returns the index of this point in the trajectory.
Definition: Track.h:646
std::size_t nHits() const
Returns the number of hits associated with this track.
Definition: Track.h:764
double value
Definition: spectrum.C:18
auto hits() const -> decltype(auto)
Returns a collection-like range of hits of this track, at point order.
Definition: Track.h:758
point_iterator endPoint() const
Returns the iterator past the last point.
Definition: Track.h:950
value_type operator*() const
Definition: Track.h:1285
An element of a collection proxy.
recob::TrackFitHitInfo const * fitInfoPtr() const
Returns fit info associated with the trajectory point.
Definition: Track.h:643
TrackPoint point(std::size_t index) const
Definition: Track.h:954
recob::Hit const * hit() const
Returns a pointer to the hit on the trajectory point, if any.
Definition: Track.h:649
auto pointsWithFlags(recob::TrackTrajectory::PointFlags_t::Mask_t mask) const
Returns an iterable range with only points matching the mask.
Definition: Track.h:1373
typename track_proxy_t::point_iterator const_iterator
Definition: Track.h:1321
Represents a track trajectory before the final fit.
Definition: Track.h:497
track_proxy_t const * track
Definition: Track.h:1252
tracking::Point_t Point_t
Definition: Track.h:52
TrackType_t
Types of tracks and trajectories.
Definition: Track.h:496
Utilities to manipulate range for loops.
auto withFitHitInfo(art::InputTag const &inputTag)
Adds recob::TrackFitHitInfo information to the proxy.
Definition: Track.h:1085
point_iterator beginPoint() const
Returns the iterator to the data of the first point.
Definition: Track.h:947
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
Definition: Track.h:992
bool hasOriginalTrajectory() const
Returns whether this track is associated to a trajectory.
Definition: Track.h:800
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:46
recob::TrackTrajectory const & originalTrajectory() const
Returns a reference to the associated trajectory.
Definition: Track.h:818
Represents a track trajectory from the final fit.
Definition: Track.h:498
auto momentum() const -> decltype(auto)
Definition: Track.h:604
auto points() const
Returns an iterable range with point-by-point information.
Definition: Track.h:870
QuadExpr operator*(double v, const QuadExpr &e)
Definition: QuadExpr.h:44
Float_t track
Definition: plot.C:35
TrackPoint const * pointer
Definition: Track.h:1260
recob::Track const & track() const
Returns the track this point belongs to.
Definition: Track.h:593
bool operator!=(TrackPointIterator const &other) const
Definition: Track.h:1295
Wrapped_t const & base() const
Definition: Track.h:576
static auto make(Event const &event, art::InputTag const &tag, WithArgs &&...withArgs)
Creates and returns a collection proxy for recob::Track based on proxy::Tracks tag and with the reque...
Definition: Track.h:1185
Set of flags pertaining a point of the track.
TrackPointIterator & operator++()
Definition: Track.h:1271
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49
Event finding and building.
recob::Track const & track() const
Returns the pointed track.
Definition: Track.h:712