ATLAS Offline Software
NRpcCablingAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "NRpcCablingAlg.h"
6 
7 
8 #include <fstream>
9 #include <map>
10 
14 #include "CoralBase/Attribute.h"
15 #include "CoralBase/AttributeListSpecification.h"
19 
20 namespace Muon{
22  ATH_MSG_DEBUG("initialize " << name());
23  ATH_CHECK(m_writeKey.initialize());
24  ATH_CHECK(m_idHelperSvc.retrieve());
25  const bool ext_json = !m_extJSONFile.value().empty();
27  ATH_CHECK(m_readKeyMap.initialize(!ext_json));
28  return StatusCode::SUCCESS;
29 }
30 
31 StatusCode NRpcCablingAlg::execute(const EventContext& ctx) const {
32  ATH_MSG_VERBOSE("NRpcCablingAlg::execute()");
33 
34  // Write Cond Handle
35  SG::WriteCondHandle writeCablingHandle{m_writeKey, ctx};
36 
37  if (writeCablingHandle.isValid()) {
38  ATH_MSG_DEBUG("CondHandle "<< writeCablingHandle.fullKey() << " is already valid."
39  << ". In theory this should not be called, but may happen"
40  << " if multiple concurrent events are being processed out of order.");
41  return StatusCode::SUCCESS;
42  }
43  writeCablingHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteRunLB()));
44 
45  ATH_MSG_INFO("Load the Nrpc cabling");
46  auto writeCdo{std::make_unique<RpcCablingMap>(m_idHelperSvc.get())};
47 
49  if (!m_extJSONFile.value().empty()) {
50  std::ifstream in_json{m_extJSONFile};
51  if (!in_json.good()) {
52  ATH_MSG_FATAL("Failed to open external JSON file " << m_extJSONFile);
53  return StatusCode::FAILURE;
54  }
56  in_json>>payload;
57  ATH_CHECK(parsePayload(*writeCdo, payload));
58  } else {
59  SG::ReadCondHandle coolHandle{m_readKeyMap, ctx};
60  if (!coolHandle.isValid()) {
61  ATH_MSG_FATAL("Failed to load cabling map from COOL "<< m_readKeyMap.fullKey());
62  return StatusCode::FAILURE;
63  }
64  writeCablingHandle.addDependency(coolHandle);
65  for (const auto& itr : **coolHandle) {
66  const coral::AttributeList& atr = itr.second;
67  nlohmann::json payload = nlohmann::json::parse(*(static_cast<const std::string*>((atr["data"]).addressOfData())));
68  ATH_CHECK(parsePayload(*writeCdo, payload));
69  }
70  }
71  if (!writeCdo->finalize(msgStream()))
72  return StatusCode::FAILURE;
73 
74  ATH_CHECK(writeCablingHandle.record(std::move(writeCdo)));
75  return StatusCode::SUCCESS;
76 }
78  const nlohmann::json& payload) const {
79 
80 
81  using CablingData = RpcCablingMap::CoolDBEntry;
82  using FlatCablePtr = RpcCablingMap::FlatCablePtr;
83 
84  std::map<unsigned, FlatCablePtr> readoutCards{};
85  for (const auto& flatCable : payload["readoutCards"].items()) {
86  nlohmann::json cabl_payload = flatCable.value();
87  const unsigned id = cabl_payload["flatCableId"];
88  FlatCablePtr& newCard = readoutCards[id];
89  newCard = std::make_unique<RpcFlatCableTranslator>(id);
90  for (const auto& pinAssign : cabl_payload["pinAssignment"]) {
91  const std::array<unsigned, 2> pin = pinAssign;
92  if (!newCard->mapChannels(pin[0], pin[1], msgStream())){
93  return StatusCode::FAILURE;
94  }
95  }
96  ATH_MSG_DEBUG("Add new card "<<(*newCard));
97  }
98  constexpr int8_t phiBit = CablingData::measPhiBit;
99  constexpr int8_t sideBit = CablingData::stripSideBit;
100  for (const auto& cabl_chan : payload["chamberMap"].items()) {
101  nlohmann::json cabl_payload = cabl_chan.value();
102  CablingData cabl_data{};
103  const std::string stName{cabl_payload["station"]};
104  cabl_data.stationIndex = m_idHelperSvc->rpcIdHelper().stationNameIndex(stName);
105  cabl_data.eta = cabl_payload["eta"];
106  cabl_data.phi = cabl_payload["phi"];
107  cabl_data.doubletR = cabl_payload["doubletR"];
108  cabl_data.doubletPhi = cabl_payload["doubletPhi"];
109  cabl_data.doubletZ = cabl_payload["doubletZ"];
110  const int8_t phiAndStrip = cabl_payload["measPhi"];
111  cabl_data.setMeasPhiAndSide(phiAndStrip & phiBit, phiAndStrip & sideBit);
112  cabl_data.gasGap = cabl_payload["gasGap"];
114  cabl_data.subDetector = cabl_payload["subDetector"];
115  cabl_data.tdcSector = cabl_payload["tdcSector"];
116  cabl_data.tdc = cabl_payload["tdc"];
117  cabl_data.firstStrip = cabl_payload["firstStrip"];
118  unsigned int flatCable = cabl_payload["flatCableId"];
119  if (!readoutCards[flatCable]) {
120  ATH_MSG_FATAL("Failed to find a valid flat cable for "<<static_cast<const RpcCablingOfflineID&>(cabl_data)<<", flatCableId "<<flatCable);
121  return StatusCode::FAILURE;
122  }
123  cabl_data.flatCable = readoutCards[flatCable];
124  if (!cabling_map.insertChannels(std::move(cabl_data), msgStream())) {
125  return StatusCode::FAILURE;
126  }
127  }
128  return StatusCode::SUCCESS;
129 }
130 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
CondAttrListCollection.h
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
json
nlohmann::json json
Definition: HistogramDef.cxx:9
Muon::NRpcCablingAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: NRpcCablingAlg.cxx:31
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
Muon::NRpcCablingAlg::m_readKeyMap
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyMap
Definition: NRpcCablingAlg.h:36
Muon::RpcCablingOfflineID
Struct to represent the offline identifier of the Rpc measurement decomposed into the particular fiel...
Definition: RpcCablingData.h:39
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Muon
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
Definition: TrackSystemController.h:45
AthenaAttributeList.h
RpcIdHelper.h
Muon::RpcCablingMap::insertChannels
bool insertChannels(CoolDBEntry &&dbEntry, MsgStream &log)
Inserts a new dbEntry into the map.
Definition: RpcCablingMap.cxx:122
Muon::RpcCablingMap::FlatCablePtr
GeoModel::TransientConstSharedPtr< Muon::RpcFlatCableTranslator > FlatCablePtr
Definition: RpcCablingMap.h:35
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
Muon::NRpcCablingAlg::initialize
virtual StatusCode initialize() override
Definition: NRpcCablingAlg.cxx:21
Muon::NRpcCablingAlg::parsePayload
StatusCode parsePayload(RpcCablingMap &cablingMap, const nlohmann::json &payload) const
Load the payload of the actual cabling map from offline <-> online
Definition: NRpcCablingAlg.cxx:77
IOVInfiniteRange::infiniteRunLB
static EventIDRange infiniteRunLB()
Produces an EventIDRange that is infinite in RunLumi and invalid in Time.
Definition: IOVInfiniteRange.h:39
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
NRpcCablingAlg.h
Muon::RpcCablingMap
Definition: RpcCablingMap.h:27
IOVInfiniteRange.h
PathResolver.h
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
Muon::NRpcCablingAlg::m_extJSONFile
Gaudi::Property< std::string > m_extJSONFile
Definition: NRpcCablingAlg.h:39
Muon::RpcCablingMap::CoolDBEntry
Helper struct to ship all information from the Database
Definition: RpcCablingMap.h:59
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
RpcFlatCableTranslator.h
Muon::NRpcCablingAlg::m_idHelperSvc
ServiceHandle< IMuonIdHelperSvc > m_idHelperSvc
Definition: NRpcCablingAlg.h:33
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
Muon::NRpcCablingAlg::m_writeKey
SG::WriteCondHandleKey< RpcCablingMap > m_writeKey
Definition: NRpcCablingAlg.h:34