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?