ATLAS Offline Software
Loading...
Searching...
No Matches
RPCSimulation.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "RPCSimulation.h"
6
9
10
16
17#include <ranges>
18
19namespace L0Muon
20{
21
23 {
24 ATH_MSG_DEBUG("Initializing " << name() << "...");
25
26 ATH_CHECK(m_truthPartKey.initialize());
27 ATH_CHECK(m_segmentLinkKey.initialize());
28
29 ATH_CHECK(m_keyRpcRdo.initialize());
30 ATH_CHECK(m_geoCtxKey.initialize());
31 ATH_CHECK(detStore()->retrieve(m_detMgr));
32 ATH_CHECK(m_idHelperSvc.retrieve());
34 ATH_CHECK(m_outputCandKey.initialize());
35
37 if (!m_monTool.empty())
38 ATH_CHECK(m_monTool.retrieve());
39
40 return StatusCode::SUCCESS;
41 }
42
43 StatusCode RPCSimulation::execute(const EventContext &ctx) const
44 {
45 ATH_MSG_DEBUG("Executing " << name() << "...");
46
47 // output candidates container
49 ATH_CHECK(outputCands_handle.record(std::make_unique<xAOD::RPCCandDataContainer>(), std::make_unique<xAOD::RPCCandDataAuxContainer>()));
50 auto outputCands = outputCands_handle.ptr();
51
52 if (m_useTruth)
53 {
54 ATH_CHECK(buildFromTruth(*outputCands, ctx));
55 }
56 else if (m_usePatterns)
57 {
58 // ATH_CHECK(buildFromPatterns(outputCands, ctx));
59 }
60 else
61 {
62 ATH_MSG_ERROR("No valid option selected for building candidates.");
63 return StatusCode::FAILURE;
64 }
65
66 SG::ReadHandle inputRDO(m_keyRpcRdo, ctx);
67 ATH_CHECK(inputRDO.isPresent());
68 ATH_MSG_DEBUG("Number of RPC RDO: " << inputRDO->size());
69
71 if (!m_monTool.empty())
72 {
73 auto n_of_RDO = Monitored::Scalar<unsigned int>("n_of_RDO", inputRDO->size());
74 }
75
76 return StatusCode::SUCCESS;
77 }
78 std::vector<const xAOD::MuonSimHit*>
80 const EventContext& ctx) const {
81
83 using SegLinkVec_t = std::vector<SegLink_t>;
84
86 std::vector<const xAOD::MuonSimHit*> rpcHits{};
87
88 for (const SegLink_t& link : acc_link(truthPart)) {
89 const xAOD::MuonSegment* segment{*link};
91 continue;
92 }
93 auto simHits = MuonR4::getMatchingSimHits(*segment);
94 std::ranges::copy_if(simHits, std::back_inserter(rpcHits),
95 [this](const xAOD::MuonSimHit* hit){
96 return m_idHelperSvc->isRpc(hit->identify());
97 });
98 }
99 std::ranges::sort(rpcHits, [](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
100 return a->identify() < b->identify();
101 });
102 return rpcHits;
103 }
104
106 const EventContext &ctx) const
107 {
108 const ActsTrk::GeometryContext* geoContextHandle{nullptr};
109 ATH_CHECK(SG::get(geoContextHandle, m_geoCtxKey, ctx));
110 const ActsTrk::GeometryContext& gctx{*geoContextHandle};
112 const RpcIdHelper& id_helper{m_idHelperSvc->rpcIdHelper()};
114 const xAOD::TruthParticleContainer *truthParticles = nullptr;
115 ATH_CHECK(SG::get(truthParticles, m_truthPartKey, ctx));
116
118 for (const xAOD::TruthParticle* truthMuon : *truthParticles) {
119 std::vector<const xAOD::MuonSimHit*> rpcHits = collectHits(*truthMuon, ctx);
120 if (rpcHits.empty()) {
121 continue;
122 }
123
124 ATH_MSG_DEBUG("Found a muon with pdgId: " << truthMuon->pdgId());
125 const auto muonP4 = truthMuon->p4();
126
127 // Create a new candidate
128 float eta = truthMuon->eta();
129 float phi = truthMuon->phi();
130 float pt = truthMuon->pt();
132 uint8_t charge = truthMuon->charge() < 0 ? 0 : 1;
133
136 uint16_t subdetectorId = eta > 0 ? 0x65 : 0x66;
137 auto* cand = outputCands.push_back(std::make_unique<xAOD::RPCCandData>());
138
139 cand->initialize(subdetectorId, 0, 0);
140 cand->setEta(eta);
141 cand->setPhi(phi);
142 cand->setPt(pt);
143 cand->setThreshold(0);
144 cand->setCandCharge(charge);
145 cand->setMdtFlag(0);
146
147 std::array<float, 4> zPos{};
148 std::array<int, 4> nZPos{};
150 for (const xAOD::MuonSimHit* hit : rpcHits) {
152
153 // get the hit identifier and the strip position
154 const Identifier id = hit->identify();
155
156 const MuonGMR4::RpcReadoutElement* detEl = m_detMgr->getRpcReadoutElement(id);
157
158 float hitPosZ = detEl->stripPosition(gctx,id).z();
159 switch (m_idHelperSvc->stationIndex(id)) {
161 case BI: {
162 zPos[0] += hitPosZ;
163 ++nZPos[0];
164 break;
165 } case BM: {
166 const int dR = id_helper.doubletR(id);
167 zPos[dR] += hitPosZ;
168 ++nZPos[dR];
169 break;
170 } case BO: {
171 zPos[3] += hitPosZ;
172 ++nZPos[3];
173 break;
174 } default :{
175 ATH_MSG_WARNING("Unknown RPC station: " << m_idHelperSvc->toString(hit->identify()));
176 break;
177 }
178 }
179 }
180 // Set the Z positions in the candidate
181 for (int i = 0; i < 4; ++i) {
182 if (nZPos[i] > 0) {
183 zPos[i] = std::abs(zPos[i]); // Use absolute value for Z position
184 zPos[i] /= nZPos[i];
185 // Normalize the Z position to the range of 12 bits
186 // The range is from 0 to +12500, so we map it to 0-4095
187
188 //std::cout << "z before setZpos = " <<zPos[i] << std::endl;
189 //cand->setZPos(static_cast<uint16_t>(zPos[i]/
190 // L0Muon::RPCCandData::s_zPosRange*L0Muon::RPCCandData::s_zPosBitRange), i);
191 //std::cout << "z after setZpos = " <<cand->zPos(i)<< std::endl;
192
193 }
194 }
195
196 cand->setZPos(zPos);
197 cand->setCandQuality(xAOD::ICandData_v1::Quality::Q_BEST); // Set to BEST for truth
198 }
199 // Implementation of building candidates from truth
200 return StatusCode::SUCCESS;
201 }
202
203
204} // end of namespace
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
double charge(const T &p)
Definition AtlasPID.h:997
ATLAS-specific HepMC functions.
static Double_t a
Handle class for reading a decoration on an object.
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
std::vector< SegLink_t > SegLinkVec_t
const ServiceHandle< StoreGateSvc > & detStore() const
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthPartKey
truth containers
virtual StatusCode initialize() override
Gaudi::Property< bool > m_usePatterns
SG::WriteHandleKey< xAOD::RPCCandDataContainer > m_outputCandKey
Output Trigger candidates.
Gaudi::Property< bool > m_useTruth
configuration options
StatusCode buildFromTruth(xAOD::RPCCandDataContainer &outputCands, const EventContext &ctx) const
build the candidates from the MC truth
std::vector< const xAOD::MuonSimHit * > collectHits(const xAOD::TruthParticle &truthPart, const EventContext &ctx) const
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_segmentLinkKey
SG::ReadHandleKey< xAOD::NRPCRDOContainer > m_keyRpcRdo
RPC Rdo.
const MuonGMR4::MuonDetectorManager * m_detMgr
ToolHandle< GenericMonitoringTool > m_monTool
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
helper service
SG::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
virtual StatusCode execute(const EventContext &ctx) const override
Declare a monitored scalar variable.
Amg::Vector3D stripPosition(const ActsTrk::GeometryContext &ctx, const Identifier &measId) const
Returns the position of the strip center.
int doubletR(const Identifier &id) const
Handle class for reading a decoration on an object.
bool isPresent() const
Is the referenced object present in SG?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
void initialize(uint16_t subdetectorId, uint16_t sectorId, uint16_t bcTag)
Initialize candidate with basic properties.
::Muon::MuonStationIndex::ChIndex chamberIndex() const
Returns the chamber index.
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
StIndex
enum to classify the different station layers in the muon spectrometer
bool isBarrel(const ChIndex index)
Returns true if the chamber index points to a barrel chamber.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
TruthParticle_v1 TruthParticle
Typedef to implementation.
MuonSegment_v1 MuonSegment
Reference the current persistent version:
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.