[ROOT] TLegend

Dear ROOTers,

I have a problem when creating a legend. I try to draw in one single canvas different histograms referring to a specific variable, which I take from the subfolders of a root file. The problem is that inside the legend, the lines do not correspond to the histograms they refer to, but are all drawn in black.
How can I fix this?
Thank you.

PS: here I post part of the code I use (is there anything wrong in it?)

void compare ()
{
gROOT->SetStyle(“Plain”) ;
TCanvas *c1 = new TCanvas(“c1”,“Variable”,1) ;
leg = new TLegend(0.6,0.6,0.9,0.9) ;

TFile f(“myfile.root”) ;

f.cd(“subfolder1”) ;
variable->SetLineColor(kBlue) ;
leg->AddEntry(variable,“h1”,“l”) ;
HemiMax0->DrawCopy() ;

f.cd(“subfolder2”) ;
variable->SetLineColor(kRed) ;
leg->AddEntry(variable,“h2”,“l”) ;
variable->DrawCopy(“same”) ;

leg->SetHeader(“Variable”) ;
leg->Draw() ;

return ;
}

and I also attach the picture I obtain.

Hi,

I think my problem can be solved just by changing the title of each histogram, and then using the command “BuildLegend”. In fact, this works!
However, any other suggestion will be well accepted !

Manuel

Try to use the AddEntry method which add the object using the object pointer.

Hi dear all,

I have a strange problem. I have the following code and as I run the code the legend (whatever the style of the legend is; line or box) in the plot is just black coloured. Black bordered box or black line. But due to the adjustment of histogram colors, the indication for histogram “NLO…” must be blue and the indication for histogram “LO…” must be red in the Legend. I actually could not find where is the problem . Doess anybody have a suggestion on top of this problem? Thanks in advance!

TH1F* NLO_e_C6_C0_Lepton_pT_hist=new TH1F("Electron Channel", "[MC@NLO C6/C0]", 50, 0, 250);
NLO_e_C6_C0_Lepton_pT_hist->GetXaxis()->SetTitle("Electron p_{T}");
NLO_e_C6_C0_Lepton_pT_hist->GetYaxis()->SetTitle("# of Entries");
NLO_e_C6_C0_Lepton_pT_hist->GetYaxis()->SetRangeUser(-0.5, 1.0);
NLO_e_C6_C0_Lepton_pT_hist->SetMarkerStyle(7);
NLO_e_C6_C0_Lepton_pT_hist->SetMarkerColor(kBlue);
NLO_e_C6_C0_Lepton_pT_hist->Sumw2();

TH1F* NLO_mu_C6_C0_Lepton_pT_hist=new TH1F("Muon Channel", "[MC@NLO C6/C0]", 50, 0, 250);
NLO_mu_C6_C0_Lepton_pT_hist->GetXaxis()->SetTitle("Muon p_{T}");
NLO_mu_C6_C0_Lepton_pT_hist->GetYaxis()->SetTitle("# of Entries");
NLO_mu_C6_C0_Lepton_pT_hist->GetYaxis()->SetRangeUser(-0.5, 1.0);
NLO_mu_C6_C0_Lepton_pT_hist->SetMarkerStyle(7);
NLO_mu_C6_C0_Lepton_pT_hist->SetMarkerColor(kBlue);
NLO_mu_C6_C0_Lepton_pT_hist->Sumw2();

TH1F* LO_e_C6_C0_Lepton_pT_hist=new TH1F("Electron Channel", "[AcerMC C6/C0] & [MC@NLO C6/C0]", 50, 0, 250);
LO_e_C6_C0_Lepton_pT_hist->GetXaxis()->SetTitle("Electron p_{T}");
LO_e_C6_C0_Lepton_pT_hist->GetYaxis()->SetTitle("# of Entries");
LO_e_C6_C0_Lepton_pT_hist->GetYaxis()->SetRangeUser(-0.5, 1.0);
LO_e_C6_C0_Lepton_pT_hist->SetMarkerStyle(7);
LO_e_C6_C0_Lepton_pT_hist->SetMarkerColor(kRed);
LO_e_C6_C0_Lepton_pT_hist->Sumw2();

TH1F* LO_mu_C6_C0_Lepton_pT_hist=new TH1F("Muon Channel", "[AcerMC C6/C0] & [MC@NLO C6/C0]", 50, 0, 250);
LO_mu_C6_C0_Lepton_pT_hist->GetXaxis()->SetTitle("Muon p_{T}");
LO_mu_C6_C0_Lepton_pT_hist->GetYaxis()->SetTitle("# of Entries");
LO_mu_C6_C0_Lepton_pT_hist->GetYaxis()->SetRangeUser(-0.5, 1.0);
LO_mu_C6_C0_Lepton_pT_hist->SetMarkerStyle(7);
LO_mu_C6_C0_Lepton_pT_hist->SetMarkerColor(kRed);
LO_mu_C6_C0_Lepton_pT_hist->Sumw2();

TLegend leg1(0.40,0.66,0.56,0.76);
leg1.SetBorderSize(0.05);
leg1.SetTextFont(72);
leg1.SetTextSize(0.025);

leg1.AddEntry(LO_e_C6_C0_Lepton_pT_hist, "AcerMC", "l" );
leg1.AddEntry(NLO_e_C6_C0_Lepton_pT_hist, "MCatNLO", "l" );

TLegend leg2(0.4,0.66,0.56,0.76);
leg2.SetBorderSize(0.05);
leg2.SetTextFont(72);
leg2.SetTextSize(0.025);

leg2.AddEntry(LO_mu_C6_C0_Lepton_pT_hist, "AcerMC", "l" );
leg2.AddEntry(NLO_mu_C6_C0_Lepton_pT_hist, "MCatNLO", "l" );

    .
    .
    .
    .

c13.cd();
LO_e_C6_C0_Lepton_pT_hist->Draw();
NLO_e_C6_C0_Lepton_pT_hist->Draw("same");
leg1.Draw("same");
c13.Print("./plots/Lepton_Study/NLO_LO_e_C6_C0_Lepton_pT_hist.pdf","pdf");
c13.Print("./plots/Lepton_Study/NLO_LO_e_C6_C0_Lepton_pT_hist.eps","eps");

c14.cd();
LO_mu_C6_C0_Lepton_pT_hist->Draw();
NLO_mu_C6_C0_Lepton_pT_hist->Draw("same");
leg2.Draw("same");
c14.Print("./plots/Lepton_Study/NLO_LO_mu_C6_C0_Lepton_pT_hist.pdf","pdf");
c14.Print("./plots/Lepton_Study/NLO_LO_mu_C6_C0_Lepton_pT_hist.eps","eps");

Can you send a small running macro reproducing your problem ?

Hi,
Try in this way:

f.cd("subfolder1") ;
variable->SetLineColor(kBlue) ;
histclone = HemiMax0->DrawCopy() ;
leg->AddEntry(histclone,"h1","l") ;

Zhiyi.