Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4StatDouble.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4StatDouble class implementation
27//
28// Original Author: Giovanni Santin (ESA) - October 2005 in GRAS tool
29// Adapted by: John Apostolakis - November 2011
30// --------------------------------------------------------------------
31#include "G4StatDouble.hh"
32
34
36
38{
39 m_sum_wx = 0.;
40 m_sum_wx2 = 0.;
41 m_n = 0;
42 m_sum_w = 0.;
43 m_sum_w2 = 0.;
44 m_scale = 1.;
45}
46
48
50{
51 m_sum_wx += value * weight;
52 m_sum_wx2 += value * value * weight;
53 if(m_n < INT_MAX)
54 {
55 ++m_n;
56 }
57 m_sum_w += weight;
58 m_sum_w2 += weight * weight;
59
60 if(weight <= 0.)
61 {
62 G4cout << "[G4StatDouble::fill] WARNING: weight<=0. " << weight << G4endl;
63 }
64}
65
66void G4StatDouble::scale(G4double value) { m_scale = m_scale * value; }
67
69{
70 G4double mean_val = 0.;
71 if(m_sum_w > 0.)
72 {
73 mean_val = m_sum_wx / m_sum_w;
74 }
75 return m_scale * mean_val;
76}
77
79{
80 G4double factor = 0.;
81 // factor to rescale the Mean for the requested number
82 // of events (or sum of weights) ext_sum_w
83
84 if(ext_sum_w > 0)
85 {
86 factor = m_sum_w;
87 factor /= ext_sum_w;
88 }
89 return mean() * factor;
90}
91
93 G4int nn)
94{
95 G4double vrms = 0.0;
96 if(nn > 1)
97 {
98 G4double vmean = ssum_wx / ssum_w;
99 G4double xn = nn;
100 G4double tmp =
101 // from GNU Scientific Library. This part is equivalent to N/(N-1)
102 // when w_i = w
103 // ((m_sum_w * m_sum_w) / (m_sum_w * m_sum_w - m_sum_w2))
104
105 // from NIST "DATAPLOT Reference manual", Page 2-66
106 // http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf
107 // rewritten based on: SUM[w(x-m)^2]/SUM[w] = SUM[wx^2]/SUM[w] - m^2
108 // and dividing it by sqrt[n] to go from rms of distribution to the
109 // rms of the mean value
110
111 (xn / (xn - 1)) * ((ssum_wx2 / ssum_w) - (vmean * vmean));
112
113 tmp = std::max(tmp, 0.0); // this avoids observed computation problem
114 vrms = std::sqrt(tmp);
115 // G4cout << "[G4StatDoubleElement::rms] m_sum_wx: " << m_sum_wx
116 // << " m_sum_wx2: " << m_sum_wx2 << " m_sum_w: " << m_sum_w
117 // << " m_n: " << m_n << " tmp: " << tmp<< " rms: " << rms
118 // << G4endl;
119 // G4cout << "[G4StatDoubleElement::rms] (m_n / (m_n - 1)): " << (xn/(xn -
120 // 1))
121 // << " (m_sum_wx2 / m_sum_w): " << (m_sum_wx2 / m_sum_w)
122 // << " (mean * mean): " << (mean * mean)
123 // << " ((m_sum_wx2 / m_sum_w) - (mean * mean)): "
124 // << ((m_sum_wx2 / m_sum_w) - (mean * mean))
125 // << G4endl;
126 }
127 return vrms * m_scale;
128}
129
131{
132 // this method computes the RMS with "all internal" parameters:
133 // all the sums are the internal ones: m_sum_wx, m_sum_wx2, m_sum_w, m_n
134
135 return rms(m_sum_wx, m_sum_wx2, m_sum_w, m_n);
136}
137
139{
140 // this method computes the RMS with sum_w and n coming from outside:
141 // ext_sum_w and ext_n:
142 // this means that the result is normalised to the external events
143 // it is useful when, given a number ext_n of events with sum of the weights
144 // ext_sum_w, only m_n (with sum of weights m_sum_w) are actually accumulated
145 // in the internal summation (e.g. for a dose variable in a volume, because
146 // only a few particles reach that volume)
147
148 return rms(m_sum_wx, m_sum_wx2, ext_sum_w, ext_n);
149}
150
152{
153 m_n += ptr->n();
154 m_sum_w += ptr->sum_w();
155 m_sum_w2 += ptr->sum_w2();
156 m_sum_wx += ptr->sum_wx();
157 m_sum_wx2 += ptr->sum_wx2();
158}
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
G4double m_sum_wx
virtual ~G4StatDouble()
Definition: G4StatDouble.cc:47
G4double sum_w() const
Definition: G4StatDouble.hh:95
G4double m_sum_w
G4double rms()
G4double m_scale
void add(const G4StatDouble *)
G4double sum_w2() const
Definition: G4StatDouble.hh:96
G4int n() const
Definition: G4StatDouble.hh:94
G4double m_sum_w2
G4double mean() const
Definition: G4StatDouble.cc:68
G4double m_sum_wx2
void fill(G4double x, G4double weight=1.)
Definition: G4StatDouble.cc:49
G4double sum_wx2() const
Definition: G4StatDouble.hh:98
G4double sum_wx() const
Definition: G4StatDouble.hh:97
void scale(G4double)
Definition: G4StatDouble.cc:66
#define INT_MAX
Definition: templates.hh:90