Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add effective entries #121

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/FHist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,35 @@ for (H, N) in ((:Hist1D, 1), (:Hist2D, 2), (:Hist3D, 3))
"""
bincenters(h::$H) = _from_tuple(StatsBase.midpoints.(h.binedges))
@doc """
nentries(h::$($H))
Get the number of times a histogram is filled (`push!`ed)
nentries(h::$($H))
Get the number of times a histogram is filled (`push!`ed)
"""
nentries(h::$H) = h.nentries[]
@doc """
sumw2(h)
Get the sum of weights squared of the histogram, it has the same shape as `bincounts(h)`.
sumw2(h)
Get the sum of weights squared of the histogram, it has the same shape as `bincounts(h)`.
"""
sumw2(h::$H) = h.sumw2

@doc """
binerrors(f=sqrt, h)
Get the error (uncertainty) of each bin. By default, calls `sqrt` on `sumw2(h)` bin by bin as an approximation.
binerrors(f=sqrt, h)
Get the error (uncertainty) of each bin. By default, calls `sqrt` on `sumw2(h)` bin by bin as an approximation.
"""
binerrors(f::T, h::$H) where T<:Function = f.(sumw2(h))
binerrors(h::$H) = binerrors(sqrt, h)

@doc raw"""
effective_entries(h) -> scalar

Get the number of effective entries for the entire histogram:

```math
n_{eff} = \frac{(\sum Weights )^2}{(\sum Weight^2 )}
```

This is also equivalent to `integral(hist)^2 / sum(sumw2(hist))`, this is the same as `TH1::GetEffectiveEntries()`
"""
effective_entries(h::$H) = abs2(integral(h)) / sum(sumw2(h))

function Base.:(==)(h1::$H, h2::$H)
bincounts(h1) == bincounts(h2) &&
Expand Down
1 change: 1 addition & 0 deletions src/arithmatics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ for T in (:Hist1D,:Hist2D,:Hist3D)

($T)(; bincounts = newcounts, binedges = copy.(binedges(h1)), sumw2 = sumw2(h1) * num^2, nentries = nentries(h1), overflow = h1.overflow)
end
@eval *(num::Real, h1::($T)) = h1 * num

# https://github.com/aminnj/yahist/blob/4a5767f181ec7fdcc4af18cf15ceedd1c2f89019/yahist/hist1d.py#L427-L430
@eval function /(h1::($T), h2::($T))
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,15 @@ end
@test all(significance(h1,h2) .≈ (9.839916447569484, 0.30998654607114046))
end

@testset "Effective entries" begin
h = Hist1D(; binedges = 1:10)
push!(h, 3)
push!(h, 3)
@test FHist.effective_entries(h) == 2
push!(h, 8, 0.2)
@test FHist.effective_entries(h) ≈ 2.3725490196078436
push!(h, 4, 0.2)
@test FHist.effective_entries(h) ≈ 2.76923076923077
end

include("hdf5.jl")
Loading