Problems with Legend plot

I encounter a problem when plot a legend for histogram. When run the macro via root, the root prompt appear errors, mostly" no matching member function for call to ‘AddEntry’". what’s up? How can I fix this problem?

// 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(.1,.7,.3,.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");
    //when compile root prompt indicate "no matching member function for call to 'AddEntry'".
   
	//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");

	c_divide->cd(2);
	dividend->GetYaxis()->SetRangeUser(0,2.49);
	c_divide->GetPad(2)->GetGridy();
	c_divide->GetPad(2)->SetRightMargin(0.01);
	dividend->Draw();

}

//I have solved the problem by myself.
//At fist the legend part code is:
TLegend leg(.1,.7,.3,.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”);
//After I change the code into:
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”);
// ,it works well now.

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