Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:00

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 #ifndef G4CASCADE_SAMPLER_ICC
0027 #define G4CASCADE_SAMPLER_ICC
0028 //
0029 // 20100506 M. Kelsey -- Move functionity of G4CascadeChannel here,
0030 //      use as base class to G4CascadeFunctions<T>.
0031 // 20100512 M. Kelsey -- Move implementation to .icc with templating
0032 // 20100803 M. Kelsey -- Add print function for debugging.
0033 // 20101019 M. Kelsey -- CoVerity report: recursive #include
0034 // 20110728 M. Kelsey -- Fix Coverity #20231, recursive #include
0035 // 20110923 M. Kelsey -- Add optional ostream& argument to print(), pass
0036 //      to interpolator.
0037 // 20120608  M. Kelsey -- Fix variable-name "shadowing" compiler warnings.
0038 
0039 #include "Randomize.hh"
0040 #include <iostream>
0041 #include <vector>
0042 
0043 
0044 template <G4int NBINS, G4int NMULT> inline
0045 G4double G4CascadeSampler<NBINS,NMULT>::
0046 findCrossSection(G4double ke,
0047          const G4double (&xsec)[energyBins]) const {
0048   return interpolator.interpolate(ke, xsec);
0049 }
0050 
0051 template <G4int NBINS, G4int NMULT> inline
0052 G4int G4CascadeSampler<NBINS,NMULT>::
0053 findMultiplicity(G4double ke,
0054          const G4double xmult[][energyBins]) const {
0055   fillSigmaBuffer(ke, xmult);
0056   return sampleFlat() + 2;  // Convert array index to actual mult (2 to 7)
0057 }
0058 
0059 template <G4int NBINS, G4int NMULT> inline
0060 G4int G4CascadeSampler<NBINS,NMULT>::
0061 findFinalStateIndex(G4int mult, G4double ke, const G4int index[],
0062             const G4double xsec[][energyBins]) const {
0063   G4int start = index[mult-2];
0064   G4int stop = index[mult-1];
0065   if (stop-start <= 1) return start;    // Avoid unnecessary work
0066 
0067   fillSigmaBuffer(ke, xsec, start, stop);
0068   return sampleFlat();
0069 }
0070 
0071 // Optional start/stop arguments default to multiplicity arrays
0072 template <G4int NBINS, G4int NMULT> inline
0073 void G4CascadeSampler<NBINS,NMULT>::
0074 fillSigmaBuffer(G4double ke, const G4double x[][energyBins],
0075         G4int startBin, G4int stopBin) const {
0076   sigmaBuf.clear();
0077   if (stopBin-startBin <= 1) return;    // Avoid unnecessary work
0078 
0079   // NOTE:  push_back() must be used to ensure that size() gets set!
0080   sigmaBuf.reserve(stopBin-startBin);
0081   for(G4int i = startBin; i < stopBin; ++i)
0082     sigmaBuf.push_back(interpolator.interpolate(ke, x[i]));
0083 }
0084 
0085 
0086 template <G4int NBINS, G4int NMULT> inline
0087 G4int G4CascadeSampler<NBINS,NMULT>::sampleFlat() const {
0088   G4int nbins = (G4int)sigmaBuf.size();
0089   if (nbins <= 1) return 0;     // Avoid unnecessary work
0090 
0091 #ifdef G4CASCADE_DEBUG_SAMPLER
0092   G4cout << "G4CascadeSampler::sampleFlat() has " << nbins << "bins:" << G4endl;
0093   for (G4int sbi=0; sbi<nbins; sbi++) G4cout << " " << sigmaBuf[sbi];
0094   G4cout << G4endl;
0095 #endif
0096 
0097   G4int i;
0098   G4double fsum = 0.;
0099   for (i = 0; i < nbins; ++i) fsum += sigmaBuf[i];
0100 #ifdef G4CASCADE_DEBUG_SAMPLER
0101   G4cout << " buffer total (fsum) " << fsum;
0102 #endif
0103   fsum *= G4UniformRand();
0104 #ifdef G4CASCADE_DEBUG_SAMPLER
0105   G4cout << " *random-scale " << fsum << G4endl;
0106 #endif
0107 
0108   G4double partialSum = 0.0;
0109   for (i = 0; i < nbins; ++i) {
0110     partialSum += sigmaBuf[i];
0111     if (fsum < partialSum) return i;    // Breaks out of loop automatically
0112   }
0113 
0114   return 0; // Is this right?  Shouldn't it return maximum, not minimum?
0115 }
0116 
0117 
0118 template <G4int NBINS, G4int NMULT> inline
0119 void G4CascadeSampler<NBINS,NMULT>::print(std::ostream& os) const {
0120   interpolator.printBins(os);
0121 }
0122 
0123 #endif  /* G4CASCADE_SAMPLER_ICC */