BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
HistogramAccumulatorMerge.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "HistogramAccumulatorMerge.hh"
20
21#include "TH1.h"
22#include "TH1D.h"
23#include "TH2D.h"
24#include "TH3D.h"
25#include "BDSBH4DBase.hh"
26
27#include <cmath>
28#include <iostream>
29#include <string>
30
32
34 HistogramAccumulator(nullptr, 0, "", "")
35{;}
36
38 int nDimensionsIn,
39 const std::string& resultHistNameIn,
40 const std::string& resultHistTitleIn):
41 HistogramAccumulator(baseHistogram,
42 nDimensionsIn,
43 resultHistNameIn,
44 resultHistTitleIn)
45{;}
46
48{
49 // temporary variables
50 double newMean = 0;
51 double newVari = 0;
52 double var = 0;
53
54 // Want the number of events accumulated so far. We purposively set the entries
55 // in the mean histogram as the number of events accumulated, not the number of
56 // histograms (ie files here).
57 unsigned long oldEntries;
58 unsigned long newEntries;
59
60 if (nDimensions == 4)
61 {
62 oldEntries = (unsigned long)static_cast<BDSBH4DBase*>(mean)->GetEntries_BDSBH4D();
63 newEntries = (unsigned long)static_cast<BDSBH4DBase*>(newValue)->GetEntries_BDSBH4D();
64 }
65 else
66 {
67 oldEntries = (unsigned long)mean->GetEntries(); // works for base class*
68 newEntries = (unsigned long)newValue->GetEntries(); // works for base class*
69 }
70
71 unsigned long newTotalEntries = oldEntries + newEntries;
72 const double nD = (double)newEntries;
73 const double factor = nD * (nD - 1);
74
75 switch (nDimensions)
76 {
77 case 1:
78 {
79 TH1D* h1 = dynamic_cast<TH1D*>(mean);
80 TH1D* h1e = dynamic_cast<TH1D*>(variance);
81 TH1D* ht = dynamic_cast<TH1D*>(newValue);
82 for (int j = 0; j <= h1->GetNbinsX() + 1; ++j)
83 {
84 var = std::pow(ht->GetBinError(j), 2) * factor;
85 AccumulateSingleValue(h1->GetBinContent(j),
86 h1e->GetBinContent(j),
87 ht->GetBinContent(j),
88 var,
89 oldEntries, newEntries,
90 newMean, newVari);
91 h1->SetBinContent(j, newMean);
92 h1e->SetBinContent(j, newVari);
93 }
94 break;
95 }
96 case 2:
97 {
98 TH2D* h1 = dynamic_cast<TH2D*>(mean);
99 TH2D* h1e = dynamic_cast<TH2D*>(variance);
100 TH2D* ht = dynamic_cast<TH2D*>(newValue);
101 for (int j = 0; j <= h1->GetNbinsX() + 1; ++j)
102 {
103 for (int k = 0; k <= h1->GetNbinsY() + 1; ++k)
104 {
105 var = std::pow(ht->GetBinError(j,k), 2) * factor;
106 AccumulateSingleValue(h1->GetBinContent(j,k),
107 h1e->GetBinContent(j,k),
108 ht->GetBinContent(j,k),
109 var,
110 oldEntries, newEntries,
111 newMean, newVari);
112 h1->SetBinContent(j, k, newMean);
113 h1e->SetBinContent(j, k, newVari);
114 }
115 }
116 break;
117 }
118 case 3:
119 {
120 TH3D* h1 = dynamic_cast<TH3D*>(mean);
121 TH3D* h1e = dynamic_cast<TH3D*>(variance);
122 TH3D* ht = dynamic_cast<TH3D*>(newValue);
123 for (int j = 0; j <= h1->GetNbinsX() + 1; ++j)
124 {
125 for (int k = 0; k <= h1->GetNbinsY() + 1; ++k)
126 {
127 for (int l = 0; l <= h1->GetNbinsZ() + 1; ++l)
128 {
129 var = std::pow(ht->GetBinError(j,k,l), 2) * factor;
130 AccumulateSingleValue(h1->GetBinContent(j,k,l),
131 h1e->GetBinContent(j,k,l),
132 ht->GetBinContent(j,k,l),
133 var,
134 oldEntries, newEntries,
135 newMean, newVari);
136 h1->SetBinContent(j, k, l, newMean);
137 h1e->SetBinContent(j, k, l, newVari);
138 }
139 }
140 }
141 break;
142 }
143 case 4:
144 {
145#ifdef USE_BOOST
146 BDSBH4DBase* h1 = dynamic_cast<BDSBH4DBase*>(mean);
147 BDSBH4DBase* h1e = dynamic_cast<BDSBH4DBase*>(variance);
148 BDSBH4DBase* ht = dynamic_cast<BDSBH4DBase*>(newValue);
149 for (int j = -1; j <= h1->GetNbinsX(); ++j)
150 {
151 for (int k = -1; k <= h1->GetNbinsY(); ++k)
152 {
153 for (int l = -1; l <= h1->GetNbinsZ(); ++l)
154 {
155 for (int e = -1; e <= h1->GetNbinsE(); ++e)
156 {
157 var = std::pow(ht->AtError(j,k,l,e), 2) * factor;
158 AccumulateSingleValue(h1->At(j,k,l,e),
159 h1e->At(j,k,l,e),
160 ht->At(j,k,l,e),
161 var,
162 oldEntries, newEntries,
163 newMean, newVari);
164 h1->Set_BDSBH4D(j, k, l, e, newMean);
165 h1e->Set_BDSBH4D(j, k, l, e, newVari);
166 }
167 }
168 }
169 }
170 break;
171#endif
172 }
173 default:
174 {break;}
175 }
176 if(nDimensions==4)
177 {
178 dynamic_cast<BDSBH4DBase*>(mean)->SetEntries_BDSBH4D(newTotalEntries);
179 dynamic_cast<BDSBH4DBase*>(variance)->SetEntries_BDSBH4D(newTotalEntries);
180 }
181 else
182 {
183 mean->SetEntries(newTotalEntries);
184 variance->SetEntries(newTotalEntries);
185 }
186 n = newTotalEntries; // updated to Terminate() works correctly
187}
188
190 double oldVari,
191 double x,
192 double xVari,
193 unsigned long nEntriesAccumulated,
194 unsigned long nEntriesToAccumulate,
195 double& newMean,
196 double& newVari) const
197{
198 double dMean = x - oldMean;
199 double dMean2 = std::pow(dMean, 2);
200 double nA = (double)nEntriesAccumulated;
201 double nB = (double)nEntriesToAccumulate;
202 double nT = nA + nB;
203
204 newMean = oldMean + nB * (dMean / nT);
205
206 double q = (nA * nB) * (dMean2 / nT);
207 newVari = oldVari + xVari + q;
208}
Base class for the 4D histogram classes.
Definition: BDSBH4DBase.hh:37
Class to combine histograms of mean with error on mean.
virtual void AccumulateSingleValue(double oldMean, double oldVari, double x, double xVari, unsigned long nEntriesAccumulated, unsigned long nEntriesToAccumulate, double &newMean, double &newVari) const
virtual void Accumulate(TH1 *newValue)
HistogramAccumulatorMerge()
Default constructor only for ROOT reflexivity - not intended for use.
Class to accumulate and merge histograms in different ways.
int nDimensions
Number of dimensions.
unsigned long n
Counter.