Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

Writing a boost-histogram to a root file created in uproot

I am currently trying to read in a ROOT tree with uproot, book and fill histograms with boost-histogram and write these back out to a new ROOT file. This seems to work very well for the bin contents, but I can't seem to get the correct bin errors for the histograms in the final ROOT file. When I read these histograms, h.GetBinError(b) seems to be equal to h.GetBinContent(b), while I would like to have the familiar sumW2 errors which are normally produced for a ROOT histogram. My workflow is as follows, using python 3.8:

import boost_histogram as bh
import uproot3 as uproot 
bins = np.array( [1,2,3,4], dtype='d' )
hist = bh.Histogram( bh.axis.Variable(bins))

I then iterate over the input tree using uproot4.iterate, to produce an awkward array for each 'chunk', denoted here by e:

hist.fill(e["pt"], weight=(e["weight"]))
with uproot.recreate(outfilename) as outfile:
   outfile["hist_pt"] = hist.to_numpy()

I am not sure how to get the sumw2 weights in the construction of the boost-histogram, but based on the documentation I changed my histogram definition to:

hist = bh.Histogram( bh.axis.Variable(bins), storage=bh.storage.Weight() )

However, when I run my code, this causes issues with the .to_numpy() line. The error says: "what = uproot3_methods.convert.towriteable(what)" "TypeError: cannot perform reduce with flexible type"

Is there some other way to propagate sumW2-style errors from a boost-histogram to a ROOT file created in uproot? Or perhaps there is an error in my understanding of this storage= variable.... Any help would be greatly appreciated.

question from:https://stackoverflow.com/questions/65951054/writing-a-boost-histogram-to-a-root-file-created-in-uproot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...