EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_scoring.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ scoring 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: Iwan Kawrakow, 2005
25 #
26 # Contributors: Reid Townson
27 #
28 ###############################################################################
29 */
30 
31 
37 #ifndef EGS_SCORING_
38 #define EGS_SCORING_
39 
40 #include "egs_libconfig.h"
41 #include "egs_functions.h"
42 #include "egs_math.h"
43 
44 #include <iostream>
45 using namespace std;
46 
64 
65 public:
66 
68  EGS_ScoringSingle() : sum(0), sum2(0), tmp(0), current_ncase(0) {};
69 
78  inline void score(unsigned short ncase, EGS_Float f) {
79  if (ncase == current_ncase) {
80  tmp += f;
81  }
82  else {
83  finishCase(ncase,f);
84  }
85  };
86 
90  inline void finishCase(unsigned short new_case, EGS_Float new_result) {
91  current_ncase = new_case;
92  sum += tmp;
93  sum2 += tmp*tmp;
94  tmp = new_result;
95  };
96 
98  EGS_Float currentScore() const {
99  return tmp;
100  };
101 
104  void currentScore(EGS_Float &s, unsigned short &ncase) const {
105  s = tmp;
106  ncase = current_ncase;
107  };
108 
111  void currentScore(double &s, double &s2) {
112  s=sum;
113  s2=sum2;
114  };
115 
122  void currentResult(EGS_I64 ncase, double &r, double &dr) {
123  r = sum + tmp;
124  dr = sum2 + tmp*tmp;
125  r /= ncase;
126  dr /= ncase;
127  dr -= r*r;
128  if (dr > 0) {
129  dr = sqrt(dr/(ncase-1));
130  }
131  };
132 
144  bool storeState(ostream &data) {
145  //sum += tmp; sum2 += tmp*tmp; tmp = 0;
146  //data << current_ncase << " " << sum << " " << sum2 << endl;
147  data << current_ncase << " " << sum+tmp << " " << sum2+tmp *tmp
148  << "\n";
149  return data.good();
150  };
151 
161  bool setState(istream &data) {
162  data >> current_ncase >> sum >> sum2;
163  tmp = 0;
164  return data.good();
165  };
166 
170  void reset() {
171  current_ncase = 0;
172  tmp = 0;
173  sum = 0;
174  sum2 = 0;
175  };
176 
185  sum += tmp + x.sum + x.tmp;
186  sum2 += tmp*tmp + x.sum2 + x.tmp*x.tmp;
187  current_ncase = 0;
188  tmp = 0;
189  return *this;
190  };
191 
192 
193 protected:
194 
196  double sum;
198  double sum2;
200  EGS_Float tmp;
202  unsigned short current_ncase;
203 
204 };
205 
220 
221 public:
222 
227  EGS_ScoringArray(int N);
228 
230  ~EGS_ScoringArray();
231 
237  void setHistory(EGS_I64 ncase);
238 
244  inline void score(int ireg, EGS_Float f) {
245  result[ireg].score(current_ncase_short,f);
246  };
247 
254  EGS_Float currentScore(int ireg) const {
255  return result[ireg].currentScore();
256  };
257 
259  EGS_Float thisHistoryScore(int ireg) const {
260  EGS_Float res;
261  unsigned short nc;
262  result[ireg].currentScore(res,nc);
263  return nc == current_ncase_short ? res : 0;
264  };
265 
271  void currentScore(int ireg, double &s, double &s2) {
272  result[ireg].currentScore(s,s2);
273  };
274 
280  void currentResult(int ireg, double &r, double &dr) {
281  result[ireg].currentResult(current_ncase,r,dr);
282  };
283 
302  void reportResults(double norm, const char *title, bool relative_error,
303  const char *format = 0);
304 
316  bool storeState(ostream &data) {
317  data << nreg << " " << current_ncase_short << "\n";
318  if (!egsStoreI64(data,current_ncase)) {
319  return false;
320  }
321  if (!egsStoreI64(data,current_ncase_65536)) {
322  return false;
323  }
324  data << "\n";
325  for (int j=0; j<nreg; j++) {
326  if (!result[j].storeState(data)) {
327  return false;
328  }
329  }
330  return true;
331  };
332 
340  bool setState(istream &data) {
341  int nreg1;
342  data >> nreg1 >> current_ncase_short;
343  if (!data.good() || nreg1 < 1) {
344  return false;
345  }
346  if (!egsGetI64(data,current_ncase)) {
347  return false;
348  }
349  if (!egsGetI64(data,current_ncase_65536)) {
350  return false;
351  }
352  if (nreg1 != nreg) {
353  if (nreg > 0) {
354  delete [] result;
355  }
356  nreg = nreg1;
357  result = new EGS_ScoringSingle [nreg];
358  }
359  for (int j=0; j<nreg; j++) {
360  if (!result[j].setState(data)) {
361  return false;
362  }
363  }
364  return true;
365  };
366 
368  void reset() {
369  current_ncase = 0;
370  current_ncase_65536 = 0;
371  current_ncase_short = 0;
372  for (int j=0; j<nreg; j++) {
373  result[j].reset();
374  }
375  };
376 
384  current_ncase += x.current_ncase;
385  current_ncase_65536 = current_ncase >> 16;
386  EGS_I64 aux = current_ncase - (current_ncase_65536 << 16);
387  current_ncase_short = (unsigned short) aux;
388  for (int j=0; j<nreg; j++) {
389  result[j] += x.result[j];
390  }
391  return *this;
392  };
393 
398  int bins() const {
399  return nreg;
400  };
401 
406  int regions() const {
407  return nreg;
408  };
409 
410 protected:
411 
413  EGS_I64 current_ncase;
418  int nreg;
425  unsigned short current_ncase_short;
426 
427 };
428 
429 #endif
A class for scoring an array of quantities (e.g. a dose distribution) in a Monte Carlo simulation.
Definition: egs_scoring.h:219
int bins() const
Returns the number of bins (or elements or regions, the most appropriate term depending on the way th...
Definition: egs_scoring.h:398
EGS_I64 current_ncase_65536
Definition: egs_scoring.h:415
EGS_Float thisHistoryScore(int ireg) const
Returns the score in ireg in the current event.
Definition: egs_scoring.h:259
int regions() const
Returns the number of regions (or elements or bins, the most appropriate term depending on the way th...
Definition: egs_scoring.h:406
EGS_I64 current_ncase
Definition: egs_scoring.h:408
void currentScore(int ireg, double &s, double &s2)
Sets s and s2 to the sum of scores and sum of scores squared in element ireg.
Definition: egs_scoring.h:271
EGS_Float currentScore(int ireg) const
Returns the score in element ireg from the last statistically indepent event that contributed to ireg...
Definition: egs_scoring.h:254
bool storeState(ostream &data)
Stores the state of the scoring array object into the data stream data.
Definition: egs_scoring.h:316
void score(int ireg, EGS_Float f)
Add f to the score in the element ireg.
Definition: egs_scoring.h:244
void currentResult(int ireg, double &r, double &dr)
Sets r to the result in region ireg and dr to its statistical uncertainty.
Definition: egs_scoring.h:280
EGS_ScoringArray & operator+=(const EGS_ScoringArray &x)
Add the results of x to the rtesults of the invoking object.
Definition: egs_scoring.h:383
void reset()
Reset the scoring array to a pristine state.
Definition: egs_scoring.h:368
EGS_ScoringSingle * result
Definition: egs_scoring.h:420
bool setState(istream &data)
Sets the state fof the scoring array object from the data in the input stream data.
Definition: egs_scoring.h:340
unsigned short current_ncase_short
Definition: egs_scoring.h:425
A class for scoring a single quantity of interest in a Monte Carlo simulation.
Definition: egs_scoring.h:63
bool setState(istream &data)
Set the state of the scoring object from the data stream data.
Definition: egs_scoring.h:161
unsigned short current_ncase
Definition: egs_scoring.h:202
bool storeState(ostream &data)
Stores the state of the scoring object into the data stream data. Returns true on success,...
Definition: egs_scoring.h:144
EGS_ScoringSingle()
Construct a scoring object initialized to zero.
Definition: egs_scoring.h:68
void currentScore(double &s, double &s2)
Sets s to the sum of scores collected so far and s2 to the sum of scores squared.
Definition: egs_scoring.h:111
EGS_Float currentScore() const
Returns the score of the current event.
Definition: egs_scoring.h:98
void score(unsigned short ncase, EGS_Float f)
Add f to the score of the ncase'th statistically independent event.
Definition: egs_scoring.h:78
EGS_ScoringSingle & operator+=(const EGS_ScoringSingle &x)
Combine the results of two scoring objects.
Definition: egs_scoring.h:184
void reset()
Reset the scoring object to a pristine state (i.e. all counters set to zero).
Definition: egs_scoring.h:170
void currentScore(EGS_Float &s, unsigned short &ncase) const
Sets s to the score of the current event and ncase to the index of the current event.
Definition: egs_scoring.h:104
void finishCase(unsigned short new_case, EGS_Float new_result)
Finish the current 'case' (event) and start a new event with index new_case and a score of new_result...
Definition: egs_scoring.h:90
void currentResult(EGS_I64 ncase, double &r, double &dr)
Sets r to the current result and dr to its statistical uncertainty assuming ncase statistically indep...
Definition: egs_scoring.h:122
Global egspp functions header file.
Defines the EGS_EXPORT and EGS_LOCAL macros.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
Attempts to fix broken math header files.
bool EGS_EXPORT egsStoreI64(ostream &data, EGS_I64 n)
Writes the 64 bit integer n to the output stream data and returns true on success,...
bool EGS_EXPORT egsGetI64(istream &data, EGS_I64 &n)
Reads a 64 bit integer from the stream data and assigns it to n. Returns true on success,...