Skip to content

Commit

Permalink
Using an std::map for cell deduplication instead of an std::set.
Browse files Browse the repository at this point in the history
This way it becomes possible to sum up the activations of the
cells, using the value in the map.
  • Loading branch information
krasznaa authored and stephenswat committed Apr 22, 2024
1 parent 75aeb4d commit 1be9b1a
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions io/src/csv/read_cells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ traccc::cell_module get_module(const std::uint64_t geometry_id,
return result;
}

std::map<std::uint64_t, std::set<traccc::cell, ::cell_order> >
read_deduplicated_cells(std::string_view filename) {
std::map<std::uint64_t, std::vector<traccc::cell> > read_deduplicated_cells(
std::string_view filename) {

// Temporary storage for all the cells and modules.
std::map<std::uint64_t, std::set<traccc::cell, ::cell_order> > result;
std::map<std::uint64_t, std::map<traccc::cell, float, ::cell_order> >
cellMap;

// Construct the cell reader object.
auto reader = traccc::io::csv::make_cell_reader(filename);
Expand All @@ -97,12 +98,15 @@ read_deduplicated_cells(std::string_view filename) {
unsigned int nduplicates = 0;
while (reader.read(iocell)) {

// Construct a cell object.
const traccc::cell cell{iocell.channel0, iocell.channel1, iocell.value,
iocell.timestamp, 0};

// Add the cell to the module. At this point the module link of the
// cells is not set up correctly yet.
if (result[iocell.geometry_id]
.insert({iocell.channel0, iocell.channel1, iocell.value,
iocell.timestamp, 0})
.second == false) {
auto ret = cellMap[iocell.geometry_id].insert({cell, iocell.value});
if (ret.second == false) {
cellMap[iocell.geometry_id].at(cell) += iocell.value;
++nduplicates;
}
}
Expand All @@ -111,14 +115,24 @@ read_deduplicated_cells(std::string_view filename) {
<< " duplicate cells found in " << filename << std::endl;
}

// Create and fill the result container. With summed activation values.
std::map<std::uint64_t, std::vector<traccc::cell> > result;
for (const auto& [geometry_id, cells] : cellMap) {
for (const auto& [cell, value] : cells) {
traccc::cell summed_cell{cell};
summed_cell.activation = value;
result[geometry_id].push_back(summed_cell);
}
}

// Return the container.
return result;
}

std::map<std::uint64_t, std::vector<traccc::cell> > read_all_cells(
std::string_view filename) {

// Temporary storage for all the cells and modules.
// The result container.
std::map<std::uint64_t, std::vector<traccc::cell> > result;

// Construct the cell reader object.
Expand Down Expand Up @@ -154,41 +168,34 @@ void read_cells(
const std::map<std::uint64_t, detray::geometry::barcode>* barcode_map,
const bool deduplicate) {

// Helper lambda filling the output container.
const auto fill_output = [&out, geom, dconfig,
barcode_map](const auto& cellsMap) {
// Fill the output containers with the ordered cells and modules.
for (const auto& [original_geometry_id, cells] : cellsMap) {
// Modify the geometry ID of the module if a barcode map is
// provided.
std::uint64_t geometry_id = original_geometry_id;
if (barcode_map != nullptr) {
const auto it = barcode_map->find(geometry_id);
if (it != barcode_map->end()) {
geometry_id = it->second.value();
} else {
throw std::runtime_error(
"Could not find barcode for geometry ID " +
std::to_string(geometry_id));
}
}

// Add the module and its cells to the output.
out.modules.push_back(
get_module(geometry_id, geom, dconfig, original_geometry_id));
for (auto& cell : cells) {
out.cells.push_back(cell);
// Set the module link.
out.cells.back().module_link = out.modules.size() - 1;
// Get the cells and modules into an intermediate format.
auto cellsMap = (deduplicate ? read_deduplicated_cells(filename)
: read_all_cells(filename));

// Fill the output containers with the ordered cells and modules.
for (const auto& [original_geometry_id, cells] : cellsMap) {
// Modify the geometry ID of the module if a barcode map is
// provided.
std::uint64_t geometry_id = original_geometry_id;
if (barcode_map != nullptr) {
const auto it = barcode_map->find(geometry_id);
if (it != barcode_map->end()) {
geometry_id = it->second.value();
} else {
throw std::runtime_error(
"Could not find barcode for geometry ID " +
std::to_string(geometry_id));
}
}
};

// Fill the output in the appropriate way.
if (deduplicate) {
fill_output(read_deduplicated_cells(filename));
} else {
fill_output(read_all_cells(filename));
// Add the module and its cells to the output.
out.modules.push_back(
get_module(geometry_id, geom, dconfig, original_geometry_id));
for (auto& cell : cells) {
out.cells.push_back(cell);
// Set the module link.
out.cells.back().module_link = out.modules.size() - 1;
}
}
}

Expand Down

0 comments on commit 1be9b1a

Please sign in to comment.