ATLAS Offline Software
Loading...
Searching...
No Matches
TgcL0GoodMagMap.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#include "TgcL0GoodMagMap.h"
5
7
8#include <exception>
9#include <fstream>
10#include <string_view>
11
12namespace {
13
14bool parseInteger(const std::string& text, int& value) {
15 try {
16 value = CxxUtils::atoi(text);
17 return true;
18 } catch (const std::exception&) {
19 return false;
20 }
21}
22
23bool parseUnsignedInteger(const std::string& text, unsigned int& value) {
24 int parsed = 0;
25 if (!parseInteger(text, parsed) || parsed < 0) return false;
26 value = static_cast<unsigned int>(parsed);
27 return true;
28}
29
30constexpr bool hasEmptyCsvField(const std::string_view line) {
31 return line.empty() || line.front() == ',' || line.back() == ',' ||
32 line.find(",,") != std::string_view::npos;
33}
34
35} // namespace
36
37namespace L0Muon {
38
39std::unique_ptr<TgcL0GoodMagMap> TgcL0GoodMagMap::loadAscii(
40 const std::string& calibrationPath, std::string& error) {
41 error.clear();
42 std::ifstream input{calibrationPath};
43 if (!input) {
44 error = "cannot open ASCII GoodMag map: " + calibrationPath;
45 return nullptr;
46 }
47
48 auto map = std::unique_ptr<TgcL0GoodMagMap>{new TgcL0GoodMagMap};
49 unsigned int schemaVersion = 0U;
50 bool schemaVersionSeen = false;
51 bool payloadVersionSeen = false;
52 bool etaBinsSeen = false;
53 bool phiBinsSeen = false;
54 bool mapRecordsStarted = false;
55 std::string line;
56 std::size_t lineNumber = 0U;
57 while (std::getline(input, line)) {
58 ++lineNumber;
59 if (line.empty() || line[0] == '#') continue;
60 if (hasEmptyCsvField(line)) {
61 error = "empty CSV field at line " + std::to_string(lineNumber);
62 return nullptr;
63 }
64 const auto fields = CxxUtils::tokenize(line, ',');
65 if (fields.empty()) continue;
66 const std::string& record = fields[0];
67 if (record == "META") {
68 if (mapRecordsStarted) {
69 error = "META row after map records at line " +
70 std::to_string(lineNumber);
71 return nullptr;
72 }
73 if (fields.size() != 3U) {
74 error = "invalid META row at line " + std::to_string(lineNumber);
75 return nullptr;
76 }
77 const std::string& key = fields[1];
78 if (key == "schemaVersion") {
79 if (schemaVersionSeen ||
80 !parseUnsignedInteger(fields[2], schemaVersion)) {
81 error = "invalid or duplicate schemaVersion";
82 return nullptr;
83 }
84 schemaVersionSeen = true;
85 } else if (key == "payloadVersion") {
86 if (payloadVersionSeen || fields[2].empty()) {
87 error = "invalid or duplicate payloadVersion";
88 return nullptr;
89 }
90 map->m_version = fields[2];
91 payloadVersionSeen = true;
92 } else if (key == "etaBins") {
93 if (etaBinsSeen ||
94 !parseUnsignedInteger(fields[2], map->m_etaBins)) {
95 error = "invalid or duplicate etaBins";
96 return nullptr;
97 }
98 etaBinsSeen = true;
99 } else if (key == "phiBinsPerFold") {
100 if (phiBinsSeen ||
101 !parseUnsignedInteger(fields[2], map->m_phiBinsPerFold)) {
102 error = "invalid or duplicate phiBinsPerFold";
103 return nullptr;
104 }
105 phiBinsSeen = true;
106 } else {
107 error = "unknown metadata key at line " +
108 std::to_string(lineNumber) + ": " + key;
109 return nullptr;
110 }
111 continue;
112 }
113
114 mapRecordsStarted = true;
115 if (record != "POOR") {
116 error = "unknown map record at line " + std::to_string(lineNumber) +
117 ": " + record;
118 return nullptr;
119 }
120 if (fields.size() != 3U || map->m_etaBins == 0U ||
121 map->m_phiBinsPerFold == 0U) {
122 error = "invalid POOR row at line " + std::to_string(lineNumber);
123 return nullptr;
124 }
125 int etaBin = -1;
126 int phiFoldBin = -1;
127 if (!parseInteger(fields[1], etaBin) ||
128 !parseInteger(fields[2], phiFoldBin) || etaBin < 0 ||
129 phiFoldBin < 0 || etaBin >= static_cast<int>(map->m_etaBins) ||
130 phiFoldBin >= static_cast<int>(map->m_phiBinsPerFold)) {
131 error = "invalid map bin at line " + std::to_string(lineNumber);
132 return nullptr;
133 }
134 if (map->m_poorBins.empty()) {
135 const std::size_t count = static_cast<std::size_t>(map->m_etaBins) *
136 map->m_phiBinsPerFold;
137 map->m_poorBins.assign(count, false);
138 }
139 const std::size_t index = static_cast<std::size_t>(etaBin) *
140 map->m_phiBinsPerFold +
141 static_cast<std::size_t>(phiFoldBin);
142 if (map->m_poorBins[index]) {
143 error = "duplicate POOR bin at line " + std::to_string(lineNumber);
144 return nullptr;
145 }
146 map->m_poorBins[index] = true;
147 ++map->m_poorBinCount;
148 }
149
150 if (!schemaVersionSeen || schemaVersion != 1U || !payloadVersionSeen ||
151 !etaBinsSeen || !phiBinsSeen || map->m_etaBins == 0U ||
152 map->m_phiBinsPerFold == 0U) {
153 error = "incomplete or unsupported ASCII GoodMag metadata";
154 return nullptr;
155 }
156 if (map->m_poorBins.empty()) {
157 const std::size_t count = static_cast<std::size_t>(map->m_etaBins) *
158 map->m_phiBinsPerFold;
159 map->m_poorBins.assign(count, false);
160 }
161 return map;
162}
163
164bool TgcL0GoodMagMap::isGood(const int etaBin,
165 const int phiFoldBin) const {
166 if (etaBin < 0 || phiFoldBin < 0 ||
167 etaBin >= static_cast<int>(m_etaBins) ||
168 phiFoldBin >= static_cast<int>(m_phiBinsPerFold)) {
169 return false;
170 }
171 const std::size_t index = static_cast<std::size_t>(etaBin) *
173 static_cast<std::size_t>(phiFoldBin);
174 return !m_poorBins[index];
175}
176
177} // namespace L0Muon
static const Attributes_t empty
static std::unique_ptr< TgcL0GoodMagMap > loadAscii(const std::string &calibrationPath, std::string &error)
bool isGood(int etaBin, int phiFoldBin) const
Return false for a masked or out-of-range calibration bin.
std::vector< bool > m_poorBins
STL class.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:148
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition index.py:1