8 #include "AthLinks/ElementLink.h"
9 #include "Identifier/Identifier.h"
15 #include <unordered_set>
18 using IdSet_t = std::unordered_set<Identifier>;
19 unsigned int countMatched(
const std::unordered_set<const xAOD::MuonSimHit*>& simHits,
20 const IdSet_t& matchIds) {
21 return std::ranges::count_if(simHits, [&matchIds](
const xAOD::MuonSimHit* hit) {
22 return matchIds.count(hit->
identify());
37 return StatusCode::SUCCESS;
50 std::vector<IdDecorHandle_t> idDecorHandles{};
52 idDecorHandles.emplace_back(hitKey, ctx);
55 using IdSet_t = std::unordered_set<Identifier>;
56 using TruthTuple_t = std::tuple<const xAOD::TruthParticle*, IdSet_t>;
57 std::vector<TruthTuple_t> truthPartWithIds{};
58 truthPartWithIds.reserve(truthParticles->size());
60 segLinkDecor(*truthMuon).clear();
64 for (
const IdDecorHandle_t& hitDecor : idDecorHandles) {
66 [
this](
unsigned long long rawId){
67 const Identifier id{rawId};
72 truthPartWithIds.emplace_back(std::make_tuple(
truthMuon, std::move(assocIds)));
87 <<
", phi: "<<
segment->sector()<<
", nPrecHits: "<<
segment->nPrecisionHits()
88 <<
", nDoF: "<<
segment->numberDoF()<<
" sim hits: "<<simHits.size());
90 std::vector<const xAOD::MuonSimHit*> sortedHits{simHits.begin(), simHits.end()};
92 return a->identify() <
b->identify();
95 ATH_MSG_VERBOSE(
" --- associated sim hit: "<<m_idHelperSvc->toString(hit->identify())
96 <<
", locPos: "<<
Amg::toString(xAOD::toEigen(hit->localPosition()))
97 <<
", locDir: "<<
Amg::toString(xAOD::toEigen(hit->localDirection()))
98 <<
", "<<hit->genParticleLink());
102 const auto best_itr = std::ranges::max_element(truthPartWithIds,
103 [&simHits](
const TruthTuple_t& truthTupleA,
104 const TruthTuple_t& truthTupleB) {
105 return countMatched(simHits, std::get<1>(truthTupleA)) <
108 if (best_itr == truthPartWithIds.end()) {
109 ATH_MSG_WARNING(
"No truth particle matched the truth hits of the segment");
112 if (1.*
countMatched(simHits, std::get<1>(*best_itr)) < 0.5* simHits.size()) {
114 for (
const auto& [
truthMuon, assocIds]: truthPartWithIds){
115 std::stringstream unMatchedStr{};
118 if (!assocIds.count(hit->identify())){
119 unMatchedStr<<
" *** "<<m_idHelperSvc->toString(hit->identify())<<std::endl;
132 segLinkDecor(*truthPart).emplace_back(segments,
segment->index());
133 truthLinkDecor(*
segment) = TruthPartLink_t{truthParticles, truthPart->
index()};
143 return dir.dot((*linkA)->position() -
pos) <
144 dir.dot((*linkB)->position() -
pos);
147 return StatusCode::SUCCESS;