ATLAS Offline Software
Loading...
Searching...
No Matches
PrepDataToSimHitAssocAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5
13#include <span>
14namespace MuonR4{
16 ATH_CHECK(m_simHitsKey.initialize());
17 ATH_CHECK(m_prdHitKey.initialize());
18 ATH_CHECK(m_decorKey.initialize());
19 ATH_CHECK(m_idHelperSvc.retrieve());
20 ATH_CHECK(m_geoCtxKey.initialize());
21 return StatusCode::SUCCESS;
22 }
23 StatusCode PrepDataToSimHitAssocAlg::execute(const EventContext & ctx) const {
24 const ActsTrk::GeometryContext* gctx{nullptr};
25 const xAOD::MuonSimHitContainer* simHits{nullptr};
26 const xAOD::UncalibratedMeasurementContainer* measurements{nullptr};
27 ATH_CHECK(SG::get(gctx, m_geoCtxKey, ctx));
28 ATH_CHECK(SG::get(simHits, m_simHitsKey, ctx));
29 ATH_CHECK(SG::get(measurements, m_prdHitKey, ctx));
30
31 xAOD::ChamberViewer prdViewer{*measurements};
34 if (measurements->empty()){
35 return StatusCode::SUCCESS;
36 }
38 do {
39 const Identifier chambId = xAOD::identify(prdViewer.at(0));
40 const IdentifierHash viewHash = m_idHelperSvc->detElementHash(chambId);
42 decorHandle(*prdViewer.at(0)) = LinkType{};
43
45 if ((simHitViewer.size() == 0 || m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) > viewHash) &&
46 !simHitViewer.loadView(chambId)) {
47 ATH_MSG_DEBUG("No simHit view for " << m_idHelperSvc->toStringDetEl(chambId));
48 continue;
49 } else if (m_idHelperSvc->detElementHash(simHitViewer.at(0)->identify()) < viewHash \
50 && !simHitViewer.next([viewHash, this](const xAOD::MuonSimHit* hit){
51 return m_idHelperSvc->detElementHash(hit->identify()) == viewHash;
52 })) {
53 continue;
54 }
55 ATH_MSG_VERBOSE("Container size "<<simHits->size()<<" viewer size: "<<simHitViewer.size()<<" view hash: "<<viewHash);
56 std::unordered_set<Identifier> prds{};
57 for (const xAOD::UncalibratedMeasurement* measurement : prdViewer) {
59 const xAOD::MuonSimHit* bestSimHit{nullptr};
60 switch (measurement->type()) {
63 const Identifier prdId{xAOD::identify(measurement)};
65 std::ranges::find_if(simHitViewer,[&prdId](const xAOD::MuonSimHit* hit){
66 return hit->identify() == prdId;
67 });
68 if (matching_itr != simHitViewer.end()) {
69 bestSimHit =(*matching_itr);
70 }
71 break;
73 prds.clear();
74 const auto* mmHit = static_cast<const xAOD::MMCluster*>(measurement);
75 const MmIdHelper& mmIdHelper{m_idHelperSvc->mmIdHelper()};
76 const int ml = mmIdHelper.multilayer(mmHit->identify());
77 for (const uint16_t strip : mmHit->stripNumbers()) {
78 prds.insert(mmIdHelper.channelID(mmHit->identify(), ml, mmHit->gasGap(), strip));
79 }
81 std::ranges::find_if(simHitViewer,[&prds](const xAOD::MuonSimHit* hit){
82 return prds.count(hit->identify());
83 });
84 if (matching_itr != simHitViewer.end()) {
85 bestSimHit =(*matching_itr);
86 }
87 break;
91 const Identifier prdId{xAOD::identify(measurement)};
92 const MuonGMR4::MuonReadoutElement* readOutEle = xAOD::muonReadoutElement(measurement);
93 const Amg::Transform3D& locToGlob{readOutEle->localToGlobalTrans(*gctx, readOutEle->layerHash(prdId))};
94
95 const Identifier gasGapId = m_idHelperSvc->gasGapId(prdId);
97 Amg::Vector3D locPos{Amg::Vector3D::Zero()};
98 if (measurement->numDimensions() == 1) {
99 locPos = measurement->localPosition<1>().x() * Amg::Vector3D::UnitX();
100 } else {
101 locPos.block<2,1>(0,0) = xAOD::toEigen(measurement->localPosition<2>());
102 }
103 double closestDistance{m_PullCutOff};
104 for ( const xAOD::MuonSimHit* simHit : simHitViewer) {
105 if (gasGapId != m_idHelperSvc->gasGapId(simHit->identify())) {
106 continue;
107 }
108 const IdentifierHash simLayHash{readOutEle->layerHash(simHit->identify())};
109 const Amg::Transform3D globToLoc{readOutEle->globalToLocalTrans(*gctx, simLayHash) *locToGlob};
111 const Amg::Vector3D prdPos = globToLoc * locPos;
113 double dist{0.};
114 if (measurement->numDimensions() == 1) {
115 dist = std::abs(prdPos.x() - simHit->localPosition().x())
116 / std::sqrt(measurement->localCovariance<1>()(0,0));
117 } else{
118 const Amg::Vector2D diff = (prdPos - xAOD::toEigen(simHit->localPosition())).block<2,1>(0,0);
119 dist = std::sqrt(diff.dot(xAOD::toEigen(measurement->localCovariance<2>()).inverse() * diff));
120 }
121 if (dist < closestDistance) {
122 closestDistance = dist;
123 bestSimHit = simHit;
124 }
125 }
126 break;
127 } default: {
128 ATH_MSG_FATAL("Non muon measurement is parsed");
129 return StatusCode::FAILURE;
130 }
131 }
132 if (!bestSimHit) {
133 continue;
134 }
135 decorHandle(*measurement) = LinkType{*simHits, bestSimHit->index()};
136 }
137
138 } while (prdViewer.next());
139
140 return StatusCode::SUCCESS;
141 }
142}
143
#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)
void diff(const Jet &rJet1, const Jet &rJet2, std::map< std::string, double > varDiff)
Difference between jets - Non-Class function required by trigger.
Definition Jet.cxx:631
Handle class for adding a decoration to an object.
#define x
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
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.
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int gasGap, int channel) const
int multilayer(const Identifier &id) const
The MuonReadoutElement is an abstract class representing the geometry representing the muon detector.
const Amg::Transform3D & localToGlobalTrans(const ActsTrk::GeometryContext &ctx) const
Returns the local to global transformation into the ATLAS coordinate system.
virtual IdentifierHash layerHash(const Identifier &measId) const =0
Amg::Transform3D globalToLocalTrans(const ActsTrk::GeometryContext &ctx) const
Transformations to translate between local <-> global coordinates.
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitsKey
Key to the associated simHit container.
Gaudi::Property< double > m_PullCutOff
The number of standard deviations a measurement may be pulled apart in order to be associated.
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::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
Key to the geometry context.
SG::WriteDecorHandleKey< xAOD::UncalibratedMeasurementContainer > m_decorKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
size_t index() const
Return the index of this element within its container.
Handle class for adding a decoration to an object.
bool next() noexcept
Loads the hits from the next chamber.
const_iterator end() const noexcept
End iterator of the current chamber view.
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.
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 2, 1 > Vector2D
Eigen::Matrix< double, 3, 1 > Vector3D
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.
const MuonGMR4::MuonReadoutElement * muonReadoutElement(const UncalibratedMeasurement *meas)
Returns the associated readout element to the measurement.
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.
MMCluster_v1 MMCluster
UncalibratedMeasurementContainer_v1 UncalibratedMeasurementContainer
Define the version of the uncalibrated measurement container.
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.