Unable to add TLatex text to pads

Hi everyone, I have a canvas which I divided into 4 and I would like to add text to each pad. The text would be different for the different pads, so I tried to access each pad individually with c->cd() and then add the text for that pad. However, when I run my macro, all the text is printed on top of each other in my last pad. I don’t know what I am doing wrong here. Many thanks!

#include "TFile.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TMultiGraph.h"
#include "TGraphAsymmErrors.h"
#include "TString.h"
#include <vector>
#include <iostream>

void mjj_eff_tc_ntc(const char *fname = "HaddOutput_old.root") {
  if (!(fname && fname[0])) return; // just a precaution
  TFile *f = TFile::Open(fname);
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  
  std::vector<std::string> TriggerCut = {"L1_J20", "L1_J40", "L1_J50", "L1_J100"};  
  std::vector<int> pT1Cut = {85, 130, 145, 220};
  std::vector<int> pT2Cut = {60, 70, 85, 90, 100, 110, 120, 130, 145, 150, 160, 170, 185, 190, 200, 210, 220};
  
  int n = TriggerCut.size();
  int n1 = pT1Cut.size();
  int n2 = pT2Cut.size();
  
  TCanvas *c = new TCanvas("c", "", (n1 * 350), 1000);
  c->Divide(2, 2);
  c->SetFillColor(0);
  
  TH1D *h[n];
  TH1D *s[n][n1];
  TH1D *m[n][n1][n2];
  TMultiGraph *mg[n];
  TGraphAsymmErrors *eff[n][n1][n2];
  TLegend *legend[n];
  TH1D *histo1[n];
  TH1D *histo2[n][n1];
  
  for (int i = 0; i < n; i++) {
    c->cd(i + 1);
    f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_0/pT2Cut_0/Mjj", TriggerCut[i].c_str()), h[i]);

    if (!h[i]) continue; // just a precaution
    mg[i] = new TMultiGraph();
    
    for (int j = 0; j < n1; j++) { 
     if (j != i) continue;
     f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_%d/pT2Cut_0/Mjj", TriggerCut[i].c_str(), pT1Cut[j]), s[i][j]);
     f->GetObject(TString::Format("AfterCuts/yCut_0.6/pT1Cut_%d/pT2Cut_0/Mjj", pT1Cut[j]), histo1[j]);
        
      for (int k = 0; k < n2; k++) { 
	    if (pT2Cut[k] > pT1Cut[j]) continue; // skip unwanted
        if (j == 1 && pT2Cut[k] < 110) continue;
        if (j == 2 && pT2Cut[k] < 120) continue;	
	    if (j == 3 && pT2Cut[k] < 200) continue;	
	    f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_%d/pT2Cut_%d/Mjj", TriggerCut[i].c_str(), pT1Cut[j], pT2Cut[k]), m[i][j][k]);
	    f->GetObject(TString::Format("AfterCuts/yCut_0.6/pT1Cut_%d/pT2Cut_%d/Mjj", pT1Cut[j], pT2Cut[k]), histo2[j][k]);
	
	    eff[i][j][k] = new TGraphAsymmErrors();
	    eff[i][j][k]->Divide(m[i][j][k], histo2[j][k], "n");
	    eff[i][j][k]->SetMarkerSize(0.5);  
        mg[i]->Add(eff[i][j][k], "p");
      }
    }

  mg[i]->Draw("AL");
  }
  c->cd(1);
  TLatex text1(62,1,"#splitline{L1_J20}{|y*| < 0.6}");
  text1.SetTextSize(0.05);
  text1.DrawClone();

 c->cd(2);
  TLatex text2(62,1,"#splitline{L1_J40}{|y*| < 0.6}");
  text2.SetTextSize(0.05);
  text2.DrawClone();
}

Well, it seems to work for me:

That’s weird! It really doesn’t work for me…

Which OS? Which version of ROOT? Which compiler?

Version 6.18.04
OS: CentOS7
Compiler: gcc 8

can you try this:

#include "TCanvas.h"
#include "TLatex.h"

void test_latex()
{
  TCanvas *c = new TCanvas("c", "c", 1000, 600);
  c->Divide(2, 2);
  
  c->cd(1);
  TLatex text1(0.1,0.9,"#splitline{L1_J20}{|y*| < 0.6}");
  text1.SetTextSize(0.05);
  text1.DrawClone();

  c->cd(2);
  TLatex text2(0.1,0.9,"#splitline{L1_J40}{|y*| < 0.6}");
  text2.SetTextSize(0.05);
  text2.DrawClone();
}

yes, that works

So it might be due to coordinates. I’ll try with v6.18.04 and let you know (need some time, rebuilding from scratch…)

Okay, thanks a lot for the help!!

So it works also with v6.18.04 on CentOS 7…

Yes, you’re right, I’m sorry. I got rid of some pieces of code when I sent it on the forum because I thought they weren’t relevant (like title name and limits), however, it seems like when I add those pieces, it doesn’t work anymore and my text appears on the last pad only. The code that seems to cause problems is the following:

    mg[i]->GetYaxis()->SetTitle("Efficiency");
    mg[i]->GetXaxis()->SetLabelSize(0.06);
    mg[i]->GetYaxis()->SetLabelSize(0.06);    
    mg[i]->GetXaxis()->SetTitleSize(0.065);
    mg[i]->GetYaxis()->SetTitleSize(0.065);    
    mg[i]->GetYaxis()->SetTitleOffset(1.1);  
    mg[i]->GetXaxis()->SetTitleOffset(1.2);  
    mg[i]->GetXaxis()->SetTitle("M_{jj} [GeV]");    
    mg[i]->GetXaxis()->SetLimits(0, 1000);     
    mg[i]->SetMinimum(0); 
    mg[i]->SetMaximum(1.2); 

So as I said, it’s maybe a coordinate issue. Try to change them in your TLatex. Maybe @couet can take a look and give more details…

OK, I can reproduce the issue, but no clue what could be the cause… We’ll have to wait until @couet is back (tomorrow) for more help

Okay, thanks for the help in the meantime!

Sorry I am looking at your problem only now and got a bit lost in you exchange with Bertrand. What is the final code I should try ?

This code:

#include "TFile.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TMultiGraph.h"
#include "TGraphAsymmErrors.h"
#include "TString.h"
#include "TLatex.h"
#include <vector>
#include <iostream>

void mjj_eff_tc_ntc(const char *fname = "HaddOutput_old.root")
{
  if (!(fname && fname[0])) return; // just a precaution
  TFile *f = TFile::Open(fname);
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution

  std::vector<std::string> TriggerCut = {"L1_J20", "L1_J40", "L1_J50", "L1_J100"};
  std::vector<int> pT1Cut = {85, 130, 145, 220};
  std::vector<int> pT2Cut = {60, 70, 85, 90, 100, 110, 120, 130, 145, 150, 160, 170, 185, 190, 200, 210, 220};

  int n = TriggerCut.size();
  int n1 = pT1Cut.size();
  int n2 = pT2Cut.size();

  TCanvas *c = new TCanvas("c", "", (n1 * 350), 1000);
  c->Divide(2, 2);
  c->SetFillColor(0);

  TH1D *h[n];
  TH1D *s[n][n1];
  TH1D *m[n][n1][n2];
  TMultiGraph *mg[n];
  TGraphAsymmErrors *eff[n][n1][n2];
  TLegend *legend[n];
  TH1D *histo1[n];
  TH1D *histo2[n][n1];

  for (int i = 0; i < n; i++) {
    c->cd(i + 1);
    f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_0/pT2Cut_0/Mjj", TriggerCut[i].c_str()), h[i]);

    if (!h[i]) continue; // just a precaution
    mg[i] = new TMultiGraph();

    for (int j = 0; j < n1; j++) {
      if (j != i) continue;
      f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_%d/pT2Cut_0/Mjj", TriggerCut[i].c_str(), pT1Cut[j]), s[i][j]);
      f->GetObject(TString::Format("AfterCuts/yCut_0.6/pT1Cut_%d/pT2Cut_0/Mjj", pT1Cut[j]), histo1[j]);

      for (int k = 0; k < n2; k++) {
	     if (pT2Cut[k] > pT1Cut[j]) continue; // skip unwanted
        if (j == 1 && pT2Cut[k] < 110) continue;
        if (j == 2 && pT2Cut[k] < 120) continue;
	     if (j == 3 && pT2Cut[k] < 200) continue;
	     f->GetObject(TString::Format("%s/yCut_0.6/pT1Cut_%d/pT2Cut_%d/Mjj", TriggerCut[i].c_str(), pT1Cut[j], pT2Cut[k]), m[i][j][k]);
	     f->GetObject(TString::Format("AfterCuts/yCut_0.6/pT1Cut_%d/pT2Cut_%d/Mjj", pT1Cut[j], pT2Cut[k]), histo2[j][k]);

	     eff[i][j][k] = new TGraphAsymmErrors();
	     eff[i][j][k]->Divide(m[i][j][k], histo2[j][k], "n");
	     eff[i][j][k]->SetMarkerSize(0.5);
        mg[i]->Add(eff[i][j][k], "p");
      }
    }
    mg[i]->Draw("AL");
    mg[i]->GetYaxis()->SetTitle("Efficiency");
    mg[i]->GetXaxis()->SetLabelSize(0.06);
    mg[i]->GetYaxis()->SetLabelSize(0.06);
    mg[i]->GetXaxis()->SetTitleSize(0.065);
    mg[i]->GetYaxis()->SetTitleSize(0.065);
    mg[i]->GetYaxis()->SetTitleOffset(1.1);
    mg[i]->GetXaxis()->SetTitleOffset(1.2);
    mg[i]->GetXaxis()->SetTitle("M_{jj} [GeV]");
    mg[i]->GetXaxis()->SetLimits(0, 1000);
    mg[i]->SetMinimum(0);
    mg[i]->SetMaximum(1.2);
  }
  c->cd(1);
  TLatex text1(62,1,"#splitline{L1_J20}{|y*| < 0.6}");
  text1.SetTextSize(0.05);
  text1.DrawClone();

  c->cd(2);
  TLatex text2(62,1,"#splitline{L1_J40}{|y*| < 0.6}");
  text2.SetTextSize(0.05);
  text2.DrawClone();
}

The TLatex are both drawn in the fourth pad, but if you remove (comment) those lines:

    mg[i]->GetYaxis()->SetTitle("Efficiency");
    mg[i]->GetXaxis()->SetLabelSize(0.06);
    mg[i]->GetYaxis()->SetLabelSize(0.06);    
    mg[i]->GetXaxis()->SetTitleSize(0.065);
    mg[i]->GetYaxis()->SetTitleSize(0.065);    
    mg[i]->GetYaxis()->SetTitleOffset(1.1);  
    mg[i]->GetXaxis()->SetTitleOffset(1.2);  
    mg[i]->GetXaxis()->SetTitle("M_{jj} [GeV]");    
    mg[i]->GetXaxis()->SetLimits(0, 1000);     
    mg[i]->SetMinimum(0); 
    mg[i]->SetMaximum(1.2);

It works fine.

DrawClone wrong usage. Do:

  c->cd(1);
  auto text1 = new TLatex(62.,1.,"#splitline{L1_J20}{|y*| < 0.6}");
  text1->SetTextSize(0.05);
  text1->Draw();

  c->cd(2);
  auto text2 = new TLatex(62.,1.,"#splitline{L1_J40}{|y*| < 0.6}");
  text2->SetTextSize(0.05);
  text2->Draw();
2 Likes

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