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-2026 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_measurements.hpp"
0010 
0011 #include "traccc/io/csv/make_measurement_edm.hpp"
0012 #include "traccc/io/csv/make_measurement_reader.hpp"
0013 
0014 // System include(s).
0015 #include <numeric>
0016 #include <ranges>
0017 
0018 namespace traccc::io::csv {
0019 
0020 std::vector<measurement_id_type> read_measurements(
0021     edm::measurement_collection::host& measurements, std::string_view filename,
0022     const traccc::host_detector* detector,
0023     const traccc::detector_design_description::host* det_desc,
0024     const traccc::detector_conditions_description::host* det_cond,
0025     const bool do_sort) {
0026   // Construct the measurement reader object.
0027   auto reader = make_measurement_reader(filename);
0028 
0029   // For Acts data, build a map of acts->detray geometry IDs
0030   std::map<geometry_id, geometry_id> acts_to_detray_id;
0031   std::map<geometry_id, std::size_t> geometry_id_to_detector_description_index;
0032 
0033   if (detector) {
0034     host_detector_visitor<detector_type_list>(
0035         *detector, [&]<typename detector_t>(const detector_t::host& det) {
0036           for (const auto& surface_desc : det.surfaces()) {
0037             acts_to_detray_id[surface_desc.source] =
0038                 surface_desc.identifier().value();
0039           }
0040         });
0041   }
0042 
0043   if (det_cond) {
0044     for (std::size_t i = 0; i < det_cond->geometry_id().size(); ++i) {
0045       geometry_id_to_detector_description_index
0046           [det_cond->geometry_id().at(i).value()] =
0047               det_cond->module_to_design_id().at(i);
0048     }
0049   }
0050 
0051   // Read the measurements from the input file.
0052   csv::measurement iomeas;
0053   while (reader.read(iomeas)) {
0054     // Construct the measurement object.
0055     measurements.resize(measurements.size() + 1u);
0056     edm::measurement meas = measurements.at(measurements.size() - 1u);
0057     make_measurement_edm(
0058         iomeas, meas, (detector == nullptr ? nullptr : &acts_to_detray_id),
0059         det_desc,
0060         (det_cond == nullptr ? nullptr
0061                              : &geometry_id_to_detector_description_index));
0062   }
0063 
0064   // Contains the index of the new position at the entry of the old position
0065   std::vector<measurement_id_type> new_idx_map(measurements.size());
0066   if (do_sort) {
0067     // Remeber index locations
0068     std::vector<unsigned int> idx(measurements.size());
0069     std::iota(idx.begin(), idx.end(), 0u);
0070 
0071     // Sort the indices the way the measurements will be sorted
0072     // https://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes
0073     std::ranges::sort(idx.begin(), idx.end(),
0074                       [&measurements](unsigned int i, unsigned int j) {
0075                         return measurements[i] < measurements[j];
0076                       });
0077 
0078     // Create a sorted measurement collection.
0079     edm::measurement_collection::host sorted_measurements(
0080         measurements.resource());
0081     sorted_measurements.resize(measurements.size());
0082 
0083     // Map the indices to the new positions. While creating a sorted
0084     // measurement collection.
0085     for (std::size_t i = 0u; i < idx.size(); ++i) {
0086       new_idx_map[idx[i]] = static_cast<measurement_id_type>(i);
0087       sorted_measurements.at(i) = measurements.at(idx[i]);
0088     }
0089 
0090     // Override the measurements with the sorted ones.
0091     measurements = sorted_measurements;
0092   }
0093 
0094   return new_idx_map;
0095 }
0096 
0097 }  // namespace traccc::io::csv