CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CommonTools/Statistics/src/GammaSeries.cc

Go to the documentation of this file.
00001 #include "CommonTools/Statistics/src/GammaSeries.h"
00002 #include <iostream>
00003 #include <cmath>
00004 
00005 #define ITMAX 100             // maximum allowed number of iterations
00006 #define EPS 3.0e-7            // relative accuracy
00007 
00008 float GammaSeries( float a, float x )
00009 {
00010   if( x < 0.0 ) 
00011     std::cerr << "GammaSeries::negative argument x" << std::endl;
00012 
00013   if( x == 0. )
00014     return 0.;
00015 
00016   // coefficient c_n of x^n is Gamma(a)/Gamma(a+1+n), which leads to the
00017   // recurrence relation c_n = c_(n-1) / (a+n-1) with c_0 = 1/a
00018   double term = 1/a;
00019   double sum = term;
00020   double aplus = a;
00021   for( int index = 1; index <= ITMAX; index++) {
00022     ++aplus;
00023     term *= x/aplus;
00024     sum += term;
00025     if( fabs(term) < fabs(sum)*EPS )
00026       // global coefficient e^-x * x^a / Gamma(a)
00027       return sum;
00028   }
00029   std::cerr << "GammaSeries::a too large, ITMAX too small" << std::endl;
00030   return 0.;
00031 }
00032