ATLAS Offline Software
MuonPhaseII/MuonDetDescr/MuonGeoModelTestR4/src/GeoModelMdtTest.cxx
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4 */
5 #include "GeoModelMdtTest.h"
10 
11 #include <fstream>
12 
13 using namespace ActsTrk;
14 
15 namespace MuonGMR4{
16 
17 
19  ATH_CHECK(m_idHelperSvc.retrieve());
20  ATH_CHECK(m_geoCtxKey.initialize());
22  ATH_CHECK(m_tree.init(this));
23 
24  const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
25  auto translateTokenList = [this, &idHelper](const std::vector<std::string>& chNames){
26 
27  std::set<Identifier> transcriptedIds{};
28  for (const std::string& token : chNames) {
29  if (token.size() != 6) {
30  ATH_MSG_WARNING("Wrong format given for "<<token<<". Expecting 6 characters");
31  continue;
32  }
34  const std::string statName = token.substr(0, 3);
35  const unsigned statEta = std::atoi(token.substr(3, 1).c_str()) * (token[4] == 'A' ? 1 : -1);
36  const unsigned statPhi = std::atoi(token.substr(5, 1).c_str());
37  bool isValid{false};
38  const Identifier eleId = idHelper.elementID(statName, statEta, statPhi, isValid);
39  if (!isValid) {
40  ATH_MSG_WARNING("Failed to deduce a station name for " << token);
41  continue;
42  }
43  transcriptedIds.insert(eleId);
44  const Identifier secMlId = idHelper.multilayerID(eleId, 2, isValid);
45  if (isValid){
46  transcriptedIds.insert(secMlId);
47  }
48  }
49  return transcriptedIds;
50  };
51 
52  std::vector <std::string>& selectedSt = m_selectStat.value();
53  const std::vector <std::string>& excludedSt = m_excludeStat.value();
54  selectedSt.erase(std::remove_if(selectedSt.begin(), selectedSt.end(),
55  [&excludedSt](const std::string& token){
56  return std::ranges::find(excludedSt, token) != excludedSt.end();
57  }), selectedSt.end());
58 
59  if (selectedSt.size()) {
60  m_testStations = translateTokenList(selectedSt);
61  std::stringstream sstr{};
62  for (const Identifier& id : m_testStations) {
63  sstr<<" *** "<<m_idHelperSvc->toString(id)<<std::endl;
64  }
65  ATH_MSG_INFO("Test only the following stations "<<std::endl<<sstr.str());
66  } else {
67  const std::set<Identifier> excluded = translateTokenList(excludedSt);
69  for(auto itr = idHelper.detectorElement_begin();
70  itr!= idHelper.detectorElement_end();++itr){
71  if (!excluded.count(*itr)) {
72  m_testStations.insert(*itr);
73  }
74  }
76  if (!excluded.empty()) {
77  std::stringstream excluded_report{};
78  for (const Identifier& id : excluded){
79  excluded_report << " *** " << m_idHelperSvc->toStringDetEl(id) << std::endl;
80  }
81  ATH_MSG_INFO("Test all station except the following excluded ones " << std::endl << excluded_report.str());
82  }
83  }
84  ATH_CHECK(detStore()->retrieve(m_detMgr));
85  return StatusCode::SUCCESS;
86 }
88  ATH_CHECK(m_tree.write());
89  return StatusCode::SUCCESS;
90 }
92  const EventContext& ctx{Gaudi::Hive::currentContext()};
93 
94  const ActsGeometryContext* geoContextHandle{nullptr};
95  ATH_CHECK(SG::get(geoContextHandle, m_geoCtxKey, ctx));
96 
97  const ActsGeometryContext& gctx{*geoContextHandle};
98 
99  const MdtIdHelper& id_helper{m_idHelperSvc->mdtIdHelper()};
100  for (const Identifier& test_me : m_testStations) {
101  const int ml = id_helper.multilayer(test_me);
102  const std::string detStr = m_idHelperSvc->toStringDetEl(test_me);
103  ATH_MSG_DEBUG("Test retrieval of Mdt detector element "<<detStr);
104  const MdtReadoutElement* reElement = m_detMgr->getMdtReadoutElement(test_me);
105  if (!reElement) {
106  continue;
107  }
109  if (reElement->identify() != test_me) {
110  ATH_MSG_FATAL("Expected to retrieve "<<detStr<<". But got instead "<<m_idHelperSvc->toStringDetEl(reElement->identify()));
111  return StatusCode::FAILURE;
112  }
113  ATH_CHECK(dumpToTree(ctx,gctx,reElement));
114  const Amg::Transform3D globToLocal{reElement->globalToLocalTrans(gctx)};
115  const Amg::Transform3D& localToGlob{reElement->localToGlobalTrans(gctx)};
117  const Amg::Transform3D transClosure = globToLocal * localToGlob;
118  if (!Amg::doesNotDeform(transClosure)) {
119  ATH_MSG_FATAL("Closure test failed for "<<detStr<<". Ended up with "<< Amg::toString(transClosure) );
120  return StatusCode::FAILURE;
121  }
122  for (unsigned int lay = 1 ; lay <= reElement->numLayers() ; ++lay ) {
123  for (unsigned int tube = 1; tube <=reElement->numTubesInLay(); ++tube ){
124  const Identifier tube_id = id_helper.channelID(test_me,ml,lay,tube);
126  const IdentifierHash measHash = reElement->measurementHash(tube_id);
127  const Identifier cnv_tube_id = reElement->measurementId(measHash);
128  if (tube_id != cnv_tube_id) {
129  ATH_MSG_FATAL("Failed to convert "<<m_idHelperSvc->toString(tube_id)<<" back and forth "<<m_idHelperSvc->toString(cnv_tube_id));
130  return StatusCode::FAILURE;
131  }
132  }
133  }
134  }
135  dumpReadoutSideXML();
136  return StatusCode::SUCCESS;
137 }
138 void GeoModelMdtTest::dumpReadoutSideXML() const {
139  if (m_swapRead.empty()) return;
140  std::ofstream swapReadXML{m_swapRead};
141  if (!swapReadXML.good()) {
142  ATH_MSG_ERROR("Failed to create "<<m_swapRead);
143  return;
144  }
145  std::set<Identifier> chamberIDs{};
146  const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
147  swapReadXML<<"<Table name=\"MdtTubeROSides\">"<<std::endl;
148  unsigned int counter{1};
149  for (MdtIdHelper::const_id_iterator itr = idHelper.detectorElement_begin();
150  itr != idHelper.detectorElement_end();
151  ++itr){
152  const Identifier swap{*itr};
153  const MdtReadoutElement* readoutEle = m_detMgr->getMdtReadoutElement(swap);
154  if(!readoutEle) continue;
155  if (!chamberIDs.insert(idHelper.elementID(swap)).second) continue;
156  const int side = readoutEle->getParameters().readoutSide;
157  swapReadXML<<" <Row ";
158  swapReadXML<<"MDTTUBEROSIDES_DATA_ID=\""<<counter<<"\" ";
159  swapReadXML<<"stationName=\""<<m_idHelperSvc->stationNameString(swap)<<"\" ";
160  swapReadXML<<"stationEta=\""<<m_idHelperSvc->stationEta(swap)<<"\" ";
161  swapReadXML<<"stationPhi=\""<<m_idHelperSvc->stationPhi(swap)<<"\" ";
162  swapReadXML<<"side=\""<<side<<"\" ";
163  swapReadXML<<"/>"<<std::endl;
164  ++counter;
165  }
166  swapReadXML<<"</Table>"<<std::endl;
167 
168 }
169 StatusCode GeoModelMdtTest::dumpToTree(const EventContext& ctx,
170  const ActsGeometryContext& gctx,
171  const MdtReadoutElement* readoutEle) {
172 
173  m_stIndex = readoutEle->stationName();
174  m_stEta = readoutEle->stationEta();
175  m_stPhi = readoutEle->stationPhi();
176  m_stML = readoutEle->multilayer();
177  m_chamberDesign = readoutEle->chamberDesign();
178 
179  m_numLayers = readoutEle->numLayers();
180  m_numTubes = readoutEle->numTubesInLay();
181 
182  m_tubeRad = readoutEle->innerTubeRadius();
183  m_tubePitch = readoutEle->tubePitch();
184 
186  const Amg::Transform3D& transform {readoutEle->localToGlobalTrans(gctx)};
187  m_readoutTransform = transform;
188  m_alignableNode = readoutEle->alignableTransform()->getDefTransform();
189 
191  for (unsigned int lay = 1; lay <= readoutEle->numLayers(); ++lay) {
192  for (unsigned int tube = 1; tube <= readoutEle->numTubesInLay(); ++tube) {
193  const IdentifierHash measHash{readoutEle->measurementHash(lay,tube)};
194  if (!readoutEle->isValid(measHash)) continue;
195  const Amg::Transform3D& tubeTransform{readoutEle->localToGlobalTrans(gctx,measHash)};
196  m_tubeLay.push_back(lay);
197  m_tubeNum.push_back(tube);
198  m_tubeTransform.push_back(tubeTransform);
199  m_tubePosInCh.push_back(readoutEle->msSector()->globalToLocalTrans(gctx) *
200  readoutEle->center(gctx, measHash));
201  m_roPos.push_back(readoutEle->readOutPos(gctx, measHash));
202  m_tubeLength.push_back(readoutEle->tubeLength(measHash));
203  m_activeTubeLength.push_back(readoutEle->activeTubeLength(measHash));
204  m_wireLength.push_back(readoutEle->wireLength(measHash));
205  }
206  }
207 
208  return m_tree.fill(ctx) ? StatusCode::SUCCESS : StatusCode::FAILURE;
209 }
210 
211 }
212 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
MdtIdHelper::multilayer
int multilayer(const Identifier &id) const
Access to components of the ID.
Definition: MdtIdHelper.cxx:722
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
MuonGMR4::MuonReadoutElement::msSector
const SpectrometerSector * msSector() const
Returns the pointer to the envelope volume enclosing all chambers in the sector.
MuonGMR4::MdtReadoutElement::numTubesInLay
unsigned int numTubesInLay() const
Returns the number of tubes per layer.
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ActsGeometryContext.h
MuonGMR4::MdtReadoutElement::tubeLength
double tubeLength(const IdentifierHash &hash) const
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MdtReadoutElement.cxx:183
initialize
void initialize()
Definition: run_EoverP.cxx:894
MuonGMR4::MdtReadoutElement::measurementHash
static IdentifierHash measurementHash(unsigned int layerNumber, unsigned int tubeNumber)
Transform the layer and tube number to the measurementHash.
MuonGMR4::MuonReadoutElement::chamberDesign
const std::string & chamberDesign() const
The chamber design refers to the construction parameters of a readout element.
MuonGMR4::MdtReadoutElement::activeTubeLength
double activeTubeLength(const IdentifierHash &hash) const
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MdtReadoutElement.cxx:171
MuonGMR4::MdtReadoutElement::readOutPos
Amg::Vector3D readOutPos(const ActsGeometryContext &ctx, const Identifier &measId) const
Returns the global position of the readout card.
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
isValid
bool isValid(const T &p)
Av: we implement here an ATLAS-sepcific convention: all particles which are 99xxxxx are fine.
Definition: AtlasPID.h:872
SpectrometerSector.h
MuonGMR4::MdtReadoutElement::parameterBook::readoutSide
double readoutSide
Is the readout chip at positive or negative Z?
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MdtReadoutElement.h:60
TRT::Hit::side
@ side
Definition: HitInfo.h:83
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
MuonGMR4::MdtReadoutElement::measurementId
Identifier measurementId(const IdentifierHash &measHash) const override final
Converts the measurement hash back to the full Identifier.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MdtReadoutElement.cxx:46
MuonGMR4::MdtReadoutElement::multilayer
unsigned int multilayer() const
Returns the multi layer of the MdtReadoutElement.
MuonGMR4::MdtReadoutElement::wireLength
double wireLength(const IdentifierHash &hash) const
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MdtReadoutElement.cxx:186
GeoModelMdtTest.h
calibdata.tube_id
tube_id
Definition: calibdata.py:29
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
MuonGMR4
The ReadoutGeomCnvAlg converts the Run4 Readout geometry build from the GeoModelXML into the legacy M...
Definition: MdtCalibInput.h:19
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
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
MuonGMR4::SpectrometerSector::globalToLocalTrans
Amg::Transform3D globalToLocalTrans(const ActsGeometryContext &gctx) const
Returns the global -> local transformation from the ATLAS global.
Definition: SpectrometerSector.cxx:78
MuonGMR4::MdtReadoutElement::numLayers
unsigned int numLayers() const
Returns the number of tube layer.
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
Amg::doesNotDeform
bool doesNotDeform(const Amg::Transform3D &trans)
Checks whether the linear part of the transformation rotates or stetches any of the basis vectors.
Definition: GeoPrimitivesHelpers.h:383
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
MdtIdHelper
Definition: MdtIdHelper.h:61
MuonGMR4::MuonReadoutElement::center
Amg::Vector3D center(const ActsGeometryContext &ctx) const
Returns the detector center (Which is the same as the detector center of the first measurement layer)
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
MuonGMR4::MdtReadoutElement::innerTubeRadius
double innerTubeRadius() const
Returns the inner tube radius.
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
MuonGMR4::MdtReadoutElement
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MdtReadoutElement.h:18
MuonGMR4::MuonReadoutElement::alignableTransform
const GeoAlignableTransform * alignableTransform() const
Returnsthe alignable transform of the readout element.
MuonGMR4::MuonReadoutElement::identify
Identifier identify() const override final
Return the athena identifier.
MuonGMR4::MdtReadoutElement::tubePitch
double tubePitch() const
Returns the pitch between 2 tubes in a layer.
MuonGMR4::MuonReadoutElement::stationName
int stationName() const
Returns the stationName (BIS, BOS, etc) encoded into the integer.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MdtReadoutElement.h
GeoPrimitivesToStringConverter.h
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
MuonIdHelper::const_id_iterator
std::vector< Identifier >::const_iterator const_id_iterator
Definition: MuonIdHelper.h:143
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MSTrackingVolumeBuilder.cxx:25
MuonGMR4::MuonReadoutElement::stationEta
int stationEta() const
Returns the stationEta (positive A site, negative O site)
MuonGMR4::MdtReadoutElement::getParameters
const parameterBook & getParameters() const
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MdtReadoutElement.cxx:45
test_pyathena.counter
counter
Definition: test_pyathena.py:15
MuonGMR4::MdtReadoutElement::isValid
bool isValid(const IdentifierHash &measHash) const
MuonGMR4::MuonReadoutElement::stationPhi
int stationPhi() const
Returns the stationPhi (1-8) -> sector (2*phi - (isSmall))
calibdata.tube
tube
Definition: calibdata.py:30
Identifier
Definition: IdentifierFieldParser.cxx:14