Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TruthSegToTruthPartAssocAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
5 
8 #include "AthLinks/ElementLink.h"
9 #include "Identifier/Identifier.h"
13 
14 #include <unordered_set>
15 
16 namespace {
17  using IdSet_t = std::unordered_set<Identifier>;
18  unsigned int countMatched(const std::unordered_set<const xAOD::MuonSimHit*>& simHits,
19  const IdSet_t& matchIds) {
20  return std::ranges::count_if(simHits, [&matchIds](const xAOD::MuonSimHit* hit) {
21  return matchIds.count(hit->identify());
22  });
23  }
24 }
25 namespace MuonR4{
28  for (const std::string& hitIds : m_simHitIds) {
29  m_simHitKeys.emplace_back(m_truthKey, hitIds);
30  }
31  ATH_CHECK(m_simHitKeys.initialize());
35  ATH_CHECK(m_idHelperSvc.retrieve());
36  return StatusCode::SUCCESS;
37  }
38  StatusCode TruthSegToTruthPartAssocAlg::execute(const EventContext& ctx) const {
39 
40  const xAOD::TruthParticleContainer* truthParticles{nullptr};
41  ATH_CHECK(SG::get(truthParticles, m_truthKey, ctx));
42 
44  using TruthSegLink_t = std::vector<ElementLink<xAOD::MuonSegmentContainer>>;
46 
48  std::vector<IdDecorHandle_t> idDecorHandles{};
50  idDecorHandles.emplace_back(hitKey, ctx);
51  }
53  using IdSet_t = std::unordered_set<Identifier>;
54  using TruthTuple_t = std::tuple<const xAOD::TruthParticle*, IdSet_t>;
55  std::vector<TruthTuple_t> truthPartWithIds{};
56  truthPartWithIds.reserve(truthParticles->size());
57  for (const xAOD::TruthParticle* truthMuon : *truthParticles){
58  segLinkDecor(*truthMuon).clear();
59  IdSet_t assocIds{};
60  ATH_MSG_DEBUG("Truth muon "<<truthMuon->pt()<<", eta: "<<truthMuon->eta()<<", "<<truthMuon->phi()
61  <<", barcode: "<<HepMC::barcode(truthMuon));
62  for (const IdDecorHandle_t& hitDecor : idDecorHandles) {
63  std::ranges::transform(hitDecor(*truthMuon), std::inserter(assocIds, assocIds.begin()),
64  [this](unsigned long long rawId){
65  const Identifier id{rawId};
66  ATH_MSG_VERBOSE(" --- associated hit id: "<<m_idHelperSvc->toString(id));
67  return id;
68  });
69  }
70  truthPartWithIds.emplace_back(std::make_tuple(truthMuon, std::move(assocIds)));
71  }
73  const xAOD::MuonSegmentContainer* segments{nullptr};
74  ATH_CHECK(SG::get(segments, m_segmentKey, ctx));
75 
77  using TruthPartLink_t = ElementLink<xAOD::TruthParticleContainer>;
79 
80 
81  for (const xAOD::MuonSegment* segment : *segments){
82  std::unordered_set<const xAOD::MuonSimHit*> simHits = getMatchingSimHits(*segment);
84  <<" chamberId: "<<Muon::MuonStationIndex::chName(segment->chamberIndex())
85  <<", phi: "<<segment->sector()<<", nPrecHits: "<<segment->nPrecisionHits()
86  <<", nDoF: "<<segment->numberDoF()<<" sim hits: "<<simHits.size());
87  if (msgLvl(MSG::VERBOSE)){
88  std::vector<const xAOD::MuonSimHit*> sortedHits{simHits.begin(), simHits.end()};
89  std::ranges::sort(sortedHits, [](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
90  return a->identify() < b->identify();
91  });
92  for (const xAOD::MuonSimHit* hit: sortedHits) {
93  ATH_MSG_VERBOSE(" --- associated sim hit: "<<m_idHelperSvc->toString(hit->identify())
94  <<", locPos: "<<Amg::toString(xAOD::toEigen(hit->localPosition()))
95  <<", locDir: "<<Amg::toString(xAOD::toEigen(hit->localDirection()))
96  <<", "<<hit->genParticleLink());
97  }
98  }
99  /* now find the truth particle with all associated hits */
100  const auto best_itr = std::ranges::max_element(truthPartWithIds,
101  [&simHits](const TruthTuple_t& truthTupleA,
102  const TruthTuple_t& truthTupleB) {
103  return countMatched(simHits, std::get<1>(truthTupleA)) <
104  countMatched(simHits, std::get<1>(truthTupleB));
105  });
106  if (best_itr == truthPartWithIds.end()) {
107  ATH_MSG_WARNING("No truth particle matched the truth hits of the segment");
108  continue;
109  }
110  if (1.*countMatched(simHits, std::get<1>(*best_itr)) < 0.5* simHits.size()) {
111  if (msgLvl(MSG::VERBOSE)) {
112  for (const auto& [truthMuon, assocIds]: truthPartWithIds){
113  std::stringstream unMatchedStr{};
114  unsigned int counts{0};
115  for (const xAOD::MuonSimHit* hit: simHits) {
116  if (!assocIds.count(hit->identify())){
117  unMatchedStr<<" *** "<<m_idHelperSvc->toString(hit->identify())<<std::endl;
118  } else {
119  ++counts;
120  }
121  }
122  if (!counts) continue;
123  ATH_MSG_VERBOSE("Truth muon "<<truthMuon->pt()<<", eta: "<<truthMuon->eta()<<", "<<truthMuon->phi()
124  <<", barcode: "<<HepMC::barcode(truthMuon)<<", matched hits: "<<counts<<", unmatched: "<<std::endl<<unMatchedStr.str());
125  }
126  }
127  continue;
128  }
129  const xAOD::TruthParticle* truthPart{std::get<0>(*best_itr)};
130  segLinkDecor(*truthPart).emplace_back(segments, segment->index());
131  truthLinkDecor(*segment) = TruthPartLink_t{truthParticles, truthPart->index()};
132  }
133 
134  return StatusCode::SUCCESS;
135  }
136 }
Muon::MuonStationIndex::chName
static const std::string & chName(ChIndex index)
convert ChIndex into a string
Definition: MuonStationIndex.cxx:157
MuonSimHitHelpers.h
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
MuonValR4::countMatched
unsigned int countMatched(const simHitSet &truthHits, const simHitSet &recoHits)
Definition: MuonHoughTransformTester.cxx:31
xAOD::MuonSimHit_v1::identify
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
Definition: xAODMuonSimHit_V1.cxx:42
TruthSegToTruthPartAssocAlg.h
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
MuonR4::TruthSegToTruthPartAssocAlg::m_truthLinkKey
SG::WriteDecorHandleKey< xAOD::MuonSegmentContainer > m_truthLinkKey
Key of the truthParticleLink decorated onto the segment.
Definition: TruthSegToTruthPartAssocAlg.h:44
MuonR4::TruthSegToTruthPartAssocAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
Definition: TruthSegToTruthPartAssocAlg.h:32
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
MuonR4::TruthSegToTruthPartAssocAlg::initialize
virtual StatusCode initialize() override final
Definition: TruthSegToTruthPartAssocAlg.cxx:26
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
MuonR4::TruthSegToTruthPartAssocAlg::m_simHitIds
Gaudi::Property< std::vector< std::string > > m_simHitIds
List of simHit id decorations to read from the truth particle.
Definition: TruthSegToTruthPartAssocAlg.h:36
MuonR4::TruthSegToTruthPartAssocAlg::m_simHitKeys
SG::ReadDecorHandleKeyArray< xAOD::TruthParticleContainer > m_simHitKeys
Declaration of the dependency on the simHit decorations.
Definition: TruthSegToTruthPartAssocAlg.h:38
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
MuonR4::TruthSegToTruthPartAssocAlg::m_segLinkKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_segLinkKey
Declaration of the segmentLink to the truth particle.
Definition: TruthSegToTruthPartAssocAlg.h:40
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
WriteDecorHandle.h
Handle class for adding a decoration to an object.
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
MuonR4::TruthSegToTruthPartAssocAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: TruthSegToTruthPartAssocAlg.cxx:38
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::SegmentFit::toString
std::string toString(const Parameters &pars)
Definition: SegmentFitterEventData.cxx:59
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
MuonR4::TruthSegToTruthPartAssocAlg::m_segmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Key to the truth segment container to associate.
Definition: TruthSegToTruthPartAssocAlg.h:42
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
MuonR4::TruthSegToTruthPartAssocAlg::m_truthKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthKey
Key to the truth particle container to associate.
Definition: TruthSegToTruthPartAssocAlg.h:34
python.TruthMuonD3PDObject.truthMuon
truthMuon
Definition: TruthMuonD3PDObject.py:51
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
a
TList * a
Definition: liststreamerinfos.cxx:10
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
SegmentFitterEventData.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonR4::SegmentFit::localSegmentPars
Parameters localSegmentPars(const xAOD::MuonSegment &seg)
Returns the localSegPars decoration from a xAODMuon::Segment.
Definition: SegmentFitterEventData.cxx:32
ReadDecorHandle.h
Handle class for reading a decoration on an object.
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:13
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer >
MuonR4::getMatchingSimHits
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
Definition: MuonSimHitHelpers.cxx:27
HepMCHelpers.h
NSWL1::PadTriggerAdapter::segment
Muon::NSW_PadTriggerSegment segment(const NSWL1::PadTrigger &data)
Definition: PadTriggerAdapter.cxx:5