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"
5 #include "CLHEP/Random/RandGaussZiggurat.h"
6 #include "CLHEP/Random/RandFlat.h"
7 
8 namespace {
9  constexpr double percentage(unsigned int numerator, unsigned int denom) {
10  return 100. * numerator / std::max(denom, 1u);
11  }
12 }
13 namespace MuonR4 {
14 
15  MmFastDigiTool::MmFastDigiTool(const std::string& type, const std::string& name, const IInterface* pIID):
17 
23  return StatusCode::SUCCESS;
24  }
26  std::stringstream statstr{};
27  unsigned allHits{0};
28  for (unsigned int g = 0; g < m_allHits.size(); ++g) {
29  allHits += m_allHits[g];
30  statstr<<" *** Layer "<<(g+1)<<" "<<percentage(m_acceptedHits[g],m_allHits[g])
31  <<"% of "<<m_allHits[g]<<std::endl;
32  }
33  if(!allHits) return StatusCode::SUCCESS;
34  ATH_MSG_INFO("Tried to convert "<<allHits<<" hits. Successes rate per layer "<<std::endl<<statstr.str());
35  return StatusCode::SUCCESS;
36  }
37  StatusCode MmFastDigiTool::digitize(const EventContext& ctx,
38  const TimedHits& hitsToDigit,
39  xAOD::MuonSimHitContainer* sdoContainer) const {
40  const MmIdHelper& idHelper{m_idHelperSvc->mmIdHelper()};
41  // Prepare the temporary cache
42  DigiCache digitCache{};
44  const Muon::DigitEffiData* efficiencyMap{nullptr};
45  ATH_CHECK(retrieveConditions(ctx, m_effiDataKey, efficiencyMap));
46  const NswErrorCalibData* errorCalibDB{nullptr};
47  ATH_CHECK(retrieveConditions(ctx, m_uncertCalibKey, errorCalibDB));
48 
49  CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
50  for (const TimedHit& simHit : hitsToDigit) {
51  const Identifier hitId{simHit->identify()};
53  if (std::abs(simHit->pdgId()) != 13) continue;
54 
55  const MuonGMR4::MmReadoutElement* readOutEle = m_detMgr->getMmReadoutElement(hitId);
56  const Amg::Vector2D locPos{xAOD::toEigen(simHit->localPosition()).block<2,1>(0,0)};
57 
59  const unsigned int hitGapInNsw = (idHelper.multilayer(hitId) -1) * 4 + idHelper.gasGap(hitId) -1;
60  ++m_allHits[hitGapInNsw];
61 
62  const MuonGMR4::StripDesign& design{readOutEle->stripLayer(hitId).design()};
63  bool isValid{false};
64 
65  int channelNumber = design.stripNumber(locPos);
66  if(channelNumber<0){
67  if (!design.insideTrapezoid(locPos)) {
68  ATH_MSG_WARNING("Hit "<<m_idHelperSvc->toString(hitId)<<" "<<Amg::toString(locPos)
69  <<" is outside bounds "<<std::endl<<design<<" rejecting it");
70  }
71  continue;
72  }
73  const Identifier clusId = idHelper.channelID(hitId,
74  idHelper.multilayer(hitId),
75  idHelper.gasGap(hitId),
76  channelNumber, isValid);
77 
78  if(!isValid) {
79  ATH_MSG_WARNING("Invalid strip identifier for layer " << m_idHelperSvc->toStringGasGap(hitId)
80  << " channel " << channelNumber << " locPos " << Amg::toString(locPos));
81  continue;
82  }
83 
84  if(efficiencyMap && efficiencyMap->getEfficiency(clusId) < CLHEP::RandFlat::shoot(rndEngine, 0., 1.)){
85  continue;
86  }
87 
88  NswErrorCalibData::Input errorCalibInput{};
89  errorCalibInput.stripId = clusId;
90  errorCalibInput.locTheta = M_PI- simHit->localDirection().theta();
91  errorCalibInput.clusterAuthor=66; // cluster time projection method
92  const double uncert = errorCalibDB->clusterUncertainty(errorCalibInput);
93  ATH_MSG_VERBOSE("mm hit has theta " << errorCalibInput.locTheta / Gaudi::Units::deg << " and uncertainty " << uncert);
94 
95 
98  constexpr int dummyResponseTime = 100;
99  constexpr float dummyDepositedCharge = 66666;
100 
101  const double newLocalX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locPos.x(), uncert);
102  const int newChannel = design.stripNumber(newLocalX * Amg::Vector2D::UnitX());
103  if (newChannel < 0) {
104  continue;
105  }
106 
107  const Identifier digitId = idHelper.channelID(hitId,
108  idHelper.multilayer(hitId),
109  idHelper.gasGap(hitId),
110  newChannel, isValid);
111 
112  if(!isValid) {
113  ATH_MSG_WARNING("Invalid strip identifier for layer " << m_idHelperSvc->toStringGasGap(hitId)
114  << " channel " << newChannel << " locPos " << Amg::toString(locPos));
115  continue;
116  }
130 
139  const double pull = (newLocalX - (*design.center(newChannel)).x()) / (design.stripPitch() * std::cos(design.stereoAngle()));
140  const double w1 = CLHEP::RandFlat::shoot(rndEngine, 0., 0.5 *(1. - pull));
141  const double w2 = 1 - pull -2*w1;
142  const double w3 = pull + w1;
143  MmDigitCollection* outColl = fetchCollection(hitId, digitCache);
144 
145  const Identifier digitIdB = idHelper.channelID(hitId,
146  idHelper.multilayer(hitId),
147  idHelper.gasGap(hitId),
148  newChannel - 1, isValid);
149 
150  if (isValid) {
151  outColl->push_back(std::make_unique<MmDigit>(digitIdB, dummyResponseTime, w1 * dummyDepositedCharge));
152  }
153  outColl->push_back(std::make_unique<MmDigit>(digitId,dummyResponseTime, w2 * dummyDepositedCharge));
154 
155  const Identifier digitIdA = idHelper.channelID(hitId,
156  idHelper.multilayer(hitId),
157  idHelper.gasGap(hitId),
158  newChannel + 1, isValid);
159  if (isValid) {
160  outColl->push_back(std::make_unique<MmDigit>(digitIdA, dummyResponseTime, w3 * dummyDepositedCharge));
161  }
162 
163  addSDO(simHit, sdoContainer);
164  ++m_acceptedHits[hitGapInNsw];
165  }
167  ATH_CHECK(writeDigitContainer(ctx, m_writeKey, std::move(digitCache), idHelper.module_hash_max()));
168  return StatusCode::SUCCESS;
169  }
170 }
MmDigitCollection
Definition: MmDigitCollection.h:18
MuonR4::MmFastDigiTool::initialize
StatusCode initialize() override final
Definition: MmFastDigiTool.cxx:18
NswErrorCalibData
Definition: NswErrorCalibData.h:19
MuonGMR4::MmReadoutElement
Definition: MmReadoutElement.h:20
MmFastDigiTool.h
MuonGMR4::StripDesign
Definition: StripDesign.h:30
MuonR4::MmFastDigiTool::finalize
StatusCode finalize() override final
Definition: MmFastDigiTool.cxx:25
max
#define max(a, b)
Definition: cfImp.cxx:41
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
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)
Definition: AtlasPID.h:214
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:83
MuonR4::MuonDigitizationTool::addSDO
void addSDO(const TimedHit &hit, xAOD::MuonSimHitContainer *sdoContainer) const
Adds the timed simHit to the output SDO container.
Definition: MuonDigitizationTool.cxx:148
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
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::DigiCache
OutDigitCache_t< MmDigitCollection > DigiCache
Definition: MmFastDigiTool.h:33
python.StandardJetMods.pull
pull
Definition: StandardJetMods.py:264
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:581
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
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
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)
MuonR4::MmFastDigiTool::MmFastDigiTool
MmFastDigiTool(const std::string &type, const std::string &name, const IInterface *pIID)
Definition: MmFastDigiTool.cxx:15
MuonGMR4::StripLayer::design
const StripDesign & design() const
Returns the underlying strip design.
MuonR4
The CsvMuonSimHitDumper reads a Simulation Hit container for muons and dumps information to csv files...
Definition: MuonSpacePoint.h:11
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:30
MuonR4::MuonDigitizationTool::TimedHits
std::vector< TimedHitPtr< xAOD::MuonSimHit > > TimedHits
Definition: MuonDigitizationTool.h:60
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:122
MuonR4::MuonDigitizationTool::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: MuonDigitizationTool.h:120
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:37
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