ATLAS Offline Software
Loading...
Searching...
No Matches
TgcFastDigiTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4#include "TgcFastDigiTool.h"
6#include "CLHEP/Random/RandGaussZiggurat.h"
7#include "CLHEP/Random/RandFlat.h"
9namespace {
10 constexpr double percentage(unsigned int numerator, unsigned int denom) {
11 return 100. * numerator / std::max(denom, 1u);
12 }
14 using ChVec_t = std::vector<std::uint16_t>;
15 static const SG::Decorator<ChVec_t> dec_phiChannel{"SDO_phiChannels"};
16 static const SG::Decorator<ChVec_t> dec_etaChannel{"SDO_etaChannels"};
17}
18namespace MuonR4 {
19
22 ATH_CHECK(m_writeKey.initialize());
23 ATH_CHECK(m_effiDataKey.initialize(!m_effiDataKey.empty()));
24 return StatusCode::SUCCESS;
25 }
27 ATH_MSG_INFO("Tried to convert "<<m_allHits[0]<<"/"<<m_allHits[1]<<" hits. In, "
28 <<percentage(m_acceptedHits[0], m_allHits[0]) <<"/"
29 <<percentage(m_acceptedHits[1], m_allHits[1]) <<"% of the cases, the conversion was successful");
30 return StatusCode::SUCCESS;
31 }
32
33 int TgcFastDigiTool::associateBCIdTag(const EventContext& /*ctx*/,
34 const TimedHit& /*timedHit*/) const {
36 }
37
38 bool TgcFastDigiTool::digitizeWireHit(const EventContext& ctx,
39 const TimedHit& timedHit,
40 const Muon::DigitEffiData* efficiencyMap,
41 TgcDigitCollection& outColl,
42 CLHEP::HepRandomEngine* rndEngine,
43 DeadTimeMap& deadTimes) const {
44
45
47 const Identifier hitId = timedHit->identify();
48 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
49 const MuonGMR4::TgcReadoutElement* readOutEle = m_detMgr->getTgcReadoutElement(hitId);
50 const IdentifierHash measHash = readOutEle->measurementHash(hitId);
51 const Amg::Vector3D locSimHitPos{xAOD::toEigen(timedHit->localPosition())};
52 if (!readOutEle->numWireGangs(measHash)) {
53 ATH_MSG_VERBOSE("There're no wires in "<<m_idHelperSvc->toString(hitId)<<" nothing to do");
54 return false;
55 }
56 ++m_allHits[false];
57 if (efficiencyMap && efficiencyMap->getEfficiency(hitId) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
58 ATH_MSG_VERBOSE("Simulated hit "<<Amg::toString(locSimHitPos)
59 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
60 return false;
61 }
62
63 const Amg::Vector2D locPos2D{readOutEle->sensorLayout(measHash)->to2D(locSimHitPos, false)};
64
65 const MuonGMR4::WireGroupDesign& design{readOutEle->wireGangLayout(measHash)};
66 if (!design.insideTrapezoid(locPos2D)) {
67 ATH_MSG_DEBUG("The hit "<<Amg::toString(locPos2D)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
68 <<" is outside of the trapezoid "<<design);
69 return false;
70 }
71 const int wireGrpNum = design.stripNumber(locPos2D);
72
73 if (wireGrpNum < 0) {
74 ATH_MSG_DEBUG("True hit "<<Amg::toString(locPos2D)<<" "<<m_idHelperSvc->toStringGasGap(hitId)
75 <<" is not covered by any wire gang");
76 return false;
77 }
78
79 const double uncert = design.stripPitch() * design.numWiresInGroup(wireGrpNum) / std::sqrt(12);
80 const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locPos2D.x(), uncert);
83 const Amg::Vector2D smearedPos{locX, locPos2D.y()};
84 const int prdWireNum = design.stripNumber(smearedPos);
85
86 if (prdWireNum < 0) {
87 if (design.insideTrapezoid(smearedPos)) {
88 ATH_MSG_WARNING("True hit "<<Amg::toString(locPos2D, 2)<<" corresponding to "<<wireGrpNum<<" --> "
89 <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
90 }
91 return false;
92 }
93 bool isValid{false};
94 const Identifier digitId{idHelper.channelID(hitId, readOutEle->gasGapNumber(measHash), false, prdWireNum, isValid)};
95 if (!isValid) {
96 ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<prdWireNum);
97 return false;
98 }
99
100 if (!passDeadTime(digitId, hitTime(timedHit), m_deadTime, deadTimes)) {
101 ATH_MSG_VERBOSE("Reject hit due to dead map constraint");
102 return false;
103 }
104 ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toString(digitId)
105 <<", pdgId: "<<timedHit->pdgId()<<", located at "
106 <<Amg::toString(locSimHitPos, 2)<<" wire group number: "<<prdWireNum<<", time: "<<hitTime(timedHit)
107 <<" wiregroup pos "<<Amg::toString(design.center(prdWireNum).value_or(Amg::Vector2D::Zero()), 2));
108 outColl.push_back(std::make_unique<TgcDigit>(digitId, associateBCIdTag(ctx, timedHit)));
109 ++m_acceptedHits[false];
110 return true;
111 }
112 bool TgcFastDigiTool::digitizeStripHit(const EventContext& ctx,
113 const TimedHit& timedHit,
114 const Muon::DigitEffiData* efficiencyMap,
115 TgcDigitCollection& outColl,
116 CLHEP::HepRandomEngine* rndEngine,
117 DeadTimeMap& deadTimes) const {
118
119 const Identifier hitId = timedHit->identify();
120 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
121 const MuonGMR4::TgcReadoutElement* readOutEle = m_detMgr->getTgcReadoutElement(hitId);
122
123 const IdentifierHash measHash = readOutEle->measurementHash(hitId);
124 if (!readOutEle->numStrips(measHash)) {
125 ATH_MSG_DEBUG("There're no strips in "<<m_idHelperSvc->toString(hitId)<<" nothing to do");
126 return false;
127 }
128 ++(m_allHits[true]);
129
131 if (efficiencyMap && efficiencyMap->getEfficiency(hitId) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
132 ATH_MSG_VERBOSE("Simulated hit "<<xAOD::toEigen(timedHit->localPosition())
133 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
134 return false;
135 }
136
137
138 const Amg::Vector2D locSimHitPos{readOutEle->sensorLayout(measHash)->to2D(xAOD::toEigen(timedHit->localPosition()),true)};
139
140
141 const MuonGMR4::RadialStripDesign& design{readOutEle->stripLayout(measHash)};
142 if (!design.insideTrapezoid(locSimHitPos)) {
143 ATH_MSG_DEBUG("The eta hit "<<Amg::toString(locSimHitPos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
144 <<" is outside of the trapezoid "<<std::endl<<design);
145 return false;
146 }
147 int stripNum = design.stripNumber(locSimHitPos);
148 if (stripNum < 0) {
149 ATH_MSG_WARNING("Strip hit "<<Amg::toString(locSimHitPos)<<" cannot be assigned to any active strip for "
150 <<m_idHelperSvc->toStringGasGap(hitId)<<". "<<design);
151 return false;
152 }
155 const double uncert = design.stripPitch(stripNum, locSimHitPos) / std::sqrt(12);
156 const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locSimHitPos.x(), uncert);
159 const Amg::Vector2D smearedPos{locX, locSimHitPos.y()};
160 const int digitStripNum = design.stripNumber(smearedPos);
161
162 if (digitStripNum < 0) {
163 if (design.insideTrapezoid(smearedPos)) {
164 ATH_MSG_WARNING("True phi hit "<<Amg::toString(locSimHitPos, 2)<<" corresponding to "<<stripNum<<" --> "
165 <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
166 }
167 return false;
168 }
169
170 bool isValid{false};
171 const Identifier digitId{idHelper.channelID(hitId, readOutEle->gasGapNumber(measHash), true, digitStripNum, isValid)};
172 if (!isValid) {
173 ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<digitStripNum);
174 return false;
175 }
176
177 if (!passDeadTime(digitId, hitTime(timedHit), m_deadTime, deadTimes)) {
178 ATH_MSG_VERBOSE("Reject hit due to dead time constraint.");
179 return false;
180 }
181 ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toString(digitId)
182 <<", pdgId: "<<timedHit->pdgId()<<", located at "
183 <<Amg::toString(locSimHitPos, 2)<<" phi strip number: "<<digitStripNum<<", time: "<<hitTime(timedHit)
184 <<" strip position "<<Amg::toString(design.center(digitStripNum).value_or(Amg::Vector2D::Zero()), 2));
185
186 outColl.push_back(std::make_unique<TgcDigit>(digitId, associateBCIdTag(ctx, timedHit)));
187
188 ++(m_acceptedHits[true]);
189 return true;
190 }
191 StatusCode TgcFastDigiTool::digitize(const EventContext& ctx,
192 const TimedHits& hitsToDigit,
193 xAOD::MuonSimHitContainer* sdoContainer) const {
194
195 // Prepare the temporary cache
196 DigiCache digitCache{};
198 const Muon::DigitEffiData* efficiencyMap{nullptr};
199 ATH_CHECK(SG::get( efficiencyMap, m_effiDataKey, ctx));
200 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
201
202 CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
203
204 xAOD::ChamberViewer viewer{hitsToDigit, m_idHelperSvc.get()};
205 do {
206 DeadTimeMap deadTimes{};
207 for (const TimedHit& simHit : viewer) {
209 if (m_digitizeMuonOnly && !MC::isMuon(simHit)) {
210 continue;
211 }
212 TgcDigitCollection* outColl = fetchCollection(simHit->identify(), digitCache);
213
214 const bool digitizedPhi = digitizeStripHit(ctx, simHit, efficiencyMap,*outColl, rndEngine, deadTimes);
215 std::int16_t phiChannel = digitizedPhi ? idHelper.channel(outColl->back()->identify()) : -1;
216 const bool digitizedEta = digitizeWireHit(ctx,simHit, efficiencyMap,*outColl, rndEngine, deadTimes);
217
218 if (digitizedEta || digitizedPhi) {
219 xAOD::MuonSimHit* sdo = addSDO(simHit, sdoContainer);
220 sdo->setIdentifier(outColl->back()->identify());
221
222 dec_phiChannel(*sdo).clear();
223 dec_etaChannel(*sdo).clear();
224
225 if (digitizedPhi) {
226 dec_phiChannel(*sdo).push_back(phiChannel);
227 }
228 }
229 }
230 } while(viewer.next());
232 ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache), idHelper.module_hash_max()));
233 return StatusCode::SUCCESS;
234 }
235}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
ATLAS-specific HepMC functions.
const T * back() const
Access the last element in the collection as an rvalue.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
This is a "hash" representation of an Identifier.
Identifier identify() const
Definition MuonDigit.h:30
double stripPitch(int stripNumb) const
Returns the pitch of the radial strip evaluated at the strip center.
int stripNumber(const Amg::Vector2D &extPos) const override final
Returns the associated channel number of an external vector.
CheckVector2D center(int stripNumb) const
Returns the bisector of the strip (Global numbering scheme).
double stripPitch() const
Distance between two adjacent strips.
bool insideTrapezoid(const Amg::Vector2D &extPos) const
Checks whether an external point is inside the trapezoidal area.
const StripLayerPtr & sensorLayout(const IdentifierHash &hash) const
Returns the pointer to the strip layer associated with the gas gap.
unsigned numWireGangs(const IdentifierHash &layHash) const
Returns the number of wire gangs for a given gasGap [1-3].
static unsigned gasGapNumber(const IdentifierHash &measHash)
Unpacks the gas gap number from the measurement hash.
const RadialStripDesign & stripLayout(const IdentifierHash &layHash) const
Returns access to the strip design of the given gasGap [1-3] If the gap does not have strips an excep...
IdentifierHash measurementHash(const Identifier &measId) const override final
Constructs the identifier hash from the full measurement Identifier.
const WireGroupDesign & wireGangLayout(const IdentifierHash &layHash) const
Returns access to the wire group design of the given gasGap [1-3] If the gap does not have a wires an...
unsigned numStrips(const IdentifierHash &layHash) const
Returns the number of strips for a given gasGap [1-3].
int stripNumber(const Amg::Vector2D &pos) const override
Calculates the number of the strip whose center is closest to the given point.
unsigned int numWiresInGroup(unsigned int groupNum) const
Returns the number of wires in a given group.
size_type module_hash_max() const
the maximum hash value
xAOD::MuonSimHit * addSDO(const TimedHit &hit, xAOD::MuonSimHitContainer *sdoContainer) const
Adds the timed simHit to the output SDO container.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
std::vector< TimedHitPtr< xAOD::MuonSimHit > > TimedHits
CLHEP::HepRandomEngine * getRandomEngine(const EventContext &ctx) const
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
DigitColl * fetchCollection(const Identifier &hitId, OutDigitCache_t< DigitColl > &digitCache) const
Helper function that provides fetches the proper DigitCollection from the DigitCache for a given hit ...
const MuonGMR4::MuonDetectorManager * m_detMgr
StatusCode writeDigitContainer(const EventContext &ctx, const SG::WriteHandleKey< DigitCont > &key, OutDigitCache_t< DigitColl > &&digitCache, unsigned int hashMax) const
Helper function to move the collected digits into the final DigitContainer.
static double hitTime(const TimedHit &hit)
Returns the global time of the hit which is the sum of eventTime & individual hit time.
std::unordered_map< Identifier, double > DeadTimeMap
StatusCode finalize() override final
OutDigitCache_t< TgcDigitCollection > DigiCache
StatusCode digitize(const EventContext &ctx, const TimedHits &hitsToDigit, xAOD::MuonSimHitContainer *sdoContainer) const override final
Digitize the time ordered hits and write them to the digit format specific for the detector technolog...
StatusCode initialize() override final
SG::WriteHandleKey< TgcDigitContainer > m_writeKey
bool digitizeStripHit(const EventContext &ctx, const TimedHit &timedHit, const Muon::DigitEffiData *efficiencyMap, TgcDigitCollection &outColl, CLHEP::HepRandomEngine *rndEngine, DeadTimeMap &deadTimes) const
Digitize the strip hit by smearing the truth hit position according to the wire group pitch and then ...
Gaudi::Property< bool > m_digitizeMuonOnly
SG::ReadCondHandleKey< Muon::DigitEffiData > m_effiDataKey
bool digitizeWireHit(const EventContext &ctx, const TimedHit &timedHit, const Muon::DigitEffiData *efficiencyMap, TgcDigitCollection &outColl, CLHEP::HepRandomEngine *rndEngine, DeadTimeMap &deadTimes) const
Digitize the wire hit by smearing the truth hit position according to the wire group pitch and then a...
int associateBCIdTag(const EventContext &ctx, const TimedHit &timedHit) const
: Associates the global bcIdTag to the digit
Gaudi::Property< double > m_deadTime
double getEfficiency(const Identifier &channelId, bool isInnerQ1=false) const
Returns the signal generation efficiency of the sTgc channel.
@ BC_CURRENT
Definition TgcDigit.h:37
int channel(const Identifier &id) const override
Identifier channelID(int stationName, int stationEta, int stationPhi, int gasGap, int isStrip, int channel) const
bool next()
Loads the hits from the next chamber.
int pdgId() const
Returns the pdgID of the traversing particle.
void setIdentifier(const Identifier &id)
Sets the global ATLAS identifier.
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
ConstVectorMap< 3 > localPosition() const
Returns the local postion of the traversing particle.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Matrix< double, 2, 1 > Vector2D
Eigen::Matrix< double, 3, 1 > Vector3D
bool isMuon(const T &p)
This header ties the generic definitions in this package.
SG::Decorator< T, ALLOC > Decorator
Helper class to provide type-safe access to aux data, specialized for JaggedVecElt.
Definition AuxElement.h:576
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12