Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optional logarithmic sampling of the photon energies #476

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/crpropa/module/PhotoPionProduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class PhotoPionProduction: public Module {
// - output: (s-p^2) * sigma_(nucleon/gamma) [GeV^2 * mubarn]
double functs(double s, bool onProton) const;

double proposal_pdf(double eps, double epsMin, double epsMax) const;

double proposal_inv_cdf(double prob, double epsMin, double epsMax) const;

// called by: sampleEps, gaussInt
// - input: photon energy eps [eV], Ein [GeV]
// - output: probability to encounter photon of energy eps
Expand Down
29 changes: 25 additions & 4 deletions src/module/PhotoPionProduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ SophiaEventOutput PhotoPionProduction::sophiaEvent(bool onProton, double Ein, do
return output;
}

double PhotoPionProduction::proposal_pdf(double eps, double epsMin, double epsMax) const {
double prob = 1 / (eps * log(epsMax / epsMin));
return prob;
}

double PhotoPionProduction::proposal_inv_cdf(double prob, double epsMin, double epsMax) const {
double eps = epsMin * pow(epsMax / epsMin, prob);
return eps;
}

double PhotoPionProduction::sampleEps(bool onProton, double E, double z) const {
// sample eps between epsMin ... epsMax
double Ein = E / GeV;
Expand All @@ -422,10 +432,21 @@ double PhotoPionProduction::sampleEps(bool onProton, double E, double z) const {

Random &random = Random::instance();
for (int i = 0; i < 1000000; i++) {
double eps = epsMin + random.rand() * (epsMax - epsMin);
double pEps = probEps(eps, onProton, Ein, z);
if (random.rand() * pEpsMax < pEps)
return eps * eV;
double eps;
double pEps;
if (sampleLog){
// convert uniformly-sampled probability to eps via inverse CDF
eps = proposal_inv_cdf(random.rand(), epsMin, epsMax);
// accept / reject based on ratio of pdfs
pEps = probEps(eps, onProton, Ein, z);
double pEps_proposal = proposal_pdf(eps, epsMin, epsMax);
if (pEps / pEps_proposal > random.rand() * pEpsMax)
return eps * eV;
} else
eps = epsMin + random.rand() * (epsMax - epsMin);
pEps = probEps(eps, onProton, Ein, z);
if (random.rand() * pEpsMax < pEps)
return eps * eV;
}
throw std::runtime_error("error: no photon found in sampleEps, please make sure that photon field provides photons for the interaction by adapting the energy range of the tabulated photon field.");
}
Expand Down