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

Hist1D division #7

Closed
aminnj opened this issue Aug 3, 2021 · 2 comments
Closed

Hist1D division #7

aminnj opened this issue Aug 3, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@aminnj
Copy link
Collaborator

aminnj commented Aug 3, 2021

Division of two Hist1Ds currently crashes. It works with a small modification

--- a/src/arithmatics.jl
+++ b/src/arithmatics.jl
@@ -36,8 +36,8 @@ for T in (:Hist1D,)
         _f(counts) = any(x -> x<0, counts)
         (_f(h1.hist.weights) || _f(h2.hist.weights)) && error("Can't do / when bin count is negative")
         _hist = /(h1.hist, h2.hist)
-        _sumw2 =  h1.sumw2 / (h2.hist.weights .^ 2) +
-                (sqrt.(h2.sumw2) * h1.hist.weights / (h2.hist.weights .^ 2)) .^ 2
+        _sumw2 =  @. h1.sumw2 / (h2.hist.weights ^ 2) +
+                (sqrt(h2.sumw2) * h1.hist.weights / (h2.hist.weights ^ 2)) ^ 2

         ($T)(_hist, _sumw2)
     end

Though the output _sumw2 is a bunch of zeros if h1 and h2 are filled without weights. Maybe there needs to be a check inside here which "materializes" each of the sumw2s (gaussian/sqrt by default) before doing the error propagation?

@Moelf Moelf added the bug Something isn't working label Aug 3, 2021
@Moelf Moelf self-assigned this Aug 3, 2021
@aminnj
Copy link
Collaborator Author

aminnj commented Aug 4, 2021

OK, matches ROOT.

julia> using StatsBase
julia> h1 = Hist1D([0.5,1.5,1.5,2.5], Weights(ones(4)), 0:3);
julia> h2 = Hist1D([1.5,2.5,2.5], Weights(ones(3)), 0:3);
julia> (h1/h2).sumw2 |> println
[NaN, 6.0, 0.375]
import ROOT

h1 = ROOT.TH1F("h1", "", 3, 0, 3); h1.Sumw2()
h2 = ROOT.TH1F("h2", "", 3, 0, 3); h2.Sumw2()

for x in [0.5,1.5,1.5,2.5]: h1.Fill(x)
for x in [1.5,2.5,2.5]: h2.Fill(x)

h1.Divide(h2)

print([round(h1.GetBinContent(i), 4) for i in range(1,h1.GetNbinsX()+1)])
# [0.0, 2.0, 0.5]

print([round(h1.GetBinError(i)**2, 4) for i in range(1,h1.GetNbinsX()+1)])
# [0.0, 6.0, 0.375]

@aminnj
Copy link
Collaborator Author

aminnj commented Aug 4, 2021

Ignore/close this and let's just move the discussion to #8 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants