# Fitting a Poisson Distribution to a histogram

**URL:** <https://root-forum.cern.ch/t/fitting-a-poisson-distribution-to-a-histogram/12078>\
**Category:** ROOT\
**Created:** [March 17, 2011, 3:48pm UTC](https://root-forum.cern.ch/t/fitting-a-poisson-distribution-to-a-histogram/12078 "2011-03-17T15:48:22Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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 1, 2016, 8:38am UTC](https://root-forum.cern.ch/t/fitting-a-poisson-distribution-to-a-histogram/12078/7 "2016-10-01T08:38:55Z")

</div>

I guess, the trick with the parameter “[2]” is to scale “x” and then also “u” (as they appear in your “definition”), so that the “mean” (and thus also the “variance”) remains simply the value of the parameter “[1]”.

You could try to “scale” the “x-axis” of your histogram by the value of the parameter “[2]” (then also possibly “`f1->FixParameter(2, 1.0);`”) and then redo your fit:

```auto
{
  gStyle->SetOptFit(112);
  Double_t xmin = 0, xmax = 10;
  // define the Poisson fit function
  TF1 *f = new TF1("f", "[0]*TMath::Power(([1]/[2]),(x/[2]))*(TMath::Exp(-([1]/[2])))/TMath::Gamma((x/[2])+1.)", xmin, xmax);
  f->SetParameters(1, 1, 1); // you MUST set non-zero initial values for parameters
  // define and "fill" the histogram
  TH1D *h = new TH1D("h", "h", 10, xmin, xmax);
  h->SetBinContent(1, 0);
  h->SetBinContent(2, 2100);
  h->SetBinContent(3, 4400);
  h->SetBinContent(4, 1500);
  h->SetBinContent(5, 200);
  h->SetBinContent(6, 100);
  h->SetBinContent(7, 50);
  h->SetBinContent(8, 50);
  h->SetBinContent(9, 20);
  h->SetBinContent(10, 0);
  // fit the histogram
  h->Fit(f, "R"); // "R" = fit between "xmin" and "xmax" of the "f"
  std::cout << "original mean = " << f->GetParameter(1)
            << " +- " << f->GetParError(1) << std::endl;
  // return; // ... "break" here ...
  // "scale" the "fix bins x-axis" of the histogram
  Double_t s = f->GetParameter(2); // the fitted "scaling" parameter
  xmin /= s; xmax /= s;
  h->GetXaxis()->Set(h->GetNbinsX(), xmin, xmax);
  h->SetTitle("h scaled");
  // fit the "scaled" histogram
  f->SetRange(xmin, xmax); // set the new "scaled" range
  // f->FixParameter(2, 1.0); // fix the "scaling" parameter
  h->Fit(f, "R"); // "R" = fit between "xmin" and "xmax" of the "f"
  std::cout << "scaled mean = " << (s * f->GetParameter(1))
            << " +- " << (s * f->GetParError(1)) << std::endl;
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/fitting-a-poisson-distribution-to-a-histogram/12078)._
