laddu_core/
data.rs

1use accurate::{sum::Klein, traits::*};
2use arrow::array::Float32Array;
3use arrow::record_batch::RecordBatch;
4use auto_ops::impl_op_ex;
5use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
6use serde::{Deserialize, Serialize};
7use std::ops::{Deref, DerefMut, Index, IndexMut};
8use std::path::Path;
9use std::sync::Arc;
10use std::{fmt::Display, fs::File};
11
12#[cfg(feature = "rayon")]
13use rayon::prelude::*;
14
15#[cfg(feature = "mpi")]
16use mpi::{datatype::PartitionMut, topology::SimpleCommunicator, traits::*};
17
18#[cfg(feature = "mpi")]
19use crate::mpi::LadduMPI;
20
21use crate::utils::get_bin_edges;
22use crate::{
23    utils::{
24        variables::Variable,
25        vectors::{Vec3, Vec4},
26    },
27    Float, LadduError,
28};
29
30const P4_PREFIX: &str = "p4_";
31const AUX_PREFIX: &str = "aux_";
32
33/// An event that can be used to test the implementation of an
34/// [`Amplitude`](crate::amplitudes::Amplitude). This particular event contains the reaction
35/// $`\gamma p \to K_S^0 K_S^0 p`$ with a polarized photon beam.
36pub fn test_event() -> Event {
37    use crate::utils::vectors::*;
38    Event {
39        p4s: vec![
40            Vec3::new(0.0, 0.0, 8.747).with_mass(0.0),         // beam
41            Vec3::new(0.119, 0.374, 0.222).with_mass(1.007),   // "proton"
42            Vec3::new(-0.112, 0.293, 3.081).with_mass(0.498),  // "kaon"
43            Vec3::new(-0.007, -0.667, 5.446).with_mass(0.498), // "kaon"
44        ],
45        aux: vec![Vec3::new(0.385, 0.022, 0.000)],
46        weight: 0.48,
47    }
48}
49
50/// An dataset that can be used to test the implementation of an
51/// [`Amplitude`](crate::amplitudes::Amplitude). This particular dataset contains a singular
52/// [`Event`] generated from [`test_event`].
53pub fn test_dataset() -> Dataset {
54    Dataset::new(vec![Arc::new(test_event())])
55}
56
57/// A single event in a [`Dataset`] containing all the relevant particle information.
58#[derive(Debug, Clone, Default, Serialize, Deserialize)]
59pub struct Event {
60    /// A list of four-momenta for each particle.
61    pub p4s: Vec<Vec4>,
62    /// A list of auxiliary vectors which can be used to store data like particle polarization.
63    pub aux: Vec<Vec3>,
64    /// The weight given to the event.
65    pub weight: Float,
66}
67
68impl Display for Event {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        writeln!(f, "Event:")?;
71        writeln!(f, "  p4s:")?;
72        for p4 in &self.p4s {
73            writeln!(f, "    {}", p4.to_p4_string())?;
74        }
75        writeln!(f, "  eps:")?;
76        for eps_vec in &self.aux {
77            writeln!(f, "    [{}, {}, {}]", eps_vec.x, eps_vec.y, eps_vec.z)?;
78        }
79        writeln!(f, "  weight:")?;
80        writeln!(f, "    {}", self.weight)?;
81        Ok(())
82    }
83}
84
85impl Event {
86    /// Return a four-momentum from the sum of four-momenta at the given indices in the [`Event`].
87    pub fn get_p4_sum<T: AsRef<[usize]>>(&self, indices: T) -> Vec4 {
88        indices.as_ref().iter().map(|i| self.p4s[*i]).sum::<Vec4>()
89    }
90    /// Boost all the four-momenta in the [`Event`] to the rest frame of the given set of
91    /// four-momenta by indices.
92    pub fn boost_to_rest_frame_of<T: AsRef<[usize]>>(&self, indices: T) -> Self {
93        let frame = self.get_p4_sum(indices);
94        Event {
95            p4s: self
96                .p4s
97                .iter()
98                .map(|p4| p4.boost(&(-frame.beta())))
99                .collect(),
100            aux: self.aux.clone(),
101            weight: self.weight,
102        }
103    }
104}
105
106/// A collection of [`Event`]s.
107#[derive(Debug, Clone, Default)]
108pub struct Dataset {
109    /// The [`Event`]s contained in the [`Dataset`]
110    pub events: Vec<Arc<Event>>,
111}
112
113impl Dataset {
114    /// Get a reference to the [`Event`] at the given index in the [`Dataset`] (non-MPI
115    /// version).
116    ///
117    /// # Notes
118    ///
119    /// This method is not intended to be called in analyses but rather in writing methods
120    /// that have `mpi`-feature-gated versions. Most users should just index into a [`Dataset`]
121    /// as if it were any other [`Vec`]:
122    ///
123    /// ```ignore
124    /// let ds: Dataset = Dataset::new(events);
125    /// let event_0 = ds[0];
126    /// ```
127    pub fn index_local(&self, index: usize) -> &Event {
128        &self.events[index]
129    }
130
131    #[cfg(feature = "mpi")]
132    fn get_rank_index(index: usize, displs: &[i32], world: &SimpleCommunicator) -> (i32, usize) {
133        for (i, &displ) in displs.iter().enumerate() {
134            if displ as usize > index {
135                return (i as i32 - 1, index - displs[i - 1] as usize);
136            }
137        }
138        (
139            world.size() - 1,
140            index - displs[world.size() as usize - 1] as usize,
141        )
142    }
143
144    #[cfg(feature = "mpi")]
145    fn partition(events: Vec<Arc<Event>>, world: &SimpleCommunicator) -> Vec<Vec<Arc<Event>>> {
146        let (counts, displs) = world.get_counts_displs(events.len());
147        counts
148            .iter()
149            .zip(displs.iter())
150            .map(|(&count, &displ)| {
151                events
152                    .iter()
153                    .skip(displ as usize)
154                    .take(count as usize)
155                    .cloned()
156                    .collect()
157            })
158            .collect()
159    }
160
161    /// Get a reference to the [`Event`] at the given index in the [`Dataset`]
162    /// (MPI-compatible version).
163    ///
164    /// # Notes
165    ///
166    /// This method is not intended to be called in analyses but rather in writing methods
167    /// that have `mpi`-feature-gated versions. Most users should just index into a [`Dataset`]
168    /// as if it were any other [`Vec`]:
169    ///
170    /// ```ignore
171    /// let ds: Dataset = Dataset::new(events);
172    /// let event_0 = ds[0];
173    /// ```
174    #[cfg(feature = "mpi")]
175    pub fn index_mpi(&self, index: usize, world: &SimpleCommunicator) -> &Event {
176        let (_, displs) = world.get_counts_displs(self.n_events());
177        let (owning_rank, local_index) = Dataset::get_rank_index(index, &displs, world);
178        let mut serialized_event_buffer_len: usize = 0;
179        let mut serialized_event_buffer: Vec<u8> = Vec::default();
180        let config = bincode::config::standard();
181        if world.rank() == owning_rank {
182            let event = self.index_local(local_index);
183            serialized_event_buffer = bincode::serde::encode_to_vec(event, config).unwrap();
184            serialized_event_buffer_len = serialized_event_buffer.len();
185        }
186        world
187            .process_at_rank(owning_rank)
188            .broadcast_into(&mut serialized_event_buffer_len);
189        if world.rank() != owning_rank {
190            serialized_event_buffer = vec![0; serialized_event_buffer_len];
191        }
192        world
193            .process_at_rank(owning_rank)
194            .broadcast_into(&mut serialized_event_buffer);
195        let (event, _): (Event, usize) =
196            bincode::serde::decode_from_slice(&serialized_event_buffer[..], config).unwrap();
197        Box::leak(Box::new(event))
198    }
199}
200
201impl Index<usize> for Dataset {
202    type Output = Event;
203
204    fn index(&self, index: usize) -> &Self::Output {
205        #[cfg(feature = "mpi")]
206        {
207            if let Some(world) = crate::mpi::get_world() {
208                return self.index_mpi(index, &world);
209            }
210        }
211        self.index_local(index)
212    }
213}
214
215impl Dataset {
216    /// Create a new [`Dataset`] from a list of [`Event`]s (non-MPI version).
217    ///
218    /// # Notes
219    ///
220    /// This method is not intended to be called in analyses but rather in writing methods
221    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::new`] instead.
222    pub fn new_local(events: Vec<Arc<Event>>) -> Self {
223        Dataset { events }
224    }
225
226    /// Create a new [`Dataset`] from a list of [`Event`]s (MPI-compatible version).
227    ///
228    /// # Notes
229    ///
230    /// This method is not intended to be called in analyses but rather in writing methods
231    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::new`] instead.
232    #[cfg(feature = "mpi")]
233    pub fn new_mpi(events: Vec<Arc<Event>>, world: &SimpleCommunicator) -> Self {
234        Dataset {
235            events: Dataset::partition(events, world)[world.rank() as usize].clone(),
236        }
237    }
238
239    /// Create a new [`Dataset`] from a list of [`Event`]s.
240    ///
241    /// This method is prefered for external use because it contains proper MPI construction
242    /// methods. Constructing a [`Dataset`] manually is possible, but may cause issues when
243    /// interfacing with MPI and should be avoided unless you know what you are doing.
244    pub fn new(events: Vec<Arc<Event>>) -> Self {
245        #[cfg(feature = "mpi")]
246        {
247            if let Some(world) = crate::mpi::get_world() {
248                return Dataset::new_mpi(events, &world);
249            }
250        }
251        Dataset::new_local(events)
252    }
253
254    /// The number of [`Event`]s in the [`Dataset`] (non-MPI version).
255    ///
256    /// # Notes
257    ///
258    /// This method is not intended to be called in analyses but rather in writing methods
259    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::n_events`] instead.
260    pub fn n_events_local(&self) -> usize {
261        self.events.len()
262    }
263
264    /// The number of [`Event`]s in the [`Dataset`] (MPI-compatible version).
265    ///
266    /// # Notes
267    ///
268    /// This method is not intended to be called in analyses but rather in writing methods
269    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::n_events`] instead.
270    #[cfg(feature = "mpi")]
271    pub fn n_events_mpi(&self, world: &SimpleCommunicator) -> usize {
272        let mut n_events_partitioned: Vec<usize> = vec![0; world.size() as usize];
273        let n_events_local = self.n_events_local();
274        world.all_gather_into(&n_events_local, &mut n_events_partitioned);
275        n_events_partitioned.iter().sum()
276    }
277
278    /// The number of [`Event`]s in the [`Dataset`].
279    pub fn n_events(&self) -> usize {
280        #[cfg(feature = "mpi")]
281        {
282            if let Some(world) = crate::mpi::get_world() {
283                return self.n_events_mpi(&world);
284            }
285        }
286        self.n_events_local()
287    }
288}
289
290impl Dataset {
291    /// Extract a list of weights over each [`Event`] in the [`Dataset`] (non-MPI version).
292    ///
293    /// # Notes
294    ///
295    /// This method is not intended to be called in analyses but rather in writing methods
296    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::weights`] instead.
297    pub fn weights_local(&self) -> Vec<Float> {
298        #[cfg(feature = "rayon")]
299        return self.events.par_iter().map(|e| e.weight).collect();
300        #[cfg(not(feature = "rayon"))]
301        return self.events.iter().map(|e| e.weight).collect();
302    }
303
304    /// Extract a list of weights over each [`Event`] in the [`Dataset`] (MPI-compatible version).
305    ///
306    /// # Notes
307    ///
308    /// This method is not intended to be called in analyses but rather in writing methods
309    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::weights`] instead.
310    #[cfg(feature = "mpi")]
311    pub fn weights_mpi(&self, world: &SimpleCommunicator) -> Vec<Float> {
312        let local_weights = self.weights_local();
313        let n_events = self.n_events();
314        let mut buffer: Vec<Float> = vec![0.0; n_events];
315        let (counts, displs) = world.get_counts_displs(n_events);
316        {
317            let mut partitioned_buffer = PartitionMut::new(&mut buffer, counts, displs);
318            world.all_gather_varcount_into(&local_weights, &mut partitioned_buffer);
319        }
320        buffer
321    }
322
323    /// Extract a list of weights over each [`Event`] in the [`Dataset`].
324    pub fn weights(&self) -> Vec<Float> {
325        #[cfg(feature = "mpi")]
326        {
327            if let Some(world) = crate::mpi::get_world() {
328                return self.weights_mpi(&world);
329            }
330        }
331        self.weights_local()
332    }
333
334    /// Returns the sum of the weights for each [`Event`] in the [`Dataset`] (non-MPI version).
335    ///
336    /// # Notes
337    ///
338    /// This method is not intended to be called in analyses but rather in writing methods
339    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::n_events_weighted`] instead.
340    pub fn n_events_weighted_local(&self) -> Float {
341        #[cfg(feature = "rayon")]
342        return self
343            .events
344            .par_iter()
345            .map(|e| e.weight)
346            .parallel_sum_with_accumulator::<Klein<Float>>();
347        #[cfg(not(feature = "rayon"))]
348        return self.events.iter().map(|e| e.weight).sum();
349    }
350    /// Returns the sum of the weights for each [`Event`] in the [`Dataset`] (MPI-compatible version).
351    ///
352    /// # Notes
353    ///
354    /// This method is not intended to be called in analyses but rather in writing methods
355    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::n_events_weighted`] instead.
356    #[cfg(feature = "mpi")]
357    pub fn n_events_weighted_mpi(&self, world: &SimpleCommunicator) -> Float {
358        let mut n_events_weighted_partitioned: Vec<Float> = vec![0.0; world.size() as usize];
359        let n_events_weighted_local = self.n_events_weighted_local();
360        world.all_gather_into(&n_events_weighted_local, &mut n_events_weighted_partitioned);
361        #[cfg(feature = "rayon")]
362        return n_events_weighted_partitioned
363            .into_par_iter()
364            .parallel_sum_with_accumulator::<Klein<Float>>();
365        #[cfg(not(feature = "rayon"))]
366        return n_events_weighted_partitioned.iter().sum();
367    }
368
369    /// Returns the sum of the weights for each [`Event`] in the [`Dataset`].
370    pub fn n_events_weighted(&self) -> Float {
371        #[cfg(feature = "mpi")]
372        {
373            if let Some(world) = crate::mpi::get_world() {
374                return self.n_events_weighted_mpi(&world);
375            }
376        }
377        self.n_events_weighted_local()
378    }
379
380    /// Generate a new dataset with the same length by resampling the events in the original datset
381    /// with replacement. This can be used to perform error analysis via the bootstrap method. (non-MPI version).
382    ///
383    /// # Notes
384    ///
385    /// This method is not intended to be called in analyses but rather in writing methods
386    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::bootstrap`] instead.
387    pub fn bootstrap_local(&self, seed: usize) -> Arc<Dataset> {
388        let mut rng = fastrand::Rng::with_seed(seed as u64);
389        let mut indices: Vec<usize> = (0..self.n_events())
390            .map(|_| rng.usize(0..self.n_events()))
391            .collect::<Vec<usize>>();
392        indices.sort();
393        #[cfg(feature = "rayon")]
394        let bootstrapped_events: Vec<Arc<Event>> = indices
395            .into_par_iter()
396            .map(|idx| self.events[idx].clone())
397            .collect();
398        #[cfg(not(feature = "rayon"))]
399        let bootstrapped_events: Vec<Arc<Event>> = indices
400            .into_iter()
401            .map(|idx| self.events[idx].clone())
402            .collect();
403        Arc::new(Dataset {
404            events: bootstrapped_events,
405        })
406    }
407
408    /// Generate a new dataset with the same length by resampling the events in the original datset
409    /// with replacement. This can be used to perform error analysis via the bootstrap method. (MPI-compatible version).
410    ///
411    /// # Notes
412    ///
413    /// This method is not intended to be called in analyses but rather in writing methods
414    /// that have `mpi`-feature-gated versions. Most users should just call [`Dataset::bootstrap`] instead.
415    #[cfg(feature = "mpi")]
416    pub fn bootstrap_mpi(&self, seed: usize, world: &SimpleCommunicator) -> Arc<Dataset> {
417        let n_events = self.n_events();
418        let mut indices: Vec<usize> = vec![0; n_events];
419        if world.is_root() {
420            let mut rng = fastrand::Rng::with_seed(seed as u64);
421            indices = (0..n_events)
422                .map(|_| rng.usize(0..n_events))
423                .collect::<Vec<usize>>();
424            indices.sort();
425        }
426        world.process_at_root().broadcast_into(&mut indices);
427        #[cfg(feature = "rayon")]
428        let bootstrapped_events: Vec<Arc<Event>> = indices
429            .into_par_iter()
430            .map(|idx| self.events[idx].clone())
431            .collect();
432        #[cfg(not(feature = "rayon"))]
433        let bootstrapped_events: Vec<Arc<Event>> = indices
434            .into_iter()
435            .map(|idx| self.events[idx].clone())
436            .collect();
437        Arc::new(Dataset {
438            events: bootstrapped_events,
439        })
440    }
441
442    /// Generate a new dataset with the same length by resampling the events in the original datset
443    /// with replacement. This can be used to perform error analysis via the bootstrap method.
444    pub fn bootstrap(&self, seed: usize) -> Arc<Dataset> {
445        #[cfg(feature = "mpi")]
446        {
447            if let Some(world) = crate::mpi::get_world() {
448                return self.bootstrap_mpi(seed, &world);
449            }
450        }
451        self.bootstrap_local(seed)
452    }
453
454    /// Filter the [`Dataset`] by a given `predicate`, selecting events for which the predicate
455    /// returns `true`.
456    pub fn filter<P>(&self, predicate: P) -> Arc<Dataset>
457    where
458        P: Fn(&Event) -> bool + Send + Sync,
459    {
460        #[cfg(feature = "rayon")]
461        let filtered_events = self
462            .events
463            .par_iter()
464            .filter(|e| predicate(e))
465            .cloned()
466            .collect();
467        #[cfg(not(feature = "rayon"))]
468        let filtered_events = self
469            .events
470            .iter()
471            .filter(|e| predicate(e))
472            .cloned()
473            .collect();
474        Arc::new(Dataset {
475            events: filtered_events,
476        })
477    }
478
479    /// Bin a [`Dataset`] by the value of the given [`Variable`] into a number of `bins` within the
480    /// given `range`.
481    pub fn bin_by<V>(&self, variable: V, bins: usize, range: (Float, Float)) -> BinnedDataset
482    where
483        V: Variable,
484    {
485        let bin_width = (range.1 - range.0) / bins as Float;
486        let bin_edges = get_bin_edges(bins, range);
487        #[cfg(feature = "rayon")]
488        let evaluated: Vec<(usize, &Arc<Event>)> = self
489            .events
490            .par_iter()
491            .filter_map(|event| {
492                let value = variable.value(event.as_ref());
493                if value >= range.0 && value < range.1 {
494                    let bin_index = ((value - range.0) / bin_width) as usize;
495                    let bin_index = bin_index.min(bins - 1);
496                    Some((bin_index, event))
497                } else {
498                    None
499                }
500            })
501            .collect();
502        #[cfg(not(feature = "rayon"))]
503        let evaluated: Vec<(usize, &Arc<Event>)> = self
504            .events
505            .iter()
506            .filter_map(|event| {
507                let value = variable.value(event.as_ref());
508                if value >= range.0 && value < range.1 {
509                    let bin_index = ((value - range.0) / bin_width) as usize;
510                    let bin_index = bin_index.min(bins - 1);
511                    Some((bin_index, event))
512                } else {
513                    None
514                }
515            })
516            .collect();
517        let mut binned_events: Vec<Vec<Arc<Event>>> = vec![Vec::default(); bins];
518        for (bin_index, event) in evaluated {
519            binned_events[bin_index].push(event.clone());
520        }
521        BinnedDataset {
522            #[cfg(feature = "rayon")]
523            datasets: binned_events
524                .into_par_iter()
525                .map(|events| Arc::new(Dataset { events }))
526                .collect(),
527            #[cfg(not(feature = "rayon"))]
528            datasets: binned_events
529                .into_iter()
530                .map(|events| Arc::new(Dataset { events }))
531                .collect(),
532            edges: bin_edges,
533        }
534    }
535
536    /// Boost all the four-momenta in all [`Event`]s to the rest frame of the given set of
537    /// four-momenta by indices.
538    pub fn boost_to_rest_frame_of<T: AsRef<[usize]> + Sync>(&self, indices: T) -> Arc<Dataset> {
539        #[cfg(feature = "rayon")]
540        {
541            Arc::new(Dataset {
542                events: self
543                    .events
544                    .par_iter()
545                    .map(|event| Arc::new(event.boost_to_rest_frame_of(indices.as_ref())))
546                    .collect(),
547            })
548        }
549        #[cfg(not(feature = "rayon"))]
550        {
551            Arc::new(Dataset {
552                events: self
553                    .events
554                    .iter()
555                    .map(|event| Arc::new(event.boost_to_rest_frame_of(indices.as_ref())))
556                    .collect(),
557            })
558        }
559    }
560}
561
562impl_op_ex!(+ |a: &Dataset, b: &Dataset| ->  Dataset { Dataset { events: a.events.iter().chain(b.events.iter()).cloned().collect() }});
563
564fn batch_to_event(batch: &RecordBatch, row: usize) -> Event {
565    let mut p4s = Vec::new();
566    let mut aux = Vec::new();
567
568    let p4_count = batch
569        .schema()
570        .fields()
571        .iter()
572        .filter(|field| field.name().starts_with(P4_PREFIX))
573        .count()
574        / 4;
575    let aux_count = batch
576        .schema()
577        .fields()
578        .iter()
579        .filter(|field| field.name().starts_with(AUX_PREFIX))
580        .count()
581        / 3;
582
583    for i in 0..p4_count {
584        let e = batch
585            .column_by_name(&format!("{}{}_E", P4_PREFIX, i))
586            .unwrap()
587            .as_any()
588            .downcast_ref::<Float32Array>()
589            .unwrap()
590            .value(row) as Float;
591        let px = batch
592            .column_by_name(&format!("{}{}_Px", P4_PREFIX, i))
593            .unwrap()
594            .as_any()
595            .downcast_ref::<Float32Array>()
596            .unwrap()
597            .value(row) as Float;
598        let py = batch
599            .column_by_name(&format!("{}{}_Py", P4_PREFIX, i))
600            .unwrap()
601            .as_any()
602            .downcast_ref::<Float32Array>()
603            .unwrap()
604            .value(row) as Float;
605        let pz = batch
606            .column_by_name(&format!("{}{}_Pz", P4_PREFIX, i))
607            .unwrap()
608            .as_any()
609            .downcast_ref::<Float32Array>()
610            .unwrap()
611            .value(row) as Float;
612        p4s.push(Vec4::new(px, py, pz, e));
613    }
614
615    // TODO: insert empty vectors if not provided
616    for i in 0..aux_count {
617        let x = batch
618            .column_by_name(&format!("{}{}_x", AUX_PREFIX, i))
619            .unwrap()
620            .as_any()
621            .downcast_ref::<Float32Array>()
622            .unwrap()
623            .value(row) as Float;
624        let y = batch
625            .column_by_name(&format!("{}{}_y", AUX_PREFIX, i))
626            .unwrap()
627            .as_any()
628            .downcast_ref::<Float32Array>()
629            .unwrap()
630            .value(row) as Float;
631        let z = batch
632            .column_by_name(&format!("{}{}_z", AUX_PREFIX, i))
633            .unwrap()
634            .as_any()
635            .downcast_ref::<Float32Array>()
636            .unwrap()
637            .value(row) as Float;
638        aux.push(Vec3::new(x, y, z));
639    }
640
641    let weight = batch
642        .column(19)
643        .as_any()
644        .downcast_ref::<Float32Array>()
645        .unwrap()
646        .value(row) as Float;
647
648    Event { p4s, aux, weight }
649}
650
651/// Open a Parquet file and read the data into a [`Dataset`].
652pub fn open<T: AsRef<str>>(file_path: T) -> Result<Arc<Dataset>, LadduError> {
653    // TODO: make this read in directly to MPI ranks
654    let file_path = Path::new(&*shellexpand::full(file_path.as_ref())?).canonicalize()?;
655    let file = File::open(file_path)?;
656    let builder = ParquetRecordBatchReaderBuilder::try_new(file)?;
657    let reader = builder.build()?;
658    let batches: Vec<RecordBatch> = reader.collect::<Result<Vec<_>, _>>()?;
659
660    #[cfg(feature = "rayon")]
661    let events: Vec<Arc<Event>> = batches
662        .into_par_iter()
663        .flat_map(|batch| {
664            let num_rows = batch.num_rows();
665            let mut local_events = Vec::with_capacity(num_rows);
666
667            // Process each row in the batch
668            for row in 0..num_rows {
669                let event = batch_to_event(&batch, row);
670                local_events.push(Arc::new(event));
671            }
672            local_events
673        })
674        .collect();
675    #[cfg(not(feature = "rayon"))]
676    let events: Vec<Arc<Event>> = batches
677        .into_iter()
678        .flat_map(|batch| {
679            let num_rows = batch.num_rows();
680            let mut local_events = Vec::with_capacity(num_rows);
681
682            // Process each row in the batch
683            for row in 0..num_rows {
684                let event = batch_to_event(&batch, row);
685                local_events.push(Arc::new(event));
686            }
687            local_events
688        })
689        .collect();
690    Ok(Arc::new(Dataset::new(events)))
691}
692
693/// Open a Parquet file and read the data into a [`Dataset`]. This method boosts each event to the
694/// rest frame of the four-momenta at the given indices.
695pub fn open_boosted_to_rest_frame_of<T: AsRef<str>, I: AsRef<[usize]> + Sync>(
696    file_path: T,
697    indices: I,
698) -> Result<Arc<Dataset>, LadduError> {
699    // TODO: make this read in directly to MPI ranks
700    let file_path = Path::new(&*shellexpand::full(file_path.as_ref())?).canonicalize()?;
701    let file = File::open(file_path)?;
702    let builder = ParquetRecordBatchReaderBuilder::try_new(file)?;
703    let reader = builder.build()?;
704    let batches: Vec<RecordBatch> = reader.collect::<Result<Vec<_>, _>>()?;
705
706    #[cfg(feature = "rayon")]
707    let events: Vec<Arc<Event>> = batches
708        .into_par_iter()
709        .flat_map(|batch| {
710            let num_rows = batch.num_rows();
711            let mut local_events = Vec::with_capacity(num_rows);
712
713            // Process each row in the batch
714            for row in 0..num_rows {
715                let mut event = batch_to_event(&batch, row);
716                event = event.boost_to_rest_frame_of(indices.as_ref());
717                local_events.push(Arc::new(event));
718            }
719            local_events
720        })
721        .collect();
722    #[cfg(not(feature = "rayon"))]
723    let events: Vec<Arc<Event>> = batches
724        .into_iter()
725        .flat_map(|batch| {
726            let num_rows = batch.num_rows();
727            let mut local_events = Vec::with_capacity(num_rows);
728
729            // Process each row in the batch
730            for row in 0..num_rows {
731                let mut event = batch_to_event(&batch, row);
732                event = event.boost_to_rest_frame_of(indices.as_ref());
733                local_events.push(Arc::new(event));
734            }
735            local_events
736        })
737        .collect();
738    Ok(Arc::new(Dataset::new(events)))
739}
740
741/// A list of [`Dataset`]s formed by binning [`Event`]s by some [`Variable`].
742pub struct BinnedDataset {
743    datasets: Vec<Arc<Dataset>>,
744    edges: Vec<Float>,
745}
746
747impl Index<usize> for BinnedDataset {
748    type Output = Arc<Dataset>;
749
750    fn index(&self, index: usize) -> &Self::Output {
751        &self.datasets[index]
752    }
753}
754
755impl IndexMut<usize> for BinnedDataset {
756    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
757        &mut self.datasets[index]
758    }
759}
760
761impl Deref for BinnedDataset {
762    type Target = Vec<Arc<Dataset>>;
763
764    fn deref(&self) -> &Self::Target {
765        &self.datasets
766    }
767}
768
769impl DerefMut for BinnedDataset {
770    fn deref_mut(&mut self) -> &mut Self::Target {
771        &mut self.datasets
772    }
773}
774
775impl BinnedDataset {
776    /// The number of bins in the [`BinnedDataset`].
777    pub fn n_bins(&self) -> usize {
778        self.datasets.len()
779    }
780
781    /// Returns a list of the bin edges that were used to form the [`BinnedDataset`].
782    pub fn edges(&self) -> Vec<Float> {
783        self.edges.clone()
784    }
785
786    /// Returns the range that was used to form the [`BinnedDataset`].
787    pub fn range(&self) -> (Float, Float) {
788        (self.edges[0], self.edges[self.n_bins()])
789    }
790}
791
792#[cfg(test)]
793mod tests {
794    use super::*;
795    use approx::{assert_relative_eq, assert_relative_ne};
796    use serde::{Deserialize, Serialize};
797    #[test]
798    fn test_event_creation() {
799        let event = test_event();
800        assert_eq!(event.p4s.len(), 4);
801        assert_eq!(event.aux.len(), 1);
802        assert_relative_eq!(event.weight, 0.48)
803    }
804
805    #[test]
806    fn test_event_p4_sum() {
807        let event = test_event();
808        let sum = event.get_p4_sum([2, 3]);
809        assert_relative_eq!(sum.px(), event.p4s[2].px() + event.p4s[3].px());
810        assert_relative_eq!(sum.py(), event.p4s[2].py() + event.p4s[3].py());
811        assert_relative_eq!(sum.pz(), event.p4s[2].pz() + event.p4s[3].pz());
812        assert_relative_eq!(sum.e(), event.p4s[2].e() + event.p4s[3].e());
813    }
814
815    #[test]
816    fn test_event_boost() {
817        let event = test_event();
818        let event_boosted = event.boost_to_rest_frame_of([1, 2, 3]);
819        let p4_sum = event_boosted.get_p4_sum([1, 2, 3]);
820        assert_relative_eq!(p4_sum.px(), 0.0, epsilon = Float::EPSILON.sqrt());
821        assert_relative_eq!(p4_sum.py(), 0.0, epsilon = Float::EPSILON.sqrt());
822        assert_relative_eq!(p4_sum.pz(), 0.0, epsilon = Float::EPSILON.sqrt());
823    }
824
825    #[test]
826    fn test_dataset_size_check() {
827        let mut dataset = Dataset::default();
828        assert_eq!(dataset.n_events(), 0);
829        dataset.events.push(Arc::new(test_event()));
830        assert_eq!(dataset.n_events(), 1);
831    }
832
833    #[test]
834    fn test_dataset_sum() {
835        let dataset = test_dataset();
836        let dataset2 = Dataset::new(vec![Arc::new(Event {
837            p4s: test_event().p4s,
838            aux: test_event().aux,
839            weight: 0.52,
840        })]);
841        let dataset_sum = &dataset + &dataset2;
842        assert_eq!(dataset_sum[0].weight, dataset[0].weight);
843        assert_eq!(dataset_sum[1].weight, dataset2[0].weight);
844    }
845
846    #[test]
847    fn test_dataset_weights() {
848        let mut dataset = Dataset::default();
849        dataset.events.push(Arc::new(test_event()));
850        dataset.events.push(Arc::new(Event {
851            p4s: test_event().p4s,
852            aux: test_event().aux,
853            weight: 0.52,
854        }));
855        let weights = dataset.weights();
856        assert_eq!(weights.len(), 2);
857        assert_relative_eq!(weights[0], 0.48);
858        assert_relative_eq!(weights[1], 0.52);
859        assert_relative_eq!(dataset.n_events_weighted(), 1.0);
860    }
861
862    #[test]
863    fn test_dataset_filtering() {
864        let mut dataset = test_dataset();
865        dataset.events.push(Arc::new(Event {
866            p4s: vec![
867                Vec3::new(0.0, 0.0, 5.0).with_mass(0.0),
868                Vec3::new(0.0, 0.0, 1.0).with_mass(1.0),
869            ],
870            aux: vec![],
871            weight: 1.0,
872        }));
873
874        let filtered = dataset.filter(|event| event.p4s.len() == 2);
875        assert_eq!(filtered.n_events(), 1);
876        assert_eq!(filtered[0].p4s.len(), 2);
877    }
878
879    #[test]
880    fn test_dataset_boost() {
881        let dataset = test_dataset();
882        let dataset_boosted = dataset.boost_to_rest_frame_of([1, 2, 3]);
883        let p4_sum = dataset_boosted[0].get_p4_sum([1, 2, 3]);
884        assert_relative_eq!(p4_sum.px(), 0.0, epsilon = Float::EPSILON.sqrt());
885        assert_relative_eq!(p4_sum.py(), 0.0, epsilon = Float::EPSILON.sqrt());
886        assert_relative_eq!(p4_sum.pz(), 0.0, epsilon = Float::EPSILON.sqrt());
887    }
888
889    #[test]
890    fn test_binned_dataset() {
891        let dataset = Dataset::new(vec![
892            Arc::new(Event {
893                p4s: vec![Vec3::new(0.0, 0.0, 1.0).with_mass(1.0)],
894                aux: vec![],
895                weight: 1.0,
896            }),
897            Arc::new(Event {
898                p4s: vec![Vec3::new(0.0, 0.0, 2.0).with_mass(2.0)],
899                aux: vec![],
900                weight: 2.0,
901            }),
902        ]);
903
904        #[derive(Clone, Serialize, Deserialize, Debug)]
905        struct BeamEnergy;
906        impl Display for BeamEnergy {
907            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
908                write!(f, "BeamEnergy")
909            }
910        }
911        #[typetag::serde]
912        impl Variable for BeamEnergy {
913            fn value(&self, event: &Event) -> Float {
914                event.p4s[0].e()
915            }
916        }
917        assert_eq!(BeamEnergy.to_string(), "BeamEnergy");
918
919        // Test binning by first particle energy
920        let binned = dataset.bin_by(BeamEnergy, 2, (0.0, 3.0));
921
922        assert_eq!(binned.n_bins(), 2);
923        assert_eq!(binned.edges().len(), 3);
924        assert_relative_eq!(binned.edges()[0], 0.0);
925        assert_relative_eq!(binned.edges()[2], 3.0);
926        assert_eq!(binned[0].n_events(), 1);
927        assert_relative_eq!(binned[0].n_events_weighted(), 1.0);
928        assert_eq!(binned[1].n_events(), 1);
929        assert_relative_eq!(binned[1].n_events_weighted(), 2.0);
930    }
931
932    #[test]
933    fn test_dataset_bootstrap() {
934        let mut dataset = test_dataset();
935        dataset.events.push(Arc::new(Event {
936            p4s: test_event().p4s.clone(),
937            aux: test_event().aux.clone(),
938            weight: 1.0,
939        }));
940        assert_relative_ne!(dataset[0].weight, dataset[1].weight);
941
942        let bootstrapped = dataset.bootstrap(43);
943        assert_eq!(bootstrapped.n_events(), dataset.n_events());
944        assert_relative_eq!(bootstrapped[0].weight, bootstrapped[1].weight);
945
946        // Test empty dataset bootstrap
947        let empty_dataset = Dataset::default();
948        let empty_bootstrap = empty_dataset.bootstrap(43);
949        assert_eq!(empty_bootstrap.n_events(), 0);
950    }
951
952    #[test]
953    fn test_event_display() {
954        let event = test_event();
955        let display_string = format!("{}", event);
956        assert_eq!(
957            display_string,
958            "Event:\n  p4s:\n    [e = 8.74700; p = (0.00000, 0.00000, 8.74700); m = 0.00000]\n    [e = 1.10334; p = (0.11900, 0.37400, 0.22200); m = 1.00700]\n    [e = 3.13671; p = (-0.11200, 0.29300, 3.08100); m = 0.49800]\n    [e = 5.50925; p = (-0.00700, -0.66700, 5.44600); m = 0.49800]\n  eps:\n    [0.385, 0.022, 0]\n  weight:\n    0.48\n"
959        );
960    }
961}