10 #include "cetlib_except/exception.h" 12 #include "canvas/Persistency/Common/FindManyP.h" 30 mcNeutrino = beamNuMCTruth->GetNeutrino();
59 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetBeamNeutrinoMCTruth - invalid MCTruth handle" <<
std::endl;
62 bool foundNeutrino(
false);
65 for (
unsigned int i = 0; i < mcTruthHandle->size(); ++i)
73 if (nuEnergy < maxNeutrinoEnergy)
77 maxNeutrinoEnergy = nuEnergy;
78 beamNuMCTruth = mcTruth;
83 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetBeamNeutrinoMCTruth - found no beam neutrino MCTruth blocks" <<
std::endl;
98 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::CollectNeutrinoMCParticles - invalid MCTruth handle" <<
std::endl;
101 art::FindManyP<simb::MCParticle> truthToMCParticleAssns(mcTruthHandle, evt, mcParticleLabel);
102 mcParticles = truthToMCParticleAssns.at(beamNuMCTruth.
key());
115 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetHitOrigins - invalid hit handle" <<
std::endl;
117 art::FindManyP<simb::MCParticle> hitToMCParticleAssns(hitHandle, evt, backtrackLabel);
120 for (
unsigned int i = 0; i < hitHandle->size(); ++i)
125 const auto &particles(hitToMCParticleAssns.at(hit.
key()));
127 bool foundNuParticle(
false);
128 for (
const auto &part : particles)
131 if (std::find(mcParticles.begin(), mcParticles.end(), part) == mcParticles.end())
134 foundNuParticle =
true;
138 if (!hitToIsNuInducedMap.emplace(hit, foundNuParticle).second)
139 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetHitOrigins - repeated hits in input collection" <<
std::endl;
147 unsigned int nNuHits(0);
148 for (
const auto &
hit : hits)
150 const auto it(hitToIsNuInducedMap.find(
hit));
152 if (it == hitToIsNuInducedMap.end())
153 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::CountNeutrinoHits - can't find hit in hitToIsNuInducedMap" <<
std::endl;
155 nNuHits += it->second ? 1 : 0;
167 evt.
getByLabel(pandoraLabel, pfParticleHandle);
169 if (!pfParticleHandle.
isValid())
170 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetPFParticleToHitsMap - invalid PFParticle handle" <<
std::endl;
177 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetPFParticleToHitsMap - invalid cluster handle" <<
std::endl;
180 art::FindManyP<recob::Cluster> pfParticleToClusterAssns(pfParticleHandle, evt, pandoraLabel);
181 art::FindManyP<recob::Hit> clusterToHitAssns(clusterHandle, evt, pandoraLabel);
184 for (
unsigned int iPart = 0; iPart < pfParticleHandle->size(); ++iPart)
189 for (
const auto &
cluster : pfParticleToClusterAssns.at(part.
key()))
191 for (
const auto &
hit : clusterToHitAssns.at(
cluster.key()))
193 if (std::find(hits.begin(), hits.end(),
hit) != hits.end())
194 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetPFParticleToHitsMap - double counted hits!" <<
std::endl;
200 if (!pfParticleToHitsMap.emplace(part, hits).second)
201 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetPFParticleToHitsMap - repeated input PFParticles" <<
std::endl;
218 for (
const auto &part : pfParticles)
220 const auto it(pfParticleToHitsMap.find(part));
221 if (it == pfParticleToHitsMap.end())
222 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::CollectHits - can't find any hits associated to input PFParticle" <<
std::endl;
224 for (
const auto &
hit : it->second)
227 if (std::find(hits.begin(), hits.end(),
hit) == hits.end())
238 if (!sliceMetadata.empty())
239 throw cet::exception(
"LArPandora") <<
" LArPandoraSliceIdHelper::GetSliceMetadata - non empty input metadata vector" <<
std::endl;
244 unsigned int mostCompleteSliceIndex(0);
245 unsigned int maxNuHits(0);
247 for (
unsigned int sliceIndex = 0; sliceIndex < slices.size(); ++sliceIndex)
249 const Slice &slice(slices.at(sliceIndex));
253 const unsigned int nHitsInSlice(hits.size());
256 if (nNuHitsInSlice > maxNuHits)
258 mostCompleteSliceIndex = sliceIndex;
259 maxNuHits = nNuHitsInSlice;
263 metadata.
m_nHits = nHitsInSlice;
264 metadata.
m_purity = ((nHitsInSlice == 0) ? -1.
f : static_cast<float>(nNuHitsInSlice) /
static_cast<float>(nHitsInSlice));
265 metadata.
m_completeness = ((nNuHits == 0) ? -1.
f : static_cast<float>(nNuHitsInSlice) /
static_cast<float>(nNuHits));
268 sliceMetadata.push_back(metadata);
271 sliceMetadata.at(mostCompleteSliceIndex).m_isMostComplete =
true;
279 m_completeness(-
std::numeric_limits<
float>::
max()),
280 m_nHits(
std::numeric_limits<unsigned
int>::
max()),
281 m_isMostComplete(false)
double E(const int i=0) const
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.
const simb::MCNeutrino & GetNeutrino() const
static void CollectHits(const PFParticleVector &pfParticles, const PFParticlesToHits &pfParticleToHitsMap, HitVector &hits)
Collect the hits in a given vector of PFParticles.
const PFParticleVector & GetCosmicRayHypothesis() const
Get the slice as reconstructed under the cosmic-ray hypothesis.
const simb::MCParticle & Nu() const
simb::Origin_t Origin() const
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's charge was deposited by a neutrino induced partic...
std::vector< SliceMetadata > SliceMetadataVector
Cluster finding and building.
helper class for slice id tools
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...
bool isValid() const noexcept
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
const PFParticleVector & GetTargetHypothesis() const
Get the slice as reconstructed under the target hypothesis.
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::vector< art::Ptr< recob::PFParticle > > PFParticleVector
key_type key() const noexcept
std::map< art::Ptr< recob::PFParticle >, HitVector > PFParticlesToHits
std::vector< art::Ptr< simb::MCParticle > > MCParticleVector
static int max(int a, int b)
std::vector< TCSlice > slices
Detector simulation of raw signals on wires.
std::vector< art::Ptr< recob::Hit > > HitVector
Declaration of signal hit object.
static art::Ptr< simb::MCTruth > GetBeamNeutrinoMCTruth(const art::Event &evt, const std::string &truthLabel)
Get the MCTruth block for the simulated beam neutrino.
std::vector< Slice > SliceVector
Event generator information.
cet::coded_exception< error, detail::translate > exception
QTextStream & endl(QTextStream &s)