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)<<" located at "
105 <<Amg::toString(locSimHitPos, 2)<<" wire group number: "<<prdWireNum<<", time: "<<hitTime(timedHit)
106 <<" wiregroup pos "<<Amg::toString(design.center(prdWireNum).value_or(Amg::Vector2D::Zero()), 2));
107 outColl.push_back(std::make_unique<TgcDigit>(digitId, associateBCIdTag(ctx, timedHit)));
108 ++m_acceptedHits[false];
109 return true;
110 }
111 bool TgcFastDigiTool::digitizeStripHit(const EventContext& ctx,
112 const TimedHit& timedHit,
113 const Muon::DigitEffiData* efficiencyMap,
114 TgcDigitCollection& outColl,
115 CLHEP::HepRandomEngine* rndEngine,
116 DeadTimeMap& deadTimes) const {
117
118 const Identifier hitId = timedHit->identify();
119 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
120 const MuonGMR4::TgcReadoutElement* readOutEle = m_detMgr->getTgcReadoutElement(hitId);
121
122 const IdentifierHash measHash = readOutEle->measurementHash(hitId);
123 if (!readOutEle->numStrips(measHash)) {
124 ATH_MSG_DEBUG("There're no strips in "<<m_idHelperSvc->toString(hitId)<<" nothing to do");
125 return false;
126 }
127 ++(m_allHits[true]);
128
130 if (efficiencyMap && efficiencyMap->getEfficiency(hitId) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
131 ATH_MSG_VERBOSE("Simulated hit "<<xAOD::toEigen(timedHit->localPosition())
132 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
133 return false;
134 }
135
136
137 const Amg::Vector2D locSimHitPos{readOutEle->sensorLayout(measHash)->to2D(xAOD::toEigen(timedHit->localPosition()),true)};
138
139
140 const MuonGMR4::RadialStripDesign& design{readOutEle->stripLayout(measHash)};
141 if (!design.insideTrapezoid(locSimHitPos)) {
142 ATH_MSG_DEBUG("The eta hit "<<Amg::toString(locSimHitPos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
143 <<" is outside of the trapezoid "<<std::endl<<design);
144 return false;
145 }
146 int stripNum = design.stripNumber(locSimHitPos);
147 if (stripNum < 0) {
148 ATH_MSG_WARNING("Strip hit "<<Amg::toString(locSimHitPos)<<" cannot be assigned to any active strip for "
149 <<m_idHelperSvc->toStringGasGap(hitId)<<". "<<design);
150 return false;
151 }
153 const Amg::Vector2D stripNorm{design.stripNormal(stripNum)};
154
157 const double stripPitch = std::abs(*Amg::intersect<2>(design.stripLeftBottom(stripNum), design.stripLeftEdge(stripNum),
158 locSimHitPos, stripNorm)) +
159 std::abs(*Amg::intersect<2>(design.stripRightBottom(stripNum), design.stripRightEdge(stripNum),
160 locSimHitPos, stripNorm));
161
162 const double uncert = stripPitch / std::sqrt(12);
163 const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locSimHitPos.x(), uncert);
166 const Amg::Vector2D smearedPos{locX, locSimHitPos.y()};
167 const int digitStripNum = design.stripNumber(smearedPos);
168
169 if (digitStripNum < 0) {
170 if (design.insideTrapezoid(smearedPos)) {
171 ATH_MSG_WARNING("True phi hit "<<Amg::toString(locSimHitPos, 2)<<" corresponding to "<<stripNum<<" --> "
172 <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
173 }
174 return false;
175 }
176
177 bool isValid{false};
178 const Identifier digitId{idHelper.channelID(hitId, readOutEle->gasGapNumber(measHash), true, digitStripNum, isValid)};
179 if (!isValid) {
180 ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<digitStripNum);
181 return false;
182 }
183
184 if (!passDeadTime(digitId, hitTime(timedHit), m_deadTime, deadTimes)) {
185 ATH_MSG_VERBOSE("Reject hit due to dead time constraint.");
186 return false;
187 }
188 ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toString(digitId)<<" located at "
189 <<Amg::toString(locSimHitPos, 2)<<" phi strip number: "<<digitStripNum<<", time: "<<hitTime(timedHit)
190 <<" strip position "<<Amg::toString(design.center(digitStripNum).value_or(Amg::Vector2D::Zero()), 2));
191
192 outColl.push_back(std::make_unique<TgcDigit>(digitId, associateBCIdTag(ctx, timedHit)));
193
194 ++(m_acceptedHits[true]);
195 return true;
196 }
197 StatusCode TgcFastDigiTool::digitize(const EventContext& ctx,
198 const TimedHits& hitsToDigit,
199 xAOD::MuonSimHitContainer* sdoContainer) const {
200
201 // Prepare the temporary cache
202 DigiCache digitCache{};
204 const Muon::DigitEffiData* efficiencyMap{nullptr};
205 ATH_CHECK(SG::get( efficiencyMap, m_effiDataKey, ctx));
206 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
207
208 CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
209
210 xAOD::ChamberViewer viewer{hitsToDigit, m_idHelperSvc.get()};
211 do {
212 DeadTimeMap deadTimes{};
213 for (const TimedHit& simHit : viewer) {
215 if (m_digitizeMuonOnly && !MC::isMuon(simHit)) {
216 continue;
217 }
218 TgcDigitCollection* outColl = fetchCollection(simHit->identify(), digitCache);
219
220 const bool digitizedPhi = digitizeStripHit(ctx, simHit, efficiencyMap,*outColl, rndEngine, deadTimes);
221 std::int16_t phiChannel = digitizedPhi ? idHelper.channel(outColl->back()->identify()) : -1;
222 const bool digitizedEta = digitizeWireHit(ctx,simHit, efficiencyMap,*outColl, rndEngine, deadTimes);
223
224 if (digitizedEta || digitizedPhi) {
225 xAOD::MuonSimHit* sdo = addSDO(simHit, sdoContainer);
226 sdo->setIdentifier(outColl->back()->identify());
227
228 dec_phiChannel(*sdo).clear();
229 dec_etaChannel(*sdo).clear();
230
231 if (digitizedPhi) {
232 dec_phiChannel(*sdo).push_back(phiChannel);
233 }
234 }
235 }
236 } while(viewer.next());
238 ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache), idHelper.module_hash_max()));
239 return StatusCode::SUCCESS;
240 }
241}
#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)
bool isValid(const T &p)
Av: we implement here an ATLAS-sepcific convention: all particles which are 99xxxxx are fine.
Definition AtlasPID.h:878
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
Amg::Vector2D stripNormal(int stripNumber) const
@bief: Returns the vector perpendicular to the stripDir and pointing to the next strip
Amg::Vector2D stripLeftEdge(int stripNumber) const
: Returns the direction of the left edge
Amg::Vector2D stripRightBottom(int stripNumber) const
: Returns the intersecton of the strip right edge at the bottom panel's edge
Amg::Vector2D stripRightEdge(int stripNumber) const
: Returns the direction of the right edge
int stripNumber(const Amg::Vector2D &extPos) const override final
Returns the associated channel number of an external vector.
Amg::Vector2D stripLeftBottom(int stripNumber) const
: Returns the intersection of the left strip edge at the bottom panel's edge
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.
Helper class to provide type-safe access to aux data.
Definition Decorator.h:59
@ 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() noexcept
Loads the hits from the next chamber.
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::optional< double > intersect(const AmgVector(N)&posA, const AmgVector(N)&dirA, const AmgVector(N)&posB, const AmgVector(N)&dirB)
Calculates the point B' along the line B that's closest to a second line A.
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.
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
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.