hepmc2

Crates.iohepmc2
lib.rshepmc2
version0.7.0
sourcesrc
created_at2021-02-11 10:33:56.53869
updated_at2024-04-24 19:15:22.938199
descriptionRead and write files in the HepMC2 format
homepage
repositoryhttps://github.com/a-maier/hepmc2
max_upload_size
id353733
size52,741
(a-maier)

documentation

README

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

Commit count: 62

cargo fmt