Unable to fill a 2d histogram

Hi Experts,

I am trying to fill a 2D histogram with hits on x vs z values. I am unable to fill the histogram, however doing a cout I get the result.
I am attaching the block of the code as well the screenshot of the terminal result. I will be extremly grateful if you point out

my mistake ?

TH2F* h2D = new TH2F(h2D, x vs z , 100, 10000, 20000, 100, -4000, 4000);

for (Long64_t jentry=0; jentry<nentries;jentry++)

	Long64_t ientry = LoadTree(jentry);

	if (ientry < 0) break;
   	nb = b_RecoHitPos->GetEntry(ientry);   
	nbytes += nb; 
for (Long64_t nHit=0; nHit<nHits;nHit++)
	if (RecoHitPos[nHit][0] != -999) 
        h2D->Fill(RecoHitPos[nHit][0], RecoHitPos[nHit][2]);
        cout<<"nHit"<<nHit<<"	RecoHitPos[nHit][0]"<<RecoHitPos[nHit][0]<<"	nHit"<<nHit<<"	RecoHitPos[nHit][2]"<<RecoHitPos[nHit][2]<< endl;


   TCanvas* canvas = new TCanvas("canvas", "canvas", 800, 600);
   h2D->Draw("colz"); // "colz" option for a 2D color plot


Dear @Rana ,

Thanks for reaching out to the ROOT forum! What do you mean exactly with not being able to fill the histogram? The code you shows seems to be filling the histogram object, maybe when you plot it doesn’t show what you expect?

While looking at the two screenshots, I noticed that the values at index [0] are roughly between 2500 and 3500, while the values at index 2 are roughly between 12K and 13K. In your program, the binning you provide is (10000, 20000) for the x axis and (-4000,4000) for the y axis. But in the for loop, the values you are using to fill the x and y in the call to Fill seem to have their ranges switched. Can this be the issue perhaps?


1 Like

Hi Vincenzo,
Thanks a lot for pointing that out. It worked !!