ATLAS Offline Software
MdtToyCablingJsonDumpAlg.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 
9 
10 #include "nlohmann/json.hpp"
11 #include <fstream>
12 
13 
14 
15 MdtToyCablingJsonDumpAlg::MdtToyCablingJsonDumpAlg(const std::string& name, ISvcLocator* pSvcLocator):
16  AthAlgorithm{name, pSvcLocator} {}
17 
19  ATH_CHECK(m_idHelperSvc.retrieve());
21  if (!m_idHelperSvc->hasMDT()) {
22  ATH_MSG_FATAL("You can't write mdt cablings without mdt detectors? ");
23  return StatusCode::FAILURE;
24  }
25  return StatusCode::SUCCESS;
26 }
28 
29 
30 
31  using Mapping = MdtMezzanineCard::Mapping;
32 
33  Mapping tubeMapping{};
35  for (int channel = 0; channel<12; ++channel) {
36  tubeMapping[2*channel] = channel;
37  tubeMapping[2*channel +1] = 23 -channel;
38 
39  }
40  const MdtMezzanineCard mapping4Layers{tubeMapping, 4, 1};
41  const MdtMezzanineCard mapping3Layers{tubeMapping, 3, 2};
42 
43 
44  {
45  std::ofstream outStream{m_mezzanineJSON};
46  if (!outStream.good()) {
47  ATH_MSG_FATAL("Failed to create JSON file "<<m_mezzanineJSON);
48  return StatusCode::FAILURE;
49  }
50  auto dumpCard = [&outStream](const MdtMezzanineCard& card) {
51  outStream<<" {"<<std::endl;
52  outStream<<" \"mezzId\": "<<static_cast<int>(card.id())<<","<<std::endl;
53  outStream<<" \"nTubeLayer\": "<<static_cast<int>(card.numTubeLayers())<<","<<std::endl;
54  outStream<<" \"tdcToTubeMap\": [";
55  for (size_t ch = 0 ; ch < card.tdcToTubeMap().size(); ++ch) {
56  outStream<<static_cast<int>(card.tdcToTubeMap()[ch]);
57  if (ch + 1 != card.tdcToTubeMap().size()) {
58  outStream<<",";
59  }
60  }
61  outStream<<"]"<<std::endl;
62  outStream<<" }";
63  };
64  outStream<<"["<<std::endl;
65  dumpCard(mapping4Layers);
66  outStream<<","<<std::endl;
67  dumpCard(mapping3Layers);
68  outStream<<"]"<<std::endl;
69  }
70 
71  ATH_MSG_VERBOSE("Test mappings "<<std::endl<<mapping4Layers<<std::endl<<mapping3Layers);
72 
73  std::vector<const MuonGMR4::MdtReadoutElement*> reEles{m_detMgr->getAllMdtReadoutElements()};
74 
76  constexpr std::array<int, 4> subDet{97,98, 99, 100};
77  std::array<int, 4> mrodCounter{make_array<int,4>(0)};
78  std::array<int, 4> csmCounter{make_array<int,4>(0)};
79  constexpr int mrodMax = 40;
80 
81  nlohmann::json allChannels{};
82  for (const MuonGMR4::MdtReadoutElement* reEle : reEles) {
83  const int detIndex = 2*(!reEle->isBarrel()) + (reEle->stationEta() < 0);
84  const int mrod = (++mrodCounter[detIndex]);
85  if (mrod ==mrodMax) {
86  mrodCounter[detIndex] = 0;
87  (++csmCounter[detIndex]);
88  }
89 
90  const MdtMezzanineCard& card {reEle->numLayers() == 4 ? mapping4Layers: mapping3Layers};
91  int tdc{0};
92 
93  for (uint tube =1; tube<= reEle->numTubesInLay(); tube+=card.numTubesPerLayer()) {
94  nlohmann::json cablingChannel{};
95  cablingChannel["station"] = m_idHelperSvc->stationNameString(reEle->identify());
96  cablingChannel["eta"] = reEle->stationEta();
97  cablingChannel["phi"] = reEle->stationPhi();
98  cablingChannel["ml"] = reEle->multilayer();
99  cablingChannel["subDet"] = subDet[detIndex];
100  cablingChannel["csm"] = csmCounter[detIndex];
101  cablingChannel["mrod"] = mrod;
102  cablingChannel["tdcId"] = (tdc++);
103  cablingChannel["mezzId"] = static_cast<int>(card.id());
104  cablingChannel["tubeZero"] = tube;
105  allChannels.push_back(cablingChannel);
106  }
107  }
108  std::ofstream outStream{m_cablingJSON};
109  if (!outStream.good()) {
110  ATH_MSG_FATAL("Failed to create JSON file "<<m_cablingJSON);
111  return StatusCode::FAILURE;
112  }
113  outStream<<allChannels.dump(2)<<std::endl;
114  return StatusCode::SUCCESS;
115 }
116 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
MdtMezzanineCard.h
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
sendEI_SPB.ch
ch
Definition: sendEI_SPB.py:35
MdtToyCablingJsonDumpAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MdtToyCablingJsonDumpAlg.h:31
plotting.yearwise_efficiency.channel
channel
Definition: yearwise_efficiency.py:24
json
nlohmann::json json
Definition: HistogramDef.cxx:9
MdtToyCablingJsonDumpAlg.h
MdtMezzanineCard::Mapping
std::array< uint8_t, 24 > Mapping
Definition: MdtMezzanineCard.h:39
MdtToyCablingJsonDumpAlg::initialize
virtual StatusCode initialize() override
Definition: MdtToyCablingJsonDumpAlg.cxx:18
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
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
MdtToyCablingJsonDumpAlg::m_mezzanineJSON
Gaudi::Property< std::string > m_mezzanineJSON
Definition: MdtToyCablingJsonDumpAlg.h:37
MdtMezzanineCard
MdtMezzanineCard - Helper struct to represent the structure of a mezzanine card in a consistent way E...
Definition: MdtMezzanineCard.h:34
MdtToyCablingJsonDumpAlg::m_cablingJSON
Gaudi::Property< std::string > m_cablingJSON
Definition: MdtToyCablingJsonDumpAlg.h:35
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
MuonMDT_CablingMap.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
jobOptions.card
card
Definition: jobOptions.GenevaPy8_Zmumu.py:11
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MdtToyCablingJsonDumpAlg::execute
virtual StatusCode execute() override
Definition: MdtToyCablingJsonDumpAlg.cxx:27
AthAlgorithm
Definition: AthAlgorithm.h:47
MuonGMR4::MdtReadoutElement
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MdtReadoutElement.h:22
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
MdtToyCablingJsonDumpAlg::m_detMgr
const MuonGMR4::MuonDetectorManager * m_detMgr
Definition: MdtToyCablingJsonDumpAlg.h:33
MdtToyCablingJsonDumpAlg::MdtToyCablingJsonDumpAlg
MdtToyCablingJsonDumpAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MdtToyCablingJsonDumpAlg.cxx:15
calibdata.tube
tube
Definition: calibdata.py:31