Skip to content

Inconsistent information for histogram contours #20860

@bgrube

Description

@bgrube

Check duplicate issues.

  • Checked for duplicates

Description

When setting the number of contours of 2D histogram via TH1::SetContour(n), only n - 1 contours are created. In contrast, TH1::GetContour returns n levels. Also the list of contours, obtained by calling (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours"); after drawing the histogram using the "CONT0 LIST" option, contains n elements, but the last entry does not contain any graphs. The index of the list of contours is shifted by 1 with respect to the level values.

Reproducer

The issue is illustrated by the script below, which compares the results obtained by explicitly defining contour levels and by using TH1::SetContour(n):

void test(TH1* hist)
{
	// plot histogram with contours
	TCanvas* canv = new TCanvas();
	hist->Draw("CONT0 Z LIST");
	canv->Update();
	// loop over contours and copy graphs
	TObjArray* contours = (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
	std::cout << "Found " << contours->GetSize() << " contour levels." << std::endl;
	double contourLevels[contours->GetSize()];
	hist->GetContour(contourLevels);
	std::vector<TGraph*> graphs;
	for (int contourLevelIndex = 0; contourLevelIndex < contours->GetSize(); ++contourLevelIndex) {
		TList* contoursAtLevel = (TList*)contours->At(contourLevelIndex);
		std::cout << "Contour with index " << contourLevelIndex << " and level " << contourLevels[contourLevelIndex]
		          << " has " << contoursAtLevel->GetSize() << " graph(s)." <<  std::endl;
		for (TObject* graph: *contoursAtLevel) {
			graphs.push_back((TGraph*)graph->Clone());
		}
	}
	// overlay histogram with contours
	hist->SetContour(100);
	hist->Draw("COLZ");
	for (TGraph* graph : graphs) {
		graph->SetLineColor(kRed);
		graph->Draw("C");
	}
}


void testContours()
{
	TF2* fcn = new TF2("fcn", "x", -1, 1, -1, 1);
	TH1* hist = fcn->GetHistogram();
	// this produced the expected result
	hist->SetContour(2, std::vector<double>{-0.5, 0.5}.data());
	test(hist);
	// this does not work as expected
	hist->SetContour(2);
	test(hist);
}

ROOT version

ROOT 6.36.06
Built for macosxarm64 on Nov 26 2025, 12:01:10
From tags/6-36-06@6-36-06
With Apple clang version 17.0.0 (clang-1700.0.13.5)

ROOT 6.32.20
Built for linuxx8664gcc on Nov 26 2025, 17:16:23
From tags/6-32-20@6-32-20
With c++ (GCC) 11.5.0 20240719 (Red Hat 11.5.0-11)

Installation method

MacPorts, build from source

Operating system

MacOS, Linux

Additional context

No response

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions