ATLAS Offline Software
MuonNSW_CablingAlg.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 "MuonNSW_CablingAlg.h"
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()));
31  ATH_CHECK(m_idHelperSvc.retrieve());
32  return StatusCode::SUCCESS;
33 }
34 
35 StatusCode 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 retrive 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 
96  Nsw_CablingMap& cabling_map) const {
97 
98  if (theJSON.empty())
99  return StatusCode::SUCCESS;
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 }
MuonNSW_CablingAlg::initialize
virtual StatusCode initialize() override
Definition: MuonNSW_CablingAlg.cxx:27
Nsw_CablingMap
Definition: Nsw_CablingMap.h:18
MuonNSW_CablingAlg::MuonNSW_CablingAlg
MuonNSW_CablingAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonNSW_CablingAlg.cxx:23
NswZebraData::firstChannel
int16_t firstChannel
Definition: NswZebraData.h:20
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
MuonNSW_CablingAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: MuonNSW_CablingAlg.cxx:35
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
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
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
MuonNSW_CablingAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonNSW_CablingAlg.h:32
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
MuonNSW_CablingAlg.h
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
isValid
bool isValid(const T &p)
Av: we implement here an ATLAS-sepcific convention: all particles which are 99xxxxx are fine.
Definition: AtlasPID.h:620
sTgcIdHelper::sTgcChannelTypes
sTgcChannelTypes
Definition: sTgcIdHelper.h:190
AthenaAttributeList.h
CaloSwCorrections.gap
def gap(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:212
inf
TStreamerInfo * inf
Definition: liststreamerinfos.cxx:12
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
MuonNSW_CablingAlg::loadCablingSchema
StatusCode loadCablingSchema(const std::string &payload, Nsw_CablingMap &cabling_map) const
Definition: MuonNSW_CablingAlg.cxx:95
MuonNSW_CablingAlg::m_JSONFile
Gaudi::Property< std::string > m_JSONFile
Definition: MuonNSW_CablingAlg.h:37
Nsw_CablingMap::addConnector
bool addConnector(const Identifier &gapID, const NswZebraData &connector, MsgStream &msg)
Definition: Nsw_CablingMap.cxx:76
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
IOVInfiniteRange::infiniteRunLB
static EventIDRange infiniteRunLB()
Produces an EventIDRange that is infinite in RunLumi and invalid in Time.
Definition: IOVInfiniteRange.h:39
MdtIdHelper.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonNSW_CablingAlg::m_writeKey
SG::WriteCondHandleKey< Nsw_CablingMap > m_writeKey
Definition: MuonNSW_CablingAlg.h:35
IOVInfiniteRange.h
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
NswZebraData
Definition: NswZebraData.h:18
sTgcIdHelper
Definition: sTgcIdHelper.h:55
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
SG::ReadCondHandleKey< CondAttrListCollection >
MmIdHelper
Definition: MmIdHelper.h:54
CondAttrListCollection::const_iterator
ChanAttrListMap::const_iterator const_iterator
Definition: CondAttrListCollection.h:63
TransientAddress.h
MuonNSW_CablingAlg::m_readCablingKeys
SG::ReadCondHandleKeyArray< CondAttrListCollection > m_readCablingKeys
Definition: MuonNSW_CablingAlg.h:33
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Identifier
Definition: IdentifierFieldParser.cxx:14