Dynamic Variables?

Just a few questions:

Is it possible to have a dynamic number of variables in root?
eg. I’m reading data from a file:

new plot
x y z
2 3 4
5 6 7
8 9 1

new plot
x y z
4 5 2
5 7 5
1 9 3

I dont know how many histograms the inputted file will want me to create. How do I get the script to generate a new histogram each time it sees the words new plot for example. I’m reading the file line by line, and hence this trigger is easy. What I dont know how to do is create a new histogram with an individual name each time.
eg, in the example above I would end up with 2 histograms. histo1 and histo2. but I didnt know that this is what I would get, the user may have put in 3 histograms or even 100 histograms.

Also, Is it possible to fill a histogram and then change its number of bins?
Because I’m reading line by line, I wont know how much data the user wants me to plot when I want to plot it and will only know after the last line has been read. eg. Is it possible to just initiate a histogram with 4 bins, and then later change it to 198 bins.

Many Thanks,



create the TH3F (or whatever other type you prefer) when needed, and add it to a TList. The TList will contain all the histograms you created.

I don’t understand the question on bins and I assume that might be caused by you misunderstanding what bins are. They are not the number of entries filled into a histogram! See e.g. en.wikipedia.org/wiki/Histogram If you that already then I don’t understand how you want to define the number of bins.

If the axis range is unknown at the time of creating it you can instead create a TNtuple (even in memory), fill that with the coordinates, and when the next histogram comes along or the file ends you dump the TNtuple into a TH3: ntuple->Draw(“x:y:z”).

Cheers, Axel.

Hi Axel,
Thank you for your help,

On the subject of bins:
The file that I’m reading the data from doesn’t tell me the number of bins needed directly, however the data is already organised into bins. It isn’t raw data, it has been processed and sorted so that there is one ‘data’ entry that corresponds directly to the total weight for that bin.
eg: An actual sample of the data is:
0.7000 0.2293E-02 0.0000E+00
0.9000 0.4344E-02 0.0000E+00
1.1000 0.3576E-02 0.0000E+00
1.3000 0.2863E-02 0.0000E+00
1.5000 0.2609E-02 0.0000E+00
where the first column is the centre point of the bin, the second column is the weight assigned to that bin and the third column is the error on the bin. The number of bins in this case is therefore determined by the number of lines of processed data in the input file.
eg. in the above example I would have 5 bins, ranging from 0.6-0.8, 0.8-1.0, 1.0-1.2, 1.2-1.4, 1.4-1.6.
The problem is that I dont know how many bins I’m going to have before I start plotting them because I read the data line by line.

I have also come across the difficulty of adding error bars to a histogram. So far I have only found the histogram1.SetErrors(const array) method, which would require a complete array of all the errors to be applied at the same time. Again this runs into difficulties due to reading line by line as I would have to collect the errors into an array as I go along, and then apply them all at the end.
Is there anyway to set the errors as I go through. eg, apply them as I fill the histogram.
Currently, my only thought is to use a string that I can append to easily and then simply read off all of the string entries to an array at the end and use the array with the SetErrors method, but this seems clumsy.

Many Thanks for all your help,



you could simply store the values in a std::vector< double> x, y, error, and once you have read all the values for a histogram you create it from these vectors.

Cheers, Axel.

Hi Axel,
Thank you, thats great.

I’ve now semi-finished the code that I’m writing, (its attached, along with the input file), but I’m getting some pretty nasty error messages out of it that I dont really understand. If you have any ideas I’d be very grateful.

Error Message:

Many Thanks for all your help.


wbbj1.txt needs to be renamed as wbbj1.top
final1.txt needs to be renamed as final1.C
final1.txt (4.51 KB)
wbbj1.txt (5.15 KB)

Dont worry. Realised that this was a separate issue, and hence have created a separate thread for it.
Thank you for all your help. Geoff

All sorted now.
Thank you for your help.

Geoff Herbert