问题
I have a txt file which contains numbers. I want to draw a histogram according to this file in root. But I can't do this.
{
TFile *f = new TFile("myfile.root");
f.ls();
TH1F * h1 = (TH1F*)f.Get("h1");
h1->Draw();
}
回答1:
Have a look at this tutorial from the ROOT website. As far as I know ROOT can't open a plain text file directly. The TFile("myfile.root")
call can be used to open a ROOT file (i.e. a binary file created by ROOT containing persistified ROOT objects such as histograms), which as you point out causes errors if you try to open a plain text file using it. The approach shown in the tutorial page is to create a TH1, then use standard C++ I/O streams to read from your text fiule and fill the histogram in the normal way.
来源:https://stackoverflow.com/questions/34933112/open-txt-file-and-draw-histogram-in-root