Filling a histogram

Hey, not sure if this is the place for this post. I am learning the basics, trying to fill a histogram of 10 bins with specific values with Fill().

Bin Value
1 2
2 4
3 0
4 5
5 1
6 7
7 8
8 6
9 4
10 5

How can I do this? I tried to create an array and fill it with a loop like this:

root [14] TH1F hist3(“hist3”,“practice”,10,1,10);
root [15] int y [10] = { 2, 4, 0, 5, 1, 7, 8, 6, 4, 5 };
root [16] for (int i=0;i<5;i++) hist3.Fill(y[i]);
root [17] hist3.Draw()
root [18] hist3.Draw();

but this is what was drawn.

Shouldn’t I have 10 events with the frequency of the value’s occurrence shown by the vertical axis?

Thanks for any help.


Hi lpf,

with “10,1,10” you end up having 10 bins, each 0.9 units wide:

1 <= x <1.9 1.9 <= x < 2.8 2.8 <= x < 3.7 3.7 <= x < 4.6 4.6 <= x < 5.5 ...
In your code you loop from index 0 to index 4, so you are trying to fill the histo with values 2, 4, 0, 5, 1.

2 goes in the second bin: 4 in the fourth; 0 goes in the “underflow” bin (outside, to the left); 5 in the fifth bin, and 1 in the first. The plot you have is correct: 4 bins with 1 event each.

EDIT: I have a couple of ideas of what you were expecting, maybe you can try these two different sets of commands and see if they are useful… :wink:

root [0] TH1F hist3("hist3","practice",10,0,10); // changed 1 to 0 root [1] int y [10] = { 2, 4, 0, 5, 1, 7, 8, 6, 4, 5 }; root [2] for (int i=0;i<10;i++) hist3.Fill(y[i]); // changed <5 to <10 root [3] hist3.Draw();

root [0] TH1F hist3("hist3","practice",10,1,10); root [1] int y [10] = { 2, 4, 0, 5, 1, 7, 8, 6, 4, 5 }; root [2] for (int i=0;i<10;i++) hist3.SetBinContent(i+1,y[i]); // replaced Fill with SetBinContent root [3] hist3.Draw();