Expand description

This crate aims to provide a way to open data saved in ROOT file format and particularly Tree and Branch inside Tree. This crate is in fact a port of groot written in Go and uproot written in Python.

Example: Iter over a branch tree containing i32 values

use oxyroot::RootFile;
let s = "examples/from_uproot/data/HZZ.root";
let tree = RootFile::open(s).expect("Can not open file").get_tree("events").unwrap();
let NJet = tree.branch("NJet").unwrap().as_iter::<i32>();
NJet.for_each(|v| println!("v = {v}"));

Example: Iter over a branch tree containing Vec<i32> (aka std::vector<int32_t>) values

use oxyroot::RootFile;
let s = "tests/stl_containers/stl_containers.root";
let tree = RootFile::open(s).expect("Can not open file").get_tree("tree").unwrap();
let vector_int32 = tree.branch("vector_int32")
                   .unwrap().as_iter::<Vec<i32>>()
                   .collect::<Vec<_>>();
assert_eq!(
    vector_int32,
    [
        vec![1],
        vec![1, 2],
        vec![1, 2, 3],
        vec![1, 2, 3, 4],
        vec![1, 2, 3, 4, 5]
    ]
);

Which types can be read from a branch?

Primitives and C++ STL standards

oxyroot can iterate over branch which contains :

  • primitive types like i32, f64, bool…
  • String (from TString, char* or std::string)
  • Vec (from std::vector or array)
  • HashMap
C++Rust
std::stringString
std::vectorVec
std::mapHashMap
std::setHashSet
T*Slice<T>
T[N]array
TStringString

Examples can be found in tests.

Structures

Structure serialized in Branch can be also read but the parsing code has to be written :

struct sd_t {
         Int_t a;
         Int_t b;
};

sd_t sd;
tree->Branch("v_i",&sd, 3200000, 0);

The sd struct can be read with a code like this :

use oxyroot::RBuffer;
use oxyroot::RootFile;
struct Sd {
    a: i32,
    b: i32,
};

let parse = |r: &mut RBuffer| Sd {
    a: r.read_i32().unwrap(),
    b: r.read_i32().unwrap(),
};


let s = "tests_data/doc/struct_sd.root";
let tree = RootFile::open(s).expect("Can not open file").get_tree("T").unwrap();

// branch v_i contains v_i which will be zipped.
let mut b = tree.branch("v_i").unwrap().get_basket(parse);

for i in -10..10 {
    let sd = b.next().unwrap();
}

Macros

Structs

Helper struct to parse data read in file

Rust equivalent of TFile.

Represent a array of T* in C++

Rust equivalent of TTree

Enums

Rust equivalent of TBranch or TBranchElement (ie column) of a TTree

Traits

Trait that permits reading a type from an ROOT file.

Automatically implemented if Unmarshaler is implemented