56 :
G4VEmModel(nam),fParticleChange(0),fParticle(0),isInitialised(false),
57 logAtomicCrossSection(0),atomicFormFactor(0),logFormFactorTable(0),
58 pMaxTable(0),samplingTable(0),fLocalTable(false)
60 fIntrinsicLowEnergyLimit = 100.0*
eV;
61 fIntrinsicHighEnergyLimit = 100.0*
GeV;
78 G4double logenergy = std::log(fIntrinsicLowEnergyLimit/2.);
79 G4double logmaxenergy = std::log(1.5*fIntrinsicHighEnergyLimit);
82 G4double logfactor1 = std::log(10.)/250.;
84 logEnergyGridPMax.push_back(logenergy);
86 if (logenergy < logtransitionenergy)
87 logenergy += logfactor1;
89 logenergy += logfactor2;
90 logEnergyGridPMax.push_back(logenergy);
91 }
while (logenergy < logmaxenergy);
100 std::map <G4int,G4PhysicsFreeVector*>::iterator i;
101 if (logAtomicCrossSection)
107 delete logAtomicCrossSection;
112 std::map <G4int,G4PhysicsFreeVector*>::iterator i;
113 if (atomicFormFactor)
119 delete atomicFormFactor;
127 void G4PenelopeRayleighModel::ClearTables()
131 G4Exception(
"G4PenelopeRayleighModel::ClearTables()",
134 std::map <const G4Material*,G4PhysicsFreeVector*>::iterator i;
136 if (logFormFactorTable)
142 delete logFormFactorTable;
143 logFormFactorTable = 0;
156 std::map<const G4Material*,G4PenelopeSamplingData*>::iterator ii;
163 delete samplingTable;
175 if (verboseLevel > 3)
176 G4cout <<
"Calling G4PenelopeRayleighModel::Initialise()" <<
G4endl;
190 if (!logAtomicCrossSection)
191 logAtomicCrossSection =
new std::map<G4int,G4PhysicsFreeVector*>;
192 if (!atomicFormFactor)
193 atomicFormFactor =
new std::map<G4int,G4PhysicsFreeVector*>;
195 if (!logFormFactorTable)
196 logFormFactorTable =
new std::map<const G4Material*,G4PhysicsFreeVector*>;
198 pMaxTable =
new std::map<const G4Material*,G4PhysicsFreeVector*>;
200 samplingTable =
new std::map<const G4Material*,G4PenelopeSamplingData*>;
214 G4int iZ = (
G4int) theElementVector->at(j)->GetZ();
216 if (!logAtomicCrossSection->count(iZ))
221 if (!logFormFactorTable->count(material))
222 BuildFormFactorTable(material);
225 if (!(samplingTable->count(material)))
226 InitializeSamplingAlgorithm(material);
229 if (!pMaxTable->count(material))
230 GetPMaxTable(material);
234 if (verboseLevel > 1) {
235 G4cout <<
"Penelope Rayleigh model v2008 is initialized " << G4endl
243 if(isInitialised)
return;
245 isInitialised =
true;
253 if (verboseLevel > 3)
254 G4cout <<
"Calling G4PenelopeRayleighModel::InitialiseLocal()" <<
G4endl;
267 logAtomicCrossSection = theModel->logAtomicCrossSection;
268 atomicFormFactor = theModel->atomicFormFactor;
269 logFormFactorTable = theModel->logFormFactorTable;
270 pMaxTable = theModel->pMaxTable;
271 samplingTable = theModel->samplingTable;
274 logQSquareGrid = theModel->logQSquareGrid;
277 verboseLevel = theModel->verboseLevel;
297 if (verboseLevel > 3)
298 G4cout <<
"Calling CrossSectionPerAtom() of G4PenelopeRayleighModel" <<
G4endl;
304 if (!logAtomicCrossSection)
309 logAtomicCrossSection =
new std::map<G4int,G4PhysicsFreeVector*>;
312 if (!logAtomicCrossSection->count(iZ))
317 ed <<
"Unable to retrieve the cross section table for Z=" << iZ <<
G4endl;
318 ed <<
"This can happen only in Unit Tests or via G4EmCalculator" <<
G4endl;
319 G4Exception(
"G4PenelopeRayleighModel::ComputeCrossSectionPerAtom()",
333 ed <<
"Unable to find Z=" << iZ <<
" in the atomic cross section table" <<
G4endl;
334 G4Exception(
"G4PenelopeRayleighModel::ComputeCrossSectionPerAtom()",
340 cross = std::exp(logXS);
342 if (verboseLevel > 2)
343 G4cout <<
"Rayleigh cross section at " << energy/
keV <<
" keV for Z=" << Z <<
354 G4Exception(
"G4PenelopeRayleighModel::BuildFormFactorTable()",
365 std::vector<G4double> *StechiometricFactors =
new std::vector<G4double>;
366 for (
G4int i=0;i<nElements;i++)
368 G4double fraction = fractionVector[i];
369 G4double atomicWeigth = (*elementVector)[i]->GetA()/(
g/
mole);
370 StechiometricFactors->push_back(fraction/atomicWeigth);
373 G4double MaxStechiometricFactor = 0.;
374 for (
G4int i=0;i<nElements;i++)
376 if ((*StechiometricFactors)[i] > MaxStechiometricFactor)
377 MaxStechiometricFactor = (*StechiometricFactors)[i];
379 if (MaxStechiometricFactor<1e-16)
382 ed <<
"Inconsistent data of atomic composition for " <<
384 G4Exception(
"G4PenelopeRayleighModel::BuildFormFactorTable()",
388 for (
G4int i=0;i<nElements;i++)
389 (*StechiometricFactors)[i] /= MaxStechiometricFactor;
393 for (
G4int i=0;i<nElements;i++)
394 atomsPerMolecule += (*StechiometricFactors)[i];
402 for (
size_t k=0;k<logQSquareGrid.size();k++)
405 for (
G4int i=0;i<nElements;i++)
407 G4int iZ = (
G4int) (*elementVector)[i]->GetZ();
410 ff2 += f*f*(*StechiometricFactors)[i];
413 theFFVec->
PutValue(k,logQSquareGrid[k],std::log(ff2));
415 logFormFactorTable->insert(std::make_pair(material,theFFVec));
417 delete StechiometricFactors;
444 if (verboseLevel > 3)
445 G4cout <<
"Calling SamplingSecondaries() of G4PenelopeRayleighModel" <<
G4endl;
449 if (photonEnergy0 <= fIntrinsicLowEnergyLimit)
463 if (!pMaxTable || !samplingTable)
465 G4Exception(
"G4PenelopeRayleighModel::SampleSecondaries()",
487 G4double G = 0.5*(1+cosTheta*cosTheta);
495 G4double LastQ2inTheTable = theDataTable->
GetX(nData-1);
513 cosTheta = 1.0-2.0*xx/q2max;
514 G4double G = 0.5*(1+cosTheta*cosTheta);
520 G4double sinTheta = std::sqrt(1-cosTheta*cosTheta);
524 G4double dirX = sinTheta*std::cos(phi);
525 G4double dirY = sinTheta*std::sin(phi);
531 photonDirection1.
rotateUz(photonDirection0);
541 void G4PenelopeRayleighModel::ReadDataFile(
const G4int Z)
545 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
548 if (verboseLevel > 2)
550 G4cout <<
"G4PenelopeRayleighModel::ReadDataFile()" <<
G4endl;
551 G4cout <<
"Going to read Rayleigh data files for Z=" << Z <<
G4endl;
554 char* path = getenv(
"G4LEDATA");
557 G4String excep =
"G4LEDATA environment variable not set!";
558 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
566 std::ostringstream ost;
568 ost << path <<
"/penelope/rayleigh/pdgra" << Z <<
".p08";
570 ost << path <<
"/penelope/rayleigh/pdgra0" << Z <<
".p08";
571 std::ifstream
file(ost.str().c_str());
575 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
580 file >> readZ >> nPoints;
582 if (readZ != Z || nPoints <= 0 || nPoints >= 5000)
585 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
586 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
592 for (
size_t i=0;i<nPoints;i++)
598 theVec->
PutValue(i,std::log(ene),std::log(xs));
599 if (
file.eof() && i != (nPoints-1))
602 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
603 ed <<
"Found less than " << nPoints <<
"entries " <<
G4endl;
604 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
608 if (!logAtomicCrossSection)
610 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
611 "em2044",
FatalException,
"Unable to allocate the atomic cross section table");
615 logAtomicCrossSection->insert(std::make_pair(Z,theVec));
621 std::ostringstream ost2;
623 ost2 << path <<
"/penelope/rayleigh/pdaff" << Z <<
".p08";
625 ost2 << path <<
"/penelope/rayleigh/pdaff0" << Z <<
".p08";
626 file.open(ost2.str().c_str());
630 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
633 file >> readZ >> nPoints;
635 if (readZ != Z || nPoints <= 0 || nPoints >= 5000)
638 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
639 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
647 if (!logQSquareGrid.size())
649 for (
size_t i=0;i<nPoints;i++)
656 logQSquareGrid.push_back(2.0*std::log(q));
658 if (
file.eof() && i != (nPoints-1))
661 ed <<
"Corrupted data file for Z=" << Z <<
G4endl;
662 ed <<
"Found less than " << nPoints <<
"entries " <<
G4endl;
663 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
667 if (!atomicFormFactor)
669 G4Exception(
"G4PenelopeRayleighModel::ReadDataFile()",
671 "Unable to allocate the atomicFormFactor data table");
675 atomicFormFactor->insert(std::make_pair(Z,theFFVec));
688 G4double logQSquared = (QSquared>1e-10) ? std::log(QSquared) : -23.;
690 G4double maxlogQ2 = logQSquareGrid[logQSquareGrid.size()-1];
699 ed <<
"Unable to retrieve F squared table for " << mat->
GetName() <<
G4endl;
700 G4Exception(
"G4PenelopeRayleighModel::GetFSquared()",
704 if (logQSquared < -20)
707 f2 = std::exp(logf2);
709 else if (logQSquared > maxlogQ2)
715 f2 = std::exp(logf2);
718 if (verboseLevel > 3)
721 G4cout <<
"Q^2 = " << QSquared <<
" (units of 1/(m_e*c); F^2 = " << f2 <<
G4endl;
728 void G4PenelopeRayleighModel::InitializeSamplingAlgorithm(
const G4Material* mat)
732 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
737 const size_t np = 150;
739 for (
size_t i=1;i<logQSquareGrid.size();i++)
741 G4double Q2 = std::exp(logQSquareGrid[i]);
742 if (GetFSquared(mat,Q2) > 1e-35)
744 q2max = std::exp(logQSquareGrid[i-1]);
749 size_t nReducedPoints = np/4;
754 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
756 "Too few points to initialize the sampling algorithm");
758 if (q2min > (q2max-1e-10))
760 G4cout <<
"q2min= " << q2min <<
" q2max= " << q2max <<
G4endl;
761 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
763 "Too narrow grid to initialize the sampling algorithm");
776 const G4int nip = 51;
780 for (
size_t i=1;i<NUNIF-1;i++)
788 G4cout <<
"Vector x has " << x->size() <<
" points, while NUNIF = " << NUNIF <<
G4endl;
797 for (
size_t i=0;i<NUNIF-1;i++)
806 for (
G4int k=0;k<nip;k++)
810 pdfi->push_back(pdfk);
816 pdfih->push_back(pdfIK);
824 for (
G4int k=1;k<nip;k++)
827 G4double next = previous + cons*((*pdfi)[k-1]+4.0*(*pdfih)[k-1]+(*pdfi)[k]);
828 sumi->push_back(next);
831 G4double lastIntegral = (*sumi)[sumi->size()-1];
832 area->push_back(lastIntegral);
835 for (
size_t k=0;k<sumi->size();k++)
836 (*sumi)[k] *= factor;
839 if ((*pdfi)[0] < 1e-35)
840 (*pdfi)[0] = 1e-5*pdfmax;
841 if ((*pdfi)[pdfi->size()-1] < 1e-35)
842 (*pdfi)[pdfi->size()-1] = 1e-5*pdfmax;
845 G4double pui = (*pdfi)[pdfi->size()-1]*factor;
846 G4double B_temp = 1.0-1.0/(pli*pui*dx*dx);
847 G4double A_temp = (1.0/(pli*dx))-1.0-B_temp;
848 G4double C_temp = 1.0+A_temp+B_temp;
857 a->push_back(A_temp);
858 b->push_back(B_temp);
859 c->push_back(C_temp);
871 for (
G4int k=0;k<nip;k++)
874 G4double pap = (*area)[i]*(1.0+((*a)[i]+(*b)[i]*rr)*rr)*(1.0+((*a)[i]+(*b)[i]*rr)*rr)/
875 ((1.0-(*b)[i]*rr*rr)*(*c)[i]*((*x)[i+1]-(*x)[i]));
876 if (k == 0 || k == nip-1)
877 (*err)[i] += 0.5*std::fabs(pap-(*pdfi)[k]);
879 (*err)[i] += std::fabs(pap-(*pdfi)[k]);
884 if ((*err)[i] > 0.1*(*area)[i] && icase == 1)
900 (*x)[x->size()-1] = q2max;
907 if (x->size() != NUNIF || a->size() != NUNIF ||
908 err->size() != NUNIF || area->size() != NUNIF)
911 ed <<
"Problem in building the Table for Sampling: array dimensions do not match" <<
G4endl;
912 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
924 for (
size_t i=0;i<err->size()-2;i++)
927 if ((*err)[i] > maxError)
929 maxError = (*err)[i];
935 G4double newx = 0.5*((*x)[iErrMax]+(*x)[iErrMax+1]);
937 x->insert(x->begin()+iErrMax+1,newx);
939 area->insert(area->begin()+iErrMax+1,0.);
940 a->insert(a->begin()+iErrMax+1,0.);
941 b->insert(b->begin()+iErrMax+1,0.);
942 c->insert(c->begin()+iErrMax+1,0.);
943 err->insert(err->begin()+iErrMax+1,0.);
946 for (
size_t i=iErrMax;i<=iErrMax+1;i++)
953 G4double dxLocal = (*x)[i+1]-(*x)[i];
956 for (
G4int k=0;k<nip;k++)
960 pdfi->push_back(pdfk);
966 pdfih->push_back(pdfIK);
974 for (
G4int k=1;k<nip;k++)
977 G4double next = previous + cons*((*pdfi)[k-1]+4.0*(*pdfih)[k-1]+(*pdfi)[k]);
978 sumi->push_back(next);
980 G4double lastIntegral = (*sumi)[sumi->size()-1];
981 (*area)[i] = lastIntegral;
985 for (
size_t k=0;k<sumi->size();k++)
986 (*sumi)[k] *= factor;
989 if ((*pdfi)[0] < 1e-35)
990 (*pdfi)[0] = 1e-5*pdfmax;
991 if ((*pdfi)[pdfi->size()-1] < 1e-35)
992 (*pdfi)[pdfi->size()-1] = 1e-5*pdfmax;
995 G4double pui = (*pdfi)[pdfi->size()-1]*factor;
996 G4double B_temp = 1.0-1.0/(pli*pui*dxLocal*dxLocal);
997 G4double A_temp = (1.0/(pli*dxLocal))-1.0-B_temp;
998 G4double C_temp = 1.0+A_temp+B_temp;
1019 for (
G4int k=0;k<nip;k++)
1022 G4double pap = (*area)[i]*(1.0+((*a)[i]+(*b)[i]*rr)*rr)*(1.0+((*a)[i]+(*b)[i]*rr)*rr)/
1023 ((1.0-(*b)[i]*rr*rr)*(*c)[i]*((*x)[i+1]-(*x)[i]));
1024 if (k == 0 || k == nip-1)
1025 (*err)[i] += 0.5*std::fabs(pap-(*pdfi)[k]);
1027 (*err)[i] += std::fabs(pap-(*pdfi)[k]);
1032 if ((*err)[i] > 0.1*(*area)[i] && icase == 1)
1045 }
while(x->size() < np);
1047 if (x->size() != np || a->size() != np ||
1048 err->size() != np || area->size() != np)
1050 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
1052 "Problem in building the extended Table for Sampling: array dimensions do not match ");
1059 for (
size_t i=0;i<np-1;i++)
1063 for (
size_t i=0;i<np-1;i++)
1067 errMax =
std::max(errMax,(*err)[i]);
1073 for (
size_t i=0;i<np-1;i++)
1076 PAC->push_back(previous+(*area)[i]);
1078 (*PAC)[PAC->size()-1] = 1.;
1085 std::vector<size_t> *ITTL =
new std::vector<size_t>;
1086 std::vector<size_t> *ITTU =
new std::vector<size_t>;
1089 for (
size_t i=0;i<np;i++)
1097 for (
size_t i=1;i<(np-1);i++)
1101 for (
size_t j=(*ITTL)[i-1];j<np && !found;j++)
1103 if ((*PAC)[j] > ptst)
1111 (*ITTU)[ITTU->size()-2] = ITTU->size()-1;
1112 (*ITTU)[ITTU->size()-1] = ITTU->size()-1;
1113 (*ITTL)[ITTL->size()-1] = ITTU->size()-2;
1115 if (ITTU->size() != np || ITTU->size() != np)
1117 G4Exception(
"G4PenelopeRayleighModel::InitializeSamplingAlgorithm()",
1119 "Problem in building the Limit Tables for Sampling: array dimensions do not match");
1127 for (
size_t i=0;i<np;i++)
1129 theTable->
AddPoint((*x)[i],(*PAC)[i],(*a)[i],(*b)[i],(*ITTL)[i],(*ITTU)[i]);
1132 if (verboseLevel > 2)
1134 G4cout <<
"*************************************************************************" <<
1136 G4cout <<
"Sampling table for Penelope Rayleigh scattering in " << mat->
GetName() <<
G4endl;
1139 samplingTable->insert(std::make_pair(mat,theTable));
1160 void G4PenelopeRayleighModel::GetPMaxTable(
const G4Material* mat)
1164 G4Exception(
"G4PenelopeRayleighModel::GetPMaxTable()",
1169 G4cout <<
"G4PenelopeRayleighModel::BuildPMaxTable" <<
G4endl;
1170 G4cout <<
"Going to instanziate the pMaxTable !" <<
G4endl;
1172 pMaxTable =
new std::map<const G4Material*,G4PhysicsFreeVector*>;
1175 if (pMaxTable->count(mat))
1181 G4Exception(
"G4PenelopeRayleighModel::GetPMaxTable()",
1183 "SamplingTable is not properly instantiated");
1187 if (!samplingTable->count(mat))
1188 InitializeSamplingAlgorithm(mat);
1193 size_t nOfEnergyPoints = logEnergyGridPMax.size();
1196 const size_t nip = 51;
1198 for (
size_t ie=0;ie<logEnergyGridPMax.size();ie++)
1212 size_t lowerBound = 0;
1213 size_t upperBound = tablePoints-1;
1214 while (lowerBound <= upperBound)
1216 size_t midBin = (lowerBound + upperBound)/2;
1217 if( Qm2 < theTable->GetX(midBin))
1218 { upperBound = midBin-1; }
1220 { lowerBound = midBin+1; }
1230 for (
size_t k=0;k<nip;k++)
1234 (theTable->
GetX(upperBound+1)-Q1);
1239 if (std::fabs(con1) > 1.0e-16*std::fabs(con2))
1240 etap = con2*(1.0-std::sqrt(1.0-2.0*tau*con1/(con2*con2)))/con1;
1244 (1.0+(theA+theB*etap)*etap)*(1.0+(theA+theB*etap)*etap)/
1245 ((1.0-theB*etap*etap)*ci*(theTable->
GetX(upperBound+1)-Q1));
1246 fun->push_back(theFun);
1254 (5.0*(*fun)[0]+8.0*(*fun)[1]-(*fun)[2])*CONS;
1255 sum->push_back(secondPoint);
1256 for (
size_t hh=2;
hh<nip-1;
hh++)
1259 G4double next = previous+(13.0*((*fun)[
hh-1]+(*fun)[
hh])-
1260 (*fun)[
hh+1]-(*fun)[
hh-2])*HCONS;
1261 sum->push_back(next);
1263 G4double last = (*sum)[nip-2]+(5.0*(*fun)[nip-1]+8.0*(*fun)[nip-2]-
1264 (*fun)[nip-3])*CONS;
1265 sum->push_back(last);
1266 thePMax = thePAC + (*sum)[sum->size()-1];
1277 thePMax = theTable->
GetPAC(0);
1281 theVec->
PutValue(ie,energy,thePMax);
1284 pMaxTable->insert(std::make_pair(mat,theVec));
1293 G4cout <<
"*****************************************************************" <<
G4endl;
1297 G4cout <<
"*****************************************************************" <<
G4endl;
1298 if (!logFormFactorTable->count(mat))
1299 BuildFormFactorTable(mat);
G4double LowEnergyLimit() const
std::vector< G4Element * > G4ElementVector
void PutValue(size_t binNumber, G4double binValue, G4double dataValue)
std::ostringstream G4ExceptionDescription
virtual ~G4PenelopeRayleighModel()
G4double GetKineticEnergy() const
G4double HighEnergyLimit() const
virtual void Initialise(const G4ParticleDefinition *, const G4DataVector &)
G4double GetB(size_t index)
const G4String & GetName() const
virtual void InitialiseLocal(const G4ParticleDefinition *, G4VEmModel *masterModel)
G4double GetPAC(size_t index)
size_t GetVectorLength() const
G4double GetLowEdgeEnergy(size_t binNumber) const
const G4ElementVector * GetElementVector() const
void ProposeMomentumDirection(G4double Px, G4double Py, G4double Pz)
#define G4MUTEX_INITIALIZER
void ProposeLocalEnergyDeposit(G4double anEnergyPart)
void SetHighEnergyLimit(G4double)
size_t GetNumberOfStoredPoints()
G4GLOB_DLL std::ostream G4cout
size_t GetTableSize() const
const G4ThreeVector & GetMomentumDirection() const
Hep3Vector & rotateUz(const Hep3Vector &)
void DumpFormFactorTable(const G4Material *)
G4double Value(G4double theEnergy, size_t &lastidx) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
static G4ProductionCutsTable * GetProductionCutsTable()
G4double SampleValue(G4double rndm)
const G4MaterialCutsCouple * GetMaterialCutsCouple(G4int i) const
T max(const T t1, const T t2)
brief Return the largest of the two arguments
G4double GetA(size_t index)
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
G4double GetX(size_t index)
G4ParticleChangeForGamma * fParticleChange
void SetProposedKineticEnergy(G4double proposedKinEnergy)
virtual void SampleSecondaries(std::vector< G4DynamicParticle * > *, const G4MaterialCutsCouple *, const G4DynamicParticle *, G4double tmin, G4double maxEnergy)
size_t GetNumberOfElements() const
void ProposeTrackStatus(G4TrackStatus status)
G4PenelopeRayleighModel(const G4ParticleDefinition *p=0, const G4String &processName="PenRayleigh")
void AddPoint(G4double x0, G4double pac0, G4double a0, G4double b0, size_t ITTL0, size_t ITTU0)
const G4double * GetFractionVector() const
virtual G4double ComputeCrossSectionPerAtom(const G4ParticleDefinition *, G4double kinEnergy, G4double Z, G4double A=0, G4double cut=0, G4double emax=DBL_MAX)
const G4ParticleDefinition * fParticle
G4ParticleChangeForGamma * GetParticleChangeForGamma()
const G4Material * GetMaterial() const