Cone.h
Go to the documentation of this file.
1 #ifndef CONE_H
2 #define CONE_H
3 
4 #include <iostream>
5 #include <vector>
6 #include <string>
7 
8 // art and larsoft includes
11 #include "canvas/Persistency/Common/FindManyP.h"
13 
14 namespace detinfo {
15  class DetectorClocksData;
16  class DetectorPropertiesData;
17 }
18 
19 namespace pizero {
20 
21 // Class to store information of hits that fall within it.
22 class Cone {
23  private:
24  const art::Event* m_evt;
27  TVector3 m_start;
28  TVector3 m_direction;
29  double m_length;
30  double m_ang = 15.0/360 * 2*3.1415926; // Opening angle of cone in radians.
31  double m_width = 1; // Has to be overwritten.
32  double m_startoffset = 10;
33 
34  std::vector<const recob::Hit*> m_hits;
35  std::vector<const recob::SpacePoint*> m_spacepoints;
36 
37  public:
39  detinfo::DetectorClocksData const& clockData,
40  detinfo::DetectorPropertiesData const& detProp,
41  const TVector3& newStart,
42  const TVector3& newDir,
43  const double newLen):
44  m_evt(&evt),
45  m_clockdata(&clockData),
46  m_detprop(&detProp),
47  m_start(newStart), m_direction(newDir.Unit()), m_length(newLen)
48  {
49  // Move the start back by a constant to catch all hits.
50  m_start = m_start - m_direction.Unit()*m_startoffset;
51  m_length += m_startoffset;
52  // Determine the width of the cone at the end.
53  m_width = m_length * tan(m_ang);
54 
55  // Get all spacepoints in the event.
56  std::string spsLabel = "pandora";
57  auto spHandle = m_evt->getHandle<std::vector<recob::SpacePoint> >(spsLabel);
58  if (!spHandle) { return; }
59  // Get associations between the spacepoints and hits.
60  const art::FindManyP<recob::Hit> sphits(spHandle, *m_evt, spsLabel);
61 
62  // Loop through spacepoints to test whether they're in the cone.
63  for(const recob::SpacePoint& sp : *spHandle) {
64  const TVector3 pos(sp.XYZ());
65  // 0 <= projection <= cone length
66  const double proj = (pos-m_start).Dot(m_direction);
67  if(proj < 0 || proj > m_length) continue;
68  // Cone radius at projection.
69  const double cradius = (proj/m_length)*m_width/2;
70  // rejection < cone radius
71  const double rej = ((pos-m_start) - proj*m_direction).Mag();
72  if(rej > cradius) continue;
73 
74  // This spacepoint is inside the cone. Put it among the results.
75  for(const art::Ptr<recob::Hit>& hit : sphits.at(sp.ID())) {
76  m_hits.push_back(&*hit);
77  }
78  m_spacepoints.push_back(&sp);
79  }
80  }
81 
82  // Member getters
83  TVector3 start() const { return m_start; }
84  TVector3 end() const { return m_start + m_direction*m_length; }
85  TVector3 direction() const { return m_direction; }
86  double length() const { return m_length; }
87  double width() const { return m_width; }
88  std::vector<const recob::Hit*> hits() const { return m_hits; }
89  std::vector<const recob::SpacePoint*> spacepoints() const { return m_spacepoints; }
90 
91  double energy(calo::CalorimetryAlg caloAlg) const {
93  return shUtil.EstimateEnergyFromHitCharge(*m_clockdata, *m_detprop, m_hits, caloAlg)[2];
94  }
95  double completeness(const simb::MCParticle& mcpart,
96  const std::string hitLabel = "hitpdune") const;
97  double purity(const simb::MCParticle& mcpart) const;
98 };
99 
100 inline double Cone::completeness(const simb::MCParticle& mcpart,
101  const std::string hitLabel) const {
102  // Get all hits in the event.
103  auto hitHandle = m_evt->getHandle<std::vector<recob::Hit> >(hitLabel);
104  if(!hitHandle) {
105  std::cout << "pizero::Cone::completeness: could not find hits in event.\n";
106  return 0;
107  }
108 
109  // Check for each hit whether it came from the MCParticle and if so,
110  // whether it fell inside the cone.
112  double sharedHits = 0;
113  double MChits = 0;
114  for(const recob::Hit& hit : *hitHandle) {
115  for(const int trackId : bt_serv->HitToTrackIds(*m_clockdata, hit)) {
116  if(std::abs(trackId) == std::abs(mcpart.TrackId())) {
117  // Hit is in MCParticle.
118  ++MChits;
119  // Check if hit in cone.
120  for(const recob::Hit* chit : m_hits) {
121  // Compare the time and place of hit for lack of ID
122  if(chit->PeakTime() - hit.PeakTime() < 1e-5 &&
123  chit->Channel() == hit.Channel()) {
124  ++sharedHits;
125  // std::cout << "Found shared hit!\n";
126  break;
127  }
128  }
129  break;
130  } // if hit is from MCP
131  } // for trackID in hit
132  } // for hit in event
133 
134  return MChits==0? 0: sharedHits/MChits;
135 } // Cone::completeness
136 
137 inline double Cone::purity(const simb::MCParticle& mcpart) const {
138  unsigned sharedHits = 0;
140  for(const recob::Hit* hit : m_hits) {
141  for(const int trackId : bt_serv->HitToTrackIds(*m_clockdata, *hit)) {
142  if(std::abs(trackId) == std::abs(mcpart.TrackId())) {
143  ++sharedHits;
144  break;
145  }
146  }
147  }
148 
149  return (double)sharedHits / m_hits.size();
150 }
151 
152 
153 } // namespace pizero
154 
155 #endif // CONE_H
detinfo::DetectorPropertiesData const * m_detprop
Definition: Cone.h:26
double width() const
Definition: Cone.h:87
std::vector< double > EstimateEnergyFromHitCharge(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const std::vector< const recob::Hit * > &hits, calo::CalorimetryAlg caloAlg)
TVector3 m_start
Definition: Cone.h:27
TVector3 direction() const
Definition: Cone.h:85
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
std::vector< const recob::Hit * > hits() const
Definition: Cone.h:88
double m_length
Definition: Cone.h:29
std::vector< int > HitToTrackIds(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
int TrackId() const
Definition: MCParticle.h:210
T abs(T value)
const double e
TVector3 m_direction
Definition: Cone.h:28
Definition: Cone.h:19
const art::Event * m_evt
Definition: Cone.h:24
General LArSoft Utilities.
Detector simulation of raw signals on wires.
TVector3 start() const
Definition: Cone.h:83
std::vector< const recob::Hit * > m_hits
Definition: Cone.h:34
double energy(calo::CalorimetryAlg caloAlg) const
Definition: Cone.h:91
TVector3 end() const
Definition: Cone.h:84
Contains all timing reference information for the detector.
Cone(const art::Event &evt, detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const TVector3 &newStart, const TVector3 &newDir, const double newLen)
Definition: Cone.h:38
double length() const
Definition: Cone.h:86
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
std::vector< const recob::SpacePoint * > m_spacepoints
Definition: Cone.h:35
TCEvent evt
Definition: DataStructs.cxx:7
std::vector< const recob::SpacePoint * > spacepoints() const
Definition: Cone.h:89
detinfo::DetectorClocksData const * m_clockdata
Definition: Cone.h:25