ATLAS Offline Software
MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "MdtDigitizationTool.h"
9 #include "CLHEP/Random/RandGaussZiggurat.h"
10 namespace{
11  constexpr double timeToTdcCnv = 1. / IMdtCalibrationTool::tdcBinSize;
12 }
13 
14 namespace MuonR4 {
15 
16  MdtDigitizationTool::MdtDigitizationTool(const std::string& type, const std::string& name, const IInterface* pIID):
18 
25  return StatusCode::SUCCESS;
26  }
27  StatusCode MdtDigitizationTool::digitize(const EventContext& ctx,
28  const TimedHits& hitsToDigit,
29  xAOD::MuonSimHitContainer* sdoContainer) const {
30 
31 
32  const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
33  // Prepare the temporary cache
34 
35  using DigitSDOPair = std::pair<std::unique_ptr<MdtDigit>, TimedHit>;
37  const MuonCalib::MdtCalibDataContainer* calibData{nullptr};
38  ATH_CHECK(retrieveConditions(ctx, m_calibDbKey, calibData));
39  const MdtCondDbData* badTubes{nullptr};
40  ATH_CHECK(retrieveConditions(ctx, m_badTubeKey, badTubes));
41  const Muon::TwinTubeMap* twinTubes{nullptr};
42  ATH_CHECK(retrieveConditions(ctx, m_twinTubeKey, twinTubes));
43  CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
44 
45  DigiCache digitCache{};
46 
47  xAOD::ChamberViewer viewer{hitsToDigit, m_idHelperSvc.get()};
48  do {
49  Identifier lastTube{};
50  double deadTime{0.};
51  std::vector<DigitSDOPair> digitsInChamber{};
52 
53  for (const TimedHitPtr<xAOD::MuonSimHit>& simHit : viewer) {
54  const Identifier hitId{simHit->identify()};
56  if (!badTubes->isGood(hitId)) {
57  ATH_MSG_VERBOSE("Hit "<<m_idHelperSvc->toString(hitId)<<" is rejected due to masking in DB.");
58  continue;
59  }
60  const MuonGMR4::MdtReadoutElement* readOutEle = m_detMgr->getMdtReadoutElement(hitId);
61  const IdentifierHash measHash{readOutEle->measurementHash(hitId)};
62  if (m_digitizeMuonOnly && std::abs(simHit->pdgId()) != 13) {
63  ATH_MSG_VERBOSE("Hit is not from a muon");
64  continue;
65  }
66 
67  const Amg::Vector3D locPos{xAOD::toEigen(simHit->localPosition())};
68  const double distRO = readOutEle->distanceToReadout(measHash, locPos);
69 
70  const MdtDigiToolInput digiInput(std::abs(locPos.perp()), distRO, 0., 0., 0., 0., hitId);
71  const MdtDigiToolOutput digiOutput(m_digiTool->digitize(ctx, digiInput, rndEngine));
72  if (!digiOutput.wasEfficient()) {
73  ATH_MSG_VERBOSE("Hit "<<m_idHelperSvc->toString(hitId)<<" is rejected due to inefficiency modelling.");
74  continue;
75  }
76  const double arrivalTime{simHit->globalTime()};
77 
78  const MuonCalib::MdtFullCalibData* tubeConstants = calibData->getCalibData(hitId, msgStream());
79  assert(tubeConstants != nullptr);
80  assert(tubeConstants->tubeCalib.get() != nullptr);
81  assert(tubeConstants->tubeCalib->getCalib(hitId) != nullptr);
82 
83  const MuonCalib::MdtTubeCalibContainer::SingleTubeCalib& tubeCalib{*tubeConstants->tubeCalib->getCalib(hitId)};
84 
85  const double sigPropTime = tubeCalib.inversePropSpeed*distRO;
86  ATH_MSG_VERBOSE(m_idHelperSvc->toString(hitId)<<" "<<Amg::toString(locPos)<<" distance to readout: "<<distRO<<" --> "<<sigPropTime);
89  const double totalTdcTime = digiOutput.driftTime() + arrivalTime + tubeCalib.t0 + sigPropTime;
90  if (lastTube != hitId || deadTime < totalTdcTime) {
91  lastTube = hitId;
92  deadTime = totalTdcTime + m_deadTime;
93  } else {
94  ATH_MSG_VERBOSE("Hit "<<m_idHelperSvc->toString(hitId)<<" has been produced within dead time");
95  continue;
96  }
98 
99  const bool hasHPTdc = m_idHelperSvc->hasHPTDC(hitId);
101  const uint16_t tdcCounts = timeToTdcCnv*(hasHPTdc ? 4 : 1)*CLHEP::RandGaussZiggurat::shoot(rndEngine, totalTdcTime, m_timeResTDC);
102  const uint16_t adcCounts = (hasHPTdc ? 4 : 1) *CLHEP::RandGaussZiggurat::shoot(rndEngine, digiOutput.adc(), m_timeResADC);
103 
104  auto digit = std::make_unique<MdtDigit>(hitId, tdcCounts, adcCounts, false);
105  ATH_MSG_VERBOSE("Add digit "<<m_idHelperSvc->toString(digit->identify())<<", tdc: "<<digit->tdc()<<", adc: "<<digit->adc());
106  digitsInChamber.push_back(std::make_pair(std::move(digit), simHit));
108  if (!twinTubes || !twinTubes->isTwinTubeLayer(hitId)) {
109  continue;
110  }
111  const Identifier twinId{twinTubes->twinId(hitId)};
112  if (twinId == hitId) {
113  ATH_MSG_VERBOSE("The hit "<<m_idHelperSvc->toString(hitId)<<" has no twins.");
114  continue;
115  }
116  const IdentifierHash twinHash{readOutEle->measurementHash(twinId)};
117  if (!readOutEle->isValid(twinHash)) {
118  ATH_MSG_VERBOSE("Reject "<<m_idHelperSvc->toString(twinId)<<" as there's no tube ");
119  continue;
120  }
122  const double twinDist = readOutEle->activeTubeLength(measHash) - distRO +
123  readOutEle->activeTubeLength(twinHash);
124 
125  ATH_MSG_VERBOSE("Twin hit :"<<m_idHelperSvc->toString(twinId)<< ", local Z: "<<simHit->localPosition().z()
126  <<", tube length: "<<readOutEle->activeTubeLength(measHash)<<", distRO: "<<distRO
127  <<", twin length: "<<readOutEle->activeTubeLength(twinHash)
128  <<", twin distance: "<<twinDist);
129  const MuonCalib::MdtTubeCalibContainer::SingleTubeCalib& twinCalib{*tubeConstants->tubeCalib->getCalib(twinId)};
130 
131  const double twinPropTime = tubeCalib.inversePropSpeed*twinDist;
134  const double twinTdcTime = digiOutput.driftTime() + arrivalTime + twinPropTime
135  + twinCalib.t0 + twinTubes->hvDelayTime(twinId);
136 
137  const uint16_t twinTdcCounts = timeToTdcCnv*(hasHPTdc ? 4 : 1)*CLHEP::RandGaussZiggurat::shoot(rndEngine, twinTdcTime, m_resTwin);
138  const uint16_t twinAdcCoutns = (hasHPTdc ? 4 : 1) *CLHEP::RandGaussZiggurat::shoot(rndEngine, digiOutput.adc(), m_timeResADC);
139  digit = std::make_unique<MdtDigit>(twinId, twinTdcCounts, twinAdcCoutns, false);
140  ATH_MSG_VERBOSE("Add twin digit "<<m_idHelperSvc->toString(digit->identify())<<", tdc: "<<digit->tdc()
141  <<", adc: "<<digit->adc()<<", local z: "<<simHit->localPosition().z());
142  digitsInChamber.push_back(std::make_pair(std::move(digit), simHit));
143  }
146  std::ranges::sort(digitsInChamber,[](const DigitSDOPair&a, const DigitSDOPair& b){
147  if (a.first->identify() != b.first->identify()) {
148  return a.first->identify() < b.first->identify();
149  }
150  return a.first->tdc() < b.first->tdc();
151  });
152 
153  for (std::vector<DigitSDOPair>::iterator saveMe = digitsInChamber.begin(); saveMe != digitsInChamber.end() ; ) {
154  addSDO(saveMe->second, sdoContainer);
155  const MdtDigit* saved = fetchCollection(saveMe->first->identify(), digitCache)->push_back(std::move(saveMe->first));
156  const uint16_t deadInterval = saved->tdc() + timeToTdcCnv*(m_idHelperSvc->hasHPTDC(saved->identify()) ?4 : 1)*m_deadTime;
158  saveMe = std::find_if(saveMe +1, digitsInChamber.end(),
159  [deadInterval, saved](const DigitSDOPair& digitized) {
160  return saved->identify() != digitized.first->identify() || deadInterval < saved->tdc();
161  });
162  }
163  } while (viewer.next());
165  ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache), idHelper.module_hash_max()));
166  return StatusCode::SUCCESS;
167  }
168 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
MdtDigiToolInput
Definition: MdtDigiToolInput.h:26
MuonR4::MdtDigitizationTool::m_deadTime
Gaudi::Property< double > m_deadTime
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:42
COOLRates.deadTime
deadTime
Definition: COOLRates.py:1220
MdtDigit
Definition: MdtDigit.h:19
MuonR4::MdtDigitizationTool::m_badTubeKey
SG::ReadCondHandleKey< MdtCondDbData > m_badTubeKey
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:34
MuonGMR4::MdtReadoutElement::measurementHash
static IdentifierHash measurementHash(unsigned int layerNumber, unsigned int tubeNumber)
Transform the layer and tube number to the measurementHash.
IMdtCalibrationTool.h
MuonGMR4::MdtReadoutElement::activeTubeLength
double activeTubeLength(const IdentifierHash &hash) const
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MdtReadoutElement.cxx:173
MdtCondDbData
Definition: MdtCondDbData.h:21
MuonR4::MdtDigitizationTool::m_timeResTDC
Gaudi::Property< double > m_timeResTDC
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:40
TimedHitPtr< xAOD::MuonSimHit >
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
checkRpcDigits.digit
digit
Definition: checkRpcDigits.py:186
MuonCalib::MdtFullCalibData
class which holds the full set of calibration constants for a given tube
Definition: MdtFullCalibData.h:15
MuonR4::MuonDigitizationTool::retrieveConditions
StatusCode retrieveConditions(const EventContext &ctx, const SG::ReadCondHandleKey< Container > &key, const Container *&contPtr) const
Helper function to access the conditions data.
MdtDigiToolOutput
Definition: MdtDigiToolOutput.h:19
MuonR4::MdtDigitizationTool::m_timeResADC
Gaudi::Property< double > m_timeResADC
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:41
MuonR4::MdtDigitizationTool::m_calibDbKey
SG::ReadCondHandleKey< MuonCalib::MdtCalibDataContainer > m_calibDbKey
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:30
xAOD::ChamberViewer
Definition: ChamberViewer.h:65
WriteHandle.h
Handle class for recording to StoreGate.
MuonCalib::MdtFullCalibData::tubeCalib
TubeContainerPtr tubeCalib
Definition: MdtFullCalibData.h:22
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
Muon::TwinTubeMap
Definition: TwinTubeMap.h:16
MuonR4::MuonDigitizationTool::getRandomEngine
CLHEP::HepRandomEngine * getRandomEngine(const EventContext &ctx) const
Definition: MuonDigitizationTool.cxx:135
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonR4::MdtDigitizationTool::m_resTwin
Gaudi::Property< double > m_resTwin
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:46
MdtIdHelper
Definition: MdtIdHelper.h:61
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonR4::MdtDigitizationTool::initialize
StatusCode initialize() override final
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.cxx:19
MdtDigiToolOutput::wasEfficient
double wasEfficient() const
Definition: MdtDigiToolOutput.h:29
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
MuonR4::MdtDigitizationTool::DigiCache
OutDigitCache_t< MdtDigitCollection > DigiCache
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:49
MuonR4::MuonDigitizationTool::addSDO
xAOD::MuonSimHit * addSDO(const TimedHit &hit, xAOD::MuonSimHitContainer *sdoContainer) const
Adds the timed simHit to the output SDO container.
Definition: MuonDigitizationTool.cxx:148
GeoModel::TransientConstSharedPtr::get
Obj * get()
Get (non-const) access to the underlying object.
Definition: TransientConstSharedPtr.h:17
MuonGMR4::MdtReadoutElement
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MdtReadoutElement.h:22
MuonR4::MdtDigitizationTool::digitize
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...
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.cxx:27
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
MuonGMR4::MdtReadoutElement::distanceToReadout
double distanceToReadout(const ActsGeometryContext &ctx, const Identifier &measId, const Amg::Vector3D &globPoint) const
Returns the distance along the wire from the readout card The distance is given as the delta z of the...
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
MuonR4::MuonDigitizationTool::writeDigitContainer
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.
MdtDigiToolInput.h
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
ChamberViewer.h
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MdtDigitizationTool.h
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::MuonDigitizationTool::initialize
StatusCode initialize() override
Definition: MuonDigitizationTool.cxx:17
MuonDigit::identify
Identifier identify() const
Definition: MuonDigit.h:30
a
TList * a
Definition: liststreamerinfos.cxx:10
MuonR4::MuonDigitizationTool
Barebone implementation of the I/O infrastructure for all MuonDigitizationTools.
Definition: MuonDigitizationTool.h:41
MuonR4::MuonDigitizationTool::TimedHits
std::vector< TimedHitPtr< xAOD::MuonSimHit > > TimedHits
Definition: MuonDigitizationTool.h:71
MuonCalib::MdtCalibDataContainer
Definition: MdtCalibDataContainer.h:20
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
MuonR4::MuonDigitizationTool::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonDigitizationTool.h:142
MuonR4::MdtDigitizationTool::m_digitizeMuonOnly
Gaudi::Property< bool > m_digitizeMuonOnly
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:43
MdtDigiToolOutput::driftTime
double driftTime() const
Definition: MdtDigiToolOutput.h:27
MdtDigiToolOutput::adc
double adc() const
Definition: MdtDigiToolOutput.h:28
MuonR4::MdtDigitizationTool::MdtDigitizationTool
MdtDigitizationTool(const std::string &type, const std::string &name, const IInterface *pIID)
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.cxx:16
MuonR4::MuonDigitizationTool::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: MuonDigitizationTool.h:140
MuonR4::MdtDigitizationTool::m_useTwinTube
Gaudi::Property< bool > m_useTwinTube
Properties to model the Twin tube signals.
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:45
MuonCalib::MdtTubeCalibContainer::SingleTubeCalib
Definition: MdtTubeCalibContainer.h:18
MuonR4::MdtDigitizationTool::m_digiTool
ToolHandle< IMDT_DigitizationTool > m_digiTool
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:38
MuonR4::MdtDigitizationTool::m_twinTubeKey
SG::ReadCondHandleKey< Muon::TwinTubeMap > m_twinTubeKey
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:36
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
MdtDigit::tdc
int16_t tdc() const
Definition: MdtDigit.h:26
IMdtCalibrationTool::tdcBinSize
static constexpr double tdcBinSize
Conversion to go from tdc counts -> drift Time.
Definition: IMdtCalibrationTool.h:38
MuonR4::MuonDigitizationTool::fetchCollection
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 ...
MuonGMR4::MdtReadoutElement::isValid
bool isValid(const IdentifierHash &measHash) const
MuonR4::MdtDigitizationTool::m_writeKey
SG::WriteHandleKey< MdtDigitContainer > m_writeKey
Definition: MuonPhaseII/MuonDigitization/MdtDigitizationR4/src/MdtDigitizationTool.h:28
Identifier
Definition: IdentifierFieldParser.cxx:14