28 constexpr
G4ID kInvalidG4ID =
29 std::numeric_limits<G4ID>::lowest();
36 [](
const std::pair<G4ID, EDeposit>& a,
const std::pair<G4ID, EDeposit>& b) ->
bool {
37 return std::nextafter(a.second, std::numeric_limits<EDeposit>::lowest()) <= b.second &&
38 std::nextafter(a.second, std::numeric_limits<EDeposit>::max()) >= b.second ?
43 if (idToEDepMap.end() == highestContribIt) {
45 <<
"TruthMatchUtils did not manage to find a max element in the g4 ID to energy deposit " 46 "map. The map size is: " 47 << idToEDepMap.size();
50 return highestContribIt;
60 return kInvalidG4ID != g4ID;
67 const bool rollupUnsavedIDs)
71 if (idToEDepMap.empty())
return kInvalidG4ID;
73 return MaxEDepElementInMap(idToEDepMap)->first;
81 const bool rollupUnsavedIDs)
87 if (idToEDepMap.empty())
return kInvalidG4ID;
89 return MaxEDepElementInMap(idToEDepMap)->first;
97 const bool rollupUnsavedIDs)
102 const EDeposit recoCharge(static_cast<EDeposit>(pHit->Integral()));
103 auto [
iterator, inserted] = idToChargeDepMap.try_emplace(g4ID, recoCharge);
104 if (!inserted)
iterator->second += recoCharge;
107 if (idToChargeDepMap.empty())
return kInvalidG4ID;
109 return MaxEDepElementInMap(idToChargeDepMap)->first;
117 const bool rollupUnsavedIDs)
119 std::map<G4ID, unsigned int> idToHitCountMap;
122 auto [
iterator, inserted] = idToHitCountMap.try_emplace(g4ID, 1);
123 if (!(inserted))
iterator->second++;
126 if (idToHitCountMap.empty())
return kInvalidG4ID;
128 std::map<unsigned int, std::vector<G4ID>> hitCountToIDMap;
129 for (
auto const& [g4ID, hitCount] : idToHitCountMap) {
130 auto [
iterator, inserted] = hitCountToIDMap.try_emplace(hitCount, std::vector<G4ID>{g4ID});
131 if (!inserted)
iterator->second.emplace_back(g4ID);
134 if (hitCountToIDMap.empty()) {
136 <<
"TruthMatchUtils::TrueParticleIDFromTotalRecoHits - Did not fill the hit count to g4 ID " 138 <<
"(map size == " << hitCountToIDMap.size() <<
")." 139 <<
" The G4 ID to hit count map size is " << idToHitCountMap.size()
140 <<
". The hit count to G4 ID vector map should not be empty in this case." 141 <<
" Something has gone wrong.";
144 std::map<unsigned int, std::vector<G4ID>>::const_reverse_iterator lastElementIt(
145 hitCountToIDMap.rbegin());
146 unsigned int nMaxContributingIDs(lastElementIt->second.size());
148 if (0 == nMaxContributingIDs) {
150 <<
"TruthMatchUtils::TrueParticleIDFromTotalRecoHits - Counted a max number of contributing " 152 << lastElementIt->first <<
" hits) but did not find any G4 IDs. Something has gone wrong.";
154 else if (1 < nMaxContributingIDs) {
155 mf::LogInfo(
"TruthMatchUtils::TrueParticleIDFromTotalRecoHits")
156 <<
"There are " << nMaxContributingIDs
157 <<
" particles which tie for highest number of contributing hits (" << lastElementIt->first
158 <<
" hits). Using TruthMatchUtils::TrueParticleIDFromTotalTrueEnergy instead." << std::endl;
162 return lastElementIt->second.front();
170 const bool rollupUnsavedIDs)
173 const std::vector<sim::TrackIDE> trackIDEs(btServ->
HitToTrackIDEs(clockData, pHit));
174 if (trackIDEs.empty())
return;
178 static_cast<G4ID>(rollupUnsavedIDs ?
std::abs(trackIDE.trackID) : trackIDE.trackID));
179 const EDeposit eDep(static_cast<EDeposit>(trackIDE.energy));
180 auto [
iterator, inserted] = idToEDepMap.try_emplace(g4ID, eDep);
181 if (!inserted)
iterator->second += eDep;
184 if (idToEDepMap.empty()) {
186 <<
"TruthMatchUtils::FillG4IDToEnergyDepositMap did not fill the IDToEDepositMap map (map " 188 << idToEDepMap.size() <<
")." 189 <<
" The sim::TrackIDE vector size is " << trackIDEs.size()
190 <<
". The IDToEDepositMap should not be empty when the" 191 <<
" sim::TrackIDE vector is also not empty. Something has gone wrong.";
G4ID TrueParticleIDFromTotalRecoCharge(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit >> &pHits, const bool rollupUnsavedIDs)
The G4 ID of the true particle whose matched hits have produced the largest amount of reconstructed c...
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
G4ID TrueParticleID(detinfo::DetectorClocksData const &clockData, const art::Ptr< recob::Hit > &pHit, const bool rollupUnsavedIDs)
The G4 ID of the true particle which deposits the most energy in the recob::Hit.
constexpr auto abs(T v)
Returns the absolute value of the argument.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Utilities for matching a recob::Hit or vector of recob::Hit to the ID of the most significantly contr...
std::map< G4ID, EDeposit > IDToEDepositMap
bool Valid(const G4ID g4ID) noexcept
Test whether a G4ID returned by the TruthMatchUtils functions is valid.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
void FillG4IDToEnergyDepositMap(IDToEDepositMap &idToEDepMap, detinfo::DetectorClocksData const &clockData, const art::Ptr< recob::Hit > &pHit, const bool rollupUnsavedIDs)
Fill an energy deposition map (maps G4 ID to true energy deposition) for a recob::Hit.
Contains all timing reference information for the detector.
G4ID TrueParticleIDFromTotalTrueEnergy(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit >> &pHits, const bool rollupUnsavedIDs)
The G4 ID of the true particle which deposits the most energy in a vector of recob::Hit.
G4ID TrueParticleIDFromTotalRecoHits(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit >> &pHits, const bool rollupUnsavedIDs)
The G4 ID of the true particle who has been truth-matched to the most hits in a recob::Hit vector...
Ionization energy from a Geant4 track.