# Number of bins for histogram being filled in real-time (pyroot)

**URL:** <https://root-forum.cern.ch/t/number-of-bins-for-histogram-being-filled-in-real-time-pyroot/40533>\
**Category:** Newbie\
**Created:** [July 20, 2020, 7:38pm UTC](https://root-forum.cern.ch/t/number-of-bins-for-histogram-being-filled-in-real-time-pyroot/40533 "2020-07-20T19:38:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paipai](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@paipai](https://root-forum.cern.ch/u/paipai)\
**Post date:** [July 20, 2020, 7:38pm UTC](https://root-forum.cern.ch/t/number-of-bins-for-histogram-being-filled-in-real-time-pyroot/40533/1 "2020-07-20T19:38:53Z")

</div>

Hi!  
I have created a program which reads a file, which is constantly updated with new entries/lines - in real time. The file gets written because of a measurement process which takes an arbitrary amount of time. So basically it is an infinite `while` loop which appends all entries to a list and constantly checks for new entries/lines to append to the list. You can break out of the while loop by pressing a button in a GUI.

A sketch of the code would look like this:

```auto
data = []
while True:
       .........
       for line in f:
              data.append(float(line))
       .......
       if something:
              break

```

Now in the while loop I want to fill a histogram in real time with each new datapoint. The problem is, how do I adapt the number of bins? As a rule of thumb I want my number of bins to be the square root of my number of entries. But my number of entries depends on how long I measure, that is how long my original file is being written. And as I have understood I have to define my histogram before the while loop and set a number of bins.

So it I think of something like this:

```auto
data = []
h = ROOT.TH1D("h", "Histogram", ????????? , 0, 0)
c1 = ROOT.TCanvas()

while True:
       .........
       for line in f:
              data.append(float(line))
              h.Fill(data[-1])
       .......
       if something:
              break

h.Draw()
c1.Update()

```

But to set the number of bins I have to know how much entries I will have in advance. But that is not possible.

How would I go about this? Thank you.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [July 21, 2020, 9:06am UTC](https://root-forum.cern.ch/t/number-of-bins-for-histogram-being-filled-in-real-time-pyroot/40533/2 "2020-07-21T09:06:49Z")

</div>

Hi,  
if you keep all data in a list anyway, how about creating and filling the histogram after the `while True` loop completed?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![paipai](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@paipai](https://root-forum.cern.ch/u/paipai)\
**Post date:** [July 21, 2020, 9:49am UTC](https://root-forum.cern.ch/t/number-of-bins-for-histogram-being-filled-in-real-time-pyroot/40533/3 "2020-07-21T09:49:11Z")

</div>

Hi, thanks for your reply.  
I did that initially but then I thought it would be more time efficient to do it “on the go” when the dataset gets really big. So I was wondering if that is somehow possible.
