ATLAS Offline Software
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 #include "xAODTruth/TruthVertex.h"
14 
15 #include <unordered_set>
16 
17 namespace {
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());
23  });
24  }
25 }
26 namespace MuonR4{
29  for (const std::string& hitIds : m_simHitIds) {
30  m_simHitKeys.emplace_back(m_truthKey, hitIds);
31  }
32  ATH_CHECK(m_simHitKeys.initialize());
36  ATH_CHECK(m_idHelperSvc.retrieve());
37  return StatusCode::SUCCESS;
38  }
39  StatusCode TruthSegToTruthPartAssocAlg::execute(const EventContext& ctx) const {
40 
41  const xAOD::TruthParticleContainer* truthParticles{nullptr};
42  ATH_CHECK(SG::get(truthParticles, m_truthKey, ctx));
43 
46  using SegLinkVec_t = std::vector<SegLink_t>;
48 
50  std::vector<IdDecorHandle_t> idDecorHandles{};
52  idDecorHandles.emplace_back(hitKey, ctx);
53  }
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());
59  for (const xAOD::TruthParticle* truthMuon : *truthParticles){
60  segLinkDecor(*truthMuon).clear();
61  IdSet_t assocIds{};
62  ATH_MSG_DEBUG("Truth muon "<<truthMuon->pt()<<", eta: "<<truthMuon->eta()<<", "<<truthMuon->phi()
63  <<", barcode: "<< HepMC::uniqueID(truthMuon));
64  for (const IdDecorHandle_t& hitDecor : idDecorHandles) {
65  std::ranges::transform(hitDecor(*truthMuon), std::inserter(assocIds, assocIds.begin()),
66  [this](unsigned long long rawId){
67  const Identifier id{rawId};
68  ATH_MSG_VERBOSE(" --- associated hit id: "<<m_idHelperSvc->toString(id));
69  return id;
70  });
71  }
72  truthPartWithIds.emplace_back(std::make_tuple(truthMuon, std::move(assocIds)));
73  }
75  const xAOD::MuonSegmentContainer* segments{nullptr};
76  ATH_CHECK(SG::get(segments, m_segmentKey, ctx));
77 
79  using TruthPartLink_t = ElementLink<xAOD::TruthParticleContainer>;
81 
82 
83  for (const xAOD::MuonSegment* segment : *segments){
84  std::unordered_set<const xAOD::MuonSimHit*> simHits = getMatchingSimHits(*segment);
86  <<" chamberId: "<<Muon::MuonStationIndex::chName(segment->chamberIndex())
87  <<", phi: "<<segment->sector()<<", nPrecHits: "<<segment->nPrecisionHits()
88  <<", nDoF: "<<segment->numberDoF()<<" sim hits: "<<simHits.size());
89  if (msgLvl(MSG::VERBOSE)){
90  std::vector<const xAOD::MuonSimHit*> sortedHits{simHits.begin(), simHits.end()};
91  std::ranges::sort(sortedHits, [](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
92  return a->identify() < b->identify();
93  });
94  for (const xAOD::MuonSimHit* hit: sortedHits) {
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());
99  }
100  }
101  /* now find the truth particle with all associated hits */
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)) <
106  countMatched(simHits, std::get<1>(truthTupleB));
107  });
108  if (best_itr == truthPartWithIds.end()) {
109  ATH_MSG_WARNING("No truth particle matched the truth hits of the segment");
110  continue;
111  }
112  if (1.*countMatched(simHits, std::get<1>(*best_itr)) < 0.5* simHits.size()) {
113  if (msgLvl(MSG::VERBOSE)) {
114  for (const auto& [truthMuon, assocIds]: truthPartWithIds){
115  std::stringstream unMatchedStr{};
116  unsigned int counts{0};
117  for (const xAOD::MuonSimHit* hit: simHits) {
118  if (!assocIds.count(hit->identify())){
119  unMatchedStr<<" *** "<<m_idHelperSvc->toString(hit->identify())<<std::endl;
120  } else {
121  ++counts;
122  }
123  }
124  if (!counts) continue;
125  ATH_MSG_VERBOSE("Truth muon "<<truthMuon->pt()<<", eta: "<<truthMuon->eta()<<", "<<truthMuon->phi()
126  <<", barcode: "<<HepMC::uniqueID(truthMuon)<<", matched hits: "<<counts<<", unmatched: "<<std::endl<<unMatchedStr.str());
127  }
128  }
129  continue;
130  }
131  const xAOD::TruthParticle* truthPart{std::get<0>(*best_itr)};
132  segLinkDecor(*truthPart).emplace_back(segments, segment->index());
133  truthLinkDecor(*segment) = TruthPartLink_t{truthParticles, truthPart->index()};
134 
135  }
137  for (const xAOD::TruthParticle* truthMuon : *truthParticles){
138  const Amg::Vector3D dir = Amg::Vector3D{truthMuon->px(), truthMuon->py(), truthMuon->pz()}.normalized();
139  const xAOD::TruthVertex* vtx{truthMuon->prodVtx()};
140  const Amg::Vector3D pos = (vtx? Amg::Vector3D{vtx->x(), vtx->y(), vtx->z()} : Amg::Vector3D::Zero());
141  SegLinkVec_t& linkedSegs{segLinkDecor(*truthMuon)};
142  std::ranges::sort(linkedSegs,[&pos, &dir](const SegLink_t& linkA, const SegLink_t& linkB){
143  return dir.dot((*linkA)->position() - pos) <
144  dir.dot((*linkB)->position() - pos);
145  });
146  }
147  return StatusCode::SUCCESS;
148  }
149 }
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:27
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
L1TopoRatesCalculator_submatrix_plotter.counts
counts
Definition: L1TopoRatesCalculator_submatrix_plotter.py:74
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Muon::MuonStationIndex::chName
const std::string & chName(ChIndex index)
convert ChIndex into a string
Definition: MuonStationIndex.cxx:119
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
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
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:116
MuonR4::TruthSegToTruthPartAssocAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: TruthSegToTruthPartAssocAlg.cxx:39
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::SegmentFit::toString
std::string toString(const Parameters &pars)
Definition: SegmentFitterEventData.cxx:60
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
MuonR4::SegLink_t
ElementLink< MuonR4::SegmentContainer > SegLink_t
Definition: xAODSegmentCnvAlg.cxx:18
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
beamspotman.dir
string dir
Definition: beamspotman.py:619
TruthVertex.h
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:37
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:76
MuonR4::TruthSegToTruthPartAssocAlg::m_truthKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthKey
Key to the truth particle container to associate.
Definition: TruthSegToTruthPartAssocAlg.h:34
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
python.TruthMuonD3PDObject.truthMuon
truthMuon
Definition: TruthMuonD3PDObject.py:51
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::SegLinkVec_t
std::vector< SegLink_t > SegLinkVec_t
Definition: MeasurementMarkerAlg.cxx:15
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:33
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
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32
NSWL1::PadTriggerAdapter::segment
Muon::NSW_PadTriggerSegment segment(const NSWL1::PadTrigger &data)
Definition: PadTriggerAdapter.cxx:5