TH2F with different y-binning for each x bin

I want to create a TH2F histogram with variable bins in both x and y. However, I want the binning in y to be different for each x bin. Is this possible to do with the TH2F constructor?

ROOT Version: 6.24/02
Platform: Ubuntu 20.04
Compiler: gcc 9.4.0


Dear @phm5024 ,

You are probably looking for this constructor of TH2.

Possibly, you might also be looking for TH2Poly. I am not completely sure just based on the wording of your question.

Let me know which one you actually needed.

Cheers,
Vincenzo

Thank you. I am not sure that either does what I am looking for. I would like to have a certain binning in x, but then for each bin x, define a different binning y. So, for example, for Bin 1 in x, my bin edges in y are [0, 1, 2, 3]; but for Bin 2 in x, my bin edges in y are [0, 0.5, 1.5, 3]. And so on for the rest of the bins in x.

If I understand correctly, the TH2 constructor you shared will only allow the same y binning scheme for all x bins.

And I only want rectangular bins, so I am not sure that TH2Poly is what I am looking for either.

Hi @phm5024,
I think TH2Poly is what you want.
In the script below I created a 2D histogram with the axis you said. I also added a 3rd x bin with y edges [0,3].

Then I filled each bin with a different weight. The result I obtain is shown in the attached picture.

Among the different drawing option I tried only "colz" seems reliable, "surf" or "lego" do not work.

Best

{
   TH2Poly *hp = new TH2Poly();
    hp->AddBin(0,0,1,1);
    hp->AddBin(0,1,1,2);
    hp->AddBin(0,2,1,3);
    hp->AddBin(1,0,3,0.5);
    hp->AddBin(1,0.5,3,1.5);
    hp->AddBin(1,1.5,3,3);
    hp->AddBin(3,0,6,3);

    hp->Fill(0.5,0.5,10);
    hp->Fill(0.5,1.5,20);
    hp->Fill(0.5,2.5,30);
    hp->Fill(2.,0.25,40);
    hp->Fill(2.,1,50);
    hp->Fill(2.,2.5,60);
    hp->Fill(5.,2.5,70);
   
    hp->Draw("colz");
}

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.