Skip to main content

gondola_core/physics/
tracklet.rs

1// This file is part of gaps-online-software and published 
2// under the GPLv3 license
3
4use crate::prelude::*;
5
6// copy over the G4Process so that we do not need to
7// include G4 here
8/// Types of serializable data structures used
9/// throughout the TOF system. 
10#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, FromRepr, AsRefStr, EnumIter)]
11#[cfg_attr(feature = "pybindings", pyclass(eq, eq_int))]
12#[repr(u8)]
13pub enum G4ProcessType {
14   NotDefined        = 0u8,
15   Transportation    = 10u8,
16   Electromagnetic   = 20u8,
17   Optical           = 30u8,
18   Hadronic          = 40u8,
19   PhotoLeptonHadron = 50u8,
20   Decay             = 60u8,
21   General           = 70u8,
22   Parametrisation   = 80u8,
23   UserDefined       = 90u8,
24   Parallel          = 100u8,
25   Phonon            = 110u8,
26   Ucn               = 120u8,
27   Unknown           = 255u8, // since 0 is already assigned
28}
29
30// in case we have pybindings for this type, 
31// expand it so that it can be used as keys
32// in dictionaries
33#[cfg(feature = "pybindings")]
34#[pymethods]
35impl G4ProcessType {
36
37  #[getter]
38  fn __hash__(&self) -> usize {
39    (*self as u8) as usize
40  } 
41}
42
43expand_and_test_enum!(G4ProcessType, test_g4processtype_repr);
44
45//------------------------------------------------------------
46
47
48/// A "RecoHit" is truly just an assembly of coordinates, time 
49/// and energy, allowing to carry an error on the position along. 
50#[derive(Debug, Copy, Clone, PartialEq)]
51#[cfg_attr(feature="pybindings", pyclass)] 
52pub struct RecoHit {
53  pub x      : f32,
54  pub x_err  : f32,
55  pub y      : f32,
56  pub y_err  : f32,
57  pub z      : f32,
58  pub z_err  : f32,
59  pub time   : f32,
60  pub energy : f32,
61  pub volume : u32
62}
63
64impl RecoHit {
65  pub fn new() -> Self {
66    Self {
67      x      : 0.0,
68      x_err  : 0.0,
69      y      : 0.0,
70      y_err  : 0.0,
71      z      : 0.0,
72      z_err  : 0.0,
73      time   : 0.0,
74      energy : 0.0,
75      volume : 0
76    }
77  }
78}
79
80impl Serialization for RecoHit {
81  //FIXME - this has fixed size!!
82  fn from_bytestream(stream : &Vec<u8>,
83                     pos    : &mut usize) 
84    -> Result<Self, SerializationError> {
85    let mut hit = Self::new();
86    let head    = parse_u16(stream, pos);
87    if head != Self::HEAD {
88      error!("Decoding of HEAD failed! Got {} instead!", head);
89      return Err(SerializationError::HeadInvalid);
90    }
91    hit.x      = parse_f32(stream, pos);
92    hit.x_err  = parse_f32(stream, pos);
93    hit.y      = parse_f32(stream, pos);
94    hit.y_err  = parse_f32(stream, pos);
95    hit.z      = parse_f32(stream, pos);
96    hit.z_err  = parse_f32(stream, pos);
97    hit.time   = parse_f32(stream, pos);
98    hit.energy = parse_f32(stream, pos);
99    hit.volume = parse_u32(stream, pos);
100    let tail   = parse_u16(stream, pos);
101    if tail != Self::TAIL {
102      //error!("Decoding of TAIL failed for version {}! Got {} instead!", version, tail);
103      error!("Decoding of TAIL failed! Got {} instead!", tail);
104      return Err(SerializationError::TailInvalid);
105    }
106    Ok(hit) 
107  }
108}
109
110impl fmt::Display for RecoHit {
111  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112    let mut repr = String::from("<RecoHit");
113    repr += &(format!("\n  x {:.2} y {:.2} z {:.2}", self.x, self.y, self.z));
114    repr += &(format!("\n  energy {:.2}", self.energy));
115    repr += &(format!("\n  time   {:.2}>", self.time));
116    repr += &(format!("\n  volume {}>", self.volume));
117    write!(f,"{}", repr)
118  } 
119}
120
121// --------------------------------------------
122
123/// This is semantics. However, in a vertex the 
124/// energy will return the energy of the particle 
125/// at that actual position, not the energy depositions
126type Vertex = RecoHit;
127
128// --------------------------------------------
129
130/// A representation of a physics track, as a single, 
131/// unbent, stright line
132#[derive(Debug, Copy, Clone, PartialEq)]
133#[cfg_attr(feature="pybindings", pyclass)] 
134pub struct Tracklet {
135  pub start         : Vertex,
136  pub stop          : Vertex,
137  pub is_infinite   : bool,
138  pub vertex_mom_x  : f32,
139  pub vertex_mom_y  : f32,
140  pub vertex_mom_z  : f32,
141  /// particle identifier
142  pub pdg           : i32,
143  pub beta          : f32,
144}
145
146impl Tracklet {
147  pub fn new() -> Self {
148    Self {
149      start         : RecoHit::new(),
150      stop          : RecoHit::new(),
151      is_infinite   : true,
152      vertex_mom_x  : 0.0,
153      vertex_mom_y  : 0.0,
154      vertex_mom_z  : 0.0,
155      pdg           : 0,
156      beta          : 0.0,
157    }
158  }
159
160  pub fn get_vertex_pos(&self) -> (f32,f32,f32) {
161    (self.start.x, self.start.y, self.start.z)
162  }
163  
164  pub fn get_vertex_mom(&self) -> (f32,f32,f32) {
165    (self.vertex_mom_x, self.vertex_mom_y, self.vertex_mom_z)
166  }
167}
168
169impl Serialization for Tracklet {
170  //FIXME - this has fixed size!!
171  fn from_bytestream(stream : &Vec<u8>,
172                     pos    : &mut usize) 
173    -> Result<Self, SerializationError> {
174    let mut tracklet = Self::new();
175    let head    = parse_u16(stream, pos);
176    if head != Self::HEAD {
177      error!("Decoding of HEAD failed! Got {} instead!", head);
178      return Err(SerializationError::HeadInvalid);
179    }
180    tracklet.start         = RecoHit::from_bytestream(stream, pos)?;
181    tracklet.stop          = RecoHit::from_bytestream(stream, pos)?;
182    tracklet.is_infinite   = parse_bool(stream, pos);
183    tracklet.vertex_mom_x  = parse_f32(stream, pos);
184    tracklet.vertex_mom_y  = parse_f32(stream, pos);
185    tracklet.vertex_mom_z  = parse_f32(stream, pos);
186    tracklet.pdg           = parse_i32(stream, pos);
187    tracklet.beta          = parse_f32(stream, pos);
188    let tail   = parse_u16(stream, pos);
189    if tail != Self::TAIL {
190      //error!("Decoding of TAIL failed for version {}! Got {} instead!", version, tail);
191      error!("Decoding of TAIL failed! Got {} instead!", tail);
192      return Err(SerializationError::TailInvalid);
193    }
194    Ok(tracklet) 
195  }
196}
197
198#[cfg(feature="pybindings")]
199#[pymethods]
200impl Tracklet {
201  
202  #[getter]
203  #[pyo3(name="vertex_mom")]
204  fn get_vertex_mom_py(&self) -> (f32,f32,f32) {
205    self.get_vertex_mom() 
206  }
207
208  #[getter] 
209  #[pyo3(name="vertex_energy")]
210  fn get_vertex_energy_py(&self) -> f32 {
211    self.start.energy
212  }
213
214  #[getter]
215  #[pyo3(name="vertex_pos")]
216  fn get_vertex_pos_py(&self) -> (f32,f32,f32) {
217    self.get_vertex_pos()
218  }
219  
220  #[getter]
221  #[pyo3(name="beta")]
222  fn get_beta_py(&self) -> f32 {
223    self.beta
224  }
225
226  /// Time at the vertex
227  #[getter]
228  #[pyo3(name="vertex_time")]
229  fn get_vertex_time_py(&self) -> f32 {
230    self.start.time
231  }
232
233  /// Position where the particle stopped (for the MC primary:
234  /// where it came to rest or interacted)
235  #[getter]
236  #[pyo3(name="stop_pos")]
237  fn get_stop_pos_py(&self) -> (f32,f32,f32) {
238    (self.stop.x, self.stop.y, self.stop.z)
239  }
240  
241  /// Volume where the particle stopped (for the MC primary:
242  /// where it came to rest or interacted)
243  #[getter]
244  #[pyo3(name="stop_volume")]
245  fn get_stop_volume_py(&self) -> u32 {
246    self.stop.volume
247  }
248
249  /// Time at the stopping position
250  #[getter]
251  #[pyo3(name="stop_time")]
252  fn get_stop_time_py(&self) -> f32 {
253    self.stop.time
254  }
255
256  /// Kinetic energy at the stopping position
257  #[getter]
258  #[pyo3(name="stop_energy")]
259  fn get_stop_energy_py(&self) -> f32 {
260    self.stop.energy
261  }
262
263  /// True if the tracklet has no stopping position
264  #[getter]
265  #[pyo3(name="is_infinite")]
266  fn get_is_infinite_py(&self) -> bool {
267    self.is_infinite
268  }
269
270  /// Particle identifier
271  #[getter]
272  #[pyo3(name="pdg")]
273  fn get_pdg_py(&self) -> i32 {
274    self.pdg
275  }
276}
277
278impl fmt::Display for Tracklet {
279  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
280    let mut repr = String::from("<Tracklet");
281    repr += &(format!("\n  vertex {}", self.start));
282    //repr += &(format!("\n  vertex {:.2} {:.2} {:.2}", self.start.x, self.start.y, self.start.z));
283    repr += &(format!("\n  pdg    {}>", self.pdg));
284    write!(f,"{}", repr)
285  } 
286}
287
288#[cfg(feature="pybindings")]
289pythonize!(Tracklet);
290
291
292pub struct Track {
293  pub tracklets : Vec<Tracklet>
294}
295