Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2022-2023 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #include <memory>
0009 #include <optional>
0010 
0011 #include <traccc/efficiency/nseed_performance_writer.hpp>
0012 #include <traccc/efficiency/track_filter.hpp>
0013 #include <traccc/efficiency/track_matcher.hpp>
0014 
0015 namespace traccc {
0016 nseed_performance_writer::nseed_performance_writer(
0017     const std::string& prefix, std::unique_ptr<track_filter>&& filter,
0018     std::unique_ptr<track_matcher>&& matcher)
0019     : _prefix(prefix),
0020       _filter(std::move(filter)),
0021       _matcher(std::move(matcher)) {}
0022 
0023 void nseed_performance_writer::initialize() {
0024   output_seed_file.open(_prefix + "seeds.csv");
0025   output_track_file.open(_prefix + "tracks.csv");
0026 
0027   /*
0028    * This is a while away from proper file handling in C++, but it'll do.
0029    */
0030   assert(output_seed_file.good());
0031   assert(output_track_file.good());
0032 
0033   write_seed_header();
0034   write_track_header();
0035 }
0036 
0037 void nseed_performance_writer::finalize() {
0038   if (output_seed_file.is_open()) {
0039     output_seed_file.close();
0040   }
0041 
0042   if (output_track_file.is_open()) {
0043     output_track_file.close();
0044   }
0045 }
0046 
0047 void nseed_performance_writer::write_seed_header() {
0048   if (output_seed_file.good()) {
0049     output_seed_file << "event_id" << sep << "seed_id" << sep << "length" << sep
0050                      << "particle_id" << std::endl;
0051   }
0052 }
0053 
0054 void nseed_performance_writer::write_seed_row(
0055     std::size_t evt_id, std::size_t seed_id, std::size_t length,
0056     std::optional<std::size_t> part_id) {
0057   if (output_seed_file.good()) {
0058     output_seed_file << evt_id << sep << seed_id << sep << length << sep
0059                      << (part_id ? std::to_string(*part_id) : "-1")
0060                      << std::endl;
0061   }
0062 }
0063 
0064 void nseed_performance_writer::write_track_header() {
0065   if (output_track_file.good()) {
0066     output_track_file << "event_id" << sep << "particle_id" << sep
0067                       << "pass_cuts" << sep << "q" << sep << "eta" << sep
0068                       << "phi" << sep << "pt" << std::endl;
0069   }
0070 }
0071 
0072 void nseed_performance_writer::write_track_row(std::size_t evt_id,
0073                                                std::size_t part_id,
0074                                                bool pass_cuts, scalar q,
0075                                                scalar eta, scalar phi,
0076                                                scalar pt) {
0077   if (output_track_file.good()) {
0078     output_track_file << evt_id << sep << part_id << sep
0079                       << (pass_cuts ? "true" : "false") << sep << q << sep
0080                       << eta << sep << phi << sep << pt << std::endl;
0081   }
0082 }
0083 
0084 std::string nseed_performance_writer::generate_report_str() const {
0085   /*
0086    * Construct a stonking great string. Constructing a string of fixed format
0087    * like this isn't optimal, but hey it's an R&D project.
0088    */
0089   char buffer[512];
0090   std::string result;
0091 
0092   /*
0093    * Print a cute little header.
0094    */
0095   snprintf(buffer, 512, "==> Seed finding efficiency ...\n");
0096   result.append(buffer);
0097 
0098   /*
0099    * Print descriptors of the filter and matcher.
0100    */
0101   snprintf(buffer, 512, "- %-20s : %s\n", "Particle filter",
0102            _filter->get_name().c_str());
0103   result.append(buffer);
0104   snprintf(buffer, 512, "- %-20s : %s\n", "Particle matcher",
0105            _matcher->get_name().c_str());
0106   result.append(buffer);
0107 
0108   /*
0109    * Print some discrete statistics...
0110    */
0111   snprintf(buffer, 512, "- %-20s : %7ld\n", "Total seeds",
0112            (_stats.true_seeds + _stats.false_seeds));
0113   result.append(buffer);
0114   snprintf(buffer, 512, "- %-20s : %7ld\n", "True seeds", _stats.true_seeds);
0115   result.append(buffer);
0116   snprintf(buffer, 512, "- %-20s : %7ld\n", "False seeds", _stats.false_seeds);
0117   result.append(buffer);
0118   snprintf(buffer, 512, "- %-20s : %7ld\n", "Total tracks",
0119            (_stats.unmatched_tracks + _stats.matched_tracks));
0120   result.append(buffer);
0121   snprintf(buffer, 512, "- %-20s : %7ld\n", "Matched tracks",
0122            _stats.matched_tracks);
0123   result.append(buffer);
0124   snprintf(buffer, 512, "- %-20s : %7ld\n", "Unmatched tracks",
0125            _stats.unmatched_tracks);
0126   result.append(buffer);
0127 
0128   /*
0129    * ...and some compound statistics.
0130    */
0131   snprintf(buffer, 512, "- %-20s : %6.2f%%\n", "Precision",
0132            (100. * static_cast<double>(_stats.true_seeds) /
0133             static_cast<double>(_stats.true_seeds + _stats.false_seeds)));
0134   result.append(buffer);
0135   snprintf(buffer, 512, "- %-20s : %6.2f%%\n", "Fake rate",
0136            (100. * static_cast<double>(_stats.false_seeds) /
0137             static_cast<double>(_stats.true_seeds + _stats.false_seeds)));
0138   result.append(buffer);
0139   snprintf(
0140       buffer, 512, "- %-20s : %6.2f%%\n", "Recall/Efficiency",
0141       (100. * static_cast<double>(_stats.matched_tracks) /
0142        static_cast<double>(_stats.unmatched_tracks + _stats.matched_tracks)));
0143   result.append(buffer);
0144 
0145   return result;
0146 }
0147 }  // namespace traccc