Skip to main content

gondola_core/physics/searchlight/
detector.rs

1//! The detector geometry searchlight needs: TOF paddles and their
2//! overlaps
3// This file is part of gaps-online-software and published
4// under the GPLv3 license
5
6use crate::prelude::*;
7
8use glam::DVec3;
9
10/// A TOF paddle, in mm
11#[derive(Debug, Copy, Clone, PartialEq)]
12pub struct SearchlightPaddle {
13  pub center    : DVec3,
14  /// unit vector along the paddle
15  pub principal : DVec3,
16  /// unit vector along the thickness of the paddle
17  pub normal    : DVec3,
18  pub width     : f64,
19  pub length    : f64,
20}
21
22/// A tracker strip: which coordinate it measures, and the half
23/// length of the strip (mm)
24#[derive(Debug, Copy, Clone, PartialEq)]
25pub struct SearchlightStrip {
26  /// 0: x, 1: y
27  pub measured   : u8,
28  pub half_chord : f64,
29}
30
31/// The detector geometry for searchlight, built once from the
32/// database
33#[derive(Debug, Clone, Default)]
34#[cfg_attr(feature="pybindings", pyclass)]
35pub struct SearchlightGeometry {
36  pub paddles  : HashMap<u8, SearchlightPaddle>,
37  pub overlaps : TofPaddleOverlaps,
38  /// strip id -> strip, needed for the secondary tracks
39  pub strips   : HashMap<u32, SearchlightStrip>,
40}
41
42impl SearchlightGeometry {
43  /// # Arguments:
44  ///   * paddles : paddle id -> paddle, as from the database
45  pub fn new(paddles : &HashMap<u8, TofPaddle>) -> Self {
46    let vec = |v : (f32, f32, f32)| DVec3::new(v.0 as f64, v.1 as f64, v.2 as f64);
47    let mut geo = HashMap::<u8, SearchlightPaddle>::new();
48    for (&pid, p) in paddles {
49      // the database has positions and sizes in cm
50      geo.insert(pid, SearchlightPaddle {
51        center    : 10.0*vec(p.center_pos()),
52        principal : vec(p.principal()),
53        normal    : vec(p.normal()),
54        width     : 10.0*p.width as f64,
55        length    : 10.0*p.length as f64,
56      });
57    }
58    Self {
59      paddles  : geo,
60      overlaps : TofPaddleOverlaps::new(paddles),
61      strips   : HashMap::new(),
62    }
63  }
64
65  /// Radius of the Si(Li) detectors (mm)
66  pub const R_DISK : f64 = 50.0;
67
68  /// Add the tracker strips: a strip measures the coordinate in
69  /// which its centre is offset from the centre of its detector,
70  /// and its length is the chord of the detector disk there.
71  ///
72  /// # Arguments:
73  ///   * strips : strip id -> strip, as from the database
74  pub fn with_strips(mut self, strips : &HashMap<u32, TrackerStrip>) -> Self {
75    for (&sid, st) in strips {
76      // the database has positions in cm
77      let ox = 10.0*st.global_pos_x_l0 as f64 - 10.0*st.global_pos_x_det_l0 as f64;
78      let oy = 10.0*st.global_pos_y_l0 as f64 - 10.0*st.global_pos_y_det_l0 as f64;
79      let (measured, o) = if ox.abs() > oy.abs() { (0u8, ox) } else { (1u8, oy) };
80      let half_chord = (Self::R_DISK*Self::R_DISK - o*o).max(100.0).sqrt();
81      self.strips.insert(sid, SearchlightStrip { measured, half_chord });
82    }
83    self
84  }
85
86  /// The point on the axis of the paddle which is closest to
87  /// the line through a along u (unit vector), limited to the
88  /// length of the paddle, and its distance from the line.
89  /// None if the paddle is unknown.
90  pub fn closest_on_paddle(&self, paddle_id : u8, a : DVec3, u : DVec3) -> Option<(DVec3, f64)> {
91    let p  = self.paddles.get(&paddle_id)?;
92    let h  = 0.5*p.length;
93    let w  = p.center - a;
94    let b  = p.principal.dot(u);
95    let d  = 1.0 - b*b;
96    let s  = if d < 1e-9 { 0.0 } else { (b*u.dot(w) - p.principal.dot(w))/d };
97    let pt = p.center + s.clamp(-h, h)*p.principal;
98    let r  = pt - a;
99    Some((pt, (r - r.dot(u)*u).length()))
100  }
101}