Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/examples/extended/electromagnetic/TestEm7/src/G4ScreenedNuclearRecoil.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /examples/extended/electromagnetic/TestEm7/src/G4ScreenedNuclearRecoil.cc (Version 11.3.0) and /examples/extended/electromagnetic/TestEm7/src/G4ScreenedNuclearRecoil.cc (Version 9.3.p1)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 /// \file electromagnetic/TestEm7/src/G4Screen << 
 27 /// \brief Implementation of the G4ScreenedNuc << 
 28 //                                                 26 //
                                                   >>  27 // G4ScreenedNuclearRecoil.cc,v 1.57 2008/05/07 11:51:26 marcus Exp
                                                   >>  28 // GEANT4 tag 
 29 //                                                 29 //
 30 //                                                 30 //
 31 // Class Description                               31 // Class Description
 32 // Process for screened electromagnetic nuclea <<  32 // Process for screened electromagnetic nuclear elastic scattering; 
 33 // Physics comes from:                             33 // Physics comes from:
 34 // Marcus H. Mendenhall and Robert A. Weller,  <<  34 // Marcus H. Mendenhall and Robert A. Weller, 
 35 // "Algorithms  for  the rapid  computation  o <<  35 // "Algorithms  for  the rapid  computation  of  classical  cross  
 36 // sections  for  screened  Coulomb  collision     36 // sections  for  screened  Coulomb  collisions  "
 37 // Nuclear  Instruments  and  Methods  in  Phy <<  37 // Nuclear  Instruments  and  Methods  in  Physics  Research  B58  (1991)  11-17  
 38 // The only input required is a screening func     38 // The only input required is a screening function phi(r/a) which is the ratio
 39 // of the actual interatomic potential for two <<  39 // of the actual interatomic potential for two atoms with atomic numbers Z1 and Z2,
 40 // numbers Z1 and Z2,                          <<  40 // to the unscreened potential Z1*Z2*e^2/r where e^2 is elm_coupling in Geant4 units
 41 // to the unscreened potential Z1*Z2*e^2/r whe << 
 42 // Geant4 units                                << 
 43 //                                                 41 //
 44 // First version, April 2004, Marcus H. Menden     42 // First version, April 2004, Marcus H. Mendenhall, Vanderbilt University
 45 //                                                 43 //
 46 // 5 May, 2004, Marcus Mendenhall                  44 // 5 May, 2004, Marcus Mendenhall
 47 // Added an option for enhancing hard collisio <<  45 // Added an option for enhancing hard collisions statistically, to allow 
 48 // backscattering calculations to be carried o     46 // backscattering calculations to be carried out with much improved event rates,
 49 // without distorting the multiple-scattering      47 // without distorting the multiple-scattering broadening too much.
 50 // the method SetCrossSectionHardening(G4doubl <<  48 // the method SetCrossSectionHardening(G4double fraction, G4double HardeningFactor)
 51 //                                     Hardeni << 
 52 // sets what fraction of the events will be ra     49 // sets what fraction of the events will be randomly hardened,
 53 // and the factor by which the impact area is      50 // and the factor by which the impact area is reduced for such selected events.
 54 //                                                 51 //
 55 // 21 November, 2004, Marcus Mendenhall            52 // 21 November, 2004, Marcus Mendenhall
 56 // added static_nucleus to IsApplicable            53 // added static_nucleus to IsApplicable
 57 //                                             <<  54 // 
 58 // 7 December, 2004, Marcus Mendenhall             55 // 7 December, 2004, Marcus Mendenhall
 59 // changed mean free path of stopping particle     56 // changed mean free path of stopping particle from 0.0 to 1.0*nanometer
 60 // to avoid new verbose warning about 0 MFP in     57 // to avoid new verbose warning about 0 MFP in 4.6.2p02
 61 //                                             <<  58 // 
 62 // 17 December, 2004, Marcus Mendenhall            59 // 17 December, 2004, Marcus Mendenhall
 63 // added code to permit screening out overly c <<  60 // added code to permit screening out overly close collisions which are 
 64 // expected to be hadronic, not Coulombic          61 // expected to be hadronic, not Coulombic
 65 //                                                 62 //
 66 // 19 December, 2004, Marcus Mendenhall            63 // 19 December, 2004, Marcus Mendenhall
 67 // massive rewrite to add modular physics stag     64 // massive rewrite to add modular physics stages and plug-in cross section table
 68 // computation.  This allows one to select (e. <<  65 // computation.  This allows one to select (e.g.) between the normal external python
 69 // python process and an embedded python inter <<  66 // process and an embedded python interpreter (which is much faster) for generating
 70 // for generating the tables.                  <<  67 // the tables.
 71 // It also allows one to switch between sub-sa <<  68 // It also allows one to switch between sub-sampled scattering (event biasing) and
 72 // and normal scattering, and between non-rela <<  69 // normal scattering, and between non-relativistic kinematics and relativistic
 73 // relativistic kinematic approximations, with <<  70 // kinematic approximations, without having a class for every combination. Further, one can
 74 // combination. Further, one can add extra sta <<  71 // add extra stages to the scattering, which can implement various book-keeping processes.
 75 // implement various book-keeping processes.   <<  72 // 
 76 //                                             << 
 77 // January 2007, Marcus Mendenhall                 73 // January 2007, Marcus Mendenhall
 78 // Reorganized heavily for inclusion in Geant4 <<  74 // Reorganized heavily for inclusion in Geant4 Core.  All modules merged into 
 79 // one source and header, all historic code re     75 // one source and header, all historic code removed.
 80 //                                             <<  76 // 
 81 // Class Description - End                         77 // Class Description - End
 82                                                    78 
 83 //....oooOO0OOooo........oooOO0OOooo........oo << 
 84                                                    79 
 85 #include "G4ScreenedNuclearRecoil.hh"          <<  80 #include <stdio.h>
 86                                                    81 
 87 #include "globals.hh"                              82 #include "globals.hh"
 88                                                    83 
 89 #include <stdio.h>                             <<  84 #include "G4ScreenedNuclearRecoil.hh"
 90                                                    85 
 91 const char* G4ScreenedCoulombCrossSectionInfo: <<  86 const char* G4ScreenedCoulombCrossSectionInfo::CVSFileVers() { return 
 92 {                                              <<  87   "G4ScreenedNuclearRecoil.cc,v 1.57 2008/05/07 11:51:26 marcus Exp GEANT4 tag ";
 93   return "G4ScreenedNuclearRecoil.cc,v 1.57 20 << 
 94 }                                                  88 }
 95                                                    89 
 96 #include "c2_factory.hh"                       <<  90 #include "G4ParticleTypes.hh"
 97                                                <<  91 #include "G4ParticleTable.hh"
                                                   >>  92 #include "G4VParticleChange.hh"
                                                   >>  93 #include "G4ParticleChangeForLoss.hh"
 98 #include "G4DataVector.hh"                         94 #include "G4DataVector.hh"
 99 #include "G4DynamicParticle.hh"                <<  95 #include "G4Track.hh"
                                                   >>  96 #include "G4Step.hh"
                                                   >>  97 
                                                   >>  98 #include "G4Material.hh"
100 #include "G4Element.hh"                            99 #include "G4Element.hh"
101 #include "G4ElementVector.hh"                  << 
102 #include "G4EmProcessSubType.hh"               << 
103 #include "G4IonTable.hh"                       << 
104 #include "G4Isotope.hh"                           100 #include "G4Isotope.hh"
105 #include "G4IsotopeVector.hh"                  << 
106 #include "G4LindhardPartition.hh"              << 
107 #include "G4Material.hh"                       << 
108 #include "G4MaterialCutsCouple.hh"                101 #include "G4MaterialCutsCouple.hh"
109 #include "G4ParticleChangeForLoss.hh"          << 102 #include "G4ElementVector.hh"
                                                   >> 103 #include "G4IsotopeVector.hh"
                                                   >> 104 
                                                   >> 105 #include "G4EmProcessSubType.hh"
                                                   >> 106 
                                                   >> 107 #include "G4RangeTest.hh"
110 #include "G4ParticleDefinition.hh"                108 #include "G4ParticleDefinition.hh"
111 #include "G4ParticleTable.hh"                  << 109 #include "G4DynamicParticle.hh"
112 #include "G4ParticleTypes.hh"                  << 
113 #include "G4ProcessManager.hh"                    110 #include "G4ProcessManager.hh"
114 #include "G4StableIsotopes.hh"                    111 #include "G4StableIsotopes.hh"
115 #include "G4Step.hh"                           << 112 #include "G4LindhardPartition.hh"
116 #include "G4Track.hh"                          << 113 
117 #include "G4VParticleChange.hh"                << 
118 #include "Randomize.hh"                           114 #include "Randomize.hh"
119                                                   115 
120 #include <iomanip>                             << 116 #include "CLHEP/Units/PhysicalConstants.h"
                                                   >> 117 
121 #include <iostream>                               118 #include <iostream>
122 static c2_factory<G4double> c2;  // this makes << 119 #include <iomanip>
                                                   >> 120 
                                                   >> 121 #include "c2_factory.hh"
                                                   >> 122 static c2_factory<G4double> c2; // this makes a lot of notation shorter
123 typedef c2_ptr<G4double> c2p;                     123 typedef c2_ptr<G4double> c2p;
124                                                   124 
125 G4ScreenedCoulombCrossSection::~G4ScreenedCoul    125 G4ScreenedCoulombCrossSection::~G4ScreenedCoulombCrossSection()
126 {                                                 126 {
127   screeningData.clear();                       << 127   screeningData.clear();
128   MFPTables.clear();                           << 128   MFPTables.clear();  
129 }                                                 129 }
130                                                   130 
131 const G4double G4ScreenedCoulombCrossSection:: << 131 const G4double G4ScreenedCoulombCrossSection::massmap[nMassMapElements+1]={
132   0,          1.007940,   4.002602,   6.941000 << 132   0, 1.007940, 4.002602, 6.941000, 9.012182, 10.811000, 12.010700, 
133   15.999400,  18.998403,  20.179700,  22.98977 << 133   14.006700, 15.999400, 18.998403, 20.179700, 22.989770, 24.305000, 26.981538, 28.085500, 
134   32.065000,  35.453000,  39.948000,  39.09830 << 134   30.973761, 32.065000, 35.453000, 39.948000, 39.098300, 40.078000, 44.955910, 47.867000, 
135   51.996100,  54.938049,  55.845000,  58.93320 << 135   50.941500, 51.996100, 54.938049, 55.845000, 58.933200, 58.693400, 63.546000, 65.409000, 
136   72.640000,  74.921600,  78.960000,  79.90400 << 136   69.723000, 72.640000, 74.921600, 78.960000, 79.904000, 83.798000, 85.467800, 87.620000, 
137   91.224000,  92.906380,  95.940000,  98.00000 << 137   88.905850, 91.224000, 92.906380, 95.940000, 98.000000, 101.070000, 102.905500, 106.420000, 
138   112.411000, 114.818000, 118.710000, 121.7600 << 138   107.868200, 112.411000, 114.818000, 118.710000, 121.760000, 127.600000, 126.904470, 131.293000, 
139   137.327000, 138.905500, 140.116000, 140.9076 << 139   132.905450, 137.327000, 138.905500, 140.116000, 140.907650, 144.240000, 145.000000, 150.360000, 
140   157.250000, 158.925340, 162.500000, 164.9303 << 140   151.964000, 157.250000, 158.925340, 162.500000, 164.930320, 167.259000, 168.934210, 173.040000, 
141   178.490000, 180.947900, 183.840000, 186.2070 << 141   174.967000, 178.490000, 180.947900, 183.840000, 186.207000, 190.230000, 192.217000, 195.078000, 
142   200.590000, 204.383300, 207.200000, 208.9803 << 142   196.966550, 200.590000, 204.383300, 207.200000, 208.980380, 209.000000, 210.000000, 222.000000, 
143   226.000000, 227.000000, 232.038100, 231.0358 << 143   223.000000, 226.000000, 227.000000, 232.038100, 231.035880, 238.028910, 237.000000, 244.000000, 
144   247.000000, 247.000000, 251.000000, 252.0000 << 144   243.000000, 247.000000, 247.000000, 251.000000, 252.000000, 257.000000, 258.000000, 259.000000, 
145   261.000000, 262.000000, 266.000000, 264.0000 << 145   262.000000, 261.000000, 262.000000, 266.000000, 264.000000, 277.000000, 268.000000, 281.000000, 
146   285.000000, 282.500000, 289.000000, 287.5000 << 146   272.000000, 285.000000, 282.500000, 289.000000, 287.500000, 292.000000};
147                                                << 147 
148 G4ParticleDefinition*                          << 148 G4ParticleDefinition* G4ScreenedCoulombCrossSection::SelectRandomUnweightedTarget(const G4MaterialCutsCouple* couple)
149 G4ScreenedCoulombCrossSection::SelectRandomUnw << 149 {
150 {                                              << 150   // Select randomly an element within the material, according to number density only 
151   // Select randomly an element within the mat << 151   const G4Material* material = couple->GetMaterial();
152   // density only                              << 152   G4int nMatElements = material->GetNumberOfElements();
153   const G4Material* material = couple->GetMate << 153   const G4ElementVector* elementVector = material->GetElementVector();
154   G4int nMatElements = material->GetNumberOfEl << 154   const G4Element *element=0;
155   const G4ElementVector* elementVector = mater << 155   G4ParticleDefinition*target=0;
156   const G4Element* element = 0;                << 156   
157   G4ParticleDefinition* target = 0;            << 157   // Special case: the material consists of one element
158                                                << 158   if (nMatElements == 1)
159   // Special case: the material consists of on << 159     {
160   if (nMatElements == 1) {                     << 160     element= (*elementVector)[0];
161     element = (*elementVector)[0];             << 161     }
162   }                                            << 162   else
163   else {                                       << 163     {
164     // Composite material                      << 164     // Composite material
165     G4double random = G4UniformRand() * materi << 165     G4double random = G4UniformRand() * material->GetTotNbOfAtomsPerVolume();
166     G4double nsum = 0.0;                       << 166     G4double nsum=0.0;
167     const G4double* atomDensities = material-> << 167     const G4double *atomDensities=material->GetVecNbOfAtomsPerVolume();
168                                                << 168     
169     for (G4int k = 0; k < nMatElements; k++) { << 169     for (G4int k=0 ; k < nMatElements ; k++ )
170       nsum += atomDensities[k];                << 170         {
171       element = (*elementVector)[k];           << 171       nsum+=atomDensities[k];
172       if (nsum >= random) break;               << 172       element= (*elementVector)[k];
173     }                                          << 173       if (nsum >= random) break;
174   }                                            << 174         }
175                                                << 
176   G4int N = 0;                                 << 
177   G4int Z = element->GetZasInt();              << 
178                                                << 
179   G4int nIsotopes = element->GetNumberOfIsotop << 
180   if (0 < nIsotopes) {                         << 
181     if (Z <= 92) {                             << 
182       // we have no detailed material isotopic << 
183       // so use G4StableIsotopes table up to Z << 
184       static G4StableIsotopes theIso;          << 
185       // get a stable isotope table for defaul << 
186       nIsotopes = theIso.GetNumberOfIsotopes(Z << 
187       G4double random = 100.0 * G4UniformRand( << 
188       // values are expressed as percent, sum  << 
189       G4int tablestart = theIso.GetFirstIsotop << 
190       G4double asum = 0.0;                     << 
191       for (G4int i = 0; i < nIsotopes; i++) {  << 
192         asum += theIso.GetAbundance(i + tables << 
193         N = theIso.GetIsotopeNucleonCount(i +  << 
194         if (asum >= random) break;             << 
195       }                                        << 
196     }                                          << 
197     else {                                     << 
198       // too heavy for stable isotope table, j << 
199       N = (G4int)std::floor(element->GetN() +  << 
200     }                                          << 
201   }                                            << 
202   else {                                       << 
203     G4int i;                                   << 
204     const G4IsotopeVector* isoV = element->Get << 
205     G4double random = G4UniformRand();         << 
206     G4double* abundance = element->GetRelative << 
207     G4double asum = 0.0;                       << 
208     for (i = 0; i < nIsotopes; i++) {          << 
209       asum += abundance[i];                    << 
210       N = (*isoV)[i]->GetN();                  << 
211       if (asum >= random) break;               << 
212     }                                             175     }
213   }                                            << 
214                                                   176 
215   // get the official definition of this nucle << 177   G4int N=0;
216   // value of A note that GetIon is very slow, << 178   G4int Z=(G4int)std::floor(element->GetZ()+0.5);
217   // we have already found ourselves.          << 179   
218   ParticleCache::iterator p = targetMap.find(Z << 180   G4int nIsotopes=element->GetNumberOfIsotopes();
219   if (p != targetMap.end()) {                  << 181   if(!nIsotopes) {
220     target = (*p).second;                      << 182     if(Z<=92) {
221   }                                            << 183       // we have no detailed material isotopic info available, 
222   else {                                       << 184       // so use G4StableIsotopes table up to Z=92
223     target = G4IonTable::GetIonTable()->GetIon << 185       static G4StableIsotopes theIso; // get a stable isotope table for default results
224     targetMap[Z * 1000 + N] = target;          << 186       nIsotopes=theIso.GetNumberOfIsotopes(Z);
225   }                                            << 187       G4double random = 100.0*G4UniformRand(); // values are expressed as percent, sum is 100
226   return target;                               << 188       G4int tablestart=theIso.GetFirstIsotope(Z);
                                                   >> 189       G4double asum=0.0;
                                                   >> 190       for(G4int i=0; i<nIsotopes; i++) {
                                                   >> 191         asum+=theIso.GetAbundance(i+tablestart);
                                                   >> 192         N=theIso.GetIsotopeNucleonCount(i+tablestart);
                                                   >> 193         if(asum >= random) break;
                                                   >> 194       }
                                                   >> 195     } else {
                                                   >> 196       // too heavy for stable isotope table, just use mean mass
                                                   >> 197       N=(G4int)std::floor(element->GetN()+0.5);
                                                   >> 198     }
                                                   >> 199   } else {
                                                   >> 200     G4int i;
                                                   >> 201     const G4IsotopeVector *isoV=element->GetIsotopeVector();
                                                   >> 202     G4double random = G4UniformRand();
                                                   >> 203     G4double *abundance=element->GetRelativeAbundanceVector();
                                                   >> 204     G4double asum=0.0;
                                                   >> 205     for(i=0; i<nIsotopes; i++) {
                                                   >> 206       asum+=abundance[i];
                                                   >> 207       N=(*isoV)[i]->GetN();
                                                   >> 208       if(asum >= random) break;
                                                   >> 209     }
                                                   >> 210   }
                                                   >> 211   
                                                   >> 212   // get the official definition of this nucleus, to get the correct value of A
                                                   >> 213   // note that GetIon is very slow, so we will cache ones we have already found ourselves.
                                                   >> 214   ParticleCache::iterator p=targetMap.find(Z*1000+N);
                                                   >> 215   if (p != targetMap.end()) {
                                                   >> 216     target=(*p).second;
                                                   >> 217   } else{
                                                   >> 218     target=G4ParticleTable::GetParticleTable()->GetIon(Z, N, 0.0);  
                                                   >> 219     targetMap[Z*1000+N]=target;
                                                   >> 220   }
                                                   >> 221   return target;
227 }                                                 222 }
228                                                   223 
229 void G4ScreenedCoulombCrossSection::BuildMFPTa    224 void G4ScreenedCoulombCrossSection::BuildMFPTables()
230 {                                                 225 {
231   const G4int nmfpvals = 200;                  << 226   const G4int nmfpvals=200;
232                                                << 
233   std::vector<G4double> evals(nmfpvals), mfpva << 
234                                                   227 
235   // sum up inverse MFPs per element for each  << 228   std::vector<G4double> evals(nmfpvals), mfpvals(nmfpvals);
236   const G4MaterialTable* materialTable = G4Mat << 
237   if (materialTable == 0) {                    << 
238     return;                                    << 
239   }                                            << 
240   // G4Exception("G4ScreenedCoulombCrossSectio << 
241   //- no MaterialTable found)");               << 
242                                                << 
243   G4int nMaterials = G4Material::GetNumberOfMa << 
244                                                << 
245   for (G4int matidx = 0; matidx < nMaterials;  << 
246     const G4Material* material = (*materialTab << 
247     const G4ElementVector& elementVector = *(m << 
248     const G4int nMatElements = material->GetNu << 
249                                                << 
250     const G4Element* element = 0;              << 
251     const G4double* atomDensities = material-> << 
252                                                << 
253     G4double emin = 0, emax = 0;               << 
254     // find innermost range of cross section f << 
255     for (G4int kel = 0; kel < nMatElements; ke << 
256       element = elementVector[kel];            << 
257       G4int Z = (G4int)std::floor(element->Get << 
258       const G4_c2_function& ifunc = sigmaMap[Z << 
259       if (!kel || ifunc.xmin() > emin) emin =  << 
260       if (!kel || ifunc.xmax() < emax) emax =  << 
261     }                                          << 
262                                                   229 
263     G4double logint = std::log(emax / emin) /  << 230   // sum up inverse MFPs per element for each material  
264     // logarithmic increment for tables        << 231   const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
265                                                << 232   if (materialTable == 0)
266     // compute energy scale for interpolator.  << 233     G4Exception("G4ScreenedCoulombCrossSection::BuildMFPTables - no MaterialTable found)");
267     // both ends to avoid range errors         << 234   
268     for (G4int i = 1; i < nmfpvals - 1; i++)   << 235   G4int nMaterials = G4Material::GetNumberOfMaterials();
269       evals[i] = emin * std::exp(logint * i);  << 236 
270     evals.front() = emin;                      << 237   for (G4int matidx=0; matidx < nMaterials; matidx++) {
271     evals.back() = emax;                       << 238 
272                                                << 239     const G4Material* material= (*materialTable)[matidx];
273     // zero out the inverse mfp sums to start  << 240     const G4ElementVector &elementVector = *(material->GetElementVector());
274     for (G4int eidx = 0; eidx < nmfpvals; eidx << 241     const G4int nMatElements = material->GetNumberOfElements();
275       mfpvals[eidx] = 0.0;                     << 242 
276                                                << 243     const G4Element *element=0;
277     // sum inverse mfp for each element in thi << 244     const G4double *atomDensities=material->GetVecNbOfAtomsPerVolume();
278     // energy                                  << 245     
279     for (G4int kel = 0; kel < nMatElements; ke << 246     G4double emin=0, emax=0; // find innermost range of cross section functions
280       element = elementVector[kel];            << 247     for (G4int kel=0 ; kel < nMatElements ; kel++ )
281       G4int Z = (G4int)std::floor(element->Get << 248         {
282       const G4_c2_function& sigma = sigmaMap[Z << 249       element=elementVector[kel];
283       G4double ndens = atomDensities[kel];     << 250       G4int Z=(G4int)std::floor(element->GetZ()+0.5);
284       // compute atom fraction for this elemen << 251       const G4_c2_function &ifunc=sigmaMap[Z];
285                                                << 252       if(!kel || ifunc.xmin() > emin) emin=ifunc.xmin();
286       for (G4int eidx = 0; eidx < nmfpvals; ei << 253       if(!kel || ifunc.xmax() < emax) emax=ifunc.xmax();      
287         mfpvals[eidx] += ndens * sigma(evals[e << 254         }
288       }                                        << 255     
                                                   >> 256     G4double logint=std::log(emax/emin) / (nmfpvals-1) ; // logarithmic increment for tables
                                                   >> 257     
                                                   >> 258     // compute energy scale for interpolator.  Force exact values at both ends to avoid range errors
                                                   >> 259     for (G4int i=1; i<nmfpvals-1; i++) evals[i]=emin*std::exp(logint*i);
                                                   >> 260     evals.front()=emin;
                                                   >> 261     evals.back()=emax;
                                                   >> 262     
                                                   >> 263     // zero out the inverse mfp sums to start
                                                   >> 264     for (G4int eidx=0; eidx < nmfpvals; eidx++) mfpvals[eidx] = 0.0; 
                                                   >> 265     
                                                   >> 266     // sum inverse mfp for each element in this material and for each energy
                                                   >> 267     for (G4int kel=0 ; kel < nMatElements ; kel++ )
                                                   >> 268         {
                                                   >> 269       element=elementVector[kel];
                                                   >> 270       G4int Z=(G4int)std::floor(element->GetZ()+0.5);
                                                   >> 271       const G4_c2_function &sigma=sigmaMap[Z];
                                                   >> 272       G4double ndens = atomDensities[kel]; // compute atom fraction for this element in this material
                                                   >> 273             
                                                   >> 274       for (G4int eidx=0; eidx < nmfpvals; eidx++) {
                                                   >> 275           mfpvals[eidx] += ndens*sigma(evals[eidx]);
                                                   >> 276       }
                                                   >> 277         }
                                                   >> 278     
                                                   >> 279     // convert inverse mfp to regular mfp
                                                   >> 280     for (G4int eidx=0; eidx < nmfpvals; eidx++) {
                                                   >> 281       mfpvals[eidx] = 1.0/mfpvals[eidx];
                                                   >> 282     }
                                                   >> 283     // and make a new interpolating function out of the sum
                                                   >> 284     MFPTables[matidx] = c2.log_log_interpolating_function().load(evals, mfpvals,true,0,true,0);
289     }                                             285     }
                                                   >> 286 }
290                                                   287 
291     // convert inverse mfp to regular mfp      << 288 G4ScreenedNuclearRecoil::
292     for (G4int eidx = 0; eidx < nmfpvals; eidx << 289 G4ScreenedNuclearRecoil(const G4String& processName, 
293       mfpvals[eidx] = 1.0 / mfpvals[eidx];     << 290                  const G4String &ScreeningKey,
294     }                                          << 291                  G4bool GenerateRecoils, 
295     // and make a new interpolating function o << 292                  G4double RecoilCutoff, G4double PhysicsCutoff) : 
296     MFPTables[matidx] = c2.log_log_interpolati << 293   G4VDiscreteProcess(processName),
297   }                                            << 294   screeningKey(ScreeningKey),
298 }                                              << 295   generateRecoils(GenerateRecoils), avoidReactions(1), 
299                                                << 296   recoilCutoff(RecoilCutoff), physicsCutoff(PhysicsCutoff),
300 G4ScreenedNuclearRecoil::G4ScreenedNuclearReco << 297   hardeningFraction(0.0), hardeningFactor(1.0),
301                                                << 298   externalCrossSectionConstructor(0),
302                                                << 299   NIELPartitionFunction(new G4LindhardRobinsonPartition)
303                                                << 300 {
304   : G4VDiscreteProcess(processName, fElectroma << 301     // for now, point to class instance of this. Doing it by creating a new one fails
305     screeningKey(ScreeningKey),                << 302     // to correctly update NIEL
306     generateRecoils(GenerateRecoils),          << 303     // not even this is needed... done in G4VProcess().
307     avoidReactions(1),                         << 304     // pParticleChange=&aParticleChange; 
308     recoilCutoff(RecoilCutoff),                << 305     processMaxEnergy=50000.0*MeV;
309     physicsCutoff(PhysicsCutoff),              << 306     highEnergyLimit=100.0*MeV;
310     hardeningFraction(0.0),                    << 307     lowEnergyLimit=physicsCutoff;
311     hardeningFactor(1.0),                      << 308     registerDepositedEnergy=1; // by default, don't hide NIEL
312     externalCrossSectionConstructor(0),        << 309     MFPScale=1.0;
313     NIELPartitionFunction(new G4LindhardRobins << 310     // SetVerboseLevel(2);
314 {                                              << 311     AddStage(new G4ScreenedCoulombClassicalKinematics);
315   // for now, point to class instance of this. << 312     AddStage(new G4SingleScatter); 
316   // one fails                                 << 313     SetProcessSubType(fCoulombScattering);
317   // to correctly update NIEL                  << 
318   // not even this is needed... done in G4VPro << 
319   // pParticleChange=&aParticleChange;         << 
320   processMaxEnergy = 50000.0 * MeV;            << 
321   highEnergyLimit = 100.0 * MeV;               << 
322   lowEnergyLimit = physicsCutoff;              << 
323   registerDepositedEnergy = 1;  // by default, << 
324   MFPScale = 1.0;                              << 
325   // SetVerboseLevel(2);                       << 
326   AddStage(new G4ScreenedCoulombClassicalKinem << 
327   AddStage(new G4SingleScatter);               << 
328   SetProcessSubType(fCoulombScattering);       << 
329 }                                                 314 }
330                                                   315 
331 void G4ScreenedNuclearRecoil::ResetTables()       316 void G4ScreenedNuclearRecoil::ResetTables()
332 {                                                 317 {
333   std::map<G4int, G4ScreenedCoulombCrossSectio << 318   
334   for (; xt != crossSectionHandlers.end(); xt+ << 319   std::map<G4int, G4ScreenedCoulombCrossSection*>::iterator xt=crossSectionHandlers.begin();
335     delete (*xt).second;                       << 320   for(;xt != crossSectionHandlers.end(); xt++) {
336   }                                            << 321     delete (*xt).second;
337   crossSectionHandlers.clear();                << 322   }
                                                   >> 323   crossSectionHandlers.clear();
338 }                                                 324 }
339                                                   325 
340 void G4ScreenedNuclearRecoil::ClearStages()       326 void G4ScreenedNuclearRecoil::ClearStages()
341 {                                                 327 {
342   // I don't think I like deleting the process << 328   // I don't think I like deleting the processes here... they are better abandoned
343   // abandoned                                 << 329   // if the creator doesn't get rid of them
344   // if the creator doesn't get rid of them    << 330   // std::vector<G4ScreenedCollisionStage *>::iterator stage=collisionStages.begin();
345   // std::vector<G4ScreenedCollisionStage *>:: << 331   //for(; stage != collisionStages.end(); stage++) delete (*stage);
346   // collisionStages.begin();                  << 
347   // for(; stage != collisionStages.end(); sta << 
348                                                   332 
349   collisionStages.clear();                     << 333   collisionStages.clear();
350 }                                                 334 }
351                                                   335 
352 void G4ScreenedNuclearRecoil::SetNIELPartition << 336 void G4ScreenedNuclearRecoil::SetNIELPartitionFunction(const G4VNIELPartition *part)
353 {                                                 337 {
354   if (NIELPartitionFunction) delete NIELPartit << 338   if(NIELPartitionFunction) delete NIELPartitionFunction;
355   NIELPartitionFunction = part;                << 339   NIELPartitionFunction=part;
356 }                                                 340 }
357                                                   341 
358 void G4ScreenedNuclearRecoil::DepositEnergy(G4 << 342 void G4ScreenedNuclearRecoil::DepositEnergy(G4int z1, G4double a1, const G4Material *material, G4double energy)
359                                             G4 << 
360 {                                                 343 {
361   if (!NIELPartitionFunction) {                << 344   if(!NIELPartitionFunction) {
362     IonizingLoss += energy;                    << 345     IonizingLoss+=energy;
363   }                                            << 346   } else {
364   else {                                       << 347     G4double part=NIELPartitionFunction->PartitionNIEL(z1, a1, material, energy);
365     G4double part = NIELPartitionFunction->Par << 348     IonizingLoss+=energy*(1-part);
366     IonizingLoss += energy * (1 - part);       << 349     NIEL += energy*part;
367     NIEL += energy * part;                     << 350   }
368   }                                            << 
369 }                                                 351 }
370                                                   352 
371 G4ScreenedNuclearRecoil::~G4ScreenedNuclearRec    353 G4ScreenedNuclearRecoil::~G4ScreenedNuclearRecoil()
372 {                                                 354 {
373   ResetTables();                               << 355   ResetTables();
374 }                                                 356 }
375                                                   357 
376 // returns true if it appears the nuclei colli << 358 // returns true if it appears the nuclei collided, and we are interested in checking
377 // in checking                                 << 359 G4bool G4ScreenedNuclearRecoil::CheckNuclearCollision(
378 G4bool G4ScreenedNuclearRecoil::CheckNuclearCo << 360       G4double A, G4double a1, G4double apsis) {
379 {                                              << 361   return avoidReactions && (apsis < (1.1*(std::pow(A,1.0/3.0)+std::pow(a1,1.0/3.0)) + 1.4)*fermi);
380   return avoidReactions                        << 362   // nuclei are within 1.4 fm (reduced pion Compton wavelength) of each other at apsis, 
381          && (apsis < (1.1 * (std::pow(A, 1.0 / << 363   // this is hadronic, skip it
382   // nuclei are within 1.4 fm (reduced pion Co << 364 }
383   // other at apsis,                           << 365 
384   // this is hadronic, skip it                 << 366 G4ScreenedCoulombCrossSection *G4ScreenedNuclearRecoil::GetNewCrossSectionHandler(void) {
385 }                                              << 367   G4ScreenedCoulombCrossSection *xc;
386                                                << 368   if(!externalCrossSectionConstructor) xc=new G4NativeScreenedCoulombCrossSection;
387 G4ScreenedCoulombCrossSection* G4ScreenedNucle << 369   else xc=externalCrossSectionConstructor->create();
388 {                                              << 370   xc->SetVerbosity(verboseLevel);
389   G4ScreenedCoulombCrossSection* xc;           << 371   return xc;
390   if (!externalCrossSectionConstructor)        << 372 }
391     xc = new G4NativeScreenedCoulombCrossSecti << 373 
392   else                                         << 374 G4double G4ScreenedNuclearRecoil::GetMeanFreePath(const G4Track& track,
393     xc = externalCrossSectionConstructor->crea << 375               G4double, 
394   xc->SetVerbosity(verboseLevel);              << 376               G4ForceCondition* cond)
395   return xc;                                   << 377 {
396 }                                              << 378   const G4DynamicParticle* incoming = track.GetDynamicParticle();
397                                                << 379   G4double energy = incoming->GetKineticEnergy();
398 G4double G4ScreenedNuclearRecoil::GetMeanFreeP << 380   G4double a1=incoming->GetDefinition()->GetPDGMass()/amu_c2;
399                                                << 381   
400 {                                              << 382   G4double meanFreePath;
401   const G4DynamicParticle* incoming = track.Ge << 383   *cond=NotForced;
402   G4double energy = incoming->GetKineticEnergy << 384   
403   G4double a1 = incoming->GetDefinition()->Get << 385   if (energy < lowEnergyLimit || energy < recoilCutoff*a1) {
404                                                << 386     *cond=Forced;
405   G4double meanFreePath;                       << 387     return 1.0*nm; /* catch and stop slow particles to collect their NIEL! */
406   *cond = NotForced;                           << 388   } else if (energy > processMaxEnergy*a1) {
407                                                << 389     return DBL_MAX; // infinite mean free path
408   if (energy < lowEnergyLimit || energy < reco << 390   } else if (energy > highEnergyLimit*a1) energy=highEnergyLimit*a1; /* constant MFP at high energy */
409     *cond = Forced;                            << 391 
410     return 1.0 * nm;                           << 392   G4double fz1=incoming->GetDefinition()->GetPDGCharge();
411     /* catch and stop slow particles to collec << 393   G4int z1=(G4int)(fz1/eplus + 0.5);
412   }                                            << 394 
413   else if (energy > processMaxEnergy * a1) {   << 395   std::map<G4int, G4ScreenedCoulombCrossSection*>::iterator xh=
414     return DBL_MAX;  // infinite mean free pat << 396     crossSectionHandlers.find(z1);
415   }                                            << 397   G4ScreenedCoulombCrossSection *xs;
416   else if (energy > highEnergyLimit * a1)      << 398   
417     energy = highEnergyLimit * a1;             << 399   if (xh==crossSectionHandlers.end()) {
418   /* constant MFP at high energy */            << 400     xs =crossSectionHandlers[z1]=GetNewCrossSectionHandler();
419                                                << 401     xs->LoadData(screeningKey, z1, a1, physicsCutoff);
420   G4double fz1 = incoming->GetDefinition()->Ge << 402     xs->BuildMFPTables();
421   G4int z1 = (G4int)(fz1 / eplus + 0.5);       << 403   } else xs=(*xh).second;
422                                                << 404   
423   std::map<G4int, G4ScreenedCoulombCrossSectio << 405   const G4MaterialCutsCouple* materialCouple = track.GetMaterialCutsCouple();
424   G4ScreenedCoulombCrossSection* xs;           << 406   size_t materialIndex = materialCouple->GetMaterial()->GetIndex();
425                                                << 407 
426   if (xh == crossSectionHandlers.end()) {      << 408   const G4_c2_function &mfp=*(*xs)[materialIndex];
427     xs = crossSectionHandlers[z1] = GetNewCros << 409   
428     xs->LoadData(screeningKey, z1, a1, physics << 410   // make absolutely certain we don't get an out-of-range energy
429     xs->BuildMFPTables();                      << 411   meanFreePath = mfp(std::min(std::max(energy, mfp.xmin()), mfp.xmax()));
430   }                                            << 412   
431   else                                         << 413   // G4cout << "MFP: " << meanFreePath << " index " << materialIndex << " energy " << energy << " MFPScale " << MFPScale << G4endl;
432     xs = (*xh).second;                         << 414   
433                                                << 415   return meanFreePath*MFPScale;
434   const G4MaterialCutsCouple* materialCouple = << 
435   size_t materialIndex = materialCouple->GetMa << 
436                                                << 
437   const G4_c2_function& mfp = *(*xs)[materialI << 
438                                                << 
439   // make absolutely certain we don't get an o << 
440   meanFreePath = mfp(std::min(std::max(energy, << 
441                                                << 
442   // G4cout << "MFP: " << meanFreePath << " in << 
443   //<< " energy " << energy << " MFPScale " << << 
444                                                << 
445   return meanFreePath * MFPScale;              << 
446 }                                                 416 }
447                                                   417 
448 G4VParticleChange* G4ScreenedNuclearRecoil::Po    418 G4VParticleChange* G4ScreenedNuclearRecoil::PostStepDoIt(const G4Track& aTrack, const G4Step& aStep)
449 {                                                 419 {
450   validCollision = 1;                          << 420   validCollision=1;
451   pParticleChange->Initialize(aTrack);         << 421   pParticleChange->Initialize(aTrack);
452   NIEL = 0.0;  // default is no NIEL deposited << 422   NIEL=0.0; // default is no NIEL deposited
453   IonizingLoss = 0.0;                          << 423   IonizingLoss=0.0;
454                                                << 424   
455   // do universal setup                        << 425   // do universal setup
456                                                << 426   
457   const G4DynamicParticle* incidentParticle =  << 427   const G4DynamicParticle* incidentParticle = aTrack.GetDynamicParticle();
458   G4ParticleDefinition* baseParticle = aTrack. << 428   G4ParticleDefinition *baseParticle=aTrack.GetDefinition();
459                                                << 429   
460   G4double fz1 = baseParticle->GetPDGCharge()  << 430   G4double fz1=baseParticle->GetPDGCharge()/eplus;
461   G4int z1 = (G4int)(fz1 + 0.5);               << 431   G4int z1=(G4int)(fz1+0.5);
462   G4double a1 = baseParticle->GetPDGMass() / a << 432   G4double a1=baseParticle->GetPDGMass()/amu_c2;
463   G4double incidentEnergy = incidentParticle-> << 433   G4double incidentEnergy = incidentParticle->GetKineticEnergy();
464                                                << 434     
465   // Select randomly one element and (possibly << 435   // Select randomly one element and (possibly) isotope in the current material.
466   // current material.                         << 436   const G4MaterialCutsCouple* couple = aTrack.GetMaterialCutsCouple();
467   const G4MaterialCutsCouple* couple = aTrack. << 437   
468                                                << 438   const G4Material* mat = couple->GetMaterial();
469   const G4Material* mat = couple->GetMaterial( << 439 
470                                                << 440   G4double P=0.0; // the impact parameter of this collision
471   G4double P = 0.0;  // the impact parameter o << 441 
472                                                << 442   if(incidentEnergy < GetRecoilCutoff()*a1) { // check energy sanity on entry
473   if (incidentEnergy < GetRecoilCutoff() * a1) << 443     DepositEnergy(z1, baseParticle->GetPDGMass()/amu_c2, mat, incidentEnergy);
474     // check energy sanity on entry            << 444     GetParticleChange().ProposeEnergy(0.0);
475     DepositEnergy(z1, baseParticle->GetPDGMass << 445     // stop the particle and bail out
476     GetParticleChange().ProposeEnergy(0.0);    << 446     validCollision=0;
477     // stop the particle and bail out          << 447   } else {
478     validCollision = 0;                        << 448     
479   }                                            << 449     G4double numberDensity=mat->GetTotNbOfAtomsPerVolume();
480   else {                                       << 450     G4double lattice=0.5/std::pow(numberDensity,1.0/3.0); // typical lattice half-spacing
481     G4double numberDensity = mat->GetTotNbOfAt << 451     G4double length=GetCurrentInteractionLength();
482     G4double lattice = 0.5 / std::pow(numberDe << 452     G4double sigopi=1.0/(CLHEP::pi*numberDensity*length);  // this is sigma0/pi
483     // typical lattice half-spacing            << 453     
484     G4double length = GetCurrentInteractionLen << 454     // compute the impact parameter very early, so if is rejected as too far away, little effort is wasted
485     G4double sigopi = 1.0 / (pi * numberDensit << 455     // this is the TRIM method for determining an impact parameter based on the flight path
486     // this is sigma0/pi                       << 456     // this gives a cumulative distribution of N(P)= 1-exp(-pi P^2 n l)
487                                                << 457     // which says the probability of NOT hitting a disk of area sigma= pi P^2 =exp(-sigma N l) 
488     // compute the impact parameter very early << 458     // which may be reasonable
489     // as too far away, little effort is waste << 459     if(sigopi < lattice*lattice) { 
490     // this is the TRIM method for determining << 460       // normal long-flight approximation
491     // based on the flight path                << 461       P = std::sqrt(-std::log(G4UniformRand()) *sigopi);
492     // this gives a cumulative distribution of << 462     } else {
493     // N(P)= 1-exp(-pi P^2 n l)                << 463       // short-flight limit
494     // which says the probability of NOT hitti << 464       P = std::sqrt(G4UniformRand())*lattice;
495     // sigma= pi P^2 =exp(-sigma N l)          << 465     }
496     // which may be reasonable                 << 466       
497     if (sigopi < lattice * lattice) {          << 467     G4double fraction=GetHardeningFraction();
498       // normal long-flight approximation      << 468     if(fraction && G4UniformRand() < fraction) { 
499       P = std::sqrt(-std::log(G4UniformRand()) << 469       // pick out some events, and increase the central cross section
500     }                                          << 470       // by reducing the impact parameter
501     else {                                     << 471       P /= std::sqrt(GetHardeningFactor());
502       // short-flight limit                    << 472     }
503       P = std::sqrt(G4UniformRand()) * lattice << 473     
504     }                                          << 474     
505                                                << 475     // check if we are far enough away that the energy transfer must be below cutoff, 
506     G4double fraction = GetHardeningFraction() << 476     // and leave everything alone if so, saving a lot of time.
507     if (fraction && G4UniformRand() < fraction << 477     if(P*P > sigopi) {
508       // pick out some events, and increase th << 478       if(GetVerboseLevel() > 1) 
509       // section by reducing the impact parame << 479         printf("ScreenedNuclear impact reject: length=%.3f P=%.4f limit=%.4f\n",
510       P /= std::sqrt(GetHardeningFactor());    << 480              length/angstrom, P/angstrom,std::sqrt(sigopi)/angstrom);
511     }                                          << 481       // no collision, don't follow up with anything
512                                                << 482       validCollision=0;
513     // check if we are far enough away that th << 483     }
514     // must be below cutoff,                   << 484   }
515     // and leave everything alone if so, savin << 485   
516     if (P * P > sigopi) {                      << 486   // find out what we hit, and record it in our kinematics block.
517       if (GetVerboseLevel() > 1)               << 487   kinematics.targetMaterial=mat;
518         printf("ScreenedNuclear impact reject: << 488   kinematics.a1=a1;
519                P / angstrom, std::sqrt(sigopi) << 489   
520       // no collision, don't follow up with an << 490   if(validCollision) {
521       validCollision = 0;                      << 491     G4ScreenedCoulombCrossSection *xsect=GetCrossSectionHandlers()[z1];
522     }                                          << 492     G4ParticleDefinition *recoilIon=
523   }                                            << 493       xsect->SelectRandomUnweightedTarget(couple);
524                                                << 494     kinematics.crossSection=xsect;
525   // find out what we hit, and record it in ou << 495     kinematics.recoilIon=recoilIon;
526   kinematics.targetMaterial = mat;             << 496     kinematics.impactParameter=P;
527   kinematics.a1 = a1;                          << 497     kinematics.a2=recoilIon->GetPDGMass()/amu_c2;
528                                                << 498   } else {
529   if (validCollision) {                        << 499     kinematics.recoilIon=0;
530     G4ScreenedCoulombCrossSection* xsect = Get << 500     kinematics.impactParameter=0;
531     G4ParticleDefinition* recoilIon = xsect->S << 501     kinematics.a2=0;
532     kinematics.crossSection = xsect;           << 502   }
533     kinematics.recoilIon = recoilIon;          << 503   
534     kinematics.impactParameter = P;            << 504   std::vector<G4ScreenedCollisionStage *>::iterator stage=collisionStages.begin();
535     kinematics.a2 = recoilIon->GetPDGMass() /  << 505   
536   }                                            << 506   for(; stage != collisionStages.end(); stage++) 
537   else {                                       << 507     (*stage)->DoCollisionStep(this,aTrack, aStep);
538     kinematics.recoilIon = 0;                  << 508   
539     kinematics.impactParameter = 0;            << 509   if(registerDepositedEnergy) {
540     kinematics.a2 = 0;                         << 510     pParticleChange->ProposeLocalEnergyDeposit(IonizingLoss+NIEL);
541   }                                            << 511     pParticleChange->ProposeNonIonizingEnergyDeposit(NIEL);
542                                                << 512     //MHM G4cout << "depositing energy, total = " << IonizingLoss+NIEL << " NIEL = " << NIEL << G4endl;
543   std::vector<G4ScreenedCollisionStage*>::iter << 513   }
544                                                << 514 
545   for (; stage != collisionStages.end(); stage << 515   return G4VDiscreteProcess::PostStepDoIt( aTrack, aStep );
546     (*stage)->DoCollisionStep(this, aTrack, aS << 516 }
547                                                << 517 
548   if (registerDepositedEnergy) {               << 518 G4ScreenedCoulombClassicalKinematics::G4ScreenedCoulombClassicalKinematics() :
549     pParticleChange->ProposeLocalEnergyDeposit << 519 // instantiate all the needed functions statically, so no allocation is done at run time
550     pParticleChange->ProposeNonIonizingEnergyD << 520 // we will be solving x^2 - x phi(x*au)/eps - beta^2 == 0.0
551     // MHM G4cout << "depositing energy, total << 521 // or, for easier scaling, x'^2 - x' au phi(x')/eps - beta^2 au^2
552     //<< IonizingLoss+NIEL << " NIEL = " << NI << 522 // note that only the last of these gets deleted, since it owns the rest
553   }                                            << 523 phifunc(c2.const_plugin_function()),
554                                                << 524 xovereps(c2.linear(0., 0., 0.)), // will fill this in with the right slope at run time
555   return G4VDiscreteProcess::PostStepDoIt(aTra << 525 diff(c2.quadratic(0., 0., 0., 1.)-xovereps*phifunc)
556 }                                              << 526 {
557                                                << 527 }
558 G4ScreenedCoulombClassicalKinematics::G4Screen << 528 
559   :  // instantiate all the needed functions s << 529 G4bool G4ScreenedCoulombClassicalKinematics::DoScreeningComputation(G4ScreenedNuclearRecoil *master,
560      // done at run time                       << 530     const G4ScreeningTables *screen, G4double eps, G4double beta)
561      // we will be solving x^2 - x phi(x*au)/e << 531 {
562      // or, for easier scaling, x'^2 - x' au p << 532   G4double au=screen->au;
563      // note that only the last of these gets  << 533   G4CoulombKinematicsInfo &kin=master->GetKinematics();
564     phifunc(c2.const_plugin_function()),       << 534   G4double A=kin.a2;
565     xovereps(c2.linear(0., 0., 0.)),           << 535   G4double a1=kin.a1;
566     // will fill this in with the right slope  << 536   
567     diff(c2.quadratic(0., 0., 0., 1.) - xovere << 537   G4double xx0; // first estimate of closest approach
568 {}                                             << 538   if(eps < 5.0) {
569                                                << 539     G4double y=std::log(eps);
570 G4bool G4ScreenedCoulombClassicalKinematics::D << 540     G4double mlrho4=((((3.517e-4*y+1.401e-2)*y+2.393e-1)*y+2.734)*y+2.220);
571                                                << 541     G4double rho4=std::exp(-mlrho4); // W&M eq. 18
572                                                << 542     G4double bb2=0.5*beta*beta;
573 {                                              << 543     xx0=std::sqrt(bb2+std::sqrt(bb2*bb2+rho4)); // W&M eq. 17
574   G4double au = screen->au;                    << 544   } else {
575   G4CoulombKinematicsInfo& kin = master->GetKi << 545     G4double ee=1.0/(2.0*eps);
576   G4double A = kin.a2;                         << 546     xx0=ee+std::sqrt(ee*ee+beta*beta); // W&M eq. 15 (Rutherford value)   
577   G4double a1 = kin.a1;                        << 547     if(master->CheckNuclearCollision(A, a1, xx0*au)) return 0; // nuclei too close
578                                                << 548     
579   G4double xx0;  // first estimate of closest  << 549   }
580   if (eps < 5.0) {                             << 550   
581     G4double y = std::log(eps);                << 551   // we will be solving x^2 - x phi(x*au)/eps - beta^2 == 0.0
582     G4double mlrho4 = ((((3.517e-4 * y + 1.401 << 552   // or, for easier scaling, x'^2 - x' au phi(x')/eps - beta^2 au^2
583     G4double rho4 = std::exp(-mlrho4);  // W&M << 553   xovereps.reset(0., 0.0, au/eps); // slope of x*au/eps term
584     G4double bb2 = 0.5 * beta * beta;          << 554   phifunc.set_function(&(screen->EMphiData.get())); // install interpolating table
585     xx0 = std::sqrt(bb2 + std::sqrt(bb2 * bb2  << 555   G4double xx1, phip, phip2;
586   }                                            << 556   G4int root_error; 
587   else {                                       << 557   xx1=diff->find_root(phifunc.xmin(), std::min(10*xx0*au,phifunc.xmax()), 
588     G4double ee = 1.0 / (2.0 * eps);           << 558              std::min(xx0*au, phifunc.xmax()), beta*beta*au*au, &root_error, &phip, &phip2)/au; 
589     xx0 = ee + std::sqrt(ee * ee + beta * beta << 559   
590     if (master->CheckNuclearCollision(A, a1, x << 560   if(root_error) {
591     // nuclei too close                        << 561     G4cout << "Screened Coulomb Root Finder Error" << G4endl;
592   }                                            << 562     G4cout << "au " << au << " A " << A << " a1 " << a1 << " xx1 " << xx1 << " eps " << eps << " beta " << beta << G4endl;
593                                                << 563     G4cout << " xmin " << phifunc.xmin() << " xmax " << std::min(10*xx0*au,phifunc.xmax()) ;
594   // we will be solving x^2 - x phi(x*au)/eps  << 564     G4cout << " f(xmin) " << phifunc(phifunc.xmin()) <<  " f(xmax) " << phifunc(std::min(10*xx0*au,phifunc.xmax())) ;
595   // or, for easier scaling, x'^2 - x' au phi( << 565     G4cout << " xstart " << std::min(xx0*au, phifunc.xmax()) <<  " target " <<  beta*beta*au*au ;
596   xovereps.reset(0., 0.0, au / eps);  // slope << 566     G4cout << G4endl;
597   phifunc.set_function(&(screen->EMphiData.get << 567     throw c2_exception("Failed root find");
598   // install interpolating table               << 568   }
599   G4double xx1, phip, phip2;                   << 569   
600   G4int root_error;                            << 570   // phiprime is scaled by one factor of au because phi is evaluated at (xx0*au),
601   xx1 = diff->find_root(phifunc.xmin(), std::m << 571   G4double phiprime=phip*au;
602                         std::min(xx0 * au, phi << 572   
603                         &phip, &phip2)         << 573   //lambda0 is from W&M 19
604         / au;                                  << 574   G4double lambda0=1.0/std::sqrt(0.5+beta*beta/(2.0*xx1*xx1)-phiprime/(2.0*eps));
605                                                << 575   
606   if (root_error) {                            << 576   //compute the 6-term Lobatto integral alpha (per W&M 21, with different coefficients)
607     G4cout << "Screened Coulomb Root Finder Er << 577   // this is probably completely un-needed but gives the highest quality results,
608     G4cout << "au " << au << " A " << A << " a << 578   G4double alpha=(1.0+ lambda0)/30.0;
609            << " beta " << beta << G4endl;      << 579   G4double xvals[]={0.98302349, 0.84652241, 0.53235309, 0.18347974};
610     G4cout << " xmin " << phifunc.xmin() << "  << 580   G4double weights[]={0.03472124, 0.14769029, 0.23485003, 0.18602489};
611     G4cout << " f(xmin) " << phifunc(phifunc.x << 581   for(G4int k=0; k<4; k++) {
612            << phifunc(std::min(10 * xx0 * au,  << 582     G4double x, ff;
613     G4cout << " xstart " << std::min(xx0 * au, << 583     x=xx1/xvals[k];
614            << beta * beta * au * au;           << 584     ff=1.0/std::sqrt(1.0-phifunc(x*au)/(x*eps)-beta*beta/(x*x));
615     G4cout << G4endl;                          << 585     alpha+=weights[k]*ff;
616     throw c2_exception("Failed root find");    << 586   }
617   }                                            << 587   
618                                                << 588   phifunc.unset_function(); // throws an exception if used without setting again
619   // phiprime is scaled by one factor of au be << 589 
620   // at (xx0*au),                              << 590   G4double thetac1=CLHEP::pi*beta*alpha/xx1; // complement of CM scattering angle
621   G4double phiprime = phip * au;               << 591   G4double sintheta=std::sin(thetac1); //note sin(pi-theta)=sin(theta)
622                                                << 592   G4double costheta=-std::cos(thetac1); // note cos(pi-theta)=-cos(theta)
623   // lambda0 is from W&M 19                    << 593                       // G4double psi=std::atan2(sintheta, costheta+a1/A); // lab scattering angle (M&T 3rd eq. 8.69)
624   G4double lambda0 =                           << 594   
625     1.0 / std::sqrt(0.5 + beta * beta / (2.0 * << 595   // numerics note:  because we checked above for reasonable values of beta which give real recoils,
626                                                << 596   // we don't have to look too closely for theta -> 0 here (which would cause sin(theta)
627   // compute the 6-term Lobatto integral alpha << 597   // and 1-cos(theta) to both vanish and make the atan2 ill behaved).
628   // different coefficients)                   << 598   G4double zeta=std::atan2(sintheta, 1-costheta); // lab recoil angle (M&T 3rd eq. 8.73)
629   // this is probably completely un-needed but << 599   G4double coszeta=std::cos(zeta);
630   // quality results,                          << 600   G4double sinzeta=std::sin(zeta);
631   G4double alpha = (1.0 + lambda0) / 30.0;     << 601 
632   G4double xvals[] = {0.98302349, 0.84652241,  << 602   kin.sinTheta=sintheta;
633   G4double weights[] = {0.03472124, 0.14769029 << 603   kin.cosTheta=costheta;
634   for (G4int k = 0; k < 4; k++) {              << 604   kin.sinZeta=sinzeta;
635     G4double x, ff;                            << 605   kin.cosZeta=coszeta;
636     x = xx1 / xvals[k];                        << 606   return 1; // all OK, collision is valid
637     ff = 1.0 / std::sqrt(1.0 - phifunc(x * au) << 607 }
638     alpha += weights[k] * ff;                  << 608 
639   }                                            << 609 void G4ScreenedCoulombClassicalKinematics::DoCollisionStep(G4ScreenedNuclearRecoil *master,
640                                                << 610     const G4Track& aTrack, const G4Step&) {
641   phifunc.unset_function();                    << 611   
642   // throws an exception if used without setti << 612   if(!master->GetValidCollision()) return;
643                                                << 613   
644   G4double thetac1 = pi * beta * alpha / xx1;  << 614   G4ParticleChange &aParticleChange=master->GetParticleChange();
645   // complement of CM scattering angle         << 615   G4CoulombKinematicsInfo &kin=master->GetKinematics();
646   G4double sintheta = std::sin(thetac1);  // n << 616   
647   G4double costheta = -std::cos(thetac1);  //  << 617   const G4DynamicParticle* incidentParticle = aTrack.GetDynamicParticle();
648   // G4double psi=std::atan2(sintheta, costhet << 618   G4ParticleDefinition *baseParticle=aTrack.GetDefinition();
649   // lab scattering angle (M&T 3rd eq. 8.69)   << 619   
650                                                << 620   G4double incidentEnergy = incidentParticle->GetKineticEnergy();
651   // numerics note:  because we checked above  << 621   
652   // of beta which give real recoils,          << 622   // this adjustment to a1 gives the right results for soft (constant gamma)
653   // we don't have to look too closely for the << 623   // relativistic collisions.  Hard collisions are wrong anyway, since the
654   // (which would cause sin(theta)             << 624   // Coulombic and hadronic terms interfere and cannot be added.
655   // and 1-cos(theta) to both vanish and make  << 625   G4double gamma=(1.0+incidentEnergy/baseParticle->GetPDGMass());
656   G4double zeta = std::atan2(sintheta, 1 - cos << 626   G4double a1=kin.a1*gamma; // relativistic gamma correction
657   // lab recoil angle (M&T 3rd eq. 8.73)       << 627   
658   G4double coszeta = std::cos(zeta);           << 628   G4ParticleDefinition *recoilIon=kin.recoilIon;
659   G4double sinzeta = std::sin(zeta);           << 629   G4double A=recoilIon->GetPDGMass()/amu_c2;
660                                                << 630   G4int Z=(G4int)((recoilIon->GetPDGCharge()/eplus)+0.5);
661   kin.sinTheta = sintheta;                     << 631   
662   kin.cosTheta = costheta;                     << 632   G4double Ec = incidentEnergy*(A/(A+a1)); // energy in CM frame (non-relativistic!)
663   kin.sinZeta = sinzeta;                       << 633   const G4ScreeningTables *screen=kin.crossSection->GetScreening(Z);
664   kin.cosZeta = coszeta;                       << 634   G4double au=screen->au; // screening length
665   return 1;  // all OK, collision is valid     << 635   
666 }                                              << 636   G4double beta = kin.impactParameter/au; // dimensionless impact parameter
667                                                << 637   G4double eps = Ec/(screen->z1*Z*elm_coupling/au); // dimensionless energy
668 void G4ScreenedCoulombClassicalKinematics::DoC << 638   
669                                                << 639   G4bool ok=DoScreeningComputation(master, screen, eps, beta);  
670 {                                              << 640   if(!ok) {
671   if (!master->GetValidCollision()) return;    << 641     master->SetValidCollision(0); // flag bad collision
672                                                << 642     return; // just bail out without setting valid flag
673   G4ParticleChange& aParticleChange = master-> << 643   }
674   G4CoulombKinematicsInfo& kin = master->GetKi << 644   
675                                                << 645   G4double eRecoil=4*incidentEnergy*a1*A*kin.cosZeta*kin.cosZeta/((a1+A)*(a1+A));
676   const G4DynamicParticle* incidentParticle =  << 646   kin.eRecoil=eRecoil;
677   G4ParticleDefinition* baseParticle = aTrack. << 647   
678                                                << 648   if(incidentEnergy-eRecoil < master->GetRecoilCutoff()*a1) {
679   G4double incidentEnergy = incidentParticle-> << 649     aParticleChange.ProposeEnergy(0.0);
680                                                << 650     master->DepositEnergy(int(screen->z1), a1, kin.targetMaterial, incidentEnergy-eRecoil);
681   // this adjustment to a1 gives the right res << 651   } 
682   // (constant gamma)                          << 652   
683   // relativistic collisions.  Hard collisions << 653   if(master->GetEnableRecoils() && eRecoil > master->GetRecoilCutoff() * kin.a2) {
684   // Coulombic and hadronic terms interfere an << 654     kin.recoilIon=recoilIon;    
685   G4double gamma = (1.0 + incidentEnergy / bas << 655   } else {
686   G4double a1 = kin.a1 * gamma;  // relativist << 656     kin.recoilIon=0; // this flags no recoil to be generated
687                                                << 657     master->DepositEnergy(Z, A, kin.targetMaterial, eRecoil) ;
688   G4ParticleDefinition* recoilIon = kin.recoil << 658   } 
689   G4double A = recoilIon->GetPDGMass() / amu_c << 659 }
690   G4int Z = (G4int)((recoilIon->GetPDGCharge() << 660 
691                                                << 661 void G4SingleScatter::DoCollisionStep(G4ScreenedNuclearRecoil *master,
692   G4double Ec = incidentEnergy * (A / (A + a1) << 662                     const G4Track& aTrack, const G4Step&) {
693   // energy in CM frame (non-relativistic!)    << 663   
694   const G4ScreeningTables* screen = kin.crossS << 664   if(!master->GetValidCollision()) return;
695   G4double au = screen->au;  // screening leng << 665 
696                                                << 666   G4CoulombKinematicsInfo &kin=master->GetKinematics();
697   G4double beta = kin.impactParameter / au;    << 667   G4ParticleChange &aParticleChange=master->GetParticleChange();
698   // dimensionless impact parameter            << 668 
699   G4double eps = Ec / (screen->z1 * Z * elm_co << 669   const G4DynamicParticle* incidentParticle = aTrack.GetDynamicParticle();
700   // dimensionless energy                      << 670   G4double incidentEnergy = incidentParticle->GetKineticEnergy();
701                                                << 671   G4double eRecoil=kin.eRecoil;
702   G4bool ok = DoScreeningComputation(master, s << 672   
703   if (!ok) {                                   << 673   G4double azimuth=G4UniformRand()*(2.0*CLHEP::pi);
704     master->SetValidCollision(0);  // flag bad << 674   G4double sa=std::sin(azimuth);
705     return;  // just bail out without setting  << 675   G4double ca=std::cos(azimuth);
706   }                                            << 676 
707                                                << 677   G4ThreeVector recoilMomentumDirection(kin.sinZeta*ca, kin.sinZeta*sa, kin.cosZeta);
708   G4double eRecoil =                           << 678   G4ParticleMomentum incidentDirection = incidentParticle->GetMomentumDirection();
709     4 * incidentEnergy * a1 * A * kin.cosZeta  << 679   recoilMomentumDirection=recoilMomentumDirection.rotateUz(incidentDirection);
710   kin.eRecoil = eRecoil;                       << 680   G4ThreeVector recoilMomentum=recoilMomentumDirection*std::sqrt(2.0*eRecoil*kin.a2*amu_c2);
711                                                << 681   
712   if (incidentEnergy - eRecoil < master->GetRe << 682   if(aParticleChange.GetEnergy() != 0.0) { // DoKinematics hasn't stopped it!
713     aParticleChange.ProposeEnergy(0.0);        << 683     G4ThreeVector beamMomentum=incidentParticle->GetMomentum()-recoilMomentum;
714     master->DepositEnergy(int(screen->z1), a1, << 684     aParticleChange.ProposeMomentumDirection(beamMomentum.unit()) ;
715   }                                            << 685     aParticleChange.ProposeEnergy(incidentEnergy-eRecoil);
716                                                << 686   }
717   if (master->GetEnableRecoils() && eRecoil >  << 687   
718     kin.recoilIon = recoilIon;                 << 688   if(kin.recoilIon) {
719   }                                            << 689     G4DynamicParticle* recoil = new G4DynamicParticle (kin.recoilIon,
720   else {                                       << 690         recoilMomentumDirection,eRecoil) ;
721     kin.recoilIon = 0;  // this flags no recoi << 691     
722     master->DepositEnergy(Z, A, kin.targetMate << 692     aParticleChange.SetNumberOfSecondaries(1);
723   }                                            << 693     aParticleChange.AddSecondary(recoil);   
724 }                                              << 694   }
725                                                << 695 }
726 void G4SingleScatter::DoCollisionStep(G4Screen << 696 
727                                       const G4 << 697 G4bool G4ScreenedNuclearRecoil::
728 {                                              << 698 IsApplicable(const G4ParticleDefinition& aParticleType)
729   if (!master->GetValidCollision()) return;    << 699 {
730                                                << 700   return  aParticleType == *(G4Proton::Proton()) ||
731   G4CoulombKinematicsInfo& kin = master->GetKi << 701   aParticleType.GetParticleType() == "nucleus" ||
732   G4ParticleChange& aParticleChange = master-> << 702   aParticleType.GetParticleType() == "static_nucleus";
733                                                << 703 }
734   const G4DynamicParticle* incidentParticle =  << 704 
735   G4double incidentEnergy = incidentParticle-> << 705 
736   G4double eRecoil = kin.eRecoil;              << 706 void 
737                                                << 707 G4ScreenedNuclearRecoil::
738   G4double azimuth = G4UniformRand() * (2.0 *  << 708 DumpPhysicsTable(const G4ParticleDefinition&)
739   G4double sa = std::sin(azimuth);             << 
740   G4double ca = std::cos(azimuth);             << 
741                                                << 
742   G4ThreeVector recoilMomentumDirection(kin.si << 
743   G4ParticleMomentum incidentDirection = incid << 
744   recoilMomentumDirection = recoilMomentumDire << 
745   G4ThreeVector recoilMomentum =               << 
746     recoilMomentumDirection * std::sqrt(2.0 *  << 
747                                                << 
748   if (aParticleChange.GetEnergy() != 0.0) {    << 
749     // DoKinematics hasn't stopped it!         << 
750     G4ThreeVector beamMomentum = incidentParti << 
751     aParticleChange.ProposeMomentumDirection(b << 
752     aParticleChange.ProposeEnergy(incidentEner << 
753   }                                            << 
754                                                << 
755   if (kin.recoilIon) {                         << 
756     G4DynamicParticle* recoil =                << 
757       new G4DynamicParticle(kin.recoilIon, rec << 
758                                                << 
759     aParticleChange.SetNumberOfSecondaries(1); << 
760     aParticleChange.AddSecondary(recoil);      << 
761   }                                            << 
762 }                                              << 
763                                                << 
764 G4bool G4ScreenedNuclearRecoil::IsApplicable(c << 
765 {                                              << 
766   return aParticleType == *(G4Proton::Proton() << 
767          || aParticleType.GetParticleType() == << 
768 }                                              << 
769                                                << 
770 void G4ScreenedNuclearRecoil::BuildPhysicsTabl << 
771 {                                                 709 {
772   G4String nam = aParticleType.GetParticleName << 
773   if (nam == "GenericIon" || nam == "proton" | << 
774       || nam == "alpha" || nam == "He3")       << 
775   {                                            << 
776     G4cout << G4endl << GetProcessName() << ": << 
777            << "    SubType= " << GetProcessSub << 
778            << "    maxEnergy(MeV)= " << proces << 
779   }                                            << 
780 }                                                 710 }
781                                                   711 
782 void G4ScreenedNuclearRecoil::DumpPhysicsTable << 
783                                                << 
784 // This used to be the file mhmScreenedNuclear    712 // This used to be the file mhmScreenedNuclearRecoil_native.cc
785 // it has been included here to collect this f << 713 // it has been included here to collect this file into a smaller number of packages
786 // number of packages                          << 
787                                                   714 
788 #include "G4DataVector.hh"                        715 #include "G4DataVector.hh"
                                                   >> 716 #include "G4Material.hh"
789 #include "G4Element.hh"                           717 #include "G4Element.hh"
790 #include "G4ElementVector.hh"                  << 
791 #include "G4Isotope.hh"                           718 #include "G4Isotope.hh"
792 #include "G4Material.hh"                       << 
793 #include "G4MaterialCutsCouple.hh"                719 #include "G4MaterialCutsCouple.hh"
794                                                << 720 #include "G4ElementVector.hh"
795 #include <vector>                                 721 #include <vector>
796                                                   722 
797 G4_c2_function& ZBLScreening(G4int z1, G4int z << 723 G4_c2_function &ZBLScreening(G4int z1, G4int z2, size_t npoints, G4double rMax, G4double *auval)
798 {                                                 724 {
799   static const size_t ncoef = 4;               << 725   static const size_t ncoef=4;
800   static G4double scales[ncoef] = {-3.2, -0.94 << 726   static G4double scales[ncoef]={-3.2, -0.9432, -0.4028, -0.2016};
801   static G4double coefs[ncoef] = {0.1818, 0.50 << 727   static G4double coefs[ncoef]={0.1818,0.5099,0.2802,0.0281};
802                                                << 728   
803   G4double au = 0.8854 * angstrom * 0.529 / (s << 729   G4double au=0.8854*angstrom*0.529/(std::pow(z1, 0.23)+std::pow(z2,0.23));
804   std::vector<G4double> r(npoints), phi(npoint << 730   std::vector<G4double> r(npoints), phi(npoints);
805                                                << 731   
806   for (size_t i = 0; i < npoints; i++) {       << 732   for(size_t i=0; i<npoints; i++) {
807     G4double rr = (float)i / (float)(npoints - << 733     G4double rr=(float)i/(float)(npoints-1);
808     r[i] = rr * rr * rMax;                     << 734     r[i]=rr*rr*rMax; // use quadratic r scale to make sampling fine near the center
809     // use quadratic r scale to make sampling  << 735     G4double sum=0.0;
810     G4double sum = 0.0;                        << 736     for(size_t j=0; j<ncoef; j++) sum+=coefs[j]*std::exp(scales[j]*r[i]/au);
811     for (size_t j = 0; j < ncoef; j++)         << 737     phi[i]=sum;
812       sum += coefs[j] * std::exp(scales[j] * r << 738   }
813     phi[i] = sum;                              << 739 
814   }                                            << 740   // compute the derivative at the origin for the spline
815                                                << 741   G4double phiprime0=0.0;
816   // compute the derivative at the origin for  << 742   for(size_t j=0; j<ncoef; j++) phiprime0+=scales[j]*coefs[j]*std::exp(scales[j]*r[0]/au);
817   G4double phiprime0 = 0.0;                    << 743   phiprime0*=(1.0/au); // put back in natural units;
818   for (size_t j = 0; j < ncoef; j++)           << 744   
819     phiprime0 += scales[j] * coefs[j] * std::e << 745   *auval=au;
820   phiprime0 *= (1.0 / au);  // put back in nat << 746   return c2.lin_log_interpolating_function().load(r, phi, false, phiprime0,true,0);
821                                                << 747 }
822   *auval = au;                                 << 748 
823   return c2.lin_log_interpolating_function().l << 749 G4_c2_function &MoliereScreening(G4int z1, G4int z2, size_t npoints, G4double rMax, G4double *auval)
824 }                                              << 750 { 
825                                                << 751   static const size_t ncoef=3;
826 G4_c2_function& MoliereScreening(G4int z1, G4i << 752   static G4double scales[ncoef]={-6.0, -1.2, -0.3};
827 {                                              << 753   static G4double coefs[ncoef]={0.10, 0.55, 0.35};
828   static const size_t ncoef = 3;               << 754   
829   static G4double scales[ncoef] = {-6.0, -1.2, << 755   G4double au=0.8853*0.529*angstrom/std::sqrt(std::pow(z1, 0.6667)+std::pow(z2,0.6667));
830   static G4double coefs[ncoef] = {0.10, 0.55,  << 756   std::vector<G4double> r(npoints), phi(npoints);
831                                                << 757   
832   G4double au = 0.8853 * 0.529 * angstrom / st << 758   for(size_t i=0; i<npoints; i++) {
833   std::vector<G4double> r(npoints), phi(npoint << 759     G4double rr=(float)i/(float)(npoints-1);
834                                                << 760     r[i]=rr*rr*rMax; // use quadratic r scale to make sampling fine near the center
835   for (size_t i = 0; i < npoints; i++) {       << 761     G4double sum=0.0;
836     G4double rr = (float)i / (float)(npoints - << 762     for(size_t j=0; j<ncoef; j++) sum+=coefs[j]*std::exp(scales[j]*r[i]/au);
837     r[i] = rr * rr * rMax;                     << 763     phi[i]=sum;
838     // use quadratic r scale to make sampling  << 764   }
839     G4double sum = 0.0;                        << 765   
840     for (size_t j = 0; j < ncoef; j++)         << 766   // compute the derivative at the origin for the spline
841       sum += coefs[j] * std::exp(scales[j] * r << 767   G4double phiprime0=0.0;
842     phi[i] = sum;                              << 768   for(size_t j=0; j<ncoef; j++) phiprime0+=scales[j]*coefs[j]*std::exp(scales[j]*r[0]/au);
843   }                                            << 769   phiprime0*=(1.0/au); // put back in natural units;
844                                                << 770   
845   // compute the derivative at the origin for  << 771   *auval=au;
846   G4double phiprime0 = 0.0;                    << 772   return c2.lin_log_interpolating_function().load(r, phi, false, phiprime0,true,0);
847   for (size_t j = 0; j < ncoef; j++)           << 773 }
848     phiprime0 += scales[j] * coefs[j] * std::e << 774 
849   phiprime0 *= (1.0 / au);  // put back in nat << 775 G4_c2_function &LJScreening(G4int z1, G4int z2, size_t npoints, G4double rMax, G4double *auval)
850                                                << 776 { 
851   *auval = au;                                 << 777 //from Loftager, Besenbacher, Jensen & Sorensen
852   return c2.lin_log_interpolating_function().l << 778 //PhysRev A20, 1443++, 1979
853 }                                              << 779   G4double au=0.8853*0.529*angstrom/std::sqrt(std::pow(z1, 0.6667)+std::pow(z2,0.6667));
854                                                << 780   std::vector<G4double> r(npoints), phi(npoints);
855 G4_c2_function& LJScreening(G4int z1, G4int z2 << 781   
856 {                                              << 782   for(size_t i=0; i<npoints; i++) {
857   // from Loftager, Besenbacher, Jensen & Sore << 783     G4double rr=(float)i/(float)(npoints-1);
858   // PhysRev A20, 1443++, 1979                 << 784     r[i]=rr*rr*rMax; // use quadratic r scale to make sampling fine near the center
859   G4double au = 0.8853 * 0.529 * angstrom / st << 785 
860   std::vector<G4double> r(npoints), phi(npoint << 786     G4double y=std::sqrt(9.67*r[i]/au);
861                                                << 787     G4double ysq=y*y;
862   for (size_t i = 0; i < npoints; i++) {       << 788     G4double phipoly=1+y+0.3344*ysq+0.0485*y*ysq+0.002647*ysq*ysq;
863     G4double rr = (float)i / (float)(npoints - << 789     phi[i]=phipoly*std::exp(-y);
864     r[i] = rr * rr * rMax;                     << 790     // G4cout << r[i] << " " << phi[i] << G4endl;
865     // use quadratic r scale to make sampling  << 791   }
866                                                << 792 
867     G4double y = std::sqrt(9.67 * r[i] / au);  << 793   // compute the derivative at the origin for the spline
868     G4double ysq = y * y;                      << 794   G4double logphiprime0=(9.67/2.0)*(2*0.3344-1.0); // #avoid 0/0 on first element
869     G4double phipoly = 1 + y + 0.3344 * ysq +  << 795   logphiprime0 *= (1.0/au); // #put back in natural units
870     phi[i] = phipoly * std::exp(-y);           << 796   
871     // G4cout << r[i] << " " << phi[i] << G4en << 797   *auval=au;
872   }                                            << 798   return c2.lin_log_interpolating_function().load(r, phi, false, logphiprime0*phi[0],true,0);
873                                                << 799 }
874   // compute the derivative at the origin for  << 800 
875   G4double logphiprime0 = (9.67 / 2.0) * (2 *  << 801 G4_c2_function &LJZBLScreening(G4int z1, G4int z2, size_t npoints, G4double rMax, G4double *auval)
876   // #avoid 0/0 on first element               << 802 { 
877   logphiprime0 *= (1.0 / au);  // #put back in << 803 // hybrid of LJ and ZBL, uses LJ if x < 0.25*auniv, ZBL if x > 1.5*auniv, and 
878                                                << 804 /// connector in between.  These numbers are selected so the switchover
879   *auval = au;                                 << 805 // is very near the point where the functions naturally cross.
880   return c2.lin_log_interpolating_function().l << 806   G4double auzbl, aulj;
881 }                                              << 807   
882                                                << 808   c2p zbl=ZBLScreening(z1, z2, npoints, rMax, &auzbl);
883 G4_c2_function& LJZBLScreening(G4int z1, G4int << 809   c2p lj=LJScreening(z1, z2, npoints, rMax, &aulj);
884 {                                              << 810 
885   // hybrid of LJ and ZBL, uses LJ if x < 0.25 << 811   G4double au=(auzbl+aulj)*0.5;
886   /// connector in between.  These numbers are << 812   lj->set_domain(lj->xmin(), 0.25*au);
887   // is very near the point where the function << 813   zbl->set_domain(1.5*au,zbl->xmax());
888   G4double auzbl, aulj;                        << 814   
889                                                << 815   c2p conn=c2.connector_function(lj->xmax(), lj, zbl->xmin(), zbl, true,0);
890   c2p zbl = ZBLScreening(z1, z2, npoints, rMax << 816   c2_piecewise_function_p<G4double> &pw=c2.piecewise_function();
891   c2p lj = LJScreening(z1, z2, npoints, rMax,  << 817   c2p keepit(pw);
892                                                << 818   pw.append_function(lj);
893   G4double au = (auzbl + aulj) * 0.5;          << 819   pw.append_function(conn);
894   lj->set_domain(lj->xmin(), 0.25 * au);       << 820   pw.append_function(zbl);
895   zbl->set_domain(1.5 * au, zbl->xmax());      << 821   
896                                                << 822   *auval=au;
897   c2p conn = c2.connector_function(lj->xmax(), << 823   keepit.release_for_return();
898   c2_piecewise_function_p<G4double>& pw = c2.p << 824   return pw;
899   c2p keepit(pw);                              << 825 }
900   pw.append_function(lj);                      << 826 
901   pw.append_function(conn);                    << 827 G4NativeScreenedCoulombCrossSection::~G4NativeScreenedCoulombCrossSection() {
902   pw.append_function(zbl);                     << 828 }
903                                                << 829 
904   *auval = au;                                 << 830 G4NativeScreenedCoulombCrossSection::G4NativeScreenedCoulombCrossSection() {
905   keepit.release_for_return();                 << 831   AddScreeningFunction("zbl", ZBLScreening);
906   return pw;                                   << 832   AddScreeningFunction("lj", LJScreening);  
907 }                                              << 833   AddScreeningFunction("mol", MoliereScreening);  
908                                                << 834   AddScreeningFunction("ljzbl", LJZBLScreening);  
909 G4NativeScreenedCoulombCrossSection::~G4Native << 835 }
910                                                << 836 
911 G4NativeScreenedCoulombCrossSection::G4NativeS << 837 std::vector<G4String> G4NativeScreenedCoulombCrossSection::GetScreeningKeys() const {
912 {                                              << 838   std::vector<G4String> keys;
913   AddScreeningFunction("zbl", ZBLScreening);   << 839   // find the available screening keys
914   AddScreeningFunction("lj", LJScreening);     << 840   std::map<std::string, ScreeningFunc>::const_iterator sfunciter=phiMap.begin();
915   AddScreeningFunction("mol", MoliereScreening << 841   for(; sfunciter != phiMap.end(); sfunciter++) keys.push_back((*sfunciter).first);
916   AddScreeningFunction("ljzbl", LJZBLScreening << 842   return keys;
917 }                                              << 843 }
918                                                << 844 
919 std::vector<G4String> G4NativeScreenedCoulombC << 845 static inline G4double cm_energy(G4double a1, G4double a2, G4double t0) {
920 {                                              << 846   // "relativistically correct energy in CM frame"
921   std::vector<G4String> keys;                  << 847   G4double m1=a1*amu_c2, m2=a2*amu_c2;
922   // find the available screening keys         << 848   G4double mc2=(m1+m2);
923   std::map<std::string, ScreeningFunc>::const_ << 849   G4double f=2.0*m2*t0/(mc2*mc2);
924   for (; sfunciter != phiMap.end(); sfunciter+ << 850   // old way: return (f < 1e-6) ?  0.5*mc2*f : mc2*(std::sqrt(1.0+f)-1.0);
925     keys.push_back((*sfunciter).first);        << 851   // formally equivalent to previous, but numerically stable for all f without conditional
926   return keys;                                 << 852   // uses identity (sqrt(1+x) - 1)(sqrt(1+x) + 1) = x
927 }                                              << 853   return mc2*f/(std::sqrt(1.0+f)+1.0); 
928                                                << 854 }
929 static inline G4double cm_energy(G4double a1,  << 855 
930 {                                              << 856 static inline G4double  thetac(G4double m1, G4double m2, G4double eratio) {
931   // "relativistically correct energy in CM fr << 857   G4double s2th2=eratio*( (m1+m2)*(m1+m2)/(4.0*m1*m2) );
932   G4double m1 = a1 * amu_c2, mass2 = a2 * amu_ << 858   G4double sth2=std::sqrt(s2th2);
933   G4double mc2 = (m1 + mass2);                 << 859   return 2.0*std::asin(sth2);
934   G4double f = 2.0 * mass2 * t0 / (mc2 * mc2); << 860 }
935   // old way: return (f < 1e-6) ?  0.5*mc2*f : << 861 
936   // formally equivalent to previous, but nume << 862 void G4NativeScreenedCoulombCrossSection::LoadData(G4String screeningKey, G4int z1, G4double a1, G4double recoilCutoff)
937   // f without conditional                     << 863 {     
938   // uses identity (sqrt(1+x) - 1)(sqrt(1+x) + << 864   static const size_t sigLen=200; // since sigma doesn't matter much, a very coarse table will do     
939   return mc2 * f / (std::sqrt(1.0 + f) + 1.0); << 865   G4DataVector energies(sigLen);
940 }                                              << 866   G4DataVector data(sigLen);
941                                                << 867   
942 static inline G4double thetac(G4double m1, G4d << 868   a1=standardmass(z1); // use standardized values for mass for building tables
943 {                                              << 869   
944   G4double s2th2 = eratio * ((m1 + mass2) * (m << 870   const G4MaterialTable* materialTable = G4Material::GetMaterialTable();
945   G4double sth2 = std::sqrt(s2th2);            << 871   if (materialTable == 0)
946   return 2.0 * std::asin(sth2);                << 872     G4Exception("mhmNativeCrossSection::LoadData - no MaterialTable found)");
947 }                                              << 873   
948                                                << 874   G4int nMaterials = G4Material::GetNumberOfMaterials();
949 void G4NativeScreenedCoulombCrossSection::Load << 875   
950                                                << 876   for (G4int m=0; m<nMaterials; m++)
951 {                                              << 877     {
952   static const size_t sigLen = 200;            << 878     const G4Material* material= (*materialTable)[m];
953   // since sigma doesn't matter much, a very c << 879     const G4ElementVector* elementVector = material->GetElementVector();
954   G4DataVector energies(sigLen);               << 880     const G4int nMatElements = material->GetNumberOfElements();
955   G4DataVector data(sigLen);                   << 881     
956                                                << 882     for (G4int iEl=0; iEl<nMatElements; iEl++)
957   a1 = standardmass(z1);                       << 883     {
958   // use standardized values for mass for buil << 884       G4Element* element = (*elementVector)[iEl];
959                                                << 885       G4int Z = (G4int) element->GetZ();
960   const G4MaterialTable* materialTable = G4Mat << 886       G4double a2=element->GetA()*(mole/gram);
961   G4int nMaterials = G4Material::GetNumberOfMa << 887       
962                                                << 888       if(sigmaMap.find(Z)!=sigmaMap.end()) continue; // we've already got this element
963   for (G4int im = 0; im < nMaterials; im++) {  << 889       
964     const G4Material* material = (*materialTab << 890       // find the screening function generator we need
965     const G4ElementVector* elementVector = mat << 891       std::map<std::string, ScreeningFunc>::iterator sfunciter=phiMap.find(screeningKey);
966     const G4int nMatElements = material->GetNu << 892       if(sfunciter==phiMap.end()) {
967                                                << 893         G4cout << "no such screening key " << screeningKey << G4endl; // FIXME later
968     for (G4int iEl = 0; iEl < nMatElements; iE << 894         exit(1);
969       const G4Element* element = (*elementVect << 895       }
970       G4int Z = element->GetZasInt();          << 896       ScreeningFunc sfunc=(*sfunciter).second;
971       G4double a2 = element->GetA() * (mole /  << 897       
972                                                << 898       G4double au; 
973       if (sigmaMap.find(Z) != sigmaMap.end())  << 899       G4_c2_ptr screen=sfunc(z1, Z, 200, 50.0*angstrom, &au); // generate the screening data
974       // we've already got this element        << 900       G4ScreeningTables st;
975                                                << 901   
976       // find the screening function generator << 902       st.EMphiData=screen; //save our phi table
977       std::map<std::string, ScreeningFunc>::it << 903       st.z1=z1; st.m1=a1; st.z2=Z; st.m2=a2; st.emin=recoilCutoff;
978       if (sfunciter == phiMap.end()) {         << 904       st.au=au;
979         G4ExceptionDescription ed;             << 905 
980         ed << "No such screening key <" << scr << 906       // now comes the hard part... build the total cross section tables from the phi table     
981         G4Exception("G4NativeScreenedCoulombCr << 907       //based on (pi-thetac) = pi*beta*alpha/x0, but noting that alpha is very nearly unity, always
982       }                                        << 908       //so just solve it wth alpha=1, which makes the solution much easier
983       ScreeningFunc sfunc = (*sfunciter).secon << 909       //this function returns an approximation to (beta/x0)^2=phi(x0)/(eps*x0)-1 ~ ((pi-thetac)/pi)^2
984                                                << 910       //Since we don't need exact sigma values, this is good enough (within a factor of 2 almost always)
985       G4double au;                             << 911       //this rearranges to phi(x0)/(x0*eps) = 2*theta/pi - theta^2/pi^2
986       G4_c2_ptr screen = sfunc(z1, Z, 200, 50. << 912       
987       // generate the screening data           << 913       c2_linear_p<G4double> &c2eps=c2.linear(0.0, 0.0, 0.0); // will store an appropriate eps inside this in loop
988       G4ScreeningTables st;                    << 914       G4_c2_ptr phiau=screen(c2.linear(0.0, 0.0, au));
989                                                << 915       G4_c2_ptr x0func(phiau/c2eps); // this will be phi(x)/(x*eps) when c2eps is correctly set
990       st.EMphiData = screen;  // save our phi  << 916       x0func->set_domain(1e-6*angstrom/au, 0.9999*screen->xmax()/au); // needed for inverse function
991       st.z1 = z1;                              << 917       // use the c2_inverse_function interface for the root finder... it is more efficient for an ordered 
992       st.m1 = a1;                              << 918       // computation of values.
993       st.z2 = Z;                               << 919       G4_c2_ptr x0_solution(c2.inverse_function(x0func));
994       st.m2 = a2;                              << 920       
995       st.emin = recoilCutoff;                  << 921       G4double m1c2=a1*amu_c2;
996       st.au = au;                              << 922       G4double escale=z1*Z*elm_coupling/au; // energy at screening distance
997                                                << 923       G4double emax=m1c2; // model is doubtful in very relativistic range
998       // now comes the hard part... build the  << 924       G4double eratkin=0.9999*(4*a1*a2)/((a1+a2)*(a1+a2)); // #maximum kinematic ratio possible at 180 degrees
999       // tables from the phi table             << 925       G4double cmfact0=st.emin/cm_energy(a1, a2, st.emin);
1000       // based on (pi-thetac) = pi*beta*alpha << 926       G4double l1=std::log(emax);
1001       // alpha is very nearly unity, always   << 927       G4double l0=std::log(st.emin*cmfact0/eratkin);
1002       // so just solve it wth alpha=1, which  << 928 
1003       // much easier                          << 929       if(verbosity >=1) 
1004       // this function returns an approximati << 930         G4cout << "Native Screening: " << screeningKey << " " << z1 << " " << a1 << " " << 
1005       // (beta/x0)^2=phi(x0)/(eps*x0)-1 ~ ((p << 931           Z << " " << a2 << " " << recoilCutoff << G4endl;
1006       // Since we don't need exact sigma valu << 932       
1007       // (within a factor of 2 almost always) << 933       for(size_t idx=0; idx<sigLen; idx++) {
1008       // this rearranges to phi(x0)/(x0*eps)  << 934         G4double ee=std::exp(idx*((l1-l0)/sigLen)+l0);
1009       // 2*theta/pi - theta^2/pi^2            << 935         G4double gamma=1.0+ee/m1c2;
1010                                               << 936         G4double eratio=(cmfact0*st.emin)/ee; // factor by which ee needs to be reduced to get emin
1011       c2_linear_p<G4double>& c2eps = c2.linea << 937         G4double theta=thetac(gamma*a1, a2, eratio);
1012       // will store an appropriate eps inside << 938       
1013       G4_c2_ptr phiau = screen(c2.linear(0.0, << 939         G4double eps=cm_energy(a1, a2, ee)/escale; // #make sure lab energy is converted to CM for these calculations
1014       G4_c2_ptr x0func(phiau / c2eps);        << 940         c2eps.reset(0.0, 0.0, eps); // set correct slope in this function
1015       // this will be phi(x)/(x*eps) when c2e << 941         
1016       x0func->set_domain(1e-6 * angstrom / au << 942         G4double q=theta/pi;
1017       // needed for inverse function          << 943         // G4cout << ee << " " << m1c2 << " " << gamma << " " << eps << " " << theta << " " << q << G4endl;
1018       // use the c2_inverse_function interfac << 944         // old way using root finder
1019       // it is more efficient for an ordered  << 945         // G4double x0= x0func->find_root(1e-6*angstrom/au, 0.9999*screen.xmax()/au, 1.0, 2*q-q*q);
1020       // computation of values.               << 946         // new way using c2_inverse_function which caches useful information so should be a bit faster
1021       G4_c2_ptr x0_solution(c2.inverse_functi << 947         // since we are scanning this in strict order.
1022                                               << 948         G4double x0=0;
1023       G4double m1c2 = a1 * amu_c2;            << 949         try {
1024       G4double escale = z1 * Z * elm_coupling << 950           x0=x0_solution(2*q-q*q);
1025       // energy at screening distance         << 951         } catch(c2_exception e) {
1026       G4double emax = m1c2;                   << 952           G4Exception(
1027       // model is doubtful in very relativist << 953             G4String("G4ScreenedNuclearRecoil: failure in inverse solution to generate MFP Tables: ")+e.what()
1028       G4double eratkin = 0.9999 * (4 * a1 * a << 954           );
1029       // #maximum kinematic ratio possible at << 955         }
1030       G4double cmfact0 = st.emin / cm_energy( << 956         G4double betasquared=x0*x0 - x0*phiau(x0)/eps;  
1031       G4double l1 = std::log(emax);           << 957         G4double sigma=pi*betasquared*au*au;
1032       G4double l0 = std::log(st.emin * cmfact << 958         energies[idx]=ee;
1033                                               << 959         data[idx]=sigma;
1034       if (verbosity >= 1)                     << 960       }
1035         G4cout << "Native Screening: " << scr << 961       screeningData[Z]=st;
1036                << a2 << " " << recoilCutoff < << 962       sigmaMap[Z] = c2.log_log_interpolating_function().load(energies, data, true,0,true,0);
1037                                               << 963     }
1038       for (size_t idx = 0; idx < sigLen; idx+ << 964   }
1039         G4double ee = std::exp(idx * ((l1 - l << 
1040         G4double gamma = 1.0 + ee / m1c2;     << 
1041         G4double eratio = (cmfact0 * st.emin) << 
1042         // factor by which ee needs to be red << 
1043         G4double theta = thetac(gamma * a1, a << 
1044                                               << 
1045         G4double eps = cm_energy(a1, a2, ee)  << 
1046         // #make sure lab energy is converted << 
1047         // calculations                       << 
1048         c2eps.reset(0.0, 0.0, eps);           << 
1049         // set correct slope in this function << 
1050                                               << 
1051         G4double q = theta / pi;              << 
1052         // G4cout << ee << " " << m1c2 << " " << 
1053         // << eps << " " << theta << " " << q << 
1054         // old way using root finder          << 
1055         // G4double x0= x0func->find_root(1e- << 
1056         // 0.9999*screen.xmax()/au, 1.0, 2*q- << 
1057         // new way using c2_inverse_function  << 
1058         // useful information so should be a  << 
1059         // since we are scanning this in stri << 
1060         G4double x0 = 0;                      << 
1061         try {                                 << 
1062           x0 = x0_solution(2 * q - q * q);    << 
1063         }                                     << 
1064         catch (c2_exception&) {               << 
1065           G4Exception("G4ScreenedNuclearRecoi << 
1066                       "failure in inverse sol << 
1067         }                                     << 
1068         G4double betasquared = x0 * x0 - x0 * << 
1069         G4double sigma = pi * betasquared * a << 
1070         energies[idx] = ee;                   << 
1071         data[idx] = sigma;                    << 
1072       }                                       << 
1073       screeningData[Z] = st;                  << 
1074       sigmaMap[Z] = c2.log_log_interpolating_ << 
1075     }                                         << 
1076   }                                           << 
1077 }                                                965 }
1078                                                  966