I am using PyROOT to try to loop over the files in a folder, get a specific histogram which is present in all the files (two in a minimal test) and draw all the histograms in the same canvas. The minimal example which I run with just two histograms is the following.
import ROOT
import os
inputDir = "inputExample/"
outputDir = "outputExample/"
c1 = ROOT.TCanvas('c1', 'c1')
for filename in os.listdir(inputDir):
inputFile = ROOT.TFile.Open(inputDir+filename)
hist = inputFile.Get("variables/Method_BDT/BDT/MVA_BDT_trainingRejBvsS")
if filename == "first.root":
hist.Draw("")
else:
hist.Draw("SAME")
c1.SaveAs(outputDir+"Superimposed.png")
I do not understand why only the second histogram is saved. Here is another minimal example which I would expect to be equivalent and is working correctly (I get both histograms drawn in the same canvas).
import ROOT
inputDir = "inputExample/"
outputDir = "outputExample/"
c1 = ROOT.TCanvas('c1', 'c1')
inputFile1 = ROOT.TFile.Open(inputDir+"first.root")
hist = inputFile1.Get("variables/Method_BDT/BDT/MVA_BDT_trainingRejBvsS")
hist.Draw("")
inputFile2 = ROOT.TFile.Open(inputDir+"second.root")
hist = inputFile2.Get("variables/Method_BDT/BDT/MVA_BDT_trainingRejBvsS")
hist.Draw("SAME")
c1.SaveAs(outputDir+"Superimposed.png")