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>;
26 using ChVec_t = std::vector<std::uint16_t>;
28 static const SG::ConstAccessor<ChVec_t> acc_phiChannel{"SDO_phiChannels"};
29 static const SG::ConstAccessor<ChVec_t> acc_etaChannel{"SDO_etaChannels"};
30 static const SG::ConstAccessor<ChVec_t> acc_padChannel{"SDO_padChannels"};
31}
32
33namespace MuonR4{
34
35
37 ATH_CHECK(m_simHitsKey.initialize());
38 ATH_CHECK(m_prdHitKey.initialize());
39 ATH_CHECK(m_decorKey.initialize());
40 ATH_CHECK(m_idHelperSvc.retrieve());
41 return StatusCode::SUCCESS;
42 }
43 template <typename PrdType_t>
44 const xAOD::MuonSimHit*
46 const xAOD::MuonMeasurement* measurement) const {
47
48 const auto* prd = dynamic_cast<const PrdType_t*>(measurement);
49 // const MuonGMR4::MuonReadoutElement* re = prd->readoutElement();
50 for (const xAOD::MuonSimHit* hit : simHits) {
52 const Identifier hitId = hit->identify();
53 if (hitId == prd->identify()) {
54 return hit;
55 }
56 if constexpr(!std::is_same_v<PrdType_t, xAOD::MdtDriftCircle>) {
57 if (prd->readoutElement()->layerHash(hitId) != prd->layerHash()) {
58 continue;
59 }
60 }
61 if constexpr(std::is_same_v<PrdType_t, xAOD::TgcStrip> ||
62 std::is_same_v<PrdType_t, xAOD::RpcMeasurement>) {
63 if (( prd->measuresPhi() && Acts::rangeContainsValue(acc_phiChannel(*hit), prd->channelNumber())) ||
64 (!prd->measuresPhi() && Acts::rangeContainsValue(acc_etaChannel(*hit), prd->channelNumber()))) {
65 return hit;
66 }
67 } else if constexpr (std::is_same_v<PrdType_t, xAOD::MMCluster> ||
68 std::is_same_v<PrdType_t, xAOD::sTgcStripCluster>) {
69 for (auto ch : prd->stripNumbers()) {
70 if (Acts::rangeContainsValue(acc_etaChannel(*hit), ch)) {
71 return hit;
72 }
73 }
74 } else if constexpr(std::is_same_v<PrdType_t, xAOD::sTgcWireHit>) {
75 if (Acts::rangeContainsValue(acc_phiChannel(*hit), prd->channelNumber())){
76 return hit;
77 }
78 } else if constexpr(std::is_same_v<PrdType_t, xAOD::sTgcPadHit>) {
79 if (Acts::rangeContainsValue(acc_padChannel(*hit), prd->channelNumber())){
80 return hit;
81 }
82 }
83 }
84 return nullptr;
85 }
86
87 StatusCode PrepDataToSimHitAssocAlg::execute(const EventContext & ctx) const {
88 const xAOD::MuonSimHitContainer* simHits{nullptr};
89 const xAOD::MuonMeasurementContainer* measurements{nullptr};
90 ATH_CHECK(SG::get(simHits, m_simHitsKey, ctx));
91 ATH_CHECK(SG::get(measurements, m_prdHitKey, ctx));
92
93 if (measurements->empty()){
94 return StatusCode::SUCCESS;
95 }
96
97 xAOD::ChamberViewer prdViewer{*measurements};
99 xAOD::ContainerDecorator decorHandle{m_decorKey, ctx, LinkType{}};
101 do {
102 const Identifier chambId = prdViewer.at(0)->identify();
103 const IdentifierHash viewHash = m_idHelperSvc->detElementHash(chambId);
105 if ((simHitViewer.size() == 0 || m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) > viewHash) &&
106 !simHitViewer.loadView(chambId)) {
107 ATH_MSG_DEBUG("No simHit view for " << m_idHelperSvc->toStringDetEl(chambId));
108 continue;
109 } else if (m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) < viewHash \
110 && !simHitViewer.next([viewHash, this](const xAOD::MuonSimHit* hit){
111 return m_idHelperSvc->detElementHash(hit->identify()) == viewHash;
112 })) {
113 continue;
114 }
115 ATH_MSG_VERBOSE("Container size "<<simHits->size()<<" viewer size: "<<simHitViewer.size()
116 <<" view hash: "<<viewHash);
117 for (const xAOD::MuonMeasurement* measurement : prdViewer) {
119 const xAOD::MuonSimHit* bestSimHit{nullptr};
120 switch (measurement->type()) {
122 bestSimHit = truthMatchPrd<xAOD::MdtDriftCircle>(simHitViewer, measurement);
123 break;
125 bestSimHit = truthMatchPrd<xAOD::RpcMeasurement>(simHitViewer, measurement);
126 break;
128 bestSimHit = truthMatchPrd<xAOD::TgcStrip>(simHitViewer, measurement);
129 break;
131 bestSimHit = truthMatchPrd<xAOD::MMCluster>(simHitViewer, measurement);
132 break;
134 const auto* prd = static_cast<const xAOD::sTgcMeasurement*>(measurement);
135 switch (prd->channelType()) {
137 case Strip:{
138 bestSimHit = truthMatchPrd<xAOD::sTgcStripCluster>(simHitViewer, measurement);
139 break;
140 }
141 case Wire:{
142 bestSimHit = truthMatchPrd<xAOD::sTgcWireHit>(simHitViewer, measurement);
143 break;
144 }
145 case Pad:{
146 bestSimHit = truthMatchPrd<xAOD::sTgcPadHit>(simHitViewer, measurement);
147 break;
148 }
149 }
150 break;
151 } default: {
152 ATH_MSG_FATAL("Non muon measurement is parsed");
153 return StatusCode::FAILURE;
154 }
155 }
156 if (!bestSimHit) {
157 continue;
158 }
159
160 decorHandle(*measurement) = LinkType{*simHits, bestSimHit->index()};
161 }
162 } while (prdViewer.next());
163
164 return StatusCode::SUCCESS;
165 }
166}
167
#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)
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