ATLAS Offline Software
TwinTubeMappingCondAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 
6 #include <fstream>
7 
11 #include "nlohmann/json.hpp"
12 namespace Muon{
15  ATH_CHECK(m_idHelperSvc.retrieve());
16  ATH_CHECK(m_readKey.initialize(m_extJSONFile.value().empty()));
17  ATH_CHECK(m_writeKey.initialize());
18 
19  return StatusCode::SUCCESS;
20  }
21  StatusCode TwinTubeMappingCondAlg::execute(const EventContext& ctx) const {
22  ATH_MSG_VERBOSE("execute()");
23  // Write Cond Handle
24  SG::WriteCondHandle writeHandle{m_writeKey, ctx};
25  if (writeHandle.isValid()) {
26  ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
27  << ". In theory this should not be called, but may happen"
28  << " if multiple concurrent events are being processed out of order.");
29  return StatusCode::SUCCESS;
30  }
31  writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteRunLB()));
32 
34  if (m_extJSONFile.value().size()){
35  std::ifstream in_json{m_extJSONFile};
36  if (!in_json.good()) {
37  ATH_MSG_FATAL("Failed to open external JSON file "<<m_extJSONFile);
38  return StatusCode::FAILURE;
39  }
40  in_json >> blob;
41  } else {
42  SG::ReadCondHandle readHandle{m_readKey, ctx};
43 
44  if (!readHandle.isValid()) {
45  ATH_MSG_FATAL("Failed to load cabling map from COOL "<< m_readKey.fullKey());
46  return StatusCode::FAILURE;
47  }
48  writeHandle.addDependency(readHandle);
49  for (const auto& itr : **readHandle) {
50  const coral::AttributeList& atr = itr.second;
51  blob = nlohmann::json::parse(*(static_cast<const std::string*>((atr["data"]).addressOfData())));
53  break;
54  }
55  }
56  auto condObj = std::make_unique<TwinTubeMap>(m_idHelperSvc.get());
57  condObj->setDefaultHVDelay(m_hvDelay);
58  const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
59 
60  std::unordered_map<unsigned int, HedgehogBoardPtr> hedgeHogBoards{};
61  using Mapping = HedgehogBoard::Mapping;
62  for (const auto& boardMapping : blob["HedgehogBoards"].items()) {
63  nlohmann::json payLoad = boardMapping.value();
64  ATH_MSG_VERBOSE("Parse payload "<<payLoad);
65  const unsigned boardId = payLoad["boardId"];
66  const Mapping mapping = payLoad["hedgeHogPins"];
67  const unsigned tubeLayers = payLoad["nTubeLayers"];
68  auto newBoard = std::make_unique<HedgehogBoard>(mapping, tubeLayers, boardId);
69  if (payLoad.find("hvDelayTime") != payLoad.end()) {
70  newBoard->setHVDelayTime(payLoad["hvDelayTime"]);
71  }
72  HedgehogBoardPtr& storeMe = hedgeHogBoards[newBoard->boardId()];
73  if (storeMe) {
74  ATH_MSG_FATAL("There's already a board registered under "<<storeMe->boardId()<<".");
75  return StatusCode::FAILURE;
76  }
77  storeMe = std::move(newBoard);
78  }
79  for (const auto& twinMapping : blob["TwinTubeMapping"].items()) {
80  nlohmann::json payLoad = twinMapping.value();
81  ATH_MSG_VERBOSE("Parse "<<payLoad);
82  const int stIndex = idHelper.stationNameIndex(payLoad["station"]);
83  const int eta = payLoad["eta"];
84  const int phi = payLoad["phi"];
85  const int ml = payLoad["ml"];
86  bool isValid{false};
87  const Identifier detElId{idHelper.channelID(stIndex, eta, phi, ml, 1, 1, isValid)};
88  if(!isValid) {
89  ATH_MSG_FATAL("Failed to build valid identifier from "<<payLoad);
90  return StatusCode::FAILURE;
91  };
92  const std::vector<unsigned int> boardMounting = payLoad["mountedBoards"];
93  for (unsigned int place = 0; place < boardMounting.size(); ++place) {
94  ATH_CHECK(condObj->addHedgeHogBoard(detElId, hedgeHogBoards[boardMounting[place]], place));
95  }
96  }
97  ATH_CHECK(writeHandle.record(std::move(condObj)));
98  return StatusCode::SUCCESS;
99  }
100 }
Muon::TwinTubeMappingCondAlg::m_readKey
SG::ReadCondHandleKey< CondAttrListCollection > m_readKey
Definition: TwinTubeMappingCondAlg.h:31
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
json
nlohmann::json json
Definition: HistogramDef.cxx:9
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
Muon::TwinTubeMappingCondAlg::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: TwinTubeMappingCondAlg.cxx:21
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:225
Muon
This class provides conversion from CSC RDO data to CSC Digits.
Definition: TrackSystemController.h:45
Muon::TwinTubeMappingCondAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: TwinTubeMappingCondAlg.h:30
Muon::HedgehogBoard::Mapping
std::array< uint8_t, nChPerBoard > Mapping
Definition: HedgehogBoard.h:37
find_tgc_unfilled_channelids.mapping
mapping
Definition: find_tgc_unfilled_channelids.py:17
ReadCondHandle.h
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
WriteCondHandle.h
IOVInfiniteRange::infiniteRunLB
static EventIDRange infiniteRunLB()
Produces an EventIDRange that is infinite in RunLumi and invalid in Time.
Definition: IOVInfiniteRange.h:39
MdtIdHelper
Definition: MdtIdHelper.h:61
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Muon::HedgehogBoard::HedgehogBoardPtr
std::shared_ptr< const HedgehogBoard > HedgehogBoardPtr
Definition: HedgehogBoard.h:34
IOVInfiniteRange.h
Muon::TwinTubeMappingCondAlg::m_hvDelay
Gaudi::Property< double > m_hvDelay
Definition: TwinTubeMappingCondAlg.h:37
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
TwinTubeMappingCondAlg.h
Muon::TwinTubeMappingCondAlg::m_writeKey
SG::WriteCondHandleKey< TwinTubeMap > m_writeKey
Definition: TwinTubeMappingCondAlg.h:32
Muon::TwinTubeMappingCondAlg::initialize
StatusCode initialize() override final
Definition: TwinTubeMappingCondAlg.cxx:14
Muon::TwinTubeMappingCondAlg::m_extJSONFile
Gaudi::Property< std::string > m_extJSONFile
Definition: TwinTubeMappingCondAlg.h:34
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
CaloCondBlobAlgs_fillNoiseFromASCII.blob
blob
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:96
Muon::HedgehogBoardPtr
HedgehogBoard::HedgehogBoardPtr HedgehogBoardPtr
Definition: TwinTubeMappingCondAlg.cxx:13
Identifier
Definition: IdentifierFieldParser.cxx:14