# Poisson distribution for a given mean

**URL:** <https://root-forum.cern.ch/t/poisson-distribution-for-a-given-mean/33945>\
**Category:** Roofit and RooStats\
**Created:** [May 7, 2019, 4:10pm UTC](https://root-forum.cern.ch/t/poisson-distribution-for-a-given-mean/33945 "2019-05-07T16:10:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surya001](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/surya001/32/17880_2.png) [@Surya001](https://root-forum.cern.ch/u/Surya001)\
**Post date:** [May 7, 2019, 4:10pm UTC](https://root-forum.cern.ch/t/poisson-distribution-for-a-given-mean/33945/1 "2019-05-07T16:10:55Z")

</div>

Dear Experts,  
Please help me in getting the poisson distribution for a given mean for a given number of iteration, and to to plot them as a histogram.

Thank you!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 7, 2019, 6:21pm UTC](https://root-forum.cern.ch/t/poisson-distribution-for-a-given-mean/33945/2 "2019-05-07T18:21:58Z")

</div>

Some ideas:

> [@\[Solved\] Fit TH1D with Poisson Function](https://root-forum.cern.ch/t/solved-fit-th1d-with-poisson-function/29886/6):
>
> I think the problem is in the “x range” of your histogram. From your picture, I can see that “x\_max = 10.e-6” and the [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution) expects “x\_max \>= 1” (so, in principle you should also have “mean \>= 0.5”). You could try to “scale” your “x range”: { double xmin = 0.; double xmax = 10.e-6; TF1 \*f = new TF1("f", "[0] \* TMath::Poisson((x/[2]), ([1]/[2]))", xmin, xmax); double constant = 160.; double mean = 1.5e-6; double xscaling = 2.e-7; f-\>SetParameters(constant, mean, xsc…

> [@Fitting a Poisson Distribution to a histogram](https://root-forum.cern.ch/t/fitting-a-poisson-distribution-to-a-histogram/12078/7):
>
> 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: { gStyle-\>SetOptFit(112); Double\_t xmin = 0, xmax = 10; // define the Poisson fit function TF1 \*f = new TF1("…

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [May 8, 2019, 7:47am UTC](https://root-forum.cern.ch/t/poisson-distribution-for-a-given-mean/33945/3 "2019-05-08T07:47:14Z")

</div>

Since you asked in the RooFit forum, here is how you would do it:

```auto
unsigned int upperEnd = 100;
RooRealVar x("x", "x", 0, upperEnd);
//We use a custom binning to make all bins start at integer values. Otherwise, the plot might look a bit odd
x.setBinning(RooUniformBinning(0, upperEnd, upperEnd));

//Set mean to 5.
RooRealVar mean("mean", "mean", 5., 0, 100);
RooPoisson pois("Poisson", "Poisson", x, mean);
auto data = pois.generate(x, 100);
auto frame = x.frame();
data->plotOn(frame);
pois.plotOn(frame);
frame->Draw();

//Same with mean at 50:
mean = 50.;
auto data2 = pois.generate(x, 100);
data2->plotOn(frame);
pois.plotOn(frame, RooFit::LineColor(kRed));
frame->Draw();

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 22, 2019, 7:47am UTC](https://root-forum.cern.ch/t/poisson-distribution-for-a-given-mean/33945/4 "2019-05-22T07:47:17Z")

</div>

Closed on May 22, 2019, 7:47am UTC

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