ATLAS Offline Software
Loading...
Searching...
No Matches
CscILinesCondAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "CscILinesCondAlg.h"
6
7#include <fstream>
8#include <memory>
14
15namespace Muon{
17 ATH_CHECK(m_readKey.initialize(m_readFromJSON.value().empty()));
18 ATH_CHECK(m_writeKey.initialize());
19 ATH_CHECK(m_idHelperSvc.retrieve());
20 if (!m_readFromJSON.value().empty()){
21 ATH_MSG_INFO("Load Intenal Csc alignment from JSON "<<m_readFromJSON);
22 } else {
23 ATH_MSG_INFO("Load Internal Csc alignment from COOL <"<<m_readKey.key()<<">");
24 }
25 return StatusCode::SUCCESS;
26
27}
28StatusCode CscILinesCondAlg::execute(const EventContext& ctx) const {
29 SG::WriteCondHandle writeHandle{m_writeKey, ctx};
30 if (writeHandle.isValid()) {
31 ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
32 << ". In theory this should not be called, but may happen"
33 << " if multiple concurrent events are being processed out of order.");
34 return StatusCode::SUCCESS;
35 }
36 writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteTime()));
37 auto writeCdo{std::make_unique<ALineContainer>()};
38 if (!m_readKey.empty()) {
39 SG::ReadCondHandle readHandle{m_readKey, ctx};
40 if (!readHandle.isValid()) {
41 ATH_MSG_FATAL("Failed to load I lines from COOL "<<m_readKey.fullKey());
42 return StatusCode::FAILURE;
43 }
44 writeHandle.addDependency(readHandle);
45 // =======================
46 // Retrieve the collection of strings read out from the DB
47 // =======================
48 // unpack the strings in the collection and update the
49 // ALlineContainer in TDS
50 for (CondAttrListCollection::const_iterator itr = readHandle->begin();
51 itr != readHandle->end(); ++itr) {
52
53 const coral::AttributeList& atr = itr->second;
54 std::string data;
55 if (atr["data"].specification().type() == typeid(coral::Blob)) {
56 ATH_MSG_VERBOSE("Loading data as a BLOB, uncompressing...");
58 ATH_MSG_FATAL("Cannot uncompress BLOB! Aborting...");
59 return StatusCode::FAILURE;
60 }
61 } else {
62 data = *(static_cast<const std::string*>((atr["data"]).addressOfData()));
63 }
64 ATH_MSG_DEBUG("Data load is " << data << " FINISHED HERE ");
65 nlohmann::json lines = nlohmann::json::array();
66
67 // new format -----------------------------------
68 if (m_newFormat2020) {
69 nlohmann::json j = nlohmann::json::parse(data);
70 lines = j["corrections"];
71 } else {
72 // old format -----------------------------------
74 }
75 ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
76 }
77 if (!m_readFromJSON.value().empty()) {
78 std::ifstream inStream{PathResolverFindCalibFile(m_readFromJSON)};
79 if (!inStream.good()) {
80 ATH_MSG_FATAL("No such file or directory");
81 return StatusCode::FAILURE;
82 }
83 nlohmann::json lines;
84 inStream >> lines;
85 ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
86 }
87 }
88
89
90 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
91 ATH_MSG_INFO("Saved successfully internal CSC alignment "<<m_writeKey.fullKey()<<" with validity range "<<writeHandle.getRange());
92 return StatusCode::SUCCESS;
93}
94
95
96StatusCode CscILinesCondAlg::loadDataFromLegacy(const std::string& data, nlohmann::json& json) const {
97 // Parse corrections
98 constexpr std::string_view delimiter{"\n"};
99 json = nlohmann::json::array();
100 auto lines = CxxUtils::tokenize(data, delimiter);
101 for (const std::string& blobline : lines) {
102 nlohmann::json line;
103 constexpr std::string_view delimiter {":"};
104 auto tokens = CxxUtils::tokenize(blobline, delimiter);
105 // Check if tokens is not empty
106 if (tokens.empty()) {
107 ATH_MSG_FATAL("Empty string retrieved from DB ");
108 return StatusCode::FAILURE;
109 }
110 const std::string_view type = tokens[0];
111 // Parse line
112 if ('#' == type[0]) {
113 // skip it
114 continue;
115 }
116 if (type.compare(0, 4, "Corr") == 0) {
117 //# Amdb like clob for ilines using geometry tag ISZT-R06-02
118 //# ISZT_DATA_ID VERS TYP JFF JZZ JOB JLAY TRAS TRAZ TRAT ROTS ROTZ ROTT
119 //
120 //.... example
121 // Corr: CSL 1 -1 3 1 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
122
123 constexpr std::string_view delimiter{" "};
124 auto tokens = CxxUtils::tokenize(blobline, delimiter);
125 if (tokens.size() != 12) {
126 ATH_MSG_FATAL("Invalid length in string retrieved from DB in folder. String length is "
127 << tokens.size());
128 return StatusCode::FAILURE;
129 }
130
131 ATH_MSG_VERBOSE("Parsing Line = ");
132 for (std::string_view token : tokens) ATH_MSG_VERBOSE(token << " | ");
133
134
135 // Start parsing
136 int ival = 1;
137 // Station Component identification
138 line["typ"] = std::string(tokens[ival++]);
139 line["jff"] = CxxUtils::atoi(tokens[ival++]);
140 line["jzz"] = CxxUtils::atoi(tokens[ival++]);
141 line["job"] = CxxUtils::atoi(tokens[ival++]);
142 line["jlay"] = CxxUtils::atoi(tokens[ival++]);
143
144 // I-line
145 line["tras"] = CxxUtils::atof(tokens[ival++]);
146 line["traz"] = CxxUtils::atof(tokens[ival++]);
147 line["trat"] = CxxUtils::atof(tokens[ival++]);
148 line["rots"] = CxxUtils::atof(tokens[ival++]);
149 line["rotz"] = CxxUtils::atof(tokens[ival++]);
150 line["rott"] = CxxUtils::atof(tokens[ival++]);
151 }
152 if (line.empty()) continue;
153 json.push_back(std::move(line));
154 }
155 return StatusCode::SUCCESS;
156}
157StatusCode CscILinesCondAlg::parseDataFromJSON(const nlohmann::json& lines,
158 ALineContainer& writeCdo) const{
159
160 for (auto& corr : lines.items()) {
161 nlohmann::json line = corr.value();
162 ALinePar newILine{};
163
164 // Station Component identification
165 const int jff = line["jff"];
166 const int jzz = line["jzz"];
167 const int job = line["job"];
168 const int jlay = line["jlay"];
169 const std::string stationType = line["typ"];
170 newILine.setAmdbId(stationType, jzz, jff, job);
171
172 Identifier id{0};
173 bool is_valid{false};
174 if (stationType[0] == 'C') {
175 // csc case
176 constexpr int chamberLayer = 2;
177 if (job != 3) ATH_MSG_WARNING("job = " << job << " is not 3 => chamberLayer should be 1 - not existing ! setting 2");
178 id = m_idHelperSvc->cscIdHelper().channelID(stationType, jzz, jff, chamberLayer, jlay, 0, 1, is_valid);
179 ATH_MSG_VERBOSE("identifier being assigned is " << m_idHelperSvc->toString(id));
180 }
181 if (!is_valid) {
182 ATH_MSG_ERROR("There is a non CSC chamber in the list of CSC internal alignment parameters.");
183 return StatusCode::FAILURE;
184 }
185 newILine.setIdentifier(id);
186
187 newILine.setParameters(line["tras"], line["traz"], line["trat"],
188 line["rots"], line["rotz"], line["rott"]);
189 // new Iline
190 const auto insertItr = writeCdo.insert(newILine);
191 if (!insertItr.second) {
192 ATH_MSG_FATAL("Could not insert "<<newILine<<" as "<<(*insertItr.first)<<" has already been safed before" );
193 return StatusCode::FAILURE;
194
195 }
196 }
197 return StatusCode::SUCCESS;
198}
199}
#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_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::set< ALinePar, std::less<> > ALineContainer
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
nlohmann::json json
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
void setParameters(float s, float z, float t, float rotS, float rotZ, float rotT)
Definition ALinePar.cxx:26
ChanAttrListMap::const_iterator const_iterator
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
void setIdentifier(const Identifier &id)
Setters and getters for the Athena Identifier.
void setAmdbId(const std::string &stName, int stEta, int stPhi, int stJob)
AMDB identifiers. They're often not the same as the ATLAS ones (TGCs)
SG::ReadCondHandleKey< CondAttrListCollection > m_readKey
SG::WriteCondHandleKey< ALineContainer > m_writeKey
virtual StatusCode initialize() override
virtual StatusCode execute(const EventContext &ctx) const override
Gaudi::Property< std::string > m_readFromJSON
Load the alignment parameters from a JSON file.
Gaudi::Property< bool > m_newFormat2020
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
StatusCode loadDataFromLegacy(const std::string &data, nlohmann::json &json) const
Load the Alignment data from the legacy format where the channels are parsed line wise The data is th...
StatusCode parseDataFromJSON(const nlohmann::json &lines, ALineContainer &writeCdo) const
Parse the JSON blob to fill the I Line container.
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
bool readBlobAsString(const coral::Blob &, std::string &)
std::vector< std::string > tokenize(const std::string &the_str, std::string_view delimiters)
Splits the string into smaller substrings.
double atof(std::string_view str)
Converts a string into a double / float.
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.