ATLAS Offline Software
Loading...
Searching...
No Matches
MuonNSW_CablingAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <stdlib.h>
8
9#include <fstream>
10#include <map>
11#include <string>
12
15#include "CoralBase/Attribute.h"
16#include "CoralBase/AttributeListSpecification.h"
20#include "nlohmann/json.hpp"
22
24 ISvcLocator* pSvcLocator)
25 : AthReentrantAlgorithm(name, pSvcLocator) {}
26
28 ATH_MSG_DEBUG("initialize " << name());
29 ATH_CHECK(m_readCablingKeys.initialize(m_JSONFile.value().empty()));
30 ATH_CHECK(m_writeKey.initialize());
31 ATH_CHECK(m_idHelperSvc.retrieve());
32 return StatusCode::SUCCESS;
33}
34
35StatusCode MuonNSW_CablingAlg::execute(const EventContext& ctx) const {
36 ATH_MSG_DEBUG("Load the Micro mega cabling map");
37 // Write Cond Handle
39 if (writeHandle.isValid()) {
40 ATH_MSG_DEBUG("CondHandle "<< writeHandle.fullKey() << " is already valid."
41 << ". In theory this should not be called, but may happen"
42 << " if multiple concurrent events are being processed out of order.");
43 return StatusCode::SUCCESS;
44 }
45 writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteRunLB()));
46
47 std::unique_ptr<Nsw_CablingMap> writeCdo{std::make_unique<Nsw_CablingMap>(m_idHelperSvc.get())};
48
50 if (!m_JSONFile.value().empty()) {
51 std::ifstream inf{m_JSONFile};
52 if (!inf.good()) {
53 ATH_MSG_FATAL("Cannot locate external JSON file " << m_JSONFile);
54 return StatusCode::FAILURE;
55 }
56 std::string payload{};
57 while (std::getline(inf, payload)) {
58 ATH_CHECK(loadCablingSchema(payload, *writeCdo));
59 }
60 }
61
63 else if (!m_readCablingKeys.empty()) {
66 if (!readHandle.isValid()) {
67 ATH_MSG_FATAL("Failed to retrieve the cabling data from the database "
68 << key.fullKey());
69 return StatusCode::FAILURE;
70 }
71
72 writeHandle.addDependency(readHandle);
73 ATH_MSG_DEBUG("Size of CondAttrListCollection " << readHandle.fullKey()
74 << " readCdo->size()= " << readHandle->size());
75 ATH_MSG_DEBUG("Range of input is " << readHandle.getRange()
76 << ", range of output is " << writeHandle.getRange());
77
78 // iterate through data
80 for(itr = readHandle->begin(); itr != readHandle->end(); ++itr) {
81 const coral::AttributeList& atr = itr->second;
82 std::string payload = *(static_cast<const std::string *>((atr["data"]).addressOfData()));
83 ATH_CHECK(loadCablingSchema(payload, *writeCdo));
84 }
85 }
86 }
87
88 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
89 ATH_MSG_INFO("recorded new " << writeHandle.key() << " with range "
90 << writeHandle.getRange()
91 << " into Conditions Store");
92 return StatusCode::SUCCESS;
93}
94
95StatusCode MuonNSW_CablingAlg::loadCablingSchema(const std::string& theJSON,
96 Nsw_CablingMap& cabling_map) const {
97
98 if (theJSON.empty())
99 return StatusCode::SUCCESS;
100 nlohmann::json payload = nlohmann::json::parse(theJSON);
101 const MmIdHelper& mm_helper{m_idHelperSvc->mmIdHelper()};
102 const sTgcIdHelper& sTgc_helper{m_idHelperSvc->stgcIdHelper()};
103 for (const auto& db_channel : payload.items()) {
104 nlohmann::json cabling_payload = db_channel.value();
105
106 std::string stName = cabling_payload["station"];
107 const int eta = cabling_payload["eta"];
108 const int phi = cabling_payload["phi"];
109 const int multilayer = cabling_payload["multilayer"];
110 const int gap = cabling_payload["gasgap"];
111 bool isValid{false};
112 Identifier gap_id{};
113 if (stName[0] == 'M') {
114 gap_id = mm_helper.channelID(stName, eta, phi, multilayer, gap, 1, isValid);
115 } else {
116 const int chType{cabling_payload["channeltype"]};
117 gap_id = sTgc_helper.channelID(stName, eta, phi, multilayer,gap, chType, 1, isValid);
118 }
119 if (!isValid) {
120 ATH_MSG_FATAL("Failed to deduce a valid identifier from st:"
121 << stName << " eta: " << eta << " phi: " << phi
122 << " multilayer: " << multilayer
123 << " gasgap: " << gap);
124 return StatusCode::FAILURE;
125 }
126
127 NswZebraData zebra_connector{};
128 zebra_connector.firstChannel = cabling_payload["FirstZebra"];
129 zebra_connector.lastChannel = cabling_payload["LastZebra"];
130 zebra_connector.shiftChannel = cabling_payload["ZebraShift"];
131 if (!cabling_map.addConnector(gap_id, zebra_connector, msgStream()))
132 return StatusCode::FAILURE;
133 }
134 return StatusCode::SUCCESS;
135}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#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)
bool isValid(const T &p)
Av: we implement here an ATLAS-sepcific convention: all particles which are 99xxxxx are fine.
Definition AtlasPID.h:878
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
An algorithm that can be simultaneously executed in multiple threads.
ChanAttrListMap::const_iterator const_iterator
static EventIDRange infiniteRunLB()
Produces an EventIDRange that is infinite in RunLumi and invalid in Time.
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int gasGap, int channel) const
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Gaudi::Property< std::string > m_JSONFile
StatusCode loadCablingSchema(const std::string &payload, Nsw_CablingMap &cabling_map) const
MuonNSW_CablingAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadCondHandleKeyArray< CondAttrListCollection > m_readCablingKeys
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteCondHandleKey< Nsw_CablingMap > m_writeKey
virtual StatusCode initialize() override
bool addConnector(const Identifier &gapID, const NswZebraData &connector, MsgStream &msg)
const DataObjID & fullKey() const
const EventIDRange & getRange()
const std::string & key() const
void addDependency(const EventIDRange &range)
const EventIDRange & getRange() const
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
const DataObjID & fullKey() const
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int gasGap, int channelType, int channel) const
TStreamerInfo * inf
int16_t lastChannel
int16_t firstChannel
int16_t shiftChannel