source: trunk/source/processes/hadronic/models/cascade/cascade/include/G4CascadeSampler.icc @ 1316

Last change on this file since 1316 was 1316, checked in by garnier, 14 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 4.3 KB
Line 
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// $Id: G4CascadeSampler.icc,v 1.2 2010/05/21 18:26:16 mkelsey Exp $
26// GEANT4 tag: $Name: geant4-09-04-beta-cand-01 $
27//
28// 20100506 M. Kelsey -- Move functionity of G4CascadeChannel here,
29//              use as base class to G4CascadeFunctions<T>.
30// 20100512 M. Kelsey -- Move implementation to .icc with templating
31
32#include "G4CascadeSampler.hh"
33#include "Randomize.hh"
34#include <vector>
35
36
37template <int NBINS, int NMULT> inline
38G4double G4CascadeSampler<NBINS,NMULT>::
39findCrossSection(double ke,
40                 const G4double (&xsec)[energyBins]) const {
41  return interpolator.interpolate(ke, xsec);
42}
43
44template <int NBINS, int NMULT> inline
45G4int G4CascadeSampler<NBINS,NMULT>::
46findMultiplicity(G4double ke,
47                 const G4double xmult[][energyBins]) const {
48  fillSigmaBuffer(ke, xmult);
49  return sampleFlat() + 2;      // Convert array index to actual mult (2 to 7)
50}
51
52template <int NBINS, int NMULT> inline
53G4int G4CascadeSampler<NBINS,NMULT>::
54findFinalStateIndex(G4int mult, G4double ke, const G4int index[],
55                    const G4double xsec[][energyBins]) const {
56  G4int start = index[mult-2];
57  G4int stop = index[mult-1];
58  if (stop-start <= 1) return start;    // Avoid unnecessary work
59
60  fillSigmaBuffer(ke, xsec, start, stop);
61  return sampleFlat();
62}
63
64// Optional start/stop arguments default to multiplicity arrays
65template <int NBINS, int NMULT> inline
66void G4CascadeSampler<NBINS,NMULT>::
67fillSigmaBuffer(G4double ke, const G4double x[][energyBins],
68                G4int startBin, G4int stopBin) const {
69  sigmaBuf.clear();
70  if (stopBin-startBin <= 1) return;    // Avoid unnecessary work
71
72  // NOTE:  push_back() must be used to ensure that size() gets set!
73  sigmaBuf.reserve(stopBin-startBin);
74  for(G4int m = startBin; m < stopBin; m++)
75    sigmaBuf.push_back(interpolator.interpolate(ke, x[m]));
76}
77
78
79template <int NBINS, int NMULT> inline
80G4int G4CascadeSampler<NBINS,NMULT>::sampleFlat() const {
81  G4int nbins = sigmaBuf.size();
82  if (nbins <= 1) return 0;             // Avoid unnecessary work
83
84#ifdef G4CASCADE_DEBUG_SAMPLER
85  G4cout << "G4CascadeSampler::sampleFlat() has " << nbins << "bins:" << G4endl;
86  for (G4int sbi=0; sbi<nbins; sbi++) G4cout << " " << sigmaBuf[sbi];
87  G4cout << G4endl;
88#endif
89
90  G4int i;
91  G4double fsum = 0.;
92  for (i = 0; i < nbins; i++) fsum += sigmaBuf[i];
93#ifdef G4CASCADE_DEBUG_SAMPLER
94  G4cout << " buffer total (fsum) " << fsum;
95#endif
96  fsum *= G4UniformRand();
97#ifdef G4CASCADE_DEBUG_SAMPLER
98  G4cout << " *random-scale " << fsum << G4endl;
99#endif
100
101  G4double partialSum = 0.0;
102  for (i = 0; i < nbins; i++) {
103    partialSum += sigmaBuf[i];
104    if (fsum < partialSum) return i;    // Breaks out of loop automatically
105  }
106
107  return 0;     // Is this right?  Shouldn't it return maximum, not minimum?
108}
Note: See TracBrowser for help on using the repository browser.