Bugfix in AliPoints2Memory
[u/mrichter/AliRoot.git] / GEANT321 / gphys / gpoiss.F
1 *
2 * $Id$
3 *
4 * $Log$
5 * Revision 1.1.1.1  1995/10/24 10:21:32  cernlib
6 * Geant
7 *
8 *
9 #include "geant321/pilot.h"
10 *CMZ :  3.21/02 29/03/94  15.41.23  by  S.Giani
11 *-- Author :
12       SUBROUTINE GPOISS(AMVEC,NPVEC,LEN)
13 C.
14 C.    ******************************************************************
15 C.    *                                                                *
16 C.    *  Generates  a vector NPVEC of LEN random numbers               *
17 C.    *  POISSON distribued with mean values AMVEC                     *
18 C.    *                                                                *
19 C.    *  If the mean value A greater than PLIM, N is calculated        *
20 C.    *  according to the Gaussian approximation of the Poisson        *
21 C.    *  distribution.                                                 *
22 C.    *                                                                *
23 C.    *     ==> Called by : GLANDZ,GMCOUL                              *
24 C.    *                                                                *
25 C.    *  Author      : L.Urban                                         *
26 C.    *  Date        : 28.04.1988           Last update :  1.02.1990   *
27 C.    *                                                                *
28 C.    ******************************************************************
29 C.
30 #include "geant321/gconsp.inc"
31       REAL AMVEC(*),RNDM(2), N
32       INTEGER NPVEC(*)
33       PARAMETER (PLIM=16.,HMXINT=2E+9)
34 *
35       DO 30 I=1,LEN
36 *     Protection against negative mean values
37          N=0.
38          IF(AMVEC(I).GT.0.) THEN
39             IF(AMVEC(I).LE.PLIM) THEN
40                CALL GRNDM(RNDM,1)
41                R=RNDM(1)
42                P=EXP(-AMVEC(I))
43                S=P
44                IF(R.LE.S) GOTO 20
45    10          N=N+1.
46                P=P*AMVEC(I)/N
47                S=S+P
48                IF(S.LT.R.AND.P.GT.1.E-30) GOTO 10
49             ELSE
50                CALL GRNDM(RNDM,2)
51                RR=SQRT(-2.*LOG(RNDM(1)))
52                PHI=TWOPI*RNDM(2)
53                X=RR*COS(PHI)
54                N=MIN(MAX(AMVEC(I)+X*SQRT(AMVEC(I)),0.),HMXINT)
55             ENDIF
56          ENDIF
57 *
58    20    NPVEC(I) = N
59    30 CONTINUE
60 *
61       END