All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbndcode/sbndcode/RecoUtils/RecoUtils.cc
Go to the documentation of this file.
1 #include "RecoUtils.h"
2 
3 
5  const art::Ptr<recob::Hit> hit, bool rollup_unsaved_ids) {
6  std::map<int,double> id_to_energy_map;
7  art::ServiceHandle<cheat::BackTrackerService> bt_serv;
8  std::vector<sim::TrackIDE> track_ides = bt_serv->HitToTrackIDEs(clockData, hit);
9  for (unsigned int idIt = 0; idIt < track_ides.size(); ++idIt) {
10  int id = track_ides.at(idIt).trackID;
11  if (rollup_unsaved_ids) id = std::abs(id);
12  double energy = track_ides.at(idIt).energy;
13  id_to_energy_map[id]+=energy;
14  }
15  //Now loop over the map to find the maximum contributor
16  double likely_particle_contrib_energy = -99999;
17  int likely_track_id = 0;
18  for (std::map<int,double>::iterator mapIt = id_to_energy_map.begin(); mapIt != id_to_energy_map.end(); mapIt++){
19  double particle_contrib_energy = mapIt->second;
20  if (particle_contrib_energy > likely_particle_contrib_energy){
21  likely_particle_contrib_energy = particle_contrib_energy;
22  likely_track_id = mapIt->first;
23  }
24  }
25  return likely_track_id;
26 }
27 
28 
29 
30 int RecoUtils::TrueParticleIDFromTotalTrueEnergy(detinfo::DetectorClocksData const& clockData, const std::vector<art::Ptr<recob::Hit> >& hits, bool rollup_unsaved_ids) {
31  art::ServiceHandle<cheat::BackTrackerService> bt_serv;
32  std::map<int,double> trackIDToEDepMap;
33  for (std::vector<art::Ptr<recob::Hit> >::const_iterator hitIt = hits.begin(); hitIt != hits.end(); ++hitIt) {
34  art::Ptr<recob::Hit> hit = *hitIt;
35  std::vector<sim::TrackIDE> trackIDs = bt_serv->HitToTrackIDEs(clockData, hit);
36  for (unsigned int idIt = 0; idIt < trackIDs.size(); ++idIt) {
37  int id = trackIDs[idIt].trackID;
38  if (rollup_unsaved_ids) id = std::abs(id);
39  trackIDToEDepMap[id] += trackIDs[idIt].energy;
40  }
41  }
42 
43  //Loop over the map and find the track which contributes the highest energy to the hit vector
44  double maxenergy = -1;
45  int objectTrack = -99999;
46  for (std::map<int,double>::iterator mapIt = trackIDToEDepMap.begin(); mapIt != trackIDToEDepMap.end(); mapIt++){
47  double energy = mapIt->second;
48  double trackid = mapIt->first;
49  if (energy > maxenergy){
50  maxenergy = energy;
51  objectTrack = trackid;
52  }
53  }
54 
55  return objectTrack;
56 }
57 
58 
59 
60 int RecoUtils::TrueParticleIDFromTotalRecoCharge(detinfo::DetectorClocksData const& clockData, const std::vector<art::Ptr<recob::Hit> >& hits, bool rollup_unsaved_ids) {
61  // Make a map of the tracks which are associated with this object and the charge each contributes
62  std::map<int,double> trackMap;
63  for (std::vector<art::Ptr<recob::Hit> >::const_iterator hitIt = hits.begin(); hitIt != hits.end(); ++hitIt) {
64  art::Ptr<recob::Hit> hit = *hitIt;
65  int trackID = TrueParticleID(clockData, hit, rollup_unsaved_ids);
66  trackMap[trackID] += hit->Integral();
67  }
68 
69  // Pick the track with the highest charge as the 'true track'
70  double highestCharge = 0;
71  int objectTrack = -99999;
72  for (std::map<int,double>::iterator trackIt = trackMap.begin(); trackIt != trackMap.end(); ++trackIt) {
73  if (trackIt->second > highestCharge) {
74  highestCharge = trackIt->second;
75  objectTrack = trackIt->first;
76  }
77  }
78  return objectTrack;
79 }
80 
81 
82 
83 int RecoUtils::TrueParticleIDFromTotalRecoHits(detinfo::DetectorClocksData const& clockData,const std::vector<art::Ptr<recob::Hit> >& hits, bool rollup_unsaved_ids) {
84  // Make a map of the tracks which are associated with this object and the number of hits they are the primary contributor to
85  std::map<int,int> trackMap;
86  for (std::vector<art::Ptr<recob::Hit> >::const_iterator hitIt = hits.begin(); hitIt != hits.end(); ++hitIt) {
87  art::Ptr<recob::Hit> hit = *hitIt;
88  int trackID = TrueParticleID(clockData, hit, rollup_unsaved_ids);
89  trackMap[trackID]++;
90  }
91 
92  // Pick the track which is the primary contributor to the most hits as the 'true track'
93  int objectTrack = -99999;
94  int highestCount = -1;
95  int NHighestCounts = 0;
96  for (std::map<int,int>::iterator trackIt = trackMap.begin(); trackIt != trackMap.end(); ++trackIt) {
97  if (trackIt->second > highestCount) {
98  highestCount = trackIt->second;
99  objectTrack = trackIt->first;
100  NHighestCounts = 1;
101  }
102  else if (trackIt->second == highestCount){
103  NHighestCounts++;
104  }
105  }
106  if (NHighestCounts > 1){
107  std::cout<<"RecoUtils::TrueParticleIDFromTotalRecoHits - There are " << NHighestCounts << " particles which tie for highest number of contributing hits (" << highestCount<<" hits). Using RecoUtils::TrueParticleIDFromTotalTrueEnergy instead."<<std::endl;
108  objectTrack = RecoUtils::TrueParticleIDFromTotalTrueEnergy(clockData, hits,rollup_unsaved_ids);
109  }
110  return objectTrack;
111 }
112 
113 
114 
115 bool RecoUtils::IsInsideTPC(TVector3 position, double distance_buffer){
116  double vtx[3] = {position.X(), position.Y(), position.Z()};
117  bool inside = false;
118  art::ServiceHandle<geo::Geometry> geom;
119  geo::TPCID idtpc = geom->FindTPCAtPosition(vtx);
120 
121  if (geom->HasTPC(idtpc))
122  {
123  const geo::TPCGeo& tpcgeo = geom->GetElement(idtpc);
124  double minx = tpcgeo.MinX(); double maxx = tpcgeo.MaxX();
125  double miny = tpcgeo.MinY(); double maxy = tpcgeo.MaxY();
126  double minz = tpcgeo.MinZ(); double maxz = tpcgeo.MaxZ();
127 
128  for (size_t c = 0; c < geom->Ncryostats(); c++)
129  {
130  const geo::CryostatGeo& cryostat = geom->Cryostat(c);
131  for (size_t t = 0; t < cryostat.NTPC(); t++)
132  {
133  const geo::TPCGeo& tpcg = cryostat.TPC(t);
134  if (tpcg.MinX() < minx) minx = tpcg.MinX();
135  if (tpcg.MaxX() > maxx) maxx = tpcg.MaxX();
136  if (tpcg.MinY() < miny) miny = tpcg.MinY();
137  if (tpcg.MaxY() > maxy) maxy = tpcg.MaxY();
138  if (tpcg.MinZ() < minz) minz = tpcg.MinZ();
139  if (tpcg.MaxZ() > maxz) maxz = tpcg.MaxZ();
140  }
141  }
142 
143  //x
144  double dista = fabs(minx - position.X());
145  double distb = fabs(position.X() - maxx);
146  if ((position.X() > minx) && (position.X() < maxx) &&
147  (dista > distance_buffer) && (distb > distance_buffer)) inside = true;
148  //y
149  dista = fabs(maxy - position.Y());
150  distb = fabs(position.Y() - miny);
151  if (inside && (position.Y() > miny) && (position.Y() < maxy) &&
152  (dista > distance_buffer) && (distb > distance_buffer)) inside = true;
153  else inside = false;
154  //z
155  dista = fabs(maxz - position.Z());
156  distb = fabs(position.Z() - minz);
157  if (inside && (position.Z() > minz) && (position.Z() < maxz) &&
158  (dista > distance_buffer) && (distb > distance_buffer)) inside = true;
159  else inside = false;
160  }
161 
162  return inside;
163 }
164 
165 double RecoUtils::CalculateTrackLength(const art::Ptr<recob::Track> track){
166  double length = 0;
167  if (track->NumberTrajectoryPoints()==1) return length; //Nothing to calculate if there is only one point
168 
169  for (size_t i_tp = 0; i_tp < track->NumberTrajectoryPoints()-1; i_tp++){ //Loop from the first to 2nd to last point
170  TVector3 this_point(track->TrajectoryPoint(i_tp).position.X(),track->TrajectoryPoint(i_tp).position.Y(),track->TrajectoryPoint(i_tp).position.Z());
171  if (!RecoUtils::IsInsideTPC(this_point,0)){
172  std::cout<<"RecoUtils::CalculateTrackLength - Current trajectory point not in the TPC volume. Skip over this point in the track length calculation"<<std::endl;
173  continue;
174  }
175  TVector3 next_point(track->TrajectoryPoint(i_tp+1).position.X(),track->TrajectoryPoint(i_tp+1).position.Y(),track->TrajectoryPoint(i_tp+1).position.Z());
176  if (!RecoUtils::IsInsideTPC(next_point,0)){
177  std::cout<<"RecoUtils::CalculateTrackLength - Next trajectory point not in the TPC volume. Skip over this point in the track length calculation"<<std::endl;
178  continue;
179  }
180 
181  length+=(next_point-this_point).Mag();
182  }
183  return length;
184 }
int TrueParticleIDFromTotalTrueEnergy(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
double CalculateTrackLength(const art::Ptr< recob::Track > track)
double MinX() const
Returns the world x coordinate of the start of the box.
Definition: BoxBoundedGeo.h:88
Geometry information for a single TPC.
Definition: TPCGeo.h:38
double MaxX() const
Returns the world x coordinate of the end of the box.
Definition: BoxBoundedGeo.h:91
process_name use argoneut_mc_hitfinder track
process_name hit
Definition: cheaterreco.fcl:51
Geometry information for a single cryostat.
Definition: CryostatGeo.h:43
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
T abs(T value)
double MinZ() const
Returns the world z coordinate of the start of the box.
unsigned int NTPC() const
Number of TPCs in this cryostat.
Definition: CryostatGeo.h:181
int TrueParticleIDFromTotalRecoHits(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
const PlaneGeo & GetElement(PlaneID const &planeid) const
Definition: TPCGeo.h:214
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
double MaxY() const
Returns the world y coordinate of the end of the box.
Contains all timing reference information for the detector.
const TPCGeo & TPC(unsigned int itpc) const
Return the itpc&#39;th TPC in the cryostat.
Definition: CryostatGeo.cxx:93
double MaxZ() const
Returns the world z coordinate of the end of the box.
int TrueParticleIDFromTotalRecoCharge(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
double MinY() const
Returns the world y coordinate of the start of the box.
BEGIN_PROLOG could also be cout
int TrueParticleID(detinfo::DetectorClocksData const &clockData, const art::Ptr< recob::Hit > hit, bool rollup_unsaved_ids=1)
bool IsInsideTPC(TVector3 position, double distance_buffer)