STacks, Legendas and transparencies

Dear developers
I discovered some strange interaction between THStacks and legends that I wanted to share with you, also to understand if it’s a bug, a feature or an abuse on my side. The following script reproduces the problem:

void testscript()
{
	   THStack *hs = new THStack("hs","Stacked 1D histograms");
	   //create three 1-d histograms
	   TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
	   h1st->FillRandom("gaus",20000);
	   h1st->SetFillColor(kRed);
	   h1st->SetMarkerStyle(21);
	   h1st->SetMarkerColor(kRed);
	   hs->Add(h1st);
	   TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
	   h2st->FillRandom("gaus",15000);
	   h2st->SetFillColor(kBlue);
	   h2st->SetMarkerStyle(21);
	   h2st->SetMarkerColor(kBlue);
	   hs->Add(h2st);
	   TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);
	   h3st->FillRandom("gaus",10000);
	   h3st->SetFillColor(kGreen);
	   h3st->SetMarkerStyle(21);
	   h3st->SetMarkerColor(kGreen);
	   hs->Add(h3st);

	   TLegend* testleg = new TLegend(0.75, 0.75, 0.89, 0.89, "Legenda");
	   testleg->AddEntry(h1st, "h1");
	   testleg->AddEntry(h2st, "h2");
	   testleg->AddEntry(h3st, "h3");
	   testleg->SetFillStyle(4050);


		TCanvas* newcanvas = new TCanvas("testname", "Test", 800, 600);
		newcanvas->cd();
		TPad* stackPad = new TPad("stackPad", "Pad containing the stack", 0.05, 0.35, 0.95, 0.95);
		TPad* SNPad = new TPad("SNPad", "Pad containing the SN Ratio", 0.05, 0.05, 0.95, 0.32);
		stackPad->Draw();
		SNPad->Draw();

		stackPad->cd();
		hs->Draw();
		testleg->Draw();

		SNPad->cd();
		h3st->Draw();

		newcanvas->Update();
}

I fill a THStack with two or more histograms
Then I create a legend referring to those histograms and I draw both the THStack and the Legend. The legend is set to be half transparent to allow the user to see what is under it in case it covers a piece of the plot.
If I draw this on a standard pad or canvas everything works fine
If I do this in a canvas with multiple pads like the one in the code above the stack is drawn but then removed from the pad as soon as the legend is written. If I remove the line:

testleg->SetFillStyle(4050);

everything works correctly again.
Am i doing something wrong here?

I’m using ROOT 5.28f

Thanks
Stefano

As far as I am aware, fill styles 4000 to 4100 are valid for TPad only: http://root.cern.ch/root/html/TAttFill.html

But then why the drawing works fine if I do it in a pad without parent and as soon as I do it in a multi pad canvas only one of the two objects completely disappears?

To make the legend fully transparent do:

  testleg->SetFillStyle(0);

Since the last ROOT version you can also make it semi transparent using a semi transparent color but it will be visible only on pdf output, TImage output, and with the new Quartz port on Mac.

1 Like