You should use a sphere filled with the material VACUUM. This way, the source spectrum is not altered.
When you convert the energy from GeV to MeV in your weight calculation, you need to multiply by 1000 before you raise to the power.
You polynomial can and will return negative value at low energies. You should implement a protection against this. The easiest would be to increase the minimum sampled energy.