Skip to content

Commit

Permalink
handle [var][fix] style doubly jagged branch (#188)
Browse files Browse the repository at this point in the history
* handle [var][fix] style doubly jagged branch

* fix tests for 1.6
  • Loading branch information
Moelf authored Oct 10, 2022
1 parent 232e85b commit 8b16c8f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnROOT"
uuid = "3cd96dde-e98d-4713-81e9-a4a1b0235ce9"
authors = ["Tamas Gal", "Jerry Ling", "Johannes Schumann", "Nick Amin"]
version = "0.8.15"
version = "0.8.16"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
8 changes: 8 additions & 0 deletions src/custom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ Base.iterate(x::FixLenVector) = iterate(x.vec)
Base.iterate(x::FixLenVector, n) = iterate(x.vec, n)
Base.getindex(x::FixLenVector, n) = getindex(x.vec, n)

#N.B this is a hack, we deal with ntoh in reinterpret step
Base.ntoh(x::FixLenVector) = x
const VorView = Union{Vector{UInt8}, SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int64}}, true}}

function Base.reinterpret(::Type{Vector{FixLenVector{N, T}}}, data::Vector{UInt8}) where {N,T}
vs = reinterpret(T, data)
@. vs = ntoh(vs)
return FixLenVector.(reinterpret(SVector{N, T}, vs))
end

function Base.reinterpret(::Type{FixLenVector{N, T}}, v::VorView) where {N, T}
vs = reinterpret(T, v)
@. vs = ntoh(vs)
Expand Down
27 changes: 22 additions & 5 deletions src/root.jl
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,31 @@ function auto_T_JaggT(f::ROOTFile, branch; customstructs::Dict{String, Type})
end
end
else
# since no classname were found, we now try to determine
# type based on leaf information
_type, _jaggtype = leaf_jaggtype(leaf, _jaggtype)
end
return _type, _jaggtype
end

function leaf_jaggtype(leaf, _jaggtype)
_type = primitivetype(leaf)
if leaf.fLen > 1 # treat NTuple as Nojagg since size is static
_type = FixLenVector{Int(leaf.fLen), _type}
return _type, Nojagg
leafLen = leaf.fLen
if leafLen > 1 # treat NTuple as Nojagg since size is static
_fTitle = replace(leaf.fTitle, "[$(leafLen)]" => "")
# looking for more `[var]`
m = match(r"\[\D+\]", _fTitle)
_type = FixLenVector{Int(leafLen), _type}
if isnothing(m)
return _type, Nojagg
else
#FIXME this only handles [var][fix] case
return Vector{_type}, Nooffsetjagg
end
end
_type = _jaggtype === Nojagg ? _type : Vector{_type}
end
return _type, _jaggtype

return _type, _jaggtype
end


Expand Down
47 changes: 41 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,9 @@ end
@test v2[2].int32_array == data[3].int32_array
end

@testset "Doubly jagged branches" begin
rootfile = ROOTFile(joinpath(SAMPLES_DIR, "tree_with_doubly_jagged.root"))
@testset "Doubly jagged [var][var] branches" begin
# this is vector<vector<blah>>
rootfile = UnROOT.samplefile("tree_with_doubly_jagged.root")
vvi = [[[2], [3, 5]], [[7, 9, 11], [13]], [[17], [19], []], [], [[]]]
vvf = [[[2.5], [3.5, 5.5]], [[7.5, 9.5, 11.5], [13.5]], [[17.5], [19.5], []], [], [[]]]
@test UnROOT.array(rootfile, "t1/bi") == vvi
Expand All @@ -331,6 +332,33 @@ end
close(rootfile)
end

@testset "Doubly jagged [var][fix] branches" begin
# issue #187
# this is vector<Int[N]>
f = UnROOT.samplefile("tree_with_varfix_doubly_jagged.root")
tree = LazyTree(f, "outtree")
@test tree.nparticles == [4,3,2]
@test length.(tree.P) == [4,3,2]
@test eltype(tree.P[1]) <: AbstractVector
# also compared to uproot
@test tree[1].P == [
[0.9411764705882353, 0.8888888888888888, 0.8421052631578947, 0.8],
[1.0, 0.9285714285714286, 0.8666666666666667, 0.8125],
[1.1111111111111112, 1.0, 0.9090909090909091, 0.8333333333333334],
[1.4, 1.1666666666666667, 1.0, 0.875]
]
@test tree[3].P == [
[0.8222222222222222,
0.8043478260869565,
0.7872340425531915,
0.7708333333333334],
[0.8292682926829268,
0.8095238095238095,
0.7906976744186046,
0.7727272727272727]
]
end

@testset "NanoAOD" begin
rootfile = ROOTFile(joinpath(SAMPLES_DIR, "NanoAODv5_sample.root"))
event = UnROOT.array(rootfile, "Events/event")
Expand Down Expand Up @@ -641,8 +669,11 @@ nthreads == 1 && @warn "Running on a single thread. Please re-run the test suite


if get(ENV, "CI", "false") == "true"
# Make sure CI runs with more than 1 thread
@test Threads.nthreads()>1 skip=(nthreads == 1)
if nthreads >= 1
@test Threads.nthreads()>1
else
@warn "CI wasn't run with multi thread"
end
end
nmus = zeros(Int, Threads.nthreads())
Threads.@threads for i in 1:length(t)
Expand Down Expand Up @@ -671,15 +702,19 @@ t = LazyTree(ROOTFile(joinpath(SAMPLES_DIR, "NanoAODv5_sample.root")), "Events",
Threads.@threads for evt in t
nmus[Threads.threadid()] += length(evt.Muon_pt)
end
@test count(>(0), nmus) > 1 skip = (nthreads==1)# test @threads is actually threading
if nthreads > 1
@test count(>(0), nmus) > 1# test @threads is actually threading
end
@test sum(nmus) == 878


nmus .= 0
Threads.@threads for evt in t
nmus[Threads.threadid()] += length(evt.Muon_pt)
end
@test count(>(0), nmus) > 1 skip = (nthreads==1)# test @threads is actually threading
if nthreads > 1
@test count(>(0), nmus) > 1
end
@test sum(nmus) == 878

nmus .= 0
Expand Down
27 changes: 27 additions & 0 deletions test/samples/tree_with_varfix_doubly_jagged.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "TFile.h"
#include "TTree.h"

int maketree(){
TFile f("tree_with_varfix_doubly_jagged.root", "RECREATE", "");
TTree tree = TTree("outtree", "outtree");
int nparticles{};
double P[100][4];
tree.Branch("nparticles", &nparticles, "nparticles/I");
tree.Branch("P", P, "P[nparticles][4]/D");
double counter1 = 1;
double counter2 = 1;
for (auto ev = 0; ev<3; ++ev){
nparticles = 4-ev;
for (auto i = nparticles; i>=0; --i){
counter1 += 3;
for (auto j = 0; j<=3; ++j){
P[i][j] = counter1 / (counter2);
counter2++;
}
}
tree.Fill();
}
f.Write();
f.Close();
return 0;
}
Binary file added test/samples/tree_with_varfix_doubly_jagged.root
Binary file not shown.

2 comments on commit 8b16c8f

@Moelf
Copy link
Member Author

@Moelf Moelf commented on 8b16c8f Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/69877

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.16 -m "<description of version>" 8b16c8fa184266fdb566b58302d8132cd669751d
git push origin v0.8.16

Please sign in to comment.