EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_track_scoring.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ particle track scoring object
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, 2009
25 #
26 # Contributors: Georgi Gerganov
27 # Alexandre Demelo
28 # Hannah Gallop
29 #
30 ###############################################################################
31 */
32 
33 
39 #include "egs_track_scoring.h"
40 #include "egs_input.h"
41 #include "egs_functions.h"
42 
43 static bool EGS_TRACK_SCORING_LOCAL inputSet = false;
44 
45 EGS_TrackScoring::EGS_TrackScoring(const string &Name, EGS_ObjectFactory *f) :
46  EGS_AusgabObject(Name,f), m_pts(0), m_start(0), m_stop(1024), m_lastCase(-1),
47  m_nScore(0), m_bufSize(16), m_score(false), m_didScore(false),
48  m_score_photons(true), m_score_electrons(true), m_score_positrons(true), m_fnExtra(""), m_include_time(false) {
49  otype = "EGS_TrackScoring";
50 }
51 
52 EGS_TrackScoring::~EGS_TrackScoring() {
53  if (m_pts) {
54  delete m_pts;
55  }
56 }
57 
58 void EGS_TrackScoring::setApplication(EGS_Application *App) {
60  if (!app) {
61  return;
62  }
63  if (m_pts) {
64  delete m_pts;
65  m_pts = 0;
66  }
67  if (m_bufSize < 1) {
68  m_bufSize = 1024;
69  }
70  string fname(app->getOutputFile());
71  fname += m_fnExtra;
72  if (!egsIsAbsolutePath(fname)) {
73  fname = egsJoinPath(app->getAppDir(),fname);
74  }
75 
76  fname += ".ptracks";
77 
78  // Determine whether a dynamic geometry or source was used
79  // Only do this if the user didn't explicitly say whether to include time indices
80  if (m_autoDetectDynamic) {
81  m_include_time = app->containsDynamic();
82  }
83 
84  // create new particleTrackContainer using the m_include_time boolean which
85  // controls time index writting and filetype
87 
88  description = "\nParticle Track Scoring (";
89  description += name;
90  description += ")\n";
91  description += "======================================================\n";
92  description += " - Scoring photon tracks = ";
93  description += m_score_photons ? "YES\n" : "NO\n";
94  description += " - Scoring electron tracks = ";
95  description += m_score_electrons ? "YES\n" : "NO\n";
96  description += " - Scoring positron tracks = ";
97  description += m_score_positrons ? "YES\n" : "NO\n";
98  description += " - Include time index = ";
99  description += m_include_time ? "YES\n" : "NO\n";
100  description += " - First event to score = ";
101  char buf[32];
102  sprintf(buf,"%lld\n",m_start);
103  description += buf;
104  description += " - Last event to score = ";
105  sprintf(buf,"%lld\n",m_stop);
106  description += buf;
107  description += " - Track buffer size = ";
108  sprintf(buf,"%d\n",m_bufSize);
109  description += buf;
110  description += " - Output file name = ";
111  description += fname;
112  description += "\n\n";
113 }
114 
115 void EGS_TrackScoring::reportResults() {
116  egsInformation("\nParticle Track Scoring (%s)\n",name.c_str());
117  egsInformation("======================================================\n");
118  egsInformation(" Total events scored: %lld\n",m_nScore);
119  if (m_pts) {
120  m_pts->reportResults(false);
121  }
122 }
123 
124 
125 extern "C" {
126 
127  static void setInputs() {
128  inputSet = true;
129 
130  setBaseAusgabObjectInputs();
131 
132  ausBlockInput->getSingleInput("library")->setValues({"egs_track_scoring"});
133 
134  // Format: name, isRequired, description, vector string of allowed values
135  ausBlockInput->addSingleInput("score photons", false, "Score photons? Default is yes.", {"yes", "no"});
136  ausBlockInput->addSingleInput("score electrons", false, "Score the electrons? Default is yes.", {"yes", "no"});
137  ausBlockInput->addSingleInput("score positrons", false, "Score positrons? Default is yes.", {"yes", "no"});
138  ausBlockInput->addSingleInput("start scoring", false, "The history at which to start recording tracks. Defaults to 0.");
139  ausBlockInput->addSingleInput("stop scoring", false, "The history at which to stop recording tracks. Defaults to 1024.");
140  ausBlockInput->addSingleInput("buffer size", false, "The number of tracks to save in a buffer before writing out to a file. Defaults to 1024.");
141  ausBlockInput->addSingleInput("file name addition", false, "A string that is appended to the input file name for the .ptracks file");
142  }
143 
144  EGS_TRACK_SCORING_EXPORT string getExample() {
145  string example;
146  example = {
147  R"(
148  # Example of egs_track_scoring
149  :start ausgab object:
150  library = egs_track_scoring
151  name = my_score
152  score photons = yes
153  score electrons = yes
154  score positrons = yes
155  start scoring = 0
156  stop scoring = 1024
157  :stop ausgab object:
158 )"};
159  return example;
160  }
161 
162  EGS_TRACK_SCORING_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
163  if(!inputSet) {
164  setInputs();
165  }
166  return ausBlockInput;
167  }
168 
169  EGS_TRACK_SCORING_EXPORT EGS_AusgabObject *createAusgabObject(EGS_Input *input,
170  EGS_ObjectFactory *f) {
171  const static char *func = "createAusgabObject(track_scoring)";
172  if (!input) {
173  egsWarning("%s: null input?\n",func);
174  return 0;
175  }
176 
177  vector<string> sc_options;
178  sc_options.push_back("no");
179  sc_options.push_back("yes");
180  bool scph = input->getInput("score photons",sc_options,true);
181  bool scel = input->getInput("score electrons",sc_options,true);
182  bool scpo = input->getInput("score positrons",sc_options,true);
183  if (!scph && !scel && !scpo) {
184  return 0;
185  }
186 
187  // include time index lets the program know whether to write the time
188  // index to the tracks file
189  // The default is -1 so we know if this input wasn't specified
190  bool found = false;
191  bool incltime = input->getInput("include time index",sc_options,1,&found);
192  // If the user didn't specify whether or not to include time indices
193  // check if a dynamic source or geometry is in use
194  bool autoDetectDynamic = false;
195  if (!found) {
196  autoDetectDynamic = true;
197  }
198 
199  EGS_I64 first = 0, last = 1024;
200  input->getInput("start scoring",first);
201  input->getInput("stop scoring",last);
202  int bufSize = 1024;
203  input->getInput("buffer size",bufSize);
204  string fnExtra;
205  input->getInput("file name addition",fnExtra);
206  EGS_TrackScoring *result = new EGS_TrackScoring("",f);
207  result->setScorePhotons(scph);
208  result->setScoreElectrons(scel);
209  result->setScorePositrons(scpo);
210  result->setIncludeTime(incltime); // incltime boolean is set from aquired input for the trackscoring object (sets m_include_time)
211  result->setAutoDetectDynamic(autoDetectDynamic);
212  result->setFirstEvent(first);
213  result->setLastEvent(last);
214  result->setBufferSize(bufSize);
215  result->setFileNameExtra(fnExtra);
216  result->setName(input);
217  return result;
218  }
219 
220 }
Base class for advanced EGSnrc C++ applications.
const string & getOutputFile() const
Returns the base name of the output file(s)
const string & getAppDir() const
Returns the absolute path to the user code directory.
virtual void setApplication(EGS_Application *App)
Set the application this object belongs to.
string description
A short ausgab object description.
EGS_Application * app
The application this object belongs to.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
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:341
An object factory.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
string name
The object name.
A class that stores all the tracks in a simulation.
void reportResults(bool with_header=true)
Report results from the track scoring process so far.
A track scoring object: header.
EGS_I64 m_start
Minimum event index for which to score tracks.
bool m_score_electrons
Score electron tracks?
bool m_include_time
include time index in tracks file?
string m_fnExtra
String to append to output file name.
bool m_autoDetectDynamic
Option for autodetecting whether to include time indices.
EGS_I64 m_stop
Maximum event index for which to score tracks.
int m_bufSize
The track container size.
EGS_ParticleTrackContainer * m_pts
The particle track container.
bool m_score_photons
Score photon tracks?
EGS_I64 m_nScore
Number of events for which tracks were scored.
bool m_score_positrons
Score positron tracks?
Global egspp functions header file.
EGS_Input class header file.
EGS_RADIATIVE_SPLITTING_EXPORT EGS_AusgabObject * createAusgabObject(EGS_Input *input, EGS_ObjectFactory *f)
A track scoring ausgab object.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
bool egsIsAbsolutePath(const string &path)
Does the string path represent an absolute path name?
string egsJoinPath(const string &first, const string &second)
Join two path variables (or a path and a file name) using the platform specific directory separator a...
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.