ATLAS Offline Software
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>
14 namespace 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());
21  return StatusCode::SUCCESS;
22  }
23  StatusCode PrepDataToSimHitAssocAlg::execute(const EventContext & ctx) const {
24  const ActsGeometryContext* 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);
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 
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
xAOD::identify
const Identifier & identify(const UncalibratedMeasurement *meas)
Returns the associated identifier from the muon measurement.
Definition: MuonSpectrometer/MuonPhaseII/Event/xAOD/xAODMuonPrepData/Root/UtilFunctions.cxx:82
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
UtilFunctions.h
xAOD::MuonSimHit_v1::identify
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
Definition: xAODMuonSimHit_V1.cxx:42
xAOD::MMCluster_v1
Definition: MMCluster_v1.h:20
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
MuonR4::PrepDataToSimHitAssocAlg::initialize
StatusCode initialize() override final
Definition: PrepDataToSimHitAssocAlg.cxx:15
xAOD::UncalibMeasType::MMClusterType
@ MMClusterType
MuonGMR4::MuonReadoutElement
The MuonReadoutElement is an abstract class representing the geometry representing the muon detector.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MuonReadoutElement.h:38
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
MuonGMR4::MuonReadoutElement::globalToLocalTrans
Amg::Transform3D globalToLocalTrans(const ActsGeometryContext &ctx) const
Transformations to translate between local <-> global coordinates.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MuonReadoutElement.cxx:78
MuonGMR4::MuonReadoutElement::layerHash
virtual IdentifierHash layerHash(const Identifier &measId) const =0
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::UncalibMeasType::sTgcStripType
@ sTgcStripType
x
#define x
xAOD::UncalibMeasType::TgcStripType
@ TgcStripType
MuonR4::PrepDataToSimHitAssocAlg::m_decorKey
SG::WriteDecorHandleKey< xAOD::UncalibratedMeasurementContainer > m_decorKey
Definition: PrepDataToSimHitAssocAlg.h:35
MuonR4::PrepDataToSimHitAssocAlg::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: PrepDataToSimHitAssocAlg.cxx:23
xAOD::ChamberViewer
Definition: ChamberViewer.h:59
MuonR4::PrepDataToSimHitAssocAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Key to the geometry context.
Definition: PrepDataToSimHitAssocAlg.h:39
xAOD::UncalibratedMeasurement_v1
Definition: UncalibratedMeasurement_v1.h:13
MuonR4::PrepDataToSimHitAssocAlg::m_prdHitKey
SG::ReadHandleKey< xAOD::UncalibratedMeasurementContainer > m_prdHitKey
Key to the uncalibrated measurements to decorate.
Definition: PrepDataToSimHitAssocAlg.h:32
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
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
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
xAOD::ChamberView::Mode::DetElement
@ DetElement
View ends if the detElementHash changes.
WriteDecorHandle.h
Handle class for adding a decoration to an object.
MuonR4::PrepDataToSimHitAssocAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
Definition: PrepDataToSimHitAssocAlg.h:37
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
TgcStrip.h
MuonR4::PrepDataToSimHitAssocAlg::m_PullCutOff
Gaudi::Property< double > m_PullCutOff
The number of standard deviations a measurement may be pulled apart in order to be associated.
Definition: PrepDataToSimHitAssocAlg.h:41
MuonR4::PrepDataToSimHitAssocAlg::m_simHitsKey
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitsKey
Key to the associated simHit container.
Definition: PrepDataToSimHitAssocAlg.h:30
sTgcMeasurement.h
RpcMeasurement.h
ChamberViewer.h
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MmIdHelper
Definition: MmIdHelper.h:54
MuonGMR4::MuonReadoutElement::localToGlobalTrans
const Amg::Transform3D & localToGlobalTrans(const ActsGeometryContext &ctx) const
Returns the local to global transformation into the ATLAS coordinate system.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MuonReadoutElement.cxx:81
PrepDataToSimHitAssocAlg.h
MMCluster.h
xAOD::muonReadoutElement
const MuonGMR4::MuonReadoutElement * muonReadoutElement(const UncalibratedMeasurement *meas)
Returns the associated readout element to the measurement.
Definition: MuonSpectrometer/MuonPhaseII/Event/xAOD/xAODMuonPrepData/Root/UtilFunctions.cxx:40
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
xAOD::UncalibMeasType::RpcStripType
@ RpcStripType
xAOD::UncalibMeasType::MdtDriftCircleType
@ MdtDriftCircleType
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32
Identifier
Definition: IdentifierFieldParser.cxx:14