ATLAS Offline Software
Loading...
Searching...
No Matches
PrepDataToSimHitAssocAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
5
16
19
20#include "Acts/Utilities/Helpers.hpp"
21#include <span>
22#include <cassert>
23namespace{
25 using ChVec_t = std::vector<std::uint16_t>;
27 static const SG::ConstAccessor<ChVec_t> acc_phiChannel{"SDO_phiChannels"};
28 static const SG::ConstAccessor<ChVec_t> acc_etaChannel{"SDO_etaChannels"};
29 static const SG::ConstAccessor<ChVec_t> acc_padChannel{"SDO_padChannels"};
30}
31
32namespace MuonR4{
33
34
36 ATH_CHECK(m_simHitsKey.initialize());
37 ATH_CHECK(m_prdHitKey.initialize());
38 ATH_CHECK(m_decorKey.initialize());
39 ATH_CHECK(m_idHelperSvc.retrieve());
40 return StatusCode::SUCCESS;
41 }
42 template <typename PrdType_t>
43 const xAOD::MuonSimHit*
45 const xAOD::MuonMeasurement* measurement) const {
46
47 const auto* prd = dynamic_cast<const PrdType_t*>(measurement);
48 // const MuonGMR4::MuonReadoutElement* re = prd->readoutElement();
49 for (const xAOD::MuonSimHit* hit : simHits) {
51 const Identifier hitId = hit->identify();
52 if (hitId == prd->identify()) {
53 return hit;
54 }
55 if constexpr(!std::is_same_v<PrdType_t, xAOD::MdtDriftCircle>) {
56 if (prd->readoutElement()->layerHash(hitId) != prd->layerHash()) {
57 continue;
58 }
59 }
60 if constexpr(std::is_same_v<PrdType_t, xAOD::TgcStrip> ||
61 std::is_same_v<PrdType_t, xAOD::RpcMeasurement>) {
62 if (( prd->measuresPhi() && Acts::rangeContainsValue(acc_phiChannel(*hit), prd->channelNumber())) ||
63 (!prd->measuresPhi() && Acts::rangeContainsValue(acc_etaChannel(*hit), prd->channelNumber()))) {
64 return hit;
65 }
66 } else if constexpr (std::is_same_v<PrdType_t, xAOD::MMCluster> ||
67 std::is_same_v<PrdType_t, xAOD::sTgcStripCluster>) {
68 for (auto ch : prd->stripNumbers()) {
69 if (Acts::rangeContainsValue(acc_etaChannel(*hit), ch)) {
70 return hit;
71 }
72 }
73 } else if constexpr(std::is_same_v<PrdType_t, xAOD::sTgcWireHit>) {
74 if (Acts::rangeContainsValue(acc_phiChannel(*hit), prd->channelNumber())){
75 return hit;
76 }
77 } else if constexpr(std::is_same_v<PrdType_t, xAOD::sTgcPadHit>) {
78 if (Acts::rangeContainsValue(acc_padChannel(*hit), prd->channelNumber())){
79 return hit;
80 }
81 }
82 }
83 return nullptr;
84 }
85
86 StatusCode PrepDataToSimHitAssocAlg::execute(const EventContext & ctx) const {
87 const xAOD::MuonSimHitContainer* simHits{nullptr};
88 const xAOD::MuonMeasurementContainer* measurements{nullptr};
89 ATH_CHECK(SG::get(simHits, m_simHitsKey, ctx));
90 ATH_CHECK(SG::get(measurements, m_prdHitKey, ctx));
91
92 if (measurements->empty()){
93 return StatusCode::SUCCESS;
94 }
95
96 xAOD::ChamberViewer prdViewer{*measurements};
98 xAOD::ContainerDecorator decorHandle{m_decorKey, ctx, LinkType{}};
100 do {
101 const Identifier chambId = prdViewer.at(0)->identify();
102 const IdentifierHash viewHash = m_idHelperSvc->detElementHash(chambId);
104 if ((simHitViewer.size() == 0 || m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) > viewHash) &&
105 !simHitViewer.loadView(chambId)) {
106 ATH_MSG_DEBUG("No simHit view for " << m_idHelperSvc->toStringDetEl(chambId));
107 continue;
108 } else if (m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) < viewHash \
109 && !simHitViewer.next([viewHash, this](const xAOD::MuonSimHit* hit){
110 return m_idHelperSvc->detElementHash(hit->identify()) == viewHash;
111 })) {
112 continue;
113 }
114 ATH_MSG_VERBOSE("Container size "<<simHits->size()<<" viewer size: "<<simHitViewer.size()
115 <<" view hash: "<<viewHash);
116 for (const xAOD::MuonMeasurement* measurement : prdViewer) {
118 const xAOD::MuonSimHit* bestSimHit{nullptr};
119 switch (measurement->type()) {
121 bestSimHit = truthMatchPrd<xAOD::MdtDriftCircle>(simHitViewer, measurement);
122 break;
124 bestSimHit = truthMatchPrd<xAOD::RpcMeasurement>(simHitViewer, measurement);
125 break;
127 bestSimHit = truthMatchPrd<xAOD::TgcStrip>(simHitViewer, measurement);
128 break;
130 bestSimHit = truthMatchPrd<xAOD::MMCluster>(simHitViewer, measurement);
131 break;
133 const auto* prd = static_cast<const xAOD::sTgcMeasurement*>(measurement);
134 switch (prd->channelType()) {
136 case Strip:{
137 bestSimHit = truthMatchPrd<xAOD::sTgcStripCluster>(simHitViewer, measurement);
138 break;
139 }
140 case Wire:{
141 bestSimHit = truthMatchPrd<xAOD::sTgcWireHit>(simHitViewer, measurement);
142 break;
143 }
144 case Pad:{
145 bestSimHit = truthMatchPrd<xAOD::sTgcPadHit>(simHitViewer, measurement);
146 break;
147 }
148 }
149 break;
150 } default: {
151 ATH_MSG_FATAL("Non muon measurement is parsed");
152 return StatusCode::FAILURE;
153 }
154 }
155 if (!bestSimHit) {
156 continue;
157 }
158
159 decorHandle(*measurement) = LinkType{*simHits, bestSimHit->index(), ctx};
160 }
161 } while (prdViewer.next());
162
163 return StatusCode::SUCCESS;
164 }
165}
166
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_FATAL(x,...)
bool hit(const Container &ids, int pdgId)
Handle class for adding a decoration to an object.
size_type size() const noexcept
Returns the number of elements in the collection.
bool empty() const noexcept
Returns true if the collection is empty.
This is a "hash" representation of an Identifier.
xAOD::ChamberViewer< Cont_t > Viewer_t
Abrivate the chamber viewer type.
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitsKey
Key to the associated simHit container.
SG::ReadHandleKey< xAOD::MuonMeasurementContainer > m_prdHitKey
Key to the uncalibrated measurements to decorate.
ElementLink< xAOD::MuonSimHitContainer > LinkType
Attached sim-hit link decoration to the measurement container.
StatusCode execute(const EventContext &ctx) const override final
const xAOD::MuonSimHit * truthMatchPrd(Viewer_t< xAOD::MuonSimHitContainer > &simHits, const xAOD::MuonMeasurement *measurement) const
Searches for the best matching sim hit to a measurement.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
SG::WriteDecorHandleKey< xAOD::MuonMeasurementContainer > m_decorKey
Definition Pad.h:10
Helper class to provide constant type-safe access to aux data.
const_ref at(const std::size_t idx) const
Returns the i-the measurement from the current chamber.
std::size_t size() const noexcept
Returns how many hits are in the current chamber.
bool loadView(const Identifier &chamberId)
Loads the view matching the parsed identifier.
bool next()
Loads the hits from the next chamber.
Auxiliary class to instantiate WriteDecorHandles.The handles can be created in an empty state.
This header ties the generic definitions in this package.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
@ DetElement
View ends if the detElementHash changes.
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
MuonMeasurement_v1 MuonMeasurement
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
sTgcMeasurement_v1 sTgcMeasurement
MuonMeasurementContainer_v1 MuonMeasurementContainer