# Legend plot problem

I add two legends to two historams. The fist legend works, but second legend doesn’t appear after the code compiled. My code is below. what’s up? How can I fix it?

``````// divide and add 1D histograms
void format_h(TH1F* h, int linecolor){
h->SetLineWidth(3);
h->SetLineColor(linecolor);
}

void macro6(){
auto sig_h=new TH1F("sig_h","Signal Hist",50,0,10);
auto gaus_h1=new TH1F("gaus_h1","Gauss Histo 1",30,0,10);
auto gaus_h2=new TH1F("gaus_h2","Gauss Histo 2",30,0,10);
auto bkg_h=new TH1F("exp_h","Exponetail Histo",50,0,10);

//simulate the measurements
TRandom3 rndgen;
for(int imeas=0;imeas<4000;imeas++){
bkg_h->Fill(rndgen.Exp(4));
if(imeas%4==0) gaus_h1->Fill(rndgen.Gaus(5,2));
if(imeas%4==0)gaus_h2->Fill(rndgen.Gaus(5,2));
if(imeas%10==0)sig_h->Fill(rndgen.Gaus(5,.2));
}

//format histograms
int i=0;
for(auto hist:{sig_h,bkg_h,gaus_h1,gaus_h2})
format_h(hist,1+i++);
//sum
auto sum_h=new TH1F(*bkg_h);
sum_h->Add(sig_h,1.0);
sum_h->SetTitle("Exponential +Gaussion;X variable;Y variable");
format_h(sum_h,kBlue);

auto c_sum=new TCanvas();
sum_h->Draw("hist");
bkg_h->Draw("SameHist");
sig_h->Draw("SameHist");

//add legend
TLegend leg(.7,.7,.9,.9,"Signal and Background");
leg.SetFillColor(0);
leg.AddEntry(sum_h,"Signal and Background");
leg.AddEntry(sig_h,"Signal");
leg.AddEntry(bkg_h,"Background");
leg.DrawClone("Same");

//divide
auto dividend=new TH1F(*gaus_h1);
dividend->Divide(gaus_h2);

//graphical maquillage
dividend->SetTitle(";X axis; Gaus Histo 1/Gaus Histo 2");
format_h(dividend,kOrange);
gaus_h1->SetTitle(";;Gaus Histo 1 and Gaus Histo 2");
gStyle->SetOptStat(0);

TCanvas* c_divide=new TCanvas();
c_divide->Divide(1,2,0,0);
c_divide->cd(1);
c_divide->GetPad(1)->SetRightMargin(0.01);
gaus_h1->DrawNormalized("Hist");
gaus_h2->DrawNormalized("HistSame");

TLegend leg2(.7,.7,.9,.9,"Gaus Hist");
leg2.SetFillColor(0);
leg2.AddEntry(gaus_h1,"Gaus Hist 1");
leg2.AddEntry(gaus_h2,"Gaus Hist 2");
leg2.Draw();
//this part legend doesn't appear in the digram after the code compiled.
c_divide->cd(2);
dividend->GetYaxis()->SetRangeUser(0,2.49);
c_divide->GetPad(2)->GetGridy();
c_divide->GetPad(2)->SetRightMargin(0.01);
dividend->Draw();

}
``````

Hi,

First of all, you are using TLegend::Draw() in the second case.
But object will be destroyed after end of `macro6()` function.
Just replace code for second legend:

``````   TLegend *leg2 = new TLegend(.7,.7,.9,.9,"Gaus Hist");
leg2->SetFillColor(0);
leg2->AddEntry(gaus_h1,"Gaus Hist 1");
leg2->AddEntry(gaus_h2,"Gaus Hist 2");
leg2->Draw();
``````

Regards,
Sergey

The following macro is working for me:

``````// divide and add 1D histograms
void format_h(TH1F* h, int linecolor){
h->SetLineWidth(3);
h->SetLineColor(linecolor);
}

void macro6(){
auto sig_h=new TH1F("sig_h","Signal Hist",50,0,10);
auto gaus_h1=new TH1F("gaus_h1","Gauss Histo 1",30,0,10);
auto gaus_h2=new TH1F("gaus_h2","Gauss Histo 2",30,0,10);
auto bkg_h=new TH1F("exp_h","Exponetail Histo",50,0,10);

//simulate the measurements
TRandom3 rndgen;
for(int imeas=0;imeas<4000;imeas++){
bkg_h->Fill(rndgen.Exp(4));
if(imeas%4==0) gaus_h1->Fill(rndgen.Gaus(5,2));
if(imeas%4==0)gaus_h2->Fill(rndgen.Gaus(5,2));
if(imeas%10==0)sig_h->Fill(rndgen.Gaus(5,.2));
}

//format histograms
int i=0;
for(auto hist:{sig_h,bkg_h,gaus_h1,gaus_h2})
format_h(hist,1+i++);
//sum
auto sum_h=new TH1F(*bkg_h);
sum_h->Add(sig_h,1.0);
sum_h->SetTitle("Exponential +Gaussion;X variable;Y variable");
format_h(sum_h,kBlue);

auto c_sum=new TCanvas();
sum_h->Draw("hist");
bkg_h->Draw("SameHist");
sig_h->Draw("SameHist");

//add legend
auto leg = new TLegend(.7,.7,.9,.9,"Signal and Background");
leg->SetFillColor(0);
leg->AddEntry(sum_h,"Signal and Background");
leg->AddEntry(sig_h,"Signal");
leg->AddEntry(bkg_h,"Background");
leg->Draw();

//divide
auto dividend=new TH1F(*gaus_h1);
dividend->Divide(gaus_h2);

//graphical maquillage
dividend->SetTitle(";X axis; Gaus Histo 1/Gaus Histo 2");
format_h(dividend,kOrange);
gaus_h1->SetTitle(";;Gaus Histo 1 and Gaus Histo 2");
gStyle->SetOptStat(0);

// 2nd canvas
TCanvas* c_divide=new TCanvas();
c_divide->Divide(1,2,0,0);
c_divide->cd(1);
c_divide->GetPad(1)->SetRightMargin(0.01);
gaus_h1->DrawNormalized("Hist");
gaus_h2->DrawNormalized("HistSame");

auto leg2 = new TLegend(.7,.7,.9,.9,"Gaus Hist");
leg2->SetFillColor(0);
leg2->AddEntry(gaus_h1,"Gaus Hist 1");
leg2->AddEntry(gaus_h2,"Gaus Hist 2");
leg2->Draw();

//this part legend doesn't appear in the digram after the code compiled.
c_divide->cd(2);
dividend->GetYaxis()->SetRangeUser(0,2.49);
c_divide->GetPad(2)->GetGridy();
c_divide->GetPad(2)->SetRightMargin(0.01);
dividend->Draw();
}
``````

Thanks. I do as your advice, and it works.

Thanks for your attention and your help. the problem is solved now.

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