Dividing and plotting two 2D histograms with pyROOT in Jupyter Hub(SWAN)

Hi everyone,
I’m trying to get the ratio of two 2D histograms from a root file and make a 2D ‘COLZ’ plot in Jupyter hub (SWAN). I can overlay these two 2D histograms, but I can’t do the same with their ratio. I’d appreciate it if you can show me where I do wrong. Here is my code:

data = ROOT.TFile("hist-data.root",'READ')
mc = ROOT.TFile("hist-allbkg.root",'READ')

c1 = TCanvas()
thd = TH2F()
thmc = TH2F()

thd = data.Get("PS_deltaPhi_minj1j2MET2D_all") 
thmc = mc.Get("PS_deltaPhi_minj1j2MET2D_all") 

thr = thd.Clone("thr")
thr.Sumw2()
thr.Divide(thmc)

c1.Draw()
c1.cd()
thr.Draw("COLZ")

c1.SaveAs("Plots_2D_MET/PS_deltaPhi_minj1j2MET2D.pdf")

It looks like thr is not constructed properly. It is not defined well but I don’t see why. Isn’t Clone() enough?

If I divide them into 2x2 pads, I can see all three working, but the example above is still not working. Working ex. with 3 pads

thr = thd.Clone()
thr.Divide(thmc)

c1.Divide(2,2);
c1.Draw()
c1.cd(1)
thd.Draw("COLZ")
c1.cd(2)
thmc.Draw("COLZ")
c1.cd(3)
thr.Draw('COLZ')

That does not draw the histogram. In your first example, you only draw thr as a heat map. you should see only that.

What I meant by that is I am cloning it but the resulting canvas is still empty. Then I tried 2x2 pads with 3 heat maps, which works. I still don’t know why one single plot is not drawn/or drawn empty in the first case.

Hi,

Just to make sure, this does not only happen in the notebook, right? If you run your code as a Python script and save your canvas to a PDF, you see an empty canvas?

I don’t see a reason why dividing it in pads would make it work, perhaps @couet knows?

If you provide a complete/running example reproducing the problem I can investigate it.

It looks like the source of the problem was input files. After fixing them, I can’t reproduce the problem. Thank you for your time.

Glad it works now!

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