Add text to different plots on the same canvas with TLatex

Hey all, I am trying to add some text to my plots with TLatex, but my text doesn’t appear on my plots. I have 4 plots on 1 canvas, and I would like to add 1 piece of text to each plot, so for example, on my first plot I would like it to say ‘pT1 > 80’, on the second ‘pT1 > 130’ etc. I tried the following, but it is not working… 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(const char *fname = "HaddOutput.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_J75"};
  std::vector<int> pT1Cut = {80, 130, 145, 190};
  std::vector<int> pT2Cut = {60, 70, 80, 90, 100, 110, 120, 130, 145, 150, 160, 170, 190};
  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];
  TF1 *fit[n][n1][n2];
  TF1 *sigmoid[n][n1][n2];

  TLegend *legend[n];
  
  TLatex *latex[n];

  TH1D *histo = (TH1D*)f->Get("AfterCuts/yCut_0.3/pT1Cut_0/pT2Cut_0/Mjj");  
  
  for (int i = 0; i < n; i++) {
    c->cd(i + 1);
    latex[I]->SetTextAlign(12);
    latex[I]->SetTextFont(200);  
    f->GetObject(TString::Format("%s/yCut_0.3/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.3/pT1Cut_%d/pT2Cut_0/Mjj", TriggerCut[i].c_str(), pT1Cut[j]), s[i][j]);
        
      for (int k = 0; k < n2; k++) { 
	    if (pT2Cut[k] > pT1Cut[j]) continue; // skip unwanted
	    f->GetObject(TString::Format("%s/yCut_0.3/pT1Cut_%d/pT2Cut_%d/Mjj",      TriggerCut[i].c_str(), pT1Cut[j], pT2Cut[k]), m[i][j][k]);
	    eff[i][j][k] = new TGraphAsymmErrors();
	    eff[i][j][k]->Divide(m[i][j][k], histo, "n");	
        mg[i]->Add(eff[i][j][k], "p");                            
      }
    }
  }  
   latex[0]->DrawLatex(.1,.80,"pT1 >80");
   latex[1]->DrawLatex(.1,.55,"pT1>130");
   latex[2]->DrawLatex(.1,.55,"pT1>145");
   latex[3]->DrawLatex(.1,.55,"pT1>185");
}

you should draw the TLatex after the cd … it seems you do it at the end of the loop. As it is your TLaTeX are all drawn in pad #n

I’m not sure I understand what you mean, but I tried to do it without a loop, and so I did the following. However, I get: "warning: invalid memory pointer passed to a callee:
latex->SetTextAlign(12); " I also don’t know how to find the coordinates of my pads… Thanks!

  c->cd(1);
  TLatex *latex1;
  latex1->SetTextAlign(12);
  latex1->SetTextFont(100);
  latex1->DrawLatex(1,1,"pT1>80");

is c defined ? yes the text will be drawn in the last pad you cd in. The simple immediate way to know which coordinate to use if to look at you plot axis. they are the ones.

Ah okay, I get the coordinates now thanks! And yes, I defined my canvas at the start and named it c…

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