45 static bool EGS_PHSP_SOURCE_LOCAL inputSet =
false;
53 void EGS_PhspSource::init() {
54 otype =
"EGS_PhspSource";
82 void EGS_PhspSource::openFile(
const string &phsp_file) {
93 the_file.open(phsp_file.c_str(),ios::binary | ios::in);
95 egsWarning(
"EGS_PhspSource::openFile: failed to open binary file %s"
96 " for reading\n",phsp_file.c_str());
101 for (
int i=0; i<5; i++) {
104 egsWarning(
"EGS_PhspSource::openFile: an I/O error occured "
105 "while reading the first record of %s\n",phsp_file.c_str());
110 if (cmode ==
"MODE0") {
114 else if (cmode ==
"MODE2") {
119 egsWarning(
"EGS_PhspSource::openFile: the file %s is not a MODE0 or"
120 " MODE2 file\n",phsp_file.c_str());
125 float emax, emin, pinc;
126 the_file.read((
char *) &n,
sizeof(
int));
127 the_file.read((
char *) &n_photon,
sizeof(
int));
128 the_file.read((
char *) &emax,
sizeof(
float));
129 the_file.read((
char *) &emin,
sizeof(
float));
130 the_file.read((
char *) &pinc,
sizeof(
float));
132 egsWarning(
"EGS_PhspSource::openFile: an I/O error occured "
133 "while reading the first record of %s\n",phsp_file.c_str());
138 if (n <= 0 || n_photon < 0 || n_photon > n || emin < 0 || emax < 0 ||
139 emax < emin || pinc < 0) {
146 if (n <= 0 || n_photon < 0 || n_photon > n || emin < 0 || emax < 0 ||
147 emax < emin || pinc < 0) {
148 egsWarning(
"EGS_PhspSource::openFile: phase space file header"
149 " contains meaningless values with and without byte swaping:\n");
154 egsWarning(
" number of photons: %d\n",n_photon);
156 if (n_photon > n)
egsWarning(
" number of photons (%d) is "
157 "greater than number of particles (%d)\n",n_photon,n);
165 egsWarning(
" emin > emax: %g %g\n",emin,emax);
178 istream::off_type nend = n;
184 egsWarning(
"EGS_PhspSource::openFile: failed to read the last"
185 " particle in the file, this indicates some unknown error condition\n");
208 int err = input->
getInput(
"phase space file",fname);
210 egsWarning(
"EGS_PhspSource: no 'phase space file' input\n");
215 egsWarning(
"EGS_PhspSource: errors while opening the phase space file"
216 " %s\n",fname.c_str());
219 vector<EGS_Float> cutout;
220 err = input->
getInput(
"cutout",cutout);
221 if (!err && cutout.size() == 4) {
222 setCutout(cutout[0],cutout[1],cutout[2],cutout[3]);
224 vector<string> ptype;
225 ptype.push_back(
"electrons");
226 ptype.push_back(
"photons");
227 ptype.push_back(
"positrons");
228 ptype.push_back(
"all");
229 ptype.push_back(
"charged");
230 particle_type = input->
getInput(
"particle type",ptype,3)-1;
231 vector<int> the_filter;
232 vector<string> the_latch;
233 err = input->
getInput(
"filter type",the_filter);
234 int err1 = input->
getInput(
"latch setting",the_latch);
236 if (the_filter[0] >= 0 && the_filter[0] <= 3) {
237 int nbit1 = the_latch[0].size();
239 if (!the_filter[0]) {
240 nbit2 = the_latch[1].size();
242 if (nbit1 + nbit2 > 0) {
243 int *the_bits =
new int [nbit1+nbit2];
244 for (
int j=0; j<nbit1+nbit2; j++) {
246 the_bits[j]=(int)(the_latch[0][j] ==
'1');
249 the_bits[j] = (int)(the_latch[1][j-nbit1] ==
'1');
252 setFilter(the_filter[0],nbit1,nbit2,the_bits);
257 vector<EGS_Float> wwindow;
258 err = input->
getInput(
"weight window",wwindow);
259 if (!err && wwindow.size() == 2) {
264 err = input->
getInput(
"reuse photons",ntmp);
265 if (!err && ntmp > 0) {
269 err = input->
getInput(
"recycle photons",ntmp);
270 if (!err && ntmp > 0) {
274 err = input->
getInput(
"reuse electrons",ntmp);
275 if (!err && ntmp > 0) {
279 err = input->
getInput(
"recycle electrons",ntmp);
280 if (!err && ntmp > 0) {
290 if (!
recl)
egsFatal(
"EGS_PhspSource::readParticle(): the file is not "
305 EGS_Float aux = p.u*p.u+p.v*p.v;
320 if (rejectParticle()) {
342 void EGS_PhspSource::setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun,
int npar,
int nchunk) {
345 EGS_I64 particlesPerChunk =
Nparticle/(npar*nchunk);
346 int ichunk = nstart/nrun;
347 if (ichunk > npar*nchunk-1) {
350 egsWarning(
"EGS_PhspSource: Remainder of histories will reuse the last chunk of the phase space source.\n");
351 ichunk = npar*nchunk-1;
353 Nfirst = ichunk*particlesPerChunk+1;
354 if (ichunk == npar*nchunk-1) {
364 egsInformation(
"EGS_PhspSource: using phsp portion between %lld and %lld\n",
368 void EGS_PhspSource::readParticle() {
370 egsWarning(
"EGS_PhspSource::readParticle(): reached the end of the "
371 "phase space file chunk (%lld)\n will start from the beginning "
372 "of the chunk (%lld) but this "
373 "implies that uncertainty estimates will be inaccurate\n",
383 egsFatal(
"EGS_PhspSource::readParticle(): I/O error while reading "
384 "phase space file\n");
386 data = (__egs_data32 *) &
record[0];
388 data = (__egs_data32 *) &
record[4];
390 data = (__egs_data32 *) &
record[8];
392 data = (__egs_data32 *) &
record[12];
394 data = (__egs_data32 *) &
record[16];
396 data = (__egs_data32 *) &
record[20];
398 data = (__egs_data32 *) &
record[24];
409 if (p.latch & 1073741824) {
412 else if (p.latch & 536870912) {
437 bool EGS_PhspSource::rejectParticle()
const {
438 if (particle_type < 2 && p.q != particle_type) {
441 if (particle_type == 3 && !p.q) {
444 if (p.x < Xmin || p.x > Xmax || p.y < Ymin || p.y > Ymax) {
447 if (p.wt < wmin || p.wt > wmax) {
450 if (p.latch & 2147483648UL) {
453 if (filter_type < 0) {
456 if (filter_type == 0) {
459 r1 = !(p.latch & filter1);
464 bool r2 = (p.latch & filter2);
467 if (filter_type == 1) {
468 return (p.latch & filter1);
470 int l = p.latch >> 24;
471 bool res = l & filter1;
472 return filter_type == 3 ? res : !res;
475 void EGS_PhspSource::setFilter(
int type,
int nbit1,
int nbit2,
const int *bits) {
476 if (type < 0 || type > 3) {
477 egsWarning(
"EGS_PhspSource::setFilter: invalid filter type %d\n",type);
485 egsWarning(
"EGS_PhspSource::setFilter: maximum number of bits is "
486 "limited to 29, you requested %d\n",ntot);
493 if (filter_type == 0) {
497 for (i=0; i<ntot; i++) {
499 unsigned int aux = 1;
510 else if (filter_type == 1) {
512 else if (filter_type == 2) {
514 else if (filter_type == 3) {
520 static void setInputs() {
523 setBaseSourceInputs(
false,
false);
525 srcBlockInput->getSingleInput(
"library")->setValues({
"egs_phsp_source"});
528 srcBlockInput->addSingleInput(
"phase space file",
true,
"The name of the phase-space file.");
529 srcBlockInput->addSingleInput(
"particle type",
true,
"The type of particle to keep from the phase-space. Other types are discarded.", {
"all",
"charged",
"electrons",
"positrons",
"photons"});
530 srcBlockInput->addSingleInput(
"cutout",
false,
"Discard particles outside of a rectanglular field: 'x1 y1 x2 y2'");
531 srcBlockInput->addSingleInput(
"weight window",
false,
"A weight window, outside of which particles are discarded: 'wtmin wtmax'. This allows you to discard high weight particles.");
532 srcBlockInput->addSingleInput(
"recycle photons",
false,
"The number of time to recycle each photon. E.g. set to 1 to use each photon twice. Set <1 or neglect to disable.");
533 srcBlockInput->addSingleInput(
"recycle electrons",
false,
"The number of time to recycle each electron. E.g. set to 1 to use each electron twice. Set <1 or neglect to disable.");
536 EGS_PHSP_SOURCE_EXPORT
string getExample() {
540 # Example of egs_phsp_soure
543 library = egs_phsp_source
544 phase space file = ../BEAM_EX16MVp/EX16MVp.egsphsp1
548 recycle electrons = 10
554 EGS_PHSP_SOURCE_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
558 return srcBlockInput;
564 createSourceTemplate<EGS_PhspSource>(input,f,
"phsp source");
static EGS_Application * activeApplication()
Get the active application.
Base source class. All particle sources must be derived from this class.
string description
A short source description.
string otype
The object type.
int Nrecycle_g
Number of times to recycle a photon.
int Nrecycle
Number of times to recycle current particle.
EGS_Float Emin
Minimum energy (obtained from the phsp file)
EGS_I64 Nfirst
first record this source can use
string the_file_name
The phase-space file name.
EGS_I64 Nparticle
Number of particles in the file.
int Nrecycle_e
Number of times to recycle a charged particle.
EGS_Float Emax
Maximum energy (obtained from the phsp file)
EGS_PhspSource(const string &phsp_file, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
int recl
The particle record length.
int Nrewind
Number of times the phsp file was rewinded.
EGS_I64 Nlast
Last record this source can use.
EGS_I64 Npos
Next record to be read.
ifstream the_file
Phase space data stream.
int Nuse
Number of times current particle was used so far.
char * record
Memory to read a particle into.
EGS_I64 Nread
Number of particles read so far.
EGS_I64 Nphoton
Number of photons in the file.
bool mode2
true, if a MODE2 file
EGS_Float Pinc
Number of incident particles that created the file.
Base random number generator class. All random number generators should be derived from this class.
A class representing 3D vectors.
EGS_Application class header file.
Global egspp functions header file.
A phase-space file source.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
void egsSwapBytes(int *n)
Swap the bytes of 32 bit integers.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.
const EGS_Float veryFar
A very large float.