55 :
G4VEmModel(nam),fParticleChange(0),isInitialised(false),logAtomicCrossSection(0),
56 atomicFormFactor(0),logFormFactorTable(0),pMaxTable(0),samplingTable(0)
58 fIntrinsicLowEnergyLimit = 100.0*eV;
59 fIntrinsicHighEnergyLimit = 100.0*GeV;
72 G4double logenergy = std::log(fIntrinsicLowEnergyLimit/2.);
73 G4double logmaxenergy = std::log(1.5*fIntrinsicHighEnergyLimit);
75 G4double logtransitionenergy = std::log(160*keV);
76 G4double logfactor1 = std::log(10.)/250.;
78 logEnergyGridPMax.push_back(logenergy);
80 if (logenergy < logtransitionenergy)
81 logenergy += logfactor1;
83 logenergy += logfactor2;
84 logEnergyGridPMax.push_back(logenergy);
85 }
while (logenergy < logmaxenergy);
92 std::map <G4int,G4PhysicsFreeVector*>::iterator i;
93 if (logAtomicCrossSection)
95 for (i=logAtomicCrossSection->begin();i != logAtomicCrossSection->end();i++)
96 if (i->second)
delete i->second;
97 delete logAtomicCrossSection;
100 if (atomicFormFactor)
102 for (i=atomicFormFactor->begin();i != atomicFormFactor->end();i++)
103 if (i->second)
delete i->second;
104 delete atomicFormFactor;
111void G4PenelopeRayleighModel::ClearTables()
113 std::map <const G4Material*,G4PhysicsFreeVector*>::iterator i;
115 if (logFormFactorTable)
117 for (i=logFormFactorTable->begin(); i != logFormFactorTable->end(); i++)
118 if (i->second)
delete i->second;
119 delete logFormFactorTable;
120 logFormFactorTable = 0;
125 for (i=pMaxTable->begin(); i != pMaxTable->end(); i++)
126 if (i->second)
delete i->second;
131 std::map<const G4Material*,G4PenelopeSamplingData*>::iterator ii;
134 for (ii=samplingTable->begin(); ii != samplingTable->end(); ii++)
135 if (ii->second)
delete ii->second;
136 delete samplingTable;
148 if (verboseLevel > 3)
149 G4cout <<
"Calling G4PenelopeRayleighModel::Initialise()" <<
G4endl;
158 if (!logAtomicCrossSection)
159 logAtomicCrossSection =
new std::map<G4int,G4PhysicsFreeVector*>;
160 if (!atomicFormFactor)
161 atomicFormFactor =
new std::map<G4int,G4PhysicsFreeVector*>;
163 if (!logFormFactorTable)
164 logFormFactorTable =
new std::map<const G4Material*,G4PhysicsFreeVector*>;
166 pMaxTable =
new std::map<const G4Material*,G4PhysicsFreeVector*>;
168 samplingTable =
new std::map<const G4Material*,G4PenelopeSamplingData*>;
171 if (verboseLevel > 0) {
172 G4cout <<
"Penelope Rayleigh model v2008 is initialized " <<
G4endl
179 if(isInitialised)
return;
181 isInitialised =
true;
197 if (verboseLevel > 3)
198 G4cout <<
"Calling CrossSectionPerAtom() of G4PenelopeRayleighModel" <<
G4endl;
203 if (!logAtomicCrossSection->count(iZ))
206 if (!logAtomicCrossSection->count(iZ))
208 G4Exception(
"G4PenelopeRayleighModel::ComputeCrossSectionPerAtom()",
209 "em2040",
FatalException,
"Unable to load the cross section table");
218 ed <<
"Unable to find Z=" << iZ <<
" in the atomic cross section table" <<
G4endl;
219 G4Exception(
"G4PenelopeRayleighModel::ComputeCrossSectionPerAtom()",
225 cross = std::exp(logXS);
227 if (verboseLevel > 2)
228 G4cout <<
"Rayleigh cross section at " << energy/keV <<
" keV for Z=" << Z <<
229 " = " << cross/barn <<
" barn" <<
G4endl;
235void G4PenelopeRayleighModel::BuildFormFactorTable(
const G4Material* material)
246 std::vector<G4double> *StechiometricFactors =
new std::vector<G4double>;
247 for (
G4int i=0;i<nElements;i++)
249 G4double fraction = fractionVector[i];
250 G4double atomicWeigth = (*elementVector)[i]->GetA()/(g/mole);
251 StechiometricFactors->push_back(fraction/atomicWeigth);
254 G4double MaxStechiometricFactor = 0.;
255 for (
G4int i=0;i<nElements;i++)
257 if ((*StechiometricFactors)[i] > MaxStechiometricFactor)
258 MaxStechiometricFactor = (*StechiometricFactors)[i];
260 if (MaxStechiometricFactor<1e-16)
263 ed <<
"Inconsistent data of atomic composition for " <<
265 G4Exception(
"G4PenelopeRayleighModel::BuildFormFactorTable()",
269 for (
G4int i=0;i<nElements;i++)
270 (*StechiometricFactors)[i] /= MaxStechiometricFactor;
274 for (
G4int i=0;i<nElements;i++)
275 atomsPerMolecule += (*StechiometricFactors)[i];
283 for (
size_t k=0;k<logQSquareGrid.size();k++)
286 for (
G4int i=0;i<nElements;i++)
288 G4int iZ = (
G4int) (*elementVector)[i]->GetZ();
291 ff2 += f*f*(*StechiometricFactors)[i];
294 theFFVec->
PutValue(k,logQSquareGrid[k],std::log(ff2));
296 logFormFactorTable->insert(std::make_pair(material,theFFVec));
298 delete StechiometricFactors;
325 if (verboseLevel > 3)
326 G4cout <<
"Calling SamplingSecondaries() of G4PenelopeRayleighModel" <<
G4endl;
330 if (photonEnergy0 <= fIntrinsicLowEnergyLimit)
343 if (!pMaxTable || !samplingTable)
345 G4Exception(
"G4PenelopeRayleighModel::SampleSecondaries()",
351 if (!(samplingTable->count(theMat)))
352 InitializeSamplingAlgorithm(theMat);
356 if (!pMaxTable->count(theMat))
357 GetPMaxTable(theMat);
363 G4double qmax = 2.0*photonEnergy0/electron_mass_c2;
372 G4double G = 0.5*(1+cosTheta*cosTheta);
380 G4double LastQ2inTheTable = theDataTable->
GetX(nData-1);
381 G4double q2max = std::min(qmax*qmax,LastQ2inTheTable);
398 cosTheta = 1.0-2.0*xx/q2max;
399 G4double G = 0.5*(1+cosTheta*cosTheta);
405 G4double sinTheta = std::sqrt(1-cosTheta*cosTheta);
409 G4double dirX = sinTheta*std::cos(phi);
410 G4double dirY = sinTheta*std::sin(phi);
416 photonDirection1.
rotateUz(photonDirection0);
426void G4PenelopeRayleighModel::ReadDataFile(
const G4int Z)
428 if (verboseLevel > 2)
430 G4cout <<
"G4PenelopeRayleighModel::ReadDataFile()" <<
G4endl;
431 G4cout <<
"Going to read Rayleigh data files for Z=" << Z <<
G4endl;
434 char* path = getenv(
"G4LEDATA");
437 G4String excep =
"G4LEDATA environment variable not set!";
438 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
446 std::ostringstream ost;
448 ost << path <<
"/penelope/rayleigh/pdgra" << Z <<
".p08";
450 ost << path <<
"/penelope/rayleigh/pdgra0" << Z <<
".p08";
451 std::ifstream file(ost.str().c_str());
455 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
460 file >> readZ >> nPoints;
462 if (readZ != Z || nPoints <= 0 || nPoints >= 5000)
465 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
466 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
472 for (
size_t i=0;i<nPoints;i++)
474 file >> ene >> f1 >> f2 >> xs;
478 theVec->
PutValue(i,std::log(ene),std::log(xs));
479 if (file.eof() && i != (nPoints-1))
482 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
483 ed <<
"Found less than " << nPoints <<
"entries " <<
G4endl;
484 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
488 if (!logAtomicCrossSection)
490 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
491 "em2044",
FatalException,
"Unable to allocate the atomic cross section table");
495 logAtomicCrossSection->insert(std::make_pair(Z,theVec));
501 std::ostringstream ost2;
503 ost2 << path <<
"/penelope/rayleigh/pdaff" << Z <<
".p08";
505 ost2 << path <<
"/penelope/rayleigh/pdaff0" << Z <<
".p08";
506 file.open(ost2.str().c_str());
510 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
513 file >> readZ >> nPoints;
515 if (readZ != Z || nPoints <= 0 || nPoints >= 5000)
518 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
519 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
527 if (!logQSquareGrid.size())
529 for (
size_t i=0;i<nPoints;i++)
531 file >> q >> ff >> incoh;
536 logQSquareGrid.push_back(2.0*std::log(q));
538 if (file.eof() && i != (nPoints-1))
541 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
542 ed <<
"Found less than " << nPoints <<
"entries " <<
G4endl;
543 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
547 if (!atomicFormFactor)
549 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
551 "Unable to allocate the atomicFormFactor data table");
555 atomicFormFactor->insert(std::make_pair(Z,theFFVec));
568 G4double logQSquared = (QSquared>1e-10) ? std::log(QSquared) : -23.;
570 G4double maxlogQ2 = logQSquareGrid[logQSquareGrid.size()-1];
572 if (!logFormFactorTable->count(mat))
573 BuildFormFactorTable(mat);
581 ed <<
"Unable to retrieve F squared table for " << mat->
GetName() <<
G4endl;
582 G4Exception(
"G4PenelopeRayleighModel::GetFSquared()",
586 if (logQSquared < -20)
589 f2 = std::exp(logf2);
591 else if (logQSquared > maxlogQ2)
597 f2 = std::exp(logf2);
600 if (verboseLevel > 3)
603 G4cout <<
"Q^2 = " << QSquared <<
" (units of 1/(m_e*c); F^2 = " << f2 <<
G4endl;
610void G4PenelopeRayleighModel::InitializeSamplingAlgorithm(
const G4Material* mat)
614 const size_t np = 150;
615 for (
size_t i=1;i<logQSquareGrid.size();i++)
617 G4double Q2 = std::exp(logQSquareGrid[i]);
618 if (GetFSquared(mat,Q2) > 1e-35)
620 q2max = std::exp(logQSquareGrid[i-1]);
624 size_t nReducedPoints = np/4;
629 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
631 "Too few points to initialize the sampling algorithm");
633 if (q2min > (q2max-1e-10))
635 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
637 "Too narrow grid to initialize the sampling algorithm");
649 size_t NUNIF = std::min(std::max(((
size_t)8),nReducedPoints),np/2);
650 const G4int nip = 51;
654 for (
size_t i=1;i<NUNIF-1;i++)
662 G4cout <<
"Vector x has " << x->size() <<
" points, while NUNIF = " << NUNIF <<
G4endl;
671 for (
size_t i=0;i<NUNIF-1;i++)
680 for (
G4int k=0;k<nip;k++)
683 G4double pdfk = std::max(GetFSquared(mat,xik),0.);
684 pdfi->push_back(pdfk);
685 pdfmax = std::max(pdfmax,pdfk);
689 G4double pdfIK = std::max(GetFSquared(mat,xih),0.);
690 pdfih->push_back(pdfIK);
691 pdfmax = std::max(pdfmax,pdfIK);
698 for (
G4int k=1;k<nip;k++)
701 G4double next = previous + cons*((*pdfi)[k-1]+4.0*(*pdfih)[k-1]+(*pdfi)[k]);
702 sumi->push_back(next);
705 G4double lastIntegral = (*sumi)[sumi->size()-1];
706 area->push_back(lastIntegral);
709 for (
size_t k=0;k<sumi->size();k++)
710 (*sumi)[k] *= factor;
713 if ((*pdfi)[0] < 1e-35)
714 (*pdfi)[0] = 1e-5*pdfmax;
715 if ((*pdfi)[pdfi->size()-1] < 1e-35)
716 (*pdfi)[pdfi->size()-1] = 1e-5*pdfmax;
719 G4double pui = (*pdfi)[pdfi->size()-1]*factor;
720 G4double B_temp = 1.0-1.0/(pli*pui*dx*dx);
721 G4double A_temp = (1.0/(pli*dx))-1.0-B_temp;
722 G4double C_temp = 1.0+A_temp+B_temp;
731 a->push_back(A_temp);
732 b->push_back(B_temp);
733 c->push_back(C_temp);
745 for (
G4int k=0;k<nip;k++)
748 G4double pap = (*area)[i]*(1.0+((*a)[i]+(*b)[i]*rr)*rr)*(1.0+((*a)[i]+(*b)[i]*rr)*rr)/
749 ((1.0-(*b)[i]*rr*rr)*(*c)[i]*((*x)[i+1]-(*x)[i]));
750 if (k == 0 || k == nip-1)
751 (*err)[i] += 0.5*std::fabs(pap-(*pdfi)[k]);
753 (*err)[i] += std::fabs(pap-(*pdfi)[k]);
758 if ((*err)[i] > 0.1*(*area)[i] && icase == 1)
774 (*x)[x->size()-1] = q2max;
781 if (x->size() != NUNIF || a->size() != NUNIF ||
782 err->size() != NUNIF || area->size() != NUNIF)
785 ed <<
"Problem in building the Table for Sampling: array dimensions do not match" <<
G4endl;
786 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
798 for (
size_t i=0;i<err->size()-2;i++)
801 if ((*err)[i] > maxError)
803 maxError = (*err)[i];
809 G4double newx = 0.5*((*x)[iErrMax]+(*x)[iErrMax+1]);
811 x->insert(x->begin()+iErrMax+1,newx);
813 area->insert(area->begin()+iErrMax+1,0.);
814 a->insert(a->begin()+iErrMax+1,0.);
815 b->insert(b->begin()+iErrMax+1,0.);
816 c->insert(c->begin()+iErrMax+1,0.);
817 err->insert(err->begin()+iErrMax+1,0.);
820 for (
size_t i=iErrMax;i<=iErrMax+1;i++)
827 G4double dxLocal = (*x)[i+1]-(*x)[i];
830 for (
G4int k=0;k<nip;k++)
833 G4double pdfk = std::max(GetFSquared(mat,xik),0.);
834 pdfi->push_back(pdfk);
835 pdfmax = std::max(pdfmax,pdfk);
839 G4double pdfIK = std::max(GetFSquared(mat,xih),0.);
840 pdfih->push_back(pdfIK);
841 pdfmax = std::max(pdfmax,pdfIK);
848 for (
G4int k=1;k<nip;k++)
851 G4double next = previous + cons*((*pdfi)[k-1]+4.0*(*pdfih)[k-1]+(*pdfi)[k]);
852 sumi->push_back(next);
854 G4double lastIntegral = (*sumi)[sumi->size()-1];
855 (*area)[i] = lastIntegral;
859 for (
size_t k=0;k<sumi->size();k++)
860 (*sumi)[k] *= factor;
863 if ((*pdfi)[0] < 1e-35)
864 (*pdfi)[0] = 1e-5*pdfmax;
865 if ((*pdfi)[pdfi->size()-1] < 1e-35)
866 (*pdfi)[pdfi->size()-1] = 1e-5*pdfmax;
869 G4double pui = (*pdfi)[pdfi->size()-1]*factor;
870 G4double B_temp = 1.0-1.0/(pli*pui*dxLocal*dxLocal);
871 G4double A_temp = (1.0/(pli*dxLocal))-1.0-B_temp;
872 G4double C_temp = 1.0+A_temp+B_temp;
893 for (
G4int k=0;k<nip;k++)
896 G4double pap = (*area)[i]*(1.0+((*a)[i]+(*b)[i]*rr)*rr)*(1.0+((*a)[i]+(*b)[i]*rr)*rr)/
897 ((1.0-(*b)[i]*rr*rr)*(*c)[i]*((*x)[i+1]-(*x)[i]));
898 if (k == 0 || k == nip-1)
899 (*err)[i] += 0.5*std::fabs(pap-(*pdfi)[k]);
901 (*err)[i] += std::fabs(pap-(*pdfi)[k]);
906 if ((*err)[i] > 0.1*(*area)[i] && icase == 1)
919 }
while(x->size() < np);
921 if (x->size() != np || a->size() != np ||
922 err->size() != np || area->size() != np)
924 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
926 "Problem in building the extended Table for Sampling: array dimensions do not match ");
933 for (
size_t i=0;i<np-1;i++)
937 for (
size_t i=0;i<np-1;i++)
941 errMax = std::max(errMax,(*err)[i]);
947 for (
size_t i=0;i<np-1;i++)
950 PAC->push_back(previous+(*area)[i]);
952 (*PAC)[PAC->size()-1] = 1.;
959 std::vector<size_t> *ITTL =
new std::vector<size_t>;
960 std::vector<size_t> *ITTU =
new std::vector<size_t>;
963 for (
size_t i=0;i<np;i++)
971 for (
size_t i=1;i<(np-1);i++)
975 for (
size_t j=(*ITTL)[i-1];j<np && !found;j++)
977 if ((*PAC)[j] > ptst)
985 (*ITTU)[ITTU->size()-2] = ITTU->size()-1;
986 (*ITTU)[ITTU->size()-1] = ITTU->size()-1;
987 (*ITTL)[ITTL->size()-1] = ITTU->size()-2;
989 if (ITTU->size() != np || ITTU->size() != np)
991 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
993 "Problem in building the Limit Tables for Sampling: array dimensions do not match");
1001 for (
size_t i=0;i<np;i++)
1003 theTable->
AddPoint((*x)[i],(*PAC)[i],(*a)[i],(*b)[i],(*ITTL)[i],(*ITTU)[i]);
1006 if (verboseLevel > 2)
1008 G4cout <<
"*************************************************************************" <<
1010 G4cout <<
"Sampling table for Penelope Rayleigh scattering in " << mat->
GetName() <<
G4endl;
1013 samplingTable->insert(std::make_pair(mat,theTable));
1034void G4PenelopeRayleighModel::GetPMaxTable(
const G4Material* mat)
1038 G4cout <<
"G4PenelopeRayleighModel::BuildPMaxTable" <<
G4endl;
1039 G4cout <<
"Going to instanziate the pMaxTable !" <<
G4endl;
1041 pMaxTable =
new std::map<const G4Material*,G4PhysicsFreeVector*>;
1044 if (pMaxTable->count(mat))
1050 G4Exception(
"G4PenelopeRayleighModel::GetPMaxTable()",
1052 "SamplingTable is not properly instantiated");
1056 if (!samplingTable->count(mat))
1057 InitializeSamplingAlgorithm(mat);
1062 size_t nOfEnergyPoints = logEnergyGridPMax.size();
1065 const size_t nip = 51;
1067 for (
size_t ie=0;ie<logEnergyGridPMax.size();ie++)
1069 G4double energy = std::exp(logEnergyGridPMax[ie]);
1070 G4double Qm = 2.0*energy/electron_mass_c2;
1081 size_t lowerBound = 0;
1082 size_t upperBound = tablePoints-1;
1083 while (lowerBound <= upperBound)
1085 size_t midBin = (lowerBound + upperBound)/2;
1086 if( Qm2 < theTable->GetX(midBin))
1087 { upperBound = midBin-1; }
1089 { lowerBound = midBin+1; }
1099 for (
size_t k=0;k<nip;k++)
1103 (theTable->
GetX(upperBound+1)-Q1);
1108 if (std::fabs(con1) > 1.0e-16*std::fabs(con2))
1109 etap = con2*(1.0-std::sqrt(1.0-2.0*tau*con1/(con2*con2)))/con1;
1113 (1.0+(theA+theB*etap)*etap)*(1.0+(theA+theB*etap)*etap)/
1114 ((1.0-theB*etap*etap)*ci*(theTable->
GetX(upperBound+1)-Q1));
1115 fun->push_back(theFun);
1123 (5.0*(*fun)[0]+8.0*(*fun)[1]-(*fun)[2])*CONS;
1124 sum->push_back(secondPoint);
1125 for (
size_t hh=2;hh<nip-1;hh++)
1128 G4double next = previous+(13.0*((*fun)[hh-1]+(*fun)[hh])-
1129 (*fun)[hh+1]-(*fun)[hh-2])*HCONS;
1130 sum->push_back(next);
1132 G4double last = (*sum)[nip-2]+(5.0*(*fun)[nip-1]+8.0*(*fun)[nip-2]-
1133 (*fun)[nip-3])*CONS;
1134 sum->push_back(last);
1135 thePMax = thePAC + (*sum)[sum->size()-1];
1146 thePMax = theTable->
GetPAC(0);
1150 theVec->
PutValue(ie,energy,thePMax);
1153 pMaxTable->insert(std::make_pair(mat,theVec));
1162 G4cout <<
"*****************************************************************" <<
G4endl;
1166 G4cout <<
"*****************************************************************" <<
G4endl;
1167 if (!logFormFactorTable->count(mat))
1168 BuildFormFactorTable(mat);
std::vector< G4Element * > G4ElementVector
G4DLLIMPORT std::ostream G4cout
Hep3Vector & rotateUz(const Hep3Vector &)
const G4ThreeVector & GetMomentumDirection() const
G4double GetKineticEnergy() const
const G4Material * GetMaterial() const
const G4ElementVector * GetElementVector() const
const G4double * GetFractionVector() const
size_t GetNumberOfElements() const
const G4String & GetName() const
void SetProposedKineticEnergy(G4double proposedKinEnergy)
void ProposeMomentumDirection(G4double Px, G4double Py, G4double Pz)
virtual void Initialise(const G4ParticleDefinition *, const G4DataVector &)
virtual G4double ComputeCrossSectionPerAtom(const G4ParticleDefinition *, G4double kinEnergy, G4double Z, G4double A=0, G4double cut=0, G4double emax=DBL_MAX)
void DumpFormFactorTable(const G4Material *)
G4ParticleChangeForGamma * fParticleChange
virtual ~G4PenelopeRayleighModel()
G4PenelopeRayleighModel(const G4ParticleDefinition *p=0, const G4String &processName="PenRayleigh")
virtual void SampleSecondaries(std::vector< G4DynamicParticle * > *, const G4MaterialCutsCouple *, const G4DynamicParticle *, G4double tmin, G4double maxEnergy)
G4double GetA(size_t index)
G4double GetPAC(size_t index)
size_t GetNumberOfStoredPoints()
void AddPoint(G4double x0, G4double pac0, G4double a0, G4double b0, size_t ITTL0, size_t ITTU0)
G4double GetX(size_t index)
G4double SampleValue(G4double rndm)
G4double GetB(size_t index)
void PutValue(size_t binNumber, G4double binValue, G4double dataValue)
G4double Value(G4double theEnergy)
size_t GetVectorLength() const
virtual G4double GetLowEdgeEnergy(size_t binNumber) const
void SetHighEnergyLimit(G4double)
G4ParticleChangeForGamma * GetParticleChangeForGamma()
G4double LowEnergyLimit() const
G4double HighEnergyLimit() const
void ProposeTrackStatus(G4TrackStatus status)
void ProposeLocalEnergyDeposit(G4double anEnergyPart)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
std::ostringstream G4ExceptionDescription