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

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:

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:

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.

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

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.