To fill a histogram by reading text file

Hello,

I want to plot a 1D histogram from a .dat file having only one column. I found a code for the same, but it is not giving me the correct values.

I am writing you the code. Please help me to plot this histogram.

TH1F *h = new TH1F("h", "example histogram",100,4.,8.);
ifstream inp; double x;
inp.open("expo.dat");
while(!(inp >> x)==0){h->Fill(x);}
h->Draw();

I am also attaching my expo.dat file for an example.

Thank you.

Regards,
Kajal
expo.dat (7.71 KB)

Does each line of the file correspond to a single bin of the histogram ?
If that’s the case you should not use Fill() but SetBinContent() .

Hello Couet,

It gives me error when I do SetBinContent, instead of Fill.
(Error: Can’t call TH1F::SetBinContent(x) in current scope (tmpfile):1:
Possible candidates are…
(in TH1F)
)

Can you help me by telling some other way to plot a histogram from a .dat file?

Thank you.

Kajal

look at the SetBinContent help (I added the URL in my previous post … it has 2 parameters … not one).

Hello Couet,

Yes, there are two parameters. Sorry for bothering you again, I am very new to this coding world. How to I set the bin number (i.e., the first parameter)?

{
   TH1F *h = new TH1F("h", "example histogram",1000,4.,8.);
   ifstream inp; double x;
   inp.open("expo.dat");
   for (int i=1; i<=1000; i++) {
      inp >> x;
      h->SetBinContent(i,x);
   }
   h->Draw();
}

Hello,

The code is making a canvas which is empty.
Is this a problem because of my root version? I am using Version 5.34/30 . :unamused:
Thank you.

Kajal

I just tried again the attached macro and it is fine with 5.34
expo.C (212 Bytes)

Thank you Couet :slight_smile:

Hello Couet,

I need to fill a histogram by reading text file, i tried your macro but it doesn’t work for me :confused:

Can u help me , please ?

Thanks

Ayoub

To help you I would need the example you wrote.

Here is the code that i wrote !

macroex.C (268 Bytes)

I’m also attaching my txtfile (my example)

Vop_Iavg_stdDEV.txt (502 Bytes)

Your macro is just a copy/paste of a well know root example. You do not try to read the file.
Take the example in this post as model to learn how to read the file.

This one ?

{
   TH1F *h = new TH1F("h", "example histogram",1000,4.,8.);
   ifstream inp; double x;
   inp.open("expo.dat");
   for (int i=1; i<=1000; i++) {
      inp >> x;
      h->SetBinContent(i,x);
   }
   h->Draw();
}

Yes you should at least read your file. That’s basic C++ that’s not a ROOT question.
Note also that your file has 3 columns. and you seems to attempt to make a TGraph … which takes only 2 arrays …

http://www.cplusplus.com/doc/tutorial/files/

1 Like

TGraph doesn’t take more than 2 arrays ?

By definition a TGraph is an X,Y plot … so, yes, two arrays only…
This is clearly said on the first line of the doc:
https://root.cern/doc/master/classTGraph.html

Macro2.C (741 Bytes)
chiffres.txt (116 Bytes)

An error occurred : Warning : TGraphErrors is Zombie

I do not see any problem:

$ root Macro2.C 
   ----------------------------------------------------------------
  | Welcome to ROOT 6.09/03                    http://root.cern.ch |
  |                                   (c) 1995-2017, The ROOT Team |
  | Built for macosx64                                             |
  | From heads/master@v6-09-02-487-ga8ffe35, Apr 18 2017, 09:47:08 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'     |
   ----------------------------------------------------------------

root [0] 
Processing Macro2.C...
x[0]=1, y[0]=6, ex[0]=0, ey[0]=5
x[1]=2, y[1]=12, ex[1]=0, ey[1]=5
x[2]=3, y[2]=14, ex[2]=0, ey[2]=4.7
x[3]=4, y[3]=20, ex[3]=0, ey[3]=4.5
x[4]=5, y[4]=22, ex[4]=0, ey[4]=4.2
x[5]=6, y[5]=24, ex[5]=0, ey[5]=5.1
x[6]=7, y[6]=35, ex[6]=0, ey[6]=2.9
x[7]=8, y[7]=45, ex[7]=0, ey[7]=4.1
x[8]=9, y[8]=44, ex[8]=0, ey[8]=4.8
x[9]=10, y[9]=53, ex[9]=0, ey[9]=5.43
(int) 0
root [1]