File indexing completed on 2026-11-26 09:26:09
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <cmath>
0012 #include <cstdint>
0013 #include <limits>
0014
0015 #include "traccc/definitions/qualifiers.hpp"
0016 #include "traccc/edm/measurement_collection.hpp"
0017 #include "traccc/seeding/strip_spacepoint_formation_data.hpp"
0018
0019 namespace traccc::details {
0020
0021 template <typename collection_t, typename value_t, typename comparison_t>
0022 TRACCC_HOST_DEVICE inline unsigned int collection_bound(
0023 const collection_t& collection, const value_t& value,
0024 comparison_t comparison) {
0025 unsigned int first = 0u;
0026 unsigned int last = static_cast<unsigned int>(collection.size());
0027 while (first < last) {
0028 const unsigned int middle = first + (last - first) / 2u;
0029 if (comparison(collection.at(middle), value)) {
0030 first = middle + 1u;
0031 } else {
0032 last = middle;
0033 }
0034 }
0035 return first;
0036 }
0037
0038 struct strip_surface_before {
0039 TRACCC_HOST_DEVICE bool operator()(
0040 const strip_measurement_surface_info& candidate,
0041 const std::uint64_t surface_link) const {
0042 return candidate.surface_link < surface_link;
0043 }
0044 };
0045
0046
0047 struct strip_material {
0048 point3 center{};
0049 vector3 direction{};
0050 vector3 trajectory{};
0051 vector3 normal{};
0052 scalar half_length{0.f};
0053 unsigned int valid{0u};
0054 };
0055
0056 template <typename surface_info_collection_t>
0057 TRACCC_HOST_DEVICE inline bool find_strip_surface_info(
0058 const surface_info_collection_t& surface_infos,
0059 const std::uint64_t surface_link, strip_measurement_surface_info& result) {
0060 const unsigned int first =
0061 collection_bound(surface_infos, surface_link, strip_surface_before{});
0062 if ((first >= surface_infos.size()) ||
0063 (surface_infos.at(first).surface_link != surface_link)) {
0064 return false;
0065 }
0066 result = surface_infos.at(first);
0067 return true;
0068 }
0069
0070 TRACCC_HOST_DEVICE inline point3 surface_global_position(
0071 const strip_measurement_surface_info& info, const point2& p) {
0072 return {info.origin[0] + p[0] * info.local_u[0] + p[1] * info.local_v[0],
0073 info.origin[1] + p[0] * info.local_u[1] + p[1] * info.local_v[1],
0074 info.origin[2] + p[0] * info.local_u[2] + p[1] * info.local_v[2]};
0075 }
0076
0077 template <typename measurement_backend_t>
0078 TRACCC_HOST_DEVICE inline int measured_strip_index(
0079 const edm::measurement<measurement_backend_t>& measurement,
0080 const strip_index_mapping& mapping) {
0081 if ((mapping.component > 1u) || (mapping.count == 0u) ||
0082 !(mapping.pitch > 0.f)) {
0083 return -1;
0084 }
0085 const scalar local = measurement.local_position()[mapping.component];
0086 const scalar value =
0087 mapping.scale * std::floor(local / mapping.pitch) + mapping.offset;
0088
0089 if (!(value >= static_cast<scalar>(std::numeric_limits<int>::lowest())) ||
0090 !(value <= static_cast<scalar>(std::numeric_limits<int>::max()))) {
0091 return -1;
0092 }
0093 if (mapping.check_before_truncation &&
0094 !((value >= 0.f) && (value < static_cast<scalar>(mapping.count)))) {
0095 return -1;
0096 }
0097 const int index = static_cast<int>(value);
0098 return ((index >= 0) && (static_cast<unsigned int>(index) < mapping.count))
0099 ? index
0100 : -1;
0101 }
0102
0103 struct local_strip_endpoints {
0104 point2 first{};
0105 point2 second{};
0106 unsigned int valid{0u};
0107 };
0108
0109 TRACCC_HOST_DEVICE inline local_strip_endpoints linear_strip_endpoints(
0110 const int strip, const linear_strip_geometry& geometry) {
0111 local_strip_endpoints result{};
0112 if ((strip < 0) || (geometry.n_rows == 0u) ||
0113 (static_cast<unsigned int>(strip) >= geometry.n_strips) ||
0114 !(geometry.pitch > 0.f) || !(geometry.row_length > 0.f)) {
0115 return result;
0116 }
0117 int row = 0;
0118 if (geometry.n_rows > 1u) {
0119 const scalar row_value =
0120 std::floor(geometry.row_coordinate / geometry.row_length) +
0121 static_cast<scalar>(geometry.n_rows) * 0.5f;
0122 if (!(row_value >=
0123 static_cast<scalar>(std::numeric_limits<int>::lowest())) ||
0124 !(row_value <= static_cast<scalar>(std::numeric_limits<int>::max()))) {
0125 return result;
0126 }
0127 row = static_cast<int>(row_value);
0128 }
0129 if ((row < 0) || (static_cast<unsigned int>(row) >= geometry.n_rows)) {
0130 return result;
0131 }
0132 const scalar start =
0133 (static_cast<scalar>(row) - static_cast<scalar>(geometry.n_rows) * 0.5f) *
0134 geometry.row_length;
0135 const scalar end = start + geometry.row_length;
0136 const scalar transverse =
0137 (static_cast<scalar>(strip) -
0138 static_cast<scalar>(geometry.n_strips) * 0.5f + 0.5f) *
0139 geometry.pitch;
0140 result.first = {start, transverse};
0141 result.second = {end, transverse};
0142 result.valid = 1u;
0143 return result;
0144 }
0145
0146 TRACCC_HOST_DEVICE inline point2 radial_strip_position_at_radius(
0147 const int strip, const scalar radius,
0148 const radial_strip_geometry& geometry) {
0149 const scalar phi = (static_cast<scalar>(strip) -
0150 static_cast<scalar>(geometry.n_strips) * 0.5f + 0.5f) *
0151 geometry.angular_pitch;
0152 const scalar b = -2.f * geometry.frame_chord *
0153 std::sin(0.5f * geometry.stereo_angle + phi);
0154 const scalar c =
0155 geometry.frame_chord * geometry.frame_chord - radius * radius;
0156 const scalar strip_r = 0.5f * (-b + std::sqrt(b * b - 4.f * c));
0157 const scalar strip_x = strip_r * std::cos(phi);
0158 const scalar strip_y = strip_r * std::sin(phi);
0159 const scalar x = geometry.cos_stereo * (strip_x - geometry.focal_radius) -
0160 geometry.sin_stereo * strip_y + geometry.focal_radius;
0161 const scalar y = geometry.sin_stereo * (strip_x - geometry.focal_radius) +
0162 geometry.cos_stereo * strip_y;
0163 if (geometry.local_frame == strip_local_frame::polar) {
0164 return {std::sqrt(x * x + y * y), std::atan2(y, x)};
0165 }
0166 return {x, y};
0167 }
0168
0169 template <typename measurement_backend_t>
0170 TRACCC_HOST_DEVICE inline strip_material make_strip_material(
0171 const edm::measurement<measurement_backend_t>& measurement,
0172 const strip_measurement_surface_info& info, const point3& beam_spot) {
0173 strip_material result{};
0174 if (measurement.dimensions() != 1u) {
0175 return result;
0176 }
0177 const int strip = measured_strip_index(measurement, info.index_mapping);
0178 if (strip < 0) {
0179 return result;
0180 }
0181 point3 first{};
0182 point3 second{};
0183 switch (info.geometry_model) {
0184 case strip_geometry_model::linear: {
0185 const auto endpoints = linear_strip_endpoints(strip, info.linear);
0186 if (endpoints.valid == 0u) {
0187 return result;
0188 }
0189 first = surface_global_position(info, endpoints.first);
0190 second = surface_global_position(info, endpoints.second);
0191 break;
0192 }
0193 case strip_geometry_model::radial: {
0194 if ((static_cast<unsigned int>(strip) >= info.radial.n_strips) ||
0195 !(info.radial.angular_pitch > 0.f) ||
0196 !(info.radial.min_radius > 0.f) ||
0197 !(info.radial.max_radius > info.radial.min_radius)) {
0198 return result;
0199 }
0200 first = surface_global_position(
0201 info, radial_strip_position_at_radius(strip, info.radial.min_radius,
0202 info.radial));
0203 second = surface_global_position(
0204 info, radial_strip_position_at_radius(strip, info.radial.max_radius,
0205 info.radial));
0206 break;
0207 }
0208 default:
0209 return result;
0210 }
0211
0212 const scalar center_x = 0.5f * (first[0] + second[0]);
0213 const scalar center_y = 0.5f * (first[1] + second[1]);
0214 const scalar center_z = 0.5f * (first[2] + second[2]);
0215 const scalar direction_x = first[0] - second[0];
0216 const scalar direction_y = first[1] - second[1];
0217 const scalar direction_z = first[2] - second[2];
0218 const scalar trajectory_x = 2.f * (center_x - beam_spot[0]);
0219 const scalar trajectory_y = 2.f * (center_y - beam_spot[1]);
0220 const scalar trajectory_z = 2.f * (center_z - beam_spot[2]);
0221 const scalar length =
0222 std::sqrt(direction_x * direction_x + direction_y * direction_y +
0223 direction_z * direction_z);
0224
0225 result.center = {center_x, center_y, center_z};
0226 result.direction = {direction_x, direction_y, direction_z};
0227 result.trajectory = {trajectory_x, trajectory_y, trajectory_z};
0228 result.normal = {direction_y * trajectory_z - direction_z * trajectory_y,
0229 direction_z * trajectory_x - direction_x * trajectory_z,
0230 direction_x * trajectory_y - direction_y * trajectory_x};
0231 result.half_length = 0.5f * length;
0232 result.valid = length > 0.f ? 1u : 0u;
0233 return result;
0234 }
0235
0236 }