HOW to put an array into a one-dimensional histogram?


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();
}

image
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,

can you show the content of your array?

As for your code:

  1. 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++)
  1. Please move
h1->Draw();

out of the loop.

for(int i = 0; i < 50; i++) h1->SetBinContent(i + 1, cs[i]);

Thanks for your reply.This is the array:

All of your numbers are outside of (-5, +5). Then why do you try to put them in a histogram spanning (-5, +5)?

I have found this mistake and correct it and now i think it is right. Thanks for your correction.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.