Convert a root histogram to an array (to use in matplotlib to re-create the histogram with better aesthetics)

Hi,

I want to convert a root histogram that I have to an array so that I can use the array to re-create the histogram using matplotlib with better aesthetics. I show bellow my code and even though it produces a histogram, it doesn’t look like the original histogram. Can any one please help me to figure what I’m doing wrong here? (I change the bins=10 from bins=binnxs because the code fails)

Thanks!!!

#!/usr/bin/env python

import ROOT as Root
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
h1 = fig.add_subplot(1,1,1)


loc = str("../V_tag_hists/VV_0t/CR_0bHHT/")
a = Root.TFile.Open(loc+"hists_data_lowMET.root")
b = Root.TFile.Open(loc+"hists_dijet_lowMET.root")

x = a.Get("HT")
y = b.Get("HT")

binnxs = [x.GetXaxis().GetBinLowEdge(1)]
bin_contenta = []
bin_contentb = []
print(binnxs)


for i in xrange(1,x.GetNbinsX()+1):
    bin_contenta.append(x.GetBinContent(i))
    binnxs.append(x.GetXaxis().GetBinUpEdge(i))
for j in xrange(1,y.GetNbinsX()+1):
    bin_contentb.append(y.GetBinContent(j))
   

h1.hist([bin_contenta,bin_contentb], bins=10, histtype='step', stacked = True)

plt.tight_layout()
plt.show()
plt.savefig('new.png',facecolor=fig.get_facecolor(),         
            edgecolor=fig.get_edgecolor())

It would be also interesting for us to know what you mean with “better aesthetics” ? May be a picture example, produced with MathPlotlib, showing what you would like to have which apparently ROOT cannot do ? We may come with a solution avoiding the conversion… who knows ? …

1 Like

I currently develop a pyroot ode that produces following plot

but I want to do the same thing using matplotlib so that ultimately I want it to make it something like this

Thank you!

Since last summer ROOT has a nice class doing this kind of plots. It is called TRatioPlot .

2 Likes

Hi Couet,

Thank you very much for letting me know about TRatioPlot, it’s so awesome!!! :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.