# Plotting a portion of a histogram?

**URL:** <https://root-forum.cern.ch/t/plotting-a-portion-of-a-histogram/22053>\
**Category:** ROOT\
**Created:** [October 16, 2016, 12:33am UTC](https://root-forum.cern.ch/t/plotting-a-portion-of-a-histogram/22053 "2016-10-16T00:33:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![OmnipotentEntity](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/41988e/32.png) [@OmnipotentEntity](https://root-forum.cern.ch/u/OmnipotentEntity)\
**Post date:** [October 16, 2016, 12:33am UTC](https://root-forum.cern.ch/t/plotting-a-portion-of-a-histogram/22053/1 "2016-10-16T00:33:22Z")

</div>

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?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [October 16, 2016, 9:52am UTC](https://root-forum.cern.ch/t/plotting-a-portion-of-a-histogram/22053/2 "2016-10-16T09:52:12Z")

</div>

`{
  gStyle->SetOptFit();
  TH1::SetDefaultSumw2(kTRUE); // ensure proper error propagation
  TH1D *counts = new TH1D("counts", "full histogram", 100, -5, 5);
  // counts->Sumw2(kTRUE); // not needed if "TH1::SetDefaultSumw2"
  counts->FillRandom("gaus", 11000);
  TH1D *bgcounts = new TH1D("bgcounts", "background", 100, -5, 5);
  // bgcounts->Sumw2(kTRUE); // not needed if "TH1::SetDefaultSumw2"
  bgcounts->FillRandom("gaus", 1000);
  TH1D *noBgCounts = new TH1D(*counts); // make a copy of "counts"
  noBgCounts->SetNameTitle("noBgCounts", "full histogram - background");
  // noBgCounts->Sumw2(kTRUE); // not needed if "TH1::SetDefaultSumw2"
  noBgCounts->Add(bgcounts, -1.0); // subtract "bgcounts"
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(2, 2);
  c->cd(1);
  counts->Draw();
  c->cd(2);
  bgcounts->Draw();
  c->cd(3);
  noBgCounts->DrawCopy(); // "DrawCopy" because we change its "range" below
  c->cd(4);
  // set the viewing range for the X axis
#if 0 /* 0 or 1 */
  Int_t start_bin = 30, end_bin = 70;
  noBgCounts->GetXaxis()->SetRange(start_bin, end_bin); // "bin numbers"
#else /* 0 or 1 */
  Double_t xmin = -2, xmax = 2;
  noBgCounts->GetXaxis()->SetRangeUser(xmin, xmax); // "user coordinates"
#endif /* 0 or 1 */
  // noBgCounts->Draw();
  noBgCounts->Fit("gaus");
  c->cd(3);
  noBgCounts->GetFunction("gaus")->Draw("SAME");
  c->cd(0);
}`
