ATLAS Offline Software
xAODSimHitToTgcMeasCnvAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
10 #include <StoreGate/ReadHandle.h>
12 #include <StoreGate/WriteHandle.h>
13 #include <CLHEP/Random/RandGaussZiggurat.h>
14 #include <GaudiKernel/PhysicalConstants.h>
15 // Random Numbers
17 
18 namespace {
19  constexpr double percentage( unsigned int numerator, unsigned int denom) {
20  return 100. * numerator / std::max(denom, 1u);
21  }
22 
23 }
25  ISvcLocator* pSvcLocator):
26  AthReentrantAlgorithm{name, pSvcLocator} {}
27 
30  ATH_CHECK(m_readKey.initialize());
31  ATH_CHECK(m_writeKey.initialize());
32  ATH_CHECK(m_idHelperSvc.retrieve());
34  return StatusCode::SUCCESS;
35 }
37  ATH_MSG_INFO("Tried to convert "<<m_allHits[0]<<"/"<<m_allHits[1]<<" hits. In, "
38  <<percentage(m_acceptedHits[0], m_allHits[0]) <<"/"
39  <<percentage(m_acceptedHits[1], m_allHits[1]) <<" cases, the conversion was successful");
40  return StatusCode::SUCCESS;
41 }
42 StatusCode xAODSimHitToTgcMeasCnvAlg::execute(const EventContext& ctx) const {
44  if (!simHitContainer.isPresent()){
45  ATH_MSG_FATAL("Failed to retrieve "<<m_readKey.fullKey());
46  return StatusCode::FAILURE;
47  }
48 
50  ATH_CHECK(gctxHandle.isPresent());
51  const ActsGeometryContext& gctx{*gctxHandle};
52 
54  ATH_CHECK(prdContainer.record(std::make_unique<xAOD::TgcStripContainer>(),
55  std::make_unique<xAOD::TgcStripAuxContainer>()));
56 
57  const TgcIdHelper& id_helper{m_idHelperSvc->tgcIdHelper()};
58  CLHEP::HepRandomEngine* rndEngine = getRandomEngine(ctx);
59  auto dumpHit = [&](const Identifier& hitId,
60  const double xLoc,
61  const double xUncert) {
62 
63  xAOD::TgcStrip* prd = new xAOD::TgcStrip();
64  prdContainer->push_back(prd);
65  prd->setIdentifier(hitId.get_compact());
66  xAOD::MeasVector<1> lPos{xLoc};
67  xAOD::MeasMatrix<1> cov{xUncert * xUncert};
68  prd->setMeasurement<1>(m_idHelperSvc->detElementHash(hitId),
69  std::move(lPos), std::move(cov));
70  prd->setChannelNumber(id_helper.channel(hitId));
71  prd->setGasGap(id_helper.gasGap(hitId));
72  const bool measPhi{id_helper.measuresPhi(hitId)};
73  ++(m_acceptedHits[measPhi]);
74  prd->setMeasuresPhi(measPhi);
75  const MuonGMR4::TgcReadoutElement* readOutEle = m_DetMgr->getTgcReadoutElement(hitId);
76  prd->setReadoutElement(readOutEle);
77  };
78 
79  auto processEtaHit = [&] (const Amg::Vector3D& locSimHitPos,
80  const Identifier& hitId) {
81  ++(m_allHits[false]);
82  const MuonGMR4::TgcReadoutElement* readOutEle = m_DetMgr->getTgcReadoutElement(hitId);
83  const unsigned int gasGap = id_helper.gasGap(hitId);
84  const MuonGMR4::WireGroupDesign& design{readOutEle->wireGangLayout(gasGap)};
85  if (!design.insideTrapezoid(locSimHitPos.block<2,1>(0,0))) {
86  ATH_MSG_DEBUG("The hit "<<Amg::toString(locSimHitPos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
87  <<" is outside of the trapezoid "<<design);
88  return;
89  }
90  const int wireGrpNum = design.stripNumber(locSimHitPos.block<2,1>(0,0));
91 
92  if (wireGrpNum < 0) {
93  ATH_MSG_DEBUG("True hit is not "<<Amg::toString(locSimHitPos)<<" "<<m_idHelperSvc->toStringGasGap(hitId));
94  return;
95  }
96 
97  const double uncert = design.stripPitch() * design.numWiresInGroup(wireGrpNum) / std::sqrt(12);
98  const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locSimHitPos.x(), uncert);
101  const Amg::Vector2D smearedPos{locX, locSimHitPos.y()};
102  const int prdWireNum = design.stripNumber(smearedPos);
103 
104  if (prdWireNum < 0) {
105  if (design.insideTrapezoid(smearedPos)) {
106  ATH_MSG_WARNING("True hit "<<Amg::toString(locSimHitPos, 2)<<" corresponding to "<<wireGrpNum<<" --> "
107  <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
108  }
109  return;
110  }
111  bool isValid{false};
112  const Identifier prdId{id_helper.channelID(hitId, gasGap, false, prdWireNum, isValid)};
113  if (!isValid) {
114  ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<prdWireNum);
115  }
116  ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toStringGasGap(hitId)<<" located in gas gap at "
117  <<Amg::toString(locSimHitPos, 2)<<" eta strip number: "<<prdWireNum
118  <<" strip position "<<Amg::toString(design.center(prdWireNum).value_or(Amg::Vector2D::Zero()), 2));
119 
120  dumpHit(prdId, locX, uncert);
121  };
122 
123  auto processStripHit = [&] (const Amg::Vector3D& locSimHitPos,
124  const Identifier& hitId) {
125  ++(m_allHits[true]);
126  const MuonGMR4::TgcReadoutElement* readOutEle = m_DetMgr->getTgcReadoutElement(hitId);
127  const unsigned int gasGap = id_helper.gasGap(hitId);
128  const MuonGMR4::RadialStripDesign& design{readOutEle->stripLayout(gasGap)};
129  if (!design.insideTrapezoid(locSimHitPos.block<2,1>(0,0))) {
130  ATH_MSG_DEBUG("The eta hit "<<Amg::toString(locSimHitPos)<<" in "<<m_idHelperSvc->toStringGasGap(hitId)
131  <<" is outside of the trapezoid "<<design);
132  return;
133  }
134  int stripNum = design.stripNumber(locSimHitPos.block<2,1>(0,0));
135  if (stripNum < 0) {
136  ATH_MSG_WARNING("Strip hit "<<Amg::toString(locSimHitPos)<<" cannot be assigned to any active strip for "
137  <<m_idHelperSvc->toStringGasGap(hitId)<<". "<<design);
138  return;
139  }
140  const double stripPitch = std::abs(*Amg::intersect<2>(design.stripLeftBottom(stripNum), design.stripLeftEdge(stripNum),
141  locSimHitPos.block<2,1>(0,0), design.stripNormal(stripNum))) +
142  std::abs(*Amg::intersect<2>(design.stripRightBottom(stripNum), design.stripRightEdge(stripNum),
143  locSimHitPos.block<2,1>(0,0), design.stripNormal(stripNum)));
144 
145  const double uncert = stripPitch / std::sqrt(12);
146  const double locX = CLHEP::RandGaussZiggurat::shoot(rndEngine, locSimHitPos.x(), uncert);
149  const Amg::Vector2D smearedPos{locX, locSimHitPos.y()};
150  const int prdStripNum = design.stripNumber(smearedPos);
151 
152  if (prdStripNum < 0) {
153  if (design.insideTrapezoid(smearedPos)) {
154  ATH_MSG_WARNING("True phi hit "<<Amg::toString(locSimHitPos, 2)<<" corresponding to "<<stripNum<<" --> "
155  <<Amg::toString(smearedPos)<<" "<<uncert<<" is outside of "<<design);
156  }
157  return;
158  }
159  bool isValid{false};
160  const Identifier prdId{id_helper.channelID(hitId, gasGap, true, prdStripNum, isValid)};
161  if (!isValid) {
162  ATH_MSG_WARNING("Invalid channel "<< m_idHelperSvc->toStringGasGap(hitId)<<", channel: "<<prdStripNum);
163  }
164  ATH_MSG_VERBOSE("Convert simulated hit "<<m_idHelperSvc->toStringGasGap(hitId)<<" located in gas gap at "
165  <<Amg::toString(locSimHitPos, 2)<<" eta strip number: "<<prdStripNum
166  <<" strip position "<<Amg::toString(design.center(prdStripNum).value_or(Amg::Vector2D::Zero()), 2));
167 
168  dumpHit(prdId, locX, uncert);
169  };
170  for (const xAOD::MuonSimHit* simHit : *simHitContainer) {
171  const Identifier hitId = simHit->identify();
172  // ignore radiation for now
173  if (std::abs(simHit->pdgId()) != 13) continue;
174 
175  const MuonGMR4::TgcReadoutElement* readOutEle = m_DetMgr->getTgcReadoutElement(hitId);
176  const Amg::Vector3D locSimHitPos{xAOD::toEigen(simHit->localPosition())};
177  const int gasGap = id_helper.gasGap(hitId);
178 
179  if (readOutEle->numWireGangs(gasGap)) {
180  processEtaHit(locSimHitPos, hitId);
181  }
182  if (readOutEle->numStrips(gasGap)) {
185 
186  const Amg::Transform3D toPhiRot{readOutEle->globalToLocalTrans(gctx, stripHash) *
187  readOutEle->localToGlobalTrans(gctx, wireHash)};
188 
189  processStripHit(toPhiRot * locSimHitPos, hitId);
190  }
191  }
192  return StatusCode::SUCCESS;
193 }
194 
195 CLHEP::HepRandomEngine* xAODSimHitToTgcMeasCnvAlg::getRandomEngine(const EventContext& ctx) const {
196  ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, m_streamName);
197  std::string rngName = name() + m_streamName;
198  rngWrapper->setSeed(rngName, ctx);
199  return rngWrapper->getEngine(ctx);
200 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ATHRNG::RNGWrapper::setSeed
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition: RNGWrapper.h:169
MuonGMR4::TgcReadoutElement::numWireGangs
unsigned int numWireGangs(unsigned int gasGap) const
Returns the number of wire gangs for a given gasGap [1-3].
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
dumpTgcDigiDeadChambers.gasGap
list gasGap
Definition: dumpTgcDigiDeadChambers.py:33
MuonGMR4::TgcReadoutElement::wireGangLayout
const WireGroupDesign & wireGangLayout(unsigned int gasGap) const
Returns access to the wire group design of the given gasGap [1-3] If the gap does not have a wires an...
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
xAOD::TgcStrip_v1::setChannelNumber
void setChannelNumber(uint16_t chan)
xAOD::TgcStrip_v1::setGasGap
void setGasGap(uint8_t gapNum)
MuonGMR4::WireGroupDesign
Definition: WireGroupDesign.h:23
max
#define max(a, b)
Definition: cfImp.cxx:41
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::locX
@ locX
Definition: ParamDefs.h:43
TgcIdHelper
Definition: TgcIdHelper.h:50
xAODSimHitToTgcMeasCnvAlg::execute
StatusCode execute(const EventContext &ctx) const override
Definition: xAODSimHitToTgcMeasCnvAlg.cxx:42
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAOD::TgcStrip_v1
Definition: TgcStrip_v1.h:16
xAODSimHitToTgcMeasCnvAlg.h
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:201
MuonGMR4::MuonReadoutElement::globalToLocalTrans
Amg::Transform3D globalToLocalTrans(const ActsGeometryContext &ctx) const
Transformations to translate between local <-> global coordinates.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MuonReadoutElement.cxx:78
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:214
MuonGMR4::TgcReadoutElement::constructHash
static IdentifierHash constructHash(unsigned int measCh, unsigned int gasGap, const bool isStrip)
Constructs the Hash out of the Identifier fields (channel, gasGap, isStrip)
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
ReadCondHandle.h
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
MuonGMR4::TgcReadoutElement::numStrips
unsigned int numStrips(unsigned int gasGap) const
Returns the number of strips for a given gasGap [1-3].
MuonGMR4::TgcReadoutElement::stripLayout
const RadialStripDesign & stripLayout(unsigned int gasGap) const
Returns access to the strip design of the given gasGap [1-3] If the gap does not have strips an excep...
WriteHandle.h
Handle class for recording to StoreGate.
xAOD::TgcStrip
TgcStrip_v1 TgcStrip
Defined the version of the TgcStrip.
Definition: TgcStrip.h:12
xAOD::UncalibratedMeasurement_v1::setIdentifier
void setIdentifier(const DetectorIdentType measId)
Sets the full Identifier of the measurement.
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
xAODSimHitToTgcMeasCnvAlg::m_readKey
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_readKey
Definition: xAODSimHitToTgcMeasCnvAlg.h:45
xAODSimHitToTgcMeasCnvAlg::initialize
StatusCode initialize() override
Definition: xAODSimHitToTgcMeasCnvAlg.cxx:28
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
MuonChamber.h
xAODSimHitToTgcMeasCnvAlg::getRandomEngine
CLHEP::HepRandomEngine * getRandomEngine(const EventContext &ctx) const
Definition: xAODSimHitToTgcMeasCnvAlg.cxx:195
xAOD::TgcStrip_v1::setMeasuresPhi
void setMeasuresPhi(uint8_t measPhi)
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
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
compute_lumi.denom
denom
Definition: compute_lumi.py:76
xAODSimHitToTgcMeasCnvAlg::m_DetMgr
const MuonGMR4::MuonDetectorManager * m_DetMgr
Access to the new readout geometry.
Definition: xAODSimHitToTgcMeasCnvAlg.h:52
xAOD::MeasVector
Eigen::Matrix< float, N, 1 > MeasVector
Abrivation of the Matrix & Covariance definitions.
Definition: MeasurementDefs.h:52
xAOD::TgcStrip_v1::setReadoutElement
void setReadoutElement(const MuonGMR4::TgcReadoutElement *readoutEle)
set the pointer to the TgcReadoutElement
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ATHRNG::RNGWrapper
A wrapper class for event-slot-local random engines.
Definition: RNGWrapper.h:56
xAODSimHitToTgcMeasCnvAlg::m_writeKey
SG::WriteHandleKey< xAOD::TgcStripContainer > m_writeKey
Definition: xAODSimHitToTgcMeasCnvAlg.h:48
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
ATHRNG::RNGWrapper::getEngine
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition: RNGWrapper.h:134
RNGWrapper.h
xAODSimHitToTgcMeasCnvAlg::xAODSimHitToTgcMeasCnvAlg
xAODSimHitToTgcMeasCnvAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: xAODSimHitToTgcMeasCnvAlg.cxx:24
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
xAOD::UncalibratedMeasurement_v1::setMeasurement
void setMeasurement(const DetectorIDHashType idHash, MeasVector< N > locPos, MeasMatrix< N > locCov)
Sets IdentifierHash, local position and local covariance of the measurement.
Identifier::get_compact
value_type get_compact(void) const
Get the compact id.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonGMR4::MuonReadoutElement::localToGlobalTrans
const Amg::Transform3D & localToGlobalTrans(const ActsGeometryContext &ctx) const
Returns the local to global transformation into the ATLAS coordinate system.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MuonReadoutElement.cxx:81
xAODSimHitToTgcMeasCnvAlg::finalize
StatusCode finalize() override
Definition: xAODSimHitToTgcMeasCnvAlg.cxx:36
xAODSimHitToTgcMeasCnvAlg::m_rndmSvc
ServiceHandle< IAthRNGSvc > m_rndmSvc
Definition: xAODSimHitToTgcMeasCnvAlg.h:56
xAOD::MeasMatrix
Eigen::Matrix< float, N, N > MeasMatrix
Definition: MeasurementDefs.h:54
xAODSimHitToTgcMeasCnvAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: xAODSimHitToTgcMeasCnvAlg.h:54
ReadHandle.h
Handle class for reading from StoreGate.
IdentifierHash
Definition: IdentifierHash.h:38
MuonGMR4::RadialStripDesign
Definition: RadialStripDesign.h:23
TgcReadoutElement.h
TgcStripAuxContainer.h
xAODSimHitToTgcMeasCnvAlg::m_streamName
Gaudi::Property< std::string > m_streamName
Definition: xAODSimHitToTgcMeasCnvAlg.h:57
MuonGMR4::TgcReadoutElement
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/TgcReadoutElement.h:20
xAODSimHitToTgcMeasCnvAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Definition: xAODSimHitToTgcMeasCnvAlg.h:43
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32