2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
26// G4StatAnalysis inline methods implementation
28// Author: J.Madsen, 25.10.2018
29// --------------------------------------------------------------------
42#include "G4Allocator.hh"
45//----------------------------------------------------------------------------//
47G4StatAnalysis::G4StatAnalysis() {}
49//----------------------------------------------------------------------------//
51G4double G4StatAnalysis::GetMean() const
53 return (fHits > 0) ? fSum1 / ((G4double) fHits) : 0.;
56//----------------------------------------------------------------------------//
58const G4double& G4StatAnalysis::GetSum() const { return fSum1; }
60//----------------------------------------------------------------------------//
62const G4double& G4StatAnalysis::GetSumSquared() const { return fSum2; }
64//----------------------------------------------------------------------------//
66const G4double& G4StatAnalysis::GetSum1() const { return fSum1; }
68//----------------------------------------------------------------------------//
70const G4double& G4StatAnalysis::GetSum2() const { return fSum2; }
72//----------------------------------------------------------------------------//
74const G4int& G4StatAnalysis::GetHits() const { return fHits; }
76//----------------------------------------------------------------------------//
78G4int G4StatAnalysis::GetNumNonZero() const { return fHits - fZero; }
80//----------------------------------------------------------------------------//
82G4int G4StatAnalysis::GetNumZero() const { return fZero; }
84//----------------------------------------------------------------------------//
86void G4StatAnalysis::SetSum(const G4double& val) { fSum1 = val; }
88//----------------------------------------------------------------------------//
90void G4StatAnalysis::SetSumSquared(const G4double& val) { fSum2 = val; }
92//----------------------------------------------------------------------------//
94void G4StatAnalysis::SetSum1(const G4double& val) { fSum1 = val; }
96//----------------------------------------------------------------------------//
98void G4StatAnalysis::SetSum2(const G4double& val) { fSum2 = val; }
100//----------------------------------------------------------------------------//
102void G4StatAnalysis::SetHits(const G4int& val) { fHits = val; }
104//----------------------------------------------------------------------------//
106void G4StatAnalysis::SetZero(const G4int& val) { fZero = val; }
108//----------------------------------------------------------------------------//
110G4double G4StatAnalysis::GetFOM() const
112 G4double elapsed_time = this->GetCpuTime();
113 G4double relative_err = this->GetRelativeError();
114 // lambda for equation clarity (will be inlined)
115 auto compute_figure_of_merit = [&]() {
116 return (1.0 / (relative_err * relative_err) / elapsed_time);
118 return (std::fabs(relative_err) > 0.0 && elapsed_time > 0.0)
119 ? compute_figure_of_merit()
120 : ((fHits > 0) ? 1.0 : 0.0);
123//----------------------------------------------------------------------------//
125G4double G4StatAnalysis::GetRelativeError() const
127 // lambda for equation clarity (will be inlined)
128 auto compute_relative_error = [&]() {
129 return (GetStdDev() / GetMean() / std::sqrt((G4double) fHits));
131 return (std::fabs(GetMean()) > 0 && fHits > 0) ? compute_relative_error()
132 : ((fHits > 0) ? 1.0 : 0.0);
135//----------------------------------------------------------------------------//
137G4double G4StatAnalysis::GetStdDev() const
139 return ::sqrt(std::fabs(GetVariance()));
142//----------------------------------------------------------------------------//
144G4double G4StatAnalysis::GetVariance() const
146 // lambda for equation clarity (will be inlined)
147 auto compute_variance = [&]() {
148 return ((fSum2 - (std::pow(fSum1, 2.0) / fHits)) /
149 (((G4double) fHits) - 1.0));
151 return (fHits > 1) ? compute_variance() : 0.0;
154//----------------------------------------------------------------------------//
156G4double G4StatAnalysis::GetCoeffVariation() const
158 // lambda for equation clarity (will be inlined)
159 auto coefficient_of_variation = [&]() {
160 G4double hits = fHits;
161 return ::sqrt((hits / (hits - 1.0)) *
162 ((fSum2 / (fSum1 * fSum1)) - (1.0 / hits)));
164 return (fHits > 1) ? coefficient_of_variation() : 0.0;
165 // return (fHits > 0 && fabs(fSum1) > 0.0)
166 // ? (100.0*GetStdDev()/GetMean()) : 0.0;
169//----------------------------------------------------------------------------//
171G4double G4StatAnalysis::GetEfficiency() const
173 G4double hits = fHits;
174 G4double nzero = fHits - fZero;
175 return (fHits > 0) ? (nzero / hits) : 0.0;
178//----------------------------------------------------------------------------//
180G4double G4StatAnalysis::GetR2Int() const
182 G4double hits = fHits;
184 ? (fSum2 / (fSum1 * fSum1)) - 1.0 / (GetEfficiency() * hits)
188//----------------------------------------------------------------------------//
190G4double G4StatAnalysis::GetR2Eff() const
192 G4double hits = fHits;
193 return (fHits > 0) ? (1.0 - GetEfficiency()) / (GetEfficiency() * hits) : 0.0;
196//----------------------------------------------------------------------------//
198G4StatAnalysis::operator G4double() const { return this->GetSum(); }
200//----------------------------------------------------------------------------//
202void G4StatAnalysis::Reset()
210//----------------------------------------------------------------------------//
212void G4StatAnalysis::Add(const G4double& val, const G4double& weight)
215 fSum1 += val * weight;
216 fSum2 += val * val * weight;
217 if(std::fabs(val * weight) <
218 std::fabs(GetMean() * std::numeric_limits<double>::epsilon()))
222//----------------------------------------------------------------------------//
224void G4StatAnalysis::Rescale(const G4double& factor)
227 fSum2 *= factor * factor;
230//----------------------------------------------------------------------------//
232void G4StatAnalysis::PrintInfo(std::ostream& os, const std::string& tab) const
234 G4int _hits = this->GetHits();
235 G4double _sum = this->GetSum();
236 G4double _sigma = this->GetStdDev();
237 G4double _coeff = this->GetCoeffVariation();
238 G4double _error = this->GetRelativeError();
239 G4double _eff = this->GetEfficiency();
240 G4double _fom = this->GetFOM();
241 G4double _r2int = this->GetR2Int();
242 G4double _r2eff = this->GetR2Eff();
248 using std::scientific;
249 using std::setprecision;
252 std::stringstream ss;
253 ss << tab //<< scientific
254 << setprecision(os.precision()) << right << _sum << left
255 << " [sigma: " << right << _sigma << left << " | error: " << right
256 << _error << left << " | coeff: " << right << _coeff << left
257 << " | eff: " << right << _eff << left << " | fom: " << right << _fom
258 << left << " | r2int: " << right << _r2int << left << " | r2eff: " << right
259 << _r2eff << left << " | hits: " << right << _hits << left << " ]";
264//----------------------------------------------------------------------------//
266G4double G4StatAnalysis::GetCpuTime() const
268 tms* startTime = GetCpuClock();
271 return ((endTime.tms_stime - startTime->tms_stime) +
272 (endTime.tms_utime - startTime->tms_utime)) /
273 sysconf(_SC_CLK_TCK);
276//----------------------------------------------------------------------------//
278G4StatAnalysis& G4StatAnalysis::operator+=(const G4double& _val)
284//----------------------------------------------------------------------------//
286G4StatAnalysis& G4StatAnalysis::operator/=(const G4double& _val)
289 fSum2 /= (_val * _val);
293//----------------------------------------------------------------------------//
295G4StatAnalysis& G4StatAnalysis::operator+=(const G4StatAnalysis& rhs)
304//----------------------------------------------------------------------------//
306G4StatAnalysis& G4StatAnalysis::operator-=(const G4StatAnalysis& rhs)
315//----------------------------------------------------------------------------//
317inline G4Allocator<G4StatAnalysis>*& _aStatAnalysisAllocator_G4MT_TLS_()
319 G4ThreadLocalStatic G4Allocator<G4StatAnalysis>* _instance =
320 new G4Allocator<G4StatAnalysis>();
324//----------------------------------------------------------------------------//
326void* G4StatAnalysis::operator new(std::size_t)
328 G4Allocator<G4StatAnalysis>& _allocator =
329 *_aStatAnalysisAllocator_G4MT_TLS_();
330 return (void*) _allocator.MallocSingle();
333//----------------------------------------------------------------------------//
335void G4StatAnalysis::operator delete(void* _ptr)
337 G4Allocator<G4StatAnalysis>& _allocator =
338 *_aStatAnalysisAllocator_G4MT_TLS_();
339 _allocator.FreeSingle((G4StatAnalysis*) _ptr);
342//----------------------------------------------------------------------------//