ATLAS Offline Software
Loading...
Searching...
No Matches
MdtCablingTestAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4#include "MdtCablingTestAlg.h"
8
9#include <fstream>
10
11MdtCablingTestAlg::MdtCablingTestAlg(const std::string& name, ISvcLocator* pSvcLocator):
12 AthAlgorithm(name,pSvcLocator) {}
13
14
16 ATH_CHECK(m_idHelperSvc.retrieve());
17 ATH_CHECK(m_DetectorManagerKey.initialize());
18 ATH_CHECK(m_cablingKey.initialize());
19 ATH_CHECK(m_deadChanKey.initialize(!m_deadChanKey.empty()));
20 return StatusCode::SUCCESS;
21}
22
24 const EventContext& ctx = Gaudi::Hive::currentContext();
25 std::unique_ptr<std::fstream> f_dump = !m_dumpFile.value().empty() ?
26 std::make_unique<std::fstream>(m_dumpFile, std::fstream::out) : nullptr;
27 ATH_MSG_INFO("Start validation of the Mdt cabling. Dump complete mapping into "<<m_dumpFile);
28 {
29 using Mapping = MdtMezzanineCard::Mapping;
30 using OffChnl = MdtMezzanineCard::OfflineCh;
31 Mapping staggering{};
32 for (unsigned i = 0 ; i < staggering.size(); ++i){
33 staggering[i] = (staggering.size() -1) -i;
34 }
35 for (unsigned i = 0 ; i < staggering.size() ; i+=2){
36 std::swap(staggering[i], staggering[i+1]);
37 }
38 for (unsigned nLay : {3,4}){
39 MdtMezzanineCard testCard(staggering, nLay , 0);
40 if (!testCard.checkConsistency(msgStream())) return StatusCode::FAILURE;
41 ATH_MSG_INFO("Test mezzanine "<<testCard);
43 for (unsigned ch = 0 ; ch < staggering.size() ; ++ch) {
44 OffChnl off = testCard.offlineTube(ch, msgStream());
45 if (!off.isValid) {
46 ATH_MSG_FATAL("Failed to load tube & layer for channel "<<ch);
47 return StatusCode::FAILURE;
48 }
49 unsigned back_ch = testCard.tdcChannel(off.layer, off.tube +1 , msgStream());
50 if (ch != back_ch) {
51 ATH_MSG_FATAL("Forward conversion of "<<ch<<" lead to "
52 <<static_cast<int>(off.layer)<<","
53 <<static_cast<int>(off.tube +1)<<" and then back to "
54 <<back_ch);
55 return StatusCode::FAILURE;
56 }
57 }
59 for (unsigned lay = 1 ; lay <= nLay ; ++lay) {
60 for (unsigned int tube = 1 ; tube <= testCard.numTubesPerLayer() ; ++tube) {
61 const unsigned ch1 = testCard.tdcChannel(lay,tube,msgStream());
62 const unsigned ch2 = testCard.tdcChannel(lay,tube + testCard.numTubesPerLayer(),msgStream());
63
64 if(ch1 != ch2) {
65 ATH_MSG_FATAL("The tube "<<tube<<" in layer "<<lay
66 <<" leads to 2 different answers "<<ch1<<" vs. "<<ch2);
67 return StatusCode::FAILURE;
68 }
69 }
70 }
71 }
72 }
73
75 if (!detectorMgr.isValid()){
76 ATH_MSG_FATAL("Failed to retrieve the Detector manager "<<m_DetectorManagerKey.fullKey());
77 return StatusCode::FAILURE;
78 }
79
81 if (!cabling.isValid()) {
82 ATH_MSG_ERROR("Failed to retrieve the Mdt cabling "<<m_cablingKey.fullKey());
83 return StatusCode::FAILURE;
84 }
85
86 const MdtCondDbData* deadChan{nullptr};
87 if (!m_deadChanKey.empty()) {
89 if (!deadChanHandle.isValid()) {
90 ATH_MSG_FATAL("Failed to retrieve Mdt conditions "<<m_deadChanKey.fullKey());
91 return StatusCode::FAILURE;
92 }
93 deadChan = deadChanHandle.cptr();
94
95 }
96
97 const MdtIdHelper& idHelper = m_idHelperSvc->mdtIdHelper();
98 unsigned int n_elements{0}, n_success{0};
99 bool failure{false};
100
101 for (auto det_itr = idHelper.detectorElement_begin(); det_itr != idHelper.detectorElement_end(); ++det_itr){
102 const MuonGM::MdtReadoutElement* readEle = detectorMgr->getMdtReadoutElement(*det_itr);
103
104 if (!readEle) {
105 ATH_MSG_DEBUG("Detector element does not exist. ");
106 continue;
107 }
108 if (deadChan && !deadChan->isGoodChamber(readEle->identify())) {
109 ATH_MSG_ALWAYS("Dead station found "<<m_idHelperSvc->toStringChamber(readEle->identify()));
110 continue;
111 }
112 ATH_MSG_DEBUG("Check station "<<m_idHelperSvc->toStringChamber(readEle->identify()));
113 for (int layer = 1 ; layer <= readEle->getNLayers(); ++layer){
114 for (int tube = 1 ; tube <= readEle->getNtubesperlayer(); ++tube){
115 bool is_valid{false};
116 const Identifier tube_id = idHelper.channelID(readEle->identify(), readEle->getMultilayer(), layer, tube, is_valid);
117 if (!is_valid){
118 ATH_MSG_VERBOSE("Invalid element");
119 continue;
120 }
121 if (deadChan && !deadChan->isGood(tube_id)) {
122 ATH_MSG_ALWAYS("Dead dube detected "<<m_idHelperSvc->toString(tube_id));
123 continue;
124 }
125 ++n_elements;
127 MdtCablingData cabling_data{};
128 cabling->convert(tube_id,cabling_data);
130 if (!cabling->getOnlineId(cabling_data,msgStream())){
131 ATH_MSG_ERROR("Could no retrieve a valid online channel for "<<m_idHelperSvc->toString(tube_id)<<" from station ID "<<m_idHelperSvc->toString(readEle->identify()));
132 failure = true;
133 continue;
134 }
135 if (f_dump) (*f_dump)<<cabling_data<<std::endl;
137
139 const MdtCablingOffData off_data{};
140 static_cast<MdtCablingOffData&> (cabling_data) = off_data;
141 if (!cabling->getOfflineId(cabling_data, msgStream())){
142 ATH_MSG_ERROR("Could not convert the online cabling "<<cabling_data<<" to an offline identifier. Initial identifier "<<m_idHelperSvc->toString(tube_id));
143 failure = true;
144 continue;
145 }
146
147 Identifier test_id{};
148 if (!cabling->convert(cabling_data, test_id, true)){
149 ATH_MSG_ERROR("The extracted offline identifier "<<cabling_data<<" does not make sense");
150 failure = true;
151 continue;
152 }
153 if (test_id != tube_id){
154 ATH_MSG_ERROR("The forward -> backward conversion failed. Started with "<<m_idHelperSvc->toString(tube_id)<<" ended with "<<m_idHelperSvc->toString(test_id));
155 failure = true;
156 continue;
157 }
158
160 static_cast<MdtCablingOffData&>(cabling_data) = off_data;
161
163 cabling_data.tdcId = 0xff;
164 cabling_data.channelId =0xff;
165 if (!cabling->getOfflineId(cabling_data, msgStream()) || !cabling->convert(cabling_data, test_id, true)) {
166 ATH_MSG_ERROR("Failed to decode Mdt module "<<m_idHelperSvc->toString(tube_id));
167 failure = true;
168 continue;
169 }
170 MdtCablingData mrod_module{cabling_data};
171 if (!cabling->getOnlineId(mrod_module,msgStream())){
172 ATH_MSG_ERROR("Could no retrieve a valid online channel for "<<m_idHelperSvc->toString(tube_id));
173 failure = true;
174 continue;
175 }
177 if (idHelper.elementID(test_id) != idHelper.elementID(readEle->identify()) && static_cast<const MdtCablingOnData&>(mrod_module)!=(cabling_data)) {
178 ATH_MSG_ERROR("Failed to decode module "<<m_idHelperSvc->toString(tube_id)<<" got "<<m_idHelperSvc->toString(test_id) );
179 failure = true;
180 } else ++n_success;
181 }
182 }
183 }
184 ATH_MSG_INFO( n_success<<" out of "<<n_elements<<" channels were successfully validated.");
185 return failure ? StatusCode::FAILURE: StatusCode::SUCCESS;
186}
187
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_ALWAYS(x)
#define ATH_MSG_DEBUG(x)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
virtual StatusCode initialize() override
Gaudi::Property< std::string > m_dumpFile
SG::ReadCondHandleKey< MuonGM::MuonDetectorManager > m_DetectorManagerKey
MdtCablingTestAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadCondHandleKey< MdtCondDbData > m_deadChanKey
SG::ReadCondHandleKey< MuonMDT_CablingMap > m_cablingKey
virtual StatusCode execute() override
bool isGood(const Identifier &Id) const
Returns if the identifier (tube/multiLayer/chamber) is masked in the conditions database.
bool isGoodChamber(const Identifier &Id) const
Returns true if the complete chamber has not dead channels.
Identifier elementID(int stationName, int stationEta, int stationPhi) const
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int tubeLayer, int tube) const
MdtMezzanineCard - Helper struct to represent the structure of a mezzanine card in a consistent way E...
bool checkConsistency(MsgStream &msg) const
checks whether the tdc mapping is complete.
OfflineCh offlineTube(uint8_t tdc, MsgStream &msg) const
uint8_t numTubesPerLayer() const
returns the number of tubes per layer;
uint8_t tdcChannel(uint8_t tubeLay, uint8_t tube, MsgStream &msg) const
returns the tdc channel number
std::array< uint8_t, 24 > Mapping
int getNLayers() const
Returns the number of tube layers inside the multilayer.
int getMultilayer() const
Returns the multilayer represented by the readout element.
int getNtubesperlayer() const
Returns the number of tubes in each tube layer.
Identifier identify() const override final
Returns the ATLAS Identifier of the MuonReadOutElement.
const_id_iterator detectorElement_begin() const
Iterators over full set of ids.
const_id_iterator detectorElement_end() const
const_pointer_type cptr()
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)
Split the offline part of the cabling apart to use it later for sorting.
Helper struct to pipe the result from the tdc -> offline channel translation.