Sure, Iβll try to explain how to read the documentation as well as this will benefit you greatly in the future.
Documentation
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:
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):
Examples
TTree
With the above information in hand you can do the following:
TTree tree("tree");
tree.ReadFile("hist.txt","bin:value");
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++) {
tree.GetEntry(i);
binEdges.push_back(bin);
values.push_back(value);
}
tree.ResetBranchAddresses();
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));
}
TGraph
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.