2 // ********************************************************************
 
    3 // * License and Disclaimer                                           *
 
    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.                             *
 
   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.         *
 
   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 // ********************************************************************
 
   26 #ifndef G4CASCADE_SAMPLER_ICC
 
   27 #define G4CASCADE_SAMPLER_ICC
 
   28 // $Id: G4CascadeSampler.icc 66241 2012-12-13 18:34:42Z gunter $
 
   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
 
   38 // 20120608  M. Kelsey -- Fix variable-name "shadowing" compiler warnings.
 
   40 #include "Randomize.hh"
 
   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);
 
   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)
 
   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
 
   68   fillSigmaBuffer(ke, xsec, start, stop);
 
   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 {
 
   78   if (stopBin-startBin <= 1) return;       // Avoid unnecessary work
 
   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]));
 
   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
 
   92 #ifdef G4CASCADE_DEBUG_SAMPLER
 
   93   G4cout << "G4CascadeSampler::sampleFlat() has " << nbins << "bins:" << G4endl;
 
   94   for (G4int sbi=0; sbi<nbins; sbi++) G4cout << " " << sigmaBuf[sbi];
 
  100   for (i = 0; i < nbins; i++) fsum += sigmaBuf[i];
 
  101 #ifdef G4CASCADE_DEBUG_SAMPLER
 
  102   G4cout << " buffer total (fsum) " << fsum;
 
  104   fsum *= G4UniformRand();
 
  105 #ifdef G4CASCADE_DEBUG_SAMPLER
 
  106   G4cout << " *random-scale " << fsum << G4endl;
 
  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
 
  115   return 0;        // Is this right?  Shouldn't it return maximum, not minimum?
 
  119 template <int NBINS, int NMULT> inline
 
  120 void G4CascadeSampler<NBINS,NMULT>::print(std::ostream& os) const {
 
  121   interpolator.printBins(os);
 
  124 #endif     /* G4CASCADE_SAMPLER_ICC */