Centering histogram bins for fitting

Hello-

I’m running into an issue where I fill my histograms, but when I fit them with a TF1, it fits to the middle of the bins, meaning my 0 bin is fitted as though it’s 0.5, 1 is fitted to 1.5, etc. Is there a way to either fit to the left bin edge, or fill the histogram such that the bins are centered instead of the 0 going from 0-1, etc?

Some sample code similar to what I’m doing:
(assume I have a tree called ‘tr’ with data in it)
TH1F *h_t1= new TH1F(“h_t1”,“Title”,numbins, lowbin, highbin);
for (int i =0; i<num_of_entries;i++)
{
tr->GetEntry(i);
h_t1->Fill(brd_t1_comparison-1000);
}
TCanvas *c_t1 = new TCanvas();
TF1 *gausfit = new TF1(“gausfit”,"[0]exp(-0.5((x-[1])/[2])^2)", -6, 6);
h_t1->Draw();
h_t1->Draw(“E0,SAME”);
h_t1->Fit(gausfit,"");

I’ve tried doing h_t1->SetBarOffset(-0.5), but that only shifts where the bins are drawn, not where the TF1 fits them. Is there an easy way to do this that I’m missing?

Thanks for any help you can provide!


_ROOT Version:6.06.04
_Platform:Scientific Linux 7
Compiler: Not Provided


This is purely graphics. Nothing to do with the fitting.

1 Like
TH1F *h_t1= new TH1F("h_t1", "Title", numbins, (lowbin - 0.5), (highbin + 0.5));

or simply:

TH1F *h_t1= new TH1F("h_t1", "Title", numbins, (lowbin - 0.5), (numbins - 0.5));

BTW. Search the TH1 class reference for the “Convention for numbering bins”.

I figured! Thanks for confirming my suspicions :slight_smile:

Thanks a million! I got it to work using this while adding an extra bin onto the specified numbins, so everything worked out.

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