Crate hepmc2

source ·
Expand description

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. For examples, generate the async API documentation in the root of this project:

cargo doc --open --no-default-features -F tokio

§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?;

Re-exports§

  • pub use crate::event::Event;
  • pub use crate::reader::Reader;
  • pub use crate::writer::Writer;

Modules§