ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
bfield.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
12#include "detray/definitions/algebra.hpp"
13#include "detray/io/utils/file_handle.hpp"
14
15// Covfie include(s)
16#include <covfie/core/backend/primitive/constant.hpp>
17#include <covfie/core/backend/transformer/affine.hpp>
18#include <covfie/core/backend/transformer/clamp.hpp>
19#include <covfie/core/backend/transformer/linear.hpp>
20#include <covfie/core/backend/transformer/nearest_neighbour.hpp>
21#include <covfie/core/backend/transformer/strided.hpp>
22#include <covfie/core/field.hpp>
23#include <covfie/core/vector.hpp>
24
25// System include(s)
26#include <ios>
27#include <iostream>
28#include <stdexcept>
29#include <string>
30
31namespace detray {
32
33namespace bfield {
34
36template <typename T>
37using const_bknd_t = covfie::backend::constant<covfie::vector::vector_d<T, 3>,
38 covfie::vector::vector_d<T, 3>>;
39
40template <typename T>
41using const_field_t = covfie::field<const_bknd_t<T>>;
42
44template <typename T>
45using inhom_bknd_t = covfie::backend::affine<
46 covfie::backend::linear<covfie::backend::clamp<covfie::backend::strided<
47 covfie::vector::vector_d<std::size_t, 3>,
48 covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>>;
49
50// Test that the type is a valid backend for a field
51static_assert(covfie::concepts::field_backend<inhom_bknd_t<float>>,
52 "inhom_bknd_t is not a valid host field backend type");
53
55template <typename T>
56using inhom_bknd_io_t =
57 covfie::backend::affine<covfie::backend::linear<covfie::backend::strided<
58 covfie::vector::vector_d<std::size_t, 3>,
59 covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>;
60
61static_assert(covfie::concepts::field_backend<inhom_bknd_io_t<float>>,
62 "inhom_bknd_io_t is not a valid host field backend type");
63
65template <typename T>
66using inhom_bknd_nn_t =
67 covfie::backend::affine<covfie::backend::nearest_neighbour<
68 covfie::backend::clamp<covfie::backend::strided<
69 covfie::vector::ulong3,
70 covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>>;
71
72static_assert(covfie::concepts::field_backend<inhom_bknd_nn_t<float>>,
73 "inhom_bknd_nn_t is not a valid host field backend type");
74
76template <typename T>
77using inhom_bknd_nn_io_t = covfie::backend::affine<
78 covfie::backend::nearest_neighbour<covfie::backend::strided<
79 covfie::vector::ulong3,
80 covfie::backend::array<covfie::vector::vector_d<T, 3>>>>>;
81
82static_assert(covfie::concepts::field_backend<inhom_bknd_nn_io_t<float>>,
83 "inhom_bknd_nn_io_t is not a valid host field backend type");
84
85template <typename T>
86using inhom_field_t = covfie::field<inhom_bknd_t<T>>;
87
88template <typename T>
89using inhom_field_io_t = covfie::field<inhom_bknd_io_t<T>>;
90
91} // namespace bfield
92
97inline bool check_covfie_file(const std::string& file_name) {
98 // Open binary file
99 io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
100
101 // See "covfie/lib/core/utility/binary_io.hpp"
102 std::uint32_t hdr = covfie::utility::read_binary<std::uint32_t>(*file);
103
104 // Compare to magic bytes
105 return (hdr == covfie::utility::MAGIC_HEADER);
106}
107
111template <typename bfield_t>
112inline bfield_t read_bfield(const std::string& file_name) {
113 if (!check_covfie_file(file_name)) {
114 throw std::runtime_error("Not a valid covfie file: " + file_name);
115 }
116
117 // Open binary file
118 io::file_handle file{file_name, std::ios_base::in | std::ios_base::binary};
119
120 return bfield_t(*file);
121}
122
125template <typename T, concepts::vector3D vector3_t>
126inline bfield::const_field_t<T> create_const_field(const vector3_t& B) {
127 return bfield::const_field_t<T>{covfie::make_parameter_pack(
128 typename bfield::const_bknd_t<T>::configuration_t{B[0], B[1], B[2]})};
129}
130
132template <typename T>
133inline bfield::inhom_field_t<T> create_inhom_field() {
134 return bfield::inhom_field_t<T>(read_bfield<bfield::inhom_field_io_t<T>>(
135 !std::getenv("DETRAY_BFIELD_FILE") ? ""
136 : std::getenv("DETRAY_BFIELD_FILE")));
137}
138
139} // namespace detray
Wrapper around a file stream.
Definition file_handle.hpp:42
Detray include(s).
Definition array_operators.hpp:18
bool check_covfie_file(const std::string &file_name)
Function that reads the first 4 bytes of a potential bfield file and checks that it contains data for...
Definition bfield.hpp:97
bfield::const_field_t< T > create_const_field(const vector3_t &B)
Definition bfield.hpp:126
bfield_t read_bfield(const std::string &file_name)
function that reads a covfie field from file
Definition bfield.hpp:112
bfield::inhom_field_t< T > create_inhom_field()
Definition bfield.hpp:133