21 namespace itk::fragment {
34 std::vector<Hit> hits;
35 std::vector<uint64_t> stream;
39 void sort_sims(std::map<uint32_t, SimData>& sims) {
41 for (
auto &[
id, sim] : sims) {
43 std::sort(sim.hits.begin(), sim.hits.end(),
44 [](
const Hit& a,
const Hit& b) {
45 uint64_t aval = uint64_t(a.row) << 32 | a.col << 16 | a.tot;
46 uint64_t bval = uint64_t(b.row) << 32 | b.col << 16 | b.tot;
53 void remove_bad_sims(std::map<uint32_t, SimData>& sims) {
55 std::vector<uint32_t> ids;
57 for (
auto &[
id, sim] : sims) {
59 uint64_t prev_val = -1;
60 for (
auto& hit : sim.hits) {
61 uint64_t val = uint64_t(hit.row) << 32 | hit.col << 16;
62 if (prev_val == val) {
63 ids.push_back(sim.stream_id);
64 log.
error(
"stream_id: 0x{:x} | duplicated pixel [col: {:3}, row: {:3}]",
65 sim.stream_id,
int(hit.col),
int(hit.row),
int(hit.tot));
72 for (
auto id : ids) sims.erase(
id);
74 log.
warn(
"Removed bad streams due to duplicated pixel hits: {}", ids.size());
78 std::map<uint32_t, SimData> load_stream(
const std::string& fname) {
81 std::map<uint32_t, SimData> sims;
92 fin.open(fname, std::ios::in);
95 log.
error(
"cannot open file.");
96 return std::map<uint32_t, SimData>{};
100 int prev_stream_id = -1;
101 int prev_chip_id = -1;
103 while (getline(fin, line)) {
107 char stype[200] =
"";
109 uint32_t stream_id = -1;
110 std::sscanf(line.c_str(),
"%s %x %s", sid, &stream_id, stype);
112 if (strcmp(sid,
"OnlineID:") != 0)
continue;
114 auto& sim = sims[stream_id];
115 sim.stream_id = stream_id;
118 if (strcmp(stype,
"pixelID:") == 0) {
125 int chip_id, col, row, tot;
130 std::sscanf(line.c_str(),
"%s %x %s %s %s %s %s %x %s %d %s %d %s %d",
131 sid, &stream_id, spix, s, s, s, schip, &chip_id, stot, &tot, scol, &col, srow, &row);
132 if (strcmp(scol,
"col:") != 0 or strcmp(srow,
"row:") != 0 or strcmp(stot,
"ToT:") != 0) {
133 log.
error(
"incorrect line: {}", line);
134 return std::map<uint32_t, SimData>{};
136 if (stream_id == -1 || chip_id == -1) {
137 log.
error(
"undefined stream: {} or chip: {}", stream_id, chip_id);
140 if (stream_id == prev_stream_id && chip_id != prev_chip_id) {
141 log.
error(
"chip id mismatch");
145 sim.hits.emplace_back(col, row, tot);
147 if (strcmp(stype,
"Stream:") == 0) {
151 auto buff = line.c_str();
153 std::sscanf(buff,
"%s %x %s %n", sid, &stream_id, sstr, &cnt);
158 std::vector<uint64_t> stream;
163 std::sscanf(buff,
" %s%n", shex, &cnt);
166 uint32_t data = strtol(shex, NULL, 16);
168 data64 = data64 << 32 | data;
169 if (i % 2 == 1) stream.push_back(data64);
173 sim.stream = std::move(stream);
175 for (
auto data : stream) {
179 log.
warn(
"line: {}", line);
187 remove_bad_sims(sims);
189 for (
auto &[
id, sim] : sims) {
191 log.
trace(
"id: 0x{:08x} hits.count: {}, stream.size: {}",
id, sim.hits.size(), sim.stream.size());
193 log.
debug(
"sims.size: {}", sims.size());
Logger class definition, wrapper around CoreLogger, adds templated methods for different logging leve...
Definition: logger.hpp:108
void error(fmt::format_string< Args... > fmt, Args &&...args)
Logs a message with error level.
Definition: logger.hpp:164
void debug(fmt::format_string< Args... > fmt, Args &&...args)
Logs a message with debug level.
Definition: logger.hpp:140
void trace(fmt::format_string< Args... > fmt, Args &&...args)
Logs a message with trace level.
Definition: logger.hpp:132
void warn(fmt::format_string< Args... > fmt, Args &&...args)
Logs a message with warning level.
Definition: logger.hpp:156
Definition: loadstream.hpp:26
Definition: loadstream.hpp:32