Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooPoisson.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2 * Project: RooFit *
3 * *
4 * Simple Poisson PDF
5 * author: Kyle Cranmer <cranmer@cern.ch>
6 * *
7 *****************************************************************************/
8
9/** \class RooPoisson
10 \ingroup Roofit
11
12Poisson pdf
13**/
14
15#include "RooPoisson.h"
16#include "RooRandom.h"
17#include "RooMath.h"
18#include "RooNaNPacker.h"
19#include "RooBatchCompute.h"
20
22
24
25////////////////////////////////////////////////////////////////////////////////
26/// Constructor
27
28RooPoisson::RooPoisson(const char *name, const char *title,
29 RooAbsReal& _x,
30 RooAbsReal& _mean,
31 Bool_t noRounding) :
32 RooAbsPdf(name,title),
33 x("x","x",this,_x),
34 mean("mean","mean",this,_mean),
35 _noRounding(noRounding)
36{
37}
38
39////////////////////////////////////////////////////////////////////////////////
40/// Copy constructor
41
42 RooPoisson::RooPoisson(const RooPoisson& other, const char* name) :
43 RooAbsPdf(other,name),
44 x("x",this,other.x),
45 mean("mean",this,other.mean),
46 _noRounding(other._noRounding),
47 _protectNegative(other._protectNegative)
48{
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Implementation in terms of the TMath::Poisson() function.
53
55{
56 Double_t k = _noRounding ? x : floor(x);
57 if(_protectNegative && mean<0) {
58 RooNaNPacker np;
59 np.setPayload(-mean);
60 return np._payload;
61 }
62 return TMath::Poisson(k,mean) ;
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// Compute multiple values of the Poisson distribution.
67void RooPoisson::computeBatch(cudaStream_t* stream, double* output, size_t nEvents, RooFit::Detail::DataMap const& dataMap) const
68{
70 dispatch->compute(stream, RooBatchCompute::Poisson, output, nEvents,
71 {dataMap.at(x), dataMap.at(mean)},
72 {static_cast<double>(_protectNegative), static_cast<double>(_noRounding)});
73}
74
75////////////////////////////////////////////////////////////////////////////////
76
77Int_t RooPoisson::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
78{
79 if (matchArgs(allVars,analVars,x)) return 1 ;
80 if (matchArgs(allVars, analVars, mean)) return 2;
81 return 0 ;
82}
83
84////////////////////////////////////////////////////////////////////////////////
85
86Double_t RooPoisson::analyticalIntegral(Int_t code, const char* rangeName) const
87{
88 R__ASSERT(code == 1 || code == 2) ;
89
90 if(_protectNegative && mean<0)
91 return exp(-2*mean); // make it fall quickly
92
93 if (code == 1) {
94 // Implement integral over x as summation. Add special handling in case
95 // range boundaries are not on integer values of x
96 const double xmin = std::max(0., x.min(rangeName));
97 const double xmax = x.max(rangeName);
98
99 if (xmax < 0. || xmax < xmin) {
100 return 0.;
101 }
102 if (!x.hasMax() || RooNumber::isInfinite(xmax)) {
103 //Integrating the full Poisson distribution here
104 return 1.;
105 }
106
107 // The range as integers. ixmin is included, ixmax outside.
108 const unsigned int ixmin = xmin;
109 const unsigned int ixmax = std::min(xmax + 1.,
110 (double)std::numeric_limits<unsigned int>::max());
111
112 // Sum from 0 to just before the bin outside of the range.
113 if (ixmin == 0) {
114 return ROOT::Math::poisson_cdf(ixmax - 1, mean);
115 }
116 else {
117 // If necessary, subtract from 0 to the beginning of the range
118 if (ixmin <= mean) {
119 return ROOT::Math::poisson_cdf(ixmax - 1, mean) - ROOT::Math::poisson_cdf(ixmin - 1, mean);
120 }
121 else {
122 //Avoid catastrophic cancellation in the high tails:
123 return ROOT::Math::poisson_cdf_c(ixmin - 1, mean) - ROOT::Math::poisson_cdf_c(ixmax - 1, mean);
124 }
125
126 }
127
128 } else if(code == 2) {
129
130 // the integral with respect to the mean is the integral of a gamma distribution
131 Double_t mean_min = mean.min(rangeName);
132 Double_t mean_max = mean.max(rangeName);
133
134 Double_t ix;
135 if(_noRounding) ix = x + 1;
136 else ix = Int_t(TMath::Floor(x)) + 1.0; // negative ix does not need protection (gamma returns 0.0)
137
138 return ROOT::Math::gamma_cdf(mean_max, ix, 1.0) - ROOT::Math::gamma_cdf(mean_min, ix, 1.0);
139 }
140
141 return 0;
142
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// Advertise internal generator in x
147
148Int_t RooPoisson::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t /*staticInitOK*/) const
149{
150 if (matchArgs(directVars,generateVars,x)) return 1 ;
151 return 0 ;
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Implement internal generator using TRandom::Poisson
156
158{
159 R__ASSERT(code==1) ;
160 Double_t xgen ;
161 while(1) {
163 if (xgen<=x.max() && xgen>=x.min()) {
164 x = xgen ;
165 break;
166 }
167 }
168 return;
169}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
#define ClassImp(name)
Definition Rtypes.h:364
#define R__ASSERT(e)
Definition TError.h:118
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:64
Bool_t matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
virtual void compute(cudaStream_t *, Computer, RestrictArr, size_t, const VarVector &, const ArgVector &={})=0
auto & at(RooAbsArg const *arg, RooAbsArg const *=nullptr)
Definition DataMap.h:88
static Int_t isInfinite(Double_t x)
Return true if x is infinite by RooNumBer internal specification.
Definition RooNumber.cxx:58
Poisson pdf.
Definition RooPoisson.h:19
RooRealProxy x
Definition RooPoisson.h:46
void computeBatch(cudaStream_t *, double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Compute multiple values of the Poisson distribution.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
bool _noRounding
Definition RooPoisson.h:48
bool _protectNegative
Definition RooPoisson.h:49
void generateEvent(Int_t code) override
Implement internal generator using TRandom::Poisson.
Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, Bool_t staticInitOK=kTRUE) const override
Advertise internal generator in x.
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const override
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
double evaluate() const override
Implementation in terms of the TMath::Poisson() function.
RooRealProxy mean
Definition RooPoisson.h:47
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:53
bool hasMax(const char *rname=0) const
Check if the range has a upper bound. This requires the payload to be RooAbsRealLValue or derived.
double min(const char *rname=0) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
double max(const char *rname=0) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
virtual Int_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition TRandom.cxx:402
double poisson_cdf(unsigned int n, double mu)
Cumulative distribution function of the Poisson distribution Lower tail of the integral of the poisso...
double poisson_cdf_c(unsigned int n, double mu)
Complement of the cumulative distribution function of the Poisson distribution.
double gamma_cdf(double x, double alpha, double theta, double x0=0)
Cumulative distribution function of the gamma distribution (lower tail).
Double_t x[n]
Definition legend1.C:17
R__EXTERN RooBatchComputeInterface * dispatchCUDA
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
Double_t Floor(Double_t x)
Definition TMath.h:653
Double_t Poisson(Double_t x, Double_t par)
Compute the Poisson distribution function for (x,par).
Definition TMath.cxx:564
Little struct that can pack a float into the unused bits of the mantissa of a NaN double.
double _payload
void setPayload(float payload)
Pack float into mantissa of NaN.
static void output(int code)
Definition gifencode.c:226