use crate::{
ParticleID,
event::{Event, EventBuilder}
};
use std::marker::PhantomData;
use std::os::raw::c_double;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct EventView<'a> {
pub id: usize,
pub weights: *const c_double,
pub type_sets: *const TypeSetView<'a>,
pub n_weights: usize,
pub n_type_sets: usize,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct TypeSetView<'a> {
pub pid: i32,
pub momenta: *const FourMomentum,
pub n_momenta: usize,
phantom: PhantomData<&'a ()>,
}
#[derive(Clone, Debug)]
pub struct TypeSet {
pub pid: i32,
pub momenta: Vec<FourMomentum>,
}
impl TypeSet {
pub fn view(&self) -> TypeSetView<'_> {
TypeSetView {
pid: self.pid,
momenta: self.momenta.as_ptr(),
n_momenta: self.momenta.len(),
phantom: PhantomData,
}
}
}
pub type FourMomentum = [c_double; 4];
impl<'a> From<EventView<'a>> for Event {
fn from(view: EventView<'a>) -> Self {
use crate::n64;
let EventView {
id,
weights,
type_sets,
n_weights,
n_type_sets,
} = view;
let n_particles =
(0..n_type_sets)
.map(|n| unsafe { (*type_sets.add(n)).n_momenta })
.sum();
let mut event = EventBuilder::with_capacity(n_particles);
for n_weight in 0..n_weights {
let weight = unsafe { *weights.add(n_weight) };
event.add_weight(n64(weight));
}
for n_set in 0..n_type_sets {
let TypeSetView {
pid,
momenta,
n_momenta,
..
} = unsafe { *type_sets.add(n_set) };
for n_p in 0..n_momenta {
let p = unsafe { *momenta.add(n_p) };
event.add_outgoing(ParticleID::new(pid), p.map(n64).into());
}
}
let mut event = event.build();
event.id = id;
event
}
}