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) 2022-2026 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #pragma once
0009 
0010 // Local include(s).
0011 #include <traccc/efficiency/track_filter.hpp>
0012 #include <traccc/efficiency/track_matcher.hpp>
0013 #include <traccc/utils/event_data.hpp>
0014 
0015 // Project include(s).
0016 #include <traccc/edm/measurement_collection.hpp>
0017 #include <traccc/edm/nseed.hpp>
0018 #include <traccc/edm/spacepoint_collection.hpp>
0019 
0020 // System include(s).
0021 #include <cassert>
0022 #include <fstream>
0023 #include <iostream>
0024 #include <memory>
0025 #include <optional>
0026 #include <set>
0027 
0028 namespace traccc {
0029 class nseed_performance_writer {
0030  public:
0031   struct statistics {
0032     std::size_t true_seeds = 0, false_seeds = 0;
0033     std::size_t matched_tracks = 0, unmatched_tracks = 0;
0034   };
0035 
0036   nseed_performance_writer(const std::string& p,
0037                            std::unique_ptr<track_filter>&& f,
0038                            std::unique_ptr<track_matcher>&& m);
0039 
0040   void initialize();
0041   void finalize();
0042 
0043   template <typename SeedIt, typename SpIt>
0044   void register_event(
0045       std::size_t ev, const SeedIt sb, const SeedIt se,
0046       const edm::spacepoint_collection::const_view& spacepoints_view,
0047       const edm::measurement_collection::const_view& measurements_view,
0048       const event_data& em) {
0049     std::size_t seed_id = 0;
0050 
0051     std::multiset<std::size_t> matched_tracks;
0052 
0053     const edm::spacepoint_collection::const_device spacepoints{
0054         spacepoints_view};
0055     const edm::measurement_collection::const_device measurements{
0056         measurements_view};
0057 
0058     for (SeedIt s = sb; s != se; ++s) {
0059       std::vector<std::vector<uint64_t>> particle_ids;
0060 
0061       std::transform(
0062           s->cbegin(), s->cend(), std::back_inserter(particle_ids),
0063           [&em, &measurements,
0064            &spacepoints](const edm::spacepoint_collection::host::size_type& l) {
0065             assert(spacepoints.at(l).measurement_index_2() ==
0066                    edm::spacepoint_collection::host::INVALID_MEASUREMENT_INDEX);
0067             const edm::measurement meas = measurements.at(
0068                 spacepoints
0069                     .at(static_cast<
0070                         edm::measurement_collection::const_device::size_type>(
0071                         l))
0072                     .measurement_index_1());
0073 
0074             const auto& ptcs = em.m_meas_to_ptc_map.find(meas)->second;
0075 
0076             std::vector<uint64_t> ptc_ids;
0077 
0078             for (auto const& [ptc, _] : ptcs) {
0079               ptc_ids.push_back(ptc.particle_id);
0080             }
0081 
0082             return ptc_ids;
0083           });
0084 
0085       std::optional<uint64_t> pid = _matcher->operator()(particle_ids);
0086 
0087       if (pid) {
0088         _stats.true_seeds++;
0089 
0090         matched_tracks.insert(*pid);
0091       } else {
0092         _stats.false_seeds++;
0093       }
0094 
0095       write_seed_row(ev, seed_id++, s->size(), pid);
0096     }
0097 
0098     for (const auto& [_, ptc] : em.m_particle_map) {
0099       bool pass = _filter->operator()(ptc);
0100 
0101       if (pass) {
0102         if (matched_tracks.count(ptc.particle_id) > 0) {
0103           _stats.matched_tracks++;
0104         } else {
0105           _stats.unmatched_tracks++;
0106         }
0107       }
0108 
0109       const scalar eta = vector::eta(ptc.momentum);
0110       const scalar phi = vector::phi(ptc.momentum);
0111       const scalar pT = vector::perp(ptc.momentum);
0112 
0113       write_track_row(ev, ptc.particle_id, pass, ptc.charge, eta, phi, pT);
0114     }
0115   }
0116 
0117   std::string generate_report_str() const;
0118 
0119  private:
0120   static constexpr std::string_view sep = ",";
0121 
0122   void write_seed_header();
0123   void write_seed_row(std::size_t, std::size_t, std::size_t,
0124                       std::optional<std::size_t>);
0125 
0126   void write_track_header();
0127   void write_track_row(std::size_t, std::size_t, bool, scalar, scalar, scalar,
0128                        scalar);
0129 
0130   std::string _prefix;
0131 
0132   std::unique_ptr<track_filter> _filter;
0133   std::unique_ptr<track_matcher> _matcher;
0134 
0135   std::ofstream output_seed_file, output_track_file;
0136 
0137   statistics _stats;
0138 };
0139 }  // namespace traccc