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
9#include <GeoModelKernel/throwExcept.h>
10
13
14namespace MuonR4{
16 ATH_MSG_INFO("SimHitKey "<<m_simHitKey.key()<<", "<<m_streamName<<", SDO container: "<<m_sdoKey.key());
17 if (m_simHitKey.empty() && m_inputObjectName.empty()) {
18 ATH_MSG_FATAL("Property <SimHitKey> not set !");
19 return StatusCode::FAILURE;
20 }
21 if (m_streamName.empty()) {
22 ATH_MSG_FATAL("Property "<<m_streamName<<" not set !");
23 return StatusCode::FAILURE;
24 }
27 ATH_CHECK(m_mergeSvc.retrieve());
28 }
30 ATH_CHECK(m_simHitKey.initialize());
31 ATH_CHECK(m_geoCtxKey.initialize());
32 ATH_CHECK(m_sdoKey.initialize(!m_sdoKey.empty()));
33 ATH_CHECK(m_rndmSvc.retrieve());
34 ATH_CHECK(detStore()->retrieve(m_detMgr));
35 ATH_CHECK(m_idHelperSvc.retrieve());
36 return StatusCode::SUCCESS;
37 }
38
39 StatusCode MuonDigitizationTool::prepareEvent(const EventContext& /*ctx*/,
40 unsigned int nInputEvents) {
41
42 ATH_MSG_DEBUG("prepareEvent() called for " << nInputEvents << " input events");
43 m_timedHits.clear();
44 m_simHits.clear();
45 return StatusCode::SUCCESS;
46 }
47
48 StatusCode MuonDigitizationTool::fillTimedHits(PileUpHits&& hitColl, TimedHits& timedHits) const {
49 for (const auto& [timeIndex, simHitColl] : hitColl) {
50 timedHits.reserve(timedHits.capacity() + simHitColl->size());
51 for (const xAOD::MuonSimHit* simHit : *simHitColl) {
52 timedHits.emplace_back(timeIndex.time(), timeIndex.index(), simHit, timeIndex.type());
53 }
54 }
55 std::stable_sort(timedHits.begin(), timedHits.end(),
56 [](const TimedHit& a, const TimedHit& b){
57 if (a->identify() != b->identify()){
58 return a->identify() < b->identify();
59 }
60 if (a.eventId() != b.eventId()) {
61 return a.eventId() < b.eventId();
62 }
63 return a.eventTime() < b.eventTime();
64 });
65 return StatusCode::SUCCESS;
66 }
68 return hit.eventTime() + hit->globalTime();
69 }
70 StatusCode MuonDigitizationTool::processAllSubEvents(const EventContext& ctx) {
71 const MuonDigitizationTool* digiTool = this;
72 return digiTool->processAllSubEvents(ctx);
73 }
74
75 StatusCode MuonDigitizationTool::processAllSubEvents(const EventContext& ctx) const {
76 PileUpHits hitCollList{};
77 TimedHits timedHits{};
80 const xAOD::MuonSimHitContainer* hitCollection{nullptr};
81 ATH_CHECK(SG::get(hitCollection, m_simHitKey, ctx));
82 hitCollList.emplace_back(PileUpTimeEventIndex(0), hitCollection);
83 } else {
84 ATH_CHECK(m_mergeSvc->retrieveSubEvtsData(m_inputObjectName, hitCollList));
85 }
86 ATH_CHECK(fillTimedHits(std::move(hitCollList), timedHits));
87 SDOFiller_t sdoContainer{};
88 ATH_CHECK(digitize(ctx, timedHits, !m_sdoKey.empty() ? sdoContainer.get() : nullptr));
89 ATH_CHECK(sdoContainer.record(m_sdoKey, ctx));
90 return StatusCode::SUCCESS;
91 }
92 StatusCode MuonDigitizationTool::mergeEvent(const EventContext& ctx) {
93 ATH_MSG_DEBUG("mergeEvent()");
94
95 SDOFiller_t sdoContainer{};
96 ATH_CHECK(digitize(ctx, m_timedHits, !m_sdoKey.empty() ? sdoContainer.get() : nullptr));
97 ATH_CHECK(sdoContainer.record(m_sdoKey, ctx));
98 m_timedHits.clear();
99 m_simHits.clear();
100 return StatusCode::SUCCESS;
101 }
102
103
104 StatusCode MuonDigitizationTool::processBunchXing(int bunchXing,
105 SubEventIterator bSubEvents,
106 SubEventIterator eSubEvents) {
107 ATH_MSG_DEBUG("processBunchXing()" << bunchXing);
108 PileUpHits hitList{}, hitListPermanent{};
109 ATH_CHECK(m_mergeSvc->retrieveSubSetEvtData(m_inputObjectName, hitList, bunchXing, bSubEvents, eSubEvents));
110 ATH_MSG_VERBOSE(hitList.size() << " hits in xAODMuonSimHitContainer " << m_inputObjectName << " found");
111 for (auto& [hitPtr, hitContainer] : hitList) {
112 auto copyContainer = std::make_unique<xAOD::MuonSimHitContainer>();
113 auto copyAuxContainer = std::make_unique<xAOD::MuonSimHitAuxContainer>();
114 copyContainer->setStore(copyAuxContainer.get());
115 for (const xAOD::MuonSimHit* copyMe : *hitContainer) {
116 (*copyContainer->push_back(std::make_unique<xAOD::MuonSimHit>())) = (*copyMe);
117 }
118 hitListPermanent.emplace_back(hitPtr, copyContainer.get());
119 m_simHits.emplace_back(std::move(copyContainer), std::move(copyAuxContainer));
120 }
121 ATH_CHECK(fillTimedHits(std::move(hitListPermanent), m_timedHits));
122 return StatusCode::SUCCESS;
123 }
124
125 CLHEP::HepRandomEngine* MuonDigitizationTool::getRandomEngine(const EventContext&ctx) const {
126 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, m_streamName);
127 std::string rngName = m_streamName;
128 rngWrapper->setSeed(rngName, ctx);
129 return rngWrapper->getEngine(ctx);
130 }
131 const ActsTrk::GeometryContext& MuonDigitizationTool::getGeoCtx(const EventContext& ctx) const {
132 const ActsTrk::GeometryContext* gctx{};
133 if (!SG::get(gctx, m_geoCtxKey, ctx).isSuccess()) {
134 THROW_EXCEPTION("Failed to retrieve the geometry context "<<m_geoCtxKey.fullKey());
135 }
136 return *gctx;
137 }
139 xAOD::MuonSimHitContainer* sdoContainer) const {
140 if(!sdoContainer) {
141 ATH_MSG_VERBOSE("No SDO container setup of writing");
142 return nullptr;
143 }
145 ATH_MSG_VERBOSE("Hit "<<m_idHelperSvc->toString(hit->identify())<<" is a pile-up truth link");
146 return nullptr;
147 }
148 ATH_MSG_VERBOSE(m_idHelperSvc->toString(hit->identify())<<", pdgID: "<<hit->pdgId()<< " genParticleLink :"<<hit->genParticleLink());
149 ATH_MSG_VERBOSE("Genparticle: "<<hit->genParticleLink());
150 xAOD::MuonSimHit* sdoHit = sdoContainer->push_back(std::make_unique<xAOD::MuonSimHit>());
151 (*sdoHit) = (*hit);
152 static const SG::Accessor<float> acc_eventTime{"MuSim_evtTime"};
153 static const SG::Accessor<unsigned short> acc_eventID{"MuSim_evtID"};
154 static const SG::Accessor<unsigned short> acc_puType{"MuSim_puType"};
155 acc_eventTime(*sdoHit) = hit.eventTime();
156 acc_eventID(*sdoHit) = hit.eventId();
157 acc_puType(*sdoHit) = hit.pileupType();
158 return sdoHit;
159 }
161 const double hitTime,
162 const double deadTimeWindow,
163 DeadTimeMap& deadTimeMap) {
164 auto insertItr = deadTimeMap.insert(std::make_pair(channelId,hitTime));
166 if (insertItr.second) {
167 return true;
168 }
169 if (hitTime - insertItr.first->second < deadTimeWindow) {
170 return false;
171 }
173 insertItr.first->second = hitTime;
174 return true;
175 }
176}
#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
xAOD::FillContainer< xAOD::MuonSimHitContainer, xAOD::MuonSimHitAuxContainer > SDOFiller_t
if(pathvar)
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.
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
StatusCode record(const SG::WriteHandleKey< Cont_t > &key, const EventContext &ctx)
Record the container to store gate using the passed write handle key.
Cont_t * get() const
get operator
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.
MuonSimHitAuxContainer_v1 MuonSimHitAuxContainer
Define the version of the pixel cluster container.
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
a struct encapsulating the identifier of a pile-up event
#define THROW_EXCEPTION(MESSAGE)
Definition throwExcept.h:10