kajal
September 16, 2016, 1:36pm
#1
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)
couet
September 16, 2016, 1:46pm
#2
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() .
kajal
September 16, 2016, 1:50pm
#3
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
couet
September 16, 2016, 1:53pm
#4
look at the SetBinContent help (I added the URL in my previous post … it has 2 parameters … not one).
kajal
September 16, 2016, 1:57pm
#5
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)?
couet
September 16, 2016, 2:12pm
#6
{
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();
}
kajal
September 16, 2016, 2:27pm
#7
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 .
Thank you.
Kajal
couet
September 19, 2016, 7:37am
#8
I just tried again the attached macro and it is fine with 5.34
expo.C (212 Bytes)
Ayoub
April 12, 2017, 12:02pm
#10
Hello Couet,
I need to fill a histogram by reading text file, i tried your macro but it doesn’t work for me
Can u help me , please ?
Thanks
Ayoub
couet
April 12, 2017, 12:07pm
#11
To help you I would need the example you wrote.
Ayoub
April 12, 2017, 1:31pm
#12
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)
couet
April 12, 2017, 2:56pm
#13
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.
Ayoub
April 12, 2017, 3:00pm
#14
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();
}
couet
April 12, 2017, 3:12pm
#15
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 …
couet
April 12, 2017, 3:13pm
#16
1 Like
Ayoub
April 18, 2017, 9:51am
#17
TGraph doesn’t take more than 2 arrays ?
couet
April 18, 2017, 10:44am
#18
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
Ayoub
April 18, 2017, 11:07am
#19
Macro2.C (741 Bytes)
chiffres.txt (116 Bytes)
An error occurred : Warning : TGraphErrors is Zombie
couet
April 18, 2017, 11:35am
#20
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]