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 #
29 ###############################################################################
30 */
31 
32 
38 #include "egs_track_scoring.h"
39 #include "egs_input.h"
40 #include "egs_functions.h"
41 
42 EGS_TrackScoring::EGS_TrackScoring(const string &Name, EGS_ObjectFactory *f) :
43  EGS_AusgabObject(Name,f), m_pts(0), m_start(0), m_stop(1024), m_lastCase(-1),
44  m_nScore(0), m_bufSize(16), m_score(false), m_didScore(false),
45  m_score_photons(true), m_score_electrons(true), m_score_positrons(true), m_fnExtra(""), m_include_time(false) {
46  otype = "EGS_TrackScoring";
47 }
48 
49 EGS_TrackScoring::~EGS_TrackScoring() {
50  if (m_pts) {
51  delete m_pts;
52  }
53 }
54 
55 void EGS_TrackScoring::setApplication(EGS_Application *App) {
57  if (!app) {
58  return;
59  }
60  if (m_pts) {
61  delete m_pts;
62  m_pts = 0;
63  }
64  if (m_bufSize < 1) {
65  m_bufSize = 1024;
66  }
67  string fname(app->getOutputFile());
68  fname += m_fnExtra;
69  if (!egsIsAbsolutePath(fname)) {
70  fname = egsJoinPath(app->getAppDir(),fname);
71  }
72  int i_parallel = -1;
73  if (app->getNparallel() > 1) {
74  i_parallel = app->getIparallel();
75  }
76  if (i_parallel >= 0) {
77  char buf[16];
78  sprintf(buf,"_w%d",i_parallel);
79  fname += buf;
80  }
81 
82  fname += ".ptracks";
83 
84  // Determine whether a dynamic geometry or source was used
85  // Only do this if the user didn't explicitly say whether to include time indices
86  if (m_autoDetectDynamic) {
87  m_include_time = app->containsDynamic();
88  }
89 
90  // create new particleTrackContainer using the m_include_time boolean which
91  // controls time index writting and filetype
93 
94  description = "\nParticle Track Scoring (";
95  description += name;
96  description += ")\n";
97  description += "======================================================\n";
98  description += " - Scoring photon tracks = ";
99  description += m_score_photons ? "YES\n" : "NO\n";
100  description += " - Scoring electron tracks = ";
101  description += m_score_electrons ? "YES\n" : "NO\n";
102  description += " - Scoring positron tracks = ";
103  description += m_score_positrons ? "YES\n" : "NO\n";
104  description += " - Include time index = ";
105  description += m_include_time ? "YES\n" : "NO\n";
106  description += " - First event to score = ";
107  char buf[32];
108  sprintf(buf,"%lld\n",m_start);
109  description += buf;
110  description += " - Last event to score = ";
111  sprintf(buf,"%lld\n",m_stop);
112  description += buf;
113  description += " - Track buffer size = ";
114  sprintf(buf,"%d\n",m_bufSize);
115  description += buf;
116  description += " - Output file name = ";
117  description += fname;
118  description += "\n\n";
119 }
120 
121 void EGS_TrackScoring::reportResults() {
122  egsInformation("\nParticle Track Scoring (%s)\n",name.c_str());
123  egsInformation("======================================================\n");
124  egsInformation(" Total events scored: %lld\n",m_nScore);
125  if (m_pts) {
126  m_pts->reportResults(false);
127  }
128 }
129 
130 extern "C" {
131 
132  EGS_TRACK_SCORING_EXPORT EGS_AusgabObject *createAusgabObject(EGS_Input *input,
133  EGS_ObjectFactory *f) {
134  const static char *func = "createAusgabObject(track_scoring)";
135  if (!input) {
136  egsWarning("%s: null input?\n",func);
137  return 0;
138  }
139  vector<string> sc_options;
140  sc_options.push_back("no");
141  sc_options.push_back("yes");
142  bool scph = input->getInput("score photons",sc_options,true);
143  bool scel = input->getInput("score electrons",sc_options,true);
144  bool scpo = input->getInput("score positrons",sc_options,true);
145  if (!scph && !scel && !scpo) {
146  return 0;
147  }
148 
149  // include time index lets the program know whether to write the time
150  // index to the tracks file
151  // The default is -1 so we know if this input wasn't specified
152  bool found = false;
153  bool incltime = input->getInput("include time index",sc_options,1,&found);
154  // If the user didn't specify whether or not to include time indices
155  // check if a dynamic source or geometry is in use
156  bool autoDetectDynamic = false;
157  if (!found) {
158  autoDetectDynamic = true;
159  }
160 
161  EGS_I64 first = 0, last = 1024;
162  input->getInput("start scoring",first);
163  input->getInput("stop scoring",last);
164  int bufSize = 1024;
165  input->getInput("buffer size",bufSize);
166  string fnExtra;
167  input->getInput("file name addition",fnExtra);
168  EGS_TrackScoring *result = new EGS_TrackScoring("",f);
169  result->setScorePhotons(scph);
170  result->setScoreElectrons(scel);
171  result->setScorePositrons(scpo);
172  result->setIncludeTime(incltime); // incltime boolean is set from aquired input for the trackscoring object (sets m_include_time)
173  result->setAutoDetectDynamic(autoDetectDynamic);
174  result->setFirstEvent(first);
175  result->setLastEvent(last);
176  result->setBufferSize(bufSize);
177  result->setFileNameExtra(fnExtra);
178  result->setName(input);
179  return result;
180  }
181 
182 }
Base class for advanced EGSnrc C++ applications.
int getNparallel() const
Returns the number of parallel jobs executing.
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.
int getIparallel() const
Returns the job number in a parallel run.
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:338
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.