[833] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
| 27 | // $Id: G4Poisson.hh,v 1.9 2006/06/29 19:00:44 gunter Exp $ |
---|
[990] | 28 | // GEANT4 tag $Name: geant4-09-02-ref-02 $ |
---|
[833] | 29 | // |
---|
| 30 | // |
---|
| 31 | // ------------------------------------------------------------ |
---|
| 32 | // GEANT 4 class header file |
---|
| 33 | // ------------------------------------------------------------ |
---|
| 34 | // Class description: |
---|
| 35 | // |
---|
| 36 | // G4Poisson is the C++ implementation of the CERNLIB GPOISS algorithm |
---|
| 37 | // for the generation of Poisson distributed random numbers. It has been |
---|
| 38 | // adapted to invoke HepRandom from CLHEP for the primary engine generators. |
---|
| 39 | // GPOISS is recognized to be a faster algorithm, providing however a less |
---|
| 40 | // accurate output, than the algorithm adopted in CLHEP. |
---|
| 41 | |
---|
| 42 | // ------------------------------------------------------------ |
---|
| 43 | #ifndef G4POISSON_HH |
---|
| 44 | #define G4POISSON_HH |
---|
| 45 | |
---|
| 46 | #include "globals.hh" |
---|
| 47 | #include "Randomize.hh" |
---|
| 48 | |
---|
| 49 | inline G4long G4Poisson(G4double mean) |
---|
| 50 | { |
---|
| 51 | G4long number = 0; |
---|
| 52 | const G4int border = 16; |
---|
| 53 | G4double limit = 2e9; |
---|
| 54 | |
---|
| 55 | if(mean <= border) { |
---|
| 56 | G4double position = CLHEP::RandFlat::shoot(); |
---|
| 57 | G4double poissonValue = std::exp(-mean); |
---|
| 58 | G4double poissonSum = poissonValue; |
---|
| 59 | |
---|
| 60 | while(poissonSum <= position) { |
---|
| 61 | number++ ; |
---|
| 62 | poissonValue *= mean/number; |
---|
| 63 | poissonSum += poissonValue; |
---|
| 64 | } |
---|
| 65 | return number; |
---|
| 66 | } // the case of mean <= 16 |
---|
| 67 | |
---|
| 68 | G4double value, t, y; |
---|
| 69 | t = std::sqrt(-2*std::log(CLHEP::RandFlat::shoot())); |
---|
| 70 | y = twopi*CLHEP::RandFlat::shoot(); |
---|
| 71 | t *= std::cos(y); |
---|
| 72 | value = mean + t*std::sqrt(mean) + 0.5; |
---|
| 73 | if(value <= 0) {return 0;} |
---|
| 74 | if(value >= limit) { return G4long(limit);} |
---|
| 75 | return G4long(value); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | #endif /* G4POISSON_HH */ |
---|