Some doubts on Contour Plots

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.14/04
Platform: linuxx8664 (Ubuntu 16.04.12)
Compiler: gcc 5.4.0

I have a few questions on Contour plots. Basically, I am learning to make contour plots.

  1. I was trying to make a contour plot from a Delphes output ROOT file. I wanted the as Z-axis the event numbers or the constituent particle number of that branch. I am not sure which variable it’s taking as the Z-axis, but it cannot be the number of events. How to set Z-axis as the number of events. I have attached a Sample macro and a Dropbox link to the ROOT files. Also, I am attaching the contour that I could make.
    SampleMacro.C (1.6 KB)
    ROOT Files

  2. I have an ASCII file with three columns. The first two columns are two variables and the third column is the function of them. I want to plot a contour of the two variables with the 3rd column as the Z-axis. How to do that? I am also attaching a small sample data file.
    data.txt (2.8 KB)


The “Z-axis” is the number of entries in histograms’ bins (seems that bins in your histograms have between 0 and 13 entries).

Thanks, Wile.
Yes, that I noticed. The thing is that when I was plotting Eta and Phi 1-D histograms, they were okay as you already know. But when I am trying to plot contour why the z-axis is taking such a range of values? In the input ROOT files there are more than 9k events for Photon.
Do have any idea?


Your “signalSP.root” has 10000 entries in the “Delphes” tree.
You create a 50 bins x 50 bins histogram which means that in average you can expect 4 entries per bin.

  TGraph2D *g = new TGraph2D("data.txt");
  g->Draw("pcolz"); // "pcolz" or "colz" or "surf2z" or ...

Thank you so much, Wile. This is working fine. Just one question: if I have more than 3 columns then should I do the same thing as in TGraph1D to read the columns, i.e., TGraph2D *g = new TGraph2D("data.txt", "%*lg %lg %lg %lg %*lg"); or anything else?


Thanks a lot Olivier. Sorry, I should have noticed that.
Thanks again.


Hi Wile and Olivier,
Sorry to again bother you on the same topic. When I tried the above commands as a macro it worked fine with a data file with only 3 columns. But when I tried running the same macro with a data file with multiple columns and selecting specific columns it gives error messages.

Error in <TGraph2D::Paint>: Empty TGraph2D

I am attaching both the data file and the macro. Can you tell me what’s wrong here?
data.txt (2.8 KB)
not_working.txt (247.2 KB)
Test.C (199 Bytes)

NOTE that when I’m working with the file ‘working.txt’, I am deleting the first %*lg in line-2.


Okay, sorry. It’s now working when I replace the %*lg with %*s.
But how do I interchange the X-axis and Y-axis?


If you do not want to change your macro you will need to invert the columns in data.txt:

awk '{ t=$1 ; $1=$2; $2=t; print }' data.txt > data2.txt

Thanks, Olivier and Wile. Both the suggestions working like charm.
In particular, I liked command suggested by Olivier. It’ll save me a lot of time in other cases also. But also, I noticed that in the new file it’s replacing the column separator from ‘tab’ to ‘space’. Is that expected?
Anyway, thanks a lot.


If your file has three columns, and you want to invert the first two, and separate them with tabs do:

 awk '{ print $2"\t"$1"\t"$3 }' data.txt > data2.txt

There is many awk tutorials on the web.

Thanks. This is working fine…
Thanks a lot Olivier.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.