meixue
December 26, 2020, 11:21am
1
Please read tips for efficient and successful posting and posting code
I am trying to put an array cs[50] (which is double type) into a one-dimensional histogram, but my code have some problem,and the TH1D is like this. Can you help me to correct my code?
The following is my code:
TCanvas *c2=new TCanvas("c2","cs",600,600);
TH1D *h1=new TH1D("h1","cs",50,-5,5);
for(int i=0;i<50;i++)
{
double x =cs[i];
h1->Fill(x);
h1->Draw();
}
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided
yus
December 26, 2020, 12:12pm
2
Hi,
can you show the content of your array?
As for your code:
You shouldn’t start from bin #0 , so please replace
for(int i=0;i<50;i++)
with
for(int i=1;i<=50;i++)
Please move
h1->Draw();
out of the loop.
for(int i = 0; i < 50; i++) h1->SetBinContent(i + 1, cs[i]);
meixue
December 27, 2020, 7:38am
4
Thanks for your reply.This is the array:
yus
December 27, 2020, 10:26am
5
All of your numbers are outside of (-5, +5). Then why do you try to put them in a histogram spanning (-5, +5)?
meixue
December 27, 2020, 11:03am
6
I have found this mistake and correct it and now i think it is right. Thanks for your correction.
system
Closed
January 10, 2021, 11:03am
7
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.