.txt files into a TH1D ROOT histogram

Hello, this is trentacoollime, who has been helped by this website several times in the past.

I am posting this new thread to ask a question about seemingly common but non-existing(?) question.

I have 2 text files that I need to convert into TH1D.

The text files consist of two columns where the first column is the left end of the bin and the second column is the number of bin content.

There is a thread that easily converts .txt file into TGraph but I could find none so far for TH1D.

Could you help me for this case?

Thank you!!

I think such a method does not exist for TH1. You have to fill your TH1 by yourself using Fill or SetBinContent

How about TTree::ReadFile?

Thanks for your response, how would the command exactly look like to your the Fill or SetBinContent function?

Could you give command for using TTree::ReadFile?

Thanks! (I am a novice)

Sure, I’ll try to explain how to read the documentation as well as this will benefit you greatly in the future.


TTree::ReadFile indicates that ReadFile is a member of he TTree class and as such you can call the method from any instance of a TTree. The documentation indicates gives the following signature which explains what arguments are expected and what is returned:

Long64_t TTree::ReadFile(const char * filename,
		         const char * branchDescriptor = "",
                         char delimiter = ' ' 

So we need to provides the filename, the others have some default arguments so we don’t necessarily need to provide them. Although if we read the documentation we find that that providing an empty branch descriptor will cause ReadFile to assume the branch descriptor is in the first line of the file:

This also explains that we need to specify a name for each column of data and the type of values they contain, float, double, etc.

Finally we can specify the delimiter between columns, a space is the default (unless the file extension is csv):



With the above information in hand you can do the following:

TTree tree("tree");
float bin, value;
tree.SetBranchAddress("bin", &bin);
tree.SetBranchAddress("value", &value);

//Get the bin edges and values out of the tree
std::vector<float> binEdges, values;
for (int i = 0; i < tree.GetEntries(); i++) {


auto hist = new TH1F("hist", "", binEdges.size() - 1, binEdges.data());
//Fill the histogram
for (int i = 0; i < values.size(); i++) {
   hist->SetBinContent(binEdges.at(i), values.at(i));


Alternatively, you could use TGraph as you have eluded:

TGraph graph("hist.txt");

auto binEdges = graph.GetX();
auto values = graph.GetY();

auto hist = new TH1F("hist", "", graph.GetN() - 1, binEdges);
//Fill the histogram
for (int i = 0; i < values.size(); i++) {
   hist->SetBinContent(binEdges[i], values[i]);

Both of these methods will have a problem with the last bin though as it seems you haven’t indicated the value for the right edge of the final bin. I have not tested either of these scripts.


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