sandreco  1.0.8
A modular reconstruction software for SAND
EDEPTree.h
1 #pragma once
2 
3 #include <edep_reader/EDEPTrajectory.h>
4 
5 #include <unordered_map>
6 #include <vector>
7 
8 class TGeoNode;
9 class TGeoNavigator;
10 
24 class EDEPTree : public EDEPTrajectory {
25 public:
28 
29  EDEPTree() = default;
30  // Trajectories point to the root and into the internal storage, so the tree cannot be copied or moved.
31  EDEPTree(const EDEPTree&) = delete;
32  EDEPTree(EDEPTree&&) = delete;
33  EDEPTree& operator= (const EDEPTree&) = delete;
34  EDEPTree& operator= (EDEPTree&&) = delete;
35 
36  iterator begin() { return iterator(order_.begin()); }
37  iterator end() { return iterator(order_.end()); }
38  const_iterator begin() const { return const_iterator(order_.begin()); }
39  const_iterator end() const { return const_iterator(order_.end()); }
40  const_iterator cbegin() const { return begin(); }
41  const_iterator cend() const { return end(); }
43  std::size_t size() const { return order_.size(); }
44 
46  void InizializeFromEdep(const TG4Event& edep_event);
47 
48  bool HasTrajectory(int trj_id) const { return id_to_pos_.find(trj_id) != id_to_pos_.end(); }
49  iterator GetTrajectory(int trj_id) { return at_pos(find_pos(trj_id)); }
50  const_iterator GetTrajectory(int trj_id) const { return at_pos(find_pos(trj_id)); }
52  iterator GetParentOf(int trj_id);
53  const_iterator GetParentOf(int trj_id) const;
54 
56  iterator GetTrajectoryWithHitId(int id) { return at_pos(hit_pos(id)); }
57  const_iterator GetTrajectoryWithHitId(int id) const { return at_pos(hit_pos(id)); }
58  iterator GetTrajectoryWithHitIdInDetector(int id, sand::subdetector_t c);
59  const_iterator GetTrajectoryWithHitIdInDetector(int id, sand::subdetector_t c) const;
61  const EDEPHit* GetHit(int id) const {
62  return (id >= 0 && static_cast<std::size_t>(id) < hit_ptr_.size()) ? hit_ptr_.at(id) : nullptr;
63  }
64 
66  template <typename OutputIterator, typename F>
67  OutputIterator Filter(OutputIterator out_it, F&& funct) const {
68  for (const auto& trj : *this) {
69  if (funct(trj)) {
70  *out_it = trj;
71  ++out_it;
72  }
73  }
74  return out_it;
75  }
76 
77  private:
79  unsigned classify(const TGeoNode* node);
80  void fill_points(EDEPTrajectory& trj, const TG4Trajectory& g4trj, TGeoNavigator& nav);
81  void check_in_next(EDEPTrajectory& trj, unsigned in, unsigned next, const TG4TrajectoryPoint& pt,
82  const TG4TrajectoryPoint& next_pt) const;
83 
84  int find_pos(int trj_id) const {
85  auto it = id_to_pos_.find(trj_id);
86  return it == id_to_pos_.end() ? -1 : it->second;
87  }
88  int hit_pos(int hit_id) const {
89  return (hit_id >= 0 && static_cast<std::size_t>(hit_id) < hit_to_pos_.size()) ? hit_to_pos_.at(hit_id) : -1;
90  }
91  iterator at_pos(int pos) { return pos < 0 ? end() : begin() + pos; }
92  const_iterator at_pos(int pos) const { return pos < 0 ? end() : begin() + pos; }
93 
94  std::vector<EDEPTrajectory> nodes_;
95  std::vector<EDEPTrajectory*> order_;
96  std::unordered_map<int, int> id_to_pos_;
97  std::vector<int> hit_to_pos_;
98  std::vector<const EDEPHit*> hit_ptr_;
99  std::unordered_map<const TGeoNode*, unsigned> volume_cache_;
100  std::vector<unsigned> point_volumes_;
101 };
Represents a hit in a detector.
Definition: EDEPHit.h:17
Represents a trajectory of a particle through a detector.
Definition: EDEPTrajectory.h:96
Tree of the trajectories of an edep-sim event. The tree itself is the (id -1) root,...
Definition: EDEPTree.h:24
std::size_t size() const
Total number of trajectories in the tree.
Definition: EDEPTree.h:43
iterator GetTrajectoryWithHitId(int id)
Iterator to the trajectory that produced hit id, end() if there is none.
Definition: EDEPTree.h:56
OutputIterator Filter(OutputIterator out_it, F &&funct) const
Copies the trajectories satisfying funct to out_it.
Definition: EDEPTree.h:67
iterator GetParentOf(int trj_id)
Iterator to the parent of trj_id, end() if the trajectory is unknown or a primary.
Definition: EDEPTree.cpp:266
void InizializeFromEdep(const TG4Event &edep_event)
Rebuilds the tree from edep_event, replacing the previous content.
Definition: EDEPTree.cpp:143
const EDEPHit * GetHit(int id) const
The hit with global index id, nullptr if it does not belong to any trajectory.
Definition: EDEPTree.h:61
Definition: EDEPTrajectory.h:21