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

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 ?
``````// ^^^^^^ 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->Fill(100*r.Rndm(),0);`
Does this mean hn->Fill(x value = random number, here is the weight?)
And for `hs->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->Fill(100*r.Rndm(),1);`
`hs->Fill(100*r.Rndm(),0+1+[0-99]); // [1-100]`

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

Hi,

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 . 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()`.

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 *' is not a structure or union`. And sincerely, I do not know what it means.

By modifying the loop in the following way:

``````  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:

``````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?

Hi,

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

I used

``````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.

Hi,

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

Lorenzo