All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Static Public Member Functions | Private Types | Static Private Member Functions | List of all members
lar_pandora::LArPandoraSliceIdHelper Class Reference

Helper class for slice id tools. More...

#include <LArPandoraSliceIdHelper.h>

Classes

class  SliceMetadata
 Class to hold MC metdata about slices. More...
 

Public Types

typedef std::vector
< SliceMetadata
SliceMetadataVector
 

Static Public Member Functions

static void GetSliceMetadata (const SliceVector &slices, const art::Event &evt, const std::string &truthLabel, const std::string &mcParticleLabel, const std::string &hitLabel, const std::string &backtrackLabel, const std::string &pandoraLabel, SliceMetadataVector &sliceMetadata, simb::MCNeutrino &mcNeutrino)
 Get MC metadata about each slice. More...
 

Private Types

typedef std::unordered_map
< art::Ptr< recob::Hit >, bool > 
HitToBoolMap
 

Static Private Member Functions

static art::Ptr< simb::MCTruth > GetBeamNeutrinoMCTruth (const art::Event &evt, const std::string &truthLabel)
 Get the MCTruth block for the simulated beam neutrino. More...
 
static void CollectNeutrinoMCParticles (const art::Event &evt, const std::string &truthLabel, const std::string &mcParticleLabel, const art::Ptr< simb::MCTruth > &beamNuMCTruth, MCParticleVector &mcParticles)
 Collect all MCParticles that come from the beam neutrino interaction. More...
 
static void GetHitOrigins (const art::Event &evt, const std::string &hitLabel, const std::string &backtrackLabel, const MCParticleVector &mcParticles, HitVector &hits, HitToBoolMap &hitToIsNuInducedMap)
 For each hit in the event, determine if any of it's charge was deposited by a neutrino induced particle. More...
 
static unsigned int CountNeutrinoHits (const HitVector &hits, const HitToBoolMap &hitToIsNuInducedMap)
 Count the number of hits in an input vector that are neutrino induced. More...
 
static void GetPFParticleToHitsMap (const art::Event &evt, const std::string &pandoraLabel, PFParticlesToHits &pfParticleToHitsMap)
 Get the mapping from PFParticles to associated hits (via clusters) More...
 
static void GetReconstructedHitsInSlice (const Slice &slice, const PFParticlesToHits &pfParticleToHitsMap, HitVector &hits)
 Collect the hits in the slice that have been added to a PFParticle (under either reconstruction hypothesis) More...
 
static void CollectHits (const PFParticleVector &pfParticles, const PFParticlesToHits &pfParticleToHitsMap, HitVector &hits)
 Collect the hits in a given vector of PFParticles. More...
 
static void GetSliceMetadata (const SliceVector &slices, const PFParticlesToHits &pfParticleToHitsMap, const HitToBoolMap &hitToIsNuInducedMap, const unsigned int nNuHits, SliceMetadataVector &sliceMetadata)
 Calculate the MC slice metadata. More...
 

Detailed Description

Helper class for slice id tools.

Definition at line 34 of file LArPandoraSliceIdHelper.h.

Member Typedef Documentation

typedef std::unordered_map<art::Ptr<recob::Hit>, bool> lar_pandora::LArPandoraSliceIdHelper::HitToBoolMap
private

Definition at line 76 of file LArPandoraSliceIdHelper.h.

Definition at line 54 of file LArPandoraSliceIdHelper.h.

Member Function Documentation

void lar_pandora::LArPandoraSliceIdHelper::CollectHits ( const PFParticleVector pfParticles,
const PFParticlesToHits pfParticleToHitsMap,
HitVector hits 
)
staticprivate

Collect the hits in a given vector of PFParticles.

Parameters
pfParticlesthe input vector of PFParticles
pfParticleToHitsMapthe input mapping from PFParticles to hits
hitsthe output vector of hits

Definition at line 214 of file LArPandoraSliceIdHelper.cxx.

215 {
216  for (const auto &part : pfParticles)
217  {
218  const auto it(pfParticleToHitsMap.find(part));
219  if (it == pfParticleToHitsMap.end())
220  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::CollectHits - can't find any hits associated to input PFParticle" << std::endl;
221 
222  for (const auto &hit : it->second)
223  {
224  // ATTN here we ensure that we don't double count hits, even if the input PFParticles are from different Pandora instances
225  if (std::find(hits.begin(), hits.end(), hit) == hits.end())
226  hits.push_back(hit);
227  }
228  }
229 }
process_name hit
Definition: cheaterreco.fcl:51
void lar_pandora::LArPandoraSliceIdHelper::CollectNeutrinoMCParticles ( const art::Event &  evt,
const std::string &  truthLabel,
const std::string &  mcParticleLabel,
const art::Ptr< simb::MCTruth > &  beamNuMCTruth,
MCParticleVector mcParticles 
)
staticprivate

Collect all MCParticles that come from the beam neutrino interaction.

Parameters
evtthe art event
truthLabelthe label of the MCTruth producer
mcParticleLabelthe label of the MCParticle producer
beamNuMCTruththe MCTruth block for the beam neutrino
mcParticlesthe output vector of neutrino induced MCParticles

Definition at line 88 of file LArPandoraSliceIdHelper.cxx.

90 {
91  // Get the MCTruth handle
92  art::Handle< std::vector<simb::MCTruth> > mcTruthHandle;
93  evt.getByLabel(truthLabel, mcTruthHandle);
94 
95  if (!mcTruthHandle.isValid())
96  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::CollectNeutrinoMCParticles - invalid MCTruth handle" << std::endl;
97 
98  // Find MCParticles that are associated to the beam neutrino MCTruth block
99  art::FindManyP<simb::MCParticle> truthToMCParticleAssns(mcTruthHandle, evt, mcParticleLabel);
100  mcParticles = truthToMCParticleAssns.at(beamNuMCTruth.key()); // ATTN will throw if association from beamNuMCTruth doesn't exist. We want this!
101 }
TCEvent evt
Definition: DataStructs.cxx:8
unsigned int lar_pandora::LArPandoraSliceIdHelper::CountNeutrinoHits ( const HitVector hits,
const HitToBoolMap hitToIsNuInducedMap 
)
staticprivate

Count the number of hits in an input vector that are neutrino induced.

Parameters
hitsthe input vector of hits
hitToIsNuInducedMapthe mapping from hits to isNuInduced boolean
Returns
the number of hits that are neutrino induced

Definition at line 143 of file LArPandoraSliceIdHelper.cxx.

144 {
145  unsigned int nNuHits(0);
146  for (const auto &hit : hits)
147  {
148  const auto it(hitToIsNuInducedMap.find(hit));
149 
150  if (it == hitToIsNuInducedMap.end())
151  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::CountNeutrinoHits - can't find hit in hitToIsNuInducedMap" << std::endl;
152 
153  nNuHits += it->second ? 1 : 0;
154  }
155 
156  return nNuHits;
157 }
process_name hit
Definition: cheaterreco.fcl:51
art::Ptr< simb::MCTruth > lar_pandora::LArPandoraSliceIdHelper::GetBeamNeutrinoMCTruth ( const art::Event &  evt,
const std::string &  truthLabel 
)
staticprivate

Get the MCTruth block for the simulated beam neutrino.

Parameters
evtthe art event
truthLabelthe label of the MCTruth producer
Returns
the MCTruth block for the simulated beam neutrino

Definition at line 50 of file LArPandoraSliceIdHelper.cxx.

51 {
52  // Get the MCTruth handle
53  art::Handle< std::vector<simb::MCTruth> > mcTruthHandle;
54  evt.getByLabel(truthLabel, mcTruthHandle);
55 
56  if (!mcTruthHandle.isValid())
57  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetBeamNeutrinoMCTruth - invalid MCTruth handle" << std::endl;
58 
59  // Look for the truth block that is from the beam neutrino, and ensure we choose the one with the highest energy if there are multiple
60  bool foundNeutrino(false);
61  float maxNeutrinoEnergy(-std::numeric_limits<float>::max());
62  art::Ptr<simb::MCTruth> beamNuMCTruth;
63  for (unsigned int i = 0; i < mcTruthHandle->size(); ++i)
64  {
65  const art::Ptr<simb::MCTruth> mcTruth(mcTruthHandle, i);
66 
67  if (mcTruth->Origin() != simb::kBeamNeutrino)
68  continue;
69 
70  const float nuEnergy(mcTruth->GetNeutrino().Nu().E());
71  if (nuEnergy < maxNeutrinoEnergy)
72  continue;
73 
74  // Select this as the beam neutrino
75  maxNeutrinoEnergy = nuEnergy;
76  beamNuMCTruth = mcTruth;
77  foundNeutrino = true;
78  }
79 
80  if (!foundNeutrino)
81  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetBeamNeutrinoMCTruth - found no beam neutrino MCTruth blocks" << std::endl;
82 
83  return beamNuMCTruth;
84 }
TCEvent evt
Definition: DataStructs.cxx:8
void lar_pandora::LArPandoraSliceIdHelper::GetHitOrigins ( const art::Event &  evt,
const std::string &  hitLabel,
const std::string &  backtrackLabel,
const MCParticleVector mcParticles,
HitVector hits,
HitToBoolMap hitToIsNuInducedMap 
)
staticprivate

For each hit in the event, determine if any of it's charge was deposited by a neutrino induced particle.

Parameters
evtthe art event
hitLabelthe label of the Hit producer
backtrackLabelthe label of the Hit->MCParticle association producer - backtracker
mcParticlesthe input vector of neutrino induced MCParticles
hitsthe output vector of all hits
hitToIsNuInducedMapthe output mapping from hits to a bool = true if hit is neutrino induced

Definition at line 105 of file LArPandoraSliceIdHelper.cxx.

107 {
108  // Collect the hits from the event
109  art::Handle< std::vector<recob::Hit> > hitHandle;
110  evt.getByLabel(hitLabel, hitHandle);
111 
112  if (!hitHandle.isValid())
113  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetHitOrigins - invalid hit handle" << std::endl;
114 
115  art::FindManyP<simb::MCParticle> hitToMCParticleAssns(hitHandle, evt, backtrackLabel);
116 
117  // Find the hits that are associated to a neutrino induced MCParticle using the Hit->MCParticle associations form the backtracker
118  for (unsigned int i = 0; i < hitHandle->size(); ++i)
119  {
120  const art::Ptr<recob::Hit> hit(hitHandle, i);
121  hits.push_back(hit);
122 
123  const auto &particles(hitToMCParticleAssns.at(hit.key()));
124 
125  bool foundNuParticle(false);
126  for (const auto &part : particles)
127  {
128  // If the MCParticles isn't in the list of neutrino particles
129  if (std::find(mcParticles.begin(), mcParticles.end(), part) == mcParticles.end())
130  continue;
131 
132  foundNuParticle = true;
133  break;
134  }
135 
136  if (!hitToIsNuInducedMap.emplace(hit, foundNuParticle).second)
137  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetHitOrigins - repeated hits in input collection" << std::endl;
138  }
139 }
process_name hit
Definition: cheaterreco.fcl:51
TCEvent evt
Definition: DataStructs.cxx:8
void lar_pandora::LArPandoraSliceIdHelper::GetPFParticleToHitsMap ( const art::Event &  evt,
const std::string &  pandoraLabel,
PFParticlesToHits pfParticleToHitsMap 
)
staticprivate

Get the mapping from PFParticles to associated hits (via clusters)

Parameters
evtthe art event
pandoraLabelthe label of the PFParticle <-> Cluster and Cluster <-> Hit associations - Pandora pattern recognition
pfParticleToHitsMapthe output mapping from PFParticles to associated hits

Definition at line 161 of file LArPandoraSliceIdHelper.cxx.

162 {
163  // Get the PFParticles
164  art::Handle< std::vector<recob::PFParticle> > pfParticleHandle;
165  evt.getByLabel(pandoraLabel, pfParticleHandle);
166 
167  if (!pfParticleHandle.isValid())
168  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetPFParticleToHitsMap - invalid PFParticle handle" << std::endl;
169 
170  // Get the Clusters
171  art::Handle< std::vector<recob::Cluster> > clusterHandle;
172  evt.getByLabel(pandoraLabel, clusterHandle);
173 
174  if (!clusterHandle.isValid())
175  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetPFParticleToHitsMap - invalid cluster handle" << std::endl;
176 
177  // Get the associations between PFParticles -> Clusters -> Hits
178  art::FindManyP<recob::Cluster> pfParticleToClusterAssns(pfParticleHandle, evt, pandoraLabel);
179  art::FindManyP<recob::Hit> clusterToHitAssns(clusterHandle, evt, pandoraLabel);
180 
181  // Get the hits associated to each PFParticles
182  for (unsigned int iPart = 0; iPart < pfParticleHandle->size(); ++iPart)
183  {
184  const art::Ptr<recob::PFParticle> part(pfParticleHandle, iPart);
185  HitVector hits;
186 
187  for (const auto &cluster : pfParticleToClusterAssns.at(part.key()))
188  {
189  for (const auto &hit : clusterToHitAssns.at(cluster.key()))
190  {
191  if (std::find(hits.begin(), hits.end(), hit) != hits.end())
192  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetPFParticleToHitsMap - double counted hits!" << std::endl;
193 
194  hits.push_back(hit);
195  }
196  }
197 
198  if (!pfParticleToHitsMap.emplace(part, hits).second)
199  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetPFParticleToHitsMap - repeated input PFParticles" << std::endl;
200  }
201 }
process_name cluster
Definition: cheaterreco.fcl:51
process_name hit
Definition: cheaterreco.fcl:51
std::vector< art::Ptr< recob::Hit > > HitVector
TCEvent evt
Definition: DataStructs.cxx:8
void lar_pandora::LArPandoraSliceIdHelper::GetReconstructedHitsInSlice ( const Slice slice,
const PFParticlesToHits pfParticleToHitsMap,
HitVector hits 
)
staticprivate

Collect the hits in the slice that have been added to a PFParticle (under either reconstruction hypothesis)

Parameters
slicethe input slice
pfParticleToHitsMapthe input mapping from PFParticles to hits
hitsthe output vector of reconstructed hits in the slice

Definition at line 205 of file LArPandoraSliceIdHelper.cxx.

206 {
207  // ATTN here we use the PFParticles from both hypotheses to collect the hits. Hits will not be double counted
208  LArPandoraSliceIdHelper::CollectHits(slice.GetTargetHypothesis(), pfParticleToHitsMap, hits);
209  LArPandoraSliceIdHelper::CollectHits(slice.GetCosmicRayHypothesis(), pfParticleToHitsMap, hits);
210 }
static void CollectHits(const PFParticleVector &pfParticles, const PFParticlesToHits &pfParticleToHitsMap, HitVector &hits)
Collect the hits in a given vector of PFParticles.
void lar_pandora::LArPandoraSliceIdHelper::GetSliceMetadata ( const SliceVector slices,
const art::Event &  evt,
const std::string &  truthLabel,
const std::string &  mcParticleLabel,
const std::string &  hitLabel,
const std::string &  backtrackLabel,
const std::string &  pandoraLabel,
SliceMetadataVector sliceMetadata,
simb::MCNeutrino &  mcNeutrino 
)
static

Get MC metadata about each slice.

Parameters
slicesthe input vector of slices
evtthe art event
truthLabelthe label of the MCTruth producer
mcParticleLabelthe label of the MCParticle producer
hitLabelthe label of the Hit producer
backtrackLabelthe label of the Hit -> MCParticle association producer
pandoraLabelthe label of the Pandora all outcomes producer
sliceMetadatathe output vector of slice metadata (mapping 1:1 to the slices)
interactionTypethe output true interaction type code of the MCNeutrino
nuEnergythe output true energy of the neutrino
nuNeutrinothe mc neutrino from the beam neutrino MCTruth block

Definition at line 22 of file LArPandoraSliceIdHelper.cxx.

25 {
26  // Find the beam neutrino in MC
27  const auto beamNuMCTruth(LArPandoraSliceIdHelper::GetBeamNeutrinoMCTruth(evt, truthLabel));
28  mcNeutrino = beamNuMCTruth->GetNeutrino();
29 
30  // Collect all MC particles resulting from the beam neutrino
31  MCParticleVector mcParticles;
32  LArPandoraSliceIdHelper::CollectNeutrinoMCParticles(evt, truthLabel, mcParticleLabel, beamNuMCTruth, mcParticles);
33 
34  // Get the hits and determine which are neutrino induced
35  HitVector hits;
36  HitToBoolMap hitToIsNuInducedMap;
37  LArPandoraSliceIdHelper::GetHitOrigins(evt, hitLabel, backtrackLabel, mcParticles, hits, hitToIsNuInducedMap);
38  const unsigned int nNuHits(LArPandoraSliceIdHelper::CountNeutrinoHits(hits, hitToIsNuInducedMap));
39 
40  // Get the mapping from PFParticle to hits through clusters
41  PFParticlesToHits pfParticleToHitsMap;
42  LArPandoraSliceIdHelper::GetPFParticleToHitsMap(evt, pandoraLabel, pfParticleToHitsMap);
43 
44  // Calculate the metadata for each slice
45  LArPandoraSliceIdHelper::GetSliceMetadata(slices, pfParticleToHitsMap, hitToIsNuInducedMap, nNuHits, sliceMetadata);
46 }
std::unordered_map< art::Ptr< recob::Hit >, bool > HitToBoolMap
static unsigned int CountNeutrinoHits(const HitVector &hits, const HitToBoolMap &hitToIsNuInducedMap)
Count the number of hits in an input vector that are neutrino induced.
static void CollectNeutrinoMCParticles(const art::Event &evt, const std::string &truthLabel, const std::string &mcParticleLabel, const art::Ptr< simb::MCTruth > &beamNuMCTruth, MCParticleVector &mcParticles)
Collect all MCParticles that come from the beam neutrino interaction.
static void GetPFParticleToHitsMap(const art::Event &evt, const std::string &pandoraLabel, PFParticlesToHits &pfParticleToHitsMap)
Get the mapping from PFParticles to associated hits (via clusters)
static void GetHitOrigins(const art::Event &evt, const std::string &hitLabel, const std::string &backtrackLabel, const MCParticleVector &mcParticles, HitVector &hits, HitToBoolMap &hitToIsNuInducedMap)
For each hit in the event, determine if any of it&#39;s charge was deposited by a neutrino induced partic...
static void GetSliceMetadata(const SliceVector &slices, const art::Event &evt, const std::string &truthLabel, const std::string &mcParticleLabel, const std::string &hitLabel, const std::string &backtrackLabel, const std::string &pandoraLabel, SliceMetadataVector &sliceMetadata, simb::MCNeutrino &mcNeutrino)
Get MC metadata about each slice.
std::map< art::Ptr< recob::PFParticle >, HitVector > PFParticlesToHits
std::vector< art::Ptr< simb::MCParticle > > MCParticleVector
std::vector< TCSlice > slices
Definition: DataStructs.cxx:13
std::vector< art::Ptr< recob::Hit > > HitVector
static art::Ptr< simb::MCTruth > GetBeamNeutrinoMCTruth(const art::Event &evt, const std::string &truthLabel)
Get the MCTruth block for the simulated beam neutrino.
TCEvent evt
Definition: DataStructs.cxx:8
void lar_pandora::LArPandoraSliceIdHelper::GetSliceMetadata ( const SliceVector slices,
const PFParticlesToHits pfParticleToHitsMap,
const HitToBoolMap hitToIsNuInducedMap,
const unsigned int  nNuHits,
SliceMetadataVector sliceMetadata 
)
staticprivate

Calculate the MC slice metadata.

Parameters
slicesthe input vector of slices
pfParticleToHitsMapthe input mapping from PFParticles to hits
hitToIsNuInducedMapthe input mapping from hits to isNuInduced boolean
nNuHitsthe total number of neutrino induced hits in the event
sliceMetadatathe output vector of metadata objects correspoinding 1:1 to the input slices

Definition at line 233 of file LArPandoraSliceIdHelper.cxx.

235 {
236  if (!sliceMetadata.empty())
237  throw cet::exception("LArPandora") << " LArPandoraSliceIdHelper::GetSliceMetadata - non empty input metadata vector" << std::endl;
238 
239  if (slices.empty())
240  return;
241 
242  unsigned int mostCompleteSliceIndex(0);
243  unsigned int maxNuHits(0);
244 
245  for (unsigned int sliceIndex = 0; sliceIndex < slices.size(); ++sliceIndex)
246  {
247  const Slice &slice(slices.at(sliceIndex));
248  HitVector hits;
249  LArPandoraSliceIdHelper::GetReconstructedHitsInSlice(slice, pfParticleToHitsMap, hits);
250 
251  const unsigned int nHitsInSlice(hits.size());
252  const unsigned int nNuHitsInSlice(LArPandoraSliceIdHelper::CountNeutrinoHits(hits, hitToIsNuInducedMap));
253 
254  if (nNuHitsInSlice > maxNuHits)
255  {
256  mostCompleteSliceIndex = sliceIndex;
257  maxNuHits = nNuHitsInSlice;
258  }
259 
260  SliceMetadata metadata;
261  metadata.m_nHits = nHitsInSlice;
262  metadata.m_purity = ((nHitsInSlice == 0) ? -1.f : static_cast<float>(nNuHitsInSlice) / static_cast<float>(nHitsInSlice));
263  metadata.m_completeness = ((nNuHits == 0) ? -1.f : static_cast<float>(nNuHitsInSlice) / static_cast<float>(nNuHits));
264  metadata.m_isMostComplete = false;
265 
266  sliceMetadata.push_back(metadata);
267  }
268 
269  sliceMetadata.at(mostCompleteSliceIndex).m_isMostComplete = true;
270 }
static unsigned int CountNeutrinoHits(const HitVector &hits, const HitToBoolMap &hitToIsNuInducedMap)
Count the number of hits in an input vector that are neutrino induced.
SlicingAlgorithm::Slice Slice
static void GetReconstructedHitsInSlice(const Slice &slice, const PFParticlesToHits &pfParticleToHitsMap, HitVector &hits)
Collect the hits in the slice that have been added to a PFParticle (under either reconstruction hypot...
std::vector< TCSlice > slices
Definition: DataStructs.cxx:13
std::vector< art::Ptr< recob::Hit > > HitVector

The documentation for this class was generated from the following files: