ATLAS Offline Software
MmFastDigiTool.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 "MmFastDigiTool.h"
6 #include "CLHEP/Random/RandGaussZiggurat.h"
7 #include "CLHEP/Random/RandFlat.h"
8 
9 namespace {
10  constexpr double percentage(unsigned int numerator, unsigned int denom) {
11  return 100. * numerator / std::max(denom, 1u);
12  }
13 }
14 namespace MuonR4 {
15 
16  MmFastDigiTool::MmFastDigiTool(const std::string& type, const std::string& name, const IInterface* pIID):
18 
24  return StatusCode::SUCCESS;
25  }
27  std::stringstream statstr{};
28  unsigned allHits{0};
29  for (unsigned int g = 0; g < m_allHits.size(); ++g) {
30  allHits += m_allHits[g];
31  statstr<<" *** Layer "<<(g+1)<<" "<<percentage(m_acceptedHits[g],m_allHits[g])
32  <<"% of "<<m_allHits[g]<<std::endl;
33  }
34  if(!allHits) return StatusCode::SUCCESS;
35  ATH_MSG_INFO("Tried to convert "<<allHits<<" hits. Successes rate per layer "<<std::endl<<statstr.str());
36  return StatusCode::SUCCESS;
37  }
38  StatusCode MmFastDigiTool::digitize(const EventContext& ctx,
39  const TimedHits& hitsToDigit,
40  xAOD::MuonSimHitContainer* sdoContainer) const {
41  const MmIdHelper& idHelper{m_idHelperSvc->mmIdHelper()};
42  // Prepare the temporary cache
43  DigiCache digitCache{};
45  const Muon::DigitEffiData* efficiencyMap{nullptr};
46  ATH_CHECK(retrieveConditions(ctx, m_effiDataKey, efficiencyMap));
47  const NswErrorCalibData* errorCalibDB{nullptr};
48  ATH_CHECK(retrieveConditions(ctx, m_uncertCalibKey, errorCalibDB));
49 
50  CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
51  xAOD::ChamberViewer viewer{hitsToDigit, m_idHelperSvc.get()};
52  do {
53  DeadTimeMap deadTimes{};
54  for (const TimedHit& simHit : viewer) {
55  if (m_digitizeMuonOnly && std::abs(simHit->pdgId()) != 13){
56  continue;
57  }
58  const Identifier hitId{simHit->identify()};
59 
60  const MuonGMR4::MmReadoutElement* readOutEle = m_detMgr->getMmReadoutElement(hitId);
61  const Amg::Vector2D locPos{xAOD::toEigen(simHit->localPosition()).block<2,1>(0,0)};
62 
64  const unsigned int hitGapInNsw = (idHelper.multilayer(hitId) -1) * 4 + idHelper.gasGap(hitId) -1;
65  ++m_allHits[hitGapInNsw];
66 
67  const MuonGMR4::StripDesign& design{readOutEle->stripLayer(hitId).design()};
68  bool isValid{false};
69 
70  int channelNumber = design.stripNumber(locPos);
71  if(channelNumber<0){
72  if (!design.insideTrapezoid(locPos)) {
73  ATH_MSG_WARNING("Hit "<<m_idHelperSvc->toString(hitId)<<" "<<Amg::toString(locPos)
74  <<" is outside bounds "<<std::endl<<design<<" rejecting it");
75  }
76  continue;
77  }
78  const Identifier clusId = idHelper.channelID(hitId,
79  idHelper.multilayer(hitId),
80  idHelper.gasGap(hitId),
81  channelNumber, isValid);
82 
83  if(!isValid) {
84  ATH_MSG_WARNING("Invalid strip identifier for layer " << m_idHelperSvc->toStringGasGap(hitId)
85  << " channel " << channelNumber << " locPos " << Amg::toString(locPos));
86  continue;
87  }
88 
89  if(efficiencyMap && efficiencyMap->getEfficiency(clusId) < CLHEP::RandFlat::shoot(rndEngine, 0., 1.)){
90  continue;
91  }
92  if (!passDeadTime(clusId, hitTime(simHit), m_deadTime, deadTimes)) {
94  continue;
95  }
96  NswErrorCalibData::Input errorCalibInput{};
97  errorCalibInput.stripId = clusId;
98  errorCalibInput.locTheta = M_PI- simHit->localDirection().theta();
99  errorCalibInput.clusterAuthor=66; // cluster time projection method
100  const double uncert = errorCalibDB->clusterUncertainty(errorCalibInput);
101  ATH_MSG_VERBOSE("mm hit has theta " << errorCalibInput.locTheta / Gaudi::Units::deg << " and uncertainty " << uncert);
102 
103 
106  constexpr int dummyResponseTime = 100;
107  constexpr float dummyDepositedCharge = 66666;
108 
109  const double newLocalX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locPos.x(), uncert);
110  const int newChannel = design.stripNumber(newLocalX * Amg::Vector2D::UnitX());
111  if (newChannel < 0) {
112  continue;
113  }
114 
115  const Identifier digitId = idHelper.channelID(hitId,
116  idHelper.multilayer(hitId),
117  idHelper.gasGap(hitId),
118  newChannel, isValid);
119 
120  if(!isValid) {
121  ATH_MSG_WARNING("Invalid strip identifier for layer " << m_idHelperSvc->toStringGasGap(hitId)
122  << " channel " << newChannel << " locPos " << Amg::toString(locPos));
123  continue;
124  }
138 
147  const double pull = (newLocalX - (*design.center(newChannel)).x()) / (design.stripPitch() * std::cos(design.stereoAngle()));
148  const double w1 = CLHEP::RandFlat::shoot(rndEngine, 0., 0.5 *(1. - pull));
149  const double w2 = 1 - pull -2*w1;
150  const double w3 = pull + w1;
151  MmDigitCollection* outColl = fetchCollection(hitId, digitCache);
152 
153  const Identifier digitIdB = idHelper.channelID(hitId,
154  idHelper.multilayer(hitId),
155  idHelper.gasGap(hitId),
156  newChannel - 1, isValid);
157 
158  if (isValid) {
159  outColl->push_back(std::make_unique<MmDigit>(digitIdB, dummyResponseTime, w1 * dummyDepositedCharge));
160  }
161  outColl->push_back(std::make_unique<MmDigit>(digitId,dummyResponseTime, w2 * dummyDepositedCharge));
162 
163  const Identifier digitIdA = idHelper.channelID(hitId,
164  idHelper.multilayer(hitId),
165  idHelper.gasGap(hitId),
166  newChannel + 1, isValid);
167  if (isValid) {
168  outColl->push_back(std::make_unique<MmDigit>(digitIdA, dummyResponseTime, w3 * dummyDepositedCharge));
169  }
170 
171  xAOD::MuonSimHit* sdoHit = addSDO(simHit, sdoContainer);
172  sdoHit->setIdentifier(clusId);
173  ++m_acceptedHits[hitGapInNsw];
174  }
175  } while(viewer.next());
177  ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache), idHelper.module_hash_max()));
178  return StatusCode::SUCCESS;
179  }
180 }
MmDigitCollection
Definition: MmDigitCollection.h:18
MuonR4::MmFastDigiTool::initialize
StatusCode initialize() override final
Definition: MmFastDigiTool.cxx:19
NswErrorCalibData
Definition: NswErrorCalibData.h:19
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
MuonR4::MuonDigitizationTool::passDeadTime
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.
Definition: MuonDigitizationTool.cxx:169
MuonGMR4::MmReadoutElement
Definition: MmReadoutElement.h:19
MmFastDigiTool.h
MuonGMR4::StripDesign
Definition: StripDesign.h:30
MuonR4::MmFastDigiTool::finalize
StatusCode finalize() override final
Definition: MmFastDigiTool.cxx:26
xAOD::MuonSimHit_v1::setIdentifier
void setIdentifier(const Identifier &id)
Sets the global ATLAS identifier.
Definition: xAODMuonSimHit_V1.cxx:43
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
M_PI
#define M_PI
Definition: ActiveFraction.h:11
deg
#define deg
Definition: SbPolyhedron.cxx:17
TimedHitPtr< xAOD::MuonSimHit >
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
isValid
bool isValid(const T &p)
Av: we implement here an ATLAS-sepcific convention: all particles which are 99xxxxx are fine.
Definition: AtlasPID.h:620
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
x
#define x
MuonR4::MuonDigitizationTool::retrieveConditions
StatusCode retrieveConditions(const EventContext &ctx, const SG::ReadCondHandleKey< Container > &key, const Container *&contPtr) const
Helper function to access the conditions data.
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
xAOD::ChamberViewer
Definition: ChamberViewer.h:65
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
MuonR4::MuonDigitizationTool::getRandomEngine
CLHEP::HepRandomEngine * getRandomEngine(const EventContext &ctx) const
Definition: MuonDigitizationTool.cxx:135
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonR4::MmFastDigiTool::m_digitizeMuonOnly
Gaudi::Property< bool > m_digitizeMuonOnly
Definition: MmFastDigiTool.h:49
MuonR4::MmFastDigiTool::DigiCache
OutDigitCache_t< MmDigitCollection > DigiCache
Definition: MmFastDigiTool.h:33
python.StandardJetMods.pull
pull
Definition: StandardJetMods.py:282
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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:794
MuonR4::MmFastDigiTool::m_effiDataKey
SG::ReadCondHandleKey< Muon::DigitEffiData > m_effiDataKey
Definition: MmFastDigiTool.h:36
compute_lumi.denom
denom
Definition: compute_lumi.py:76
NswErrorCalibData::Input::stripId
Identifier stripId
Identifier of the strip.
Definition: NswErrorCalibData.h:27
MuonGMR4::MmReadoutElement::stripLayer
const StripLayer & stripLayer(const Identifier &measId) const
Muon::DigitEffiData
Definition: DigitEffiData.h:23
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
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
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.
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
ChamberViewer.h
MuonR4::MmFastDigiTool::m_deadTime
Gaudi::Property< double > m_deadTime
Definition: MmFastDigiTool.h:47
MuonR4::MmFastDigiTool::MmFastDigiTool
MmFastDigiTool(const std::string &type, const std::string &name, const IInterface *pIID)
Definition: MmFastDigiTool.cxx:16
MuonGMR4::StripLayer::design
const StripDesign & design() const
Returns the underlying strip design.
MuonR4::MuonDigitizationTool::hitTime
static double hitTime(const TimedHit &hit)
Returns the global time of the hit which is the sum of eventTime & individual hit time.
Definition: MuonDigitizationTool.cxx:69
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::MuonDigitizationTool::initialize
StatusCode initialize() override
Definition: MuonDigitizationTool.cxx:17
MmIdHelper
Definition: MmIdHelper.h:54
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
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonR4::MmFastDigiTool::m_writeKey
SG::WriteHandleKey< MmDigitContainer > m_writeKey
Definition: MmFastDigiTool.h:34
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
MuonR4::MuonDigitizationTool::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonDigitizationTool.h:142
MuonR4::MuonDigitizationTool::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: MuonDigitizationTool.h:140
MuonR4::MuonDigitizationTool::DeadTimeMap
std::unordered_map< Identifier, double > DeadTimeMap
Definition: MuonDigitizationTool.h:129
NswErrorCalibData::Input
Helper struct to be parsed to the object to derive the specific error of the cluster.
Definition: NswErrorCalibData.h:25
MuonR4::MmFastDigiTool::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: MmFastDigiTool.cxx: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 ...
MuonR4::MmFastDigiTool::m_uncertCalibKey
SG::ReadCondHandleKey< NswErrorCalibData > m_uncertCalibKey
Definition: MmFastDigiTool.h:39
Identifier
Definition: IdentifierFieldParser.cxx:14