ATLAS Offline Software
Loading...
Searching...
No Matches
sTgcFastDigiTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#include "sTgcFastDigiTool.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 using channelType = sTgcIdHelper::sTgcChannelTypes;
14 using ChVec_t = std::vector<std::uint16_t>;
15
16 static const SG::Decorator<ChVec_t> dec_stripCh{"sTgc_stripChannels"};
17 static const SG::Decorator<ChVec_t> dec_wireCh{"sTgc_wireChannels"};
18 static const SG::Decorator<ChVec_t> dec_padCh{"sTgc_padChannels"};
19
20}
21namespace MuonR4 {
22
25 ATH_CHECK(m_writeKey.initialize());
26 ATH_CHECK(m_effiDataKey.initialize(!m_effiDataKey.empty()));
27 ATH_CHECK(m_uncertCalibKey.initialize());
28 return StatusCode::SUCCESS;
29 }
31 ATH_MSG_INFO("Tried to convert "<<m_allHits[channelType::Strip]<<"/"
32 <<m_allHits[channelType::Wire]<<"/"
33 <<m_allHits[channelType::Pad]<<" strip/wire/pad hits. In, "
34 <<percentage(m_acceptedHits[channelType::Strip], m_allHits[channelType::Strip]) <<"/"
35 <<percentage(m_acceptedHits[channelType::Wire], m_allHits[channelType::Wire])<<"/"
36 <<percentage(m_acceptedHits[channelType::Pad], m_allHits[channelType::Pad])
37 <<"% of the cases, the conversion was successful");
38 return StatusCode::SUCCESS;
39 }
40 StatusCode sTgcFastDigiTool::digitize(const EventContext& ctx,
41 const TimedHits& hitsToDigit,
42 xAOD::MuonSimHitContainer* sdoContainer) const {
43 const sTgcIdHelper& idHelper{m_idHelperSvc->stgcIdHelper()};
44 // Prepare the temporary cache
45 DigiCache digitCache{};
47 const Muon::DigitEffiData* efficiencyMap{nullptr};
48 ATH_CHECK(SG::get(efficiencyMap, m_effiDataKey, ctx));
49 const NswErrorCalibData* nswUncertDB{nullptr};
50 ATH_CHECK(SG::get(nswUncertDB, m_uncertCalibKey, ctx));
51
52 CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
53 xAOD::ChamberViewer viewer{hitsToDigit, m_idHelperSvc.get()};
54 do {
55 for (const TimedHit& simHit : viewer) {
57 if (m_digitizeMuonOnly && !MC::isMuon(simHit)){
58 continue;
59 }
60
61 if (simHit->energyDeposit() < m_energyDepositThreshold){
62 ATH_MSG_VERBOSE("Hit with Energy Deposit of " << simHit->energyDeposit()
63 << " less than " << m_energyDepositThreshold << ". Skip this hit." );
64 continue;
65 }
66
67 const double hitKineticEnergy = simHit->kineticEnergy();
68 if (hitKineticEnergy < m_limitElectronKineticEnergy && MC::isElectron(simHit)) {
69 ATH_MSG_DEBUG("Skip electron hit with kinetic energy " << hitKineticEnergy
70 << ", which is less than the lower limit of " << m_limitElectronKineticEnergy);
71 continue;
72 }
73 sTgcDigitCollection* digiColl = fetchCollection(simHit->identify(), digitCache);
74 const bool digitizedPad = digitizePad(ctx, simHit, efficiencyMap, rndEngine, *digiColl);
75 const std::int16_t padChannel = digitizedPad ? idHelper.channel(digiColl->back()->identify()) : -1;
76 const bool digitizedWire = digitizeWire(ctx, simHit, efficiencyMap, rndEngine, *digiColl);
77 const std::int16_t wireChannel = digitizedWire ? idHelper.channel(digiColl->back()->identify()) : -1;
78 const bool digitizedStrip = digitizeStrip(ctx, simHit, nswUncertDB, efficiencyMap, rndEngine, *digiColl);
79 const std::int16_t stripCh = digitizedStrip ? idHelper.channel(digiColl->back()->identify()) : -1;
80
81 if (digitizedStrip || digitizedPad || digitizedWire) {
82 xAOD::MuonSimHit* sdo = addSDO(simHit, sdoContainer);
83 ChVec_t& stripChV{dec_stripCh(*sdo)}, wireCh{dec_wireCh(*sdo)}, padCh{dec_padCh(*sdo)};
84 if (stripCh > 0) { stripChV.push_back(stripCh); }
85 if (wireChannel > 0) { stripChV.push_back(wireChannel); }
86 if (padChannel > 0) { stripChV.push_back(padChannel); }
87 sdo->setIdentifier(digiColl->back()->identify());
88 }
89 }
90 } while (viewer.next());
92 ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache),
93 idHelper.module_hash_max()));
94 return StatusCode::SUCCESS;
95 }
96 bool sTgcFastDigiTool::digitizeStrip(const EventContext& ctx,
97 const TimedHit& timedHit,
98 const NswErrorCalibData* errorCalibDB,
99 const Muon::DigitEffiData* efficiencyMap,
100 CLHEP::HepRandomEngine* rndEngine,
101 sTgcDigitCollection& outCollection) const {
102
103 if (!m_digitizeStrip) {
104 return false;
105 }
106 ++m_allHits[channelType::Strip];
107 const Identifier hitId{timedHit->identify()};
108 const MuonGMR4::sTgcReadoutElement* readOutEle{m_detMgr->getsTgcReadoutElement(hitId)};
109
110 const sTgcIdHelper& idHelper{m_idHelperSvc->stgcIdHelper()};
111
112 const IdentifierHash meashHash = readOutEle->measurementHash(hitId);
113 const MuonGMR4::StripDesign& design{readOutEle->stripDesign(meashHash)};
114
115 const Amg::Vector2D stripPos = readOutEle->stripLayer(meashHash)
116 .to2D(xAOD::toEigen(timedHit->localPosition()), false);
117
118 const int stripNum = design.stripNumber(stripPos);
119 if (stripNum < 0) {
120 ATH_MSG_VERBOSE("Strip hit "<<Amg::toString(stripPos)<<" "<<m_idHelperSvc->toStringGasGap(hitId)
121 <<" is out of range "<<std::endl<<design);
122 return false;
123 }
124
125
126 bool isValid{false};
127 const int gasGap = idHelper.gasGap(hitId);
128 const Identifier stripId = idHelper.channelID(hitId, readOutEle->multilayer(),
129 gasGap, channelType::Strip, stripNum, isValid);
130
131 if (!isValid) {
132 ATH_MSG_WARNING("Failed to deduce a valid identifier from "
133 <<m_idHelperSvc->toStringGasGap(hitId)<<" strip: "<<stripNum);
134 return false;
135 }
136
138 bool isInnerQ1 = readOutEle->isEtaZero(readOutEle->measurementHash(hitId), stripPos);
139 if (efficiencyMap && efficiencyMap->getEfficiency(hitId, isInnerQ1) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
140 ATH_MSG_VERBOSE("Simulated strip hit "<<xAOD::toEigen(timedHit->localPosition())
141 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
142 return false;
143 }
144
145 NswErrorCalibData::Input errorCalibInput{};
146 errorCalibInput.stripId= stripId;
147 errorCalibInput.locTheta = M_PI - timedHit->localDirection().theta();
148 errorCalibInput.clusterAuthor = 3; // centroid
149
150 const double uncert = errorCalibDB->clusterUncertainty(errorCalibInput);
151 const double smearedX = CLHEP::RandGaussZiggurat::shoot(rndEngine, stripPos.x(), uncert);
152
153 const Amg::Vector2D digitPos{smearedX * Amg::Vector2D::UnitX()};
154
155 const int digitStrip = design.stripNumber(digitPos);
156 if (digitStrip < 0) {
157 ATH_MSG_VERBOSE("Smeared strip hit "<<Amg::toString(digitPos)<<" "<<m_idHelperSvc->toStringGasGap(hitId)
158 <<" is out of range "<<std::endl<<design);
159 return false;
160 }
161 const Identifier digitId = idHelper.channelID(hitId, readOutEle->multilayer(),
162 gasGap, channelType::Strip, digitStrip, isValid);
163
164 if (!isValid) {
165 ATH_MSG_WARNING("Failed to deduce a valid identifier from "
166 <<m_idHelperSvc->toStringGasGap(hitId)<<" digit: "<<digitStrip);
167 return false;
168 }
169 constexpr double dummyCharge = 66666;
190 const double pull = (smearedX - (*design.center(digitStrip)).x()) / design.stripPitch();
191 const double w1 = CLHEP::RandFlat::shoot(rndEngine, 0., 0.5 *(1. - pull));
192 const double w2 = 1. - pull -2.*w1;
193 const double w3 = pull + w1;
194
195 if (digitStrip> 1) {
196 const Identifier stripIdB = idHelper.channelID(hitId, readOutEle->multilayer(),
197 gasGap, channelType::Strip, digitStrip -1);
198 outCollection.push_back(std::make_unique<sTgcDigit>(stripIdB,
199 associateBCIdTag(ctx, timedHit),
200 hitTime(timedHit), dummyCharge * w1, false, false));
201 }
202 outCollection.push_back(std::make_unique<sTgcDigit>(digitId,
203 associateBCIdTag(ctx, timedHit),
204 hitTime(timedHit), dummyCharge * w2, false, false));
205
206 if (digitStrip + 1 <= design.numStrips()) {
207 const Identifier stripIdA = idHelper.channelID(hitId, readOutEle->multilayer(),
208 gasGap, channelType::Strip, digitStrip + 1);
209
210 outCollection.push_back(std::make_unique<sTgcDigit>(stripIdA,
211 associateBCIdTag(ctx, timedHit),
212 hitTime(timedHit), dummyCharge * w3, false, false));
213 }
214 ++m_acceptedHits[channelType::Strip];
215 return true;
216 }
217
218 bool sTgcFastDigiTool::digitizeWire(const EventContext& ctx,
219 const TimedHit& timedHit,
220 const Muon::DigitEffiData* efficiencyMap,
221 CLHEP::HepRandomEngine* rndEngine,
222 sTgcDigitCollection& outCollection) const {
223
224 if (!m_digitizeWire) {
225 return false;
226 }
227
228 ++m_allHits[channelType::Wire];
229
230 const Identifier hitId{timedHit->identify()};
232 if (efficiencyMap && efficiencyMap->getEfficiency(hitId) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
233 ATH_MSG_VERBOSE("Simulated wire hit "<<xAOD::toEigen(timedHit->localPosition())
234 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
235 return false;
236 }
237 const sTgcIdHelper& idHelper{m_idHelperSvc->stgcIdHelper()};
238 const MuonGMR4::sTgcReadoutElement* readOutEle = m_detMgr->getsTgcReadoutElement(hitId);
239 const IdentifierHash hitHash = readOutEle->measurementHash(hitId);
240 const int gasGap = idHelper.gasGap(hitId);
241
242 const Amg::Vector2D wirePos = readOutEle->stripLayer(hitHash).to2D(xAOD::toEigen(timedHit->localPosition()), true);
243 // do not digitise wires that are never read out in reality
244 bool isInnerQ1 = readOutEle->isEtaZero(hitHash, wirePos);
245 if(isInnerQ1) {
246 return false;
247 }
249 if (efficiencyMap && efficiencyMap->getEfficiency(hitId, isInnerQ1) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
250 ATH_MSG_VERBOSE("Simulated wire hit "<<xAOD::toEigen(timedHit->localPosition())
251 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
252 return false;
253 }
254
255 const MuonGMR4::WireGroupDesign& design{readOutEle->wireDesign(hitHash)};
256
257 const int wireGrpNum = design.stripNumber(wirePos);
258 if (wireGrpNum < 0) {
259 ATH_MSG_VERBOSE("The wire "<<Amg::toString(wirePos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
260 <<" is outside of the acceptance of "<<std::endl<<design);
261 return false;
262 }
263 const double uncert = design.stripPitch() * design.numWiresInGroup(wireGrpNum);
264
265 const double smearedX = CLHEP::RandGaussZiggurat::shoot(rndEngine, wirePos.x(), uncert);
266
267 const Amg::Vector2D digitPos{smearedX * Amg::Vector2D::UnitX()};
268
269 const int digitWire = design.stripNumber(digitPos);
270 if (digitWire < 0) {
271 ATH_MSG_VERBOSE("Strip hit "<<Amg::toString(digitPos)<<" "<<m_idHelperSvc->toStringGasGap(hitId)
272 <<" is out of range "<<std::endl<<design);
273 return false;
274 }
275 bool isValid{false};
276 const Identifier digitId = idHelper.channelID(hitId, readOutEle->multilayer(),
277 gasGap, channelType::Wire, digitWire, isValid);
278
279 if (!isValid) {
280 ATH_MSG_WARNING("Failed to deduce a valid identifier from "
281 <<m_idHelperSvc->toStringGasGap(hitId)<<" digit: "<<digitWire);
282 return false;
283 }
284 outCollection.push_back(std::make_unique<sTgcDigit>(digitId,
285 associateBCIdTag(ctx, timedHit),
286 hitTime(timedHit), 666, false, false));
287
288
289 ++m_acceptedHits[channelType::Wire];
290 return true;
291 }
292 int sTgcFastDigiTool::associateBCIdTag(const EventContext& /*ctx*/,
293 const TimedHit& /*timedHit*/) const {
295 return 0;
296 }
297
298 bool sTgcFastDigiTool::digitizePad(const EventContext& ctx,
299 const TimedHit& timedHit,
300 const Muon::DigitEffiData* efficiencyMap,
301 CLHEP::HepRandomEngine* rndEngine,
302 sTgcDigitCollection& outCollection) const {
303
304 if (!m_digitizePads) {
305 return false;
306 }
307
308 ++m_allHits[channelType::Pad];
309
310 const Identifier hitId{timedHit->identify()};
311 const sTgcIdHelper& idHelper{m_idHelperSvc->stgcIdHelper()};
312 const MuonGMR4::sTgcReadoutElement* readOutEle = m_detMgr->getsTgcReadoutElement(hitId);
313 const int gasGap = idHelper.gasGap(hitId);
314
315 const IdentifierHash hitHash = readOutEle->createHash(gasGap, sTgcIdHelper::sTgcChannelTypes::Pad, 1);
316
317
318 const Amg::Vector2D padPos{readOutEle->stripLayer(hitHash).to2D(xAOD::toEigen(timedHit->localPosition()), true)};
320 const MuonGMR4::PadDesign& design{readOutEle->padDesign(hitHash)};
321
322 const auto [padEta, padPhi] = design.channelNumber(padPos);
323 if (padEta < 0 || padPhi < 0) {
324 ATH_MSG_VERBOSE("The pad "<<Amg::toString(padPos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
325 <<" is outside of the acceptance of "<<std::endl<<design);
326 return false;
327 }
328 bool isValid{false};
329 const Identifier padId = idHelper.padID(hitId, readOutEle->multilayer(),
330 gasGap, channelType::Pad, padEta, padPhi, isValid);
331
332
333 if (!isValid) {
334 ATH_MSG_WARNING("Failed to decuce a valid pad Identifier from "<<Amg::toString(padPos)
335 <<" in "<<m_idHelperSvc->toStringGasGap(hitId));
336 return false;
337 }
338
340 bool isInnerQ1 = readOutEle->isEtaZero(readOutEle->measurementHash(hitId), padPos);
341 if (efficiencyMap && efficiencyMap->getEfficiency(hitId, isInnerQ1) < CLHEP::RandFlat::shoot(rndEngine,0.,1.)){
342 ATH_MSG_VERBOSE("Simulated pad hit "<<xAOD::toEigen(timedHit->localPosition())
343 << m_idHelperSvc->toString(hitId) <<" is rejected because of efficency modelling");
344 return false;
345 }
346
347 outCollection.push_back(std::make_unique<sTgcDigit>(padId,
348 associateBCIdTag(ctx, timedHit),
349 hitTime(timedHit), 666, false, false));
350
351 ++m_acceptedHits[channelType::Pad];
352
353 return true;
354 }
355
356
357}
#define M_PI
#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.
#define x
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
std::pair< int, int > channelNumber(const Amg::Vector2D &hitPos) const
Function to retrieve the pad eta and phi given a local position coordinate.
CheckVector2D center(int stripNumb) const
Returns the bisector of the strip (Global numbering scheme)
double stripPitch() const
Distance between two adjacent strips.
virtual int stripNumber(const Amg::Vector2D &pos) const
Calculates the number of the strip whose center is closest to the given point.
virtual int numStrips() const
Number of strips on the panel.
Amg::Vector2D to2D(const Amg::Vector3D &vec, const bool phiView) const
Transforms a 3D vector from the strip design into a 2D vector.
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.
IdentifierHash measurementHash(const Identifier &measId) const override final
Constructs the identifier hash from the full measurement Identifier.
const PadDesign & padDesign(const IdentifierHash &measHash) const
Retrieves the readoutElement Layer given the Identifier/Hash.
int multilayer() const
Returns the multilayer of the sTgcReadoutElement.
const StripDesign & stripDesign(const IdentifierHash &measHash) const
Retrieves the readoutElement Layer given the Identifier/Hash.
bool isEtaZero(const IdentifierHash &measurementHash, const Amg::Vector2D &localPosition) const
const WireGroupDesign & wireDesign(const IdentifierHash &measHash) const
Retrieves the readoutElement Layer given the Identifier/Hash.
static IdentifierHash createHash(const unsigned gasGap, const unsigned channelType, const unsigned channel, const unsigned wireInGrp=0)
Create a measurement hash from the Identifier fields.
const StripLayer & stripLayer(const IdentifierHash &measId) const
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
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.
SG::ReadCondHandleKey< Muon::DigitEffiData > m_effiDataKey
StatusCode initialize() override final
Gaudi::Property< double > m_limitElectronKineticEnergy
bool digitizeStrip(const EventContext &ctx, const TimedHit &timedHit, const NswErrorCalibData *errorCalibDB, const Muon::DigitEffiData *effiData, CLHEP::HepRandomEngine *rndEngine, sTgcDigitCollection &outCollection) const
StatusCode finalize() override final
Gaudi::Property< bool > m_digitizeStrip
Gaudi::Property< bool > m_digitizePads
Gaudi::Property< double > m_energyDepositThreshold
Gaudi::Property< bool > m_digitizeMuonOnly
SG::WriteHandleKey< sTgcDigitContainer > m_writeKey
Gaudi::Property< bool > m_digitizeWire
SG::ReadCondHandleKey< NswErrorCalibData > m_uncertCalibKey
bool digitizePad(const EventContext &ctx, const TimedHit &timedHit, const Muon::DigitEffiData *effiData, CLHEP::HepRandomEngine *rndEngine, sTgcDigitCollection &outCollection) const
OutDigitCache_t< sTgcDigitCollection > 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...
bool digitizeWire(const EventContext &ctx, const TimedHit &timedHit, const Muon::DigitEffiData *effiData, CLHEP::HepRandomEngine *rndEngine, sTgcDigitCollection &outCollection) const
int associateBCIdTag(const EventContext &ctx, const TimedHit &timedHit) const
: Associates the global bcIdTag to the digit
double getEfficiency(const Identifier &channelId, bool isInnerQ1=false) const
Returns the signal generation efficiency of the sTgc channel.
double clusterUncertainty(const Input &clustInfo) const
Helper class to provide type-safe access to aux data.
Definition Decorator.h:59
int channel(const Identifier &id) const override
Identifier padID(int stationName, int stationEta, int stationPhi, int multilayer, int gasGap, int channelType, int padEta, int padPhi) const
int gasGap(const Identifier &id) const override
get the hashes
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int gasGap, int channelType, int channel) const
bool next() noexcept
Loads the hits from the next chamber.
ConstVectorMap< 3 > localDirection() const
Returns the local direction 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
bool isElectron(const T &p)
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.
Helper struct to be parsed to the object to derive the specific error of the cluster.
double locTheta
Direction of the muon in the local coordinate frame.
Identifier stripId
Identifier of the strip.
uint8_t clusterAuthor
Author of the cluster.