ATLAS Offline Software
Loading...
Searching...
No Matches
MmCTPCondDbAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
10#include <CoralBase/Blob.h>
12#include <fstream>
13
14
15
16// Initialize
18
19 ATH_CHECK(m_idHelperSvc.retrieve());
20 if( !m_idHelperSvc->hasMM() ){
21 ATH_MSG_ERROR("MuonIdHelperSvc does not have MM, the CTP calibration should not be run!");
22 return StatusCode::FAILURE;
23 }
24
25 ATH_CHECK(m_readKeyDb.initialize(m_readFromJSON.value().empty()));
26 if (m_readFromJSON.value().size()) {
27 ATH_MSG_INFO("Read the uncertainty data from a JSON file "<<m_readFromJSON);
28 } else if (!m_readKeyDb.empty()) {
29 ATH_MSG_INFO("Read the uncertainty data from a COOL databse: " << m_readKeyDb.fullKey() );
30 } else {
31 ATH_MSG_FATAL("Neither an extrenal JSON nor a COOL folder were defined. Please check");
32 return StatusCode::FAILURE;
33 }
34 ATH_CHECK(m_writeKey.initialize());
35
36
37
38 return StatusCode::SUCCESS;
39}
40
41StatusCode MmCTPCondDbAlg::execute(const EventContext& ctx) const {
42 ATH_MSG_DEBUG("execute " << name());
43 // launching Write Cond Handle
44 SG::WriteCondHandle writeHandle{m_writeKey, ctx};
45 if (writeHandle.isValid()) {
46 ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
47 << " In theory this should not be called, but may happen"
48 << " if multiple concurrent events are being processed out of order.");
49 return StatusCode::SUCCESS;
50 }
51
52 std::unique_ptr<Muon::mmCTPClusterCalibData> writeCdo{std::make_unique<Muon::mmCTPClusterCalibData>(m_idHelperSvc.get())};
53 writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteTime()));
54
55 if (!m_readFromJSON.value().empty()) {
56 std::ifstream inStream{PathResolverFindCalibFile(m_readFromJSON)};
57 if (!inStream.good()) {
58 ATH_MSG_FATAL("No such file or directory");
59 return StatusCode::FAILURE;
60 }
61 nlohmann::json lines;
62 inStream >> lines;
63 ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
64 } else{
65
66 SG::ReadCondHandle readHandle{m_readKeyDb, ctx};
67 if (!readHandle.isValid()) {
68 ATH_MSG_FATAL("Failed to load NSW error calibration folder from "<<m_readKeyDb.fullKey());
69 return StatusCode::FAILURE;
70 }
71
72 for (CondAttrListCollection::const_iterator itr = readHandle->begin();
73 itr != readHandle->end(); ++itr) {
74 const coral::AttributeList& atr = itr->second;
75 std::string data = *(static_cast<const std::string*>((atr["data"]).addressOfData()));
76 nlohmann::json lines = nlohmann::json::parse(data);
77 ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
78 }
79 }
80
81 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
82 return StatusCode::SUCCESS;
83}
84
85StatusCode MmCTPCondDbAlg::parseDataFromJSON(const nlohmann::json& lines,
86 Muon::mmCTPClusterCalibData& ctpClusterCondData) const {
87 for (auto& corr : lines.items()) {
88 nlohmann::json line = corr.value();
89
90 //Check entry keys, shall we make it to just check the first entry and then assume the rest is okay? maybe it doesn't take that much time to do it on the whole thing...
91 if (!line.contains("station") || !line.contains("phi") || !line.contains("eta") || !line.contains("multilayer") || !line.contains("gasGap")) {
92 ATH_MSG_ERROR("Missing expected keys in the JSON file");
93 return StatusCode::FAILURE; // Handle error appropriately
94 }
95
96 // Ensure the JSON array exists and has exactly 2 elements
97 if (!line.contains("P1Parameters") || !line["P1Parameters"].is_array() || line["P1Parameters"].size() != 2) {
98 ATH_MSG_ERROR("Get unexpected size of the P1Parameters array");
99 return StatusCode::FAILURE; // Handle error appropriately
100 }
101
103 const std::string stationType = line["station"];
104 const int stationPhi = line["phi"];
105 const int stationEta = line["eta"];
106 const int multiLayer = line["multilayer"];
107 const int gasGap = line["gasGap"];
108
109 // Convert JSON array to std::array<double, 2>
110 std::array<double, 2> modelPars;
111 std::copy_n(line["P1Parameters"].begin(), 2, modelPars.begin());
112
113 Identifier errorCalibId{};
114
115 //Using PCB 1 as default
116 errorCalibId = m_idHelperSvc->mmIdHelper().channelID(stationType, stationEta, stationPhi, multiLayer, gasGap, 1 );
117
119
120 ATH_CHECK(ctpClusterCondData.storeConstants(errorCalibId, std::move(constants)));
121
122
123 }
124 return StatusCode::SUCCESS;
125}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
ChanAttrListMap::const_iterator const_iterator
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
Gaudi::Property< std::string > m_readFromJSON
Parse data from COOL.
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyDb
virtual StatusCode execute(const EventContext &ctx) const override final
SG::WriteCondHandleKey< Muon::mmCTPClusterCalibData > m_writeKey
virtual StatusCode initialize() override final
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
StatusCode parseDataFromJSON(const nlohmann::json &lines, Muon::mmCTPClusterCalibData &ctpClusterCondData) const
StatusCode storeConstants(const Identifier &gasGapId, CTPParameters &&newConstants)
void addDependency(const EventIDRange &range)
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
const DataObjID & fullKey() const