EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_source_collection.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ source collection
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Marc Chamberland
27 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_source_collection.h"
38 #include "egs_input.h"
39 
41  EGS_ObjectFactory *f) : EGS_BaseSource(input,f), nsource(0), count(0) {
42  vector<EGS_BaseSource *> s;
43  egsInformation("EGS_BaseSource::EGS_BaseSource: input is:\n");
44  input->print(0,cout);
45  EGS_Input *isource;
46  while ((isource = input->takeInputItem("source",false))) {
47  egsInformation("EGS_SourceCollection: got input\n");
48  EGS_BaseSource *this_source = EGS_BaseSource::createSource(isource);
49  if (!this_source) {
50  egsWarning("EGS_SourceCollection: got null source\n");
51  }
52  else {
53  s.push_back(this_source);
54  }
55  delete isource;
56  }
57  vector<string> snames;
58  int err = input->getInput("source names",snames);
59  if (!err) {
60  for (unsigned int j=0; j<snames.size(); j++) {
61  EGS_BaseSource *this_source = EGS_BaseSource::getSource(snames[j]);
62  if (!this_source) {
63  egsWarning("EGS_SourceCollection: got null source\n");
64  }
65  else {
66  s.push_back(this_source);
67  }
68  }
69  }
70  if (s.size() < 1) {
71  egsWarning("EGS_SourceCollection: no sources\n");
72  return;
73  }
74  vector<EGS_Float> prob;
75  err = input->getInput("weights",prob);
76  if (err) {
77  egsWarning("EGS_SourceCollection: missing 'weights' input\n");
78  return;
79  }
80  if (prob.size() != s.size()) {
81  egsWarning("EGS_SourceCollection: the number of sources (%d) is not"
82  " the same as the number of input probabilities (%d)\n",
83  s.size(),prob.size());
84  return;
85  }
86  setUp(s,prob);
87 }
88 
89 void EGS_SourceCollection::setUp(const vector<EGS_BaseSource *> &S,
90  const vector<EGS_Float> &prob) {
91  otype = "EGS_SourceCollection";
92  nsource = S.size();
93  if (prob.size() < nsource) {
94  nsource = prob.size();
95  }
96  description = "Invalid source collection";
97  if (isValid()) {
98  p = new EGS_Float [nsource];
99  sources = new EGS_BaseSource* [nsource];
100  Emax = 0;
101  for (int j=0; j<nsource; j++) {
102  p[j] = prob[j];
103  sources[j] = S[j];
104  if (p[j] < 0 || !sources[j]) {
105  if (p[j] < 0) egsWarning("EGS_SourceCollection: input "
106  "probability p[%d]=%g is less than zero.\n",j,p[j]);
107  else {
108  egsWarning("EGS_SourceCollection: source %d is null\n",j);
109  }
110  delete [] p;
111  for (int i=0; i<j; j++) {
113  }
114  delete [] sources;
115  nsource = 0;
116  return;
117  }
118  sources[j]->ref();
119  EGS_Float e = sources[j]->getEmax();
120  if (e > Emax) {
121  Emax = e;
122  }
123  }
124  table = new EGS_SimpleAliasTable(nsource,p);
125  description = "Source collection";
126  last_cases = new EGS_I64 [ nsource ];
127  for (int i=0; i<nsource; i++) {
128  last_cases[i] = 0;
129  }
130  }
131 }
132 
133 extern "C" {
134 
135  EGS_SOURCE_COLLECTION_EXPORT EGS_BaseSource *createSource(EGS_Input *input,
136  EGS_ObjectFactory *f) {
137  return
138  createSourceTemplate<EGS_SourceCollection>(input,f,"source collection");
139  }
140 
141 }
static EGS_BaseSource * createSource(EGS_Input *)
Create sources from the information pointed to by input.
EGS_Float Emax
Maximum energy (max of s[j]-&gt;getEmax()).
A source collection.
EGS_Input class header file.
static void deleteObject(EGS_Object *o)
Delete an object.
void print(int nind, ostream &)
Used for debugging purposes.
Definition: egs_input.cpp:1174
EGS_SimpleAliasTable * table
Alias table for randomly picking a source.
EGS_SourceCollection(const vector< EGS_BaseSource * > &S, const vector< EGS_Float > &prob, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
int ref()
Increase the reference count to this object.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
A class for sampling random bins from a given probability distribution using the alias table techniqu...
An object factory.
static EGS_BaseSource * getSource(const string &Name)
Get a pointer to the source named Name.
EGS_BaseSource ** sources
The sources in the collection.
EGS_I64 * last_cases
Last case returned from each source.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
EGS_Float * p
The probabilities.
string otype
The object type.
EGS_Input * takeInputItem(const string &key, bool self=true)
Get the property named key.
Definition: egs_input.cpp:226
virtual EGS_Float getEmax() const =0
Return the maximum energy of this source.
int getInput(const string &key, vector< string > &values) const
Assign values to an array of strings from an input identified by key.
Definition: egs_input.cpp:338
Base source class. All particle sources must be derived from this class.
string description
A short source description.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.