Legend not displayed

Hi,

I don’t know why but in my current plot the legend won’t show up. Tried several things but I can’t nail it down…
Can you please have a look at the code?
Thanks a lot in advance!

#include <TGraphErrors.h>

int time_detection()
{
gROOT->Reset();
TCanvas *c = new TCanvas(“c”,“Elapsed time”,0,0,1200,700);
gStyle->SetOptStat(0);
gStyle->SetPadGridX(kTRUE);
gStyle->SetPadGridY(kTRUE);

Double_t x_3[3] = {0-0.3, 6-0.3, 12-0.3};
Double_t z_3[3] = {2550, 2542, 2675};
Double_t z_3rms[3] = {1811, 1831, 1815};
TGraphErrors* z_3graph = new TGraphErrors(3, x_3, z_3, 0, z_3rms);
z_3graph->SetName(“z_3graph”);

Double_t x_2[4] = {-2-0.2,0-0.2, 6-0.2, 12-0.2};
Double_t z_2[4] = {2562, 2555, 2495, 2724};
Double_t z_2rms[4] = {1842, 1840, 1783, 2030};
TGraphErrors* z_2graph = new TGraphErrors(4, x_2, z_2, 0, z_2rms);
z_2graph->SetName(“z_2graph”);

Double_t x_1[5] = {-4-0.1, -2-0.1, 0-0.1, 6-0.1, 12-0.1};
Double_t z_1[5] = {2503, 2390, 2541, 2635, 2686};
Double_t z_1rms[5] = {1740, 1668, 1798, 1822, 1819};
TGraphErrors* z_1graph = new TGraphErrors(5, x_1, z_1, 0, z_1rms);
z_1graph->SetName(“z_1graph”);

Double_t x1[5] = {-4+0.1, -2+0.1,0+0.1, 6+0.1, 12+0.1};
Double_t z1[5] = {2631, 2462, 2504, 2744, 2556};
Double_t z1rms[5] = {1882, 1716, 1767, 1959, 1837};
TGraphErrors* z1graph = new TGraphErrors(5, x1, z1, 0, z1rms);
z1graph->SetName(“z1graph”);

Double_t x2[4] = {-2+0.2,0+0.2, 6+0.2, 12+0.2};
Double_t z2[4] = {2522, 2569, 2722, 2547};
Double_t z2rms[4] = {1773, 1819, 2019, 1840};
TGraphErrors* z2graph = new TGraphErrors(4, x2, z2, 0, z2rms);
z2graph->SetName(“z2graph”);

Double_t x3[3] = {0+0.3, 6+0.3, 12+0.3};
Double_t z3[3] = {2593, 2708, 2519};
Double_t z3rms[3] = {1830, 1869, 1836};
TGraphErrors* z3graph = new TGraphErrors(3, x3, z3, 0, z3rms);
z3graph->SetName(“z3graph”);

z_3graph->GetXaxis()->SetRangeUser(-5,13);
z_1graph->GetXaxis()->SetTitle(“x / mm”);
z_1graph->GetYaxis()->SetTitle(“time / ps”);
z_1graph->SetTitle(“Elapsed time from generation until detection”);

z_1graph->SetMarkerColor(kGreen);
z_2graph->SetMarkerColor(kYellow+1);
z_3graph->SetMarkerColor(kRed+2);
z1graph->SetMarkerColor(kCyan+2);
z2graph->SetMarkerColor(kBlue+3);
z3graph->SetMarkerColor(kMagenta+3);

z_1graph->SetMarkerStyle(20);
z_2graph->SetMarkerStyle(21);
z_3graph->SetMarkerStyle(22);
z1graph->SetMarkerStyle(23);
z2graph->SetMarkerStyle(33);
z3graph->SetMarkerStyle(34);

z_1graph->SetLineWidth(2);
z_2graph->SetLineWidth(2);
z_3graph->SetLineWidth(2);
z1graph->SetLineWidth(2);
z2graph->SetLineWidth(2);
z3graph->SetLineWidth(2);

z_1graph->SetLineColor(kGreen);
z_2graph->SetLineColor(kYellow+1);
z_3graph->SetLineColor(kRed+2);
z1graph->SetLineColor(kCyan+2);
z2graph->SetLineColor(kBlue+3);
z3graph->SetLineColor(kMagenta+3);

z_1graph->SetMarkerSize(2);
z_2graph->SetMarkerSize(2);
z_3graph->SetMarkerSize(2);
z1graph->SetMarkerSize(2);
z2graph->SetMarkerSize(2);
z3graph->SetMarkerSize(2);

leg = new TLegend(0.1,0.7,0.48,0.9);
leg->SetTextSize(0.03);
leg->SetHeader("");
leg->AddEntry(z_3graph, “-3 mm”, “l”);
leg->AddEntry(z_2graph, “-2 mm”, “l”);
leg->AddEntry(z_1graph, “-1 mm”, “l”);
leg->AddEntry(z1graph, “1 mm”, “l”);
leg->AddEntry(z2graph, “2 mm”, “l”);
leg->AddEntry(z3graph, “3 mm”, “l”);
leg->SetBorderSize(2);
leg->Draw();

// gStyle->SetPalette(1);
z_1graph->Draw(“ALP”);
z_2graph->Draw(“LP”);
z_3graph->Draw(“LP”);
z3graph->Draw(“LP”);
z2graph->Draw(“LP”);
z1graph->Draw(“LP”);

return 0;
}

Move leg->Draw(); to after z1graph->Draw(“LP”);.

1 Like

Indeed… so easy and I made it only more difficult :smiley:
Thanks a lot! Especially for the fast response :slight_smile:

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