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
18
19#include "Acts/Utilities/Helpers.hpp"
20#include <span>
21#include <cassert>
22namespace{
24 using ChVec_t = std::vector<std::uint16_t>;
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::UncalibratedMeasurement* 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::UncalibratedMeasurementContainer* 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};
100 do {
101 const Identifier chambId = xAOD::identify(prdViewer.at(0));
102 const IdentifierHash viewHash = m_idHelperSvc->detElementHash(chambId);
104 decorHandle(*prdViewer.at(0)) = LinkType{};
105
107 if ((simHitViewer.size() == 0 || m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) > viewHash) &&
108 !simHitViewer.loadView(chambId)) {
109 ATH_MSG_DEBUG("No simHit view for " << m_idHelperSvc->toStringDetEl(chambId));
110 continue;
111 } else if (m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) < viewHash \
112 && !simHitViewer.next([viewHash, this](const xAOD::MuonSimHit* hit){
113 return m_idHelperSvc->detElementHash(hit->identify()) == viewHash;
114 })) {
115 continue;
116 }
117 ATH_MSG_VERBOSE("Container size "<<simHits->size()<<" viewer size: "<<simHitViewer.size()
118 <<" view hash: "<<viewHash);
119 for (const xAOD::UncalibratedMeasurement* measurement : prdViewer) {
121 const xAOD::MuonSimHit* bestSimHit{nullptr};
122 switch (measurement->type()) {
124 bestSimHit = truthMatchPrd<xAOD::MdtDriftCircle>(simHitViewer, measurement);
125 break;
127 bestSimHit = truthMatchPrd<xAOD::RpcMeasurement>(simHitViewer, measurement);
128 break;
130 bestSimHit = truthMatchPrd<xAOD::TgcStrip>(simHitViewer, measurement);
131 break;
133 bestSimHit = truthMatchPrd<xAOD::MMCluster>(simHitViewer, measurement);
134 break;
136 const auto* prd = static_cast<const xAOD::sTgcMeasurement*>(measurement);
137 switch (prd->channelType()) {
139 case Strip:{
140 bestSimHit = truthMatchPrd<xAOD::sTgcStripCluster>(simHitViewer, measurement);
141 break;
142 }
143 case Wire:{
144 bestSimHit = truthMatchPrd<xAOD::sTgcWireHit>(simHitViewer, measurement);
145 break;
146 }
147 case Pad:{
148 bestSimHit = truthMatchPrd<xAOD::sTgcPadHit>(simHitViewer, measurement);
149 break;
150 }
151 }
152 break;
153 } default: {
154 ATH_MSG_FATAL("Non muon measurement is parsed");
155 return StatusCode::FAILURE;
156 }
157 }
158 if (!bestSimHit) {
159 continue;
160 }
161
162 decorHandle(*measurement) = LinkType{*simHits, bestSimHit->index()};
163 }
164 } while (prdViewer.next());
165
166 return StatusCode::SUCCESS;
167 }
168}
169
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
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.
const xAOD::MuonSimHit * truthMatchPrd(Viewer_t< xAOD::MuonSimHitContainer > &simHits, const xAOD::UncalibratedMeasurement *measurement) const
Searches for the best matching sim hit to a measurement.
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitsKey
Key to the associated simHit container.
ElementLink< xAOD::MuonSimHitContainer > LinkType
Attached sim-hit link decoration to the measurement container.
SG::ReadHandleKey< xAOD::UncalibratedMeasurementContainer > m_prdHitKey
Key to the uncalibrated measurements to decorate.
StatusCode execute(const EventContext &ctx) const override final
SG::WriteDecorHandleKey< xAOD::UncalibratedMeasurementContainer > m_decorKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
Definition Pad.h:10
size_t index() const
Return the index of this element within its container.
Helper class to provide constant type-safe access to aux data.
Handle class for adding a decoration to an object.
bool next() noexcept
Loads the hits from the next chamber.
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.
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.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
const Identifier & identify(const UncalibratedMeasurement *meas)
Returns the associated identifier from the muon measurement.
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
sTgcMeasurement_v1 sTgcMeasurement
UncalibratedMeasurementContainer_v1 UncalibratedMeasurementContainer
Define the version of the uncalibrated measurement container.
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.