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) 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 // Library include(s).
0011 #include "traccc/resolution/res_plot_tool_config.hpp"
0012 #include "traccc/resolution/stat_plot_tool_config.hpp"
0013 #include "traccc/utils/messaging.hpp"
0014 
0015 // Project include(s).
0016 #include "traccc/edm/measurement_collection.hpp"
0017 #include "traccc/edm/particle.hpp"
0018 #include "traccc/edm/track_collection.hpp"
0019 #include "traccc/edm/track_parameters.hpp"
0020 #include "traccc/edm/track_state_collection.hpp"
0021 #include "traccc/utils/event_data.hpp"
0022 
0023 // System include(s).
0024 #include <cassert>
0025 #include <memory>
0026 
0027 namespace traccc {
0028 namespace details {
0029 
0030 /// Data members that should not pollute the API of
0031 /// @c traccc::fitting_performance_writer
0032 struct fitting_performance_writer_data;
0033 
0034 }  // namespace details
0035 
0036 class fitting_performance_writer : public messaging {
0037  public:
0038   struct config {
0039     /// Output filename.
0040     std::string file_path = "performance_track_fitting.root";
0041     /// Output file mode
0042     std::string file_mode = "RECREATE";
0043     /// Plot tool configurations.
0044     res_plot_tool_config res_config;
0045     stat_plot_tool_config stat_config;
0046   };
0047 
0048   /// Constructor with writer config
0049   fitting_performance_writer(const config& cfg,
0050                              std::unique_ptr<const traccc::Logger> logger);
0051 
0052   /// Destructor that closes the file
0053   ~fitting_performance_writer();
0054 
0055   /// Fill the tracking results into the histograms
0056   ///
0057   /// @param track The fitted track to measure/write the performance of
0058   /// @param track_states All reconstructed track states
0059   /// @param measurements All reconstructed measurements
0060   /// @param det detector object
0061   /// @param evt_map event map to find the truth values
0062   template <typename detector_t>
0063   void write(const edm::track_collection<
0064                  traccc::default_algebra>::host::proxy_type track,
0065              const edm::track_state_collection<traccc::default_algebra>::host&
0066                  track_states,
0067              const edm::measurement_collection::host& measurements,
0068              const detector_t& det, event_data& evt_data,
0069              const detector_t::geometry_context& ctx = {}) {
0070     static_assert(std::same_as<typename detector_t::algebra_type,
0071                                traccc::default_algebra>);
0072 
0073     if (track.fit_outcome() != track_fit_outcome::SUCCESS) {
0074       return;
0075     }
0076 
0077     // Get the first smoothed track state
0078     const unsigned int trk_state_idx =
0079         std::find_if(
0080             track.constituent_links().begin(), track.constituent_links().end(),
0081             [&](const edm::track_constituent_link& link) {
0082               assert(link.type == edm::track_constituent_link::track_state);
0083               return track_states.at(link.index).is_smoothed();
0084             })
0085             ->index;
0086     const edm::track_state trk_state = track_states.at(trk_state_idx);
0087     assert(!trk_state.is_hole());
0088     assert(trk_state.is_smoothed());
0089     assert(!trk_state.filtered_params().is_invalid());
0090 
0091     bool use_found = !evt_data.m_found_meas_to_ptc_map.empty();
0092 
0093     const std::map<event_data::measurement_proxy,
0094                    std::map<particle, std::size_t>>& meas_to_ptc_map =
0095         use_found ? evt_data.m_found_meas_to_ptc_map
0096                   : evt_data.m_meas_to_ptc_map;
0097     const std::map<event_data::measurement_proxy, std::pair<point3, point3>>&
0098         meas_to_param_map = use_found ? evt_data.m_found_meas_to_param_map
0099                                       : evt_data.m_meas_to_param_map;
0100 
0101     const edm::measurement meas =
0102         measurements.at(trk_state.measurement_index());
0103 
0104     // Find the contributing particle
0105     // @todo: Use identify_contributing_particles function
0106     std::map<particle, std::size_t> contributing_particles =
0107         meas_to_ptc_map.at(meas);
0108 
0109     const particle ptc = contributing_particles.begin()->first;
0110 
0111     // Find the truth global position and momentum
0112     const point3 global_pos = meas_to_param_map.at(meas).first;
0113     const vector3 global_mom = meas_to_param_map.at(meas).second;
0114 
0115     const detray::tracking_surface sf{det, meas.surface_link()};
0116     const point2 truth_bound =
0117         sf.global_to_bound(ctx, global_pos, vector::normalize(global_mom));
0118 
0119     // Return value
0120     bound_track_parameters<> truth_param{};
0121     truth_param.set_bound_local(truth_bound);
0122     truth_param.set_phi(vector::phi(global_mom));
0123     truth_param.set_theta(vector::theta(global_mom));
0124     // @todo: Assign a proper value to time
0125     truth_param.set_time(0.f);
0126     truth_param.set_qop(ptc.charge / vector::norm(global_mom));
0127 
0128     // For the moment, only fill with the first measurements
0129     write_res(truth_param, trk_state.smoothed_params(), ptc);
0130     write_stat(track, track_states, measurements);
0131   }
0132 
0133   /// Writing caches into the file
0134   void finalize();
0135 
0136  private:
0137   /// Non-templated part of the @c write(...) function
0138   void write_res(const bound_track_parameters<>& truth_param,
0139                  const bound_track_parameters<>& fit_param,
0140                  const particle& ptc);
0141 
0142   /// Non-templated part of the @c write(...) function
0143   void write_stat(
0144       const edm::track_collection<traccc::default_algebra>::host::proxy_type
0145           track,
0146       const edm::track_state_collection<traccc::default_algebra>::host&
0147           track_states,
0148       const edm::measurement_collection::host& measurements);
0149 
0150   /// Configuration for the tool
0151   config m_cfg;
0152 
0153   /// Opaque data members for the class
0154   std::unique_ptr<details::fitting_performance_writer_data> m_data;
0155 
0156 };  // class fitting_performance_writer
0157 
0158 }  // namespace traccc