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-2024 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #pragma once
0009 
0010 // VecMem include(s).
0011 #include <ios>
0012 
0013 #include <vecmem/edm/host.hpp>
0014 #include <vecmem/memory/memory_resource.hpp>
0015 
0016 // System include(s).
0017 #include <cstddef>
0018 #include <fstream>
0019 #include <string_view>
0020 #include <type_traits>
0021 #include <vector>
0022 
0023 namespace traccc::io::details {
0024 
0025 /// Function for reading a container from a binary file
0026 ///
0027 /// @param filename The full input filename
0028 /// @param mr Is the memory resource to create the result container with
0029 ///
0030 /// TODO: Change container reading to not own its result object
0031 template <typename container_t>
0032 container_t read_binary_container(std::string_view filename,
0033                                   vecmem::memory_resource* mr = nullptr) {
0034   // Make sure that the chosen types work.
0035   static_assert(std::is_standard_layout_v<typename container_t::header_type>,
0036                 "Container header type must be standard layout.");
0037   static_assert(std::is_standard_layout_v<typename container_t::item_type>,
0038                 "Container item type must be standard layout.");
0039 
0040   // Open the input file.
0041   std::ifstream in_file(filename.data(), std::ios::binary);
0042 
0043   // Read the size of the header vector.
0044   std::size_t headers_size;
0045   in_file.read(reinterpret_cast<char*>(&headers_size), sizeof(std::size_t));
0046 
0047   // Read the sizes of the item vector.
0048   std::vector<std::size_t> items_size(headers_size);
0049   in_file.read(reinterpret_cast<char*>(items_size.data()),
0050                static_cast<std::streamsize>(headers_size *
0051                                             sizeof(typename std::size_t)));
0052 
0053   // Create the result container, and set it to the correct (outer) size right
0054   // away.
0055   container_t result(headers_size, mr);
0056 
0057   // Read the header payload into memory.
0058   in_file.read(reinterpret_cast<char*>(result.get_headers().data()),
0059                headers_size * sizeof(typename container_t::header_type));
0060 
0061   // Read the items in multiple steps.
0062   for (std::size_t i = 0; i < headers_size; ++i) {
0063     result.get_items().at(i).resize(items_size.at(i));
0064     in_file.read(reinterpret_cast<char*>(result.get_items().at(i).data()),
0065                  items_size.at(i) * sizeof(typename container_t::item_type));
0066   }
0067 
0068   // Return the newly created container.
0069   return result;
0070 }
0071 
0072 /// Function for reading a collection from a binary file
0073 ///
0074 /// @param filename The full input filename
0075 /// @param mr Is the memory resource to create the result collection with
0076 ///
0077 template <typename collection_t>
0078 void read_binary_collection(collection_t& result, std::string_view filename) {
0079   // Make sure that the chosen types work.
0080   static_assert(std::is_standard_layout_v<typename collection_t::value_type>,
0081                 "Collection item type must be standard layout.");
0082 
0083   // Open the input file.
0084   std::ifstream in_file(filename.data(), std::ios::binary);
0085 
0086   // Read the size of the header vector.
0087   std::size_t size;
0088   in_file.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
0089 
0090   // Set result to the correct size.
0091   result.resize(size);
0092 
0093   // Read the items into memory.
0094   in_file.read(reinterpret_cast<char*>(result.data()),
0095                static_cast<std::streamsize>(
0096                    size * sizeof(typename collection_t::value_type)));
0097 }
0098 
0099 /// Implementation detail for @c traccc::io::details::read_binary_soa
0100 template <typename TYPE>
0101 void read_binary_soa_variable(TYPE& result, std::istream& in_file) {
0102   // Make sure that the type works.
0103   static_assert(std::is_standard_layout_v<TYPE>,
0104                 "Scalar type does not have a standard layout.");
0105 
0106   // Read the scalar variable as-is.
0107   in_file.read(reinterpret_cast<char*>(&result), sizeof(TYPE));
0108 }
0109 
0110 /// Implementation detail for @c traccc::io::details::read_binary_soa
0111 template <typename TYPE, typename ALLOC>
0112 void read_binary_soa_variable(std::vector<TYPE, ALLOC>& result,
0113                               std::istream& in_file) {
0114   // Make sure that the type works.
0115   static_assert(std::is_standard_layout_v<TYPE>,
0116                 "Vector type does not have a standard layout.");
0117 
0118   // Read the size of the vector.
0119   std::size_t size;
0120   in_file.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
0121 
0122   // Set result to the correct size.
0123   result.resize(size);
0124 
0125   // Read the items into memory.
0126   in_file.read(reinterpret_cast<char*>(result.data()),
0127                static_cast<std::streamsize>(size * sizeof(TYPE)));
0128 }
0129 
0130 /// Implementation detail for @c traccc::io::details::read_binary_soa
0131 template <typename TYPE, typename ALLOC1, typename ALLOC2>
0132 void read_binary_soa_variable(
0133     std::vector<std::vector<TYPE, ALLOC2>, ALLOC1>& result,
0134     std::istream& in_file) {
0135   // Make sure that the type works.
0136   static_assert(std::is_standard_layout_v<TYPE>,
0137                 "Jagged vector type does not have a standard layout.");
0138 
0139   // Read the size of the "outer" vector.
0140   std::size_t outer_size;
0141   in_file.read(reinterpret_cast<char*>(&outer_size), sizeof(std::size_t));
0142 
0143   // Read the sizes of the "inner" vectors.
0144   std::vector<std::size_t> inner_sizes(outer_size);
0145   in_file.read(
0146       reinterpret_cast<char*>(inner_sizes.data()),
0147       static_cast<std::streamsize>(outer_size * sizeof(typename std::size_t)));
0148 
0149   // Resize the outer vector.
0150   result.resize(outer_size);
0151 
0152   // Read the inner vectors in multiple steps.
0153   for (std::size_t i = 0; i < outer_size; ++i) {
0154     result.at(i).resize(inner_sizes.at(i));
0155     in_file.read(reinterpret_cast<char*>(result.at(i).data()),
0156                  inner_sizes.at(i) * sizeof(TYPE));
0157   }
0158 }
0159 
0160 /// Implementation detail for @c traccc::io::details::read_binary_soa
0161 template <std::size_t INDEX, typename... VARTYPES,
0162           template <typename> class INTERFACE>
0163 void read_binary_soa_impl(
0164     vecmem::edm::host<vecmem::edm::schema<VARTYPES...>, INTERFACE>& result,
0165     std::istream& in_file) {
0166   // Read the current variable.
0167   read_binary_soa_variable(result.template get<INDEX>(), in_file);
0168 
0169   // Recurse into the next variable.
0170   if constexpr (sizeof...(VARTYPES) > (INDEX + 1)) {
0171     read_binary_soa_impl<INDEX + 1>(result, in_file);
0172   }
0173 }
0174 
0175 /// Function reading an SoA container from a binary file
0176 ///
0177 /// @param filename The full input filename
0178 /// @param mr Is the memory resource to create the result container with
0179 ///
0180 template <typename... VARTYPES, template <typename> class INTERFACE>
0181 void read_binary_soa(
0182     vecmem::edm::host<vecmem::edm::schema<VARTYPES...>, INTERFACE>& result,
0183     std::string_view filename) {
0184   // Open the input file.
0185   std::ifstream in_file(filename.data(), std::ios::binary);
0186 
0187   // Read all variables recursively.
0188   read_binary_soa_impl<0>(result, in_file);
0189 }
0190 
0191 }  // namespace traccc::io::details