Skip to content

Commit 72ea382

Browse files
committed
Add unsigned integer support
1 parent 8eb8bf0 commit 72ea382

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/bootstrap.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ function unpack(io, tkey::TKey, refs::Dict{Int32, Any}, T::Type{TLeafG})
413413
T(;fields...)
414414
end
415415

416-
primitivetype(l::TLeafG) = Int64
416+
primitivetype(l::TLeafG) = l.fIsUnsigned ? UInt64 : Int64
417417

418418
# FIXME this should be generated and inherited from TLeaf
419419
Base.@kwdef struct TLeafO
@@ -518,7 +518,7 @@ function unpack(io, tkey::TKey, refs::Dict{Int32, Any}, T::Type{TLeafB})
518518
T(;fields...)
519519
end
520520

521-
primitivetype(l::TLeafB) = UInt8
521+
primitivetype(l::TLeafB) = l.fIsUnsigned ? UInt8 : Int8
522522
# FIXME this should be generated and inherited from TLeaf
523523
Base.@kwdef struct TLeafD
524524
# from TNamed

test/issues.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,17 @@ end
150150
df = LazyTree(UnROOT.samplefile("TLeafC_pr342.root"), "G4Sim")
151151
@test all(df.Process[1:10] .== ["Radioactivation", "msc", "eIoni", "Transportation", "ionIoni", "Radioactivation", "msc", "eIoni", "ionIoni", "Radioactivation"])
152152
end
153+
154+
@testset "Unsigned integer branches (TLeafB/S/I/L with fIsUnsigned)" begin
155+
f = UnROOT.samplefile("unsigned_integers.root")
156+
t = LazyTree(f, "tree")
157+
@test eltype(t.b_uint8) === UInt8
158+
@test eltype(t.b_uint16) === UInt16
159+
@test eltype(t.b_uint32) === UInt32
160+
@test eltype(t.b_uint64) === UInt64
161+
@test t.b_uint8 == UInt8[ 200, 255, 1]
162+
@test t.b_uint16 == UInt16[60000, 65535, 1]
163+
@test t.b_uint32 == UInt32[4000000000, 4294967295, 1]
164+
@test t.b_uint64 == UInt64[18000000000000000000, 18446744073709551615, 1]
165+
close(f)
166+
end

test/samples/unsigned_integers.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Generate a ROOT file with branches of all unsigned integer types.
3+
Used by UnROOT.jl tests to verify correct unsigned type reading.
4+
5+
Run with:
6+
python unsigned_integers.py
7+
or:
8+
root -l -q unsigned_integers.py
9+
"""
10+
from array import array
11+
import ROOT as r
12+
13+
f = r.TFile("unsigned_integers.root", "recreate")
14+
t = r.TTree("tree", "Tree with all unsigned integer types")
15+
16+
b_uint8 = array('B', [0])
17+
b_uint16 = array('H', [0])
18+
b_uint32 = array('I', [0])
19+
b_uint64 = array('Q', [0])
20+
21+
# type codes: b=UChar_t, s=UShort_t, i=UInt_t, l=ULong64_t
22+
t.Branch("b_uint8", b_uint8, "b_uint8/b")
23+
t.Branch("b_uint16", b_uint16, "b_uint16/s")
24+
t.Branch("b_uint32", b_uint32, "b_uint32/i")
25+
t.Branch("b_uint64", b_uint64, "b_uint64/l")
26+
27+
rows = [
28+
(200, 60000, 4000000000, 18000000000000000000),
29+
(255, 65535, 4294967295, 18446744073709551615),
30+
(1, 1, 1, 1),
31+
]
32+
33+
for u8, u16, u32, u64 in rows:
34+
b_uint8[0] = u8
35+
b_uint16[0] = u16
36+
b_uint32[0] = u32
37+
b_uint64[0] = u64
38+
t.Fill()
39+
40+
t.Write()
41+
f.Close()
42+
print("Written unsigned_integers.root")
6.66 KB
Binary file not shown.

0 commit comments

Comments
 (0)