_ROOT Version:6.32.08
_Platform:mac OS 14.7.1
Python 3.13.0
Hello,
I’m using uproot for the first time. I’m trying to take multiple histograms from .root files and edit them/combine them stacked. Here is the code I have currently:
import ROOT
import uproot
import matplotlib.pyplot as plt
# def rawHisto_to_datArr(startHisto):
# values = startHisto.values()
def get_histo(eventType, hisTyp):
thisFilePath = init_dir+eventType+'/plots/'+histotypes[hisTyp]+'.root'
print(thisFilePath)
thisOne = uproot.open(thisFilePath)[histonames[hisTyp]]
thisHist = thisOne.to_hist().plot(label=eventType)
return thisHist
init_dir = '~/Work/LNV_collider/AnalysisOutput/'
eventTypes = ['LNVF', 'ZZ2j', 'WZ2j', 'W3j']
histoType = 'bothLeps'
histotypes = {'WjPair':'Mass_2jW',
'bothLeps_Wj': 'Mass_2jW2l',
'leadingLep_Wj': 'Mass_2jW1l0',
'subleadingLep_Wj': 'Mass_2jW1l1',
'bothLeps': 'Mass_l2'}
histonames = {'WjPair': "Inv_Mass_2Jets_close_to_W",
'bothLeps_Wj': "Inv_Mass_2Jets_close_to_W_2l",
'leadingLep_Wj': "Inv_Mass_2Jets_close_to_W_1l_0",
'subleadingLep_Wj': "Inv_Mass_2Jets_close_to_W_1l_1",
'bothLeps': "Inv_Mass_2l"}
histos = []
for typ in eventTypes:
histos.append(get_histo(typ, histoType))
plt.legend()
plt.show()
Which gives me this:
What I’d like to do is stack them. If I had the data arrays, I’d do something like
ax.hist(x, n_bins, histtype='step', stacked=True, fill=False)
or ax.hist(x, n_bins, density=True, histtype='bar', stacked=True)
, where x is an array that contains all the necessary content from each original histogram.
The only solution I have so far seems overly complicated. I could take .values()
instead of using .to_hist()
then reverse engineer an array that has the right number of values in each bin by using .edges()
. Then put those all into an x
array as described above. However, given that uproot is so new to me, it seems like there’d be an easier way to do this.