Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DistributionGenerator.cc
Go to the documentation of this file.
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"
36 #include "G4HadronicDeprecate.hh"
37 #include "G4ios.hh"
38 #include <assert.h>
39 
40 // Constructor
41 
43  std::vector<G4double>& values)
44 
45 {
46  G4HadronicDeprecate("G4DistributionGenerator");
47  _x = x;
48 
49  // Check boundaries: must be size(x) = size(values) + 1
50  if (x.size() != (values.size() + 1))
51  { G4cout << " Inconsistent parameters in G4DistributionGenerator "
52  << G4endl;
53  }
54  assert (x.size() == (values.size() + 1));
55 
56  G4double tot = 0.;
57  unsigned int i;
58  for (i=0; i<values.size(); i++) { tot += values[i]; }
59  assert (tot > 0.);
60 
61  _cumProb.push_back(0.);
62  // _cumProb.push_back(values[0] / tot);
63  G4double sum = 0.;
64  for (i=0; i<values.size(); i++)
65  {
66  sum += values[i];
67  _cumProb.push_back(sum / tot); }
68 
69  // Debugging
70  /*
71  for (i=0; i<values.size(); i++)
72  { G4cout << values[i] << " " ; }
73  G4cout << " Integral = " << tot << G4endl;
74  for (i=0; i<_cumProb.size(); i++)
75  {
76  G4cout << "Variable " << _x[i]
77  << " --- cumProb = " << _cumProb[i] << G4endl;
78  }
79  */
80  // End of debugging
81 
82 }
83 
84 // Destructor
85 
87 {
88 }
89 
90 
92 {
93  G4double xRandom = _x[0];
94 
95  G4int bin = _cumProb.size() - 1;
96  unsigned int i;
97  for (i=1; i<_cumProb.size(); i++)
98  {
99  if (ranflat >= _cumProb[i-1] && ranflat < _cumProb[i])
100  {
101  bin = i - 1;
102  }
103  }
104 
105  if (bin >= 0 && bin < static_cast<G4int>(_cumProb.size()-1) && bin < static_cast<G4int>(_x.size()-1))
106  {
107  G4double coeff = (ranflat - _cumProb[bin]) * (_x[bin+1] - _x[bin]) /
108  (_cumProb[bin+1] - _cumProb[bin]);
109  xRandom = _x[bin] + coeff;
110 
111  // Deugging
112  /*
113  G4cout << "Random = " << ranflat << " Generated " << xRandom << G4endl;
114  */
115  // Endo of Debugging
116 
117  }
118  else
119  {
120  // Debugging
121  /*
122  G4cout << "Bin " << bin << " "
123  << _cumProb.size() << " "
124  << _x.size()
125  << G4endl;
126  */
127  // End of debugging
128  }
129 
130  return xRandom;
131 
132 }
133 
134