From 1be9b1a4ef707c0a1e36c4f8e03c2c7f34c11d7d Mon Sep 17 00:00:00 2001 From: Attila Krasznahorkay Date: Sat, 20 Apr 2024 08:32:18 +0200 Subject: [PATCH] Using an std::map for cell deduplication instead of an std::set. This way it becomes possible to sum up the activations of the cells, using the value in the map. --- io/src/csv/read_cells.cpp | 87 +++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/io/src/csv/read_cells.cpp b/io/src/csv/read_cells.cpp index af228485c..82ee582c5 100644 --- a/io/src/csv/read_cells.cpp +++ b/io/src/csv/read_cells.cpp @@ -83,11 +83,12 @@ traccc::cell_module get_module(const std::uint64_t geometry_id, return result; } -std::map > -read_deduplicated_cells(std::string_view filename) { +std::map > read_deduplicated_cells( + std::string_view filename) { // Temporary storage for all the cells and modules. - std::map > result; + std::map > + cellMap; // Construct the cell reader object. auto reader = traccc::io::csv::make_cell_reader(filename); @@ -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; } } @@ -111,6 +115,16 @@ 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 > 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; } @@ -118,7 +132,7 @@ read_deduplicated_cells(std::string_view filename) { std::map > read_all_cells( std::string_view filename) { - // Temporary storage for all the cells and modules. + // The result container. std::map > result; // Construct the cell reader object. @@ -154,41 +168,34 @@ void read_cells( const std::map* 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; + } } }