Open txt file and draw histogram in root

时间秒杀一切 提交于 2019-12-08 13:18:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!