My code is set up such that I have a dictionary of histograms (“trigger_mass_hist”), with each key being the name of a trigger and each value being the histogram itself. I want to graph all of the histograms in the directory on one canvas (maybe 10 histograms), but when I try to graph them as follows:
canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
for hist in trigger_mass_hist:
trigger_mass_hist[hist].Draw("hist e1")
canv1.Update()
only the last histogram shows up on the canvas. Any advice?
A hackish way would be doing it only if not present:
canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts = "hist e1"
for hist in trigger_mass_hist:
trigger_mass_hist[hist].Draw(draw_opts)
if "same" not in draw_opts:
draw_opts += " same"
canv1.Update()
But this will be slow (in is O(n)). Better do it with enumerate:
canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts = "hist e1"
for i, hist in enumerate(trigger_mass_hist):
trigger_mass_hist[hist].Draw(draw_opts)
if i == 0:
draw_opts += " same"
canv1.Update()
Or, the third option, use a THStack:
canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts = "hist e1"
stack = ROOT.THStack()
for hist in trigger_mass_hist.values():
stack.Add(hist)
stack.Draw("nostack" + draw_opts)
canv1.Update()
I draw one of them before the loop and then use the ‘same’ option in loop
from rootpy.interactive import wait
#add option or what ever
trigger_mass_hist.pop().Draw(draw_opts)
for hist in trigger_mass_hist:
hist.Draw('same'+draw_opts)
wait()
of course there was a flaw in the solution I proposed Anything like the previous very nice solutions of course would work!
I’d like at least to add another one to cope with the glitch above !
canv1.cd()
canv1.SetLogx()
canv1.SetLogy()
draw_opts="hist e1"
for i, hist in enumerate(trigger_mass_hist.itervalues()):
hist.Draw(draw_opts + "" if i==0 else " same")
canv1.Update()