# Uproot histogram behaviors

**URL:** <https://root-forum.cern.ch/t/uproot-histogram-behaviors/62414>\
**Category:** Newbie\
**Created:** [December 6, 2024, 9:52pm UTC](https://root-forum.cern.ch/t/uproot-histogram-behaviors/62414 "2024-12-06T21:52:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dyson\_Kennedy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dyson_kennedy/32/36338_2.png) [@Dyson\_Kennedy](https://root-forum.cern.ch/u/Dyson_Kennedy)\
**Post date:** [December 6, 2024, 9:52pm UTC](https://root-forum.cern.ch/t/uproot-histogram-behaviors/62414/1 "2024-12-06T21:52:05Z")

</div>

\_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:

```auto
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:  
 ![Figure_1](https://root-forum.cern.ch/uploads/default/original/3X/0/a/0aa7850b454be6c4124f2505c4a51981dd53a15f.png)

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.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [December 7, 2024, 5:12am UTC](https://root-forum.cern.ch/t/uproot-histogram-behaviors/62414/2 "2024-12-07T05:12:01Z")

</div>

I don’t know about uproot, but in ROOT you can `Add` the histograms (which you `Get` from the `TFile/s`) to a [THStack](https://root.cern/doc/master/classTHStack.html), then just draw the stack.
