@@ -33,29 +33,24 @@ julia> pywith(up.recreate("./example.root")) do file
3333```
3434
3535### Writing out a histogram with errors (sumW2)
36- [ref]( https://github.com/scikit-hep/uproot5/issues/696#issuecomment-1235918878)
36+ We need https://github.com/scikit-hep/hist for this:
3737
38- Unfourtunately `uproot` does not support this yet, so we have to use pyROOT:
3938``` julia
40- using PythonCall
41- ROOT = pyimport (" ROOT" )
42-
43- # h is a FHist.jl histogram but you just need two arrays
44- bc = bincounts (h)
45- be = binerrors (h)
39+ julia> using PythonCall, FHist
4640
47- file = ROOT. TFile (" /tmp/example.root" , " recreate" )
48- # 100 bins from 0 to 1
49- th1d = ROOT. TH1D (" blah" , " blah blah" , 100 , 0 , 1 )
50- for i in eachindex (bc)
51- # ROOT has under/overflow bins outside of normal range
52- # so we're skipping `0`-th bin by adhering Julia's 1-based index
53- th1d. SetBinContent (i, bc[i])
54- # similarly, we're skipping overflow bin
55- th1d. SetBinError (i, be[i])
41+ julia> function to_pyhist (h:: Hist1D )
42+ pyhist = pyimport (" hist" )
43+ bes, bcs, sumw2s = binedges (h), bincounts (h), sumw2 (h)
44+ h1 = pyhist. Hist. new. Variable (collect (bes)). Weight ()
45+ for idx in eachindex (bcs, sumw2s)
46+ h1. view ()[idx- 1 ] = (bcs[idx], sumw2s[idx])
47+ end
48+ h1
5649end
57- th1d. Write ()
58- file. Close ()
50+
51+ julia> pywith (up. recreate (" ./example.root" )) do file
52+ file[" myhist" ] = np. array (bincounts (h)), np. array (binedges (h))
53+ end ;
5954```
6055
6156we can veryfy the round trip gives us back the original number:
0 commit comments