EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_particle_track.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ particle tracks headers
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: Georgi Gerganov, 2009
25 #
26 # Contributors: Iwan Kawrakow
27 # Reid Townson
28 # Alexandre Demelo
29 #
30 ###############################################################################
31 */
32 
33 
39 #ifndef EGS_PARTICLE_TRACK_
40 #define EGS_PARTICLE_TRACK_
41 
42 #include "egs_vector.h"
43 #include "egs_config1.h"
44 #include "egs_functions.h"
45 
46 #include <fstream>
47 #include <cstring>
48 using namespace std;
49 
61 
62 public:
63 
67  struct Vertex {
69  EGS_Float e;
70 
71  Vertex() : x(0,0,0), e(0) {};
72  Vertex(EGS_Float px, EGS_Float py, EGS_Float pz) {
73  x.x = px;
74  x.y = py;
75  x.z = pz;
76  e = 1;
77  };
78  Vertex(EGS_Float px, EGS_Float py, EGS_Float pz, EGS_Float pe) {
79  x.x = px;
80  x.y = py;
81  x.z = pz;
82  e = pe;
83  };
84  Vertex(const EGS_Vector &px, EGS_Float pe) : x(px), e(pe) {};
85  };
86 
88  struct ParticleInfo {
89  EGS_I32 q;
90  ParticleInfo(EGS_I32 pq) {
91  q = pq;
92  };
93  ~ParticleInfo() { };
94  };
95 
97  EGS_ParticleTrack() : m_pInfo(NULL), m_size(16), m_nVertices(0) {
98  m_track = new Vertex* [m_size];
99  }
100 
103  clearTrack();
104  if (m_track) {
105  delete [] m_track;
106  }
107  m_track = NULL;
108  };
109 
115  void clearTrack();
116 
122  int writeTrack(ofstream *trsp, bool incltime);
123 
125  void addVertex(Vertex *x);
126 
132  Vertex *getVertex(int v);
133 
136  return m_nVertices;
137  };
138 
141  m_pInfo = p;
142  };
143 
146  return m_pInfo;
147  };
148 
149  void setTimeIndex(EGS_Float time) {
150  time_index = time;
151  };
152 
154  EGS_Float getTimeIndex() {
155  return time_index;
156  };
157 
158 protected:
159 
161  void grow();
162 
164  EGS_Float time_index;
165 
166  int m_size;
169 };
170 
182 
183 public:
184 
186  EGS_ParticleTrackContainer() : m_nEvents(0), m_nTracks(0),
187  m_totalTracks(0), m_stackMap(NULL), m_isScoring(NULL), m_bufferSize(0),
188  m_buffer(NULL), m_trspFile(NULL) {};
189 
197  EGS_ParticleTrackContainer(const char *fname, int buf_size, bool time_bool) : m_nEvents(0),
198  m_nTracks(0), m_totalTracks(0), m_isScoring(NULL), m_bufferSize(0), m_buffer(0),
199  m_trspFile(NULL) {
200  m_bufferSize = buf_size;
201  strncpy(head_inctime, "include time index=", 20);
202 
203  // Initialize the arrays. The incltime variable set from the time_bool
204  // passed from the constructor (track scoring object gets incltime from
205  // input file and calls and passes)
206  incltime = time_bool;
207  m_buffer = new EGS_ParticleTrack* [m_bufferSize];
208  m_stackMap = new int [m_bufferSize];
209  m_isScoring = new bool [m_bufferSize];
210  for (int i = 0; i < m_bufferSize; ++i) {
211  m_buffer[i] = new EGS_ParticleTrack();
212  m_stackMap[i] = -1;
213  m_isScoring[i] = false;
214  }
215 
216  // open output file and write header
217  m_trspFilename = string(fname);
218  m_trspFile = new ofstream(m_trspFilename.c_str(), ios::binary);
219 
220  // Now the file starts with whether or not the time index is included in the data per track
221  // E.g. "include time index=1" for true
222  m_trspFile->write(head_inctime, sizeof(head_inctime));
223  m_trspFile->write((char *)&incltime, sizeof(bool));
224 
225  int dummy = 0;
226  // at the end this will be replaced with the number of recorded tracks
227  m_trspFile->write((char *)&dummy, sizeof(int));
228 
229  };
230 
233  // if any particles are left in memory -> flush them to the file
234  if (m_nTracks > 0) {
235  flushBuffer();
236  }
237 
238  if (m_buffer) {
239  for (int i = 0; i < m_bufferSize; i++) {
240  if (m_buffer[i]) {
241  delete m_buffer[i];
242  }
243  m_buffer[i] = NULL;
244  }
245  delete [] m_buffer;
246  m_buffer = NULL;
247  }
248  if (m_stackMap) {
249  delete [] m_stackMap;
250  }
251  if (m_isScoring) {
252  delete [] m_isScoring;
253  }
254  if (m_trspFile) {
255  delete m_trspFile;
256  }
257  m_trspFile = NULL;
258  };
259 
261  void setEvents(int e) {
262  m_nEvents = e;
263  }
264 
266  int getEvents() {
267  return m_nEvents;
268  }
269 
272  return (m_nTracks > 0) ? m_buffer[m_nTracks-1]->getNumVertices() : 0;
273  };
274 
276  void addVertex(EGS_ParticleTrack::Vertex *x);
277 
284  void addVertex(int stackIndex, EGS_ParticleTrack::Vertex *x);
285 
289  EGS_ParticleTrack::Vertex *getTrackVertex(int tr, int v);
290 
293  return m_isScoring[m_nTracks-1];
294  }
295 
297  bool isScoringParticle(int stackIndex) {
298  if (m_stackMap[stackIndex] < 0) {
299  return false;
300  }
301  return m_isScoring[m_stackMap[stackIndex]];
302  }
303 
306  m_isScoring[m_nTracks-1] = false;
307  }
308 
310  void stopScoringParticle(int stackIndex) {
311  if (m_stackMap[stackIndex] >= 0) {
312  m_isScoring[m_stackMap[stackIndex]] = false;
313  }
314  m_stackMap[stackIndex] = -1;
315  }
316 
318  void startNewTrack();
319 
327  void startNewTrack(int stackIndex);
328 
330  void startNewTrack(EGS_ParticleTrack::ParticleInfo *p);
331 
334  if (m_stackMap[stackIndex] >= 0) {
335  m_buffer[m_stackMap[stackIndex]]->setParticleInfo(p);
336  }
337  }
338 
341  m_buffer[m_nTracks-1]->setParticleInfo(p);
342  }
343 
344  void setCurrentTimeIndex(EGS_Float time) {
345  m_buffer[m_nTracks-1]->setTimeIndex(time);
346  }
347 
349  int readDataFile(const char *filename);
350 
354  void reportResults(bool with_header = true);
355 
356  void tracksFileSort();
357 
358 protected:
359 
366  void flushBuffer();
367 
372  void updateHeader();
373 
374  bool incltime;
375  int m_nEvents;
376  int m_nTracks;
378 
379  int *m_stackMap;
382  bool *m_isScoring;
387  ofstream *m_trspFile;
388  string m_trspFilename;
389 
390  char head_inctime[20];
391 };
392 
393 #endif
A class that stores all the tracks in a simulation.
string m_trspFilename
filename of output file
void setParticleInfo(int stackIndex, EGS_ParticleTrack::ParticleInfo *p)
Set the type of mapped particle to p .
~EGS_ParticleTrackContainer()
The Destructor. Deallocate all allocated memory.
bool isScoringParticle(int stackIndex)
Are we still scoring the particle mapped by the stack?
EGS_ParticleTrack ** m_buffer
the tracks array
EGS_ParticleTrackContainer()
Basic Constructor. Initializes all variables.
int getCurrentNumVertices()
Get the number of vertices in the track currently being scorred.
int m_nEvents
number of events scored
void setEvents(int e)
Save the number of events (for example, decays) tracked so far.
int m_nTracks
number of tracks currently in memory
void stopScoringParticle()
Stop scoring the current particle.
int m_totalTracks
total number of tracks registered
bool isScoringParticle()
Are we still scoring the current particle?
int m_bufferSize
max number of tracks in memory
void stopScoringParticle(int stackIndex)
Stop scoring the particle mapped by the stack.
ofstream * m_trspFile
the file to which data is output
void setCurrentParticleInfo(EGS_ParticleTrack::ParticleInfo *p)
Set the type of the currently tracked particle to p .
EGS_ParticleTrackContainer(const char *fname, int buf_size, bool time_bool)
Constructor.
int getEvents()
Get the number of events tracked so far.
A class representing a single track of a particle.
EGS_ParticleTrack()
Initialize the track and allocate minimum memory.
Vertex ** m_track
the array with the vertices
EGS_Float getTimeIndex()
Get the time index of the particle being tracked.
int m_nVertices
current number of vertices in track
void setParticleInfo(ParticleInfo *p)
Define the type of the particle being tracked.
ParticleInfo * getParticleInfo()
Get the type of the particle being tracked.
~EGS_ParticleTrack()
The destructor. Deallocate all memory.
ParticleInfo * m_pInfo
type of the tracked particle
int m_size
current size of the vertex array
int getNumVertices()
Get number of vertices currently in the track.
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float y
y-component
Definition: egs_vector.h:62
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_Float x
x-component
Definition: egs_vector.h:61
Global egspp functions header file.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.
Structure describing the particle being tracked.
Structure to store the data for each interaction along the track.
EGS_Float e
energy of the particle in this vertex
EGS_Vector x
interaction (vertex) coordinates