ATLAS Offline Software
Loading...
Searching...
No Matches
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
13MdtToyTwinCablingDumpAlg::MdtToyTwinCablingDumpAlg(const std::string& name, ISvcLocator* pSvcLocator):
14 AthAlgorithm{name, pSvcLocator} {}
15
17 ATH_CHECK(m_idHelperSvc.retrieve());
18 ATH_CHECK(detStore()->retrieve(m_detMgr));
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);
38 if(std::ranges::find(m_detElIdToTwin, mlName)!= m_detElIdToTwin.end()) {
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
73 nlohmann::json payload{};
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}
constexpr std::array< T, N > make_array(const T &def_val)
Helper function to initialize in-place arrays with non-zero values.
Definition ArrayHelper.h:10
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
const ServiceHandle< StoreGateSvc > & detStore() const
int multilayer(const Identifier &id) const
Access to components of the ID.
Gaudi::Property< std::string > m_cablingJSON
MdtToyTwinCablingDumpAlg(const std::string &name, ISvcLocator *pSvcLocator)
Gaudi::Property< int > m_spacing
const MuonGMR4::MuonDetectorManager * m_detMgr
virtual StatusCode initialize() override
Gaudi::Property< std::vector< std::string > > m_stationsToTwin
Gaudi::Property< std::vector< std::string > > m_detElIdToTwin
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
bool equipREwithTwins(const Identifier &detElId) const
virtual StatusCode execute() override
Helper struct to represent the High-voltage pins and a possible connection between them.
std::shared_ptr< const HedgehogBoard > HedgehogBoardPtr
uint8_t pinNumber(const TubeLayer &tubeLay) const
Returns the pinNumber of the tubeLayer.
std::array< uint8_t, nChPerBoard > Mapping
uint8_t numTubeLayers() const
Returns the number of tube layers 3 or 4.
uint8_t numTubesPerLayer() const
Returns the number of tubes per layer.
StatusCode addHedgeHogBoard(const Identifier &detElId, const HedgehogBoardPtr &board, const uint16_t slot)
Add a new hedgehog board with twin tube mapping.