Giving title to divided canvas

Hii
I am trying to set a title in a divided canvas. But until now I am unable to do that. I would be grateful if anyone checks that out.
So the code is like this;

TCanvas c15("c15", "VTPC1 1D ResX distr in Sectors", 1024, 768);
c15.SetTitle("VTPC1");
  c15.UseCurrentStyle();
  c15.Divide(3,2);
  for (int i = 0; i < 6; i ++){
  c15.cd(i+1);
  gPad->SetLogy();
  gPad->SetLeftMargin(0.09);
  gPad->SetRightMargin(0.1);
  gPad->SetTopMargin(0.2);
  gPad->SetBottomMargin(0.05);
  gPad->Update();
  gPad->Modified();
  VTPC1_DX_distr[i]->SetTitle("");
  VTPC1_DX_distr[i]->GetXaxis()->SetLabelSize(0.05);
  VTPC1_DX_distr[i]->GetYaxis()->SetLabelSize(0.05);
  VTPC1_DX_distr[i]->GetXaxis()->SetTitleOffset(-0.5);
  VTPC1_DX_distr[i]->GetXaxis()->SetTitleSize(0.07);
  VTPC1_DX_distr[i]->GetYaxis()->SetTitleOffset(-0.6);
  VTPC1_DX_distr[i]->GetYaxis()->SetTitleSize(0.07);
  VTPC1_DX_distr[i]->SetMarkerStyle(21);
  VTPC1_DX_distr[i]->SetMarkerColor(kRed);
  VTPC1_DX_distr[i]->SetMarkerSize(0.4);
  VTPC1_DX_distr[i]->GetYaxis()->SetTitle("N_{entries} ");
  VTPC1_DX_distr[i]->GetXaxis()->SetTitle("X - X_{fit}");
  VTPC1_DX_distr[i]->Draw("E");
  //VTPC1_DX_distrMC[i]->Draw("HISTSAME");
  }

The plot is attached here.
Also I want to give titles to different sectors. How can I do that?


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


To give a global title to you canvas the simplest will be to put a TText (or TLateX) on top of it.

Thanks
but I have used that. The thing is I can’t see it maybe because the pads are covering the text.

{
   auto c1 = new TCanvas("c1", "c1",15,48,700,530);

   c1->Divide(2,2);
   c1->cd(1)->DrawFrame(0,0,1,1);
   c1->cd(2)->DrawFrame(0,0,1,1);
   c1->cd(3)->DrawFrame(0,0,1,1);
   c1->cd(4)->DrawFrame(0,0,1,1);

   c1->cd();
   TPad *padtitle = new TPad("padtitle", "padtitle",0.3,0.9,0.7,0.99);
   padtitle->Draw();
   padtitle->cd();
   padtitle->SetFillStyle(0);

   auto tex = new TLatex(0.5,0.5,"Title");
   tex->SetTextAlign(22);
   tex->SetTextSize(0.5);
   tex->Draw();
}

Thank you. Its done now giving the title to the whole canvas. Another thing is giving individual names to individual histograms. I tried using TStrings but i got the “break segmentation error”. Can you please tell me how can I solve that.

If s is a TString, to access the character string stored inside s you need to use s.Data().

histo-SetTitle("Sector1");

What would be for histo [i]->SetTitle (" ? ");

the same…

histo[i]-SetTitle("Sector1");

I do not understand where is the problem …

SO I am getting these histograms from one root file. Like this;

      VTPC1_DX_distr[i] = (TH1F*)f1->Get(Form("VTPC1ResXDistrSec%i",i+1));
      VTPC2_DX_distr[i] = (TH1F*)f1->Get(Form("VTPC2ResXDistrSec%i",i+1));
      VTPC1_DY_distr[i] = (TH1F*)f1->Get(Form("VTPC1ResYDistrSec%i",i+1));
      VTPC2_DY_distr[i] = (TH1F*)f1->Get(Form("VTPC2ResYDistrSec%i",i+1));

Then I try to plot them in canvas.
here i = 6
The code for plotting is;

  gStyle->SetOptFit(0);
  gStyle->SetOptStat(0);
  gStyle->SetStatW(0.6);
  gStyle->SetStatH(0.8);
  gStyle->SetStatX(0.85);
  gStyle->SetStatY(0.20);
  gStyle->SetPadBottomMargin(.1);

  TPaveText *t101 = new TPaveText(0.01,0.92,0.99,0.97,"Title");
  t101->SetBorderSize(0);
  t101->SetFillColor(10);
  t101->SetTextSize(.03);
  t101->AddText(" VTPC1: X - X_{fit} ");

//VTPC1_DX_distrMC[i]->Draw("HISTSAME");
  TCanvas c15("c15", "VTPC1 1D ResX distr in Sectors", 1024, 768);
  TPad *pad1 = new TPad("pad1","",0.03,0.02,0.97,0.90);
  pad1->Draw();
  t101->Draw();
  pad1->Divide(3,2);
  for (int i = 0; i < 6; i ++){
  pad1->cd(i+1);
  gPad->SetLeftMargin(0.08);
  gPad->SetRightMargin(0.0);
  gPad->SetTopMargin(0.0);
  gPad->SetBottomMargin(0.05);
  gPad->SetLogy();
  VTPC1_DX_distr[i]->SetTitle("VTPC1%d");
  VTPC1_DX_distr[i]->GetXaxis()->SetLabelSize(0.05);
  VTPC1_DX_distr[i]->GetYaxis()->SetLabelSize(0.05);
  VTPC1_DX_distr[i]->GetXaxis()->SetTitleOffset(-0.5);
  VTPC1_DX_distr[i]->GetXaxis()->SetTitleSize(0.05);
  VTPC1_DX_distr[i]->GetYaxis()->SetTitleOffset(-0.6);
  VTPC1_DX_distr[i]->GetYaxis()->SetTitleSize(0.07);
  VTPC1_DX_distr[i]->SetMarkerStyle(21);
  VTPC1_DX_distr[i]->SetMarkerColor(kRed);
  VTPC1_DX_distr[i]->SetMarkerSize(0.4);
  VTPC1_DX_distr[i]->GetYaxis()->SetTitle("N_{entries} ");
  VTPC1_DX_distr[i]->GetXaxis()->SetTitle("X - X_{fit}");
  VTPC1_DX_distr[i]->Draw("E");
  }

So here I have to change the name of the titles of each histogram. Because they already have names from the previous root file.
For example,
for VTPC1_DX_distr[i] , the titles come out to be VTPCResYDistSec1, VTPCResYDistSec2, VTPCResYDistSec3, VTPCResYDistSec4,VTPCResYDistSec5, VTPCResYDistSec6 (from the previous root file). Now I want to change the names as Sector1, Sector2, Sector3, Sector4, Sector5, Sector6.
So what code should I write to change the names of histograms.

Instead of this

VTPC1_DX_distr[i]->SetTitle(“VTPC1%d”);

try with

VTPC1_DX_distr[i]->SetTitle(Form("Sector%d", i+1));
2 Likes

Thank you. I solved the problem.

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