sandreco  1.0.8
A modular reconstruction software for SAND
EDEPTrajectory.h
1 #pragma once
2 
3 #include <edep_reader/EDEPHit.h>
4 #include <edep_reader/EDEPTrajectoryPoint.h>
5 
6 #include <array>
7 #include <cstddef>
8 #include <iterator>
9 #include <string>
10 #include <string_view>
11 #include <type_traits>
12 #include <vector>
13 
14 class EDEPTrajectory;
15 
20  template <typename T>
22  using base_type = std::vector<EDEPTrajectory*>::const_iterator;
23 
24  public:
25  using iterator_category = std::random_access_iterator_tag;
26  using value_type = std::remove_const_t<T>;
27  using difference_type = std::ptrdiff_t;
28  using pointer = T*;
29  using reference = T&;
30 
31  indirect_iterator() = default;
32  explicit indirect_iterator(base_type b) : it_(b) {}
33  template <typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
34  indirect_iterator(const indirect_iterator<U>& o) : it_(o.base()) {}
35 
36  base_type base() const { return it_; }
37 
38  reference operator* () const { return **it_; }
39  pointer operator->() const { return *it_; }
40  reference operator[] (difference_type n) const { return **(it_ + n); }
41 
42  indirect_iterator& operator++ () { ++it_; return *this; }
43  indirect_iterator operator++ (int) { auto t = *this; ++it_; return t; }
44  indirect_iterator& operator-- () { --it_; return *this; }
45  indirect_iterator operator-- (int) { auto t = *this; --it_; return t; }
46  indirect_iterator& operator+= (difference_type n) { it_ += n; return *this; }
47  indirect_iterator& operator-= (difference_type n) { it_ -= n; return *this; }
48  friend indirect_iterator operator+ (indirect_iterator i, difference_type n) { return i += n; }
49  friend indirect_iterator operator+ (difference_type n, indirect_iterator i) { return i += n; }
50  friend indirect_iterator operator- (indirect_iterator i, difference_type n) { return i -= n; }
51  friend difference_type operator- (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ - b.it_; }
52  friend bool operator== (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ == b.it_; }
53  friend bool operator!= (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ != b.it_; }
54  friend bool operator< (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ < b.it_; }
55  friend bool operator> (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ > b.it_; }
56  friend bool operator<= (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ <= b.it_; }
57  friend bool operator>= (const indirect_iterator& a, const indirect_iterator& b) { return a.it_ >= b.it_; }
58 
59  private:
60  base_type it_{};
61  };
62 
64  template <typename T>
66  public:
69  using value_type = std::remove_const_t<T>;
70 
71  explicit trajectory_range(const std::vector<EDEPTrajectory*>& v) : v_(&v) {}
72 
73  iterator begin() const { return iterator(v_->begin()); }
74  iterator end() const { return iterator(v_->end()); }
75  std::size_t size() const { return v_->size(); }
76  bool empty() const { return v_->empty(); }
77  T& operator[] (std::size_t i) const { return *(*v_).at(i); }
78  T& at(std::size_t i) const { return *v_->at(i); }
79  T& front() const { return *v_->front(); }
80  T& back() const { return *v_->back(); }
81 
82  private:
83  const std::vector<EDEPTrajectory*>* v_;
84  };
85 
97  public:
98  EDEPTrajectory() = default;
99 
100  explicit EDEPTrajectory(const TG4Trajectory& trajectory)
101  : p0_(trajectory.GetInitialMomentum()),
102  id_(trajectory.GetTrackId()),
103  parent_id_(trajectory.GetParentId()),
104  pdg_code_(trajectory.GetPDGCode()) {
105  UFW_ASSERT(id_ != parent_id_, "Trajectory is its own parent");
106  };
107 
108  EDEPTrajectory(const EDEPTrajectory&) = default;
109  EDEPTrajectory(EDEPTrajectory&&) = default;
110 
114  ~EDEPTrajectory() = default;
115 
116  // Operators
122  bool operator== (const EDEPTrajectory& trj) const;
123 
130 
137 
138  // Getters
143  EDEPTrajectory* Get() { return this; };
144 
149  const EDEPTrajectory* Get() const { return this; };
150 
156 
161  int GetId() const { return id_; };
162 
167  int GetDepth() const { return depth_; };
168 
173  int GetInteractionNumber() const { return interaction_number_; };
174 
179  std::string GetReaction() const { return reaction_; };
180 
185  int GetParentId() const { return parent_id_; };
186 
191  int GetPDGCode() const { return pdg_code_; };
192 
193  std::vector<EDEPTrajectoryPoint>& GetFirstPointsInDetector(sand::subdetector_t c) { return first_points_.at(c); }
194  const std::vector<EDEPTrajectoryPoint>& GetFirstPointsInDetector(sand::subdetector_t c) const {
195  return first_points_.at(c);
196  }
197  std::vector<EDEPTrajectoryPoint>& GetLastPointsInDetector(sand::subdetector_t c) { return last_points_.at(c); }
198  const std::vector<EDEPTrajectoryPoint>& GetLastPointsInDetector(sand::subdetector_t c) const {
199  return last_points_.at(c);
200  }
201 
206  sand::vec_4d GetInitialMomentum() const { return p0_; };
207  trajectory_range<EDEPTrajectory> GetChildrenTrajectories() { return trajectory_range<EDEPTrajectory>(children_); };
209 
214  const EDEPHitsMap& GetHitMap() const { return hit_map_; };
219  const EDEPTrajectoryPoints& GetTrajectoryPoints() const { return trajectory_points_; };
220 
225  std::vector<EDEPTrajectoryPoint> GetTrajectoryPointsVect() const;
226 
227  // Hit queries
228  bool HasHits() const { return !hit_map_.empty(); }
229  bool HasHitWithId(int id) const { return FindHit(id) != nullptr; }
230  const EDEPHit& GetHitWithId(int id) const;
231  bool HasHitInDetector(sand::subdetector_t c) const { return hit_map_.find(c) != hit_map_.end(); }
233  double GetDepositedEnergy(sand::subdetector_t c) const;
235  double GetTotalEnergyDeposit(sand::subdetector_t c) const;
237  double GetTrackLength(sand::subdetector_t c) const;
238  bool HasHitBeforeTime(double t) const;
239  bool HasHitAfterTime(double t) const;
240  bool HasHitWithEnergySmallerThan(double e) const;
241  bool HasHitWithEnergyLargerThan(double e) const;
242  bool HasHitInEnergy(double min, double max) const;
243  bool HasHitInTimeAndEnergy(double start_time, double stop_time, double min_energy, double max_energy) const;
245  bool IsTrajectorySaturated() const;
246 
247  bool HasHitInTime(double start_time, double stop_time) const;
248  bool HasHitWithIdInDetector(int id, sand::subdetector_t c) const;
249 
250  bool HasHitNearPoint(sand::pos_3d point, double distance) const;
251  bool HasHitNear4DPoint(sand::vec_4d point, double distance, double time) const;
253  const EDEPHit* GetHitNear4DPoint(sand::vec_4d point, double distance, double time) const;
254 
255 
256 
257  std::string Print(std::string& full_out, int depth = 100, int current_depth = 0) const;
258 
259  bool IsEntering(sand::subdetector_t c) const { return entering_.at(c); };
260  bool IsExiting(sand::subdetector_t c) const { return exiting_.at(c); };
261 
262  template <typename Funct>
263  bool HasHitWhere(Funct&& f) const {
264  for (const auto& hits : hit_map_) {
265  for (const auto& hit : hits.second) {
266  if (f(hit)) {
267  return true;
268  }
269  }
270  }
271  return false;
272  }
273 
274  template <typename Funct>
275  const EDEPHit* GetHitWhere(Funct&& f) const {
276  for (const auto& hits : hit_map_) {
277  for (const auto& hit : hits.second) {
278  if (f(hit)) {
279  return &hit;
280  }
281  }
282  }
283  return nullptr;
284  }
285 
287  static bool Match(std::string_view volume, std::initializer_list<std::string> names);
288 
289  friend class EDEPTree;
290 
291  protected:
292  const EDEPHit* FindHit(int id) const;
293 
294  static constexpr std::size_t kMaxPoints = 10000;
295 
296  sand::vec_4d p0_{0, 0, 0, 0};
297  EDEPHitsMap hit_map_;
298  EDEPTrajectoryPoints trajectory_points_;
299  std::vector<EDEPTrajectory*> children_;
300  std::array<bool, sand::kNumComponents> exiting_{};
301  std::array<bool, sand::kNumComponents> entering_{};
302  mutable std::array<std::vector<EDEPTrajectoryPoint>, sand::kNumComponents> last_points_;
303  mutable std::array<std::vector<EDEPTrajectoryPoint>, sand::kNumComponents> first_points_;
305  int id_ = -1;
306  int parent_id_ = -99;
307  int pdg_code_ = 0;
308  int depth_ = -1;
310  std::string reaction_;
311 };
Represents a hit in a detector.
Definition: EDEPHit.h:17
Represents a trajectory of a particle through a detector.
Definition: EDEPTrajectory.h:96
bool HasHitAfterTime(double t) const
Checks if the trajectory has hits after a specified time.
Definition: EDEPTrajectory.cpp:98
bool HasHitInEnergy(double min, double max) const
Checks if the trajectory has hits within a specified energy range.
Definition: EDEPTrajectory.cpp:120
const EDEPHit * GetHitNear4DPoint(sand::vec_4d point, double distance, double time) const
First hit near the 4D point, nullptr if none.
Definition: EDEPTrajectory.cpp:185
int pdg_code_
PDG code of the trajectory.
Definition: EDEPTrajectory.h:307
bool operator==(const EDEPTrajectory &trj) const
Equality operator for EDEPTrajectory.
Definition: EDEPTrajectory.cpp:18
double GetDepositedEnergy(sand::subdetector_t c) const
Sum of the secondary deposits in c, 0 if there are no hits there.
Definition: EDEPTrajectory.cpp:224
double GetTotalEnergyDeposit(sand::subdetector_t c) const
Sum of the total energy deposits (EDEPHit::GetEnergyDeposit) of the hits in c [MeV],...
Definition: EDEPTrajectory.cpp:242
int depth_
Depth of the trajectory.
Definition: EDEPTrajectory.h:308
int GetInteractionNumber() const
Get the number of the interaction that generated this trajectory.
Definition: EDEPTrajectory.h:173
std::array< std::vector< EDEPTrajectoryPoint >, sand::kNumComponents > first_points_
Map of all the last points in each sand::subdetector_t.
Definition: EDEPTrajectory.h:303
bool HasHitWithEnergyLargerThan(double e) const
Checks if the trajectory has hits with energy larger than the specified amount.
Definition: EDEPTrajectory.cpp:89
int id_
ID of the trajectory.
Definition: EDEPTrajectory.h:305
EDEPHitsMap hit_map_
Map of hits associated with the trajectory.
Definition: EDEPTrajectory.h:297
~EDEPTrajectory()=default
Destructor for EDEPTrajectory.
bool HasHitWithEnergySmallerThan(double e) const
Checks if the trajectory has hits with energy smaller than the specified amount.
Definition: EDEPTrajectory.cpp:80
EDEPTrajectory & operator=(const EDEPTrajectory &)=default
Assignment operator for EDEPTrajectory.
bool HasHitInTime(double start_time, double stop_time) const
Checks if the trajectory has hits within a specified time range.
Definition: EDEPTrajectory.cpp:108
bool HasHitNearPoint(sand::pos_3d point, double distance) const
Checks if any hit is within a given distance from a point.
Definition: EDEPTrajectory.cpp:151
const EDEPHit & GetHitWithId(int id) const
Returns the hit in the trajectory with the specified ID.
Definition: EDEPTrajectory.cpp:197
std::array< bool, sand::kNumComponents > exiting_
Map indicating whether the trajectory is exiting a sand::subdetector_t.
Definition: EDEPTrajectory.h:300
EDEPTrajectoryPoints trajectory_points_
Trajectory points.
Definition: EDEPTrajectory.h:298
bool HasHitNear4DPoint(sand::vec_4d point, double distance, double time) const
Checks if any hit is within a given 4D distance (space and time) from a point.
Definition: EDEPTrajectory.cpp:169
bool IsTrajectorySaturated() const
True if the trajectory reached the edep-sim limit on the number of stored points.
Definition: EDEPTrajectory.cpp:270
int parent_id_
Parent ID of the trajectory.
Definition: EDEPTrajectory.h:306
int GetPDGCode() const
Get the PDG code of this trajectory.
Definition: EDEPTrajectory.h:191
std::vector< EDEPTrajectory * > children_
Children trajectories.
Definition: EDEPTrajectory.h:299
bool HasHitWithIdInDetector(int id, sand::subdetector_t c) const
Checks if the trajectory has a hit with the specified ID in the specified detector sand::subdetector_...
Definition: EDEPTrajectory.cpp:211
int GetId() const
Get the ID of this trajectory.
Definition: EDEPTrajectory.h:161
EDEPTrajectory * GetParent() const
Get the parent trajectory of this trajectory.
Definition: EDEPTrajectory.h:155
std::vector< EDEPTrajectoryPoint > GetTrajectoryPointsVect() const
Get the trajectory points associated with this trajectory.
Definition: EDEPTrajectory.cpp:293
int GetParentId() const
Get the parent ID of this trajectory.
Definition: EDEPTrajectory.h:185
EDEPTrajectory * parent_trajectory_
Pointer to the parent trajectory.
Definition: EDEPTrajectory.h:304
bool HasHitBeforeTime(double t) const
Checks if the trajectory has hits before a specified time.
Definition: EDEPTrajectory.cpp:71
int interaction_number_
Number of the interaction that generated this trajectory.
Definition: EDEPTrajectory.h:309
int GetDepth() const
Get the depth of this trajectory.
Definition: EDEPTrajectory.h:167
std::array< std::vector< EDEPTrajectoryPoint >, sand::kNumComponents > last_points_
Map of all the first points in each sand::subdetector_t.
Definition: EDEPTrajectory.h:302
static bool Match(std::string_view volume, std::initializer_list< std::string > names)
True if volume contains any of names as a substring.
Definition: EDEPTrajectory.cpp:284
const EDEPTrajectoryPoints & GetTrajectoryPoints() const
Get the trajectory points associated with this trajectory.
Definition: EDEPTrajectory.h:219
std::string GetReaction() const
Get the reaction of the interaction that generated this trajectory.
Definition: EDEPTrajectory.h:179
std::array< bool, sand::kNumComponents > entering_
Map indicating whether the trajectory is entering a sand::subdetector_t.
Definition: EDEPTrajectory.h:301
std::string reaction_
String corresponding to the reaction that generated this trajectory.
Definition: EDEPTrajectory.h:310
const EDEPHitsMap & GetHitMap() const
Get the hit map associated with this trajectory.
Definition: EDEPTrajectory.h:214
sand::vec_4d p0_
Initial momentum of the trajectory.
Definition: EDEPTrajectory.h:296
EDEPTrajectory * Get()
Get a pointer to this trajectory.
Definition: EDEPTrajectory.h:143
double GetTrackLength(sand::subdetector_t c) const
Sum of the track lengths (EDEPHit::GetTrackLength) of the hits in c [mm], 0 if there are none.
Definition: EDEPTrajectory.cpp:257
const EDEPTrajectory * Get() const
Get a const pointer to this trajectory.
Definition: EDEPTrajectory.h:149
sand::vec_4d GetInitialMomentum() const
Get the initial momentum of this trajectory.
Definition: EDEPTrajectory.h:206
bool HasHitInTimeAndEnergy(double start_time, double stop_time, double min_energy, double max_energy) const
Checks if the trajectory has hits within a specified energy and time ranges.
Definition: EDEPTrajectory.cpp:134
Tree of the trajectories of an edep-sim event. The tree itself is the (id -1) root,...
Definition: EDEPTree.h:24
Definition: EDEPTrajectory.h:21
Non-owning view over a vector of trajectory pointers, usable as a range of trajectory references.
Definition: EDEPTrajectory.h:65