ATLAS Offline Software
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 
13 #include "StoreGate/ReadHandle.h"
14 #include "StoreGate/WriteHandle.h"
16 
17 #include <ranges>
18 
19 namespace L0Muon
20 {
21 
23  {
24  ATH_MSG_DEBUG("Initializing " << name() << "...");
25 
28 
29  ATH_CHECK(m_keyRpcRdo.initialize());
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
48  SG::WriteHandle outputCands(m_outputCandKey, ctx);
49  ATH_CHECK(outputCands.record(std::make_unique<L0Muon::RPCCandDataContainer>()));
50 
51  if (m_useTruth)
52  {
53  ATH_CHECK(buildFromTruth(*outputCands, ctx));
54  }
55  else if (m_usePatterns)
56  {
57  // ATH_CHECK(buildFromPatterns(outputCands, ctx));
58  }
59  else
60  {
61  ATH_MSG_ERROR("No valid option selected for building candidates.");
62  return StatusCode::FAILURE;
63  }
64 
65  SG::ReadHandle inputRDO(m_keyRpcRdo, ctx);
66  ATH_CHECK(inputRDO.isPresent());
67  ATH_MSG_DEBUG("Number of RPC RDO: " << inputRDO->size());
68 
70  if (!m_monTool.empty())
71  {
72  auto n_of_RDO = Monitored::Scalar<unsigned int>("n_of_RDO", inputRDO->size());
73  }
74 
75  return StatusCode::SUCCESS;
76  }
77  std::vector<const xAOD::MuonSimHit*>
79  const EventContext& ctx) const {
80 
82  using SegLinkVec_t = std::vector<SegLink_t>;
83 
85  std::vector<const xAOD::MuonSimHit*> rpcHits{};
86 
87  for (const SegLink_t& link : acc_link(truthPart)) {
88  const xAOD::MuonSegment* segment{*link};
89  if (!Muon::MuonStationIndex::isBarrel(segment->chamberIndex())){
90  continue;
91  }
92  auto simHits = MuonR4::getMatchingSimHits(*segment);
93  std::ranges::copy_if(simHits, std::back_inserter(rpcHits),
94  [this](const xAOD::MuonSimHit* hit){
95  return m_idHelperSvc->isRpc(hit->identify());
96  });
97  }
98  std::ranges::sort(rpcHits, [](const xAOD::MuonSimHit* a, const xAOD::MuonSimHit* b){
99  return a->identify() < b->identify();
100  });
101  return rpcHits;
102  }
103 
105  const EventContext &ctx) const
106  {
107  const ActsTrk::GeometryContext* geoContextHandle{nullptr};
108  ATH_CHECK(SG::get(geoContextHandle, m_geoCtxKey, ctx));
109  const ActsTrk::GeometryContext& gctx{*geoContextHandle};
111  const RpcIdHelper& id_helper{m_idHelperSvc->rpcIdHelper()};
113  const xAOD::TruthParticleContainer *truthParticles = nullptr;
114  ATH_CHECK(SG::get(truthParticles, m_truthPartKey, ctx));
115 
117  for (const xAOD::TruthParticle* truthMuon : *truthParticles) {
118  std::vector<const xAOD::MuonSimHit*> rpcHits = collectHits(*truthMuon, ctx);
119  if (rpcHits.empty()) {
120  continue;
121  }
122 
123  ATH_MSG_DEBUG("Found a muon with pdgId: " << truthMuon->pdgId());
124  const auto muonP4 = truthMuon->p4();
125 
126  // Create a new candidate
127  float eta = truthMuon->eta();
128  float phi = truthMuon->phi();
129  float pt = truthMuon->pt();
131  uint8_t charge = truthMuon->charge() < 0 ? 0 : 1;
132 
135  uint16_t subdetectorId = eta > 0 ? 0x65 : 0x66;
136  auto cand = std::make_unique<L0Muon::RPCCandData>(subdetectorId, 0, 0);
137 
138  cand->setEta(eta);
139  cand->setPhi(phi);
140  cand->setPt(pt);
141  cand->setThreshold(0);
142  cand->setCharge(charge);
143  cand->setMdtFlag(0);
144 
145  std::array<float, 4> zPos{};
146  std::array<int, 4> nZPos{};
148  for (const xAOD::MuonSimHit* hit : rpcHits) {
150 
151  // get the hit identifier and the strip position
152  const Identifier id = hit->identify();
153 
154  const MuonGMR4::RpcReadoutElement* detEl = m_detMgr->getRpcReadoutElement(id);
155 
156  float hitPosZ = detEl->stripPosition(gctx,id).z();
157  switch (m_idHelperSvc->stationIndex(id)) {
159  case BI: {
160  zPos[0] += hitPosZ;
161  ++nZPos[0];
162  break;
163  } case BM: {
164  const int dR = id_helper.doubletR(id);
165  zPos[dR] += hitPosZ;
166  ++nZPos[dR];
167  break;
168  } case BO: {
169  zPos[3] += hitPosZ;
170  ++nZPos[3];
171  break;
172  } default :{
173  ATH_MSG_WARNING("Unknown RPC station: " << m_idHelperSvc->toString(hit->identify()));
174  break;
175  }
176  }
177  }
178  // Set the Z positions in the candidate
179  for (int i = 0; i < 4; ++i) {
180  if (nZPos[i] > 0) {
181  zPos[i] = std::abs(zPos[i]); // Use absolute value for Z position
182  zPos[i] /= nZPos[i];
183  // Normalize the Z position to the range of 12 bits
184  // The range is from 0 to +12500, so we map it to 0-4095
185  cand->setZPos(static_cast<uint16_t>(zPos[i]/
187  }
188  }
189  cand->setQuality(L0Muon::RPCCandData::Quality::Q_BEST);
190  outputCands.push_back(std::move(cand));
191  }
192  // Implementation of building candidates from truth
193  return StatusCode::SUCCESS;
194  }
195 
196 
197 } // end of namespace
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
MuonSimHitHelpers.h
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
xAOD::MuonSimHit_v1::identify
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
Definition: xAODMuonSimHit_V1.cxx:42
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:553
L0Muon::RPCSimulation::m_truthPartKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthPartKey
truth containers
Definition: RPCSimulation.h:58
RPCSimulation.h
L0Muon::RPCSimulation::m_monTool
ToolHandle< GenericMonitoringTool > m_monTool
Definition: RPCSimulation.h:55
L0Muon::RPCSimulation::m_geoCtxKey
SG::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
Definition: RPCSimulation.h:66
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
MuonRoIAuxContainer.h
L0Muon::RPCSimulation::m_outputCandKey
SG::WriteHandleKey< L0Muon::RPCCandDataContainer > m_outputCandKey
Output Trigger candidates.
Definition: RPCSimulation.h:53
L0Muon::RPCCandData::Quality::Q_BEST
@ Q_BEST
test_pyathena.pt
pt
Definition: test_pyathena.py:11
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
L0Muon::RPCSimulation::collectHits
std::vector< const xAOD::MuonSimHit * > collectHits(const xAOD::TruthParticle &truthPart, const EventContext &ctx) const
Definition: RPCSimulation.cxx:78
MuonGMR4::RpcReadoutElement
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/RpcReadoutElement.h:17
RpcIdHelper
Definition: RpcIdHelper.h:51
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
MuonSegmentContainer.h
L0Muon::RPCSimulation::buildFromTruth
StatusCode buildFromTruth(L0Muon::RPCCandDataContainer &outputCands, const EventContext &ctx) const
build the candidates from the MC truth
Definition: RPCSimulation.cxx:104
L0Muon::RPCCandData::s_zPosBitRange
static constexpr uint16_t s_zPosBitRange
12 bits for z position
Definition: RPCCandData.h:41
L0Muon
Definition: L0MuonSmearingAlg.cxx:13
L0Muon::RPCSimulation::initialize
virtual StatusCode initialize() override
Definition: RPCSimulation.cxx:22
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::ReadDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:283
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
L0Muon::RPCSimulation::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
helper service
Definition: RPCSimulation.h:64
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
L0Muon::RPCSimulation::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: RPCSimulation.h:67
AnalysisUtils::copy_if
Out copy_if(In first, const In &last, Out res, const Pred &p)
Definition: IFilterUtils.h:30
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
L0Muon::RPCSimulation::m_useTruth
Gaudi::Property< bool > m_useTruth
configuration options
Definition: RPCSimulation.h:46
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
ActsTrk::GeometryContext
Definition: GeometryContext.h:28
MuonR4::SegLink_t
ElementLink< MuonR4::SegmentContainer > SegLink_t
Abrivation of the link to the reco segment container
Definition: xAODSegmentCnvAlg.cxx:19
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
L0Muon::RPCSimulation::m_segmentLinkKey
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer > m_segmentLinkKey
Definition: RPCSimulation.h:60
Muon::MuonStationIndex::StIndex
StIndex
enum to classify the different station layers in the muon spectrometer
Definition: MuonStationIndex.h:23
L0Muon::RPCSimulation::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: RPCSimulation.cxx:43
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
charge
double charge(const T &p)
Definition: AtlasPID.h:997
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
Muon::MuonStationIndex::isBarrel
bool isBarrel(const ChIndex index)
Returns true if the chamber index points to a barrel chamber.
python.TruthMuonD3PDObject.truthMuon
truthMuon
Definition: TruthMuonD3PDObject.py:51
L0Muon::RPCCandData::s_zPosRange
static constexpr float s_zPosRange
range of the RPC hits z positions
Definition: RPCCandData.h:37
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
MuonR4::SegLinkVec_t
std::vector< SegLink_t > SegLinkVec_t
Definition: MeasurementMarkerAlg.cxx:15
python.CaloAddPedShiftConfig.default
default
Definition: CaloAddPedShiftConfig.py:43
a
TList * a
Definition: liststreamerinfos.cxx:10
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
L0Muon::RPCSimulation::m_keyRpcRdo
SG::ReadHandleKey< xAOD::NRPCRDOContainer > m_keyRpcRdo
RPC Rdo.
Definition: RPCSimulation.h:50
ReadDecorHandle.h
Handle class for reading a decoration on an object.
SG::VarHandleBase::isPresent
bool isPresent() const
Is the referenced object present in SG?
Definition: StoreGate/src/VarHandleBase.cxx:400
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
ReadHandle.h
Handle class for reading from StoreGate.
MuonR4::getMatchingSimHits
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
Definition: MuonSimHitHelpers.cxx:38
HepMCHelpers.h
MuonGMR4::RpcReadoutElement::stripPosition
Amg::Vector3D stripPosition(const ActsTrk::GeometryContext &ctx, const Identifier &measId) const
Returns the position of the strip center.
L0Muon::RPCSimulation::m_usePatterns
Gaudi::Property< bool > m_usePatterns
Definition: RPCSimulation.h:47
Identifier
Definition: IdentifierFieldParser.cxx:14