1 unstable release

0.1.0 Apr 24, 2024

#128 in #reader-writer


Used in 4 crates (via hepmc2)

GPL-3.0-or-later

5KB
87 lines

hepmc2

Read and write event files in the hepmc2 format, also known as IO_GenEvent.

Caveats

This crate is inspired by the code for the ReaderAsciiHepMC2 in the HepMC3 library, version 3.2.0. The aim is to be fully compatible, but be aware that the current tests are not exhaustive.

Example

// Read events from `events_in.hepmc2` and write them to `events_out.hepmc2`
use hepmc2::{Reader, Writer};

use std::io::BufReader;
use std::fs::File;

let input = BufReader::new(File::open("events_in.hepmc2")?);
let in_events = Reader::from(input);

let output = File::create("events_out.hepmc2")?;
let mut writer = Writer::try_from(output)?;

for event in in_events {
   let event = event?;
   println!("Current cross section: {}",  event.xs);
   writer.write(&event)?
}
writer.finish()?;

Async API

By default this crate enables the sync feature which exposes a sync API. You can however switch to using a tokio-backed async API by disabling the sync feature and enabling the tokio feature.

Either run the following in the root of your crate:

cargo add hepmc2 --no-default-features -F tokio

or make sure a line like the following is present in your Cargo.toml:

hepmc2 = { version = "0.6.0", default-features = false, features = ["tokio"] }

The async API is exactly the same as the sync one but IO operations will return futures that you will, as usual, need to call await on.

Example

// Read events from `events_in.hepmc2` and write them to `events_out.hepmc2`
use hepmc2::{Reader, Writer};

use tokio::io::BufReader;
use tokio::fs::File;

let input = BufReader::new(File::open("events_in.hepmc2").await?);
let mut in_events = Reader::from(input);

let output = File::create("events_out.hepmc2").await?;
let mut writer = Writer::try_from(output).await?;

while let Some(event) = in_events.next().await {
   let event = event?;
   println!("Current cross section: {}",  event.xs);
   writer.write(&event).await?
}
writer.finish().await?;

License: GPL-3.0-or-later

Dependencies

~245–700KB
~17K SLoC