1use crate::prelude::*;
8
9pub fn unpack_traces<T>(events : &Vec<RBEvent>)
25 -> Vec<Vec<Vec<T>>>
26 where T: Float + NumAssign + NumCast + Copy {
27 let nevents = events.len();
28 let mut traces = Vec::<Vec::<Vec::<T>>>::new();
29 let mut trace = Vec::<T>::with_capacity(NWORDS);
30 let mut stop_cells = Vec::<isize>::new();
31 let mut empty_events = Vec::<Vec::<T>>::new();
32 for _ in 0..nevents {
33 empty_events.push(trace.clone());
34 }
35 for ch in 0..NCHN {
36 traces.push(empty_events.clone());
37 for (k,ev) in events.iter().enumerate() {
38 trace.clear();
39 stop_cells.push(ev.header.stop_cell as isize);
40 for k in 0..NWORDS {
41 trace.push(T::from(ev.adc[ch][k]).unwrap());
44 }
45 traces[ch][k] = trace.clone();
46 }
47 }
48 traces
49}
50
51
52#[derive(Debug, Clone, PartialEq)]
54#[cfg_attr(feature="pybindings", pyclass)]
55pub struct RBEvent {
56 pub data_type : DataType,
57 pub status : EventStatus,
58 pub header : RBEventHeader,
59 pub adc : Vec<Vec<u16>>,
60 pub hits : Vec<TofHit>,
61 pub creation_time : Option<Instant>,
63}
64
65impl RBEvent {
66
67 pub fn new() -> Self {
68 let mut adc = Vec::<Vec<u16>>::with_capacity(NCHN);
69 for _ in 0..NCHN {
70 adc.push(Vec::<u16>::new());
71 }
72 Self {
73 data_type : DataType::Unknown,
74 status : EventStatus::Unknown,
75 header : RBEventHeader::new(),
76 adc : adc,
77 hits : Vec::<TofHit>::new(),
78 creation_time : Some(Instant::now())
79 }
80 }
81
82 pub fn trace_check(&self) -> bool {
84 let mut check = true;
85 let mut nchan = 0usize;
86 let mut failed = true;
87 for ch in self.header.get_channels() {
88 if self.adc[ch as usize].len() != NWORDS {
89 check = false;
90 }
91 for k in &self.adc[ch as usize] {
92 if *k != u16::MAX {
93 failed = false;
100 }
101 }
102 nchan += 1;
103 }
104 check && nchan == NCHN && !failed
107 }
108
109 pub fn has_any_mangling_flag(&self) -> bool {
110 match self.status {
111 EventStatus::ChnSyncErrors
112 | EventStatus::CellSyncErrors
113 | EventStatus::CellAndChnSyncErrors => {
114 return true;
115 }
116 _ => {
117 return false;
118 }
119 }
120 }
121
122 pub fn self_check(&self) -> Result<(),AnalysisError> {
125 let mut pass = false;
126 for ch in self.header.get_channels() {
127 if self.adc[ch as usize].len() == 0 {
128 error!("RB {} expects ch {} but it is empty!", self.header.rb_id, ch + 1);
129 println!("{}", self.header);
130 pass = false;
131 }
132 }
133 if !pass {
134 return Err(AnalysisError::MissingChannel);
135 }
136 Ok(())
137 }
138
139 pub fn get_rbwaveforms(&self) -> Vec<RBWaveform> {
141 let mut waveforms = Vec::<RBWaveform>::new();
143 let active_channels = self.header.get_channels();
145 let pid = self.header.get_rbpaddleid();
146 if active_channels.contains(&0) || active_channels.contains(&1) {
147 let paddle_id = pid.get_paddle_id(1);
148 let mut wf = RBWaveform::new();
149 wf.rb_id = self.header.rb_id;
150 wf.event_id = self.header.event_id;
151 wf.stop_cell = self.header.stop_cell;
152 wf.paddle_id = paddle_id.0;
153 if paddle_id.1 {
154 wf.adc_b = self.adc[0].clone();
156 wf.adc_a = self.adc[1].clone();
157 wf.rb_channel_b = 0;
158 wf.rb_channel_a = 1;
159 } else {
160 wf.adc_a = self.adc[0].clone();
161 wf.adc_b = self.adc[1].clone();
162 wf.rb_channel_b = 1;
163 wf.rb_channel_a = 0;
164 }
165 waveforms.push(wf);
166 }
167 if active_channels.contains(&2) || active_channels.contains(&3) {
168 let paddle_id = pid.get_paddle_id(3);
169 let mut wf = RBWaveform::new();
170 wf.rb_id = self.header.rb_id;
171 wf.event_id = self.header.event_id;
172 wf.stop_cell = self.header.stop_cell;
173 wf.paddle_id = paddle_id.0;
174 if paddle_id.1 {
175 wf.adc_b = self.adc[2].clone();
177 wf.adc_a = self.adc[3].clone();
178 wf.rb_channel_b = 2;
179 wf.rb_channel_a = 3;
180 } else {
181 wf.adc_a = self.adc[2].clone();
182 wf.adc_b = self.adc[3].clone();
183 wf.rb_channel_b = 3;
184 wf.rb_channel_a = 2;
185 }
186 waveforms.push(wf);
187 }
188 if active_channels.contains(&4) || active_channels.contains(&5) {
189 let paddle_id = pid.get_paddle_id(5);
190 let mut wf = RBWaveform::new();
191 wf.rb_id = self.header.rb_id;
192 wf.event_id = self.header.event_id;
193 wf.stop_cell = self.header.stop_cell;
194 wf.paddle_id = paddle_id.0;
195 if paddle_id.1 {
196 wf.adc_b = self.adc[4].clone();
198 wf.adc_a = self.adc[5].clone();
199 wf.rb_channel_b = 4;
200 wf.rb_channel_a = 5;
201 } else {
202 wf.adc_a = self.adc[4].clone();
203 wf.adc_b = self.adc[5].clone();
204 wf.rb_channel_b = 5;
205 wf.rb_channel_a = 4;
206 }
207 waveforms.push(wf);
208 }
209 if active_channels.contains(&6) || active_channels.contains(&7) {
210 let paddle_id = pid.get_paddle_id(7);
211 let mut wf = RBWaveform::new();
212 wf.rb_id = self.header.rb_id;
213 wf.event_id = self.header.event_id;
214 wf.stop_cell = self.header.stop_cell;
215 wf.paddle_id = paddle_id.0;
216 if paddle_id.1 {
217 wf.adc_b = self.adc[6].clone();
219 wf.adc_a = self.adc[7].clone();
220 wf.rb_channel_b = 6;
221 wf.rb_channel_a = 7;
222 } else {
223 wf.adc_a = self.adc[6].clone();
224 wf.adc_b = self.adc[7].clone();
225 wf.rb_channel_b = 7;
226 wf.rb_channel_a = 6;
227 }
228 waveforms.push(wf);
229 }
230 waveforms
231 }
232
233 pub fn extract_datatype(stream : &Vec<u8>) -> Result<DataType, SerializationError> {
238 if stream.len() < 3 {
239 return Err(SerializationError::StreamTooShort);
240 }
241 Ok(DataType::try_from(stream[2]).unwrap_or(DataType::Unknown))
243 }
244
245 pub fn get_channel_packet_len(stream : &Vec<u8>, pos : usize) -> Result<(usize, Vec::<u8>), SerializationError> {
248 if stream.len() < 8 {
251 return Err(SerializationError::StreamTooShort);
252 }
253 let mut _pos = pos + 4;
254 let packet_len = parse_u16(stream, &mut _pos) as usize * 2; if packet_len < 44 {
256 error!("Event fragment - no channel data!");
258 return Ok((packet_len.into(), Vec::<u8>::new()));
259 }
260 let nwords = parse_u16(stream, &mut _pos) as usize + 1; debug!("Got packet len of {} bytes, roi of {}", packet_len, nwords);
262 let channel_packet_start = pos + 36;
263 let nchan_data = packet_len - 44;
264 if stream.len() < channel_packet_start + nchan_data {
265 error!("We claim there should be channel data, but the event is too short!");
266 return Err(SerializationError::StreamTooShort)
267 }
268
269 let mut nchan = 0usize;
270 while nchan * (2*nwords + 6) < nchan_data {
274 nchan += 1;
275 }
276 if nchan * (2*nwords + 6) != nchan_data {
277 error!("NCHAN consistency check failed! nchan {} , nwords {}, packet_len {}", nchan, nwords, packet_len);
278 }
279 let mut ch_ids = Vec::<u8>::new();
280 _pos = channel_packet_start;
281 for _ in 0..nchan {
282 ch_ids.push(parse_u16(stream, &mut _pos) as u8);
283 _pos += (nwords*2) as usize;
284 _pos += 4; }
286 debug!("Got channel ids {:?}", ch_ids);
287 Ok((nchan_data.into(), ch_ids))
288 }
289
290 pub fn extract_eventid(stream : &Vec<u8>) -> Result<u32, SerializationError> {
295 if stream.len() < 30 {
296 return Err(SerializationError::StreamTooShort);
297 }
298 let event_id = parse_u32(stream, &mut 10);
301 Ok(event_id)
302 }
303
304 pub fn get_ndatachan(&self) -> usize {
305 self.adc.len()
306 }
307
308 pub fn get_channel_by_id(&self, ch : usize) -> Result<&Vec::<u16>, UserError> {
309 if ch >= 9 {
310 error!("channel_by_id expects numbers from 0-8!");
311 return Err(UserError::IneligibleChannelLabel)
312 }
313 return Ok(&self.adc[ch]);
314 }
315
316 pub fn get_channel_by_label(&self, ch : u8) -> Result<&Vec::<u16>, UserError> {
317 if ch == 0 || ch > 9 {
318 error!("channel_by_label expects numbers from 1-9!");
319 return Err(UserError::IneligibleChannelLabel)
320 }
321 Ok(&self.adc[ch as usize -1])
322 }
323
324 pub fn from_bytestream_nowaveforms(stream : &Vec<u8>, pos : &mut usize)
328 -> Result<Self, SerializationError> {
329 let mut event = Self::new();
330 if parse_u16(stream, pos) != Self::HEAD {
331 error!("The given position {} does not point to a valid header signature of {}", pos, Self::HEAD);
332 return Err(SerializationError::HeadInvalid {});
333 }
334 event.data_type = DataType::try_from(parse_u8(stream, pos)).unwrap_or(DataType::Unknown);
335 event.status = EventStatus::try_from(parse_u8(stream, pos)).unwrap_or(EventStatus::Unknown);
336 let n_hits = parse_u8(stream, pos);
338 event.header = RBEventHeader::from_bytestream(stream, pos)?;
339 let stream_len = stream.len();
341 if event.header.is_event_fragment() {
342 debug!("Fragmented event {} found!", event.header.event_id);
343 let tail_pos = seek_marker(stream, Self::TAIL, *pos)?;
344 * pos = tail_pos + 2 as usize;
345 return Ok(event);
348 }
349 if event.header.drs_lost_trigger() {
350 debug!("Event {} has lost trigger!", event.header.event_id);
351 let tail_pos = seek_marker(stream, Self::TAIL, *pos)?;
352 * pos = tail_pos + 2 as usize;
353 return Ok(event);
354 }
355 let mut decoded_ch = Vec::<u8>::new();
356 for ch in event.header.get_channels().iter() {
360 if *pos + 2*NWORDS >= stream_len {
361 error!("The channel data for event {} ch {} seems corrupt! We want to get channels {:?}, but have decoded only {:?}, because the stream ends {} bytes too early!",event.header.event_id, ch, event.header.get_channels(), decoded_ch, *pos + 2*NWORDS - stream_len);
362 let tail_pos = seek_marker(stream, Self::TAIL, *pos)?;
363 * pos = tail_pos + 2 as usize;
364 return Err(SerializationError::WrongByteSize {})
365 }
366 decoded_ch.push(*ch);
367 *pos += 2*NWORDS;
369 }
370 for _ in 0..n_hits {
371 match TofHit::from_bytestream(stream, pos) {
372 Err(err) => {
373 error!("Can't read TofHit! Err {err}");
374 let mut h = TofHit::new();
375 h.valid = false;
376 event.hits.push(h);
377 },
378 Ok(h) => {
379 event.hits.push(h);
380 }
381 }
382 }
383 let tail = parse_u16(stream, pos);
384 if tail != Self::TAIL {
385 error!("After parsing the event, we found an invalid tail signature {}", tail);
386 return Err(SerializationError::TailInvalid);
387 }
388 Ok(event)
389 }
390}
391
392impl TofPackable for RBEvent {
393 const TOF_PACKET_TYPE : TofPacketType = TofPacketType::RBEvent;
394}
395
396impl Serialization for RBEvent {
397 const HEAD : u16 = 0xAAAA;
398 const TAIL : u16 = 0x5555;
399
400 fn from_bytestream(stream : &Vec<u8>, pos : &mut usize)
401 -> Result<Self, SerializationError> {
402 let mut event = Self::new();
403 if parse_u16(stream, pos) != Self::HEAD {
404 error!("The given position {} does not point to a valid header signature of {}", pos, Self::HEAD);
405 return Err(SerializationError::HeadInvalid {});
406 }
407 event.data_type = DataType::try_from(parse_u8(stream, pos)).unwrap_or(DataType::Unknown);
408 event.status = EventStatus::try_from(parse_u8(stream, pos)).unwrap_or(EventStatus::Unknown);
409 let n_hits = parse_u8(stream, pos);
411 event.header = RBEventHeader::from_bytestream(stream, pos)?;
412 let stream_len = stream.len();
414 if event.header.is_event_fragment() {
415 debug!("Fragmented event {} found!", event.header.event_id);
416 let tail_pos = seek_marker(stream, Self::TAIL, *pos)?;
417 * pos = tail_pos + 2 as usize;
418 return Ok(event);
421 }
422 if event.header.drs_lost_trigger() {
423 debug!("Event {} has lost trigger!", event.header.event_id);
424 let tail_pos = seek_marker(stream, Self::TAIL, *pos)?;
425 * pos = tail_pos + 2 as usize;
426 return Ok(event);
427 }
428 let mut decoded_ch = Vec::<u8>::new();
429 for ch in event.header.get_channels().iter() {
430 if *pos + 2*NWORDS >= stream_len {
431 error!("The channel data for event {} ch {} seems corrupt! We want to get channels {:?}, but have decoded only {:?}, because the stream ends {} bytes too early!",event.header.event_id, ch, event.header.get_channels(), decoded_ch, *pos + 2*NWORDS - stream_len);
432 let tail_pos = seek_marker(stream, Self::TAIL, *pos)?;
433 * pos = tail_pos + 2 as usize;
434 return Err(SerializationError::WrongByteSize {})
435 }
436 decoded_ch.push(*ch);
437 let data = &stream[*pos..*pos+2*NWORDS];
439 event.adc[*ch as usize] = u8_to_u16(data);
441 *pos += 2*NWORDS;
442 }
443 for _ in 0..n_hits {
444 match TofHit::from_bytestream(stream, pos) {
445 Err(err) => {
446 error!("Can't read TofHit! Err {err}");
447 let mut h = TofHit::new();
448 h.valid = false;
449 event.hits.push(h);
450 },
451 Ok(h) => {
452 event.hits.push(h);
453 }
454 }
455 }
456 let tail = parse_u16(stream, pos);
457 if tail != Self::TAIL {
460 error!("After parsing the event, we found an invalid tail signature {}", tail);
461 return Err(SerializationError::TailInvalid);
462 }
463 Ok(event)
464 }
465
466 fn to_bytestream(&self) -> Vec<u8> {
467 let mut stream = Vec::<u8>::with_capacity(18530);
469 stream.extend_from_slice(&Self::HEAD.to_le_bytes());
471 stream.push(self.data_type as u8);
472 stream.push(self.status as u8);
473 let n_hits = self.hits.len() as u8;
474 stream.push(n_hits);
475 stream.extend_from_slice(&self.header.to_bytestream());
476 let add_channels = !self.header.is_event_fragment() & !self.header.drs_lost_trigger();
478 if add_channels {
479 for n in 0..NCHN {
480 if self.adc[n].len() == 0 {
481 continue;
482 }
483 for k in 0..NWORDS {
484 stream.extend_from_slice(&self.adc[n][k].to_le_bytes());
485 }
486 }
487 }
492 for h in self.hits.iter() {
493 stream.extend_from_slice(&h.to_bytestream());
494 }
495 stream.extend_from_slice(&Self::TAIL.to_le_bytes());
496 stream
497 }
498}
499
500impl Default for RBEvent {
501
502 fn default () -> Self {
503 Self::new()
504 }
505}
506
507impl fmt::Display for RBEvent {
508 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
509 let mut adc = Vec::<usize>::new();
510 for k in 0..self.adc.len() -1 {
511 adc.push(self.adc[k].len());
512 }
513 let mut ch9_str = String::from("[");
514 for k in self.adc[8].iter().take(5) {
515 ch9_str += &k.to_string();
516 ch9_str += ","
517 }
518 ch9_str += " .. :";
519 ch9_str += &self.adc[8].len().to_string();
520 ch9_str += "]";
521 let mut ch_field = String::from("[\n");
522 for (ch, vals) in self.adc.iter().enumerate() {
523 if ch == 8 {
524 continue;
525 }
526 let label = (ch + 1).to_string();
527 ch_field += " [ch ";
528 ch_field += &ch.to_string();
529 ch_field += "('";
530 ch_field += &label;
531 ch_field += "') ";
532 for n in vals.iter().take(5) {
533 ch_field += &n.to_string();
534 ch_field += ",";
535 }
536 ch_field += "..:";
537 ch_field += &vals.len().to_string();
538 ch_field += "]\n";
539 }
540 ch_field += "]\n";
541 write!(f, "<RBEvent
542 data type : {},
543 event status : {},
544 {}
545 .. ..
546 has ch9 : {},
547 -> ch9 : {},
548 data channels :
549 -> {},
550 n hits : {},
551.. .. .. .. .. .. .. .. >",
552 self.data_type,
553 self.status,
554 self.header,
555 self.header.has_ch9(),
556 ch9_str,
557 ch_field,
558 self.hits.len())
559 }
560}
561
562#[cfg(feature = "random")]
563impl FromRandom for RBEvent {
564
565 fn from_random() -> Self {
566 let mut event = RBEvent::new();
567 let header = RBEventHeader::from_random();
568 let mut rng = rand::rng();
569 event.data_type = DataType::from_random();
570 event.status = EventStatus::from_random();
571 event.header = header;
572 event.header.status_byte = 0;
579 for ch in event.header.get_channels().iter() {
581 let random_numbers: Vec<u16> = (0..NWORDS).map(|_| rng.random()).collect();
582 event.adc[*ch as usize] = random_numbers;
583 }
584 event.creation_time = None;
585 event
586 }
587}
588
589#[cfg(feature="pybindings")]
592#[pymethods]
593impl RBEvent {
594
595 #[getter]
596 fn get_status(&self) -> EventStatus {
597 self.status
598 }
599
600 fn get_waveform<'_py>(&self, py: Python<'_py>, channel : usize) -> PyResult<Bound<'_py, PyArray1<u16>>> {
605 if channel < 1 || channel > 9 {
606 return Err(PyValueError::new_err("Channel must be > 0 and < 9"));
607 }
608 let data = &self.adc[channel - 1];
609 let py_array = data.to_pyarray(py);
610 Ok(py_array)
611 }
612
613 fn get_waveform_slow<'_py>(&self, py: Python<'_py>, channel : usize) -> PyResult<Bound<'_py, PyArray1<u16>>> {
614 let wf = self.get_channel_by_id(channel).unwrap().clone();
615 let arr = PyArray1::<u16>::from_vec(py, wf);
616 Ok(arr)
617 }
618
619
620 #[getter]
621 fn get_hits(&self) -> Vec<TofHit> {
622 self.hits.clone()
623 }
624
625 #[getter]
627 fn get_header<'_py>(&self, py : Python<'_py>) -> PyResult<Bound<'_py , RBEventHeader>> {
628 Bound::<'_py, RBEventHeader>::new(py, self.header)
629 }
630
631 #[getter]
632 fn waveforms(&self) -> Vec<RBWaveform> {
633 self.get_rbwaveforms().clone()
634 }
635}
636
637#[cfg(feature="pybindings")]
638pythonize_packable!(RBEvent);
639
640#[test]
644fn serialization_rbevent() {
645 for _ in 0..100 {
646 let event = RBEvent::from_random();
647 let stream = event.to_bytestream();
648 println!("[test rbevent] stream.len() {:?}", stream.len());
649 let test = RBEvent::from_bytestream(&stream, &mut 0).unwrap();
650 println!("[test rbevent] event frag {:?}", event.header.is_event_fragment());
651 println!("[test rbevent] lost trig {:?}", event.header.drs_lost_trigger());
652 println!("[test rbevent] event frag {:?}", test.header.is_event_fragment());
653 println!("[test rbevent] lost trig {:?}", test.header.drs_lost_trigger());
654 assert_eq!(event.header, test.header);
655 assert_eq!(event.header.get_nchan(), test.header.get_nchan());
656 assert_eq!(event.header.get_channels(), test.header.get_channels());
657 assert_eq!(event.data_type, test.data_type);
658 assert_eq!(event.status, test.status);
659 assert_eq!(event.adc.len(), test.adc.len());
660 assert_eq!(event.hits.len(), test.hits.len());
661 println!("[test rbevent] get_channels() {:?}", event.header.get_channels());
662 assert_eq!(event.adc[0].len(), test.adc[0].len());
663 assert_eq!(event.adc[1].len(), test.adc[1].len());
664 assert_eq!(event.adc[2].len(), test.adc[2].len());
665 assert_eq!(event.adc[3].len(), test.adc[3].len());
666 assert_eq!(event.adc[4].len(), test.adc[4].len());
667 assert_eq!(event.adc[5].len(), test.adc[5].len());
668 assert_eq!(event.adc[6].len(), test.adc[6].len());
669 assert_eq!(event.adc[7].len(), test.adc[7].len());
670 assert_eq!(event.adc[8].len(), test.adc[8].len());
671 assert_eq!(event.adc[0], test.adc[0]);
672 assert_eq!(event.adc[1], test.adc[1]);
673 assert_eq!(event.adc[2], test.adc[2]);
674 assert_eq!(event.adc[3], test.adc[3]);
675 assert_eq!(event.adc[4], test.adc[4]);
676 assert_eq!(event.adc[5], test.adc[5]);
677 assert_eq!(event.adc[6], test.adc[6]);
678 assert_eq!(event.adc[7], test.adc[7]);
679 assert_eq!(event.adc[8], test.adc[8]);
680 }
691}
692
693#[test]
694fn pack_rbevent() {
695 for _ in 0..100 {
696 let mut event = RBEvent::from_random();
697 event.creation_time = None;
698 let mut test : RBEvent = event.pack().unpack().unwrap();
699 test.creation_time = None;
700 assert_eq!(event, test);
701 }
702}
703