#branch #root #tree #tree-root #read #derive #oxyroot

oxyroot_derive

为oxyroot提供的派生宏

9个版本

0.1.24 2024年4月24日
0.1.23 2024年4月21日

#14 in #tree-root


用于 oxyroot

MIT/Apache

36KB
669

oxyroot

Crates.io Documentation

尝试在图书馆中读取和写入粒子物理中常用的.root二进制文件

命令行工具

灵感

为了制作这个库

  • groot中汲取了大量灵感,用于读取root文件,甚至代码组织
  • uproot中汲取灵感,提供分支接口(用于读取篮子缓冲区)

限制

目前

  • 只能写入未压缩的文件

另请参阅

另一个root读取器的Rust实现是 root-io

入门

示例:迭代包含i32值的分支树

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

示例:在分支中写入i32值

use oxyroot::{RootFile, WriterTree};
let s = "/tmp/simple.root";
let mut file = RootFile::create(s).expect("Can not create file");
let mut tree = WriterTree::new("mytree");
let it = (0..15);
tree.new_branch("it", it);
tree.write( & mut file).expect("Can not write tree");
file.close().expect("Can not close file");

示例:迭代包含Vec<i32>(即std::vector<int32_t>)值的分支

use oxyroot::RootFile;
let s = "tests/stl_containers/stl_containers.root";
let tree = RootFile::open(s).unwrap().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]
    ]
);

示例:使用ReadFromTree迭代多个分支。

如果您有一个包含多个分支的root文件,您可以使用ReadFromTree一次读取它们。要从分支xy读取Point

use oxyroot::ReadFromTree;

#[derive(ReadFromTree)]
struct Point {
    // will read from branch "x"  
    x: f64,
    // will read from branch "y"
    y: f64,
}

let s = "tests/point/point.root";
let tree = RootFile::open(s).unwrap().get_tree("tree").unwrap();
let points = Point::from_tree(tree).unwrap();

for point in points {
println!("x = {}, y = {}", point.x, point.y);
}

示例:使用WriteToTree写入多个分支。

use oxyroot::ReadFromTree;

#[derive(WriteToTree)]
struct Point {
    // will write to branch "x"  
    x: f64,
    // will write to branch "y"
    y: f64,
}

let s = "tests/point/point.root";
let mut f = RootFile::create(s).unwrap();
let mut tree = WriterTree::new("tree");

let points = vec![Point { x: 1.0, y: 2.0 }, Point { x: 3.0, y: 4.0 }];

Test::to_tree(points.into_iter(), & mut tree).unwrap();

tree.write( & mut f).unwrap();
f.close().unwrap();

功能

oxyroot使用flate2解压缩zlib压缩数据。默认后端是纯Rust crate的miniz_oxide
如果您想要最大性能,可以使用zlib-ng C库

[dependencies]
oxyroot = { version = "0.1", features = ["zlib-ng"] }

依赖关系

~0.6–1MB
~24K SLoC