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 }
13 static const SG::Decorator<std::int16_t> dec_phiChannel{"SDO_phiChannel"};
14}
15namespace MuonR4 {
16
19 ATH_CHECK(m_writeKey.initialize());
20 ATH_CHECK(m_effiDataKey.initialize(!m_effiDataKey.empty()));
21 return StatusCode::SUCCESS;
22 }
24 ATH_MSG_INFO("Tried to convert "<<m_allHits[0]<<"/"<<m_allHits[1]<<" hits. In, "
25 <<percentage(m_acceptedHits[0], m_allHits[0]) <<"/"
26 <<percentage(m_acceptedHits[1], m_allHits[1]) <<"% of the cases, the conversion was successful");
27 return StatusCode::SUCCESS;
28 }
29
30 int TgcFastDigiTool::associateBCIdTag(const EventContext& /*ctx*/,
31 const TimedHit& /*timedHit*/) const {
33 }
34
35 bool TgcFastDigiTool::digitizeWireHit(const EventContext& ctx,
36 const TimedHit& timedHit,
37 const Muon::DigitEffiData* efficiencyMap,
38 TgcDigitCollection& outColl,
39 CLHEP::HepRandomEngine* rndEngine,
40 DeadTimeMap& deadTimes) const {
41
42
44 const Identifier hitId = timedHit->identify();
45 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
46 const MuonGMR4::TgcReadoutElement* readOutEle = m_detMgr->getTgcReadoutElement(hitId);
47 const IdentifierHash measHash = readOutEle->measurementHash(hitId);
48 const Amg::Vector3D locSimHitPos{xAOD::toEigen(timedHit->localPosition())};
49 if (!readOutEle->numWireGangs(measHash)) {
50 ATH_MSG_VERBOSE("There're no wires in "<<m_idHelperSvc->toString(hitId)<<" nothing to do");
51 return false;
52 }
53 ++m_allHits[false];
54 if (efficiencyMap && efficiencyMap->getEfficiency(hitId) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
55 ATH_MSG_VERBOSE("Simulated hit "<<Amg::toString(locSimHitPos)
56 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
57 return false;
58 }
59
60 const Amg::Vector2D locPos2D{readOutEle->sensorLayout(measHash)->to2D(locSimHitPos, false)};
61
62 const MuonGMR4::WireGroupDesign& design{readOutEle->wireGangLayout(measHash)};
63 if (!design.insideTrapezoid(locPos2D)) {
64 ATH_MSG_DEBUG("The hit "<<Amg::toString(locPos2D)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
65 <<" is outside of the trapezoid "<<design);
66 return false;
67 }
68 const int wireGrpNum = design.stripNumber(locPos2D);
69
70 if (wireGrpNum < 0) {
71 ATH_MSG_DEBUG("True hit "<<Amg::toString(locPos2D)<<" "<<m_idHelperSvc->toStringGasGap(hitId)
72 <<" is not covered by any wire gang");
73 return false;
74 }
75
76 const double uncert = design.stripPitch() * design.numWiresInGroup(wireGrpNum) / std::sqrt(12);
77 const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locPos2D.x(), uncert);
80 const Amg::Vector2D smearedPos{locX, locPos2D.y()};
81 const int prdWireNum = design.stripNumber(smearedPos);
82
83 if (prdWireNum < 0) {
84 if (design.insideTrapezoid(smearedPos)) {
85 ATH_MSG_WARNING("True hit "<<Amg::toString(locPos2D, 2)<<" corresponding to "<<wireGrpNum<<" --> "
86 <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
87 }
88 return false;
89 }
90 bool isValid{false};
91 const Identifier digitId{idHelper.channelID(hitId, readOutEle->gasGapNumber(measHash), false, prdWireNum, isValid)};
92 if (!isValid) {
93 ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<prdWireNum);
94 return false;
95 }
96
97 if (!passDeadTime(digitId, hitTime(timedHit), m_deadTime, deadTimes)) {
98 ATH_MSG_VERBOSE("Reject hit due to dead map constraint");
99 return false;
100 }
101 ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toString(digitId)<<" located at "
102 <<Amg::toString(locSimHitPos, 2)<<" wire group number: "<<prdWireNum<<", time: "<<hitTime(timedHit)
103 <<" wiregroup pos "<<Amg::toString(design.center(prdWireNum).value_or(Amg::Vector2D::Zero()), 2));
104 outColl.push_back(std::make_unique<TgcDigit>(digitId, associateBCIdTag(ctx, timedHit)));
105 ++m_acceptedHits[false];
106 return true;
107 }
108 bool TgcFastDigiTool::digitizeStripHit(const EventContext& ctx,
109 const TimedHit& timedHit,
110 const Muon::DigitEffiData* efficiencyMap,
111 TgcDigitCollection& outColl,
112 CLHEP::HepRandomEngine* rndEngine,
113 DeadTimeMap& deadTimes) const {
114
115 const Identifier hitId = timedHit->identify();
116 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
117 const MuonGMR4::TgcReadoutElement* readOutEle = m_detMgr->getTgcReadoutElement(hitId);
118
119 const IdentifierHash measHash = readOutEle->measurementHash(hitId);
120 if (!readOutEle->numStrips(measHash)) {
121 ATH_MSG_DEBUG("There're no strips in "<<m_idHelperSvc->toString(hitId)<<" nothing to do");
122 return false;
123 }
124 ++(m_allHits[true]);
125
127 if (efficiencyMap && efficiencyMap->getEfficiency(hitId) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
128 ATH_MSG_VERBOSE("Simulated hit "<<xAOD::toEigen(timedHit->localPosition())
129 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
130 return false;
131 }
132
133
134 const Amg::Vector2D locSimHitPos{readOutEle->sensorLayout(measHash)->to2D(xAOD::toEigen(timedHit->localPosition()),true)};
135
136
137 const MuonGMR4::RadialStripDesign& design{readOutEle->stripLayout(measHash)};
138 if (!design.insideTrapezoid(locSimHitPos)) {
139 ATH_MSG_DEBUG("The eta hit "<<Amg::toString(locSimHitPos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
140 <<" is outside of the trapezoid "<<std::endl<<design);
141 return false;
142 }
143 int stripNum = design.stripNumber(locSimHitPos);
144 if (stripNum < 0) {
145 ATH_MSG_WARNING("Strip hit "<<Amg::toString(locSimHitPos)<<" cannot be assigned to any active strip for "
146 <<m_idHelperSvc->toStringGasGap(hitId)<<". "<<design);
147 return false;
148 }
150 const Amg::Vector2D stripNorm{design.stripNormal(stripNum)};
151
154 const double stripPitch = std::abs(*Amg::intersect<2>(design.stripLeftBottom(stripNum), design.stripLeftEdge(stripNum),
155 locSimHitPos, stripNorm)) +
156 std::abs(*Amg::intersect<2>(design.stripRightBottom(stripNum), design.stripRightEdge(stripNum),
157 locSimHitPos, stripNorm));
158
159 const double uncert = stripPitch / std::sqrt(12);
160 const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locSimHitPos.x(), uncert);
163 const Amg::Vector2D smearedPos{locX, locSimHitPos.y()};
164 const int digitStripNum = design.stripNumber(smearedPos);
165
166 if (digitStripNum < 0) {
167 if (design.insideTrapezoid(smearedPos)) {
168 ATH_MSG_WARNING("True phi hit "<<Amg::toString(locSimHitPos, 2)<<" corresponding to "<<stripNum<<" --> "
169 <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
170 }
171 return false;
172 }
173
174 bool isValid{false};
175 const Identifier digitId{idHelper.channelID(hitId, readOutEle->gasGapNumber(measHash), true, digitStripNum, isValid)};
176 if (!isValid) {
177 ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<digitStripNum);
178 return false;
179 }
180
181 if (!passDeadTime(digitId, hitTime(timedHit), m_deadTime, deadTimes)) {
182 ATH_MSG_VERBOSE("Reject hit due to dead time constraint.");
183 return false;
184 }
185 ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toString(digitId)<<" located at "
186 <<Amg::toString(locSimHitPos, 2)<<" phi strip number: "<<digitStripNum<<", time: "<<hitTime(timedHit)
187 <<" strip position "<<Amg::toString(design.center(digitStripNum).value_or(Amg::Vector2D::Zero()), 2));
188
189 outColl.push_back(std::make_unique<TgcDigit>(digitId, associateBCIdTag(ctx, timedHit)));
190
191 ++(m_acceptedHits[true]);
192 return true;
193 }
194 StatusCode TgcFastDigiTool::digitize(const EventContext& ctx,
195 const TimedHits& hitsToDigit,
196 xAOD::MuonSimHitContainer* sdoContainer) const {
197
198 // Prepare the temporary cache
199 DigiCache digitCache{};
201 const Muon::DigitEffiData* efficiencyMap{nullptr};
202 ATH_CHECK(SG::get( efficiencyMap, m_effiDataKey, ctx));
203 const TgcIdHelper& idHelper{m_idHelperSvc->tgcIdHelper()};
204
205 CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
206
207 xAOD::ChamberViewer viewer{hitsToDigit, m_idHelperSvc.get()};
208 do {
209 DeadTimeMap deadTimes{};
210 for (const TimedHit& simHit : viewer) {
212 if (m_digitizeMuonOnly && !MC::isMuon(simHit)) {
213 continue;
214 }
215 TgcDigitCollection* outColl = fetchCollection(simHit->identify(), digitCache);
216
217 const bool digitizedPhi = digitizeStripHit(ctx, simHit, efficiencyMap,*outColl, rndEngine, deadTimes);
218 std::int16_t phiChannel = digitizedPhi ? idHelper.channel(outColl->back()->identify()) : -1;
219 const bool digitizedEta = digitizeWireHit(ctx,simHit, efficiencyMap,*outColl, rndEngine, deadTimes);
220
221 if (digitizedEta || digitizedPhi) {
222 xAOD::MuonSimHit* sdo = addSDO(simHit, sdoContainer);
223 sdo->setIdentifier(outColl->back()->identify());
224 dec_phiChannel(*sdo) = phiChannel;
225 }
226 }
227 } while(viewer.next());
229 ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache), idHelper.module_hash_max()));
230 return StatusCode::SUCCESS;
231 }
232}
#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.