ATLAS Offline Software
MdtToyTwinCablingDumpAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 #include <algorithm>
6 #include <vector>
9 #include "nlohmann/json.hpp"
10 #include <fstream>
11 #include <format>
12 
13 MdtToyTwinCablingDumpAlg::MdtToyTwinCablingDumpAlg(const std::string& name, ISvcLocator* pSvcLocator):
14  AthAlgorithm{name, pSvcLocator} {}
15 
17  ATH_CHECK(m_idHelperSvc.retrieve());
19  if (!m_idHelperSvc->hasMDT()) {
20  ATH_MSG_FATAL("You can't write mdt twin cablings without mdt detectors? ");
21  return StatusCode::FAILURE;
22  }
23  return StatusCode::SUCCESS;
24 }
26  if(std::ranges::find(m_stationsToTwin, m_idHelperSvc->stationNameString(detElId))!= m_stationsToTwin.end()) {
27  return true;
28  }
29  const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
30  std::string mlName{std::format("{:}{:d}{:}{:02}M{:d}",
31  m_idHelperSvc->stationNameString(detElId),
32  std::abs(m_idHelperSvc->stationEta(detElId)),
33  (m_idHelperSvc->stationEta(detElId) > 0 ? 'A' : 'C'),
34  m_idHelperSvc->sector(detElId),
35  idHelper.multilayer(detElId))};
36 
37  ATH_MSG_DEBUG("Test "<<mlName<<", "<<m_detElIdToTwin);
39  return true;
40  }
41 
42  return m_detElIdToTwin.value().empty() && m_stationsToTwin.value().empty();
43 }
45  using Mapping = Muon::HedgehogBoard::Mapping;
46  using HedgeHogBoardPtr = Muon::HedgehogBoard::HedgehogBoardPtr;
47  auto createMap = [this](const uint8_t tubeLayers, const uint16_t id) {
48  Mapping dummyMap{make_array<uint8_t, 24>(-1)};
49  const Muon::HedgehogBoard dummyBoard{dummyMap, tubeLayers, id};
50  for (uint8_t layer = 1 ; layer<= dummyBoard.numTubeLayers(); ++layer) {
51  for (uint8_t tube = 1; tube<= dummyBoard.numTubesPerLayer(); ++ tube) {
52  const uint8_t thisPin = dummyBoard.pinNumber(layer, tube);
53  const uint8_t otherTube = dummyBoard.numTubeLayers() == 3 || tube != 2 ? tube +2 : 5;
54  const uint8_t otherPin = dummyBoard.pinNumber(layer, otherTube);
55  ATH_MSG_DEBUG("Layer "<<static_cast<int>(layer)<<", "<<static_cast<int>(tube)<<" -> pin: "
56  <<static_cast<int>(thisPin)<<" <=> tube: "<<static_cast<int>(tube + 2)<<", pin: "
57  <<static_cast<int>(otherPin));
58  if (dummyMap[thisPin] < dummyMap.size()) {
59  continue;
60  }
61  dummyMap[otherPin] = thisPin;
62  dummyMap[thisPin] = otherPin;
63  }
64  }
65  auto board = std ::make_unique<Muon::HedgehogBoard>(dummyMap, tubeLayers, id);
66  ATH_MSG_INFO("Created new hedgehog board "<<std::endl<<(*board));
67  return board;
68  };
69  HedgeHogBoardPtr threeLayBoard{createMap(3,1)}, fourLayBoard{createMap(4,2)};
70 
71  std::vector<const MuonGMR4::MdtReadoutElement*> reEles{m_detMgr->getAllMdtReadoutElements()};
72 
74  Muon::TwinTubeMap twinTubes{m_idHelperSvc.get()};
75  for(const MuonGMR4::MdtReadoutElement* reEl : reEles){
76  const Identifier id = reEl->identify();
77  if (!equipREwithTwins(id)) {
78  continue;
79  }
80  const HedgeHogBoardPtr& pickMe{reEl->numLayers() == 3 ? threeLayBoard : fourLayBoard};
81  uint16_t boardCounter{0};
82  std::vector<uint16_t> connectedBoards{};
83  for (unsigned iTube{1}; iTube<= reEl->numTubesInLay() + (reEl->numTubesInLay() % pickMe->numTubesPerLayer());
84  iTube+=pickMe->numTubesPerLayer()) {
85  ATH_CHECK(twinTubes.addHedgeHogBoard(id, pickMe, boardCounter++));
86  connectedBoards.push_back(pickMe->boardId());
87  }
88  nlohmann::json twinPair{};
89  twinPair["station"] = m_idHelperSvc->stationNameString(id);
90  twinPair["eta"] = m_idHelperSvc->stationEta(id);
91  twinPair["phi"] = m_idHelperSvc->stationPhi(id);
92  twinPair["ml"] = m_idHelperSvc->mdtIdHelper().multilayer(id);
93  twinPair["mountedBoards"] = connectedBoards;
94  payload["TwinTubeMapping"].push_back(std::move(twinPair));
95  }
96 
97  auto dumpHedgeHog = [&payload, this](const HedgeHogBoardPtr& board) {
98  if (board.use_count() < 2) {
99  ATH_MSG_INFO("The board "<<std::endl<<(*board)<<" has not been used "<<board.use_count());
100  return;
101  }
102  nlohmann::json hedge{};
103  hedge["boardId"] = board->boardId();
104  hedge["nTubeLayers"] = board->numTubeLayers();
105  hedge["hedgeHogPins"] = board->data();
106  if (board->hasHVDelayTime()) {
107  hedge["hvDelayTime"] = board->hvDelayTime();
108  }
109  payload["HedgehogBoards"].push_back(std::move(hedge));
110  };
111  dumpHedgeHog(threeLayBoard);
112  dumpHedgeHog(fourLayBoard);
113 
114  std::ofstream outStream{m_cablingJSON};
115  if(!outStream.good()){
116  ATH_MSG_FATAL("Failed to create JSON " << m_cablingJSON);
117  return StatusCode::FAILURE;
118  }
119 
120  outStream<<payload.dump(m_spacing)<<std::endl;
121 
122  return StatusCode::SUCCESS;
123 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
vtune_athena.format
format
Definition: vtune_athena.py:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
json
nlohmann::json json
Definition: HistogramDef.cxx:9
MdtToyTwinCablingDumpAlg::equipREwithTwins
bool equipREwithTwins(const Identifier &detElId) const
Definition: MdtToyTwinCablingDumpAlg.cxx:25
MdtToyTwinCablingDumpAlg::MdtToyTwinCablingDumpAlg
MdtToyTwinCablingDumpAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MdtToyTwinCablingDumpAlg.cxx:13
MdtToyTwinCablingDumpAlg::m_cablingJSON
Gaudi::Property< std::string > m_cablingJSON
Definition: MdtToyTwinCablingDumpAlg.h:37
Muon::HedgehogBoard::Mapping
std::array< uint8_t, nChPerBoard > Mapping
Definition: HedgehogBoard.h:37
MdtToyTwinCablingDumpAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MdtToyTwinCablingDumpAlg.h:33
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
MdtToyTwinCablingDumpAlg::m_spacing
Gaudi::Property< int > m_spacing
Definition: MdtToyTwinCablingDumpAlg.h:41
MdtToyTwinCablingDumpAlg.h
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
MdtToyTwinCablingDumpAlg::execute
virtual StatusCode execute() override
Definition: MdtToyTwinCablingDumpAlg.cxx:44
Muon::TwinTubeMap
Definition: TwinTubeMap.h:16
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
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
Muon::HedgehogBoard
Helper struct to represent the High-voltage pins and a possible connection between them.
Definition: HedgehogBoard.h:32
MdtIdHelper
Definition: MdtIdHelper.h:61
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Muon::HedgehogBoard::HedgehogBoardPtr
std::shared_ptr< const HedgehogBoard > HedgehogBoardPtr
Definition: HedgehogBoard.h:34
HedgehogBoard.h
AthAlgorithm
Definition: AthAlgorithm.h:47
MdtToyTwinCablingDumpAlg::m_detElIdToTwin
Gaudi::Property< std::vector< std::string > > m_detElIdToTwin
Definition: MdtToyTwinCablingDumpAlg.h:40
MuonGMR4::MdtReadoutElement
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MdtReadoutElement.h:22
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
MdtToyTwinCablingDumpAlg::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: MdtToyTwinCablingDumpAlg.h:35
TwinTubeMap.h
MdtToyTwinCablingDumpAlg::initialize
virtual StatusCode initialize() override
Definition: MdtToyTwinCablingDumpAlg.cxx:16
calibdata.tube
tube
Definition: calibdata.py:31
MdtToyTwinCablingDumpAlg::m_stationsToTwin
Gaudi::Property< std::vector< std::string > > m_stationsToTwin
Definition: MdtToyTwinCablingDumpAlg.h:38
Identifier
Definition: IdentifierFieldParser.cxx:14