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