Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2024-2025 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // Local include(s).
0009 #include "write_seeds.hpp"
0010 
0011 // System include(s).
0012 #include <fstream>
0013 #include <map>
0014 
0015 namespace traccc::io::obj {
0016 
0017 void write_seeds(std::string_view filename,
0018                  edm::seed_collection::const_view seeds_view,
0019                  edm::spacepoint_collection::const_view spacepoints_view) {
0020   // Open the output file.
0021   std::ofstream file{filename.data()};
0022   if (!file.is_open()) {
0023     throw std::runtime_error("Failed to open file: " + std::string(filename));
0024   }
0025 
0026   // Create device collections around the views.
0027   const edm::seed_collection::const_device seeds{seeds_view};
0028   const edm::spacepoint_collection::const_device spacepoints{spacepoints_view};
0029 
0030   // Map associating in-memory spacepoint indices to in-file ones.
0031   std::map<std::size_t, std::size_t> spacepoint_indices;
0032 
0033   // Helper lambda to write a spacepoint to the output file.
0034   auto write_spacepoint =
0035       [&file, &spacepoints, &spacepoint_indices](
0036           edm::spacepoint_collection::const_device::size_type memory_index,
0037           std::size_t file_index) -> bool {
0038     // Check whether this spacepoint has already been written.
0039     if (spacepoint_indices.find(memory_index) != spacepoint_indices.end()) {
0040       return false;
0041     }
0042     // Write the spacepoint.
0043     const edm::spacepoint sp = spacepoints[memory_index];
0044     file << "v " << sp.x() << " " << sp.y() << " " << sp.z() << "\n";
0045     // Remember the mapping.
0046     spacepoint_indices[memory_index] = file_index;
0047     return true;
0048   };
0049 
0050   // First, write out all of the spacepoints as vertices. Making sure that we
0051   // only write each spacepoint once. And remembering the indices of the
0052   // spacepoints in the output file, to be used when making the seeds.
0053   std::size_t file_index = 1;
0054   file << "# Spacepoints from which the seeds are built\n";
0055   for (edm::seed_collection::const_device::size_type i = 0; i < seeds.size();
0056        ++i) {
0057     const edm::seed seed = seeds.at(i);
0058     if (write_spacepoint(seed.bottom_index(), file_index)) {
0059       ++file_index;
0060     }
0061     if (write_spacepoint(seed.middle_index(), file_index)) {
0062       ++file_index;
0063     }
0064     if (write_spacepoint(seed.top_index(), file_index)) {
0065       ++file_index;
0066     }
0067   }
0068 
0069   // Helper lambda for getting an element of the spacepoint_indices map, in a
0070   // "safe" way.
0071   auto get_spacepoint_index =
0072       [&spacepoint_indices](std::size_t memory_index) -> std::size_t {
0073     auto it = spacepoint_indices.find(memory_index);
0074     if (it == spacepoint_indices.end()) {
0075       throw std::runtime_error("Spacepoint index not found");
0076     }
0077     return it->second;
0078   };
0079 
0080   // Now build the seeds as lines connecting the spacepoint vertices.
0081   file << "# Seeds\n";
0082   for (edm::seed_collection::const_device::size_type i = 0; i < seeds.size();
0083        ++i) {
0084     const edm::seed seed = seeds.at(i);
0085     file << "l " << get_spacepoint_index(seed.bottom_index()) << " "
0086          << get_spacepoint_index(seed.middle_index()) << " "
0087          << get_spacepoint_index(seed.top_index()) << "\n";
0088   }
0089 }
0090 
0091 }  // namespace traccc::io::obj