This repository was archived by the owner on Jul 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathread_cells.cpp
More file actions
179 lines (148 loc) · 5.76 KB
/
Copy pathread_cells.cpp
File metadata and controls
179 lines (148 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022-2026 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
// Local include(s).
#include "read_cells.hpp"
#include "traccc/io/csv/make_cell_reader.hpp"
#include "traccc/utils/logging.hpp"
// System include(s).
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <stdexcept>
#include <utility>
#include <vector>
namespace {
/// Comparator used for sorting cells. This sorting is one of the assumptions
/// made in the clusterization algorithm
struct cell_order {
bool operator()(const traccc::io::csv::cell& lhs,
const traccc::io::csv::cell& rhs) const {
if (lhs.channel1 != rhs.channel1) {
return (lhs.channel1 < rhs.channel1);
} else {
return (lhs.channel0 < rhs.channel0);
}
}
}; // struct cell_order
std::map<std::uint64_t, std::vector<traccc::io::csv::cell> >
read_deduplicated_cells(std::string_view filename,
std::unique_ptr<const traccc::Logger> ilogger) {
TRACCC_LOCAL_LOGGER(std::move(ilogger));
// Temporary storage for all the cells and modules.
std::map<std::uint64_t,
std::map<traccc::io::csv::cell, float, ::cell_order> >
cellMap;
// Construct the cell reader object.
auto reader = traccc::io::csv::make_cell_reader(filename);
// Read all cells from input file.
traccc::io::csv::cell iocell;
unsigned int nduplicates = 0;
while (reader.read(iocell)) {
// Add the cell to the module. At this point the module link of the
// cells is not set up correctly yet.
auto ret = cellMap[iocell.geometry_id].insert({iocell, iocell.value});
if (ret.second == false) {
cellMap[iocell.geometry_id].at(iocell) += iocell.value;
++nduplicates;
}
}
if (nduplicates > 0) {
TRACCC_WARNING(nduplicates << " duplicate cells found in " << filename);
}
// Create and fill the result container. With summed activation values.
std::map<std::uint64_t, std::vector<traccc::io::csv::cell> > result;
for (const auto& [geometry_id, cells] : cellMap) {
for (const auto& [cell, value] : cells) {
traccc::io::csv::cell summed_cell{cell};
summed_cell.value = value;
result[geometry_id].push_back(summed_cell);
}
}
// Return the container.
return result;
}
std::map<std::uint64_t, std::vector<traccc::io::csv::cell> > read_all_cells(
std::string_view filename) {
// The result container.
std::map<std::uint64_t, std::vector<traccc::io::csv::cell> > result;
// Construct the cell reader object.
auto reader = traccc::io::csv::make_cell_reader(filename);
// Read all cells from input file.
traccc::io::csv::cell iocell;
while (reader.read(iocell)) {
// Add the cell to the module. At this point the module link of the
// cells is not set up correctly yet.
result[iocell.geometry_id].push_back(iocell);
}
// Sort the cells. Deduplication or not, they do need to be sorted.
for (auto& [_, cells] : result) {
std::sort(cells.begin(), cells.end(), ::cell_order());
}
// Return the container.
return result;
}
} // namespace
namespace traccc::io::csv {
void read_cells(edm::silicon_cell_collection::host& cells,
std::string_view filename,
std::unique_ptr<const Logger> ilogger,
const silicon_detector_description::host* dd, bool deduplicate,
bool use_acts_geometry_id, bool randomize) {
// Clear the output container.
cells.resize(0u);
// Get the cells and modules into an intermediate format.
auto cellsMap =
(deduplicate ? read_deduplicated_cells(filename, ilogger->clone())
: read_all_cells(filename));
// If there is a detector description object, build a map of geometry IDs
// to indices inside the detector description.
std::map<geometry_id, unsigned int> geomIdMap;
if (dd) {
if (use_acts_geometry_id) {
for (unsigned int i = 0; i < dd->acts_geometry_id().size(); ++i) {
geomIdMap[dd->acts_geometry_id()[i]] = i;
}
} else {
for (unsigned int i = 0; i < dd->geometry_id().size(); ++i) {
geomIdMap[dd->geometry_id()[i].value()] = i;
}
}
}
// Make a random number generator. In case we need to shuffle the cells.
std::mt19937 rng;
rng.seed(static_cast<std::mt19937::result_type>(0u));
// Fill the output containers with the ordered cells and modules.
for (auto& [geometry_id, cellz] : cellsMap) {
// Figure out the index of the detector description object, for this
// group of cells.
unsigned int ddIndex = 0;
if (dd) {
auto it = geomIdMap.find(geometry_id);
if (it == geomIdMap.end()) {
throw std::runtime_error("Could not find geometry ID (" +
std::to_string(geometry_id) +
") in the detector description");
}
ddIndex = it->second;
}
// If the user asked for the cells to be randomized, then let's do so.
// But still, in a deterministic way, so that we could compare results
// across runs.
if (randomize) {
std::shuffle(cellz.begin(), cellz.end(), rng);
}
// Add the cells to the output.
for (const csv::cell& cell : cellz) {
cells.push_back({cell.channel0, cell.channel1, cell.value,
cell.timestamp, ddIndex});
}
}
}
} // namespace traccc::io::csv