Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:18

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2022-2024 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // Local include(s).
0009 #include "read_cells.hpp"
0010 
0011 #include "traccc/io/csv/make_cell_reader.hpp"
0012 #include "traccc/utils/logging.hpp"
0013 
0014 // System include(s).
0015 #include <algorithm>
0016 #include <cassert>
0017 #include <iostream>
0018 #include <map>
0019 #include <set>
0020 #include <utility>
0021 #include <vector>
0022 
0023 namespace {
0024 
0025 /// Comparator used for sorting cells. This sorting is one of the assumptions
0026 /// made in the clusterization algorithm
0027 struct cell_order {
0028   bool operator()(const traccc::io::csv::cell& lhs,
0029                   const traccc::io::csv::cell& rhs) const {
0030     if (lhs.channel1 != rhs.channel1) {
0031       return (lhs.channel1 < rhs.channel1);
0032     } else {
0033       return (lhs.channel0 < rhs.channel0);
0034     }
0035   }
0036 };  // struct cell_order
0037 
0038 std::map<std::uint64_t, std::vector<traccc::io::csv::cell> >
0039 read_deduplicated_cells(std::string_view filename,
0040                         std::unique_ptr<const traccc::Logger> ilogger) {
0041   TRACCC_LOCAL_LOGGER(std::move(ilogger));
0042 
0043   // Temporary storage for all the cells and modules.
0044   std::map<std::uint64_t, std::map<traccc::io::csv::cell, float, ::cell_order> >
0045       cellMap;
0046 
0047   // Construct the cell reader object.
0048   auto reader = traccc::io::csv::make_cell_reader(filename);
0049 
0050   // Read all cells from input file.
0051   traccc::io::csv::cell iocell;
0052   unsigned int nduplicates = 0;
0053   while (reader.read(iocell)) {
0054     // Add the cell to the module. At this point the module link of the
0055     // cells is not set up correctly yet.
0056     auto ret = cellMap[iocell.geometry_id].insert({iocell, iocell.value});
0057     if (ret.second == false) {
0058       cellMap[iocell.geometry_id].at(iocell) += iocell.value;
0059       ++nduplicates;
0060     }
0061   }
0062   if (nduplicates > 0) {
0063     TRACCC_WARNING(nduplicates << " duplicate cells found in " << filename);
0064   }
0065 
0066   // Create and fill the result container. With summed activation values.
0067   std::map<std::uint64_t, std::vector<traccc::io::csv::cell> > result;
0068   for (const auto& [geometry_id, cells] : cellMap) {
0069     for (const auto& [cell, value] : cells) {
0070       traccc::io::csv::cell summed_cell{cell};
0071       summed_cell.value = value;
0072       result[geometry_id].push_back(summed_cell);
0073     }
0074   }
0075 
0076   // Return the container.
0077   return result;
0078 }
0079 
0080 std::map<std::uint64_t, std::vector<traccc::io::csv::cell> > read_all_cells(
0081     std::string_view filename) {
0082   // The result container.
0083   std::map<std::uint64_t, std::vector<traccc::io::csv::cell> > result;
0084 
0085   // Construct the cell reader object.
0086   auto reader = traccc::io::csv::make_cell_reader(filename);
0087 
0088   // Read all cells from input file.
0089   traccc::io::csv::cell iocell;
0090   while (reader.read(iocell)) {
0091     // Add the cell to the module. At this point the module link of the
0092     // cells is not set up correctly yet.
0093     result[iocell.geometry_id].push_back(iocell);
0094   }
0095 
0096   // Sort the cells. Deduplication or not, they do need to be sorted.
0097   for (auto& [_, cells] : result) {
0098     std::sort(cells.begin(), cells.end(), ::cell_order());
0099   }
0100 
0101   // Return the container.
0102   return result;
0103 }
0104 
0105 }  // namespace
0106 
0107 namespace traccc::io::csv {
0108 
0109 void read_cells(edm::silicon_cell_collection::host& cells,
0110                 std::string_view filename,
0111                 std::unique_ptr<const Logger> ilogger,
0112                 const detector_conditions_description::host* det_cond,
0113                 bool deduplicate, bool use_acts_geometry_id) {
0114   // Clear the output container.
0115   cells.resize(0u);
0116 
0117   // Get the cells and modules into an intermediate format.
0118   auto cellsMap =
0119       (deduplicate ? read_deduplicated_cells(filename, ilogger->clone())
0120                    : read_all_cells(filename));
0121 
0122   // If there is a detector description object, build a map of geometry IDs
0123   // to indices inside the detector description.
0124   std::map<geometry_id, unsigned int> geomIdMap;
0125   if (det_cond) {
0126     if (use_acts_geometry_id) {
0127       for (unsigned int i = 0; i < det_cond->acts_geometry_id().size(); ++i) {
0128         geomIdMap[det_cond->acts_geometry_id()[i]] = i;
0129       }
0130     } else {
0131       for (unsigned int i = 0; i < det_cond->geometry_id().size(); ++i) {
0132         geomIdMap[det_cond->geometry_id()[i].value()] = i;
0133       }
0134     }
0135   }
0136 
0137   // Fill the output containers with the ordered cells and modules.
0138   for (const auto& [geometry_id, cellz] : cellsMap) {
0139     // Figure out the index of the detector description object, for this
0140     // group of cells.
0141     unsigned int ddIndex = 0;
0142 
0143     if (det_cond) {
0144       auto it = geomIdMap.find(geometry_id);
0145       if (it == geomIdMap.end()) {
0146         throw std::runtime_error("Could not find geometry ID (" +
0147                                  std::to_string(geometry_id) +
0148                                  ") in the detector description");
0149       }
0150       ddIndex = it->second;
0151     }
0152 
0153     // Add the cells to the output.
0154     for (const csv::cell& cell : cellz) {
0155       cells.push_back(
0156           {cell.channel0, cell.channel1, cell.value, cell.timestamp, ddIndex});
0157     }
0158   }
0159 }
0160 
0161 }  // namespace traccc::io::csv