TGraph2D from a grid style datafile


I am trying to import data from a file where the information is stored in a grid, not in X/Y/Z format. The reason I would like to get this format is that I have 16000 columns and hundreds of rows. So storing the X/Y values every time seems a waste.

In my case, only the Z value would be presented and the corresponding X and Y would the column- and row-indices:

file test.res: 1.4 2.3 5.1 1.3 1.4 5.4 1.4 2.3 5.1 1.3 1.4 5.4 1.1 1.3 5.2 1.4 1.1 5.0 1.4 2.3 5.1 1.3 1.4 5.4 1.3 3.3 5.4 1.1 1.3 5.1 5.2 1.4 1.1 5.0 1.4 2.3 1.0 2.1 5.0 2.3 1.6 5.2 1.1 1.3 5.1 5.2 1.4 3.4 1.1 2.3 5.2 1.4 1.1 5.0 1.4 2.3 5.1 1.3 1.4 5.4 1.3 4.3 5.4 1.1 1.3 5.1 5.2 1.4 1.1 5.0 1.4 2.3 1.0 2.2 5.0 2.3 1.6 5.2 1.1 1.3 5.1 5.2 1.4 3.4 2.4 2.1 5.1 1.3 1.4 3.4 5.2 1.1 1.3 5.1 5.2 1.4

I was hoping to use TGraph2D to plot it:

root [51] TGraph2D *g = new TGraph2D("test.res") root [52] g->Draw("surf2")

At the moment this code only reads the first 3 columns of my file and interpret them a X/Y/Z. In my case I would like to use something like this:

root [51] TGraph2D *g = new TGraph2D("test.res",format="grid") root [52] g->Draw("surf2")

Eventually, the real X and Y values could be given separately in 2 other vectors, otherwise the index is just fine.

As my knowledge of ROOT is quite limited, I was wondering whether there were already a method implemented to do such a plot ?
If not, could you please advise me for the easiest way to implement this ?

Thank you


If your X,Y value are on a regular grid, why don’t you use a 2D histogram ? TGraph2D is meant to be use when your data are (x,y,z) values not necessarily distributed on a regular grid.


Thanks for the prompt answer

I do indeed have a regular grid. I decided not to chose Histograms because my peaks are obtained with very few loci (values of X/Y). So I was concerned that using an histogram would average them over a large bin, hence reducing the “sharpness” of the signal.
Can I set the bin size to 1x1 ?

Furthermore I still do not see how I can import my grid data in the histogram directly, neither on how to use an external X/Y vectors.
Could you please give me simple example ?

Thanks again


My point was not on the file reading, but on the fact you are using TGraph2D on a regular data set. Moreover you are using the option “SURF2” to draw the graph, this option will create a 2D histogram and plot it with option surf2 so you are not gaining anything in “sharpness”. Even worst more, your result may lose in precision because TGraph2D uses the Delaunay triangles interpolation to fill the histogram which is a time consuming algorithm. That’s sound not optimal as you already have the data points on a regular grid.
The TH2D constructors are listed here:

I am afraid I still do not find an easy way to do the plot directly from the Histogram.
Maybe I have too much influence from Gnuplot’s pm3d option. It is a desperately simple one-liner: … de140.html

Below is the Gnuplot command I wanted to emulate:

splot "test.res" matrix with pm3d title "Lipid Males"



if your bins are small enough you will get a similar plot with the option SURF.

… or the option LEGO … even better may be…

If you send me your data file (and the limits along the x and Y axis) I can try to produce a macro doing that kind of plot.


I’ve been following this conversation because I have almost exactly the same problem. I think my case is only different in that I want to read in multiple grid data into a tree.

Couet, were you able to solve the problem? if so could you post it in the forum please?


I did not get the informations I asked (in particular the axis limits). But seems to me there is no real problem. As it is data on a regular grid, a 2D histogram should be used. If you plot the result as a lego plot you will get something very similar to the plot shown here.

actually the command:

root [51] TGraph2D *g = new TGraph2D("test.dat") root [52] g->Draw("surf2")

dosen’t do what I want. It treats the 1st column of test.dat as the x-axis and for the y-axis it uses the maximum range of the points in the grid.

what I want is, for example, in the sample data file:

-1.800 -1.500 -1.200 -0.900 -0.600 -0.300 7.90 10.1190 10.0400 9.9540 9.8370 9.9470 10.3450 7.65 10.1050 10.0790 10.1180 9.9790 10.1080 10.5790 7.40 10.2910 10.2780 10.3500 10.3090 10.4560 10.8390 7.15 10.1850 10.2240 10.4300 10.6320 10.8470 11.0820 6.90 9.8930 10.0090 10.4130 10.9460 11.2660 11.3160 6.65 10.2140 10.2570 10.5180 11.1290 11.5250 11.6490 6.40 10.7170 10.7210 10.8510 11.4280 11.8120 11.9510 6.15 11.4870 11.5000 11.5190 11.8980 12.1400 12.2090

x-axis = row 1; Range: 0, -3
y-axis = column 1; Range: 0, 10

I want to read this into a tree so I can plot either the full x,y grid or any single row or column I choose . The original file has 120 rows x 170 columns. Could you use this data as your test data and post your script on the forum.

I’ve tried using the TTree::ReadFile command but I keep getting an output that shows only the 1st column.


so that’s a TTree now ? not a TGraph2D ?

yes, one can do much more with a TTree. I’m not sure if other people prefer TGraph2D…for me, I think using a TTree or TNtuple is a much better option.

You should read your ascii file (scanf) and generated a TTree as explain here: