00001 // 00002 // ******************************************************************** 00003 // * License and Disclaimer * 00004 // * * 00005 // * The Geant4 software is copyright of the Copyright Holders of * 00006 // * the Geant4 Collaboration. It is provided under the terms and * 00007 // * conditions of the Geant4 Software License, included in the file * 00008 // * LICENSE and available at http://cern.ch/geant4/license . These * 00009 // * include a list of copyright holders. * 00010 // * * 00011 // * Neither the authors of this software system, nor their employing * 00012 // * institutes,nor the agencies providing financial support for this * 00013 // * work make any representation or warranty, express or implied, * 00014 // * regarding this software system or assume any liability for its * 00015 // * use. Please see the license in the file LICENSE and URL above * 00016 // * for the full disclaimer and the limitation of liability. * 00017 // * * 00018 // * This code implementation is the result of the scientific and * 00019 // * technical work of the GEANT4 collaboration. * 00020 // * By using, copying, modifying or distributing the software (or * 00021 // * any work based on the software) you agree to acknowledge its * 00022 // * use in resulting scientific publications, and indicate your * 00023 // * acceptance of all terms of the Geant4 Software license. * 00024 // ******************************************************************** 00025 // 00026 // 00027 // $Id$ 00028 // 00029 // 00030 // ------------------------------------------------------------ 00031 // GEANT 4 class header file 00032 // ------------------------------------------------------------ 00033 // Class description: 00034 // 00035 // G4Poisson is the C++ implementation of the CERNLIB GPOISS algorithm 00036 // for the generation of Poisson distributed random numbers. It has been 00037 // adapted to invoke HepRandom from CLHEP for the primary engine generators. 00038 // GPOISS is recognized to be a faster algorithm, providing however a less 00039 // accurate output, than the algorithm adopted in CLHEP. 00040 00041 // ------------------------------------------------------------ 00042 #ifndef G4POISSON_HH 00043 #define G4POISSON_HH 00044 00045 #include <CLHEP/Units/PhysicalConstants.h> 00046 00047 #include "globals.hh" 00048 #include "Randomize.hh" 00049 00050 inline G4long G4Poisson(G4double mean) 00051 { 00052 G4long number = 0; 00053 const G4int border = 16; 00054 G4double limit = 2e9; 00055 00056 if(mean <= border) { 00057 G4double position = G4UniformRand(); 00058 G4double poissonValue = std::exp(-mean); 00059 G4double poissonSum = poissonValue; 00060 00061 while(poissonSum <= position) { 00062 number++ ; 00063 poissonValue *= mean/number; 00064 poissonSum += poissonValue; 00065 } 00066 return number; 00067 } // the case of mean <= 16 00068 00069 G4double value, t, y; 00070 t = std::sqrt(-2*std::log(G4UniformRand())); 00071 y = CLHEP::twopi*G4UniformRand(); 00072 t *= std::cos(y); 00073 value = mean + t*std::sqrt(mean) + 0.5; 00074 if(value <= 0) {return 0;} 00075 if(value >= limit) { return G4long(limit);} 00076 return G4long(value); 00077 } 00078 00079 #endif /* G4POISSON_HH */