Histogram Coordinate Points

ROOT Version (e.g. 6.12/02): 6.12
Platform, compiler (e.g. CentOS 7.3, gcc6.2):

Greetings,

I have a histogram and I would like to print the (x,y) coordinate points of each bin in a list. Is there a way I can do this?

Thanks,

Daniela Olivera

Hi Daniela,

perhaps the way to do it is to loop over bins and get their content (GetBinContent) and centre (GetBinCentre)?

Best,
D

Hello Danilo,

Thank you for your response. I am an undergrad student new to ROOT. Therefore, I am not sure how to do what you suggested and I was wondering if you could please give me an example on how to do it.

Thank you for your help and time,

Daniela

``````{
auto *h1 = new TH1F("h1","h1",100,-3,3);
h1->FillRandom("gaus",10000);
for (int i=1; i<=100; i++) {
printf("x[%d] = %g y[%d] = %g\n",i,h1->GetBinCenter(i),i,h1->GetBinContent(i));
}
}
``````

Thank you so much for this. I really appreciate it.

This is what I have now and I get the list of coordinates printed to the screen.

``````TH2F *h2 = new TH2F("h2","",40,-4,4,40,-20,20);
Float_t px, py;
for(Int_t i = 0; i <25000; i++) {
gRandom->Rannor(px,py);
h2->Fill(px,5*py);
}
h2->Draw("COL");
for (int i=1; i<=40; i++) {
printf("x[%d] = %g, ",i,h2->GetXaxis()->GetBinCenter(i));
printf("y[%d] = %g\n",i,h2->GetYaxis()->GetBinCenter(i));
}
``````

Is there any way to have them printed to a .txt file?

Thanks,

Daniela

ofstream myfile;
myfile.open(“bins.txt”);
TH2F h2 = new TH2F(“h2”,"",40,-4,4,40,-20,20);
Float_t px, py;
for(Int_t i = 0; i <25000; i++) {
gRandom->Rannor(px,py);
h2->Fill(px,5
py);
}
h2->Draw(“COL”);
for (int i=1; i<=40; i++) {
printf("x[%d] = %g, ",i,h2->GetXaxis()->GetBinCenter(i));
printf(“y[%d] = %g\n”,i,h2->GetYaxis()->GetBinCenter(i));
myfile << h2->GetXaxis()->GetBinCenter(i) << " " << h2->GetBinContent(i) << endl;
}
myfile.close();

1 Like

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