# Contour Lines on a 2d histogram

I am currently working on a project that requires me to draw a contour line over a 2d histogram in order to denote which bins are below a sensitivity of 5 and which are above. attached is an example of what my current 2d histogram looks like and I am filling these histograms using an array and a for loop.

In an attempt to do something similar I created two separate histograms and used if statements to place bins in each separately an example of that is here:

``````   int j;
int k;
for(j = 0; j < 6; j++)
{

for(k = 0; k < 10; k++)
{
if (sens[(10*j)+k] <= 5)
{
h1->SetBinContent(j+1, k+1, sens[(10*j)+k]);
std::cout << "Bin # (" << j+1 << "," << k+1 << ") with values: (" << s[(10*j)+k] << "," << b[(10*j)+k] << "), Sensitivity = " << sens[(10*j)+k] << '\n';
}
else
{
h2->SetBinContent(j+1, k+1, sens[(10*j)+k]);
std::cout << "Bin # (" << j+1 << "," << k+1 << ") with values: (" << s[(10*j)+k] << "," << b[(10*j)+k] << "), Sensitivity = " << sens[(10*j)+k] << '\n';
}

}
``````

here is my code for separating the values into two histograms.

For clarification I want to draw a solid line on my histogram that separates the bins that are less than 5 and those that are greater.
I also have already tried the contour line options in the 2d histogram section and I was unable to use them in the way I wanted to, but that could be due to my lack of understanding of them.

I don’t think the contour option will draw at the bin edges, if that’s what you want. You could scan all the bins with GetBinContent and if the content is in the range you want (e.g. <=5), then you can save the upper and lower edges (in both axes, x and y, so that’s 4 numbers) of that bin and then draw lines joining those corners (a tedious, long process if you have many bins!), either by hand or with some code; as alternative, you can also create another histogram with the same binning but only filling the bins that you want to enclose, using SetBinContent (but fill each bin only once, and with the maximum value of the original histogram, see why below), and then plotting this second histogram with the option “box” on top of the original. Here is an example:

``````{
float cutvalue = 5;
// original histogram
auto h0 = new TH2F("h0","The original histogram",4,0,4,4,0,4);
for (Int_t i = 0; i < 100; i++) {
h0->Fill(gRandom->Integer(4),gRandom->Integer(4));
}
float max=h0->GetMaximum();
cout << max << endl;

// "contours" histogram
auto h2 = new TH2F("h2","only the bins to highlight",4,0,4,4,0,4);
for (Int_t i = 1; i < 5; i++) {
for (Int_t j = 1; j < 5; j++) {
if (h0->GetBinContent(i,j)<=cutvalue) {
h2->SetBinContent(i,j,max);  // fill with max so the boxes are the size of the bins
}
}
}
h2->SetLineColor(kRed);
h2->SetLineWidth(3);
//h2->SetFillStyle(3944);  // try with a fill for more contrast
//h2->SetFillColor(kRed);
float xl,xh,yl,yh;
//vector<float> vxl,vxh,vyl,vyh;
cout << "(binx, biny),\t(LLx, LLy),\t(LRx, LRy),\t(URx, URy),\t(ULx, ULy)"<<endl;
for (Int_t i = 1; i < 5; i++) {
for (Int_t j = 1; j < 5; j++) {
if (h2->GetBinContent(i,j)==max) {
xl = h2->GetXaxis()->GetBinLowEdge(i);
xh = h2->GetXaxis()->GetBinUpEdge(i);
yl = h2->GetYaxis()->GetBinLowEdge(j);
yh = h2->GetYaxis()->GetBinUpEdge(j);
/*
vxl.push_back(xl);
vxh.push_back(xh);
vyl.push_back(yl);
vyh.push_back(yh);
*/
cout <<"("<<i<<","<<j<<"),\t\t(";
cout <<xl<<","<<yl<<"),\t\t("<<xh<<","<<yl<<"),\t\t(";
cout <<xh<<","<<yh<<"),\t\t("<<xl<<","<<yh<<")"<<endl;
}
}
}
h0->Draw("colz");
h2->Draw("box,same");
//  h2->Draw("box,same,f");
}
``````

The output was:

``````(binx, biny),   (LLx, LLy),     (LRx, LRy),     (URx, URy),     (ULx, ULy)
(1,1),          (0,0),          (1,0),          (1,1),          (0,1)
(1,4),          (0,3),          (1,3),          (1,4),          (0,4)
(2,2),          (1,1),          (2,1),          (2,2),          (1,2)
(2,4),          (1,3),          (2,3),          (2,4),          (1,4)
(4,3),          (3,2),          (4,2),          (4,3),          (3,3)
(4,4),          (3,3),          (4,3),          (4,4),          (3,4)
``````
1 Like

Thank you! I was able to setup what I was looking for with your help!