CVNProtoDUNEUtils.cxx
Go to the documentation of this file.
2 
5 #include "canvas/Persistency/Common/FindManyP.h"
6 #include "canvas/Persistency/Common/FindOneP.h"
7 
9 
10 }
11 
13 
14 }
15 
16 // Get the hits from a given reco slice
17 const std::vector<const recob::Hit*> cvn::CVNProtoDUNEUtils::GetRecoSliceHits(const recob::Slice &slice, art::Event const &evt, const std::string sliceModule) const{
18 
19  return GetRecoSliceHits(slice.ID(),evt,sliceModule);
20 
21 }
22 
23 // Get the reco hits but using the slice id instead
24 const std::vector<const recob::Hit*> cvn::CVNProtoDUNEUtils::GetRecoSliceHits(const unsigned int sliceID, art::Event const &evt, const std::string sliceModule) const{
25 
26  auto recoSlices = evt.getValidHandle<std::vector<recob::Slice> >(sliceModule);
27  art::FindManyP<recob::Hit> findHits(recoSlices,evt,sliceModule);
28  std::vector<art::Ptr<recob::Hit>> inputHits = findHits.at(sliceID);
29 
30  std::vector<const recob::Hit*> sliceHits;
31 
32  for(const art::Ptr<recob::Hit> hit : inputHits){
33 
34  sliceHits.push_back(hit.get());
35 
36  }
37 
38  return sliceHits;
39 
40 }
41 
42 // Get a map of a slice number and all hits in the slice
43 const std::map<unsigned int, std::vector<const recob::Hit*>> cvn::CVNProtoDUNEUtils::GetRecoSliceHitMap(art::Event const &evt, const std::string sliceModule) const{
44 
45  auto recoSlices = evt.getValidHandle<std::vector<recob::Slice> >(sliceModule);
46  std::map<unsigned int, std::vector<const recob::Hit*>> hitMap;
47 
48  for(auto const slice : *recoSlices){
49 
50  const std::vector<const recob::Hit*> constvec = GetRecoSliceHits(slice.ID(),evt,sliceModule);
51  for(auto const h : constvec){
52  hitMap[slice.ID()].push_back(h);
53  }
54 
55  }
56 
57  return hitMap;
58 
59 }
60 
61 // Try to get the slice tagged as beam
62 unsigned short cvn::CVNProtoDUNEUtils::GetBeamSlice(art::Event const &evt, const std::string particleLabel) const{
63 
64  const std::map<unsigned int, std::vector<const recob::PFParticle*>> sliceMap = GetPFParticleSliceMap(evt,particleLabel);
65 
66  for(auto slice : sliceMap){
67  for(auto particle : slice.second){
68  if(IsBeamParticle(*particle,evt,particleLabel)){
69  return slice.first;
70  }
71  }
72  }
73 
74  return 9999;
75 
76 }
77 
78 // Return a map of primary particles grouped by their reconstructed slice. Useful for finding slices with multiple particles
79 const std::map<unsigned int,std::vector<const recob::PFParticle*>> cvn::CVNProtoDUNEUtils::GetPFParticleSliceMap(art::Event const &evt, const std::string particleLabel) const{
80 
81  return SliceMapHelper(evt,particleLabel,true);
82 
83 }
84 
85 // Return a map of all particles grouped by their reconstructed slice.
86 const std::map<unsigned int,std::vector<const recob::PFParticle*>> cvn::CVNProtoDUNEUtils::GetAllPFParticleSliceMap(art::Event const &evt, const std::string particleLabel) const{
87 
88  return SliceMapHelper(evt,particleLabel,false);
89 
90 }
91 
92 // Helper to get slice maps and avoid duplicate code
93 const std::map<unsigned int,std::vector<const recob::PFParticle*>> cvn::CVNProtoDUNEUtils::SliceMapHelper(art::Event const &evt, const std::string particleLabel, bool primaryOnly) const{
94 
95  // Get the particles
96  auto pfParticles = evt.getValidHandle<std::vector<recob::PFParticle>>(particleLabel);
97 
98  std::map<unsigned int, std::vector<const recob::PFParticle*>> sliceMap;
99 
100  for(unsigned int p = 0; p < pfParticles->size(); ++p){
101  const recob::PFParticle* particle = &(pfParticles->at(p));
102 
103  // Only the primary particles have the slice association
104  if(primaryOnly && !particle->IsPrimary()) continue;
105 
106  unsigned int thisSlice = GetPFParticleSliceIndex(*particle,evt,particleLabel);
107 
108  if(thisSlice != 9999){
109  sliceMap[thisSlice].push_back(particle);
110  }
111  }
112 
113  return sliceMap;
114 
115 }
116 
117 // Get the space points associated to the PFParticle
118 const std::vector<const recob::SpacePoint*> cvn::CVNProtoDUNEUtils::GetPFParticleSpacePoints(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const{
119 
120  // Get the particles and their associations
121  auto particles = evt.getValidHandle<std::vector<recob::PFParticle>>(particleLabel);
122  const art::FindManyP<recob::SpacePoint> findSpacePoints(particles,evt,particleLabel);
123  const std::vector<art::Ptr<recob::SpacePoint>> pfpSpacePoints = findSpacePoints.at(particle.Self());
124 
125  // We don't want the art::Ptr so we need to get rid of it
126  std::vector<const recob::SpacePoint*> sp;
127  for(auto pointer : pfpSpacePoints){
128  sp.push_back(pointer.get());
129  }
130 
131  return sp;
132 }
133 
134 // Get the reconstructed slice associated with a particle
135 const recob::Slice* cvn::CVNProtoDUNEUtils::GetPFParticleSlice(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const{
136 
137  // Perhaps we should use the associations to do this?
138  auto pfParticles = evt.getValidHandle<std::vector<recob::PFParticle>>(particleLabel);
139  const art::FindOneP<recob::Slice> findSlice(pfParticles,evt,particleLabel);
140 
141  const recob::Slice* slice = findSlice.at(particle.Self()).get();
142 
143  return slice;
144 }
145 
146 // Get the reconstructed slice associated with a particle
147 unsigned short cvn::CVNProtoDUNEUtils::GetPFParticleSliceIndex(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const{
148 
149  // Try to use slices if we can
150  try{
151  const recob::Slice* slice = GetPFParticleSlice(particle,evt,particleLabel);
152  return slice->ID();
153  }
154  // Otherwise fall back on metadata
155  catch(...){
156  std::map<std::string,float> mdMap = GetPFParticleMetaData(particle,evt,particleLabel);
157  std::string search = "SliceIndex";
158  if(mdMap.find(search) != mdMap.end()){
159  return static_cast<unsigned short>(mdMap.at(search));
160  }
161  else{
162 // std::cerr << "Object has no slice index... returning 9999" << std::endl;
163  return 9999;
164  }
165  }
166 
167 }
168 
169 const std::map<std::string,float> cvn::CVNProtoDUNEUtils::GetPFParticleMetaData(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const {
170  // Get the particles
171  auto pfParticles = evt.getValidHandle<std::vector<recob::PFParticle>>(particleLabel);
172  // And their meta data
173  const art::FindManyP<larpandoraobj::PFParticleMetadata> findMetaData(pfParticles,evt,particleLabel);
174 
175  const larpandoraobj::PFParticleMetadata metaData = *((findMetaData.at(particle.Self())).at(0));
176 
177  return metaData.GetPropertiesMap();
178 }
179 
180 // Use the pandora metadata to tell us if this is a beam particle or not
181 bool cvn::CVNProtoDUNEUtils::IsBeamParticle(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const{
182  std::map<std::string,float> mdMap = GetPFParticleMetaData(particle,evt,particleLabel);
183  if(mdMap.find("IsTestBeam") != mdMap.end()){
184  return true;
185  }
186  else{
187  return false;
188  }
189 }
const std::map< unsigned int, std::vector< const recob::PFParticle * > > SliceMapHelper(art::Event const &evt, const std::string particleLabel, bool primaryOnly) const
Helper to get the slice map and avoid code repetition.
size_t Self() const
Returns the index of this particle.
Definition: PFParticle.h:92
const std::map< std::string, float > GetPFParticleMetaData(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const
Get the metadata associated to a PFParticle from pandora.
std::string string
Definition: nybbler.cc:12
const std::map< unsigned int, std::vector< const recob::PFParticle * > > GetAllPFParticleSliceMap(art::Event const &evt, const std::string particleLabel) const
Get a map of slice index to all of the PFParticles within it.
Metadata associated to PFParticles.
unsigned short GetPFParticleSliceIndex(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const
Get the reconstructed slice number associated with a particle.
const std::vector< const recob::Hit * > GetRecoSliceHits(const recob::Slice &slice, art::Event const &evt, const std::string sliceModule) const
const std::vector< const recob::SpacePoint * > GetPFParticleSpacePoints(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const
Get the SpacePoints associated to the PFParticle.
const PropertiesMap & GetPropertiesMap() const
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
bool IsPrimary() const
Returns whether the particle is the root of the flow.
Definition: PFParticle.h:86
const recob::Slice * GetPFParticleSlice(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const
Get the reconstructed slice associated with a particle.
Definition: search.py:1
Detector simulation of raw signals on wires.
int ID() const
Definition: Slice.h:29
Declaration of signal hit object.
Hierarchical representation of particle flow.
Definition: PFParticle.h:44
unsigned short GetBeamSlice(art::Event const &evt, const std::string particleLabel) const
Try to get the slice tagged as beam. Returns 9999 if no beam slice was found.
const std::map< unsigned int, std::vector< const recob::Hit * > > GetRecoSliceHitMap(art::Event const &evt, const std::string sliceModule) const
TCEvent evt
Definition: DataStructs.cxx:7
bool IsBeamParticle(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel) const
Use the pandora metadata to tell us if this is a beam particle or not.
const std::map< unsigned int, std::vector< const recob::PFParticle * > > GetPFParticleSliceMap(art::Event const &evt, const std::string particleLabel) const
Get a map of slice index to the primary PFParticles within it.