Geant4  10.00.p01
G4CascadeSampler.icc
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 #ifndef G4CASCADE_SAMPLER_ICC
27 #define G4CASCADE_SAMPLER_ICC
28 // $Id: G4CascadeSampler.icc 66241 2012-12-13 18:34:42Z gunter $
29 //
30 // 20100506 M. Kelsey -- Move functionity of G4CascadeChannel here,
31 // use as base class to G4CascadeFunctions<T>.
32 // 20100512 M. Kelsey -- Move implementation to .icc with templating
33 // 20100803 M. Kelsey -- Add print function for debugging.
34 // 20101019 M. Kelsey -- CoVerity report: recursive #include
35 // 20110728 M. Kelsey -- Fix Coverity #20231, recursive #include
36 // 20110923 M. Kelsey -- Add optional ostream& argument to print(), pass
37 // to interpolator.
38 // 20120608 M. Kelsey -- Fix variable-name "shadowing" compiler warnings.
39 
40 #include "Randomize.hh"
41 #include <iostream>
42 #include <vector>
43 
44 
45 template <int NBINS, int NMULT> inline
46 G4double G4CascadeSampler<NBINS,NMULT>::
47 findCrossSection(double ke,
48  const G4double (&xsec)[energyBins]) const {
49  return interpolator.interpolate(ke, xsec);
50 }
51 
52 template <int NBINS, int NMULT> inline
53 G4int G4CascadeSampler<NBINS,NMULT>::
54 findMultiplicity(G4double ke,
55  const G4double xmult[][energyBins]) const {
56  fillSigmaBuffer(ke, xmult);
57  return sampleFlat() + 2; // Convert array index to actual mult (2 to 7)
58 }
59 
60 template <int NBINS, int NMULT> inline
61 G4int G4CascadeSampler<NBINS,NMULT>::
62 findFinalStateIndex(G4int mult, G4double ke, const G4int index[],
63  const G4double xsec[][energyBins]) const {
64  G4int start = index[mult-2];
65  G4int stop = index[mult-1];
66  if (stop-start <= 1) return start; // Avoid unnecessary work
67 
68  fillSigmaBuffer(ke, xsec, start, stop);
69  return sampleFlat();
70 }
71 
72 // Optional start/stop arguments default to multiplicity arrays
73 template <int NBINS, int NMULT> inline
74 void G4CascadeSampler<NBINS,NMULT>::
75 fillSigmaBuffer(G4double ke, const G4double x[][energyBins],
76  G4int startBin, G4int stopBin) const {
77  sigmaBuf.clear();
78  if (stopBin-startBin <= 1) return; // Avoid unnecessary work
79 
80  // NOTE: push_back() must be used to ensure that size() gets set!
81  sigmaBuf.reserve(stopBin-startBin);
82  for(G4int i = startBin; i < stopBin; i++)
83  sigmaBuf.push_back(interpolator.interpolate(ke, x[i]));
84 }
85 
86 
87 template <int NBINS, int NMULT> inline
88 G4int G4CascadeSampler<NBINS,NMULT>::sampleFlat() const {
89  G4int nbins = sigmaBuf.size();
90  if (nbins <= 1) return 0; // Avoid unnecessary work
91 
92 #ifdef G4CASCADE_DEBUG_SAMPLER
93  G4cout << "G4CascadeSampler::sampleFlat() has " << nbins << "bins:" << G4endl;
94  for (G4int sbi=0; sbi<nbins; sbi++) G4cout << " " << sigmaBuf[sbi];
95  G4cout << G4endl;
96 #endif
97 
98  G4int i;
99  G4double fsum = 0.;
100  for (i = 0; i < nbins; i++) fsum += sigmaBuf[i];
101 #ifdef G4CASCADE_DEBUG_SAMPLER
102  G4cout << " buffer total (fsum) " << fsum;
103 #endif
104  fsum *= G4UniformRand();
105 #ifdef G4CASCADE_DEBUG_SAMPLER
106  G4cout << " *random-scale " << fsum << G4endl;
107 #endif
108 
109  G4double partialSum = 0.0;
110  for (i = 0; i < nbins; i++) {
111  partialSum += sigmaBuf[i];
112  if (fsum < partialSum) return i; // Breaks out of loop automatically
113  }
114 
115  return 0; // Is this right? Shouldn't it return maximum, not minimum?
116 }
117 
118 
119 template <int NBINS, int NMULT> inline
120 void G4CascadeSampler<NBINS,NMULT>::print(std::ostream& os) const {
121  interpolator.printBins(os);
122 }
123 
124 #endif /* G4CASCADE_SAMPLER_ICC */