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 #include "egs_input_struct.h"
44 
45 #include <iostream>
46 using namespace std;
47 
65 
66 public:
67 
69  EGS_ScoringSingle() : sum(0), sum2(0), tmp(0), current_ncase(0) {};
70 
79  inline void score(unsigned short ncase, EGS_Float f) {
80  if (ncase == current_ncase) {
81  tmp += f;
82  }
83  else {
84  finishCase(ncase,f);
85  }
86  };
87 
91  inline void finishCase(unsigned short new_case, EGS_Float new_result) {
92  current_ncase = new_case;
93  sum += tmp;
94  sum2 += tmp*tmp;
95  tmp = new_result;
96  };
97 
99  EGS_Float currentScore() const {
100  return tmp;
101  };
102 
105  void currentScore(EGS_Float &s, unsigned short &ncase) const {
106  s = tmp;
107  ncase = current_ncase;
108  };
109 
112  void currentScore(double &s, double &s2) {
113  s=sum;
114  s2=sum2;
115  };
116 
123  void currentResult(EGS_I64 ncase, double &r, double &dr) {
124  r = sum + tmp;
125  dr = sum2 + tmp*tmp;
126  r /= ncase;
127  dr /= ncase;
128  dr -= r*r;
129  if (dr > 0) {
130  dr = sqrt(dr/(ncase-1));
131  }
132  };
133 
145  bool storeState(ostream &data) {
146  //sum += tmp; sum2 += tmp*tmp; tmp = 0;
147  //data << current_ncase << " " << sum << " " << sum2 << endl;
148  data << current_ncase << " " << sum+tmp << " " << sum2+tmp *tmp
149  << "\n";
150  return data.good();
151  };
152 
162  bool setState(istream &data) {
163  data >> current_ncase >> sum >> sum2;
164  tmp = 0;
165  return data.good();
166  };
167 
171  void reset() {
172  current_ncase = 0;
173  tmp = 0;
174  sum = 0;
175  sum2 = 0;
176  };
177 
186  sum += tmp + x.sum + x.tmp;
187  sum2 += tmp*tmp + x.sum2 + x.tmp*x.tmp;
188  current_ncase = 0;
189  tmp = 0;
190  return *this;
191  };
192 
193 
194 protected:
195 
197  double sum;
199  double sum2;
201  EGS_Float tmp;
203  unsigned short current_ncase;
204 
205 };
206 
221 
222 public:
223 
228  EGS_ScoringArray(int N);
229 
231  ~EGS_ScoringArray();
232 
238  void setHistory(EGS_I64 ncase);
239 
245  inline void score(int ireg, EGS_Float f) {
246  result[ireg].score(current_ncase_short,f);
247  };
248 
255  EGS_Float currentScore(int ireg) const {
256  return result[ireg].currentScore();
257  };
258 
260  EGS_Float thisHistoryScore(int ireg) const {
261  EGS_Float res;
262  unsigned short nc;
263  result[ireg].currentScore(res,nc);
264  return nc == current_ncase_short ? res : 0;
265  };
266 
272  void currentScore(int ireg, double &s, double &s2) {
273  result[ireg].currentScore(s,s2);
274  };
275 
281  void currentResult(int ireg, double &r, double &dr) {
282  result[ireg].currentResult(current_ncase,r,dr);
283  };
284 
303  void reportResults(double norm, const char *title, bool relative_error,
304  const char *format = 0);
305 
317  bool storeState(ostream &data) {
318  data << nreg << " " << current_ncase_short << "\n";
319  if (!egsStoreI64(data,current_ncase)) {
320  return false;
321  }
322  if (!egsStoreI64(data,current_ncase_65536)) {
323  return false;
324  }
325  data << "\n";
326  for (int j=0; j<nreg; j++) {
327  if (!result[j].storeState(data)) {
328  return false;
329  }
330  }
331  return true;
332  };
333 
341  bool setState(istream &data) {
342  int nreg1;
343  data >> nreg1 >> current_ncase_short;
344  if (!data.good() || nreg1 < 1) {
345  return false;
346  }
347  if (!egsGetI64(data,current_ncase)) {
348  return false;
349  }
350  if (!egsGetI64(data,current_ncase_65536)) {
351  return false;
352  }
353  if (nreg1 != nreg) {
354  if (nreg > 0) {
355  delete [] result;
356  }
357  nreg = nreg1;
358  result = new EGS_ScoringSingle [nreg];
359  }
360  for (int j=0; j<nreg; j++) {
361  if (!result[j].setState(data)) {
362  return false;
363  }
364  }
365  return true;
366  };
367 
369  void reset() {
370  current_ncase = 0;
371  current_ncase_65536 = 0;
372  current_ncase_short = 0;
373  for (int j=0; j<nreg; j++) {
374  result[j].reset();
375  }
376  };
377 
385  current_ncase += x.current_ncase;
386  current_ncase_65536 = current_ncase >> 16;
387  EGS_I64 aux = current_ncase - (current_ncase_65536 << 16);
388  current_ncase_short = (unsigned short) aux;
389  for (int j=0; j<nreg; j++) {
390  result[j] += x.result[j];
391  }
392  return *this;
393  };
394 
399  int bins() const {
400  return nreg;
401  };
402 
407  int regions() const {
408  return nreg;
409  };
410 
411 protected:
412 
414  EGS_I64 current_ncase;
419  int nreg;
426  unsigned short current_ncase_short;
427 
428 };
429 
430 #endif
A class for scoring an array of quantities (e.g. a dose distribution) in a Monte Carlo simulation.
Definition: egs_scoring.h:220
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:399
EGS_I64 current_ncase_65536
Definition: egs_scoring.h:416
EGS_Float thisHistoryScore(int ireg) const
Returns the score in ireg in the current event.
Definition: egs_scoring.h:260
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:407
EGS_I64 current_ncase
Definition: egs_scoring.h:409
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:272
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:255
bool storeState(ostream &data)
Stores the state of the scoring array object into the data stream data.
Definition: egs_scoring.h:317
void score(int ireg, EGS_Float f)
Add f to the score in the element ireg.
Definition: egs_scoring.h:245
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:281
EGS_ScoringArray & operator+=(const EGS_ScoringArray &x)
Add the results of x to the rtesults of the invoking object.
Definition: egs_scoring.h:384
void reset()
Reset the scoring array to a pristine state.
Definition: egs_scoring.h:369
EGS_ScoringSingle * result
Definition: egs_scoring.h:421
bool setState(istream &data)
Sets the state fof the scoring array object from the data in the input stream data.
Definition: egs_scoring.h:341
unsigned short current_ncase_short
Definition: egs_scoring.h:426
A class for scoring a single quantity of interest in a Monte Carlo simulation.
Definition: egs_scoring.h:64
bool setState(istream &data)
Set the state of the scoring object from the data stream data.
Definition: egs_scoring.h:162
unsigned short current_ncase
Definition: egs_scoring.h:203
bool storeState(ostream &data)
Stores the state of the scoring object into the data stream data. Returns true on success,...
Definition: egs_scoring.h:145
EGS_ScoringSingle()
Construct a scoring object initialized to zero.
Definition: egs_scoring.h:69
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:112
EGS_Float currentScore() const
Returns the score of the current event.
Definition: egs_scoring.h:99
void score(unsigned short ncase, EGS_Float f)
Add f to the score of the ncase'th statistically independent event.
Definition: egs_scoring.h:79
EGS_ScoringSingle & operator+=(const EGS_ScoringSingle &x)
Combine the results of two scoring objects.
Definition: egs_scoring.h:185
void reset()
Reset the scoring object to a pristine state (i.e. all counters set to zero).
Definition: egs_scoring.h:171
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:105
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:91
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:123
Global egspp functions header file.
The input struct 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,...