All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackReducer.cxx
Go to the documentation of this file.
1 #include "gallery/ValidHandle.h"
2 #include "canvas/Utilities/InputTag.h"
3 #include "canvas/Persistency/Common/FindMany.h"
4 
5 #include "nusimdata/SimulationBase/MCParticle.h"
10 
11 #include "TrackReducer.h"
12 #include "Data.h"
13 
15 
16 void sbnana::TrackReducer::Initialize(fhicl::ParameterSet* config) {
17  fTree->Branch("tracks", &fTracks);
18 }
19 
20 bool sbnana::TrackReducer::ProcessEvent(const gallery::Event& ev, const std::vector<event::Interaction> &truth, std::vector<event::RecoInteraction>& reco) {
21  std::cout << "New Event!\n";
22  fTracks.reco_tracks.clear();
23  fTracks.reco_showers.clear();
24  fTracks.truth.clear();
25 
26  // map of particle track ID's to indices
27  std::map<int, unsigned> trackID_to_index;
28 
29  // get the important truth stuff
30  const std::vector<simb::MCParticle> mcparticles = *ev.getValidHandle<std::vector<simb::MCParticle>>("largeant");
31 
32  unsigned ind = 0;
33  for (const simb::MCParticle &mc_particle: mcparticles) {
34  trackID_to_index[mc_particle.TrackId()] = ind;
35  ind ++;
36  }
37 
38  for (const simb::MCParticle &mc_particle: mcparticles) {
39  TrueParticle particle;
40  particle.pid = mc_particle.PdgCode();
41  particle.process = mc_particle.Process();
42  for (unsigned i = 0; i < mc_particle.NumberDaughters(); i++) {
43  int daughter_id = mc_particle.Daughter(i);
44  if (trackID_to_index.count(daughter_id)) particle.daughters.push_back(trackID_to_index.at(daughter_id));
45  }
46  particle.energy = mc_particle.E();
47 
48  if (mc_particle.NumberTrajectoryPoints() > 0) {
49  for (unsigned i = 0; i < mc_particle.NumberTrajectoryPoints(); i++) {
50  const TVector3 &pos = mc_particle.Position(i).Vect();
51  std::array<float, 3> traj {(float)pos.X(), (float)pos.Y(), (float)pos.Z()};
52  particle.trajectory.push_back(traj);
53  }
54  }
55  else {
56  const TVector3 &pos = mc_particle.Position().Vect();
57  std::array<float, 3> traj {(float)pos.X(), (float)pos.Y(), (float)pos.Z()};
58  particle.trajectory.push_back(traj);
59  }
60 
61  std::cout << "Particle: " << mc_particle.TrackId() << " PID: " << mc_particle.PdgCode() << " process: " << mc_particle.Process() << " energy: " << mc_particle.E() << std::endl;
62  fTracks.truth.push_back(std::move(particle));
63 
64  }
65 
66  const auto &track_handle = ev.getValidHandle<std::vector<recob::Track>>("pandoraTrack");
67  const auto &shower_handle = ev.getValidHandle<std::vector<recob::Shower>>("pandoraShower");
68 
69  art::FindManyP<recob::Hit> tracks_to_hits(track_handle, ev, "pandoraTrack");
70  art::FindManyP<recob::PFParticle> tracks_to_particles(track_handle, ev, "pandoraTrack");
71 
72  art::FindManyP<recob::PFParticle> showers_to_particles(shower_handle, ev, "pandoraShower");
73  art::FindManyP<recob::Hit> showers_to_hits(shower_handle, ev, "pandoraShower");
74 
75  // setup ID mappings
76  std::map<unsigned, unsigned> particle_to_showerID;
77  std::map<unsigned, unsigned> particle_to_trackID;
78  for (unsigned track_id = 0; track_id < track_handle->size(); track_id++) {
79  unsigned particleID = tracks_to_particles.at(track_id).at(0)->Self();
80  std::cout << "track: " << track_id << " to particle: " << particleID << std::endl;
81  particle_to_trackID[particleID] = track_id;
82  }
83 
84  for (unsigned shower_id = 0; shower_id < shower_handle->size(); shower_id++) {
85  unsigned particleID = showers_to_particles.at(shower_id).at(0)->Self();
86  std::cout << "shower: " << shower_id << " to particle: " << particleID << std::endl;
87  particle_to_showerID[particleID] = shower_id;
88  }
89 
90  for (unsigned track_id = 0; track_id < track_handle->size(); track_id++) {
91  const recob::Track &rb_track = (*track_handle)[track_id];
92  Track track;
93  for (unsigned i = 0; i < rb_track.CountValidPoints(); i++) {
94  std::array<float, 3> traj {(float)rb_track.LocationAtPoint(i).X(), (float)rb_track.LocationAtPoint(i).Y(), (float)rb_track.LocationAtPoint(i).Z()};
95  track.trajectory.push_back(traj);
96  }
97  std::vector<art::Ptr<recob::Hit>> hits = tracks_to_hits.at(track_id);
98 
99  // truth match
100  int mcp_track_id = SBNRecoUtils::TrueParticleIDFromTotalTrueEnergy(*fProviderManager, hits, true);
101  if (trackID_to_index.count(mcp_track_id)) track.truth_match = trackID_to_index.at(mcp_track_id);
102  else track.truth_match = -1;
103 
104  // get daughters
105  const recob::PFParticle &pfparticle = *tracks_to_particles.at(track_id).at(0);
106  for (unsigned daughter: pfparticle.Daughters()) {
107  std::cout << "Daughter! " << daughter << std::endl;
108  if (particle_to_trackID.count(daughter)) track.track_daughters.push_back(particle_to_trackID.at(daughter));
109  else if (particle_to_showerID.count(daughter)) track.shower_daughters.push_back(particle_to_showerID.at(daughter));
110  }
111 
112  std::cout << "New Track! n traj: " << track.trajectory.size() << std::endl;;
113 
114  fTracks.reco_tracks.push_back(track);
115  }
116 
117  for (unsigned shower_id = 0; shower_id < shower_handle->size(); shower_id++) {
118  const recob::Shower &rb_shower = (*shower_handle)[shower_id];
119  Shower shower;
120  rb_shower.Direction().GetXYZ(shower.direction);
121  rb_shower.DirectionErr().GetXYZ(shower.direction_err);
122  rb_shower.ShowerStart().GetXYZ(shower.start);
123  rb_shower.ShowerStartErr().GetXYZ(shower.start_err);
124 
125  std::vector<art::Ptr<recob::Hit>> hits = showers_to_hits.at(shower_id);
126 
127  // truth match
128  int mcp_track_id = SBNRecoUtils::TrueParticleIDFromTotalTrueEnergy(*fProviderManager, hits, true);
129  if (trackID_to_index.count(mcp_track_id)) shower.truth_match = trackID_to_index.at(mcp_track_id);
130  else shower.truth_match = -1;
131 
132  // get daughters
133  const recob::PFParticle &pfparticle = *showers_to_particles.at(shower_id).at(0);
134  for (unsigned daughter: pfparticle.Daughters()) {
135  std::cout << "Daughter! " << daughter << std::endl;
136  if (particle_to_trackID.count(daughter)) shower.track_daughters.push_back(particle_to_trackID.at(daughter));
137  else if (particle_to_showerID.count(daughter)) shower.shower_daughters.push_back(particle_to_showerID.at(daughter));
138  }
139 
140  fTracks.reco_showers.push_back(shower);
141  std::cout << "New Shower! at: " << rb_shower.ShowerStart().X() << std::endl;
142  std::cout << "Match to: " << mcp_track_id << std::endl;
143  }
144 
145  return true;
146 }
147 
float start_err[3]
Error in start location of shower [cm].
Definition: Data.h:16
const std::vector< size_t > & Daughters() const
Returns the collection of daughter particles.
Definition: PFParticle.h:114
std::vector< std::array< float, 3 > > trajectory
True trajectory of the particle [cm].
Definition: Data.h:27
std::vector< std::array< float, 3 > > trajectory
The reconstructed trajectory [cm].
Definition: Data.h:8
std::vector< unsigned > shower_daughters
The list of &quot;daughter&quot; showers to this one. Each entry is the index into the reco list of each daught...
Definition: Data.h:20
const TVector3 & Direction() const
Definition: Shower.h:189
Declaration of signal hit object.
int truth_match
Match to true particle. -1 if no truth match exists. Otherwise, is the index into the truth list...
Definition: Data.h:11
Point_t const & LocationAtPoint(size_t i) const
TTree * fTree
The output ROOT tree.
void Initialize(fhicl::ParameterSet *config=NULL)
process_name use argoneut_mc_hitfinder track
Definition: Data.h:14
process_name shower
Definition: cheaterreco.fcl:51
Definition: Data.h:7
std::vector< unsigned > track_daughters
The list of &quot;daughter&quot; tracks to this one. Each entry is the index into the reco list of each daughte...
Definition: Data.h:9
unsigned int CountValidPoints() const
float energy
Initial energy of the particle [GeV].
Definition: Data.h:26
float start[3]
Start location of shower [cm].
Definition: Data.h:15
process_name standard_reco_uboone reco
std::string process
Process which created this particle.
Definition: Data.h:29
std::vector< unsigned > track_daughters
The list of &quot;daughter&quot; tracks to this one. Each entry is the index into the reco list of each daughte...
Definition: Data.h:19
const TVector3 & DirectionErr() const
Definition: Shower.h:190
int TrueParticleIDFromTotalTrueEnergy(const core::ProviderManager &manager, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
#define DECLARE_SBN_PROCESSOR(classname)
Provides recob::Track data product.
int truth_match
Match to true particle. -1 if no truth match exists. Otherwise, is the index into the truth list...
Definition: Data.h:21
Hierarchical representation of particle flow.
Definition: PFParticle.h:44
float direction_err[3]
Error in initial direction of shower.
Definition: Data.h:18
const TVector3 & ShowerStart() const
Definition: Shower.h:192
int pid
Particle ID.
Definition: Data.h:25
std::vector< unsigned > shower_daughters
The list of &quot;daughter&quot; showers to this one. Each entry is the index into the reco list of each daught...
Definition: Data.h:10
std::vector< unsigned > daughters
The list of true &quot;daughter&quot; particles to this one. Each entry is the index into the truth list of eac...
Definition: Data.h:28
const TVector3 & ShowerStartErr() const
Definition: Shower.h:193
float direction[3]
Initialial direction of shower (unit vector)
Definition: Data.h:17
BEGIN_PROLOG could also be cout
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track:
bool ProcessEvent(const gallery::Event &ev, const std::vector< event::Interaction > &truth, std::vector< event::RecoInteraction > &reco)