Skip to content

Commit

Permalink
Merge pull request #32 from Moelf/PrettyTable1
Browse files Browse the repository at this point in the history
Better (faster) table preview and better ROOTFile preview
  • Loading branch information
8me authored Jul 10, 2021
2 parents 0968942 + 0a38167 commit ca9a715
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Tamas Gal", "Jerry Ling"]
version = "0.2.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
CodecLz4 = "5ba52731-8f18-5e0d-9241-30f10d1ec561"
CodecXz = "ba30903b-d9e8-5048-a5ec-d1f5b0d4b47b"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
Expand All @@ -12,11 +13,13 @@ LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73"
Mixers = "2a8e4939-dab8-5edc-8f64-72a8776f13de"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"

[compat]
AbstractTrees = "^0.3.0"
CodecLz4 = "^0.3.0, ^0.4.0"
CodecXz = "^0.6.0, ^0.7.0"
CodecZlib = "^0.6.0, ^0.7.0"
Expand All @@ -25,6 +28,7 @@ LRUCache = "^1.3.0"
Memoization = "^0.1.10"
Mixers = "^0.1.0"
Parameters = "^0.12.0"
PrettyTables = "^1.0.0"
StaticArrays = "^0.12.0, ^1"
Tables = "^1.0.0"
TypedTables = "^1.0.0"
Expand Down
6 changes: 4 additions & 2 deletions src/UnROOT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module UnROOT
export ROOTFile, LazyBranch, LazyTree

import Base: keys, get, getindex, show, length, iterate, position, ntoh, lock, unlock
import AbstractTrees: children, printnode, print_tree
using Base.Threads: SpinLock
ntoh(b::Bool) = b

using CodecZlib, CodecLz4, CodecXz, CodecZstd, StaticArrays
using Mixers, Parameters, Memoization, LRUCache
import Tables, TypedTables
using Mixers, Parameters, Memoization, LRUCache
import Tables, TypedTables, PrettyTables

@static if VERSION < v"1.1"
fieldtypes(T::Type) = [fieldtype(T, f) for f in fieldnames(T)]
Expand All @@ -27,6 +28,7 @@ include("bootstrap.jl")
include("root.jl")
include("iteration.jl")
include("custom.jl")
include("displays.jl")


end # module
49 changes: 49 additions & 0 deletions src/displays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function children(f::ROOTFile)
ch = Vector{TTree}()
for k in keys(f)
try
push!(ch, f[k])
catch
unlock(f) #TODO remove these hacks
end
end
ch
end
function children(t::TTree)
ks = keys(t)
[ first(ks, 5); ifelse(length(ks)>5,"","") ]
end
printnode(io::IO, t::TTree) = print(io, t.fName)
printnode(io::IO, f::ROOTFile) = print(io, f.filename)

function Base.show(io::IO, m::MIME"text/plain", tree::T) where T <: _LazyTreeType
PrettyTables.pretty_table(io, tree;
header=_make_header(tree),
alignment = :l,
compact_printing=true,
crop = :both,
display_size = (min(Base.displaysize()[1], 40), -1),
vlines = :none,
formatters = _treeformat
)
end
_symtup2str(symtup, trunc=15) = collect(first.(string.(symtup), trunc))
function _make_header(t)
pn = propertynames(t)
header = _symtup2str(pn)
subheader = _symtup2str(Tables.columntype.(Ref(t), pn))
(header, subheader)
end
function _treeformat(val, i, j)
s = if isempty(val)
"[]"
elseif val isa Vector{T} where T<:Integer
string(Int.(val))
elseif val isa Vector{T} where T<:AbstractFloat
string(round.(Float64.(val); sigdigits=3))
else
string(val)
end
s
end

1 change: 1 addition & 0 deletions src/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function Base.iterate(ba::LazyBranch{T, J}, idx=1) where {T, J}
end

const _LazyTreeType = TypedTables.Table{<:NamedTuple, 1, NamedTuple{S, N}} where {S, N <: Tuple{Vararg{LazyBranch}}}

struct LazyEvent{T<:_LazyTreeType}
tree::T
idx::Int64
Expand Down
11 changes: 6 additions & 5 deletions src/root.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ROOTFile
tkey::TKey
streamers::Streamers
directory::ROOTDirectory
lk::SpinLock
lk::ReentrantLock
end
lock(f::ROOTFile) = lock(f.lk)
unlock(f::ROOTFile) = unlock(f.lk)
Expand Down Expand Up @@ -59,17 +59,18 @@ function ROOTFile(filename::AbstractString)

directory = ROOTDirectory(tkey.fName, dir_header, keys)

ROOTFile(filename, format_version, header, fobj, tkey, streamers, directory, SpinLock())
ROOTFile(filename, format_version, header, fobj, tkey, streamers, directory, ReentrantLock())
end

function Base.show(io::IO, f::ROOTFile)
function Base.show(io::IO, m::MIME"text/plain", f::ROOTFile)
n_entries = length(f.directory.keys)
entries_suffix = n_entries == 1 ? "entry" : "entries"
n_streamers = length(f.streamers)
streamers_suffix = n_streamers == 1 ? "streamer" : "streamers"
print(io, typeof(f))
print(io, "(\"$(f.filename)\") with $n_entries $entries_suffix ")
print(io, "and $n_streamers $streamers_suffix.")
print(io, " with $n_entries $entries_suffix ")
println(io, "and $n_streamers $streamers_suffix.")
print_tree(f)
end


Expand Down

0 comments on commit ca9a715

Please sign in to comment.