Skip to content

Commit

Permalink
Custom TObject hook (#232)
Browse files Browse the repository at this point in the history
* Preliminary debugging for generic tobject stuff

* Add parser logic

* Add tests

* Bump version number
  • Loading branch information
tamasgal authored Mar 31, 2023
1 parent 09a3d2a commit 9a12043
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 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.10.1"
version = "0.10.2"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
27 changes: 27 additions & 0 deletions src/bootstrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,33 @@ function TNtuple(io, tkey::TKey, refs)
tree = TTree(io, tkey, refs; top=false) #embeded tree
end

"""
Direct parsing of streamed objects which are not sitting on branches.
"""
function parsetobject(io, tkey::TKey, streamer)
# pass the correct parser from f!
io = datastream(io, tkey)
preamble = Preamble(io, Missing)

@initparse

# the first entry in the streamer is a TOBject
parsefields!(io, fields, TObject)

# the second (and last) entry is the actual data streamer (we think)
s = streamer.streamer.fElements.elements[2]
if s.fTypeName == "map<string,string>"
skip(io, 3*4) # unclear what the first 12 bytes are
# this gives the number of elements
n = readtype(io, Int32)
skip(io, 6) # the usual header stuff?
keys = [readtype(io, String) for i 1:n]
skip(io, 6) # the usual header stuff?
values = [readtype(io, String) for i 1:n]
return Dict(zip(keys, values))
end
error("Unable to parse '$(s.fTypeName)' of '$(tkey.fClassName)'")
end

# FIXME preliminary TTree implementation
function TTree(io, tkey::TKey, refs; top=true)
Expand Down
15 changes: 11 additions & 4 deletions src/root.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,17 @@ end
return f[first(paths)][join(paths[2:end], "/")]
end
tkey = f.directory.keys[findfirst(isequal(s), keys(f))]
@debug "Retrieving $s ('$(tkey.fClassName)')"
streamer = getfield(@__MODULE__, Symbol(safename(tkey.fClassName)))
S = streamer(f.fobj, tkey, f.streamers.refs)
return S
typename = safename(tkey.fClassName)
@debug "Retrieving $s ('$(typename)')"
try
streamer = getfield(@__MODULE__, Symbol(typename))
S = streamer(f.fobj, tkey, f.streamers.refs)
return S
catch UndefVarError
end

# last resort, try direct parsing
parsetobject(f.fobj, tkey, streamerfor(f, typename))
end

# FIXME unify with above?
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ end
# close(rootfile)
end

@testset "Single TObject subclasses" begin
f = UnROOT.samplefile("triply_jagged_via_custom_class.root")
# map<string,string>
head = f["Head"]
@test 27 == length(f["Head"])
@test " 3.3" == head["DAQ"]
@test " CORSIKA 7.640 181111 1211" == head["physics"]
@test "MUSIC seawater 02-03 190204 1643" == head["propag"]
close(f)
end

@testset "LazyBranch and LazyTree" begin
rootfile = ROOTFile(joinpath(SAMPLES_DIR, "tree_with_large_array.root"))
branch = rootfile["t1"]["int32_array"]
Expand Down

2 comments on commit 9a12043

@tamasgal
Copy link
Member Author

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/80713

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.10.2 -m "<description of version>" 9a12043b5085deab1eb47f65d3d92324b2f99403
git push origin v0.10.2

Please sign in to comment.