Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:07

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 // Project include(s)
0012 #include "detray/io/backend/detail/grid_writer.hpp"
0013 #include "detray/io/backend/detail/type_info.hpp"
0014 #include "detray/io/backend/homogeneous_material_writer.hpp"
0015 #include "detray/io/frontend/payloads.hpp"
0016 #include "detray/material/material_slab.hpp"
0017 
0018 // System include(s)
0019 #include <string_view>
0020 
0021 namespace detray::io {
0022 
0023 /// @brief Material maps writer backend
0024 ///
0025 /// Fills a material @c detector_grids_payload from a @c detector instance
0026 class material_map_writer : public detail::grid_writer {
0027   using base_type = detail::grid_writer;
0028   using grid_writer_t = base_type;
0029   using mat_writer_t = homogeneous_material_writer;
0030 
0031  public:
0032   /// Tag the writer as "material_map"
0033   static constexpr std::string_view tag = "material_maps";
0034 
0035   /// Payload type that the reader processes
0036   using payload_type =
0037       detector_grids_payload<surface_material_payload, io::material_id>;
0038 
0039   /// Same constructors for this class as for base_type
0040   using base_type::base_type;
0041 
0042   /// Convert the header information into its payload
0043   template <class detector_t>
0044   static auto header_to_payload(const detector_t& det,
0045                                 const std::string_view det_name) {
0046     return grid_writer_t::header_to_payload(tag, det.material_store(),
0047                                             det_name);
0048   }
0049 
0050   /// Convert the material description of a detector @param det into its io
0051   /// payload
0052   template <class detector_t>
0053   static payload_type to_payload(
0054       const detector_t& det, const typename detector_t::name_map& /*unused*/) {
0055     using algebra_t = typename detector_t::algebra_type;
0056     using material_t = material_slab<typename detector_t::scalar_type>;
0057 
0058     payload_type grids_data;
0059 
0060     for (const auto& vol_desc : det.volumes()) {
0061       // Volume local surface indices
0062       dindex offset{dindex_invalid};
0063 
0064       /// Check if a surface has a metrial map
0065       auto vol = tracking_volume{det, vol_desc};
0066       for (const auto& sf_desc : vol.surfaces()) {
0067         if (sf_desc.index() < offset) {
0068           offset = sf_desc.index();
0069         }
0070 
0071         const auto& mat_link = sf_desc.material();
0072         // Don't look at empty links
0073         if (mat_link.is_invalid() ||
0074             mat_link.id() == detector_t::material::id::e_none) {
0075           continue;
0076         }
0077 
0078         // Do not attempt to convert homogeneous material
0079         if constexpr (detray::concepts::has_material_slabs<detector_t>) {
0080           if (mat_link.id() == detector_t::material::id::e_material_slab) {
0081             continue;
0082           }
0083         }
0084         if constexpr (detray::concepts::has_material_rods<detector_t>) {
0085           if (mat_link.id() == detector_t::material::id::e_material_rod) {
0086             continue;
0087           }
0088         }
0089 
0090         // How to convert a material slab in the grid
0091         auto mat_converter = [&sf_desc](const material_t& mat) {
0092           return mat_writer_t::to_payload<algebra_t>(mat, sf_desc.index());
0093         };
0094 
0095         // Generate the payload
0096         grid_writer_t::to_payload(det.material_store(), mat_link,
0097                                   vol_desc.index(), sf_desc.index() - offset,
0098                                   grids_data, mat_converter);
0099       }
0100     }
0101 
0102     return grids_data;
0103   }
0104 };
0105 
0106 }  // namespace detray::io