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-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 "write_tracks.hpp"
0010 
0011 // Project include(s).
0012 #include "traccc/edm/measurement_helpers.hpp"
0013 
0014 // Detray include(s)
0015 #include <detray/geometry/tracking_surface.hpp>
0016 
0017 // System include(s).
0018 #include <cassert>
0019 #include <fstream>
0020 #include <stdexcept>
0021 
0022 namespace traccc::io::obj {
0023 
0024 void write_tracks(std::string_view filename,
0025                   edm::track_container<default_algebra>::const_view tracks_view,
0026                   const traccc::host_detector& detector) {
0027   // Open the output file.
0028   std::ofstream file{filename.data()};
0029   if (!file.is_open()) {
0030     throw std::runtime_error("Failed to open file: " + std::string(filename));
0031   }
0032 
0033   // Create a device collection around the track container view.
0034   const edm::track_container<default_algebra>::const_device tracks{tracks_view};
0035 
0036   // Convenience type.
0037   using size_type =
0038       edm::track_collection<default_algebra>::const_device::size_type;
0039 
0040   // First write out the measurements / spacepoints that the tracks are
0041   // made from. Don't try to resolve the overlaps, just write out duplicate
0042   // measurements if needed.
0043   file << "# Measurements / spacepoints that the tracks are made out of\n";
0044   for (size_type i = 0; i < tracks.tracks.size(); ++i) {
0045     // Loop over the measurements that the track is made out of.
0046     for (const auto& [type, idx] : tracks.tracks.constituent_links().at(i)) {
0047       // Find the measurement of this constituent.
0048       edm::measurement_collection::const_device::object_type meas;
0049       if (type == edm::track_constituent_link::measurement) {
0050         meas = tracks.measurements.at(idx);
0051       } else if (type == edm::track_constituent_link::track_state) {
0052         meas =
0053             tracks.measurements.at(tracks.states.at(idx).measurement_index());
0054       } else {
0055         // This should not happen...
0056         throw std::runtime_error("Unknown track constituent type found");
0057       }
0058 
0059       // Find the detector surface that this measurement sits on.
0060       const auto global = host_detector_visitor<detector_type_list>(
0061           detector, [meas]<typename detector_traits_t>(
0062                         const typename detector_traits_t::host& d) {
0063             detray::tracking_surface surface{d, meas.surface_link()};
0064             return surface.local_to_global(
0065                 {},
0066                 edm::get_measurement_local<
0067                     typename detector_traits_t::host::algebra_type>(meas),
0068                 {});
0069           });
0070 
0071       // Write the 3D coordinates of the measurement / spacepoint.
0072       assert(global.size() == 3);
0073       file << "v " << getter::element(global, 0u) << " "
0074            << getter::element(global, 1u) << " " << getter::element(global, 2u)
0075            << "\n";
0076     }
0077   }
0078 
0079   // Now loop over the track candidates again, and creates lines for each
0080   // of them using the measurements / spacepoints written out earlier.
0081   file << "# Track candidates\n";
0082   std::size_t vertex_counter = 1;
0083   for (size_type i = 0; i < tracks.tracks.size(); ++i) {
0084     // Construct the lines.
0085     file << "l";
0086     for (size_type j = 0; j < tracks.tracks.at(i).constituent_links().size();
0087          ++j) {
0088       file << " " << vertex_counter++;
0089     }
0090     file << "\n";
0091   }
0092 }
0093 
0094 }  // namespace traccc::io::obj