1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*!
This crate aims to provide a way to open and write data in [ROOT](https://root.cern.ch/) file format
and particularly `Tree` and `Branch` inside `Tree`.
This crate is in fact a port of [groot](https://pkg.go.dev/go-hep.org/x/hep/groot) written
in Go and [uproot](https://github.com/scikit-hep/uproot5) written in Python.
To read from a branch and write to a branch, the API is iterator based :
- by branch:
- [`as_iter`](enum.Branch.html#method.as_iter) to read from a branch. The type to read is provided as a type parameter.
This type has to implement `Unmarshaler` trait.
- [`new_branch`](type.WriterTree.html#method.new_branch) from [`WriterTree`](type.WriterTree.html) to write to a branch. The method
[`write`](type.WriterTree.html#method.write) from [`WriterTree`](crate::WriterTree) is used
to write the data to the file by exhausting provided iterators. The type to write has to implement
[`Marshaler`](crate::Marshaler) trait. The type is deduced from the iterator.
- by Tree, by creating struct from several branches, thanks to the [`ReadFromTree`](trait.ReadFromTree.html) and [`WriteToTree`](trait.WriteToTree.html) traits. These traits
can be automatically derived with the `derive` feature which provides macros [`ReadFromTree`](derive.ReadFromTree.html) and [`WriteToTree`](derive.WriteToTree.html).
# Example: Iter over a branch tree containing `i32` values
```rust
use oxyroot::RootFile;
let s = "examples/from_uproot/data/simple.root";
let tree = RootFile::open(s).expect("Can not open file").get_tree("tree").unwrap();
let one = tree.branch("one").unwrap().as_iter::<i32>().expect("wrong type");
one.for_each(|v| println!("v = {v}"));
```
# Example : Read custom struct from branches
```no_run
use oxyroot::{ReadFromTree, RootFile};
#[derive(ReadFromTree)]
struct MyStruct {
a: i32, // will be read from branch "a" as 32 bits integer
s: String, // will be read from branch "s" as String
}
let tree = RootFile::open("in.root").unwrap().get_tree("tree").unwrap();
MyStruct::from_tree(&tree).unwrap().map(|m: MyStruct | { /* do something with m */ });
```
More information and examples here : [`ReadFromTree`](derive.ReadFromTree.html)
# Example: Show branches from a tree
```rust
use oxyroot::RootFile;
let s = "examples/from_uproot/data/simple.root";
let tree = RootFile::open(s).expect("Can not open file").get_tree("tree").unwrap();
tree.show();
```
will display
```ignore
name | typename | interpretation
-------------------------------+-------------------------------+-------------------------------
one | int32_t | i32
two | float | f32
three | char* | String
```
# Example: Write i32 values in a branch
```rust
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");
```
# Example: Iter over a branch tree containing `Vec<i32>` (aka `std::vector<int32_t>`) values
```rust
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>>().expect("wrong type")
.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::string | [String](String) |
| std::vector | [Vec](Vec) |
| std::map | [HashMap](std::collections::HashMap) |
| std::set | [HashSet](std::collections::HashSet) |
| T* | [`Slice<T>`](Slice) |
| T\[N\] | [array] |
| TString | [String] |
Examples can be found in tests.
## Structures
Structure serialized in `Branch` can be also read but the parsing code has to be written :
```C++
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 :
```no_run
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();
}
```
# Which types can be written to a branch?
## Primitives and C++ STL standards
oxyroot can iterate over branch which contains :
- primitive types like i32, f64, bool...
- String (will appear as a char*)
- Vec (to std::vector)
- HashMap (not yet implemented)
*/
// #![deny(unused_imports)]
// Show which crate feature enables conditionally compiled APIs in documentation.
extern crate core;
pub use RootFile;
pub use Branch;
pub use ReaderTree;
pub use ReadFromTree;
pub use ReadFromTreeResult;
pub use WriteToTree;
pub use WriterTree;
// pub use rtree::tree::Tree;
pub use RBuffer;
pub use Unmarshaler;
pub use UnmarshalerInto;
pub use Marshaler;
pub use SizedSlice;
pub use Slice;
pub use Named;
pub use Object;
pub use Result;
/// Derive macro available if oxyroot is built with `features = ["derive"]`.
pub use ;
pub use BranchName;
pub use StateCallBack;