Histogram from a text file

Hi all,

I need to plot a histogram from a text file with 2 columns. Can someone help with a code to do this?

Many thanks in advance,


Try TGraph instead: http://root.cern.ch/root/html/TGraph.html
Store your data in a simple text file, e.g. “MyData.txt”, then try:

root [0] TGraph *MyGraph = new TGraph("MyData.txt");
root [1] MyGraph->Draw("A*");

Or, try a TTree instead: http://root.cern.ch/root/html/TTree.html
Again, store your data in a simple text file, e.g. “MyData.txt”, then try:

root [0] TTree *MyTree = new TTree("MyTree", "MyTree");
root [1] MyTree->ReadFile("MyData.txt", "Energy_1:Energy_2");
root [2] MyTree->Draw("Energy_1:Energy_2", "", "*");
root [3] MyTree->Draw("Energy_1");
root [4] MyTree->Draw("Energy_2");

That worked, thank you. It plotted in black, can I use ''colz" to plot in colour. It didn’t work when I tried it. Chloe

Thank you. That’s ace! The colz command worked for TTree, Chloe.

In case you create the TTree, you can try:

root [2] MyTree->Draw("Energy_1:Energy_2", "", "colz")

Can I use the same commands for TH2F instead of TTree?

I don’t think there exists the “ReadFile” method in any TH2 class.
You can, for example, use “TTree::Draw” (with “>>”) or “TTree::Project” if you want to get a TH2 from a TTree.

Thank you, I will try that.

The text file has 23000 lines. Can you think of a reason why it would not plot all entries?

All entries should be present. If not, try (before you “Draw” it):


I would have expected the same thing, but the plot is not what I expected. Think I will do back and check the data again. Thanks for all your help

If you use “TTree::Draw”, you may need to use “TTree::SetEstimate” in advance, which sets the number of entries used to estimate automatic histogram limits for “TTree::Draw”. By default, fEstimate=10000 (while your TTree has something like 23000 entries). So, after you fill the TTree using “TTree::ReadFile”, simply do (once is enough):

root [...] MyTree->SetEstimate(MyTree->GetEntries());

and your automatic histogram limits will be fine (for any following “TTree::Draw” call).

Hi Chloe!

I have a ROOT macro that reads text file with 2 columns and creates a 2-dimensional histo. I’m pretty sure I could modify it so it works for you.
So let me know if you still need any help with this!

That would be great - thank you! Do you need anymore info from me?

Hi Chloe!

Here it is.
This is the way it works:
It takes testfile.txt (you can change file name on line 24), counts number of lines in this text file, searches for max and min X- and Y-values to calculate histogram boundaries and simply fills histo: first number on the line as X, then Space, then second number on the line as Y-value.

You can run it simply by doing
root -l hist2Dfromfilev03.c

If you have any difficulties please send me a small piece of your text file.
Let me know if you need anything else about that!
hist2Dfromfilev03.c (2.37 KB)

That is brilliant! Thank you very much. Is it possible to plot in colour?

I have found where to add the command. Thanks again. That’s great

Yes, sure. You have to change
on line 96 to

I would like to use this code on another textfile that contain negative. Is it possible? When I did it all the negative values were plotted as zeros. Thanks Chloe.

That’s odd, it works for me fine. Could you please send me a piece of your text file with negative values?