ATLAS Offline Software
Loading...
Searching...
No Matches
MuonDigitizationTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5
8#include <GeoModelKernel/throwExcept.h>
9
10namespace MuonR4{
12 ATH_MSG_INFO("SimHitKey "<<m_simHitKey.key()<<", "<<m_streamName<<", SDO container: "<<m_sdoKey.key());
13 if (m_simHitKey.empty() && m_inputObjectName.empty()) {
14 ATH_MSG_FATAL("Property <SimHitKey> not set !");
15 return StatusCode::FAILURE;
16 }
17 if (m_streamName.empty()) {
18 ATH_MSG_FATAL("Property "<<m_streamName<<" not set !");
19 return StatusCode::FAILURE;
20 }
23 ATH_CHECK(m_mergeSvc.retrieve());
24 }
26 ATH_CHECK(m_simHitKey.initialize());
27 ATH_CHECK(m_geoCtxKey.initialize());
28 ATH_CHECK(m_sdoKey.initialize(!m_sdoKey.empty()));
29 ATH_CHECK(m_rndmSvc.retrieve());
30 ATH_CHECK(detStore()->retrieve(m_detMgr));
31 ATH_CHECK(m_idHelperSvc.retrieve());
32 return StatusCode::SUCCESS;
33 }
34
35 StatusCode MuonDigitizationTool::prepareEvent(const EventContext& /*ctx*/,
36 unsigned int nInputEvents) {
37
38 ATH_MSG_DEBUG("prepareEvent() called for " << nInputEvents << " input events");
39 m_timedHits.clear();
40 m_simHits.clear();
41 return StatusCode::SUCCESS;
42 }
43
44 StatusCode MuonDigitizationTool::fillTimedHits(PileUpHits&& hitColl, TimedHits& timedHits) const {
45 for (const auto& [timeIndex, simHitColl] : hitColl) {
46 timedHits.reserve(timedHits.capacity() + simHitColl->size());
47 for (const xAOD::MuonSimHit* simHit : *simHitColl) {
48 timedHits.emplace_back(timeIndex.time(), timeIndex.index(), simHit, timeIndex.type());
49 }
50 }
51 std::stable_sort(timedHits.begin(), timedHits.end(),
52 [](const TimedHit& a, const TimedHit& b){
53 if (a->identify() != b->identify()){
54 return a->identify() < b->identify();
55 }
56 if (a.eventId() != b.eventId()) {
57 return a.eventId() < b.eventId();
58 }
59 return a.eventTime() < b.eventTime();
60 });
61 return StatusCode::SUCCESS;
62 }
64 return hit.eventTime() + hit->globalTime();
65 }
66 StatusCode MuonDigitizationTool::processAllSubEvents(const EventContext& ctx) {
67 const MuonDigitizationTool* digiTool = this;
68 return digiTool->processAllSubEvents(ctx);
69 }
70
71 StatusCode MuonDigitizationTool::processAllSubEvents(const EventContext& ctx) const {
72 PileUpHits hitCollList{};
73 TimedHits timedHits{};
76 const xAOD::MuonSimHitContainer* hitCollection{nullptr};
77 ATH_CHECK(SG::get(hitCollection, m_simHitKey, ctx));
78 hitCollList.emplace_back(PileUpTimeEventIndex(0), hitCollection);
79 } else {
80 ATH_CHECK(m_mergeSvc->retrieveSubEvtsData(m_inputObjectName, hitCollList));
81 }
82 ATH_CHECK(fillTimedHits(std::move(hitCollList), timedHits));
84 if (!m_sdoKey.empty()) {
86 ATH_CHECK(sdoContainer.record(std::make_unique<xAOD::MuonSimHitContainer>(),
87 std::make_unique<xAOD::MuonSimHitAuxContainer>()));
88 }
89 ATH_CHECK(digitize(ctx, timedHits, !m_sdoKey.empty() ? sdoContainer.ptr() : nullptr));
90 return StatusCode::SUCCESS;
91 }
92 StatusCode MuonDigitizationTool::mergeEvent(const EventContext& ctx) {
93 ATH_MSG_DEBUG("mergeEvent()");
94
96 if (!m_sdoKey.empty()) {
98 ATH_CHECK(sdoContainer.record(std::make_unique<xAOD::MuonSimHitContainer>(),
99 std::make_unique<xAOD::MuonSimHitAuxContainer>()));
100 }
101 ATH_CHECK(digitize(ctx, m_timedHits, !m_sdoKey.empty() ? sdoContainer.ptr() : nullptr));
102 m_timedHits.clear();
103 m_simHits.clear();
104 return StatusCode::SUCCESS;
105 }
106
107
108 StatusCode MuonDigitizationTool::processBunchXing(int bunchXing,
109 SubEventIterator bSubEvents,
110 SubEventIterator eSubEvents) {
111 ATH_MSG_DEBUG("processBunchXing()" << bunchXing);
112 PileUpHits hitList{}, hitListPermanent{};
113 ATH_CHECK(m_mergeSvc->retrieveSubSetEvtData(m_inputObjectName, hitList, bunchXing, bSubEvents, eSubEvents));
114 ATH_MSG_VERBOSE(hitList.size() << " hits in xAODMuonSimHitContainer " << m_inputObjectName << " found");
115 for (auto& [hitPtr, hitContainer] : hitList) {
116 auto copyContainer = std::make_unique<xAOD::MuonSimHitContainer>();
117 auto copyAuxContainer = std::make_unique<xAOD::MuonSimHitAuxContainer>();
118 copyContainer->setStore(copyAuxContainer.get());
119 for (const xAOD::MuonSimHit* copyMe : *hitContainer) {
120 (*copyContainer->push_back(std::make_unique<xAOD::MuonSimHit>())) = (*copyMe);
121 }
122 hitListPermanent.emplace_back(hitPtr, copyContainer.get());
123 m_simHits.emplace_back(std::move(copyContainer), std::move(copyAuxContainer));
124 }
125 ATH_CHECK(fillTimedHits(std::move(hitListPermanent), m_timedHits));
126 return StatusCode::SUCCESS;
127 }
128
129 CLHEP::HepRandomEngine* MuonDigitizationTool::getRandomEngine(const EventContext&ctx) const {
130 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, m_streamName);
131 std::string rngName = m_streamName;
132 rngWrapper->setSeed(rngName, ctx);
133 return rngWrapper->getEngine(ctx);
134 }
135 const ActsTrk::GeometryContext& MuonDigitizationTool::getGeoCtx(const EventContext& ctx) const {
136 const ActsTrk::GeometryContext* gctx{};
137 if (!SG::get(gctx, m_geoCtxKey, ctx).isSuccess()) {
138 THROW_EXCEPTION("Failed to retrieve the geometry context "<<m_geoCtxKey.fullKey());
139 }
140 return *gctx;
141 }
143 xAOD::MuonSimHitContainer* sdoContainer) const {
144 if(!sdoContainer) {
145 ATH_MSG_VERBOSE("No SDO container setup of writing");
146 return nullptr;
147 }
149 ATH_MSG_VERBOSE("Hit "<<m_idHelperSvc->toString(hit->identify())<<" is a pile-up truth link");
150 return nullptr;
151 }
152 ATH_MSG_VERBOSE(m_idHelperSvc->toString(hit->identify())<<", pdgID: "<<hit->pdgId()<< " genParticleLink :"<<hit->genParticleLink());
153 ATH_MSG_VERBOSE("Genparticle: "<<hit->genParticleLink());
154 xAOD::MuonSimHit* sdoHit = sdoContainer->push_back(std::make_unique<xAOD::MuonSimHit>());
155 (*sdoHit) = (*hit);
156 static const SG::Accessor<float> acc_eventTime{"MuSim_evtTime"};
157 static const SG::Accessor<unsigned short> acc_eventID{"MuSim_evtID"};
158 static const SG::Accessor<unsigned short> acc_puType{"MuSim_puType"};
159 acc_eventTime(*sdoHit) = hit.eventTime();
160 acc_eventID(*sdoHit) = hit.eventId();
161 acc_puType(*sdoHit) = hit.pileupType();
162 return sdoHit;
163 }
165 const double hitTime,
166 const double deadTimeWindow,
167 DeadTimeMap& deadTimeMap) {
168 auto insertItr = deadTimeMap.insert(std::make_pair(channelId,hitTime));
170 if (insertItr.second) {
171 return true;
172 }
173 if (hitTime - insertItr.first->second < deadTimeWindow) {
174 return false;
175 }
177 insertItr.first->second = hitTime;
178 return true;
179 }
180}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
std::vector< xAOD::EventInfo::SubEvent >::const_iterator SubEventIterator
Definition IPileUpTool.h:22
static Double_t a
if(febId1==febId2)
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition RNGWrapper.h:169
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition RNGWrapper.h:134
value_type push_back(value_type pElem)
Add an element to the end of the collection.
Barebone implementation of the I/O infrastructure for all MuonDigitizationTools.
xAOD::MuonSimHit * addSDO(const TimedHit &hit, xAOD::MuonSimHitContainer *sdoContainer) const
Adds the timed simHit to the output SDO container.
ServiceHandle< PileUpMergeSvc > m_mergeSvc
std::vector< SimHitLocalCopy > m_simHits
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
SG::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
std::vector< TimedHitPtr< xAOD::MuonSimHit > > TimedHits
ServiceHandle< IAthRNGSvc > m_rndmSvc
StatusCode prepareEvent(const EventContext &ctx, const unsigned int) override final
When being run from PileUpToolsAlgs, this method is called at the start of the subevts loop.
Gaudi::Property< bool > m_onlyUseContainerName
StatusCode mergeEvent(const EventContext &ctx) override final
virtual StatusCode digitize(const EventContext &ctx, const TimedHits &hitsToDigit, xAOD::MuonSimHitContainer *sdoContainer) const =0
Digitize the time ordered hits and write them to the digit format specific for the detector technolog...
CLHEP::HepRandomEngine * getRandomEngine(const EventContext &ctx) const
Gaudi::Property< std::string > m_streamName
static bool passDeadTime(const Identifier &channelId, const double hitTime, const double deadTimeWindow, DeadTimeMap &deadTimeMap)
Returns whether the new digit is within the dead time window.
TimedHitPtr< xAOD::MuonSimHit > TimedHit
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitKey
StatusCode processAllSubEvents(const EventContext &ctx) override final
alternative interface which uses the PileUpMergeSvc to obtain all the required SubEvents.
StatusCode processBunchXing(int bunchXing, SubEventIterator bSubEvents, SubEventIterator eSubEvents) override final
StatusCode fillTimedHits(PileUpHits &&hitColl, TimedHits &timedHits) const
Translates the PileUpHits into the timed hits format.
Gaudi::Property< bool > m_includePileUpTruth
const MuonGMR4::MuonDetectorManager * m_detMgr
SG::WriteHandleKey< xAOD::MuonSimHitContainer > m_sdoKey
const ActsTrk::GeometryContext & getGeoCtx(const EventContext &ctx) const
Returns the reference to the ActsTrk::GeometryContext needed to fetch global positions from the Reado...
static double hitTime(const TimedHit &hit)
Returns the global time of the hit which is the sum of eventTime & individual hit time.
PileUpMergeSvc::TimedList< xAOD::MuonSimHitContainer >::type PileUpHits
std::unordered_map< Identifier, double > DeadTimeMap
Gaudi::Property< int > m_vetoPileUpTruthLinks
Helper class to provide type-safe access to aux data.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
int pileupType() const
the type of event which the hit came from (signal, low pt minbias, high pt minbias,...
Definition TimedHitPtr.h:51
unsigned short eventId() const
the index of the component event in PileUpEventInfo.
Definition TimedHitPtr.h:47
float eventTime() const
t0 offset of the bunch xing containing the hit in ns.
Definition TimedHitPtr.h:55
int pdgId() const
Returns the pdgID of the traversing particle.
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
const HepMcParticleLink & genParticleLink() const
Returns the link to the HepMC particle producing this hit.
float globalTime() const
Returns the time ellapsed since the collision of the traversing particle.
bool ignoreTruthLink(const T &p, bool vetoPileUp)
Helper function for SDO creation in PileUpTools.
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.
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
a struct encapsulating the identifier of a pile-up event
#define THROW_EXCEPTION(MESSAGE)
Definition throwExcept.h:10