graph2D: reading an input file

Hi all,
I’m new in Root and maybe my question looks like stupid, but…I’m trying to fill a 2D Graph by reading a data file, with the command:

Just to test it, I wrote the following graph2.dat file by using NotePad (I’m working with Windows XP):
3.3 4.1 5.0
4.2 3.8 5.0
5.1 6.7 5.0
6.3 2.4 5.0
7.6 8.9 5.0
5.5 3.0 5.0

I put the file and the macro in the same folder.
When I run the macro, I get the following message:
"Cannot open file: graph2.dat, TGraph2D is Zombie, Empty TGraph2D"

Can you help me? Many thanks!

I just tried with the latest ROOT version on both linux and Windows and it is fine for me. This code has note change since a long time so it should work also with older versions. Which version are you using ?

I’m using Version 5.15/08. I enclose to this post the code and the data file (I changed file extension from “dat” to “txt” just to send it to you, that could help you in understanding my mistake!
graph2.txt (78 Bytes)
provaGraph2D_v0.C (368 Bytes)

You had CTRL-M characters in your files. With the two following files I get the attached picture.

3.3 4.1 5.0
4.2 3.8 5.0
5.1 6.7 5.0
6.3 2.4 5.0
7.6 8.9 5.0
5.5 3.0 5.0
   TCanvas *c = new TCanvas("c","Graph2D esempio",0,0,700,600);
   TGraph2D *g = new TGraph2D("graph2.txt");

Are you telling me that I cannot have any blank lines even in the code?
Anyway, I suppressed any blank line from both, the code and the data file…but still the same error message (here attached) :frowning:


I was telling that your files have a CTRL-M character at the end of each lines. Blank lines are fine. I executed again your example the same way as you did on XP. See attached file. It is still ok.

By the way, can you check that the file graph2.txt is really in the directory where root is running ? from the root prompt do:

.! dir graph2.txt

thanks!!! Now it’s solved!

I put the data file in the root/bin (where root.exe is) or in the root/tutorials (where my code is) folders…now I put in the C:\root folder and it is fine.

many thanks!


My query was relevant to this query. My data file has 4 columns and I wish to draw surface using cols 1:2:4. The columns are in the format “int float float float”.

Just using the code

TGraph2D *g1 = new TGraph2D("PsiSq.dat");

does not serve the purpose.

I tried to use

TGraph2D *g1 = new TGraph2D("PsiSq.dat","%lg %lg %lg %lg");

which crashed the root.

I want to avoid simple reading the file line after line and making the variable vector just to make job simpler.

Can you kindly help?

Try “%lg %lg %*lg %lg” and/or “%lg %lg %*s %lg”.

Thanks! I tried using “%lg %lg %*lg %lg”.

However, how can I draw 3D graph using cols 1, cols 2 and cols4 (say)?

TGraph2D specific drawing options -> class TGraph2D
2D histogram drawing option -> class THistPainter


I have seen those pages. In those cases, data file has 3 columns and all are used to plot surface plot.

In my case, data file has 4 columns and I want to plot surface using any three of those 4 columns. In GnuPlot I may use something like “splot “filename” u 1:3:4 wpm3d”. I am confused how can I address any three columns for surface plotting in Root. I do not need histogram or error plot.