All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbnana/sbnanalysis/ana/SBNOscReco/RecoUtils/RecoUtils.cc
Go to the documentation of this file.
1 #include "RecoUtils.h"
2 
3 int SBNRecoUtils::TrueParticleID(const core::ProviderManager &manager, const art::Ptr<recob::Hit> hit, bool rollup_unsaved_ids) {
4  std::map<int,double> id_to_energy_map;
5  const cheat::BackTracker *bt_serv = manager.GetBackTrackerProvider();
6  std::vector<sim::TrackIDE> track_ides = bt_serv->HitToTrackIDEs(hit);
7  for (unsigned int idIt = 0; idIt < track_ides.size(); ++idIt) {
8  int id = track_ides.at(idIt).trackID;
9  if (rollup_unsaved_ids) id = std::abs(id);
10  double energy = track_ides.at(idIt).energy;
11  id_to_energy_map[id]+=energy;
12  }
13  //Now loop over the map to find the maximum contributor
14  double likely_particle_contrib_energy = -99999;
15  int likely_track_id = 0;
16  for (std::map<int,double>::iterator mapIt = id_to_energy_map.begin(); mapIt != id_to_energy_map.end(); mapIt++){
17  double particle_contrib_energy = mapIt->second;
18  if (particle_contrib_energy > likely_particle_contrib_energy){
19  likely_particle_contrib_energy = particle_contrib_energy;
20  likely_track_id = mapIt->first;
21  }
22  }
23  return likely_track_id;
24 }
25 
26 
27 
28 int SBNRecoUtils::TrueParticleIDFromTotalTrueEnergy(const core::ProviderManager &manager, const std::vector<art::Ptr<recob::Hit> >& hits, bool rollup_unsaved_ids) {
29  const cheat::BackTracker *bt_serv = manager.GetBackTrackerProvider();
30  std::map<int,double> trackIDToEDepMap;
31  for (std::vector<art::Ptr<recob::Hit> >::const_iterator hitIt = hits.begin(); hitIt != hits.end(); ++hitIt) {
32  art::Ptr<recob::Hit> hit = *hitIt;
33  std::vector<sim::TrackIDE> trackIDs = bt_serv->HitToTrackIDEs(hit);
34  for (unsigned int idIt = 0; idIt < trackIDs.size(); ++idIt) {
35  int id = trackIDs[idIt].trackID;
36  if (rollup_unsaved_ids) id = std::abs(id);
37  trackIDToEDepMap[id] += trackIDs[idIt].energy;
38  }
39  }
40 
41  //Loop over the map and find the track which contributes the highest energy to the hit vector
42  double maxenergy = -1;
43  int objectTrack = -99999;
44  for (std::map<int,double>::iterator mapIt = trackIDToEDepMap.begin(); mapIt != trackIDToEDepMap.end(); mapIt++){
45  double energy = mapIt->second;
46  double trackid = mapIt->first;
47  if (energy > maxenergy){
48  maxenergy = energy;
49  objectTrack = trackid;
50  }
51  }
52 
53  return objectTrack;
54 }
55 
56 
57 
58 int SBNRecoUtils::TrueParticleIDFromTotalRecoCharge(const core::ProviderManager &manager, const std::vector<art::Ptr<recob::Hit> >& hits, bool rollup_unsaved_ids) {
59  // Make a map of the tracks which are associated with this object and the charge each contributes
60  std::map<int,double> trackMap;
61  for (std::vector<art::Ptr<recob::Hit> >::const_iterator hitIt = hits.begin(); hitIt != hits.end(); ++hitIt) {
62  art::Ptr<recob::Hit> hit = *hitIt;
63  int trackID = TrueParticleID(manager, hit, rollup_unsaved_ids);
64  trackMap[trackID] += hit->Integral();
65  }
66 
67  // Pick the track with the highest charge as the 'true track'
68  double highestCharge = 0;
69  int objectTrack = -99999;
70  for (std::map<int,double>::iterator trackIt = trackMap.begin(); trackIt != trackMap.end(); ++trackIt) {
71  if (trackIt->second > highestCharge) {
72  highestCharge = trackIt->second;
73  objectTrack = trackIt->first;
74  }
75  }
76  return objectTrack;
77 }
78 
79 
80 
81 int SBNRecoUtils::TrueParticleIDFromTotalRecoHits(const core::ProviderManager &manager, const std::vector<art::Ptr<recob::Hit> >& hits, bool rollup_unsaved_ids) {
82  // Make a map of the tracks which are associated with this object and the number of hits they are the primary contributor to
83  std::map<int,int> trackMap;
84  for (std::vector<art::Ptr<recob::Hit> >::const_iterator hitIt = hits.begin(); hitIt != hits.end(); ++hitIt) {
85  art::Ptr<recob::Hit> hit = *hitIt;
86  int trackID = TrueParticleID(manager, hit, rollup_unsaved_ids);
87  trackMap[trackID]++;
88  }
89 
90  // Pick the track which is the primary contributor to the most hits as the 'true track'
91  int objectTrack = -99999;
92  int highestCount = -1;
93  int NHighestCounts = 0;
94  for (std::map<int,int>::iterator trackIt = trackMap.begin(); trackIt != trackMap.end(); ++trackIt) {
95  if (trackIt->second > highestCount) {
96  highestCount = trackIt->second;
97  objectTrack = trackIt->first;
98  NHighestCounts = 1;
99  }
100  else if (trackIt->second == highestCount){
101  NHighestCounts++;
102  }
103  }
104  if (NHighestCounts > 1){
105  std::cout<<"SBNRecoUtils::TrueParticleIDFromTotalRecoHits - There are " << NHighestCounts << " particles which tie for highest number of contributing hits (" << highestCount<<" hits). Using SBNRecoUtils::TrueParticleIDFromTotalTrueEnergy instead."<<std::endl;
106  objectTrack = SBNRecoUtils::TrueParticleIDFromTotalTrueEnergy(manager, hits,rollup_unsaved_ids);
107  }
108  return objectTrack;
109 }
110 
111 
112 
113 bool SBNRecoUtils::IsInsideTPC(const core::ProviderManager &manager, TVector3 position, double distance_buffer){
114  double vtx[3] = {position.X(), position.Y(), position.Z()};
115  bool inside = false;
116  const geo::GeometryCore* geom = manager.GetGeometryProvider();
117  geo::TPCID idtpc = geom->FindTPCAtPosition(vtx);
118 
119  if (geom->HasTPC(idtpc))
120  {
121  const geo::TPCGeo& tpcgeo = geom->GetElement(idtpc);
122  double minx = tpcgeo.MinX(); double maxx = tpcgeo.MaxX();
123  double miny = tpcgeo.MinY(); double maxy = tpcgeo.MaxY();
124  double minz = tpcgeo.MinZ(); double maxz = tpcgeo.MaxZ();
125 
126  for (size_t c = 0; c < geom->Ncryostats(); c++)
127  {
128  const geo::CryostatGeo& cryostat = geom->Cryostat(c);
129  for (size_t t = 0; t < cryostat.NTPC(); t++)
130  {
131  const geo::TPCGeo& tpcg = cryostat.TPC(t);
132  if (tpcg.MinX() < minx) minx = tpcg.MinX();
133  if (tpcg.MaxX() > maxx) maxx = tpcg.MaxX();
134  if (tpcg.MinY() < miny) miny = tpcg.MinY();
135  if (tpcg.MaxY() > maxy) maxy = tpcg.MaxY();
136  if (tpcg.MinZ() < minz) minz = tpcg.MinZ();
137  if (tpcg.MaxZ() > maxz) maxz = tpcg.MaxZ();
138  }
139  }
140 
141  //x
142  double dista = fabs(minx - position.X());
143  double distb = fabs(position.X() - maxx);
144  if ((position.X() > minx) && (position.X() < maxx) &&
145  (dista > distance_buffer) && (distb > distance_buffer)) inside = true;
146  //y
147  dista = fabs(maxy - position.Y());
148  distb = fabs(position.Y() - miny);
149  if (inside && (position.Y() > miny) && (position.Y() < maxy) &&
150  (dista > distance_buffer) && (distb > distance_buffer)) inside = true;
151  else inside = false;
152  //z
153  dista = fabs(maxz - position.Z());
154  distb = fabs(position.Z() - minz);
155  if (inside && (position.Z() > minz) && (position.Z() < maxz) &&
156  (dista > distance_buffer) && (distb > distance_buffer)) inside = true;
157  else inside = false;
158  }
159 
160  return inside;
161 }
162 
163 double SBNRecoUtils::CalculateTrackLength(const core::ProviderManager &manager, const art::Ptr<recob::Track> track){
164  double length = 0;
165  if (track->NumberTrajectoryPoints()==1) return length; //Nothing to calculate if there is only one point
166 
167  for (size_t i_tp = 0; i_tp < track->NumberTrajectoryPoints()-1; i_tp++){ //Loop from the first to 2nd to last point
168  TVector3 this_point(track->TrajectoryPoint(i_tp).position.X(),track->TrajectoryPoint(i_tp).position.Y(),track->TrajectoryPoint(i_tp).position.Z());
169  if (!SBNRecoUtils::IsInsideTPC(manager, this_point,0)){
170  std::cout<<"SBNRecoUtils::CalculateTrackLength - Current trajectory point not in the TPC volume. Skip over this point in the track length calculation"<<std::endl;
171  continue;
172  }
173  TVector3 next_point(track->TrajectoryPoint(i_tp+1).position.X(),track->TrajectoryPoint(i_tp+1).position.Y(),track->TrajectoryPoint(i_tp+1).position.Z());
174  if (!SBNRecoUtils::IsInsideTPC(manager, next_point,0)){
175  std::cout<<"SBNRecoUtils::CalculateTrackLength - Next trajectory point not in the TPC volume. Skip over this point in the track length calculation"<<std::endl;
176  continue;
177  }
178 
179  length+=(next_point-this_point).Mag();
180  }
181  return length;
182 }
183 
184 double SBNRecoUtils::TrackCompletion(const core::ProviderManager &manager, int mcparticle_id, const std::vector<art::Ptr<recob::Hit>> &reco_track_hits) {
185  // get handle to back tracker
187 
188  // get all the IDE's of the truth track
189  const std::vector<const sim::IDE*> mcparticle_ides = bt->TrackIdToSimIDEs_Ps(mcparticle_id);
190  // sum it up
191  double mcparticle_energy = 0.;
192  for (auto const &ide: mcparticle_ides) {
193  mcparticle_energy += ide->energy;
194  }
195 
196  // get all the hits of the reco track that match the truth track
197  const std::vector<art::Ptr<recob::Hit>> matched_reco_track_hits = bt->TrackIdToHits_Ps(mcparticle_id, reco_track_hits);
198 
199  // for each of the hits get the energy coming from the track
200  double matched_reco_energy = 0.;
201  for (auto const &matched_reco_track_hit: matched_reco_track_hits) {
202  std::vector<sim::IDE> this_hit_IDEs = bt->HitToAvgSimIDEs(*matched_reco_track_hit);
203  for (auto const &ide: this_hit_IDEs) {
204  if (ide.trackID == mcparticle_id) {
205  matched_reco_energy += ide.energy;
206  }
207  }
208  }
209 
210  return matched_reco_energy / mcparticle_energy;
211 }
212 
CryostatGeo const & GetElement(geo::CryostatID const &cryoid) const
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
unsigned int Ncryostats() const
Returns the number of cryostats in the detector.
cheat::BackTracker * GetBackTrackerProvider() const
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:178
double CalculateTrackLength(const core::ProviderManager &manager, const art::Ptr< recob::Track > track)
const geo::GeometryCore * GetGeometryProvider() const
geo::TPCID FindTPCAtPosition(double const worldLoc[3]) const
Returns the ID of the TPC at specified location.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Interface to LArSoft services.
T abs(T value)
double TrackCompletion(const core::ProviderManager &manager, int mcparticle_id, const std::vector< art::Ptr< recob::Hit >> &reco_track_hits)
std::vector< art::Ptr< recob::Hit > > TrackIdToHits_Ps(detinfo::DetectorClocksData const &clockData, int tkId, std::vector< art::Ptr< recob::Hit >> const &hitsIn) const
Definition: BackTracker.cc:235
double MinZ() const
Returns the world z coordinate of the start of the box.
int TrueParticleIDFromTotalTrueEnergy(const core::ProviderManager &manager, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
bool IsInsideTPC(const core::ProviderManager &manager, TVector3 position, double distance_buffer)
unsigned int NTPC() const
Number of TPCs in this cryostat.
Definition: CryostatGeo.h:181
int TrueParticleID(const core::ProviderManager &manager, const art::Ptr< recob::Hit > hit, bool rollup_unsaved_ids=1)
CryostatGeo const & Cryostat(geo::CryostatID const &cryoid) const
Returns the specified cryostat.
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
Description of geometry of one entire detector.
double MaxY() const
Returns the world y coordinate of the end of the box.
int TrueParticleIDFromTotalRecoHits(const core::ProviderManager &manager, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
bool HasTPC(geo::TPCID const &tpcid) const
Returns whether we have the specified TPC.
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.
std::vector< const sim::IDE * > TrackIdToSimIDEs_Ps(int const &id) const
Definition: BackTracker.cc:63
std::vector< sim::IDE > HitToAvgSimIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
Definition: BackTracker.cc:305
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)
int TrueParticleIDFromTotalRecoCharge(const core::ProviderManager &manager, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)