Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2026 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #pragma once
0009 
0010 // Project include(s).
0011 #include "traccc/edm/measurement_collection.hpp"
0012 #include "traccc/edm/track_container.hpp"
0013 #include "traccc/edm/track_parameters.hpp"
0014 #include "traccc/edm/track_state_helpers.hpp"
0015 #include "traccc/geometry/detector.hpp"
0016 #include "traccc/geometry/host_detector.hpp"
0017 #include "traccc/io/data_format.hpp"
0018 #include "traccc/utils/event_data.hpp"
0019 #include "traccc/utils/logging.hpp"
0020 #include "traccc/utils/transcribe_to_trace.hpp"
0021 
0022 // VecMem include(s).
0023 #include <vecmem/memory/host_memory_resource.hpp>
0024 
0025 // System include(s)
0026 #include <vector>
0027 
0028 namespace traccc {
0029 
0030 /// Fill track containers Kalman comparison from simulation data
0031 void fill_track_containers(
0032     std::unique_ptr<const traccc::Logger> ilogger,
0033     const traccc::host_detector* host_det, const std::string& input_dir,
0034     const unsigned int n_events, const bool use_acts_geoid,
0035     const traccc::scalar track_min_pT, const traccc::scalar track_max_rad,
0036     std::vector<traccc::free_track_parameters<traccc::default_algebra>>& tracks,
0037     std::vector<vecmem::vector<traccc::propagation_validator::candidate_type<
0038         traccc::default_detector::host>>>& truth_traces,
0039     typename edm::measurement_collection::host& measurements,
0040     traccc::edm::track_container<traccc::default_algebra>::host&
0041         track_container) {
0042   using algebra_t = traccc::default_algebra;
0043   using detector_t = traccc::default_detector::host;
0044 
0045   TRACCC_LOCAL_LOGGER(std::move(ilogger));
0046 
0047   // Host memory resource
0048   vecmem::host_memory_resource host_mr;
0049 
0050   // Geometry context
0051   const traccc::default_detector::host::geometry_context ctx{};
0052 
0053   // Retrieve detector
0054   const detector_t& det = host_det->template as<traccc::default_detector>();
0055 
0056   tracks.reserve(n_events * 1000);
0057   truth_traces.reserve(tracks.capacity());
0058 
0059   TRACCC_VERBOSE("Reconstructing " << n_events << " events");
0060 
0061   // Read the truth data in
0062   for (std::size_t i_event = 0u; i_event < n_events; ++i_event) {
0063     traccc::event_data evt_data(input_dir, i_event, host_mr, use_acts_geoid,
0064                                 host_det, data_format::csv);
0065     TRACCC_VERBOSE("Event " << i_event << ": Found "
0066                             << evt_data.m_particle_map.size()
0067                             << " initial particles");
0068 
0069     if (evt_data.m_particle_map.empty()) {
0070       TRACCC_ERROR("Removing event " << i_event << ": Found no particles");
0071       continue;
0072     }
0073 
0074     if (evt_data.m_ptc_to_meas_map.empty()) {
0075       TRACCC_ERROR(
0076           "Removing event "
0077           << i_event
0078           << ": Found no connections between particles and measurements");
0079       continue;
0080     }
0081 
0082     std::size_t n_tracks{tracks.size()};
0083     for (const auto& [ptc_id, ptc] : evt_data.m_particle_map) {
0084       // Particle produced no valid measurements
0085       if (!evt_data.m_ptc_to_meas_map.contains(ptc)) {
0086         TRACCC_WARNING("Event "
0087                        << i_event << ": Removing particle " << ptc_id
0088                        << " since it has no measurements linked to it");
0089         continue;
0090       }
0091       // Minimum momentum
0092       const traccc::scalar pT{vector::perp(ptc.momentum)};
0093       if (pT <= track_min_pT) {
0094         TRACCC_WARNING("Event " << i_event << ": Removing particle " << ptc_id
0095                                 << " due to transv. momentum cut (pT was "
0096                                 << pT / traccc::unit<traccc::scalar>::MeV
0097                                 << " MeV)");
0098         continue;
0099       }
0100 
0101       // Make a trace of detray-understandable intersections
0102       auto truth_trace_fw = traccc::propagation_validator::transcribe_to_trace(
0103           ctx, det, ptc, evt_data.m_ptc_to_meas_map);
0104 
0105       // No meansurements found in event
0106       if (truth_trace_fw.empty()) {
0107         TRACCC_WARNING("Event " << i_event
0108                                 << ": No measurements found for particle "
0109                                 << ptc_id);
0110         continue;
0111       }
0112 
0113       // Minimum radius (remove secondaries)
0114       const traccc::scalar rad{vector::perp(ptc.vertex)};
0115       if (rad >= track_max_rad) {
0116         TRACCC_WARNING("Event " << i_event << ": Removing particle " << ptc_id
0117                                 << " due to radius cut (radius was "
0118                                 << rad / traccc::unit<traccc::scalar>::mm
0119                                 << " mm)");
0120         continue;
0121       }
0122 
0123       assert(!truth_trace_fw.empty());
0124 
0125       truth_traces.push_back(std::move(truth_trace_fw));
0126 
0127       TRACCC_DEBUG("Event " << i_event << ": Found " << truth_traces.size()
0128                             << " truth measurement(s) for track "
0129                             << tracks.size());
0130 
0131       // Transcribe measurements to global collection
0132       const auto& measurements_per_ptc = evt_data.m_ptc_to_meas_map.at(ptc);
0133       assert(!measurements_per_ptc.empty());
0134 
0135       // Current min measurement index for this track
0136       const auto meas_offset{static_cast<unsigned int>(measurements.size())};
0137 
0138       for (const auto& meas : measurements_per_ptc) {
0139         measurements.push_back(meas);
0140       }
0141 
0142       // Device measurement container with the correct(!) size
0143       edm::measurement_collection::const_device device_measurements{
0144           vecmem::get_data(measurements)};
0145 
0146       // Create the track and link one track state per measurement to it
0147       edm::track_collection<algebra_t>::host::object_type track;
0148       for (const auto& [i, meas] :
0149            detray::views::enumerate(measurements_per_ptc)) {
0150         const auto meas_idx{static_cast<unsigned int>(i + meas_offset)};
0151         const auto state_idx{
0152             static_cast<unsigned int>(track_container.states.size())};
0153 
0154         track.constituent_links().push_back(
0155             {edm::track_constituent_link::track_state, state_idx});
0156 
0157         track_container.states.push_back(
0158             edm::make_track_state<algebra_t>(device_measurements, meas_idx));
0159       }
0160 
0161       // Record the truth track candidate.
0162       track_container.tracks.push_back(track);
0163 
0164       TRACCC_DEBUG("-> Truth tracks " << track_container.tracks.size());
0165 
0166       // Construct the initial free track parameters for the propagation
0167       tracks.emplace_back(ptc.vertex, 0.f, ptc.momentum, ptc.charge);
0168     }
0169 
0170     if (tracks.size() - n_tracks == 0u) {
0171       TRACCC_WARNING("Event " << i_event << ": No eligible tracks in event");
0172     } else {
0173       TRACCC_VERBOSE("Event " << i_event << ": Found "
0174                               << tracks.size() - n_tracks
0175                               << " reconstructible truth track(s) in event");
0176     }
0177   }
0178 
0179   // Make the final measurement info available to the input containers
0180   track_container.measurements = vecmem::get_data(measurements);
0181 }
0182 
0183 }  // namespace traccc