Everything works fine except TPad and TLatex location


ROOT Version: 6.14
Platform: Ubuntu16.04
_Compiler:_gcc530


I am running a python script, which works all fine in terms of major parts (TTree, TH1F hist filling, scaling and other stuffs)

However, positions of TPad and TLatex are incorrect.

Instead of talking, I believe it’s better to post my script and the resultant plot to determine the source of error.

from __future__ import division
#import ROOT
from ROOT import *
#from histutil import *
c1 = TCanvas( 'c1', 'Histogram Drawing Options', 800,800 )
c1.SetGrid()
gStyle.SetHistLineWidth(1)
gStyle.SetLineWidth(3)
gPad.SetTickx()
gPad.SetTicky()
gStyle.SetLineWidth(3)
gStyle.SetPalette(kBird)

c1.Clear()

pad1 = TPad("pad1", "The pad with the function",0,0.25,1,1)

pad1.SetFrameLineWidth(3)


pad1.Draw()

c1.cd()
##gStyle.SetOptTitle(0)
##gStyle.SetOptStat(0)
_file0 = TFile.Open('/uscms/home/s/nobackup/LLDJ_slc6_530_CMSSW_8_0_26_patch1/src/LLDJstandalones/ntuples/config/lldjntuple_mc_AOD50w.root','read')
sample ='ZH_HToSSTobbbb_ZToLL_MH-125_MS-15_ctauS-10'
h16 = TH1F("Decaydist",'%s Decaydist' % sample,200, 0, 20)
h16w = TH1F("Decaydistw",'%s Decaydistw' % sample,200, 0, 20)
mytree16 = _file0.Get("lldjNtuple/EventTree")
                                                                                                                             1,1           Top
for entry in mytree16:
        Decaydist=mytree16.Decaydist
        weight = mytree16.Simweight
        number = Decaydist.size()
        for i in range(number):
                #h16s.Fill(weight[i],1)
                h16.Fill(Decaydist[i],1)
                h16w.Fill(Decaydist[i],weight[i])

texf = TLatex()
texf.SetTextColor(1)
texf.SetTextAlign(22)
texf.SetTextSize(0.03)
#texf.SetTextAngle(90)
texf.DrawLatex(.165,.9,"CMS")
h16.Draw('HIST')
#h16.GetXaxis().SetRangeUser(0,10)
#h16.GetYaxis().SetRangeUser(10,20000)
h16.SetLineColor(2)
h16w.Draw('HIST SAME')
h16w.SetLineColor(5)
#title = TPaveLabel(.11,.95,.35,.99,"new title","brNDC")
#title.Draw()
legend = TLegend(0.5,0.75,0.7,0.9)
legend.AddEntry(h16,"Decaydist for ctau =1","f")
legend.AddEntry(h16w,"Decaydist for ctau =2","f")
legend.Draw()
l = TLatex()

l.SetTextAlign(33);
l.SetTextSize(0.02)

l.DrawTextNDC(0.13,0.9,"CMS")
l.DrawTextNDC(0.17,0.9,"#it{Preliminary}")
c1.Modified()
c1.Update()


I am planning to make the pad of my hist to be smaller than canvas, and position “CMS, Preliminary” aligned at left top of the Histogram pad.

But, what I obtain is…


Could you point out the source of error, and if there exists any advice in terms of positioning pad,LATEX,Pavetext box easily, could you teach me (such as automatically aligning TLatex with TPad)?

So far, my positioning strategy has been just trial and re-edit, which was so confusing.

Thank you.

Basically you never use the pad pad1 as, just after drawing it, you do c1->cd(); so all the drawings after that line are in c1. I replaced it with pad1->cd() in the following example I made from your script (I do not have your histo).
Also I changed the DrawTextNDC to DrawLatexNDC.

void trenta() {

   auto c1 = new TCanvas( "c1", "Histogram Drawing Options", 800,800 );
   c1->SetGrid();
   c1->SetTickx();
   c1->SetTicky();

   gStyle->SetHistLineWidth(1);
   gStyle->SetLineWidth(3);
   gStyle->SetLineWidth(3);
   gStyle->SetPalette(kBird);


   auto pad1 = new TPad("pad1", "The pad with the function",0,0.25,1,1);
   pad1->SetFrameLineWidth(3);
   pad1->Draw();
   pad1->cd();

   auto h16  = new TH1F("Decaydist","Decaydist",200, 0, 20);   h16->Fill(10.,10.);
   auto h16w = new TH1F("Decaydistw","Decaydistw",200, 0, 20); h16->Fill(15.,15.);

   auto texf = new TLatex();
   texf->SetTextColor(1);
   texf->SetTextAlign(22);
   texf->SetTextSize(0.03);
   texf->DrawLatex(.165,.9,"CMS");

   h16->Draw("HIST");
   h16->SetLineColor(2);
   h16w->Draw("HIST SAME");
   h16w->SetLineColor(5);

   auto legend = new TLegend(0.5,0.75,0.7,0.9);
   legend->AddEntry(h16,"Decaydist for ctau =1","f");
   legend->AddEntry(h16w,"Decaydist for ctau =2","f");
   legend->Draw();

   auto l = new TLatex();

   l->SetTextAlign(33);;
   l->SetTextSize(0.02);

   l->DrawLatexNDC(0.13,0.9,"CMS");
   l->DrawLatexNDC(0.17,0.9,"#it{Preliminary}");
}
1 Like

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