Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2022-2025 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/bfield/construct_const_bfield.hpp"
0010 #include "traccc/bfield/magnetic_field_types.hpp"
0011 #include "traccc/definitions/primitives.hpp"
0012 #include "traccc/edm/track_parameters.hpp"
0013 #include "traccc/io/detector.hpp"
0014 #include "traccc/io/utils.hpp"
0015 #include "traccc/options/generation.hpp"
0016 #include "traccc/options/output_data.hpp"
0017 #include "traccc/options/program_options.hpp"
0018 #include "traccc/options/telescope_detector.hpp"
0019 #include "traccc/options/track_propagation.hpp"
0020 #include "traccc/simulation/event_generators.hpp"
0021 #include "traccc/simulation/measurement_smearer.hpp"
0022 #include "traccc/simulation/simulator.hpp"
0023 #include "traccc/simulation/smearing_writer.hpp"
0024 
0025 // detray include(s).
0026 #include <detray/geometry/mask.hpp>
0027 #include <detray/geometry/shapes/rectangle2D.hpp>
0028 #include <detray/material/material.hpp>
0029 #include <detray/test/common/build_telescope_detector.hpp>
0030 #include <detray/tracks/ray.hpp>
0031 
0032 // VecMem include(s).
0033 #include <vecmem/memory/host_memory_resource.hpp>
0034 
0035 // Boost include(s).
0036 #include <boost/filesystem.hpp>
0037 
0038 using namespace traccc;
0039 
0040 int simulate(const traccc::opts::generation& generation_opts,
0041              const traccc::opts::output_data& output_opts,
0042              const traccc::opts::track_propagation& propagation_opts,
0043              const traccc::opts::telescope_detector& telescope_opts) {
0044   // Use deterministic random number generator for testing
0045   using uniform_gen_t =
0046       detray::detail::random_numbers<scalar,
0047                                      std::uniform_real_distribution<scalar>>;
0048 
0049   // Memory resource
0050   vecmem::host_memory_resource host_mr;
0051 
0052   /*****************************
0053    * Build a telescope geometry
0054    *****************************/
0055 
0056   const auto align_vec = telescope_opts.align_vector;
0057   vector3 align_axis{align_vec[0u], align_vec[1u], align_vec[2u]};
0058   align_axis = vector::normalize(align_axis);
0059 
0060   // Plane alignment direction (aligned to z-axis)
0061   detray::detail::ray<traccc::default_algebra> traj{
0062       {0, 0, 0}, 0, align_axis, -1};
0063   // Position of planes (in mm unit)
0064   std::vector<scalar> plane_positions;
0065 
0066   for (unsigned int i = 0; i < telescope_opts.n_planes; i++) {
0067     plane_positions.push_back(static_cast<float>(i) * telescope_opts.spacing);
0068   }
0069 
0070   // B field value and its type
0071   using b_field_t = covfie::field<traccc::const_bfield_backend_t<scalar>>;
0072   const vector3 B{0, 0, 2 * traccc::unit<scalar>::T};
0073   b_field_t field = traccc::construct_const_bfield(B)
0074                         .as_field<traccc::const_bfield_backend_t<scalar>>();
0075 
0076   // Set material and thickness
0077   detray::material<scalar> mat;
0078   if (!telescope_opts.empty_material) {
0079     mat = detray::silicon<scalar>();
0080   } else {
0081     mat = detray::vacuum<scalar>();
0082   }
0083 
0084   const scalar thickness = telescope_opts.thickness;
0085 
0086   // Use rectangle surfaces
0087   detray::mask<detray::rectangle2D, traccc::default_algebra> rectangle{
0088       0u, telescope_opts.half_length, telescope_opts.half_length};
0089 
0090   detray::tel_det_config tel_cfg{rectangle};
0091   tel_cfg.positions(plane_positions);
0092   tel_cfg.module_material(mat);
0093   tel_cfg.mat_thickness(thickness);
0094   tel_cfg.pilot_track(traj);
0095 
0096   const auto [det, name_map] = build_telescope_detector(host_mr, tel_cfg);
0097 
0098   /***************************
0099    * Generate simulation data
0100    ***************************/
0101 
0102   // Origin of particles
0103   using generator_type =
0104       detray::random_track_generator<traccc::free_track_parameters<>,
0105                                      uniform_gen_t>;
0106   generator_type::configuration gen_cfg{};
0107   gen_cfg.n_tracks(generation_opts.gen_nparticles);
0108   gen_cfg.origin(traccc::point3{generation_opts.vertex[0],
0109                                 generation_opts.vertex[1],
0110                                 generation_opts.vertex[2]});
0111   gen_cfg.origin_stddev(traccc::point3{generation_opts.vertex_stddev[0],
0112                                        generation_opts.vertex_stddev[1],
0113                                        generation_opts.vertex_stddev[2]});
0114   gen_cfg.phi_range(generation_opts.phi_range);
0115   gen_cfg.theta_range(generation_opts.theta_range);
0116   gen_cfg.mom_range(generation_opts.mom_range);
0117   gen_cfg.charge(generation_opts.ptc_type.charge());
0118   generator_type generator(gen_cfg);
0119 
0120   // Smearing value for measurements
0121   traccc::measurement_smearer<traccc::default_algebra> meas_smearer(
0122       telescope_opts.smearing, telescope_opts.smearing);
0123 
0124   // Type declarations
0125   using detector_type = decltype(det);
0126   using writer_type = traccc::smearing_writer<
0127       traccc::measurement_smearer<traccc::default_algebra>>;
0128 
0129   // Writer config
0130   typename writer_type::config smearer_writer_cfg{meas_smearer};
0131 
0132   // Run simulator
0133   const std::string full_path = io::data_directory() + output_opts.directory;
0134 
0135   boost::filesystem::create_directories(full_path);
0136 
0137   auto sim =
0138       traccc::simulator<detector_type, b_field_t, generator_type, writer_type>(
0139           generation_opts.ptc_type, generation_opts.events, det, field,
0140           std::move(generator), std::move(smearer_writer_cfg), full_path);
0141   sim.get_config().propagation = propagation_opts;
0142 
0143   sim.run();
0144 
0145   // Create detector file
0146   auto writer_cfg = detray::io::detector_writer_config{}
0147                         .format(detray::io::format::json)
0148                         .replace_files(true);
0149   detray::io::write_detector(det, name_map, writer_cfg);
0150 
0151   return 1;
0152 }
0153 
0154 // The main routine
0155 //
0156 int main(int argc, char* argv[]) {
0157   std::unique_ptr<const traccc::Logger> logger = traccc::getDefaultLogger(
0158       "TracccExampleSimulateTelescope", traccc::Logging::Level::INFO);
0159 
0160   // Program options.
0161   traccc::opts::generation generation_opts;
0162   traccc::opts::output_data output_opts;
0163   traccc::opts::track_propagation propagation_opts;
0164   traccc::opts::telescope_detector telescope_opts;
0165   traccc::opts::program_options program_opts{
0166       "Telescope-Detector Simulation",
0167       {generation_opts, output_opts, propagation_opts, telescope_opts},
0168       argc,
0169       argv,
0170       logger->cloneWithSuffix("Options")};
0171 
0172   // Run the application.
0173   return simulate(generation_opts, output_opts, propagation_opts,
0174                   telescope_opts);
0175 }