EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_dose_scoring.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ dose scoring object 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: Ernesto Mainegra-Hing, 2012
25 #
26 # Contributors: Reid Townson
27 # Blake Walters
28 #
29 ###############################################################################
30 #
31 # A general dose calculation tool. Since it is outside the scope of the
32 # EGS_Application class, a few utility methods are needed in EGS_Application
33 # to retrieve information regarding media, geometry and source.
34 #
35 # Dose is output for each dose region without any spacial information. For a
36 # more detailed dose scoring grid it is better to create a user code which
37 # could produce 3D dose distributions and graph data.
38 #
39 # Can be used in any C++ user code by entering the proper input block in
40 # the ausgab object definition block.
41 #
42 # Prints out dose in each dose scoring region.
43 #
44 # Dose scoring regions can be defined by input using one the following
45 # syntax for individual regions:
46 #
47 # :start ausgab object definition:
48 # :start ausgab object:
49 # library = egs_dose_scoring
50 # name = some_name
51 # volume = V0 V1 ... Vn
52 # dose regions = IR0 IR1 ... IRn
53 # :stop ausgab object:
54 # :stop ausgab object definition:
55 #
56 # or by groups of consecutive regions:
57 #
58 # :start ausgab object definition:
59 # :start ausgab object:
60 # library = egs_dose_scoring
61 # name = some_name
62 # volume = V0 V1 ... Vn
63 # dose start region = IRI_0 ... IRI_N
64 # dose stop region = IRE_0 ... IRE_N
65 # :stop ausgab object:
66 # :stop ausgab object definition:
67 #
68 # where one can have same number of volume entries as group of regions (n=N)
69 # or same number of volume entries as number of individual regions. If there
70 # are more dose scoring regions than volume entries, a default volume is use for
71 # those regions without volume information. This default can be either the first
72 # volume entry or 1 g/cm3.
73 #
74 # TODO:
75 #
76 # - Classify dose as contributed by primary or scattered particles
77 # - Custom classification using latch
78 # - Dose to medium calculation
79 #
80 ###############################################################################
81 */
82 
83 
89 #ifndef EGS_DOSE_SCORING_
90 #define EGS_DOSE_SCORING_
91 
92 #include "egs_ausgab_object.h"
93 #include "egs_application.h"
94 #include "egs_scoring.h"
95 #include "egs_base_geometry.h"
96 
97 #ifdef WIN32
98 
99  #ifdef BUILD_DOSE_SCORING_DLL
100  #define EGS_DOSE_SCORING_EXPORT __declspec(dllexport)
101  #else
102  #define EGS_DOSE_SCORING_EXPORT __declspec(dllimport)
103  #endif
104  #define EGS_DOSE_SCORING_LOCAL
105 
106 #else
107 
108  #ifdef HAVE_VISIBILITY
109  #define EGS_DOSE_SCORING_EXPORT __attribute__ ((visibility ("default")))
110  #define EGS_DOSE_SCORING_LOCAL __attribute__ ((visibility ("hidden")))
111  #else
112  #define EGS_DOSE_SCORING_EXPORT
113  #define EGS_DOSE_SCORING_LOCAL
114  #endif
115 
116 #endif
117 
193 class EGS_DOSE_SCORING_EXPORT EGS_DoseScoring : public EGS_AusgabObject {
194 
195 public:
196 
197  EGS_DoseScoring(const string &Name="", EGS_ObjectFactory *f = 0);
198 
199  ~EGS_DoseScoring();
200 
202 
203  int ir = app->top_p.ir, imed = ir>=0 ? app->getMedium(ir):-1;
204  EGS_Float edep = app->getEdep();
205 
206  /**** energy deposition in a medium ***/
207  if (iarg <= 4 && imed >= 0 && edep > 0 && doseM) {
208  doseM->score(imed, edep*app->top_p.wt);
209  }
210 
211  //score in file array if requested
212  if (ir >=0 && doseF && iarg <=4 && df_reg[ir] >= 0 && edep) {
213  doseF->score(df_reg[ir], edep*app->top_p.wt);
214  }
215 
216  /*** Check if scoring in current region ***/
217  if (ir >= 0 && dose) {
218  if (d_reg_index[ir]<0) {
219  return 0;
220  }
221  }
222 
223  /**** energy deposition in current region ***/
224  if (iarg <= 4 && ir >= 0 && edep > 0 && dose) {
225  dose->score(d_reg_index[ir], edep*app->top_p.wt);
226  }
227  return 0;
228  };
229 
230  int processEvent(EGS_Application::AusgabCall iarg, int ir) {
231 
232  if (ir == -1) {
233  ir = app->top_p.ir;
234  }
235 
236  int imed = ir>=0 ? app->getMedium(ir):-1;
237  EGS_Float edep = app->getEdep();
238 
239  /**** energy deposition in a medium ***/
240  if (iarg <= 4 && imed >= 0 && edep > 0 && doseM) {
241  doseM->score(imed, edep*app->top_p.wt);
242  }
243 
244  //score in file array if requested
245  if (ir >= 0 && doseF && iarg <=4 && df_reg[ir] >= 0 && edep) {
246  doseF->score(df_reg[ir], edep*app->top_p.wt);
247  }
248 
249  /*** Check if scoring in current region ***/
250  if (ir >= 0 && dose) {
251  if (d_reg_index[ir]<0) {
252  return 0;
253  }
254  }
255 
256  /**** energy deposition in current region ***/
257  if (iarg <= 4 && ir >= 0 && edep > 0 && dose) {
258  dose->score(d_reg_index[ir], edep*app->top_p.wt);
259  }
260  return 0;
261  };
262 
263  bool needsCall(EGS_Application::AusgabCall iarg) const {
264  if (iarg <= 4) {
265  return true;
266  }
267  else {
268  return false;
269  }
270  };
271 
272  void setApplication(EGS_Application *App);
273 
274  void getNumberRegions(const string &str, vector<int> &regs);
275 
276  void getLabelRegions(const string &str, vector<int> &regs);
277 
278  void reportResults();
279 
280  void setCurrentCase(EGS_I64 ncase) {
281  if (ncase != m_lastCase) {
282  m_lastCase = ncase;
283  if (dose) {
284  dose->setHistory(ncase);
285  }
286  if (doseM) {
287  doseM->setHistory(ncase);
288  }
289  if (doseF) {
290  doseF->setHistory(ncase);
291  }
292  }
293  };
294  int getDigits(int i) {
295  int imax = 10;
296  while (i>=imax) {
297  imax*=10;
298  }
299  return (int)log10((float)imax);
300  };
301 
302  EGS_Float getRealRho(int ireg) {
303  int med = dose_geom->medium(ireg);
304  return dose_geom->getRelativeRho(ireg)*app->getMediumRho(med);
305  }
306 
307  void setVol(const vector<EGS_Float> volin) {
308  vol_list=volin;
309  };
310  void setVol(const EGS_Float volin) {
311  vol_list.push_back(volin);
312  };
313  void setDoseRegions(const vector <int> d_reg) {
314  d_region=d_reg;
315  };
316  void setDoseRegions(const string d_reg) {
317  d_regionString=d_reg;
318  };
319  void setMediumScoring(bool flag) {
320  score_medium_dose=flag;
321  };
322  void setRegionScoring(bool flag) {
323  score_region_dose=flag;
324  };
325  void setOutputFile(bool flag, EGS_BaseGeometry *dgeom, int ftype) {
326  output_dose_file=flag;
327  dose_geom= dgeom;
328  file_type = ftype;
329  };
330  bool getOutputFile(EGS_BaseGeometry *&dgeom, int &ftype) {
331  dgeom = dose_geom;
332  ftype = file_type;
333  return output_dose_file;
334  };
335  void setUserNorm(const EGS_Float &normi) {
336  norm_u=normi;
337  };
338  void outputDoseFile(const EGS_Float &normD);
339 
340  bool storeState(ostream &data) const;
341  bool setState(istream &data);
342  void resetCounter();
343  bool addState(istream &data);
344  int addTheStates(istream &data);
345 
346 protected:
347 
350  vector <EGS_Float> vol_list; // Input list of region volumes
351  vector <int> d_region; // Input list of dose scoring regions d_reg[i] = ir
352  string d_regionString;
353  vector <int> d_reg_index; // list index for dose scoring regions d_reg_index[ir]= 0..d_reg.size()-1
354  vector <EGS_Float> vol; // geometrical region volumes
355  EGS_Float norm_u;
356  int nreg, // number of regions in the geometry
357  nmedia; // number of media in the input file
358  int max_dreg, // maximum dose region number
359  max_medl; // maximum medium name length
360  EGS_I64 m_lastCase;
361  bool score_medium_dose,
362  score_region_dose;
363 
364  EGS_BaseGeometry *dose_geom; //EGS_XYZGeometry for which to output dose to file
366  vector<int> df_reg; //array mapping global reg. no. onto reg. no. in EGS_XYZGeometry
367  bool output_dose_file; //set to true if outputting a dose file
368  int file_type; //output file type--currently only .3ddose (file_type=0) supported
369  string df_name; //output file name--put here for convenience sake
370 };
371 
372 #endif
A class for scoring an array of quantities (e.g. a dose distribution) in a Monte Carlo simulation...
Definition: egs_scoring.h:219
EGS_ScoringArray * doseM
Scoring dose in each medium.
EGS_AusgabObject interface class header file.
virtual int processEvent(EGS_Application::AusgabCall iarg)=0
Process an ausgab call for event iarg.
A dose scoring object: header.
virtual bool storeState(ostream &data_out) const
Store the source state into the stream data_out.
EGS_I64 m_lastCase
The event set via setCurrentCase()
AusgabCall
Possible calls to the user scoring function ausgab().
virtual void resetCounter()
Reset the ausgab object state.
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
virtual bool needsCall(EGS_Application::AusgabCall iarg) const
Is the ausgab call iarg relevant for this object?
EGS_ScoringArray * dose
Scoring in each dose scoring region.
virtual bool setState(istream &data_in)
Set the ausgab object state based on data from the stream data_in.
EGS_ScoringArray * doseF
Scoring dose in each voxel in EGS_XYZGeometry.
virtual void reportResults()
Report results.
An object factory.
virtual bool addState(istream &data_in)
Add data from the stream data_in to the ausgab object state.
EGS_ScoringSingle and EGS_ScoringArray class header file.
EGS_BaseGeometry class header file.
virtual void setCurrentCase(EGS_I64 ncase)
Set the current event.
virtual void setApplication(EGS_Application *App)
Set the application this object belongs to.
EGS_Application class header file.
Base class for advanced EGSnrc C++ applications.