OmnipotentEntity
OmnipotentEntity

Reputation: 17131

Plot only specific bins in a histogram

What's the best way to plot a portion of a histogram (while preserving error data)?

For instance, I have a histogram with about 16k bins, but I'd like to only plot, say, bins 12200 - 13500. However, this histogram is made from subtracting two other histograms. So it has error data that's different from the standard Poisson error data, (I assume! If it doesn't, then I also need to figure out how to make error propagate properly.)

My approach was:

TH1D noBgCounts(counts - bgcounts);
auto noBgCounts_plot = new TH1D("Plot", titleString.c_str(), end_bin - start_bin, start_bin, end_bin);
for (int i = 0; i < end_bin - start_bin; ++i) {
  noBgCounts_plot->SetBinContent(i, noBgCounts.GetBinContent(start_bin+i));
}

And then plot noBgCounts_plot, but I'm reasonably certain looking at the return type of GetBinContent that this doesn't preserve error, so I'm left with the standard sqrt error.

I also need to fix a gaussian to this data (noBgCounts_plot), does the gaussian use the errors on the bins to formulate the errors of its parameters? (I assume so!) If so how do I fit only a portion of the histogram?

Thanks for the help!

Upvotes: 0

Views: 1251

Answers (1)

pseyfert
pseyfert

Reputation: 3603

The argument of SetBinContent and the return value of GetBinContent are really only float/double, so you copy only the central value of each histogram bin. If you also want to copy the error, then SetBinError and GetBinError can be used.

In your case, where all the bins are next to each other, you're better off with TAxis::SetRangeUser or SetRange.

TH1F* h = new TH1F("h","H",100,-5,5);
for (int i = 0 ; i < 1000; i ++) {
   h->FillRandom("gaus");
}
h->Draw();
h->GetXaxis()->SetRangeUser(-3,-1); // only draw axis range from -3 to -1
h->GetXaxis()->SetRange(10,15); // only draw bins 10 to 15
h->Draw();

When fitting with TH1::Fit (TF1 *f1, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0), the arguments xmin and xmax` set your fit range.

Upvotes: 3

Related Questions