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 #
28 ###############################################################################
29 */
30 
31 
37 #ifndef EGS_PARTICLE_TRACK_
38 #define EGS_PARTICLE_TRACK_
39 
40 #include "egs_vector.h"
41 #include "egs_config1.h"
42 #include "egs_functions.h"
43 
44 #include <fstream>
45 using namespace std;
46 
58 
59 public:
60 
64  struct Vertex {
66  EGS_Float e;
67 
68  Vertex() : x(0,0,0), e(0) {};
69  Vertex(EGS_Float px, EGS_Float py, EGS_Float pz) {
70  x.x = px;
71  x.y = py;
72  x.z = pz;
73  e = 1;
74  };
75  Vertex(EGS_Float px, EGS_Float py, EGS_Float pz, EGS_Float pe) {
76  x.x = px;
77  x.y = py;
78  x.z = pz;
79  e = pe;
80  };
81  Vertex(const EGS_Vector &px, EGS_Float pe) : x(px), e(pe) {};
82  };
83 
85  struct ParticleInfo {
86  EGS_I32 q;
87  ParticleInfo(EGS_I32 pq) {
88  q = pq;
89  };
90  ~ParticleInfo() { };
91  };
92 
94  EGS_ParticleTrack() : m_pInfo(NULL), m_size(16), m_nVertices(0) {
95  m_track = new Vertex* [m_size];
96  }
97 
100  clearTrack();
101  if (m_track) {
102  delete [] m_track;
103  }
104  m_track = NULL;
105  };
106 
112  void clearTrack();
113 
119  int writeTrack(ofstream *trsp);
120 
122  void addVertex(Vertex *x);
123 
129  Vertex *getVertex(int v);
130 
133  return m_nVertices;
134  };
135 
138  m_pInfo = p;
139  };
140 
143  return m_pInfo;
144  };
145 
146 protected:
147 
149  void grow();
150 
152 
153  int m_size;
156 };
157 
169 
170 public:
171 
173  EGS_ParticleTrackContainer() : m_nEvents(0), m_nTracks(0),
174  m_totalTracks(0), m_stackMap(NULL), m_isScoring(NULL), m_bufferSize(0),
175  m_buffer(NULL), m_trspFile(NULL) {};
176 
184  EGS_ParticleTrackContainer(const char *fname, int buf_size) : m_nEvents(0),
185  m_nTracks(0), m_totalTracks(0), m_isScoring(NULL), m_bufferSize(0), m_buffer(0),
186  m_trspFile(NULL) {
187  m_bufferSize = buf_size;
188 
189  // initialize the arrays
190  m_buffer = new EGS_ParticleTrack* [m_bufferSize];
191  m_stackMap = new int [m_bufferSize];
192  m_isScoring = new bool [m_bufferSize];
193  for (int i = 0; i < m_bufferSize; ++i) {
194  m_buffer[i] = new EGS_ParticleTrack();
195  m_stackMap[i] = -1;
196  m_isScoring[i] = false;
197  }
198 
199  // open output file and write header
200  m_trspFilename = string(fname);
201  m_trspFile = new ofstream(m_trspFilename.c_str(), ios::binary);
202  int dummy = 0;
203  // at the end this will be replaced with the number of recorded tracks
204  m_trspFile->write((char *)&dummy, sizeof(int));
205  };
206 
209  // if any particles are left in memory -> flush them to the file
210  if (m_nTracks > 0) {
211  flushBuffer();
212  }
213 
214  if (m_buffer) {
215  for (int i = 0; i < m_bufferSize; i++) {
216  if (m_buffer[i]) {
217  delete m_buffer[i];
218  }
219  m_buffer[i] = NULL;
220  }
221  delete [] m_buffer;
222  m_buffer = NULL;
223  }
224  if (m_stackMap) {
225  delete [] m_stackMap;
226  }
227  if (m_isScoring) {
228  delete [] m_isScoring;
229  }
230  if (m_trspFile) {
231  delete m_trspFile;
232  }
233  m_trspFile = NULL;
234  };
235 
237  void setEvents(int e) {
238  m_nEvents = e;
239  }
240 
242  int getEvents() {
243  return m_nEvents;
244  }
245 
248  return (m_nTracks > 0) ? m_buffer[m_nTracks-1]->getNumVertices() : 0;
249  };
250 
252  void addVertex(EGS_ParticleTrack::Vertex *x);
253 
260  void addVertex(int stackIndex, EGS_ParticleTrack::Vertex *x);
261 
265  EGS_ParticleTrack::Vertex *getTrackVertex(int tr, int v);
266 
269  return m_isScoring[m_nTracks-1];
270  }
271 
273  bool isScoringParticle(int stackIndex) {
274  if (m_stackMap[stackIndex] < 0) {
275  return false;
276  }
277  return m_isScoring[m_stackMap[stackIndex]];
278  }
279 
282  m_isScoring[m_nTracks-1] = false;
283  }
284 
286  void stopScoringParticle(int stackIndex) {
287  if (m_stackMap[stackIndex] >= 0) {
288  m_isScoring[m_stackMap[stackIndex]] = false;
289  }
290  m_stackMap[stackIndex] = -1;
291  }
292 
294  void startNewTrack();
295 
303  void startNewTrack(int stackIndex);
304 
306  void startNewTrack(EGS_ParticleTrack::ParticleInfo *p);
307 
310  if (m_stackMap[stackIndex] >= 0) {
311  m_buffer[m_stackMap[stackIndex]]->setParticleInfo(p);
312  }
313  }
314 
317  m_buffer[m_nTracks-1]->setParticleInfo(p);
318  }
319 
321  int readDataFile(const char *filename);
322 
326  void reportResults(bool with_header = true);
327 
328 protected:
329 
336  void flushBuffer();
337 
342  void updateHeader();
343 
344  int m_nEvents;
345  int m_nTracks;
347 
348  int *m_stackMap;
351  bool *m_isScoring;
356  ofstream *m_trspFile;
357  string m_trspFilename;
358 };
359 
360 #endif
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
ofstream * m_trspFile
the file to which data is output
EGS_ParticleTrack()
Initialize the track and allocate minimum memory.
int m_totalTracks
total number of tracks registered
void setCurrentParticleInfo(EGS_ParticleTrack::ParticleInfo *p)
Set the type of the currently tracked particle to p .
EGS_Vector x
interaction (vertex) coordinates
void setEvents(int e)
Save the number of events (for example, decays) tracked so far.
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.
EGS_Float y
y-component
Definition: egs_vector.h:61
void stopScoringParticle()
Stop scoring the current particle.
A class representing a single track of a particle.
A class representing 3D vectors.
Definition: egs_vector.h:56
Global egspp functions header file.
ParticleInfo * m_pInfo
type of the tracked particle
void setParticleInfo(int stackIndex, EGS_ParticleTrack::ParticleInfo *p)
Set the type of mapped particle to p .
ParticleInfo * getParticleInfo()
Get the type of the particle being tracked.
int m_nEvents
number of events scored
int getEvents()
Get the number of events tracked so far.
void setParticleInfo(ParticleInfo *p)
Define the type of the particle being tracked.
int m_size
current size of the vertex array
Structure describing the particle being tracked.
Structure to store the data for each interaction along the track.
EGS_ParticleTrackContainer(const char *fname, int buf_size)
Constructor.
A class that stores all the tracks in a simulation.
EGS_ParticleTrack ** m_buffer
the tracks array
int getCurrentNumVertices()
Get the number of vertices in the track currently being scorred.
bool isScoringParticle()
Are we still scoring the current particle?
int m_nTracks
number of tracks currently in memory
int m_bufferSize
max number of tracks in memory
void stopScoringParticle(int stackIndex)
Stop scoring the particle mapped by the stack.
~EGS_ParticleTrackContainer()
The Destructor. Deallocate all allocated memory.
int getNumVertices()
Get number of vertices currently in the track.
EGS_Float e
energy of the particle in this vertex
~EGS_ParticleTrack()
The destructor. Deallocate all memory.
EGS_ParticleTrackContainer()
Basic Constructor. Initializes all variables.
string m_trspFilename
filename of output file
bool isScoringParticle(int stackIndex)
Are we still scoring the particle mapped by the stack?
Vertex ** m_track
the array with the vertices
int m_nVertices
current number of vertices in track