Back to home page

EIC code displayed by LXR

 
 

    


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

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/definitions/algebra.hpp"
0013 #include "detray/io/utils/file_handle.hpp"
0014 
0015 // Covfie include(s)
0016 #include <covfie/core/backend/primitive/constant.hpp>
0017 #include <covfie/core/backend/transformer/affine.hpp>
0018 #include <covfie/core/backend/transformer/clamp.hpp>
0019 #include <covfie/core/backend/transformer/linear.hpp>
0020 #include <covfie/core/backend/transformer/nearest_neighbour.hpp>
0021 #include <covfie/core/backend/transformer/strided.hpp>
0022 #include <covfie/core/field.hpp>
0023 #include <covfie/core/vector.hpp>
0024 
0025 // System include(s)
0026 #include <ios>
0027 #include <iostream>
0028 #include <stdexcept>
0029 #include <string>
0030 
0031 namespace detray {
0032 
0033 namespace bfield {
0034 
0035 /// Constant bfield (host and device)
0036 template <typename T>
0037 using const_bknd_t = covfie::backend::constant<covfie::vector::vector_d<T, 3>,
0038                                                covfie::vector::vector_d<T, 3>>;
0039 
0040 template <typename T>
0041 using const_field_t = covfie::field<const_bknd_t<T>>;
0042 
0043 /// Inhomogeneous field (host)
0044 template <typename T>
0045 using inhom_bknd_t = covfie::backend::affine<
0046     covfie::backend::linear<covfie::backend::clamp<covfie::backend::strided<
0047         covfie::vector::vector_d<std::size_t, 3>,
0048         covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>>;
0049 
0050 // Test that the type is a valid backend for a field
0051 static_assert(covfie::concepts::field_backend<inhom_bknd_t<float>>,
0052               "inhom_bknd_t is not a valid host field backend type");
0053 
0054 /// Inhomogeneous field (host) for IO
0055 template <typename T>
0056 using inhom_bknd_io_t =
0057     covfie::backend::affine<covfie::backend::linear<covfie::backend::strided<
0058         covfie::vector::vector_d<std::size_t, 3>,
0059         covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>;
0060 
0061 static_assert(covfie::concepts::field_backend<inhom_bknd_io_t<float>>,
0062               "inhom_bknd_io_t is not a valid host field backend type");
0063 
0064 /// Inhomogeneous field with nearest neighbor (host)
0065 template <typename T>
0066 using inhom_bknd_nn_t =
0067     covfie::backend::affine<covfie::backend::nearest_neighbour<
0068         covfie::backend::clamp<covfie::backend::strided<
0069             covfie::vector::ulong3,
0070             covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>>;
0071 
0072 static_assert(covfie::concepts::field_backend<inhom_bknd_nn_t<float>>,
0073               "inhom_bknd_nn_t is not a valid host field backend type");
0074 
0075 /// Inhomogeneous field with nearest neighbor (host) for IO
0076 template <typename T>
0077 using inhom_bknd_nn_io_t = covfie::backend::affine<
0078     covfie::backend::nearest_neighbour<covfie::backend::strided<
0079         covfie::vector::ulong3,
0080         covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>;
0081 
0082 static_assert(covfie::concepts::field_backend<inhom_bknd_nn_io_t<float>>,
0083               "inhom_bknd_nn_io_t is not a valid host field backend type");
0084 
0085 template <typename T>
0086 using inhom_field_t = covfie::field<inhom_bknd_t<T>>;
0087 
0088 template <typename T>
0089 using inhom_field_io_t = covfie::field<inhom_bknd_io_t<T>>;
0090 
0091 }  // namespace bfield
0092 
0093 /// @brief Function that reads the first 4 bytes of a potential bfield file and
0094 /// checks that it contains data for a covfie field
0095 inline bool check_covfie_file(const std::string& file_name) {
0096   // Open binary file
0097   io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
0098 
0099   // See "covfie/lib/core/utility/binary_io.hpp"
0100   std::uint32_t hdr = covfie::utility::read_binary<std::uint32_t>(*file);
0101 
0102   // Compare to magic bytes
0103   return (hdr == covfie::utility::MAGIC_HEADER);
0104 }
0105 
0106 /// @brief function that reads a covfie field from file
0107 template <typename bfield_t>
0108 inline bfield_t read_bfield(const std::string& file_name) {
0109   if (!check_covfie_file(file_name)) {
0110     throw std::runtime_error("Not a valid covfie file: " + file_name);
0111   }
0112 
0113   // Open binary file
0114   io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
0115 
0116   return bfield_t(*file);
0117 }
0118 
0119 /// @returns a constant covfie field constructed from the field vector @param B
0120 template <typename T, concepts::vector3D vector3_t>
0121 inline bfield::const_field_t<T> create_const_field(const vector3_t& B) {
0122   return bfield::const_field_t<T>{covfie::make_parameter_pack(
0123       typename bfield::const_bknd_t<T>::configuration_t{B[0], B[1], B[2]})};
0124 }
0125 
0126 /// @returns a constant covfie field constructed from the field vector @param B
0127 template <typename T>
0128 inline bfield::inhom_field_t<T> create_inhom_field() {
0129   return bfield::inhom_field_t<T>(read_bfield<bfield::inhom_field_io_t<T>>(
0130       !std::getenv("DETRAY_BFIELD_FILE") ? ""
0131                                          : std::getenv("DETRAY_BFIELD_FILE")));
0132 }
0133 
0134 }  // namespace detray