Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 //
27 // $Id:$
28 // GEANT4 tag $Name: not supported by cvs2svn $
29 //
30 //
31 // ----------------------------------------------------------------------
32 // class G4StatDouble
33 //
34 // Implementation.
35 // Original Author: Giovanni Santin (ESA) - October 2005 in GRAS tool
36 // Adapted by: John Apostolakis - November 2011
37 
38 #include "G4StatDouble.hh"
39 
41 {
42  reset();
43 }
44 
46 {
47  m_sum_wx = 0.;
48  m_sum_wx2 = 0.;
49  m_n = 0;
50  m_sum_w = 0.;
51  m_sum_w2 = 0.;
52  m_scale = 1.;
53 }
54 
56 {}
57 
59 {
60  m_sum_wx += value * weight;
61  m_sum_wx2 += value * value * weight;
62  m_n++;
63  m_sum_w += weight;
64  m_sum_w2 += weight * weight;
65 
66  if (weight <= 0.)
67  {
68  G4cout << "[G4StatDouble::fill] WARNING: weight<=0. "
69  << weight << G4endl;
70  }
71 }
72 
74 {
75  m_scale = m_scale * value;
76 }
77 
79 {
80  G4double mean_val = 0.;
81  if (m_sum_w > 0.)
82  {
83  mean_val = m_sum_wx / m_sum_w;
84  }
85  return m_scale * mean_val;
86 }
87 
89 {
90  G4double factor = 0.;
91  // factor to rescale the Mean for the requested number
92  // of events (or sum of weights) ext_sum_w
93 
94  if (ext_sum_w > 0)
95  {
96  factor = m_sum_w;
97  factor /= ext_sum_w;
98  }
99  return mean() * factor;
100 
101 }
102 
104  G4double ssum_w, G4int nn)
105 {
106  G4double vrms;
107  if (nn > 1)
108  {
109  G4double vmean = ssum_wx / ssum_w;
110  G4double xn = nn;
111  G4double tmp =
112  // from GNU Scientific Library. This part is equivalent to N/(N-1)
113  // when w_i = w
114  // ((m_sum_w * m_sum_w) / (m_sum_w * m_sum_w - m_sum_w2))
115 
116  // from NIST "DATAPLOT Reference manual", Page 2-66
117  // http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf
118  // rewritten based on: SUM[w(x-m)^2]/SUM[w] = SUM[wx^2]/SUM[w] - m^2
119  // and dividing it by sqrt[n] to go from rms of distribution to the
120  // rms of the mean value
121 
122  (1. / (xn - 1))
123  * ((ssum_wx2 / ssum_w) - (vmean * vmean));
124 
125  if (tmp < 0.) tmp=0.; // this avoids observed computation problem
126  vrms = std::sqrt( tmp );
127 // G4cout << "[G4StatDoubleElement::rms] m_sum_wx: " << m_sum_wx
128 // << " m_sum_wx2: " << m_sum_wx2 << " m_sum_w: " << m_sum_w
129 // << " m_n: " << m_n << " tmp: " << tmp<< " rms: " << rms
130 // << G4endl;
131 // G4cout << "[G4StatDoubleElement::rms] (m_n / (m_n - 1)): " << (xn/(xn - 1))
132 // << " (m_sum_wx2 / m_sum_w): " << (m_sum_wx2 / m_sum_w)
133 // << " (mean * mean): " << (mean * mean)
134 // << " ((m_sum_wx2 / m_sum_w) - (mean * mean)): "
135 // << ((m_sum_wx2 / m_sum_w) - (mean * mean))
136 // << G4endl;
137  }
138  else
139  {
140  vrms = -1.;
141  }
142  return vrms * m_scale;
143 }
144 
146 {
147  // this method computes the RMS with "all internal" parameters:
148  // all the sums are the internal ones: m_sum_wx, m_sum_wx2, m_sum_w, m_n
149 
150  return rms(m_sum_wx, m_sum_wx2, m_sum_w, m_n);
151 }
152 
154 {
155  // this method computes the RMS with sum_w and n coming from outside:
156  // ext_sum_w and ext_n:
157  // this means that the result is normalised to the external events
158  // it is useful when, given a number ext_n of events with sum of the weights
159  // ext_sum_w, only m_n (with sum of weights m_sum_w) are actually accumulated
160  // in the internal summation (e.g. for a dose variable in a volume, because
161  // only a few particles reach that volume)
162 
163  return rms(m_sum_wx, m_sum_wx2, ext_sum_w, ext_n);
164 }