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 // File name: G4DistributionGenerator 00027 // 00028 // Author: Maria Grazia Pia (pia@genova.infn.it) 00029 // 00030 // Creation date: 8 May 1998 00031 // 00032 // ------------------------------------------------------------------- 00033 00034 #include "globals.hh" 00035 #include "G4DistributionGenerator.hh" 00036 #include "G4HadronicDeprecate.hh" 00037 #include "G4ios.hh" 00038 #include <assert.h> 00039 00040 // Constructor 00041 00042 G4DistributionGenerator::G4DistributionGenerator(std::vector<G4double>& x, 00043 std::vector<G4double>& values) 00044 00045 { 00046 G4HadronicDeprecate("G4DistributionGenerator"); 00047 _x = x; 00048 00049 // Check boundaries: must be size(x) = size(values) + 1 00050 if (x.size() != (values.size() + 1)) 00051 { G4cout << " Inconsistent parameters in G4DistributionGenerator " 00052 << G4endl; 00053 } 00054 assert (x.size() == (values.size() + 1)); 00055 00056 G4double tot = 0.; 00057 unsigned int i; 00058 for (i=0; i<values.size(); i++) { tot += values[i]; } 00059 assert (tot > 0.); 00060 00061 _cumProb.push_back(0.); 00062 // _cumProb.push_back(values[0] / tot); 00063 G4double sum = 0.; 00064 for (i=0; i<values.size(); i++) 00065 { 00066 sum += values[i]; 00067 _cumProb.push_back(sum / tot); } 00068 00069 // Debugging 00070 /* 00071 for (i=0; i<values.size(); i++) 00072 { G4cout << values[i] << " " ; } 00073 G4cout << " Integral = " << tot << G4endl; 00074 for (i=0; i<_cumProb.size(); i++) 00075 { 00076 G4cout << "Variable " << _x[i] 00077 << " --- cumProb = " << _cumProb[i] << G4endl; 00078 } 00079 */ 00080 // End of debugging 00081 00082 } 00083 00084 // Destructor 00085 00086 G4DistributionGenerator::~G4DistributionGenerator() 00087 { 00088 } 00089 00090 00091 G4double G4DistributionGenerator::Generate(G4double ranflat) 00092 { 00093 G4double xRandom = _x[0]; 00094 00095 G4int bin = _cumProb.size() - 1; 00096 unsigned int i; 00097 for (i=1; i<_cumProb.size(); i++) 00098 { 00099 if (ranflat >= _cumProb[i-1] && ranflat < _cumProb[i]) 00100 { 00101 bin = i - 1; 00102 } 00103 } 00104 00105 if (bin >= 0 && bin < static_cast<G4int>(_cumProb.size()-1) && bin < static_cast<G4int>(_x.size()-1)) 00106 { 00107 G4double coeff = (ranflat - _cumProb[bin]) * (_x[bin+1] - _x[bin]) / 00108 (_cumProb[bin+1] - _cumProb[bin]); 00109 xRandom = _x[bin] + coeff; 00110 00111 // Deugging 00112 /* 00113 G4cout << "Random = " << ranflat << " Generated " << xRandom << G4endl; 00114 */ 00115 // Endo of Debugging 00116 00117 } 00118 else 00119 { 00120 // Debugging 00121 /* 00122 G4cout << "Bin " << bin << " " 00123 << _cumProb.size() << " " 00124 << _x.size() 00125 << G4endl; 00126 */ 00127 // End of debugging 00128 } 00129 00130 return xRandom; 00131 00132 } 00133 00134