Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2023-2026 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // Project include(s).
0009 #include "traccc/definitions/common.hpp"
0010 #include "traccc/definitions/primitives.hpp"
0011 #include "traccc/examples/make_magnetic_field.hpp"
0012 #include "traccc/examples/print_fitted_tracks_statistics.hpp"
0013 #include "traccc/fitting/kalman_fitting_algorithm.hpp"
0014 #include "traccc/geometry/detector.hpp"
0015 #include "traccc/io/read_detector.hpp"
0016 #include "traccc/io/utils.hpp"
0017 #include "traccc/options/detector.hpp"
0018 #include "traccc/options/input_data.hpp"
0019 #include "traccc/options/magnetic_field.hpp"
0020 #include "traccc/options/performance.hpp"
0021 #include "traccc/options/program_options.hpp"
0022 #include "traccc/options/track_fitting.hpp"
0023 #include "traccc/options/track_propagation.hpp"
0024 #include "traccc/resolution/fitting_performance_writer.hpp"
0025 #include "traccc/utils/propagation.hpp"
0026 #include "traccc/utils/seed_generator.hpp"
0027 
0028 // VecMem include(s).
0029 #include <vecmem/memory/host_memory_resource.hpp>
0030 #include <vecmem/utils/copy.hpp>
0031 
0032 // System include(s).
0033 #include <cstdlib>
0034 #include <exception>
0035 #include <iomanip>
0036 #include <iostream>
0037 
0038 using namespace traccc;
0039 namespace po = boost::program_options;
0040 
0041 // The main routine
0042 //
0043 int main(int argc, char* argv[]) {
0044   std::unique_ptr<const traccc::Logger> ilogger = traccc::getDefaultLogger(
0045       "TracccExampleTruthFitting", traccc::Logging::Level::INFO);
0046 
0047   TRACCC_LOCAL_LOGGER(std::move(ilogger));
0048 
0049   // Program options.
0050   traccc::opts::detector detector_opts;
0051   traccc::opts::magnetic_field bfield_opts;
0052   traccc::opts::input_data input_opts;
0053   traccc::opts::track_propagation propagation_opts;
0054   traccc::opts::track_fitting fitting_opts;
0055   traccc::opts::performance performance_opts;
0056   traccc::opts::program_options program_opts{
0057       "Truth Track Fitting on the Host",
0058       {detector_opts, bfield_opts, input_opts, propagation_opts, fitting_opts,
0059        performance_opts},
0060       argc,
0061       argv,
0062       logger().cloneWithSuffix("Options")};
0063 
0064   /// Type declarations
0065   using host_detector_type = traccc::default_detector::host;
0066 
0067   // Memory resources used by the application.
0068   vecmem::host_memory_resource host_mr;
0069   // Copy obejct
0070   vecmem::copy copy;
0071 
0072   // Performance writer
0073   traccc::fitting_performance_writer fit_performance_writer(
0074       traccc::fitting_performance_writer::config{},
0075       logger().clone("FittingPerformanceWriter"));
0076 
0077   /*****************************
0078    * Build a geometry
0079    *****************************/
0080 
0081   // B field value
0082   const auto field = details::make_magnetic_field(bfield_opts);
0083 
0084   // Read the detector
0085   traccc::host_detector polymorphic_detector;
0086   traccc::io::read_detector(
0087       polymorphic_detector, host_mr, detector_opts.detector_file,
0088       detector_opts.material_file, detector_opts.grid_file);
0089 
0090   const traccc::default_detector::host& host_det =
0091       polymorphic_detector.as<traccc::default_detector>();
0092 
0093   /// Create a "misaligned" context in the transform store
0094   using xf_container = host_detector_type::transform_container;
0095   using xf_vector = xf_container::base_type;
0096 
0097   const xf_container& default_xfs = host_det.transform_store();
0098   xf_vector misaligned_xfs;
0099   misaligned_xfs.reserve(default_xfs.size());
0100   for (const auto& xf : default_xfs) {
0101     misaligned_xfs.push_back(xf);
0102   }
0103   xf_container* ptr_default_xfs = const_cast<xf_container*>(&default_xfs);
0104   ptr_default_xfs->add_context(misaligned_xfs);
0105 
0106   /*****************************
0107    * Do the reconstruction
0108    *****************************/
0109 
0110   /// Standard deviations for seed track parameters
0111   static constexpr std::array<double, e_bound_size> stddevs = {
0112       0.03 * traccc::unit<double>::mm,
0113       0.03 * traccc::unit<double>::mm,
0114       0.017,
0115       0.017,
0116       0.001 / traccc::unit<double>::GeV,
0117       1. * traccc::unit<double>::ns};
0118 
0119   // Fitting algorithm objects
0120   // Alg0
0121   traccc::fitting_config fit_cfg0(fitting_opts);
0122   fit_cfg0.propagation = propagation_opts;
0123   fit_cfg0.propagation.context = host_detector_type::geometry_context{0};
0124   traccc::host::kalman_fitting_algorithm host_fitting0(
0125       fit_cfg0, host_mr, copy, logger().clone("FittingAlg0"));
0126   // Alg1
0127   traccc::fitting_config fit_cfg1(fitting_opts);
0128   fit_cfg1.propagation = propagation_opts;
0129   fit_cfg1.propagation.context = host_detector_type::geometry_context{1};
0130   traccc::host::kalman_fitting_algorithm host_fitting1(
0131       fit_cfg1, host_mr, copy, logger().clone("FittingAlg1"));
0132 
0133   // Seed generators
0134   traccc::seed_generator<host_detector_type>::config seed_cfg{};
0135   seed_cfg.initial_sigmas = stddevs;
0136   traccc::seed_generator<host_detector_type> sg0(host_det, seed_cfg, 0,
0137                                                  fit_cfg0.propagation.context);
0138   traccc::seed_generator<host_detector_type> sg1(host_det, seed_cfg, 0,
0139                                                  fit_cfg1.propagation.context);
0140 
0141   // Iterate over events
0142   for (auto event = input_opts.skip;
0143        event < input_opts.events + input_opts.skip; ++event) {
0144     // Truth Track Candidates
0145     traccc::event_data evt_data(
0146         input_opts.directory, event, host_mr, input_opts.use_acts_geom_source,
0147         &polymorphic_detector, input_opts.format, false);
0148 
0149     // For the first half of events run Alg0
0150     if ((event - input_opts.skip) / (input_opts.events / 2) == 0) {
0151       traccc::edm::measurement_collection::host truth_measurements{host_mr};
0152       traccc::edm::track_container<default_algebra>::host
0153           truth_track_candidates{host_mr};
0154       evt_data.generate_truth_candidates(truth_track_candidates,
0155                                          truth_measurements, sg0, host_mr);
0156       truth_track_candidates.measurements =
0157           vecmem::get_data(truth_measurements);
0158 
0159       // Run fitting
0160       auto track_states = host_fitting0(
0161           polymorphic_detector, field,
0162           traccc::edm::track_container<default_algebra>::const_data(
0163               truth_track_candidates));
0164 
0165       details::print_fitted_tracks_statistics(track_states, logger());
0166 
0167       const std::size_t n_fitted_tracks = track_states.tracks.size();
0168 
0169       if (performance_opts.run) {
0170         for (unsigned int i = 0; i < n_fitted_tracks; i++) {
0171           fit_performance_writer.write(track_states.tracks.at(i),
0172                                        track_states.states, truth_measurements,
0173                                        host_det, evt_data,
0174                                        fit_cfg0.propagation.context);
0175         }
0176       }
0177     } else {
0178       traccc::edm::measurement_collection::host truth_measurements{host_mr};
0179       traccc::edm::track_container<default_algebra>::host
0180           truth_track_candidates{host_mr};
0181       evt_data.generate_truth_candidates(truth_track_candidates,
0182                                          truth_measurements, sg1, host_mr);
0183       truth_track_candidates.measurements =
0184           vecmem::get_data(truth_measurements);
0185 
0186       // Run fitting
0187       auto track_states = host_fitting1(
0188           polymorphic_detector, field,
0189           traccc::edm::track_container<default_algebra>::const_data(
0190               truth_track_candidates));
0191 
0192       details::print_fitted_tracks_statistics(track_states, logger());
0193 
0194       const std::size_t n_fitted_tracks = track_states.tracks.size();
0195 
0196       if (performance_opts.run) {
0197         for (unsigned int i = 0; i < n_fitted_tracks; i++) {
0198           fit_performance_writer.write(track_states.tracks.at(i),
0199                                        track_states.states, truth_measurements,
0200                                        host_det, evt_data,
0201                                        fit_cfg1.propagation.context);
0202         }
0203       }
0204     }
0205   }
0206 
0207   if (performance_opts.run) {
0208     fit_performance_writer.finalize();
0209   }
0210 
0211   return EXIT_SUCCESS;
0212 }