Two plotting aesthetics questions (TGraph and TAxis)

I have two questions relating to a plotting macro, code below and output pdf attached.

(1) When I draw TGraphs on a dummy TH2D (for formatting), why doesn’t my TGraph continue to the x-maximum of the plot range? As a corollary, I am puzzled why i have to subtract 1 in the TGraph range to make it properly align.

(2) How can I nudge up my second y-axis on the right side? For instance, is it possible to add blank spaces or tabs to the content of TAxis titles?

Many thanks in advance for the help.

-Will

ActivityAndRates.pdf (14.4 KB)
Rates.C (3.8 KB)

void Rates(){

TCanvas *c1 = new TCanvas(“c1”,“”,700,500);

gStyle->SetOptStat(0);
gStyle->SetLegendBorderSize(0);

TH2D *hDummy = new TH2D(“hDummy”,“”,1000,0,37,1000,0,1.30);
hDummy->Draw();
hDummy->GetXaxis()->SetTitle(“Days”);
hDummy->GetXaxis()->CenterTitle();
hDummy->GetXaxis()->SetTitleSize(0.08);
hDummy->GetXaxis()->SetTitleOffset(0.5);
hDummy->GetYaxis()->SetTitle(“Activity [GBq]”);
hDummy->GetYaxis()->CenterTitle();
hDummy->GetYaxis()->SetTitleSize(0.08);
hDummy->GetYaxis()->SetTitleOffset(0.5);

TGaxis *axis = new TGaxis(37,0.0, 37, 1.15,0,1.5,51,“+L”);
axis->SetNdivisions(505);
axis->SetLabelFont(gStyle->GetLabelFont(“Y”));
axis->SetTitleFont(gStyle->GetTitleFont(“Y”));
axis->SetTitle(“Background Rate [MHz]”);
axis->CenterTitle();
axis->SetTitleSize(0.08);
axis->SetTitleOffset(0.5);
axis->Draw();

TLine *line = new TLine(10.5,0,10.5,1.30);
line->SetLineStyle(2);
line->Draw(“same”);

double Act_Zr88_0 = 1.11; // GBq
double tHalf_Zr88 = 83.4; // days
double tHalf_Y88 = 106.629; // days
double Eff_Zr88 = 0.0002;
double Eff_Y88 = 0.0035;

TF1 *fAct_Zr88 = new TF1(“fAct_Zr88”,“[0]*exp(-log(2.0)*x/[1])”,-1,200);
fAct_Zr88->SetParameters(Act_Zr88_0, tHalf_Zr88);
fAct_Zr88->SetLineColor(kBlue+2);
fAct_Zr88->SetLineWidth(4);
fAct_Zr88->Draw(“same”);

TF1 fAct_Y88 = new TF1(“fAct_Y88”,"(1.0/[1])/(1.0/[2]-1.0/[1])[0]*(exp(-log(2.0)*x/[1])-exp(-log(2.0)*x/[2]))",-1,200);
fAct_Y88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88);
fAct_Y88->SetLineColor(kRed+2);
fAct_Y88->SetLineWidth(4);
fAct_Y88->Draw(“same”);

TF1 *fAct_Tot = new TF1(“fAct_Tot”,“[0]exp(-log(2.0)x/[1])+(1.0/[1])/(1.0/[2]-1.0/[1])[0](exp(-log(2.0)*x/[1])-exp(-log(2.0)*x/[2]))”,-1,200);
fAct_Tot->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88);
fAct_Tot->SetLineColor(kBlack);
fAct_Tot->SetLineWidth(4);
fAct_Tot->Draw(“same”);

TF1 fBkg_Zr88 = new TF1(“fBkg_Zr88”,"1000[3]*[0]*exp(-log(2.0)*x/[1])",9.5,200);
fBkg_Zr88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88);
fBkg_Zr88->SetLineColor(kBlue+2);
fBkg_Zr88->SetLineWidth(4);
fBkg_Zr88->SetLineStyle(7);
fBkg_Zr88->Draw(“same”);

TF1 fBkg_Y88 = new TF1(“fBkg_Y88”,"1000[4](1.0/[1])/(1.0/[2]-1.0/[1])[0]*(exp(-log(2.0)*x/[1])-exp(-log(2.0)*x/[2]))",9.5,200);
fBkg_Y88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88, Eff_Y88);
fBkg_Y88->SetLineColor(kRed+2);
fBkg_Y88->SetLineWidth(4);
fBkg_Y88->SetLineStyle(7);
fBkg_Y88->Draw(“same”);

TF1 fBkg_Tot = new TF1(“fBkg_Tot”,"1000[3][0]exp(-log(2.0)x/[1])+1000[4](1.0/[1])/(1.0/[2]-1.0/[1])[0]*(exp(-log(2.0)*x/[1])-exp(-log(2.0)*x/[2]))",9.5,200);
fBkg_Tot->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88, Eff_Y88);
fBkg_Tot->SetLineColor(kBlack);
fBkg_Tot->SetLineWidth(4);
fBkg_Tot->SetLineStyle(7);
fBkg_Tot->Draw(“same”);

TLegend* legZr88 = new TLegend(0.33, 0.81, .50, .89);
legZr88->SetTextSize(0.04);
legZr88->AddEntry(fAct_Zr88, “^{88}Zr Activity”, “l”);
legZr88->AddEntry(fBkg_Zr88, “^{88}Zr Rate”, “l”);
legZr88->SetMargin(0.3);
legZr88->Draw(“same”);

TLegend* legY88 = new TLegend(0.52, 0.81, .69, .89);
legY88->SetTextSize(0.04);
legY88->AddEntry(fAct_Y88, “^{88}Y Activity”, “l”);
legY88->AddEntry(fBkg_Y88, “^{88}Y Rate”, “l”);
legY88->SetMargin(0.3);
legY88->Draw(“same”);

TLegend* legTot = new TLegend(0.70, 0.81, .89, .89);
legTot->SetTextSize(0.04);
legTot->AddEntry(fAct_Tot, “Total Activity”, “l”);
legTot->AddEntry(fBkg_Tot, “Total Rate”, “l”);
legTot->SetMargin(0.3);
legTot->Draw(“same”);

TLegend* legStart = new TLegend(0.05, 0.02, .15, .06);
legStart->SetTextSize(0.04);
legStart->SetHeader(“5 AUG 24”);
legStart->Draw(“same”);

TLegend* legBeam = new TLegend(0.27, 0.02, .37, .06);
legBeam->SetTextSize(0.04);
legBeam->SetHeader(“16 AUG 24”);
legBeam->Draw(“same”);

TLegend* legEnd = new TLegend(0.85, 0.02, .95, .06);
legEnd->SetTextSize(0.04);
legEnd->SetHeader(“8 SEP 24”);
legEnd->Draw(“same”);

c1->SaveAs(“ActivityAndRates.pdf”);
}

For 1, not sure what you mean. Also, there are no TGraphs in your code.
For 2, “How can I nudge up my second y-axis on the right side?” do you mean the title of the axis (judging by the second question)? did you try simply adding the spaces to the title text?

axis->SetTitle("     Background Rate [MHz]");

Your macro produces this plot:

As @dastudillo said there is no TGraph in your code, only TF1. I do not see why you should have ranges problem. What’s wrong with the right title ? you want to center it on the axis ?
(your axis titles seem a bit too big though). Also note that to draw an empty frame we have TPad::DrawFrame, and that the option “same” does not exist for TLine::Draw() it will be ignored.

@dastudillo and @couet, thank you for your replies so far.

@couet, to your response ‘I do not see why you should have ranges problem.’ Please note that the TF1s must have a starting range as 9.5 in order to start at 10.5. Why is this? Also, please note that these functions do not continue to the right limit of the plot. Why is this? Are there simple ways to fix this?

@dastudillo, to your point, I had mistakenly thought that the leading blank spaces in my title were being ignored. I see now that I was wrong. Apologies.

My apologies also for mixing up TGraph with TF1.

Thanks,
Will

For the function line issue, it’s because your functions are defined from 9.5 (or 10.5 as you would like) to 200 (even though you are only plotting up to 37), and by default ROOT draws TF1 by sampling 100 points in x, so there aren’t enough points; you can change that number with TF1::SetNpx:
https://root.cern/doc/master/classTF1.html#a6a04facaa58306988a3189b4e8713aa5
For your case, you could increase that npx (around 500 seems to look ok), or reduce the upper limit of your functions (e.g. to 40), or a combination of both. Using DrawFrame as @couet suggested, and these 2 options:

  TCanvas *c1 = new TCanvas("c1","",700,500);

  gStyle->SetOptStat(0);
  gStyle->SetLegendBorderSize(0);

  c1->DrawFrame(0,0,37,1.30);

  TGaxis *axis = new TGaxis(37,0.0, 37, 1.15,0,1.5,51,"+L");
  axis->SetNdivisions(505);
  axis->SetLabelFont(gStyle->GetLabelFont("Y"));
  axis->SetTitleFont(gStyle->GetTitleFont("Y"));
  axis->SetTitle("    Background Rate [MHz]");
  axis->CenterTitle();
  axis->SetTitleSize(0.08);
  axis->SetTitleOffset(0.5);
  axis->Draw();

  TLine *line = new TLine(10.5,0,10.5,1.30);
  line->SetLineStyle(2);
  line->Draw();

  double Act_Zr88_0 = 1.11; // GBq
  double tHalf_Zr88 = 83.4; // days
  double tHalf_Y88 = 106.629; // days
  double Eff_Zr88 = 0.0002;
  double Eff_Y88 = 0.0035;

  // BLUE, increasing Npx
  TF1 *fBkg_Zr88 = new TF1("fBkg_Zr88","1000*[3]*[0]*exp(-log(2.0)*x/[1])",10.5,200);
  fBkg_Zr88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88);
  fBkg_Zr88->SetLineColor(kBlue+2);
  fBkg_Zr88->SetLineWidth(4);
  fBkg_Zr88->SetLineStyle(7);
  fBkg_Zr88->SetNpx(500);
  fBkg_Zr88->Draw("same");

  // RED, reducing range of TF1
  TF1 *fBkg_Y88 = new TF1("fBkg_Y88","1000*[4]*(1.0/[1])/(1.0/[2]-1.0/[1])*[0]*(exp(-log(2.0)*x/[1])-exp(-log(2.0)*x/[2]))",10.5,40);
  fBkg_Y88->SetParameters(Act_Zr88_0, tHalf_Zr88, tHalf_Y88, Eff_Zr88, Eff_Y88);
  fBkg_Y88->SetLineColor(kRed+2);
  fBkg_Y88->SetLineWidth(4);
  fBkg_Y88->SetLineStyle(7);
  fBkg_Y88->Draw("same");

@dastudillo, your answers have been quite informative to me on quite a few facets of this plotting macro from DrawFrame to SetNpx. Thank you for your time!