43 {
45 std::ifstream
input{calibrationPath};
46 if (!input) {
47 error =
"cannot open ASCII GoodMag map: " + calibrationPath;
48 return nullptr;
49 }
50
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;
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 }
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 }
77 error =
"invalid META row at line " + std::to_string(lineNumber);
78 return nullptr;
79 }
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 }
130 if (!parseInteger(fields[1],
etaBin) ||
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 +
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}
static const Attributes_t empty
TgcL0GoodMagMap()=default
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.