How to read a data file (.txt) and fill a one-dimensional histogram?

Hello, I am trying to read a data file (.txt) and fill a one-dimensional histogram with the data from this file, but I have not been able to do this. What can I do?, or what am I doing wrong?. What would be the macro that I would have to run in the Root interface?. The data file contains 20 numbers ranging from 1 to 6. I Hope someone can help me.

ROOT Version: 6.30/04
Platform: Not Provided
Compiler: Not Provided


Hello @alfredo10101 ,

Why not try searching this forum for “histogram text file” …

In 2011 the discussion started :wink:

-Eddy

Ok thanks for the information. However, right now I am having a serious problem; what happens is that I have tried several ways (with TNtuple, TTree) to read my text file unsuccessfully; I do not know what I have to do. The strange thing is that in previous days I was able to read the text file.

Can you show an example of what you were doing the previous days which does not work anymore ?

Ok, Of course, thanks. What I did was the following: First, I type in the Root interface .x macrolab1.C, and press the Enter key; Afterwards, the Root interface informs me that the file datos.txt cannot be opened; It’s from here that I can’t go any further.
I saved the macro in the macros folder (C:\root_v5.34.25\macros); I saved the datos.txt file inside the bin folder (C:\root_v5.34.25\bin). It should be added that I have used versions 6.30 and 5.34. The macro is as follows:
{
Int_t a1, a2;
TNtuple *tuple = new TNtuple(“tuple”,“data from ascii file”,“a1:a2”);
tuple->ReadFile(“datos.txt”);
tuple->Draw(“a2:a1”);
}

The text file (.txt) is as follows:

1
4
2
6
4
3
6
4
1
2
3
1
2
4
3
6
5
5
4
1

If the macro and the data file are not in the same directory, specify the path to the file in the macro.

1 Like

Ok, thanks. How do I specify the path to the file in the macro?

Just add it to the filename, like any path. If you’re on linux, something like

tuple->ReadFile("/home/user/datos.txt");

and the equivalent for Windiows. Note that if you have spaces or special characters in the path names, it can get tricky, so I’d recommend you avoid that (rename the folders if needed)!

Ok, thanks for the information.