1 | #ifndef G4CASCADE_SAMPLER_ICC |
---|
2 | #define G4CASCADE_SAMPLER_ICC |
---|
3 | // |
---|
4 | // ******************************************************************** |
---|
5 | // * License and Disclaimer * |
---|
6 | // * * |
---|
7 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
8 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
9 | // * conditions of the Geant4 Software License, included in the file * |
---|
10 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
11 | // * include a list of copyright holders. * |
---|
12 | // * * |
---|
13 | // * Neither the authors of this software system, nor their employing * |
---|
14 | // * institutes,nor the agencies providing financial support for this * |
---|
15 | // * work make any representation or warranty, express or implied, * |
---|
16 | // * regarding this software system or assume any liability for its * |
---|
17 | // * use. Please see the license in the file LICENSE and URL above * |
---|
18 | // * for the full disclaimer and the limitation of liability. * |
---|
19 | // * * |
---|
20 | // * This code implementation is the result of the scientific and * |
---|
21 | // * technical work of the GEANT4 collaboration. * |
---|
22 | // * By using, copying, modifying or distributing the software (or * |
---|
23 | // * any work based on the software) you agree to acknowledge its * |
---|
24 | // * use in resulting scientific publications, and indicate your * |
---|
25 | // * acceptance of all terms of the Geant4 Software license. * |
---|
26 | // ******************************************************************** |
---|
27 | // |
---|
28 | // $Id: G4CascadeSampler.icc,v 1.5 2010/10/19 19:48:01 mkelsey Exp $ |
---|
29 | // GEANT4 tag: $Name: hadr-casc-V09-03-85 $ |
---|
30 | // |
---|
31 | // 20100506 M. Kelsey -- Move functionity of G4CascadeChannel here, |
---|
32 | // use as base class to G4CascadeFunctions<T>. |
---|
33 | // 20100512 M. Kelsey -- Move implementation to .icc with templating |
---|
34 | // 20100803 M. Kelsey -- Add print function for debugging. |
---|
35 | // 20101019 M. Kelsey -- CoVerity report: recursive #include |
---|
36 | |
---|
37 | #include "G4CascadeSampler.hh" |
---|
38 | #include "Randomize.hh" |
---|
39 | #include <vector> |
---|
40 | |
---|
41 | |
---|
42 | template <int NBINS, int NMULT> inline |
---|
43 | G4double G4CascadeSampler<NBINS,NMULT>:: |
---|
44 | findCrossSection(double ke, |
---|
45 | const G4double (&xsec)[energyBins]) const { |
---|
46 | return interpolator.interpolate(ke, xsec); |
---|
47 | } |
---|
48 | |
---|
49 | template <int NBINS, int NMULT> inline |
---|
50 | G4int G4CascadeSampler<NBINS,NMULT>:: |
---|
51 | findMultiplicity(G4double ke, |
---|
52 | const G4double xmult[][energyBins]) const { |
---|
53 | fillSigmaBuffer(ke, xmult); |
---|
54 | return sampleFlat() + 2; // Convert array index to actual mult (2 to 7) |
---|
55 | } |
---|
56 | |
---|
57 | template <int NBINS, int NMULT> inline |
---|
58 | G4int G4CascadeSampler<NBINS,NMULT>:: |
---|
59 | findFinalStateIndex(G4int mult, G4double ke, const G4int index[], |
---|
60 | const G4double xsec[][energyBins]) const { |
---|
61 | G4int start = index[mult-2]; |
---|
62 | G4int stop = index[mult-1]; |
---|
63 | if (stop-start <= 1) return start; // Avoid unnecessary work |
---|
64 | |
---|
65 | fillSigmaBuffer(ke, xsec, start, stop); |
---|
66 | return sampleFlat(); |
---|
67 | } |
---|
68 | |
---|
69 | // Optional start/stop arguments default to multiplicity arrays |
---|
70 | template <int NBINS, int NMULT> inline |
---|
71 | void G4CascadeSampler<NBINS,NMULT>:: |
---|
72 | fillSigmaBuffer(G4double ke, const G4double x[][energyBins], |
---|
73 | G4int startBin, G4int stopBin) const { |
---|
74 | sigmaBuf.clear(); |
---|
75 | if (stopBin-startBin <= 1) return; // Avoid unnecessary work |
---|
76 | |
---|
77 | // NOTE: push_back() must be used to ensure that size() gets set! |
---|
78 | sigmaBuf.reserve(stopBin-startBin); |
---|
79 | for(G4int m = startBin; m < stopBin; m++) |
---|
80 | sigmaBuf.push_back(interpolator.interpolate(ke, x[m])); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | template <int NBINS, int NMULT> inline |
---|
85 | G4int G4CascadeSampler<NBINS,NMULT>::sampleFlat() const { |
---|
86 | G4int nbins = sigmaBuf.size(); |
---|
87 | if (nbins <= 1) return 0; // Avoid unnecessary work |
---|
88 | |
---|
89 | #ifdef G4CASCADE_DEBUG_SAMPLER |
---|
90 | G4cout << "G4CascadeSampler::sampleFlat() has " << nbins << "bins:" << G4endl; |
---|
91 | for (G4int sbi=0; sbi<nbins; sbi++) G4cout << " " << sigmaBuf[sbi]; |
---|
92 | G4cout << G4endl; |
---|
93 | #endif |
---|
94 | |
---|
95 | G4int i; |
---|
96 | G4double fsum = 0.; |
---|
97 | for (i = 0; i < nbins; i++) fsum += sigmaBuf[i]; |
---|
98 | #ifdef G4CASCADE_DEBUG_SAMPLER |
---|
99 | G4cout << " buffer total (fsum) " << fsum; |
---|
100 | #endif |
---|
101 | fsum *= G4UniformRand(); |
---|
102 | #ifdef G4CASCADE_DEBUG_SAMPLER |
---|
103 | G4cout << " *random-scale " << fsum << G4endl; |
---|
104 | #endif |
---|
105 | |
---|
106 | G4double partialSum = 0.0; |
---|
107 | for (i = 0; i < nbins; i++) { |
---|
108 | partialSum += sigmaBuf[i]; |
---|
109 | if (fsum < partialSum) return i; // Breaks out of loop automatically |
---|
110 | } |
---|
111 | |
---|
112 | return 0; // Is this right? Shouldn't it return maximum, not minimum? |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | template <int NBINS, int NMULT> inline |
---|
117 | void G4CascadeSampler<NBINS,NMULT>::print() const { |
---|
118 | interpolator.printBins(); |
---|
119 | } |
---|
120 | |
---|
121 | #endif /* G4CASCADE_SAMPLER_ICC */ |
---|