Skip to content

Commit

Permalink
add more basic types (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminnj authored Sep 17, 2021
1 parent 635ed0e commit ac66158
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ mutable struct LazyBranch{T,J,B} <: AbstractVector{T}

function LazyBranch(f::ROOTFile, b::Union{TBranch,TBranchElement})
T, J = auto_T_JaggT(f, b; customstructs=f.customstructs)
T = (T === Vector{Bool} ? BitVector : T)
_buffer = J === Nojagg ? T[] : VectorOfVectors(T(), Int32[1])
return new{T,J,typeof(_buffer)}(f, b, length(b),
b.fBasketEntry,
Expand Down
14 changes: 9 additions & 5 deletions src/root.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ end
function Base.getindex(f::ROOTFile, s::AbstractString)
S = _getindex(f, s)
if S isa Union{TBranch, TBranchElement}
try # if we can't construct LazyBranch, just give up (maybe due to custom class)
# try # if we can't construct LazyBranch, just give up (maybe due to custom class)
return LazyBranch(f, S)
catch
@warn "Can't automatically create LazyBranch for branch $s. Returning a branch object"
end
# catch
# @warn "Can't automatically create LazyBranch for branch $s. Returning a branch object"
# end
end
S
end
Expand Down Expand Up @@ -364,9 +364,13 @@ function auto_T_JaggT(f::ROOTFile, branch; customstructs::Dict{String, Type})
elseif elname == "unsigned int"
UInt32
elseif elname == "unsigned char"
Char
UInt8
elseif elname == "unsigned short"
UInt16
elseif elname == "unsigned long"
UInt64
elseif elname == "long64"
Int64
elseif elname == "ulong64"
UInt64
else
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,9 @@ end
f = UnROOT.samplefile("issue11_tdirectory.root")
@test sum(f["Data/mytree/Particle0_E"]) 1012.0
end

@testset "Basic C++ types" begin
f = UnROOT.samplefile("tree_basictypes.root")
onesrow = LazyTree(f,"t")[2] |> values .|> first .|> Int
@test all(onesrow .== 1)
end
57 changes: 57 additions & 0 deletions test/samples/tree_basictypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import ROOT as r

# from https://en.cppreference.com/w/cpp/language/types
typenames = [
# char
"char", "unsigned char",
# bool
"bool",
# short
"short", "short int", "signed short", "signed short int", "unsigned short", "unsigned short int",
# int
"int", "signed", "signed int", "unsigned", "unsigned int",
# long
"long", "long int", "long int", "signed long", "signed long int", "unsigned long", "unsigned long int",
# long long
"long long", "long long int", "signed long long", "signed long long int", "unsigned long long", "unsigned long long int",
# float/double
"float", "double",
]

print(len(typenames), typenames)

roottypestrs = set()
for typename in typenames:
obj = r.vector(typename)
# print(typename, obj, obj())
roottypestr = str(obj).split("'")[1].split("<")[1].split(">")[0]
roottypestrs.add(roottypestr)

# since many of the above map to the same "root type", `roottypestrs` ends up being

# {'long', 'Long64_t', 'unsigned char', 'short', 'unsigned long', 'long double',
# 'char', 'unsigned short', 'ULong64_t', 'float', 'bool', 'double',
# 'int', 'unsigned int'}

print(len(roottypestrs), roottypestrs)

import ROOT as r
f = r.TFile("tree_basictypes.root", "recreate")
t = r.TTree("t", "")

d_objs = dict()
for typename in roottypestrs:
nicename = typename.lower().replace("_t","").replace(" ","")
obj = r.vector(typename)()
t.Branch(nicename, obj)
d_objs[typename] = obj

for ievt in range(3):
for typename, obj in d_objs.items():
obj.clear()
for _ in range(ievt):
obj.push_back(1)
t.Fill()

t.Write()
f.Close()
Binary file added test/samples/tree_basictypes.root
Binary file not shown.

0 comments on commit ac66158

Please sign in to comment.