To get random values from poisson distribution

Hello,

I will be very thankful to get some help for my code:
I have a Gaussian histogram of 4 bins. the code to get the histogram is below:

TH1F *h1 = new TH1F("h1","d=1.2 mm,t=0.4mm",8,0,24);
TF1 f1("f1", "TMath::Abs(1/(TMath::Sqrt(2)*TMath::Sqrt(TMath::Pi())*(1.06)))*TMath::Exp(-((x-6.6)*(x-6.6))/2.2472)", 0, 24);
 h1->FillRandom("f1",400);
 h1->Draw();

For each bin, I have error given by Poisson distribution. Therefore, I want to plot the same Gaussian histogram having value for each bin randomly selected by the Poisson distribution
But when I do it, the get random function is not selecting all the random values from poisson distribution.

[code]// Define poisson for different bins:

TF1 *f1 = new TF1(“f1”, “TMath::Poisson(x,1)”,0,6);
TF1 *f2 = new TF1(“f2”, “TMath::Poisson(x,120)”,70,180);
TF1 *f3 = new TF1(“f3”, “TMath::Poisson(x,272)”,200,360);
TF1 *f4 = new TF1(“f4”, “TMath::Poisson(x,7)”,0,30);

// To redefine the same histogram with randomly selected values

TH1F *h = new TH1F(“h”, “d=0.8 and t = 0.7mm”, 8, 0, 24);
for (Int_t i=0; i<1000; i++){ h->Fill(1.5, f1->GetRandom()); h->Fill(4.5, f2->GetRandom()); h->Fill(7.5, f3->GetRandom()); h->Fill(10.5, f4->GetRandom()); cout<GetMean()<<endl;}[/code]

Thank you.

Kajal

Hi,

To get Random number from the Poisosn distribution, you should use the TRandom::Poisson function and not create a TF1. The number you obtained with the TF1 are also generated according to the given TF1 range

Best Regards

Lorenzo

Hello Lorenzo,

Thank you for your help. It makes my code easier :smiley:
Also, I figured out that I had to include the TH1F in the “For loop” along with “delete h” to generate random numbers.

Thank you so much. :slight_smile:

Kajal