BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
ParticleSetAccumulator.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "ParticleSet.hh"
19#include "ParticleSetAccumulator.hh"
20
21#include <cmath>
22#include <string>
23
25
27 n(0),
28 terminated(false),
29 resultHistName(""),
30 resultHistTitle(""),
31 mean(nullptr),
32 variance(nullptr),
33 result(nullptr)
34{;}
35
37 const std::string& resultHistNameIn,
38 const std::string& resultHistTitleIn):
39 n(0),
40 terminated(false),
41 resultHistName(resultHistNameIn),
42 resultHistTitle(resultHistTitleIn),
43 mean(nullptr),
44 variance(nullptr),
45 result(nullptr)
46{
47 std::string meanName = resultHistName + "_Mean";
48 std::string variName = resultHistName + "_Vari";
49 mean = new ParticleSet(*baseParticleSet);
50 variance = new ParticleSet(*baseParticleSet);
51 result = new ParticleSet(*baseParticleSet);
52}
53
55{
56 delete mean;
57 delete variance;
58 delete result;
59}
60
62{
63 // temporary variables
64 double newMean = 0;
65 double newVari = 0;
66 const double error = 0; // needed to pass reference to unused parameter
67 const unsigned long nEntriesToAccumulate = 1;
68
69 n++;
70 for (auto& keyBin : *mean)
71 {
72 AccumulateSingleValue(keyBin.second.value,
73 ((*variance)[keyBin.first]).value,
74 ((*newValue)[keyBin.first]).value,
75 error, n, nEntriesToAccumulate,
76 newMean, newVari);
77 (*mean)[keyBin.first].value = newMean;
78 (*variance)[keyBin.first].sumOfWeightsSquared = newVari;
79 }
80}
81
83{
84 // error on mean is sqrt(1/n) * std = sqrt(1/n) * sqrt(1/(n-1)) * sqrt(variance)
85 // the only variable is the variance, so take the rest out as a factor.
86 const double nD = (double)n; // cast only once
87 const double factor = std::sqrt(1./(nD * (nD - 1))); // nan if n = 1 -> won't be used
88 double mn = 0; // temporary variable for mean
89 double err = 0; // temporary variable for standard error on mean
90 double var = 0; // temporary variable for variance
91
92 // note here we set the std to 0 if there's only one entry (ie n = 1) to avoid
93 // division by zero and nans
94 for (auto& keyBin : *mean)
95 {
96 mn = keyBin.second.value;
97 var = keyBin.second.sumOfWeightsSquared;
98 err = n > 1 ? factor*std::sqrt(var) : 0;
99 (*result)[keyBin.first].value = mn;
100 (*result)[keyBin.first].sumOfWeightsSquared = err;
101 }
102 result->nEntries = n;
103 return result;
104}
105
107 double oldVari,
108 double x,
109 double /*xVari*/,
110 unsigned long nEntriesAccumulated,
111 unsigned long/*nEntriesToAccumulate*/,
112 double& newMean,
113 double& newVari) const
114{
115 newMean = oldMean + ((x - oldMean) / (double)nEntriesAccumulated);
116 newVari = oldVari + ((x - oldMean) * (x - newMean));
117}
Class to accumulate and merge ParticleSets in different ways.
virtual ParticleSet * Terminate()
virtual void AccumulateSingleValue(double oldMean, double oldVari, double x, double xVari, unsigned long nEntriesAccumulated, unsigned long nEntriesToAccumulate, double &newMean, double &newVari) const
ParticleSetAccumulator()
Default constructor only for ROOT reflexivity - not intended for use.
virtual void Accumulate(ParticleSet *newValue)
const std::string resultHistName
Name for resultant histogram.
A very simple 'map' type histogram axis.