Dear Rooters,
I‘m facing a fitting problem when binning. I have 2 histograms which display the same physical relation, but hist has far fewer counts than hist2: hist = factor * hist2. I need to obtain the factor. I do that by interpolating hist2 and fitting the interpolated function to hist. The following code breaks it down to an easy example:
TH1F* hist;
TH1F* hist2;
//user defined fit function
Double_t userfitf(Double_t *x, Double_t *par) {
Double_t value = par[0]*hist2->Interpolate(x[0]);
return value;
}
void QuestionRootForum(){
Int_t binningfactor = 1;
gRandom = new TRandom3();
//Create a variable bin size array
Int_t Nbins = 1000*binningfactor;
const Int_t size = Nbins+1;
Double_t xbins[size];
for (int i = 0; i< Nbins+1; i++) xbins[i] = i*1.02/(10*binningfactor);
//Create histos and fill with random gauss
hist = new TH1F("hist","hist",Nbins,xbins);
hist2 = new TH1F("hist2","hist2s",Nbins,xbins);
for (int i = 0; i < 10000; i++) hist->Fill(gRandom->Gaus(50,5));
//histo2 will have 1000 time the counts
for (int i = 0; i < 10000*1000; i++) hist2->Fill(gRandom->Gaus(50,5));
//Fit with user defined fit function
ff = new TF1("ff",userfitf,20,80,1);
hist->Fit("ff","");
}
However the factor (from hist = factor*hist2) depends on the bin width, which I think shouldn‘t be the case. Try changing binningfactor to 10 or some value and compare. Somebody suggested to normalize the fitparameter with the bin width as follows:
Double_t userfunction(Double_t *x, Double_t *par) {
Double_t normalize = hist->GetXaxis()->GetBinWidth(0);
Double_t value = par[0]/normalize*hist2->Interpolate(x[0]);
return value;
}
Is that the correct approach?
It seems a good idea to me, because in the following I will have to multiply this factor to third histogram which needs to have a different bin size than hist & hist2.
However, GetBinWidth neither works with 0 nor x[0]. How do I get it to work?