TH2 variable binning


I want to have a variable binning in a TH2 histogram, here is my code:

std::vector<Float_t> ptbins = {4.5e03, 7.e03, 10.e03, 15.e03, 20.e03, 25.e03, 30.e03, 35.e03, 40.e03, 45.e03, 50.e03, 60.e03, 80.e03, 100.e03, 200.e03, 300.e03, 500.e03, 700.e03};
std::vector<Float_t> etabins = {-3.0, -2.47, -2.37, -2.01, -1.81, -1.52, -1.37, -1.15, -0.8, -0.6, -0.1, 0, 0.1, 0.6, 0.8, 1.15, 1.37, 1.52, 1.81, 2.01, 2.37, 2.47, 3.};

Int_t ptbinnum = sizeof(ptbins);
Int_t etabinnum = sizeof(etabins);

Float_t *Ptbins = new Float_t[ptbinnum];
Float_t *Etabins = new Float_t[etabinnum];

for(unsigned int i(0); i<ptbinnum; i++) Ptbins[i] =;
for(unsigned int j(0); j<etabinnum; j++) Etabins[j] =;

h_2D_el_pt_vs_eta = Init2DHistogramVarBin(“h_2D_el_pt_vs_eta”, "h_2D_el_pt_vs_eta; #eta ; p_{T} [GeV] ", etabinnum, Etabins, ptbinnum, Ptbins);

This is not working, but I have no idea why, is there anything I’m doing wrong here?


ROOT Version: v6-10-06
Platform: macOS
Compiler: Python


h_2D_el_pt_vs_eta = Init2DHistogramVarBin("h_2D_el_pt_vs_eta", "h_2D_el_pt_vs_eta; #eta ; p_{T} [GeV] ", etabinnum - 1, Etabins, ptbinnum - 1, Ptbins);

Hi Wile, thanks for your suggestion, but it didn’t work.

I just found the solution, which was basically dividing by sizeof(Float_t):

Int_t ptbinnum = sizeof(ptbins)/sizeof(Float_t);
Int_t etabinnum = sizeof(etabins)/sizeof(Float_t);

My problem was clearly the fact that I used sizeof() (which return the number of bytes) instead of size() (the number of elements)

That’s still wrong (I didn’t notice it earlier, sorry). You need:

Int_t ptbinnum = ptbins.size();
Int_t etabinnum = etabins.size();

Yes, this is what I meant with my last comment :slight_smile: Thanks a lot!

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