Problem with showing properly TPad in divided Canvas - Only on TPad shows up

Hello

I am dividing a canvas and then I create and draw a pad for each one of the divided panels. The problem is the only the last pad seems to show up properly. I paste a snippet of my code (to keep it simple I am cutting off the cosmetics etc)

from ROOT import TFile, TH1, kRed, kGreen, kBlue, kBlack, TCanvas, TPad
from ROOT import gSystem, gStyle, gROOT, kTRUE, gDirectory, gPad
from ROOT import TCanvas, TH1D, TH1F, THStack, TFile, TPad, TLegend, TLatex, TLine, TAttMarker, TMarker, TColor
from ROOT import TFile, TTree, TH1D, TCanvas, TLorentzVector, TLatex, kRed, kBlue, kBlack, TLegend, TF1, gStyle, gROOT, THStack, TColor, TPad, gPad
from ROOT import gROOT, THStack
import os, sys

fList=['Results/FEH_2S_Skeleton_Run155_OutSideBox_TapedVTRX_NoGndOnCarrier_module9/Hybrid.root', 'Results/FEH_2S_Skeleton_Run156_OutSideBox_TapedVTRX_WithGndOnCarrier_module9/Hybrid.root','Results/FEH_2S_Skeleton_Run155_OutSideBox_TapedVTRX_NoGndOnCarrier_module9/Hybrid.root', 'Results/FEH_2S_Skeleton_Run156_OutSideBox_TapedVTRX_WithGndOnCarrier_module9/Hybrid.root']

cT = TCanvas('c','c',90,90,W,H)
cT.SetFillColor(0)
cT.SetBorderMode(0)
cT.SetFrameFillStyle(0)
cT.SetFrameBorderMode(0)

cT.SetLeftMargin(L/W)
cT.SetRightMargin(R/W)
cT.SetTopMargin(T/H)
cT.SetBottomMargin(B/H)
cT.Divide(2,2)

H = 900
W = 1600

for i, fIn in enumerate(fList) :
    f=TFile(fIn,"read")
    f.cd()

    # read the four needed histograms for each input file
    hLB=f.Get(hLBottom)
    hLT=f.Get(hLTop)
    hRB=f.Get(hRBottom)
    hRT=f.Get(hRTop)

    cT.cd(i+1)
    cP = TPad('pad',"pad",0.0,0.25,1.0,1.0)
    cP.SetLeftMargin(L/W)
    cP.SetRightMargin(R/W)
    cP.SetTopMargin(T/H)
    cP.Draw()
    cP.cd()

    hLB.Draw('hist')
    hLT.Draw("same hist p")
    hRT.Draw("same hist p")
    hRB.Draw("same hist p")

    cP.Update()
    cT.Update()
    gPad.Update()
    gPad.RedrawAxis()

cT.SaveAs("plots.pdf")

cT.Divide(2,2)

plots.pdf (54.1 KB)

I do not understand why you need the pad cP ? and why an other Divide at the end of the script ? why not simply do:

from ROOT import TFile, TH1, kRed, kGreen, kBlue, kBlack, TCanvas, TPad
from ROOT import gSystem, gStyle, gROOT, kTRUE, gDirectory, gPad
from ROOT import TCanvas, TH1D, TH1F, THStack, TFile, TPad, TLegend, TLatex, TLine, TAttMarker, TMarker, TColor
from ROOT import TFile, TTree, TH1D, TCanvas, TLorentzVector, TLatex, kRed, kBlue, kBlack, TLegend, TF1, gStyle, gROOT, THStack, TColor, TPad, gPad
from ROOT import gROOT, THStack
import os, sys

fList=['Results/FEH_2S_Skeleton_Run155_OutSideBox_TapedVTRX_NoGndOnCarrier_module9/Hybrid.root', 'Results/FEH_2S_Skeleton_Run156_OutSideBox_TapedVTRX_WithGndOnCarrier_module9/Hybrid.root','Results/FEH_2S_Skeleton_Run155_OutSideBox_TapedVTRX_NoGndOnCarrier_module9/Hybrid.root', 'Results/FEH_2S_Skeleton_Run156_OutSideBox_TapedVTRX_WithGndOnCarrier_module9/Hybrid.root']

H = 900
W = 1600

cT = TCanvas('c','c',90,90,W,H)
cT.SetFillColor(0)
cT.SetBorderMode(0)
cT.SetFrameFillStyle(0)
cT.SetFrameBorderMode(0)

cT.SetLeftMargin(L/W)
cT.SetRightMargin(R/W)
cT.SetTopMargin(T/H)
cT.SetBottomMargin(B/H)
cT.Divide(2,2)



for i, fIn in enumerate(fList) :
    f=TFile(fIn,"read") 
    f.cd()

    # read the four needed histograms for each input file
    hLB=f.Get(hLBottom)
    hLT=f.Get(hLTop)
    hRB=f.Get(hRBottom)
    hRT=f.Get(hRTop)

    cT.cd(i+1)  

    hLB.Draw('hist')
    hLT.Draw("same hist p")
    hRT.Draw("same hist p")
    hRB.Draw("same hist p")

    gPad.RedrawAxis()

cT.SaveAs("plots.pdf")

Also:

  • W and H initialization where misplaced …
  • the index I start at 0 ?

Note: I have not not ru the script. I do not have your data

@couet, thanks

I am uploading a working .py with a .root that should work (in the .py I am using the same .root four times, but this is just to make a working example for you).

Anyway, I tried running what you suggested, but still only one pad is filled…

Hybrid.root (1.1 MB)
histos.py (4.0 KB)

I think it is because you reuse the same histogram/file pointers in the loop.

if you do:

count=1
f=TFile("Hybrid.root","read")
f.cd()
hLB=f.Get(hLBottom)
hLT=f.Get(hLTop)
hRB=f.Get(hRBottom)
hRT=f.Get(hRTop)
for i,fIn in enumerate(fList) :
    hLB.SetFillColor(0)
    hLT.SetFillColor(0)
    hRB.SetFillColor(0)
    hRT.SetFillColor(0)

    hLB.SetLineColor(kBlack)
    hLT.SetLineColor(kBlue)
    hRB.SetLineColor(kRed)
    hRT.SetLineColor(kGreen)

    hLB.SetMarkerColor(kBlack)
    hLT.SetMarkerColor(kBlue)
    hRB.SetMarkerColor(kRed)
    hRT.SetMarkerColor(kGreen)

    hLB.SetLineStyle(2)
    hLT.SetLineStyle(2)
    cT.cd(count)

    hLB.Draw('hist')
    hLT.Draw("same hist p")
    hRT.Draw("same hist p")
    hRB.Draw("same hist p")

    count = count+1

cT.SaveAs("plots.pdf")

It is ok … but that’s not what you want.

sorry, I am still missing what the problem is. Why using dictionaries for the histogram does not work? To me, it looks that the problem is the navigation between the pads does not work as expected and this is why I always get the last pad of the canvas to be filled and the others.

ok, quick update. The problem was caused as I was not using a dictionary for the input files…once I do that, everything works fine!

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