``` /*****************************************************************************
* Project: RooFit                                                           *
*                                                                           *
* Simple Poisson PDF
* author: Kyle Cranmer <cranmer@cern.ch>
*                                                                           *
*****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// Poisson pdf
// END_HTML
//

#include <iostream>

#include "RooPoisson.h"
#include "RooAbsReal.h"
#include "RooAbsCategory.h"

#include "RooRandom.h"
#include "RooMath.h"
#include "TMath.h"

ClassImp(RooPoisson)

//_____________________________________________________________________________
RooPoisson::RooPoisson(const char *name, const char *title,
RooAbsReal& _x,
RooAbsReal& _mean) :
RooAbsPdf(name,title),
x("x","x",this,_x),
mean("mean","mean",this,_mean)
{
// Constructor
}

//_____________________________________________________________________________
RooPoisson::RooPoisson(const RooPoisson& other, const char* name) :
RooAbsPdf(other,name),
x("x",this,other.x),
mean("mean",this,other.mean)
{
// Copy constructor
}

//_____________________________________________________________________________
Double_t RooPoisson::evaluate() const
{
// Implementation in terms of the TMath Poisson function

Double_t k = floor(x);
return TMath::Poisson(k,mean) ;
}

//_____________________________________________________________________________
Int_t RooPoisson::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
{
if (matchArgs(allVars,analVars,x)) return 1 ;
return 0 ;
}

//_____________________________________________________________________________
Double_t RooPoisson::analyticalIntegral(Int_t code, const char* rangeName) const
{
assert(code==1) ;

// Implement integral over x as summation. Add special handling in case
// range boundaries are not on integer values of x
Double_t xmin = x.min(rangeName) ;
Double_t xmax = x.max(rangeName) ;

Int_t ixmin = Int_t (xmin) ;
Int_t ixmax = Int_t (xmax)+1 ;

Double_t fracLoBin = 1-(xmin-ixmin) ;
Double_t fracHiBin = 1-(ixmax-xmax) ;

if(ixmin == ixmax-1){ // first bin
return TMath::Poisson(ixmin, mean)*(xmax-xmin);
}

Double_t sum(0) ;
sum += TMath::Poisson(ixmin,mean)*fracLoBin ;
for (int i=ixmin+1 ; i<ixmax-1 ; i++) {
sum += TMath::Poisson(i,mean)  ;
}
sum += TMath::Poisson(ixmax-1,mean)*fracHiBin ;

return sum ;

}

//_____________________________________________________________________________
Int_t RooPoisson::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t /*staticInitOK*/) const
{
// Advertise internal generator in x

if (matchArgs(directVars,generateVars,x)) return 1 ;
return 0 ;
}

//_____________________________________________________________________________
void RooPoisson::generateEvent(Int_t code)
{
// Implement internal generator using TRandom::Poisson

assert(code==1) ;
Double_t xgen ;
while(1) {
xgen = RooRandom::randomGenerator()->Poisson(mean);
if (xgen<x.max() && xgen>x.min()) {
x = xgen ;
break;
}
}
return;
}

```
RooPoisson.cxx:1
RooPoisson.cxx:2
RooPoisson.cxx:3
RooPoisson.cxx:4
RooPoisson.cxx:5
RooPoisson.cxx:6
RooPoisson.cxx:7
RooPoisson.cxx:8
RooPoisson.cxx:9
RooPoisson.cxx:10
RooPoisson.cxx:11
RooPoisson.cxx:12
RooPoisson.cxx:13
RooPoisson.cxx:14
RooPoisson.cxx:15
RooPoisson.cxx:16
RooPoisson.cxx:17
RooPoisson.cxx:18
RooPoisson.cxx:19
RooPoisson.cxx:20
RooPoisson.cxx:21
RooPoisson.cxx:22
RooPoisson.cxx:23
RooPoisson.cxx:24
RooPoisson.cxx:25
RooPoisson.cxx:26
RooPoisson.cxx:27
RooPoisson.cxx:28
RooPoisson.cxx:29
RooPoisson.cxx:30
RooPoisson.cxx:31
RooPoisson.cxx:32
RooPoisson.cxx:33
RooPoisson.cxx:34
RooPoisson.cxx:35
RooPoisson.cxx:36
RooPoisson.cxx:37
RooPoisson.cxx:38
RooPoisson.cxx:39
RooPoisson.cxx:40
RooPoisson.cxx:41
RooPoisson.cxx:42
RooPoisson.cxx:43
RooPoisson.cxx:44
RooPoisson.cxx:45
RooPoisson.cxx:46
RooPoisson.cxx:47
RooPoisson.cxx:48
RooPoisson.cxx:49
RooPoisson.cxx:50
RooPoisson.cxx:51
RooPoisson.cxx:52
RooPoisson.cxx:53
RooPoisson.cxx:54
RooPoisson.cxx:55
RooPoisson.cxx:56
RooPoisson.cxx:57
RooPoisson.cxx:58
RooPoisson.cxx:59
RooPoisson.cxx:60
RooPoisson.cxx:61
RooPoisson.cxx:62
RooPoisson.cxx:63
RooPoisson.cxx:64
RooPoisson.cxx:65
RooPoisson.cxx:66
RooPoisson.cxx:67
RooPoisson.cxx:68
RooPoisson.cxx:69
RooPoisson.cxx:70
RooPoisson.cxx:71
RooPoisson.cxx:72
RooPoisson.cxx:73
RooPoisson.cxx:74
RooPoisson.cxx:75
RooPoisson.cxx:76
RooPoisson.cxx:77
RooPoisson.cxx:78
RooPoisson.cxx:79
RooPoisson.cxx:80
RooPoisson.cxx:81
RooPoisson.cxx:82
RooPoisson.cxx:83
RooPoisson.cxx:84
RooPoisson.cxx:85
RooPoisson.cxx:86
RooPoisson.cxx:87
RooPoisson.cxx:88
RooPoisson.cxx:89
RooPoisson.cxx:90
RooPoisson.cxx:91
RooPoisson.cxx:92
RooPoisson.cxx:93
RooPoisson.cxx:94
RooPoisson.cxx:95
RooPoisson.cxx:96
RooPoisson.cxx:97
RooPoisson.cxx:98
RooPoisson.cxx:99
RooPoisson.cxx:100
RooPoisson.cxx:101
RooPoisson.cxx:102
RooPoisson.cxx:103
RooPoisson.cxx:104
RooPoisson.cxx:105
RooPoisson.cxx:106
RooPoisson.cxx:107
RooPoisson.cxx:108
RooPoisson.cxx:109
RooPoisson.cxx:110
RooPoisson.cxx:111
RooPoisson.cxx:112
RooPoisson.cxx:113
RooPoisson.cxx:114
RooPoisson.cxx:115
RooPoisson.cxx:116
RooPoisson.cxx:117
RooPoisson.cxx:118
RooPoisson.cxx:119
RooPoisson.cxx:120
RooPoisson.cxx:121
RooPoisson.cxx:122
RooPoisson.cxx:123
RooPoisson.cxx:124
RooPoisson.cxx:125
RooPoisson.cxx:126
RooPoisson.cxx:127
RooPoisson.cxx:128
RooPoisson.cxx:129
RooPoisson.cxx:130
RooPoisson.cxx:131
RooPoisson.cxx:132
RooPoisson.cxx:133
RooPoisson.cxx:134
RooPoisson.cxx:135
RooPoisson.cxx:136
RooPoisson.cxx:137
RooPoisson.cxx:138
RooPoisson.cxx:139
RooPoisson.cxx:140
RooPoisson.cxx:141
RooPoisson.cxx:142
RooPoisson.cxx:143