Read txt file and draw histogram

Hi ;
I have got a txt file which include numbers and I want to draw histogram according to this file
I used this code but I havent got a result

{ 
  TFile *f = new TFile("a.txt"); 

  f.ls(); 
  
 TH1F * h1 = (TH1F*)f.Get("h1"); 
  h1->Draw(); 
}

Hi,

you are using the syntax to read an object from a root file.
If the file is an ascii one, you have to read its content and fill the histogram with the Fill function.
See for example Simple pyroot example problem

Cheers,
Danilo

Actually Firstly I should open my file But I cant do it I write

but I cant open

Hi I tried your example but it said .txt is not a root file and can not open file What can I do

This syntax is not valid. You can find plenty documentation outside of this forum on the internet cplusplus.com/forum/beginner/8388/ .

Hi
I try this

c1 = TCanvas("c1","c1",900,700)
h1 = ROOT.TH1F("h1", "", 10, 0, 10)
data = open('data.dat','r')
for x in data:
    print x
    x = Double()
    h1.Fill(x)
data.close()
h1.Draw()

and opened a white page Than I choose my txt file but cannot opened and said is not a root file
Secondly
I try this

string STRING;
	ifstream infile;
	infile.open ("names.txt");
        while(!infile.eof) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
	        cout<<STRING; // Prints our STRING.
        }
	infile.close();
	system ("pause");

MY screen is:
but again I havent got a result:(
I cant open my txt file and I cant draw my histogram

I am not sure I understand.
Clearly the reading part depends on the format of your data.
In addition, the snippets posted are meant to be consistently integrated in a script, probably they will not work if copy and pasted out of the box in the interpreter as they are.

Hmm
I am so confused I want just a draw histogram acording to my .txt file (my txt file which include a lot of number between -3 and 3 (-2.98, 1.25,0,1…) I shuld read this txt file and I should draw histogram for these number But I can’t do it Im resarching…

Hi
T try this method:

root[0]TGraph *t = new TGraph ("a.txt"); root[1]t->Draw("A*");
But it said that illegal number of points(0) What shoul I do?

Attach your “a.txt” fle here.

dosya.co/4qd003c99hce/a.txt.html

{
  TTree *t = new TTree("t", "a tree");
  t->ReadFile("a.txt", "v/D");
  Double_t v_min = t->GetMinimum("v");
  Double_t v_max = t->GetMaximum("v");
  Int_t n = 100;
  Double_t dv = (v_max - v_min) / n;
#if 1 /* 0 or 1 */
  TH1D *h = new TH1D("h", "a histogram",
                     (n + 2), (v_min - dv), (v_max + dv));
#else /* 0 or 1 */
  TH1D *h = new TH1D("h", "a histogram",
                     n, (v_min - dv / n), (v_max + dv / n));
#endif /* 0 or 1 */
  t->Project("h", "v");
  h->Draw();
}
1 Like

Hi;
Thanks your method but While I was writting this code Program wtote this line


You are trying to read a file which contains “binary data”, not “ascii data”.
Download the “ascii file” from the “İndir” www link that you gave in your previous post here (i.e. “a.txt”) and put it in the same subdirectory in which you run ROOT and execute the macro.

Hmm
before your last answer I tried again and my result is:
opened a white page and
is this error connected to your last answer??

Yes, check the “a.txt” file (in the current subdirectory).

I downloaded from “indir”… But didn’t it What is imply “execute macro” I try same code which you posted after Idownloaded file but I haven’t got a result Again program said Can not read file / can not open file

On this Indir www page, there is a button which downloads an “.exe” file but there is also another button which directly downloads the “a.txt” file (in “ascii” form).

dosya.co/4qd003c99hce/a.txt.html
You are saying this page but I only see one button for “indir” and I downloaded it :frowning:

Attached is the “a.txt” file which I downloaded from your Indir www page.
a.txt (14.1 KB)