Skip to content

Commit

Permalink
add FHist.jl and option to parse directly to FHist.jl objects (#294)
Browse files Browse the repository at this point in the history
* add FHist

* extend function of `UnROOT.parseTH()`
  • Loading branch information
Moelf authored Nov 22, 2023
1 parent ca08029 commit 394ab1a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1"
CodecLz4 = "5ba52731-8f18-5e0d-9241-30f10d1ec561"
CodecXz = "ba30903b-d9e8-5048-a5ec-d1f5b0d4b47b"
CodecZstd = "6b39b394-51ab-5f42-8807-6242bab2b4c2"
FHist = "68837c9b-b678-4cd5-9925-8a54edc8f695"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
Expand All @@ -33,6 +34,7 @@ BitIntegers = "^0.2.6, ^0.3"
CodecLz4 = "^0.3, ^0.4"
CodecXz = "^0.6, ^0.7"
CodecZstd = "^0.6, ^0.7, ^0.8"
FHist = "0.10"
HTTP = "^0.9.7, 1"
IterTools = "^1"
LRUCache = "^1.3.0"
Expand Down
2 changes: 1 addition & 1 deletion src/UnROOT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ reinterpret(a,b) = Base.reinterpret(a,b)

import AbstractTrees: children, printnode, print_tree

using CodecLz4, CodecXz, CodecZstd, StaticArrays, LorentzVectors, ArraysOfArrays
using CodecLz4, CodecXz, CodecZstd, StaticArrays, LorentzVectors, ArraysOfArrays, FHist
using Mixers, Parameters, Memoization, LRUCache
import IterTools: groupby

Expand Down
36 changes: 29 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,36 @@ function JaggType(f, branch, leaf)
end

"""
parseTH(th::Dict{Symbol, Any})
parseTH(th::Dict{Symbol, Any}; raw=true) -> (counts, edges, sumw2, nentries)
parseTH(th::Dict{Symbol, Any}; raw=false) -> Union{FHist.Hist1D, FHist.Hist2D}
Parse the output of [`TH`](@ref) into a tuple of `counts`, `edges`, and `sumw2`.
A `StatsBase.Histogram` can then be constructed with `Histogram(edges, counts)`.
TH1 and TH2 inputs are supported.
When `raw=true`, parse the output of [`TH`](@ref) into a tuple of `counts`, `edges`, `sumw2`, and `nentries`.
When `raw=false`, parse the output of [`TH`](@ref) into FHist.jl histograms.
# Example
```julia
julia> UnROOT.parseTH(UnROOT.samplefile("histograms1d2d.root")["myTH1D"])
([40.0, 2.0], (-2.0:2.0:2.0,), [800.0, 2.0], 4.0)
julia> UnROOT.parseTH(UnROOT.samplefile("histograms1d2d.root")["myTH1D"]; raw=false)
edges: -2.0:2.0:2.0
bin counts: [40.0, 2.0]
total count: 42.0
```
!!! note
TH1 and TH2 inputs are supported.
"""
function parseTH(th::Dict{Symbol, Any})
function parseTH(th::Dict{Symbol, Any}; raw=true)
xmin = th[:fXaxis_fXmin]
xmax = th[:fXaxis_fXmax]
xnbins = th[:fXaxis_fNbins]
xbins = isempty(th[:fXaxis_fXbins]) ? range(xmin, xmax, length=xnbins+1) : th[:fXaxis_fXbins];
counts = th[:fN]
nentries = th[:fEntries]
sumw2 = th[:fSumw2]
if th[:fYaxis_fNbins] > 1
dimension = th[:fYaxis_fNbins]
if dimension > 1
ymin = th[:fYaxis_fXmin]
ymax = th[:fYaxis_fXmax]
ynbins = th[:fYaxis_fNbins]
Expand All @@ -112,7 +128,13 @@ function parseTH(th::Dict{Symbol, Any})
sumw2 = sumw2[2:end-1]
edges = (xbins,)
end
return counts, edges, sumw2
if raw
return counts, edges, sumw2, nentries
elseif dimension > 1
return Hist2D(FHist.Histogram(edges, counts),sumw2, nentries)
else
return Hist1D(FHist.Histogram(edges, counts),sumw2, nentries)
end
end

function samplefile(filename::AbstractString)
Expand Down
18 changes: 15 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using UnROOT, LorentzVectors
using UnROOT.FHist
using StaticArrays
using InteractiveUtils
using DataFrames, SHA
Expand Down Expand Up @@ -639,9 +640,20 @@ end
@test f[k][:fN] == [0.0, 0.0, 0.0, 0.0, 0.0, 20.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]
end

@test UnROOT.parseTH(f["myTH1F"]) == ([40.0, 2.0], (-2.0:2.0:2.0,), [800.0, 2.0])
@test UnROOT.parseTH(f["myTH2D"]) == ([20.0 0.0 0.0 20.0; 1.0 0.0 0.0 1.0], (-2.0:2.0:2.0, -2.0:1.0:2.0), [400.0 0.0 0.0 400.0; 1.0 0.0 0.0 1.0])
@test UnROOT.parseTH(f["myTH1D_nonuniform"]) == ([40.0, 2.0], ([-2.0, 1.0, 2.0],), [800.0, 2.0])
th1 = UnROOT.parseTH(f["myTH1F"];raw=false)
@test bincounts(th1) == [40.0, 2.0]
@test binedges(th1) == -2.0:2.0:2.0
@test th1.sumw2 == [800.0, 2.0]

th2 = UnROOT.parseTH(f["myTH2D"];raw=false)
@test bincounts(th2) == [20.0 0.0 0.0 20.0; 1.0 0.0 0.0 1.0]
@test binedges(th2) == (-2.0:2.0:2.0, -2.0:1.0:2.0)
@test th2.sumw2 == [400.0 0.0 0.0 400.0; 1.0 0.0 0.0 1.0]

th3 = UnROOT.parseTH(f["myTH1D_nonuniform"];raw=false)
@test bincounts(th3) == [40.0, 2.0]
@test binedges(th3) == [-2.0, 1.0, 2.0]
@test th3.sumw2 == [800.0, 2.0]

close(f)

Expand Down

0 comments on commit 394ab1a

Please sign in to comment.