Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2022 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #pragma once
0009 
0010 // System include(s).
0011 #include <fstream>
0012 #include <string_view>
0013 #include <type_traits>
0014 #include <vector>
0015 
0016 namespace traccc::io::details {
0017 
0018 /// Function for writing a container into a binary file
0019 ///
0020 /// @param filename is the output filename which includes the path
0021 /// @param container is the traccc container to write
0022 ///
0023 template <typename container_t>
0024 void write_binary_container(std::string_view filename,
0025                             const container_t& container) {
0026   // Make sure that the chosen types work.
0027   static_assert(std::is_standard_layout_v<typename container_t::header_type>,
0028                 "Container header type must have standard layout.");
0029   static_assert(std::is_standard_layout_v<typename container_t::item_type>,
0030                 "Container item type must have standard layout.");
0031 
0032   // Open the output file.
0033   std::ofstream out_file(filename.data(), std::ios::binary);
0034 
0035   // Write the size of the header vector.
0036   const std::size_t headers_size = container.size();
0037   out_file.write(reinterpret_cast<const char*>(&headers_size),
0038                  sizeof(std::size_t));
0039 
0040   // Write the sizes of the item vectors.
0041   std::vector<std::size_t> item_sizes;
0042   item_sizes.reserve(headers_size);
0043   for (const typename container_t::item_vector::value_type& i :
0044        container.get_items()) {
0045     item_sizes.push_back(i.size());
0046   }
0047   out_file.write(reinterpret_cast<const char*>(item_sizes.data()),
0048                  headers_size * sizeof(std::size_t));
0049 
0050   // Write header elements.
0051   out_file.write(reinterpret_cast<const char*>(container.get_headers().data()),
0052                  container.get_headers().size() *
0053                      sizeof(typename container_t::header_type));
0054 
0055   // Write the items.
0056   for (const typename container_t::item_vector::value_type& i :
0057        container.get_items()) {
0058     out_file.write(reinterpret_cast<const char*>(i.data()),
0059                    i.size() * sizeof(typename container_t::item_type));
0060   }
0061 }
0062 
0063 /// Function for writing a collection into a binary file
0064 ///
0065 /// @param filename is the output filename which includes the path
0066 /// @param collection is the traccc collection to write
0067 ///
0068 template <typename collection_t>
0069 void write_binary_collection(std::string_view filename,
0070                              const collection_t& collection) {
0071   // Make sure that the chosen types work.
0072   static_assert(std::is_standard_layout_v<typename collection_t::value_type>,
0073                 "Collection item type must have standard layout.");
0074 
0075   // Open the output file.
0076   std::ofstream out_file(filename.data(), std::ios::binary);
0077 
0078   // Write the size of the vector.
0079   const std::size_t size = collection.size();
0080   out_file.write(reinterpret_cast<const char*>(&size), sizeof(std::size_t));
0081 
0082   // Write the items.
0083   out_file.write(reinterpret_cast<const char*>(collection.data()),
0084                  static_cast<std::streamsize>(
0085                      size * sizeof(typename collection_t::value_type)));
0086 }
0087 
0088 /// Implementation detail for @c traccc::io::details::write_binary_soa
0089 template <typename TYPE>
0090 void write_binary_soa_variable(const TYPE& var, std::ostream& out_file) {
0091   // Make sure that the type works.
0092   static_assert(std::is_standard_layout_v<TYPE>,
0093                 "Scalar type does not have a standard layout.");
0094 
0095   // Read the scalar variable as-is.
0096   out_file.write(reinterpret_cast<const char*>(&var), sizeof(TYPE));
0097 }
0098 
0099 /// Implementation detail for @c traccc::io::details::write_binary_soa
0100 template <typename TYPE>
0101 void write_binary_soa_variable(const vecmem::device_vector<TYPE>& var,
0102                                std::ostream& out_file) {
0103   // Make sure that the type works.
0104   static_assert(std::is_standard_layout_v<TYPE>,
0105                 "Vector type does not have a standard layout.");
0106 
0107   // Write the size of the vector.
0108   const std::size_t size = var.size();
0109   out_file.write(reinterpret_cast<const char*>(&size), sizeof(std::size_t));
0110 
0111   // Write the contents of the vector.
0112   out_file.write(reinterpret_cast<const char*>(var.data()),
0113                  static_cast<std::streamsize>(size * sizeof(TYPE)));
0114 }
0115 
0116 /// Implementation detail for @c traccc::io::details::write_binary_soa
0117 template <typename TYPE>
0118 void write_binary_soa_variable(const vecmem::jagged_device_vector<TYPE>& var,
0119                                std::ostream& out_file) {
0120   // Make sure that the type works.
0121   static_assert(std::is_standard_layout_v<TYPE>,
0122                 "Jagged vector type does not have a standard layout.");
0123 
0124   // Write the size of the "outer" vector.
0125   const std::size_t outer_size = var.size();
0126   out_file.write(reinterpret_cast<const char*>(&outer_size),
0127                  sizeof(std::size_t));
0128 
0129   // Write the sizes of the "inner" vectors.
0130   std::vector<std::size_t> inner_sizes(outer_size);
0131   for (std::size_t i = 0; i < outer_size; ++i) {
0132     inner_sizes.at(i) = var.at(i).size();
0133   }
0134   out_file.write(reinterpret_cast<const char*>(inner_sizes.data()),
0135                  outer_size * sizeof(typename std::size_t));
0136 
0137   // Write the inner vectors in multiple steps.
0138   for (std::size_t i = 0; i < outer_size; ++i) {
0139     out_file.write(reinterpret_cast<const char*>(var.at(i).data()),
0140                    inner_sizes.at(i) * sizeof(TYPE));
0141   }
0142 }
0143 
0144 /// Implementation detail for @c traccc::io::details::write_binary_soa
0145 template <std::size_t INDEX, typename... VARTYPES,
0146           template <typename> class INTERFACE>
0147 void write_binary_soa_impl(
0148     const vecmem::edm::device<vecmem::edm::schema<VARTYPES...>, INTERFACE>&
0149         container,
0150     std::ostream& out_file) {
0151   // Write the current variable.
0152   write_binary_soa_variable(container.template get<INDEX>(), out_file);
0153 
0154   // Recurse into the next variable.
0155   if constexpr (sizeof...(VARTYPES) > (INDEX + 1)) {
0156     write_binary_soa_impl<INDEX + 1>(container, out_file);
0157   }
0158 }
0159 
0160 /// Function reading an SoA container from a binary file
0161 ///
0162 /// @param filename The full input filename
0163 /// @param mr Is the memory resource to create the result container with
0164 ///
0165 template <typename... VARTYPES, template <typename> class INTERFACE>
0166 void write_binary_soa(
0167     std::string_view filename,
0168     const vecmem::edm::device<vecmem::edm::schema<VARTYPES...>, INTERFACE>&
0169         container) {
0170   // Open the output file.
0171   std::ofstream out_file(filename.data(), std::ios::binary);
0172 
0173   // Read all variables recursively.
0174   write_binary_soa_impl<0>(container, out_file);
0175 }
0176 
0177 }  // namespace traccc::io::details