I am attaching my program where I have made a multi graph on same pad and also added a new pad with another graph and second Y- Axis. Now I want to add legend to these graphs. I wrote the following code but the legends are not being printed.
{
auto c1 = new TCanvas("c1","photon flux for varying Tungsten thickness",1500,500); //(name, title, xlength, y length)
auto pad = new TPad("pad","",0,0,1,1); //(name, title, xlow, ylow, xup, yup)
pad->SetGrid();
pad->Draw();
pad->SetLogy(1);
pad->cd();
// create multiple graphs
TGraph *g1 = new TGraph("phflx_0.1.dat");
g1->SetLineColor(1);
g1->SetLineWidth(2);
TGraph *g2 = new TGraph("phflx_0.2.dat");
g2->SetLineColor(5);
g2->SetLineWidth(2);
TGraph *g5 = new TGraph("phflx_0.5.dat");
g5->SetLineColor(8);
g5->SetLineWidth(2);
TGraph *g8 = new TGraph("phflx_0.8.dat");
g8->SetLineColor(2);
g8->SetLineWidth(2);
TGraph *g15 = new TGraph("phflx_1.5.dat");
g15->SetLineColor(4);
g15->SetLineWidth(2);
// create a multigraph and draw it
TMultiGraph *mg = new TMultiGraph();
mg->Add(g1);
mg->Add(g2);
mg->Add(g5);
mg->Add(g8);
mg->Add(g15);
mg->SetTitle("Photon Flux for various Target Thickness; Photon Energy (MeV); Photon Flux per unit area per electron");
mg->Draw("ALP");
//create a transparent pad drawn on top of the main pad
c1->cd();
auto overlay = new TPad("overlay","",0,0,1,1);
overlay->SetFillStyle(4000);
overlay->SetFillColor(0);
overlay->SetFrameFillStyle(4000);
overlay->Draw();
overlay->cd();
//Create the second graph
auto gdr= new TGraph("cross_section.dat");
gdr->SetMarkerColor(kRed);
gdr->SetMarkerStyle(5);
gdr->SetName("gdr");
TH1F *hframe = overlay->DrawFrame(-1.4,0,31.4,200);
hframe->GetXaxis()->SetLabelOffset(99);
hframe->GetYaxis()->SetLabelOffset(99);
hframe->GetYaxis()->SetTickSize(0);
gdr->Draw("PL");
//Draw an axis on the right side
TGaxis *axis = new TGaxis(31.4,0,31.4,200,0,200,80506,"+L");
axis->SetLineColor(kRed);
axis->SetLabelColor(kRed);
axis->SetLabelOffset(0.02);
axis->Draw();
//Add Legend
TLegend *legend = new TLegend(0.1,0.7,0.48,0.9);
legend->SetHeader("Header","C"); // option "C" allows to center the header
legend->AddEntry("g1","0.1 radiation length","l");
legend->AddEntry("g2","0.2 radiation length","l");
legend->AddEntry("g5","0.5 radiation length","l");
legend->AddEntry("g8","0.8 radiation length","l");
legend->AddEntry("g15","1.5 radiation length","l");
legend->Draw();
}
Kindly Help.
_ROOT Version: 5.34/36
_Platform: CentOS &
_Compiler: Red Hat 4.8.5-28