# Unable to fill a 2d histogram

**URL:** <https://root-forum.cern.ch/t/unable-to-fill-a-2d-histogram/59264>\
**Category:** Newbie\
**Created:** [May 7, 2024, 6:19pm UTC](https://root-forum.cern.ch/t/unable-to-fill-a-2d-histogram/59264 "2024-05-07T18:19:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rana/32/36242_2.png) [@Rana](https://root-forum.cern.ch/u/Rana)\
**Post date:** [May 7, 2024, 6:19pm UTC](https://root-forum.cern.ch/t/unable-to-fill-a-2d-histogram/59264/1 "2024-05-07T18:19:47Z")

</div>

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

 ![Screenshot from 2024-05-07 14-12-55](https://root-forum.cern.ch/uploads/default/original/3X/0/3/0389777321872379b17b30e67e4e5eabc2f843a7.png)  
my mistake ?

```auto
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
   canvas->SaveAs("output.png");

    }

```

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 8, 2024, 6:07am UTC](https://root-forum.cern.ch/t/unable-to-fill-a-2d-histogram/59264/2 "2024-05-08T06:07:44Z")

</div>

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?

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Rana](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rana/32/36242_2.png) [@Rana](https://root-forum.cern.ch/u/Rana)\
**Post date:** [May 10, 2024, 1:06am UTC](https://root-forum.cern.ch/t/unable-to-fill-a-2d-histogram/59264/3 "2024-05-10T01:06:05Z")

</div>

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