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 <algorithm>
9#include <cmath>
10#include <exception>
11#include <fstream>
12#include <numbers>
13#include <string_view>
14
15namespace {
16
17bool parseInteger(const std::string& text, int& value) {
18 try {
19 value = CxxUtils::atoi(text);
20 return true;
21 } catch (const std::exception&) {
22 return false;
23 }
24}
25
26bool parseUnsignedInteger(const std::string& text, unsigned int& value) {
27 int parsed = 0;
28 if (!parseInteger(text, parsed) || parsed < 0) return false;
29 value = static_cast<unsigned int>(parsed);
30 return true;
31}
32
33constexpr bool hasEmptyCsvField(const std::string_view line) {
34 return line.empty() || line.front() == ',' || line.back() == ',' ||
35 line.find(",,") != std::string_view::npos;
36}
37
38} // namespace
39
40namespace L0Muon {
41
42std::unique_ptr<TgcL0GoodMagMap> TgcL0GoodMagMap::loadAscii(
43 const std::string& calibrationPath, std::string& error) {
44 error.clear();
45 std::ifstream input{calibrationPath};
46 if (!input) {
47 error = "cannot open ASCII GoodMag map: " + calibrationPath;
48 return nullptr;
49 }
50
51 auto map = std::unique_ptr<TgcL0GoodMagMap>{new TgcL0GoodMagMap};
52 unsigned int schemaVersion = 0U;
53 bool schemaVersionSeen = false;
54 bool payloadVersionSeen = false;
55 bool etaBinsSeen = false;
56 bool phiBinsSeen = false;
57 bool mapRecordsStarted = false;
58 std::string line;
59 std::size_t lineNumber = 0U;
60 while (std::getline(input, line)) {
61 ++lineNumber;
62 if (line.empty() || line[0] == '#') continue;
63 if (hasEmptyCsvField(line)) {
64 error = "empty CSV field at line " + std::to_string(lineNumber);
65 return nullptr;
66 }
67 const auto fields = CxxUtils::tokenize(line, ',');
68 if (fields.empty()) continue;
69 const std::string& record = fields[0];
70 if (record == "META") {
71 if (mapRecordsStarted) {
72 error = "META row after map records at line " +
73 std::to_string(lineNumber);
74 return nullptr;
75 }
76 if (fields.size() != 3U) {
77 error = "invalid META row at line " + std::to_string(lineNumber);
78 return nullptr;
79 }
80 const std::string& key = fields[1];
81 if (key == "schemaVersion") {
82 if (schemaVersionSeen ||
83 !parseUnsignedInteger(fields[2], schemaVersion)) {
84 error = "invalid or duplicate schemaVersion";
85 return nullptr;
86 }
87 schemaVersionSeen = true;
88 } else if (key == "payloadVersion") {
89 if (payloadVersionSeen || fields[2].empty()) {
90 error = "invalid or duplicate payloadVersion";
91 return nullptr;
92 }
93 map->m_version = fields[2];
94 payloadVersionSeen = true;
95 } else if (key == "etaBins") {
96 if (etaBinsSeen ||
97 !parseUnsignedInteger(fields[2], map->m_etaBins)) {
98 error = "invalid or duplicate etaBins";
99 return nullptr;
100 }
101 etaBinsSeen = true;
102 } else if (key == "phiBinsPerFold") {
103 if (phiBinsSeen ||
104 !parseUnsignedInteger(fields[2], map->m_phiBinsPerFold)) {
105 error = "invalid or duplicate phiBinsPerFold";
106 return nullptr;
107 }
108 phiBinsSeen = true;
109 } else {
110 error = "unknown metadata key at line " +
111 std::to_string(lineNumber) + ": " + key;
112 return nullptr;
113 }
114 continue;
115 }
116
117 mapRecordsStarted = true;
118 if (record != "POOR") {
119 error = "unknown map record at line " + std::to_string(lineNumber) +
120 ": " + record;
121 return nullptr;
122 }
123 if (fields.size() != 3U || map->m_etaBins == 0U ||
124 map->m_phiBinsPerFold == 0U) {
125 error = "invalid POOR row at line " + std::to_string(lineNumber);
126 return nullptr;
127 }
128 int etaBin = -1;
129 int phiFoldBin = -1;
130 if (!parseInteger(fields[1], etaBin) ||
131 !parseInteger(fields[2], phiFoldBin) || etaBin < 0 ||
132 phiFoldBin < 0 || etaBin >= static_cast<int>(map->m_etaBins) ||
133 phiFoldBin >= static_cast<int>(map->m_phiBinsPerFold)) {
134 error = "invalid map bin at line " + std::to_string(lineNumber);
135 return nullptr;
136 }
137 if (map->m_poorBins.empty()) {
138 const std::size_t count = static_cast<std::size_t>(map->m_etaBins) *
139 map->m_phiBinsPerFold;
140 map->m_poorBins.assign(count, false);
141 }
142 const std::size_t index = static_cast<std::size_t>(etaBin) *
143 map->m_phiBinsPerFold +
144 static_cast<std::size_t>(phiFoldBin);
145 if (map->m_poorBins[index]) {
146 error = "duplicate POOR bin at line " + std::to_string(lineNumber);
147 return nullptr;
148 }
149 map->m_poorBins[index] = true;
150 ++map->m_poorBinCount;
151 }
152
153 if (!schemaVersionSeen || schemaVersion != 1U || !payloadVersionSeen ||
154 !etaBinsSeen || !phiBinsSeen || map->m_etaBins == 0U ||
155 map->m_phiBinsPerFold == 0U) {
156 error = "incomplete or unsupported ASCII GoodMag metadata";
157 return nullptr;
158 }
159 if (map->m_poorBins.empty()) {
160 const std::size_t count = static_cast<std::size_t>(map->m_etaBins) *
161 map->m_phiBinsPerFold;
162 map->m_poorBins.assign(count, false);
163 }
164 return map;
165}
166
168 const int phiFoldBin) const {
169 if (etaBin < 0 || phiFoldBin < 0 ||
170 etaBin >= static_cast<int>(m_etaBins) ||
171 phiFoldBin >= static_cast<int>(m_phiBinsPerFold)) {
172 return false;
173 }
174 const std::size_t index = static_cast<std::size_t>(etaBin) *
176 static_cast<std::size_t>(phiFoldBin);
177 return !m_poorBins[index];
178}
179
180int TgcL0GoodMagMap::etaBin(const float eta, const float absEtaMin,
181 const float absEtaMax) const {
182 if (!std::isfinite(eta) || !std::isfinite(absEtaMin) ||
183 !std::isfinite(absEtaMax) || !(absEtaMax > absEtaMin)) {
184 return -1;
185 }
186 const float absEta = std::abs(eta);
187 if (absEta < absEtaMin || absEta > absEtaMax) return -1;
188 if (absEta == absEtaMax) return static_cast<int>(m_etaBins) - 1;
189 const float scaled =
190 (absEta - absEtaMin) * m_etaBins / (absEtaMax - absEtaMin);
191 const int bin = static_cast<int>(std::floor(scaled));
192 return bin >= 0 && bin < static_cast<int>(m_etaBins) ? bin : -1;
193}
194
195int TgcL0GoodMagMap::phiFoldBin(const float phi) const {
196 if (!std::isfinite(phi)) return -1;
197 const float period = 2.F * std::numbers::pi_v<float> / 8.F;
198 float folded = std::fmod(phi, period);
199 if (folded < 0.F) folded += period;
200 float fraction = folded / period;
201 if (fraction >= 1.F) fraction = 0.F;
202 return std::clamp(
203 static_cast<int>(std::floor(fraction * m_phiBinsPerFold)), 0,
204 static_cast<int>(m_phiBinsPerFold) - 1);
205}
206
207bool TgcL0GoodMagMap::isGood(const float eta, const float phi,
208 const float absEtaMin,
209 const float absEtaMax) const {
210 return isGood(etaBin(eta, absEtaMin, absEtaMax), phiFoldBin(phi));
211}
212
213} // namespace L0Muon
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
static const Attributes_t empty
int phiFoldBin(float phi) const
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
int etaBin(float eta, float absEtaMin, float absEtaMax) const
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