# Overlaying two 2D histograms

Hi all,

I have two 2D histograms, each with slightly different bin sizes, that I’d like to overlay. They’re slightly different in that the x scales are the same, but y scale on one of them increases twice as fast as the one on the other, but still has the same maximum and minimum.

When I overlay the histograms the cross-hatching appears through the filled histogram bins, making the plot look like a mess. Unless there is some way to make the binning scale on one axis change based on the position on the other axis (e.g. if the x axis is between -1 and 1 the y axis binning size will increment by 0.05, but if the x axis is anywhere else, the y axis binning size will increment by 0.10), I don’t have any choice but to overlay two histograms with different increments of the axes.

My question is: is there a way to stop the cross-hatching from appearing through the filled bins if I overlay two 2D histograms as described above, or is there a better way to go about what I’m trying to do?

Below is the code I use for creating the histograms initially, where hname is of type TString and m_2dhistNames is of type std:

int n_xbins = 80;
double xbins[81] = {-5.191, -4.889, -4.716, -4.538, -4.363, -4.191, -4.013, -3.839, -3.664, -3.489, -3.314, -3.139, -3.000, -2.650, -2.500, -2.322, -2.172, -2.043, -1.930, -1.830, -1.740, -1.653, -1.566, -1.479, -1.392, -1.305, -1.218, -1.131, -1.044, -0.957, -0.879, -0.783, -0.696, -0.609, -0.522, -0.435, -0.348, -0.271, -0.174, -0.087, 0, 0.087, 0.174, 0.271, 0.348, 0.435, 0.522, 0.609, 0.696, 0.783, 0.879, 0.957, 1.044, 1.131, 1.218, 1.305, 1.392, 1.479, 1.566, 1.653, 1.740, 1.830, 1.930, 2.043, 2.172, 2.322, 2.500, 2.650, 3.000, 3.139, 3.314, 3.489, 3.664, 3.839, 4.013, 4.191, 4.363, 4.538, 4.716, 4.889, 5.191};
int n_ybins_tenDegrees = 36;
double ybins_tenDegrees[37];

for (int i = 0; i < 37; i++) {
}

int n_ybins_fiveDegrees = 72;
double ybins_fiveDegrees[73];

for (int i = 0; i < 73; i++) {
}

hname = “h_energy_5degreePhi”;
m_2dhistNames[hname] = new TH2F(hname, “”, n_xbins, xbins, n_ybins_fiveDegrees, ybins_fiveDegrees);

hname = “h_energy_10degreePhi”;
m_2dhistNames[hname] = new TH2F(hname, “”, n_xbins, xbins, n_ybins_tenDegrees, ybins_tenDegrees);[/code]

I do not have any available source code on when I try to overlay them, because I’m currently just plotting the two histograms separately. However, when I try to overlay them I’m basically calling:

```// First histogram TH2->Draw("LEGO"); // Second histogram TH2->Draw("LEGOSAME");```

The resulting plot, as mentioned above, looks very ugly, as the cross-hatching from the axes appears over top of the histogram bins.

The option SAME does not work with LEGO and SURFACE unless the histograms have exactly the same X Y and Z ranges. It should not be used. THStack is the way to go.

Hi,

The histograms have the same X and Y ranges, but the number of bins are different for the two histograms. Sorry if I did not make that clear before.

I tried using a THStack to draw the two 2D histograms. Unfortunately, I get the following error, and the histograms do not fill correctly (it seems half of the second histogram is missing):

Error in TH2F::Add: Attempt to add histograms with different number of bins

Here is the code I used to create the stack and draw it, where graphs[] is an array of TH2*:

``` TCanvas* canvas = new TCanvas("c1", "Test stacked histograms"); THStack* stack = new THStack("stack", "Stacked histograms"); stack->Add(graphs[0]); stack->Add(graphs[1]); stack->Draw();```

I experimented with some of the different drawing options for THStack, but none of these seemed to work. I also checked out the tutorial in \$ROOTSYS/tutorials/hist/hstack.C, but the examples for stacking 2D histograms used histograms with the same number of bins.

Is there something that I am missing about THStack, or is there something else I can use? Is there a way to do what I am trying to do?

Thanks a lot for the reply.

Can you send a small running macro reproducing your problem ?

Here’s one I made. I couldn’t think of anything to fill the histograms with besides “gaus”, but it’s basically what I’m trying to do: stack two 2D histograms with different bin sizes. If you look closely at the output you can see that only about half of the second histogram added to the THStack is drawn on the canvas.

Running this should give the following error:

Error in TH2F::Add: Attempt to add histograms with different number of bins

On a side note, I was also getting this when I ran the file multiple times, but it was fine if I ran it once:

Warning in TH1::Build: Replacing existing histogram: h1 (Potential memory leak).
Warning in TH1::Build: Replacing existing histogram: h2 (Potential memory leak).

Is this something I should be worried about? I don’t understand what it’s trying to tell me.

Thanks again for the replies.
htest.C (987 Bytes)

Yes THStack works with histograms having the same number of bins.
you do not delete the histogram in the macro that is why you get the warning when you execute several time. That’s ok.
If you want to overlay lego plots they should have the same number of bins otherwise it does not work.

Alright, thanks for the help.

Dan