Orion
high-rate readout
loadstream.hpp
Go to the documentation of this file.
1 // ╭─╮╭─╴╷╭─╮┌─╮
2 // ╰─╯╵ ╵╰─╯╵ ╵
8 #pragma once
9 
10 #include <vector>
11 #include <span>
12 #include <map>
13 
14 #include <iostream>
15 #include <fstream>
16 #include <string>
17 #include<functional>
18 
19 #include "itk/logger.hpp"
20 
21 namespace itk::fragment {
22 
23 extern itk::Logger log;
24 
25 
26 struct Hit {
27  uint16_t col;
28  uint16_t row;
29  uint16_t tot;
30 };
31 
32 struct SimData {
33  uint32_t stream_id;
34  std::vector<Hit> hits;
35  std::vector<uint64_t> stream;
36 };
37 
38 
39 void sort_sims(std::map<uint32_t, SimData>& sims) {
40 
41  for (auto &[id, sim] : sims) {
42  // sort hits in (row, col, tot) order
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;
47  return aval < bval;
48  });
49  }
50 }
51 
52 
53 void remove_bad_sims(std::map<uint32_t, SimData>& sims) {
54 
55  std::vector<uint32_t> ids;
56 
57  for (auto &[id, sim] : sims) {
58  // sim.hits should be already sorted
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));
66  break;
67  }
68  prev_val = val;
69  }
70  }
71 
72  for (auto id : ids) sims.erase(id);
73 
74  log.warn("Removed bad streams due to duplicated pixel hits: {}", ids.size());
75 }
76 
77 
78 std::map<uint32_t, SimData> load_stream(const std::string& fname) {
79  std::fstream fin;
80 
81  std::map<uint32_t, SimData> sims;
82 
83  // // Open file for writing
84  // fout.open("xyz.txt", ios::out);
85 
86  // // Checking if open.
87  // if (fout.is_open()) {
88  // fout << "My Function \n";
89  // fout.close();
90  // }
91 
92  fin.open(fname, std::ios::in);
93 
94  if (!fin.is_open()) {
95  log.error("cannot open file.");
96  return std::map<uint32_t, SimData>{};
97  }
98 
99  std::string line;
100  int prev_stream_id = -1;
101  int prev_chip_id = -1;
102 
103  while (getline(fin, line)) {
104  std::string str;
105  char s[200];
106  char sid[200] = "";
107  char stype[200] = "";
108  char spix[200];
109  uint32_t stream_id = -1;
110  std::sscanf(line.c_str(), "%s %x %s", sid, &stream_id, stype);
111 
112  if (strcmp(sid, "OnlineID:") != 0) continue;
113 
114  auto& sim = sims[stream_id];
115  sim.stream_id = stream_id;
116 
117 
118  if (strcmp(stype, "pixelID:") == 0) {
119 
120  char schip[200];
121  char scol[200];
122  char srow[200];
123  char stot[200];
124  uint64_t pix_id;
125  int chip_id, col, row, tot;
126 
127  stream_id = -1;
128  chip_id = -1;
129  // OnlineID: 0x11191002 pixelID: 0x44644391b000000 IDoff: 0x446440000000000 Chip: 2 ToT: 5 col: 216 row: 114
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>{};
135  }
136  if (stream_id == -1 || chip_id == -1) {
137  log.error("undefined stream: {} or chip: {}", stream_id, chip_id);
138  abort();
139  }
140  if (stream_id == prev_stream_id && chip_id != prev_chip_id) {
141  log.error("chip id mismatch");
142  abort();
143  }
144  // log.info("{} {:3} | {} {:3} | {} {:2}", scol, col, srow, row, stot, tot);
145  sim.hits.emplace_back(col, row, tot);
146  } else
147  if (strcmp(stype, "Stream:") == 0) {
148  char sstr[200];
149  char shex[200];
150  // OnlineID: 0x1012c800 Stream: a9883017 35cb4300
151  auto buff = line.c_str();
152  int cnt = 0;
153  std::sscanf(buff, "%s %x %s %n", sid, &stream_id, sstr, &cnt);
154  buff += cnt;
155 
156  // log.info("{} 0x{:x} | {} {}", sid, stream_id, sstr, buff);
157 
158  std::vector<uint64_t> stream;
159  uint64_t data64 = 0;
160  int i = 0;
161  while (true) {
162  cnt = 0;
163  std::sscanf(buff, " %s%n", shex, &cnt);
164  if (cnt == 0) break;
165  buff += cnt;
166  uint32_t data = strtol(shex, NULL, 16);
167  // log.info(" {} | 0x{:08x}", shex, data);
168  data64 = data64 << 32 | data;
169  if (i % 2 == 1) stream.push_back(data64);
170  i++;
171  }
172 
173  sim.stream = std::move(stream);
174 
175  for (auto data : stream) {
176  // log.info(" 0x{:016x}", data);
177  }
178  } else {
179  log.warn("line: {}", line);
180  }
181  } // while
182 
183  // Close the file object.
184  fin.close();
185 
186  sort_sims(sims);
187  remove_bad_sims(sims);
188 
189  for (auto &[id, sim] : sims) {
190 
191  log.trace("id: 0x{:08x} hits.count: {}, stream.size: {}", id, sim.hits.size(), sim.stream.size());
192  }
193  log.debug("sims.size: {}", sims.size());
194 
195 
196  return sims;
197 }
198 
199 
200 
201 
202 } // itk::fragment
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
Logger definitions.
Definition: loadstream.hpp:26
Definition: loadstream.hpp:32