ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4StatDouble.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4StatDouble.cc
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 //
28 //
29 // ----------------------------------------------------------------------
30 // class G4StatDouble
31 //
32 // Implementation.
33 // Original Author: Giovanni Santin (ESA) - October 2005 in GRAS tool
34 // Adapted by: John Apostolakis - November 2011
35 
36 #include "G4StatDouble.hh"
37 
39 {
40  reset();
41 }
42 
44 {
45  reset();
46  fill(x);
47 }
48 
50 {
51  m_sum_wx = 0.;
52  m_sum_wx2 = 0.;
53  m_n = 0;
54  m_sum_w = 0.;
55  m_sum_w2 = 0.;
56  m_scale = 1.;
57 }
58 
60 {}
61 
63 {
64  m_sum_wx += value * weight;
65  m_sum_wx2 += value * value * weight;
66  if(m_n < INT_MAX) { ++m_n; }
67  m_sum_w += weight;
68  m_sum_w2 += weight * weight;
69 
70  if (weight <= 0.)
71  {
72  G4cout << "[G4StatDouble::fill] WARNING: weight<=0. "
73  << weight << G4endl;
74  }
75 }
76 
78 {
79  m_scale = m_scale * value;
80 }
81 
83 {
84  G4double mean_val = 0.;
85  if (m_sum_w > 0.)
86  {
87  mean_val = m_sum_wx / m_sum_w;
88  }
89  return m_scale * mean_val;
90 }
91 
93 {
94  G4double factor = 0.;
95  // factor to rescale the Mean for the requested number
96  // of events (or sum of weights) ext_sum_w
97 
98  if (ext_sum_w > 0)
99  {
100  factor = m_sum_w;
101  factor /= ext_sum_w;
102  }
103  return mean() * factor;
104 
105 }
106 
108  G4double ssum_w, G4int nn)
109 {
110  G4double vrms = 0.0;
111  if (nn > 1)
112  {
113  G4double vmean = ssum_wx / ssum_w;
114  G4double xn = nn;
115  G4double tmp =
116  // from GNU Scientific Library. This part is equivalent to N/(N-1)
117  // when w_i = w
118  // ((m_sum_w * m_sum_w) / (m_sum_w * m_sum_w - m_sum_w2))
119 
120  // from NIST "DATAPLOT Reference manual", Page 2-66
121  // http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf
122  // rewritten based on: SUM[w(x-m)^2]/SUM[w] = SUM[wx^2]/SUM[w] - m^2
123  // and dividing it by sqrt[n] to go from rms of distribution to the
124  // rms of the mean value
125 
126  (xn / (xn - 1))
127  * ((ssum_wx2 / ssum_w) - (vmean * vmean));
128 
129  tmp = std::max(tmp, 0.0); // this avoids observed computation problem
130  vrms = std::sqrt( tmp );
131 // G4cout << "[G4StatDoubleElement::rms] m_sum_wx: " << m_sum_wx
132 // << " m_sum_wx2: " << m_sum_wx2 << " m_sum_w: " << m_sum_w
133 // << " m_n: " << m_n << " tmp: " << tmp<< " rms: " << rms
134 // << G4endl;
135 // G4cout << "[G4StatDoubleElement::rms] (m_n / (m_n - 1)): " << (xn/(xn - 1))
136 // << " (m_sum_wx2 / m_sum_w): " << (m_sum_wx2 / m_sum_w)
137 // << " (mean * mean): " << (mean * mean)
138 // << " ((m_sum_wx2 / m_sum_w) - (mean * mean)): "
139 // << ((m_sum_wx2 / m_sum_w) - (mean * mean))
140 // << G4endl;
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 }
165 
167 {
168  m_n += ptr->n();
169  m_sum_w += ptr->sum_w();
170  m_sum_w2 += ptr->sum_w2();
171  m_sum_wx += ptr->sum_wx();
172  m_sum_wx2 += ptr->sum_wx2();
173 }