# dirs.C from root tutorials-how the histograms are filled and how to access to their entries?

**URL:** <https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067>\
**Category:** Newbie\
**Tags:** tree, graphics, hist, root\
**Created:** [August 20, 2020, 10:52am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067 "2020-08-20T10:52:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![who\_cares](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/dc4da7/32.png) [@who\_cares](https://root-forum.cern.ch/u/who_cares)\
**Post date:** [August 20, 2020, 10:52am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/1 "2020-08-20T10:52:00Z")

</div>

I checked the file “dirs.C” from root tutorials. Although I understand how the directories/folders are generated, I have many questions about the histograms generated,

1. When checking the generated histograms, why there are two with the same name `h0_0N` and `h0_0N;1` ? Also they are the same. What is their difference and why they are saved with those labels?
2. Can someone explain how the histograms are filled ?

```auto
// ^^^^^^ Fill histograms
   TRandom r;
   for (i=0;i<nplanes;i++) {	// nplanes=#folder[1-9]
      cdplane[i]->cd(); // open forder [i]
      for (j=0;j<ncounters;j++) {	// j[0-99], number of histo
         for (k=0;k<100;k++) {
            hn[i][j]->Fill(100*r.Rndm(),i+j);	
            hs[i][j]->Fill(100*r.Rndm(),i+j+k); 
         } // For over k
      }	//For over j
   }	// For over i

```

For example  
`i=0 j=0 k==[0-99]`, k runing from 0 to 100.  
`hn[0][0]->Fill(100*r.Rndm(),0);`  
Does this mean hn[0][0]-\>Fill(x value = random number, here is the weight?)   
And for `hs[0][0]->Fill(100*r.Rndm(),0+0+[0-99]);` what is the function of `k` here? is that adding `k` in the second entry of `Fill()`, does `hs` have a larger weight?

`i=0 j==1 k=[0-99]`  
`hn[0][1]->Fill(100*r.Rndm(),1);`  
`hs[0][1]->Fill(100*r.Rndm(),0+1+[0-99]); // [1-100]`

`i=1 j=[0-99] k=[0-99]`  
`hn[1][0-99]->Fill(100*r.Rndm(),1+[0-99]);	//[1-100]`  
`hs[1][0-99]->Fill(100*r.Rndm(),1+[0-99]+[0-99]); // [1-199]`

What is the function of k inside the `Fill`?

1. If I want to print and get any the entry of any of the histograms, inside the planes directories how can I access to such information? I tried to print inside the `k` loop  
`std::cout<<hn[i][j];`  
to get the value of the entry `k`  
but it gives me the address in memory ` 0x55fd7b494e20`.

How can I obtain the entry number and its value?

* * *

\_ROOT Version: root.v6.20  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [August 21, 2020, 9:45am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/2 "2020-08-21T09:45:48Z")

</div>

Hi,

To answer your questions:

1. It’s just an internal detail of how data is stored in the ROOT file, they refer to the same histogram. You can access it with name `h0_0N`.

2. Yes, the second parameter in `Fill` is the weight. The `hs` histograms are being filled randomly and with larger weights that the `hn` in the loop over `k`. `k` is used to provide that extra weight to `hs` (if there is a reason behind this besides demonstration purposes, I don’t know).

3. `hn[i][j]` is a `TH1F` object, you can check here how to obtain information from it here: [https://root.cern.ch/doc/master/classTH1F.html](https://root.cern.ch/doc/master/classTH1F.html) . The entry you are filling is determined by i, j and k, and the value you are filling with is the result of calling `100*r.Rndm()`.

---

<div class="post-metadata">

**Author:** ![who\_cares](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/dc4da7/32.png) [@who\_cares](https://root-forum.cern.ch/u/who_cares)\
**Post date:** [August 21, 2020, 10:28am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/3 "2020-08-21T10:28:12Z")

</div>

@etejedor,

Thank you for your reply. However I have already had a look to TH1F documentation, without having success when I tried to obtain the values of hn. I get error like `member reference base type 'TH1 *[100]' is not a structure or union`. And sincerely, I do not know what it means.

---

<div class="post-metadata">

**Author:** ![who\_cares](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/dc4da7/32.png) [@who\_cares](https://root-forum.cern.ch/u/who_cares)\
**Post date:** [August 21, 2020, 11:38am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/4 "2020-08-21T11:38:02Z")

</div>

By modifying the loop in the following way:

```auto
  for (int i=0;i<tnplanes;i++) { // #### NOTE T: nplanes=#folder[0-2]
      cdplane[i]->cd(); // #### NOTE T: open forder [i]
      std::cout<<"inside loop i= "<< i << endl;
      for (int j=0;j<tncounters;j++) { // #### NOTE T: j[0-], number of histo
          std::cout<<"inside loop j= "<< j << endl;
         for (int k=0;k<2;k++) {
            std::cout<<"---inside loop k= "<< k << endl;
            float xnentry = 100*r.Rndm();
            float xsentry = 100*r.Rndm();
            int nweight =(int)100*r.Rndm() + 1.0;
            int sweight =(int)100*r.Rndm()+ 1.0;
            hn[i][j]->Fill(xnentry,nweight);
            hs[i][j]->Fill(xsentry,sweight); 
            
            std::cout<<"i: "<< i << " j: "<< j << " k: " << k << "\txnentry: "<< xnentry << " nweight: "<< nweight << " xsentry: "<< xsentry << " sweight: " << sweight << endl;
            std::cout<<"hN[i "<< i << "] [j "<< j << "]: "<< hn[i][j]->GetBinContent(k);
            std::cout<<"\thS[i "<< i << "] [j "<< j << "]: "<< hs[i][j]->GetBinContent(k)<<endl;
         } // #### NOTE T: For over K
         std::cout<<"exit k loop "<< endl;
      }	// #### NOTE T: For over J
      std::cout<<"exit j loop "<< endl;
   }	// #### NOTE T: For over i  
   std::cout<<"exit i loop "<< endl;

```

Which gives me in the terminal:

```auto
inside loop i= 0
inside loop j= 0
---inside loop k= 0
i: 0 j: 0 k: 0	xnentry: 22.073 nweight: 44 xsentry: 43.0809 sweight: 56
hN[i 0] [j 0]: 0	hS[i 0] [j 0]: 0
---inside loop k= 1
i: 0 j: 0 k: 1	xnentry: 25.7467 nweight: 79 xsentry: 36.8463 sweight: 60
hN[i 0] [j 0]: 0	hS[i 0] [j 0]: 0
exit k loop 
exit j loop 
inside loop i= 1
inside loop j= 0
---inside loop k= 0
i: 1 j: 0 k: 0	xnentry: 86.0942 nweight: 48 xsentry: 37.6793 sweight: 86
hN[i 1] [j 0]: 0	hS[i 1] [j 0]: 0
---inside loop k= 1
i: 1 j: 0 k: 1	xnentry: 91.954 nweight: 26 xsentry: 66.818 sweight: 40
hN[i 1] [j 0]: 0	hS[i 1] [j 0]: 0
exit k loop 
exit j loop 
inside loop i= 2
inside loop j= 0
---inside loop k= 0
i: 2 j: 0 k: 0	xnentry: 84.3097 nweight: 11 xsentry: 53.0921 sweight: 34
hN[i 2] [j 0]: 0	hS[i 2] [j 0]: 0
---inside loop k= 1
i: 2 j: 0 k: 1	xnentry: 16.5796 nweight: 53 xsentry: 85.8411 sweight: 57
hN[i 2] [j 0]: 0	hS[i 2] [j 0]: 0
exit k loop 
exit j loop 
exit i loop 

```

I can see the values, e.g. h2\_0N has two entries(corresponding to k=0,1): xentry(k=0)=84.3097, xentry(k=1)16.5796 with wight (y axis value center in ): nweight(k=0)=11, nweight(k=1)=53 respectively. But I want to obtain the x value with `GetBinCenter` or the y value `GetBinContent` bur it gives 0.5 and 0, when I am expecting to obtain the xnentry and the nweight. Do you know the correct way to obtain such values?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [August 24, 2020, 9:26am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/5 "2020-08-24T09:26:59Z")

</div>

Hi,

@moneta can perhaps shed some light on this last question.

---

<div class="post-metadata">

**Author:** ![who\_cares](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/dc4da7/32.png) [@who\_cares](https://root-forum.cern.ch/u/who_cares)\
**Post date:** [August 24, 2020, 9:31am UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/6 "2020-08-24T09:31:01Z")

</div>

I used

```auto
std::cout<<"\t Xaxis, GetMaximumBin "<< hn[i][j]->GetMaximumBin()<<endl; 
                           std::cout<<"\t Xaxis, GetBinCenter "<< hn[i][j]->GetBinCenter( hn[i][j]->GetMaximumBin() )<<endl; 
                           std::cout<<"\t Xaxis, FinBin "<< hn[i][j]->FindBin( hn[i][j]->GetBinCenter( hn[i][j]->GetMaximumBin() ))<<endl; 
                            std::cout<<"\t Yaxis, GetBinError "<< hn[i][j]->GetBinError(hn[i][j]->GetMaximumBin())<<endl; 
                             std::cout<<"\t Yaxis, content "<< hn[i][j]->GetBinContent( hn[i][j]->GetMaximumBin() )<<endl;

```

And I got x, y.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 25, 2020, 12:27pm UTC](https://root-forum.cern.ch/t/dirs-c-from-root-tutorials-how-the-histograms-are-filled-and-how-to-access-to-their-entries/41067/7 "2020-08-25T12:27:12Z")

</div>

Hi,

I am not sure I have understood well your question. Please explain it better.

Lorenzo
