11 releases (6 breaking)
| 0.7.0 | Apr 24, 2024 |
|---|---|
| 0.6.0 | Sep 28, 2023 |
| 0.5.1 | Nov 3, 2021 |
| 0.4.1 | Mar 1, 2021 |
#815 in Science
714 downloads per month
Used in 4 crates
47KB
986 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
~1.2–2.8MB
~51K SLoC