Add a legend on a 2D histogram, including TProfile

I am creating a 2D histogram and I would like to make a legend of it.
On the same canvas I am drawing a TProfile of the histogram and I would like it to be on the legend.
However although I can get the entry, the colout doesn’t match, which is the case when plotting other functions as well.

My code is

[code]TH2F* mean_Gamma_Flash() {
gStyle->SetPalette(52); //Use Grayscale palette–51-59

//Load File.
TFile *f = new TFile(“FIMG5_300.root”);

//Loop over each histogram and store pointer in vector.
std::vector<TH1F*> hists;
for (int i=0;i<305;i++) {
TH1F hIndv = (TH1F)f->Get(Form(“FIMG5_EV_%d;1”,i));
if (hIndv)
hists.push_back(hIndv);
}

//Compute average
TH2F *hAvg = Average(hists);
TH2F *hThres = GetThreshold(hAvg,5);
TProfile *hProf = hThres->ProfileX(“hProf”);//Average histogram From cut histogram
TProfile *hProfFull = hAvg->ProfileX(“hProfFull”);//Average histogram From Full histogram
TH1F *hFreq = GetFrequent(hAvg);
TH1F *hFreqThres = GetFrequent(hThres);

//Turn off stats
hAvg->SetStats(kFALSE);

//Draw the 2D hist with the average prof on top.
hProf->SetLineColor(kRed);
hProf->SetMarkerColor(kRed);
hProfFull->SetLineColor(kGreen+1);
hProfFull->SetMarkerColor(kGreen+1);
hFreq->SetLineColor(kBlue);
hFreqThres->SetLineColor(kCyan);
hAvg->Draw(“COLZ”);
hThres->Draw(“COLZ”);
hProf->Draw(“SAME”);
hFreq->Draw(“SAME”);
hFreqThres->Draw(“SAME”);
hProfFull->Draw(“SAME”);

//Use a log scale in Z
gPad->SetLogz();
gPad->Update();

TString histfilename = "mean_gamma.txt";
SingleExportAscii(hProf,histfilename);

leg = new TLegend(0.6,0.1,0.9,0.4);
leg->AddEntry(hAvg,“2D-Stack”);
leg->AddEntry(hThres,“2D-Stack after cut”);
leg->AddEntry(“hProFull”,“Mean Value without cut”);
leg->AddEntry(“hPro”,“Mean Value with cut”);
leg->AddEntry(“hFreq”,“Most Frequent Value”);
leg->Draw();

return hAvg;
}

TH2F Average(std::vector<TH1F> hists) {
TH2F *hAvg = new TH2F(“hAvg”,“Avg”,8000,0,8000,300,0,300);
for (size_t i=0;i < hists.size(); i++) {
for (int bin = 0; bin < hists[i]->GetNbinsX(); bin++) {
int resultBin = hAvg->FindBin(bin, hists[i]->GetBinContent(bin));
int binContent = hAvg->GetBinContent(resultBin) + 1;
hAvg->SetBinContent(resultBin,binContent);
}
}
return hAvg;
}

TH1F* GetFrequent(TH2F* hist2D) {
TH1F* hFreq = new TH1F(“hFreq”,“Freq”,hist2D->GetNbinsX(),hist2D->GetXaxis()->GetBinLowEdge(1),hist2D->GetXaxis()->GetBinLowEdge(hist2D->GetNbinsX()) + hist2D->GetXaxis()->GetBinWidth(hist2D->GetNbinsX()));

for (int xBin = 1; xBin <= hist2D->GetNbinsX(); ++xBin) {
double maxYBin = 0;
for (int yBin = 1; yBin <= hist2D->GetNbinsY(); ++yBin) {
if (hist2D->GetBinContent(xBin,yBin) > hist2D->GetBinContent(xBin,maxYBin)) maxYBin = yBin;
}
hFreq->SetBinContent(xBin,maxYBin);
}
return hFreq;
}

TH2F* GetThreshold(TH2F* hist2D, int threshold) {
//TH2F* hThres = hist2D->Clone();
TH2F* hThres = new TH2F(*hist2D);
for (int xBin = 1; xBin <= hist2D->GetNbinsX(); ++xBin) {
for (int yBin = 1; yBin <= hist2D->GetNbinsY(); ++yBin) {
if (hist2D->GetBinContent(xBin,yBin) < threshold) hThres->SetBinContent(xBin,yBin,0);
}
}
return hThres;
}[/code]

In particular my legend code is

leg = new TLegend(0.6,0.1,0.9,0.4); leg->AddEntry(hAvg,"2D-Stack"); leg->AddEntry(hThres,"2D-Stack after cut"); leg->AddEntry("hProFull","Mean Value without cut"); leg->AddEntry("hPro","Mean Value with cut"); leg->AddEntry("hFreq","Most Frequent Value"); leg->Draw();

My output is the following

Any idea on how to create a proper legend?

From
root.cern.ch/root/htmldoc/TLegend.html
note:

also try to use the object pointer instead of its name in AddEntry

Which root version are you using ?

Thank you for your answer.
I am using version 5-34-19-251 in lxplus.

The way I create the mean value is using TProfile, which creates a TH1.
My code to create them is the following

TProfile *hProf = hThres->ProfileX("hProf");//Average histogram From cut histogram TProfile *hProfFull = hAvg->ProfileX("hProfFull");//Average histogram From Full histogram

The way I create the legend is the following

leg = new TLegend(0.6,0.1,0.9,0.4); leg->AddEntry("hProFull","Mean Value without cut"); leg->AddEntry("hPro","Mean Value with cut"); leg->AddEntry("hFreq","Most Frequent Value"); leg->Draw();

If I replace “hProFull” with hProFull I get an error that

Can you provide a small running macro reproducing the issue ?

A working code is the following

[code]#include “Riostream.h”
#include
#include
#include “TH1F.h”
#include “TH2F.h”
#include “TProfile.h”

TH2F* test_legend() {
TCanvas *c1 = new TCanvas(“c1”, “c1”,900,900);
gStyle->SetOptStat(0);

// Create, fill and project a 2D histogram.
TH2F h2 = new TH2F(“h2”,"",40,-4,4,40,-20,20);
Float_t px, py;
for (Int_t i = 0; i < 25000; i++) {
gRandom->Rannor(px,py);
h2->Fill(px,5
py);
}
TProfile * profh2X = h2->ProfileX(“profX”);
profh2X->SetLineColor(kRed);
TProfile * profh2Y = h2->ProfileY(“profY”);
profh2Y->SetLineColor(kBlue);

h2->Draw(“COLZ”);
profh2X->Draw(“SAME”);
profh2Y->Draw(“SAME”);

leg = new TLegend(0.6,0.1,0.9,0.4);
leg->AddEntry(profh2X,“Red Line”);
leg->AddEntry(profh2Y,“Blue Line”);
leg->Draw();

return h2;
}[/code]

I found out what was wrong…
I named my histograms “hProf” but I was adding in the legend “hPro”…
Such a stupid mistake…

Thank you very much for your time and sorry for the inconvenience!

good :smiley: