Hi,
This question is strongly related to this one. I could not reply to @couet because I was busy these days and I arrived late, the topic is closed.
So I get everything working fine except for TGraph
titles. This is mostly the code @couet shared (I modified titles to the ones I really want):
void CanvasPartition(TCanvas *C,const Int_t Nx = 2,const Int_t Ny = 2,
Float_t lMargin = 0.15, Float_t rMargin = 0.05,
Float_t bMargin = 0.15, Float_t tMargin = 0.05);
void test2()
{
gStyle->SetOptStat(0);
TCanvas *C = (TCanvas*) gROOT->FindObject("C");
if (C) delete C;
C = new TCanvas("C","canvas",1024,640);
C->SetFillStyle(4000);
// Number of PADS
const Int_t Nx = 4;
const Int_t Ny = 1;
// Margins
Float_t lMargin = 0.12;
Float_t rMargin = 0.05;
Float_t bMargin = 0.15;
Float_t tMargin = 0.03;
/*
Float_t lMargin = 0.04;
Float_t rMargin = 0.04;
Float_t bMargin = 0.08;
Float_t tMargin = 0.04;
*/
// Canvas setup
CanvasPartition(C,Nx,Ny,lMargin,rMargin,bMargin,tMargin);
TPad *pad[Nx][Ny];
for (Int_t i=0;i<Nx;i++) {
for (Int_t j=0;j<Ny;j++) {
auto gr = new TGraph();
gr->AddPoint(1,1);
gr->SetMinimum(0.6);
gr->SetMaximum(2);
if (i == 0){
gr->GetYaxis()->SetTitle("\\sigma/\\sigma_{SM}");
gr->SetTitle("#splitline{m_{ll} #in [101.0, 117.96] GeV}{#sigma_{SM} = (1876 #pm 8) #times 10^{-6} pb}");
}
if (i == 1){
gr->SetTitle("#splitline{m_{ll} #in [117.96, 140.9] GeV}{#sigma_{SM} = (662 #pm 3) #times 10^{-6} pb}");
}
if (i == 2){
gr->SetTitle("#splitline{m_{ll} #in [140.9, 181.36] GeV}{#sigma_{SM} = (446.0 #pm 1.5) #times 10^{-6} pb}");
}
if (i == 3){
gr->GetXaxis()->SetTitle("\\mathscr{O}");
gr->SetTitle("#splitline{m_{ll} #in [181.36, +#infty] GeV}{#sigma_{SM} = (517.9 #pm 1.9) #times 10^{-6} pb}");
}
C->cd(0);
// Get the pads previously created.
char pname[16];
sprintf(pname,"pad_%i_%i",i,j);
pad[i][j] = (TPad*) gROOT->FindObject(pname);
pad[i][j]->Draw();
pad[i][j]->cd();
// Size factors
Float_t xFactor = pad[0][0]->GetAbsWNDC()/pad[i][j]->GetAbsWNDC();
Float_t yFactor = pad[0][0]->GetAbsHNDC()/pad[i][j]->GetAbsHNDC();
char hname[16];
sprintf(hname,"h_%i_%i",i,j);
TH1F *hFrame = (TH1F*) gr->GetHistogram()->Clone(hname);
hFrame->Reset();
hFrame->Draw();
// y axis range
hFrame->GetYaxis()->SetRangeUser(0.0001,1.2*gr->GetHistogram()->GetMaximum());
// Format for y axis
hFrame->GetYaxis()->SetLabelFont(43);
hFrame->GetYaxis()->SetLabelSize(16);
hFrame->GetYaxis()->SetLabelOffset(0.02);
hFrame->GetYaxis()->SetTitleFont(43);
hFrame->GetYaxis()->SetTitleSize(16);
hFrame->GetYaxis()->SetTitleOffset(2);
hFrame->GetYaxis()->SetNdivisions(505);
// TICKS Y Axis
hFrame->GetYaxis()->SetTickLength(xFactor*0.04/yFactor);
// Format for x axis
hFrame->GetXaxis()->SetLabelFont(43);
hFrame->GetXaxis()->SetLabelSize(16);
hFrame->GetXaxis()->SetLabelOffset(0.02);
hFrame->GetXaxis()->SetTitleFont(43);
hFrame->GetXaxis()->SetTitleSize(16);
hFrame->GetXaxis()->SetTitleOffset(1);
hFrame->GetXaxis()->SetNdivisions(505);
// TICKS X Axis
hFrame->GetXaxis()->SetTickLength(yFactor*0.06/xFactor);
gr->Draw("P");
gPad->Update();
TPaveText* title = (TPaveText*)gPad->FindObject("title");
if (i==0) {
title->SetX1NDC(0.4);
title->SetY1NDC(0.8);
title->SetX2NDC(0.89);
title->SetY2NDC(0.99);
} else if (i==Nx-1) {
title->SetX1NDC(0.1);
title->SetY1NDC(0.8);
title->SetX2NDC(0.75);
title->SetY2NDC(0.99);
} else {
title->SetX1NDC(0.1);
title->SetY1NDC(0.8);
title->SetX2NDC(0.89);
title->SetY2NDC(0.99);
}
}
}
gPad->Modified();
gPad->Update();
C->cd();
C->Print("fig.png");
}
void CanvasPartition(TCanvas *C,const Int_t Nx,const Int_t Ny,
Float_t lMargin, Float_t rMargin,
Float_t bMargin, Float_t tMargin)
{
if (!C) return;
// Setup Pad layout:
Float_t vSpacing = 0.0;
Float_t vStep = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;
Float_t hSpacing = 0.0;
Float_t hStep = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx;
Float_t vposd,vposu,vmard,vmaru,vfactor;
Float_t hposl,hposr,hmarl,hmarr,hfactor;
for (Int_t i=0;i<Nx;i++) {
if (i==0) {
hposl = 0.0;
hposr = lMargin + hStep;
hfactor = hposr-hposl;
hmarl = lMargin / hfactor;
hmarr = 0.0;
} else if (i == Nx-1) {
hposl = hposr + hSpacing;
hposr = hposl + hStep + rMargin;
hfactor = hposr-hposl;
hmarl = 0.0;
hmarr = rMargin / (hposr-hposl);
} else {
hposl = hposr + hSpacing;
hposr = hposl + hStep;
hfactor = hposr-hposl;
hmarl = 0.0;
hmarr = 0.0;
}
for (Int_t j=0;j<Ny;j++) {
if (j==0) {
vposd = 0.0;
vposu = bMargin + vStep;
vfactor = vposu-vposd;
vmard = bMargin / vfactor;
vmaru = 0.0;
} else if (j == Ny-1) {
vposd = vposu + vSpacing;
vposu = vposd + vStep + tMargin;
vfactor = vposu-vposd;
vmard = 0.0;
vmaru = tMargin / (vposu-vposd);
} else {
vposd = vposu + vSpacing;
vposu = vposd + vStep;
vfactor = vposu-vposd;
vmard = 0.0;
vmaru = 0.0;
}
C->cd(0);
char name[16];
sprintf(name,"pad_%i_%i",i,j);
TPad *pad = (TPad*) gROOT->FindObject(name);
if (pad) delete pad;
pad = new TPad(name,"",hposl,vposd,hposr,vposu);
pad->SetLeftMargin(hmarl);
pad->SetRightMargin(hmarr);
pad->SetBottomMargin(vmard);
pad->SetTopMargin(vmaru);
pad->SetFrameBorderMode(0);
pad->SetBorderMode(0);
pad->SetBorderSize(0);
pad->Draw();
}
}
}
The result is:
If I use the margins I really want:
Float_t lMargin = 0.04;
Float_t rMargin = 0.04;
Float_t bMargin = 0.08;
Float_t tMargin = 0.04;
Titles do not have same size. And for the first case (probably) there is a small difference, due to the different titles.
Also the title for the left pad is not centered.
After some experimenting, I suppose this is related to TPaveText->SetX1NDC
and similars, but I do not find an explanation about this functions.
Please read tips for efficient and successful posting and posting code
ROOT Version: 6.24/06
Platform: linuxx8664gcc
Compiler: Not Provided