| 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 | // File name: G4DistributionGenerator
|
|---|
| 27 | //
|
|---|
| 28 | // Author: Maria Grazia Pia (pia@genova.infn.it)
|
|---|
| 29 | //
|
|---|
| 30 | // Creation date: 8 May 1998
|
|---|
| 31 | //
|
|---|
| 32 | // -------------------------------------------------------------------
|
|---|
| 33 |
|
|---|
| 34 | #include "globals.hh"
|
|---|
| 35 | #include "G4DistributionGenerator.hh"
|
|---|
| 36 | #include "G4ios.hh"
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 |
|
|---|
| 39 | // Constructor
|
|---|
| 40 |
|
|---|
| 41 | G4DistributionGenerator::G4DistributionGenerator(std::vector<G4double>& x,
|
|---|
| 42 | std::vector<G4double>& values)
|
|---|
| 43 |
|
|---|
| 44 | {
|
|---|
| 45 | _x = x;
|
|---|
| 46 |
|
|---|
| 47 | // Check boundaries: must be size(x) = size(values) + 1
|
|---|
| 48 | if (x.size() != (values.size() + 1))
|
|---|
| 49 | { G4cout << " Inconsistent parameters in G4DistributionGenerator "
|
|---|
| 50 | << G4endl;
|
|---|
| 51 | }
|
|---|
| 52 | assert (x.size() == (values.size() + 1));
|
|---|
| 53 |
|
|---|
| 54 | G4double tot = 0.;
|
|---|
| 55 | unsigned int i;
|
|---|
| 56 | for (i=0; i<values.size(); i++) { tot += values[i]; }
|
|---|
| 57 | assert (tot > 0.);
|
|---|
| 58 |
|
|---|
| 59 | _cumProb.push_back(0.);
|
|---|
| 60 | // _cumProb.push_back(values[0] / tot);
|
|---|
| 61 | G4double sum = 0.;
|
|---|
| 62 | for (i=0; i<values.size(); i++)
|
|---|
| 63 | {
|
|---|
| 64 | sum += values[i];
|
|---|
| 65 | _cumProb.push_back(sum / tot); }
|
|---|
| 66 |
|
|---|
| 67 | // Debugging
|
|---|
| 68 | /*
|
|---|
| 69 | for (i=0; i<values.size(); i++)
|
|---|
| 70 | { G4cout << values[i] << " " ; }
|
|---|
| 71 | G4cout << " Integral = " << tot << G4endl;
|
|---|
| 72 | for (i=0; i<_cumProb.size(); i++)
|
|---|
| 73 | {
|
|---|
| 74 | G4cout << "Variable " << _x[i]
|
|---|
| 75 | << " --- cumProb = " << _cumProb[i] << G4endl;
|
|---|
| 76 | }
|
|---|
| 77 | */
|
|---|
| 78 | // End of debugging
|
|---|
| 79 |
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | // Destructor
|
|---|
| 83 |
|
|---|
| 84 | G4DistributionGenerator::~G4DistributionGenerator()
|
|---|
| 85 | {
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | G4double G4DistributionGenerator::Generate(G4double ranflat)
|
|---|
| 90 | {
|
|---|
| 91 | G4double xRandom = _x[0];
|
|---|
| 92 |
|
|---|
| 93 | G4int bin = _cumProb.size() - 1;
|
|---|
| 94 | unsigned int i;
|
|---|
| 95 | for (i=1; i<_cumProb.size(); i++)
|
|---|
| 96 | {
|
|---|
| 97 | if (ranflat >= _cumProb[i-1] && ranflat < _cumProb[i])
|
|---|
| 98 | {
|
|---|
| 99 | bin = i - 1;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (bin >= 0 && bin < static_cast<G4int>(_cumProb.size()-1) && bin < static_cast<G4int>(_x.size()-1))
|
|---|
| 104 | {
|
|---|
| 105 | G4double coeff = (ranflat - _cumProb[bin]) * (_x[bin+1] - _x[bin]) /
|
|---|
| 106 | (_cumProb[bin+1] - _cumProb[bin]);
|
|---|
| 107 | xRandom = _x[bin] + coeff;
|
|---|
| 108 |
|
|---|
| 109 | // Deugging
|
|---|
| 110 | /*
|
|---|
| 111 | G4cout << "Random = " << ranflat << " Generated " << xRandom << G4endl;
|
|---|
| 112 | */
|
|---|
| 113 | // Endo of Debugging
|
|---|
| 114 |
|
|---|
| 115 | }
|
|---|
| 116 | else
|
|---|
| 117 | {
|
|---|
| 118 | // Debugging
|
|---|
| 119 | /*
|
|---|
| 120 | G4cout << "Bin " << bin << " "
|
|---|
| 121 | << _cumProb.size() << " "
|
|---|
| 122 | << _x.size()
|
|---|
| 123 | << G4endl;
|
|---|
| 124 | */
|
|---|
| 125 | // End of debugging
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return xRandom;
|
|---|
| 129 |
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|