Hi all,
I am having a problem while using automatic binning in 2D histogram. Here is the code
#include "TFile.h"
#include "TH2.h"
int main(){
double n = 10;
double x;
double y;
TFile *file = TFile::Open("test.root","recreate");
TH2D *test = new TH2D("test","test hist",1,0,0,1,0,0);
for (int i=0; i<n; i++ ){
x= i*i;
y=(i*i)*i;
test->Fill(x,y);
}
file->Write();
file->Close();
delete file;
}
It gives an empty histogram.
Can anyone please tell me what is wrong?
Thanks in advance.
brun
December 3, 2009, 2:33pm
2
You do not indicate which version you use. I suggest to do the following:
[code]#include “TFile.h”
#include “TH2.h”
int main(){
double n = 10;
double x;
double y;
TFile *file = TFile::Open(“test.root”,“recreate”);
TH2D *test = new TH2D(“test”,“test hist”,10,0,1,10,0,1);
test->SetBit(TH1::kCanRebin);
for (int i=0; i<n; i++ ){
x= ii;
y=(i i)*i;
test->Fill(x,y);
}
file->Write();
file->Close();
delete file;
} [/code]
That works. But when the y range is large, information in the lower bins gets lost because size of the bins is increased.
brun
December 4, 2009, 7:20am
4
Yes, this is obvious. Create your histogram with more bins.
Rene
I tried that. But in one case, i need large y range ie. minimum y value is 0.ooooo1 and maximum y value is 1000 and if the number of bins is greater than 10000, it gives memory allocation error.
brun
December 4, 2009, 8:11am
6
2 solutions
-use variable bin width histograms
-histogram teh log of your variable instead of the variable itself
Rene
Hi,
is there a way to have a histogram with a variable range but with a fixed number of bins?
Thank you,
Oleksiy
oh, never mind. That is exactly what “TH1::SetBit(TH1::kCanRebin)” is doing.
Thank you.