BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBH4D.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 "BDSBH4D.hh"
19
20#include "TFile.h"
21
22#ifdef USE_BOOST
23#include "BDSBH4DTypeDefs.hh"
24#include <boost/format.hpp>
25#include <boost/histogram.hpp>
26#endif
27
28#include <iostream>
29#include <sstream>
30#include <string>
31#include <vector>
32
33#ifdef USE_BOOST
34templateClassImp(BDSBH4D)
35
36template <>
38 BDSBH4DBase(3,3,3,3, 0,1, 0,1, 0,1, 0.1,0.230, "BDSBH4D","BDSBH4D","linear")
39{
40 h = boost::histogram::make_histogram_with(std::vector<double>(),
41 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
42 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
43 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
44 boost::histogram::axis::regular<double> {3, 1.0, 230.0, "energy"});
45
46 h_err = boost::histogram::make_histogram_with(std::vector<double>(),
47 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
48 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
49 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
50 boost::histogram::axis::regular<double> {3, 1.0, 230.0, "energy"});
51}
52
53template <>
55 BDSBH4DBase(3,3,3,3, 0,1, 0,1, 0,1, 0.1,0.230, "BDSBH4D","BDSBH4D","log")
56{
57 h = boost::histogram::make_histogram_with(std::vector<double>(),
58 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
59 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
60 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
61 boost::histogram::axis::regular<double, boost::histogram::axis::transform::log> {3, 1.0, 230.0, "energy"});
62
63 h_err = boost::histogram::make_histogram_with(std::vector<double>(),
64 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
65 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
66 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
67 boost::histogram::axis::regular<double, boost::histogram::axis::transform::log> {3, 1.0, 230.0, "energy"});
68}
69
70template <>
72 BDSBH4DBase(3,3,3,0,1, 0,1, 0,1, "BDSBH4D","BDSBH4D","user",std::vector<double>{0.001,0.1,0.230})
73{
74 h = boost::histogram::make_histogram_with(std::vector<double>(),
75 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
76 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
77 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
78 boost::histogram::axis::variable<double> {std::vector<double>{0.001,0.230}, "energy"});
79
80 h_err = boost::histogram::make_histogram_with(std::vector<double>(),
81 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
82 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
83 boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
84 boost::histogram::axis::variable<double> {std::vector<double>{0.001,0.1,0.230}, "energy"});
85}
86
87template <>
88BDSBH4D<boost_histogram_linear>::BDSBH4D(std::string& name, std::string& title, const std::string& eScale,
89 unsigned int nxbins, double xmin, double xmax,
90 unsigned int nybins, double ymin, double ymax,
91 unsigned int nzbins, double zmin, double zmax,
92 unsigned int nebins, double emin, double emax):
93 BDSBH4DBase(nxbins, nybins, nzbins, nebins,
94 xmin, xmax, ymin, ymax, zmin, zmax, emin, emax,
95 name, title, eScale)
96{
97 h = boost::histogram::make_histogram_with(std::vector<double>(),
98 boost::histogram::axis::regular<double> {nxbins, xmin, xmax, "x"},
99 boost::histogram::axis::regular<double> {nybins, ymin, ymax, "y"},
100 boost::histogram::axis::regular<double> {nzbins, zmin, zmax, "z"},
101 boost::histogram::axis::regular<double> {nebins, emin, emax, "energy"});
102
103 h_err = boost::histogram::make_histogram_with(std::vector<double>(),
104 boost::histogram::axis::regular<double> {nxbins, xmin, xmax, "x"},
105 boost::histogram::axis::regular<double> {nybins, ymin, ymax, "y"},
106 boost::histogram::axis::regular<double> {nzbins, zmin, zmax, "z"},
107 boost::histogram::axis::regular<double> {nebins, emin, emax, "energy"});
108}
109
110template <>
111BDSBH4D<boost_histogram_log>::BDSBH4D(std::string& name, std::string& title, const std::string& eScale,
112 unsigned int nxbins, double xmin, double xmax,
113 unsigned int nybins, double ymin, double ymax,
114 unsigned int nzbins, double zmin, double zmax,
115 unsigned int nebins, double emin, double emax):
116 BDSBH4DBase(nxbins, nybins, nzbins, nebins,
117 xmin, xmax, ymin, ymax, zmin, zmax, emin, emax,
118 name, title, eScale)
119{
120 h = boost::histogram::make_histogram_with(std::vector<double>(),
121 boost::histogram::axis::regular<double> {nxbins, xmin, xmax, "x"},
122 boost::histogram::axis::regular<double> {nybins, ymin, ymax, "y"},
123 boost::histogram::axis::regular<double> {nzbins, zmin, zmax, "z"},
124 boost::histogram::axis::regular<double, boost::histogram::axis::transform::log> {nebins, emin, emax, "energy"});
125
126 h_err = boost::histogram::make_histogram_with(std::vector<double>(),
127 boost::histogram::axis::regular<double> {nxbins, xmin, xmax, "x"},
128 boost::histogram::axis::regular<double> {nybins, ymin, ymax, "y"},
129 boost::histogram::axis::regular<double> {nzbins, zmin, zmax, "z"},
130 boost::histogram::axis::regular<double, boost::histogram::axis::transform::log> {nebins, emin, emax, "energy"});
131}
132
133template <>
134BDSBH4D<boost_histogram_variable>::BDSBH4D(std::string& name, std::string& title, const std::string& eScale,
135 const std::vector<double>& eBinEdgesIn,
136 unsigned int nxbins, double xmin, double xmax,
137 unsigned int nybins, double ymin, double ymax,
138 unsigned int nzbins, double zmin, double zmax):
139 BDSBH4DBase(nxbins, nybins, nzbins, xmin, xmax, ymin, ymax, zmin, zmax, name, title, eScale, eBinEdgesIn)
140{
141 h = boost::histogram::make_histogram_with(std::vector<double>(),
142 boost::histogram::axis::regular<double> {nxbins, xmin, xmax, "x"},
143 boost::histogram::axis::regular<double> {nybins, ymin, ymax, "y"},
144 boost::histogram::axis::regular<double> {nzbins, zmin, zmax, "z"},
145 boost::histogram::axis::variable<double> {eBinEdgesIn, "energy"});
146
147 h_err = boost::histogram::make_histogram_with(std::vector<double>(),
148 boost::histogram::axis::regular<double> {nxbins, xmin, xmax, "x"},
149 boost::histogram::axis::regular<double> {nybins, ymin, ymax, "y"},
150 boost::histogram::axis::regular<double> {nzbins, zmin, zmax, "z"},
151 boost::histogram::axis::variable<double> {eBinEdgesIn, "energy"});
152}
153
154template<class T>
156{;}
157
158template <class T>
160{
161 h.reset();
162 h_entries = 0;
163}
164
165template <class T>
166BDSBH4D<T>* BDSBH4D<T>::Clone(const char* newname) const
167{
168 auto clone = new BDSBH4D<T>(*this);
169 clone->SetName(newname);
170 return clone;
171}
172
173template <class T>
174void BDSBH4D<T>::Fill_BDSBH4D(double xValue,
175 double yValue,
176 double zValue,
177 double eValue)
178{
179 h(xValue, yValue, zValue, eValue);
180}
181
182template <class T>
183void BDSBH4D<T>::Set_BDSBH4D(int x,
184 int y,
185 int z,
186 int e,
187 double value)
188{
189 h.at(x, y, z, e) = value;
190}
191
192template <class T>
194 int y,
195 int z,
196 int e,
197 double value)
198{
199 h_err.at(x, y, z, e) = value;
200}
201
202template <class T>
203void BDSBH4D<T>::Add_BDSBH4D(BDSBH4DBase* otherHistogram)
204{
205 auto tmp = dynamic_cast<BDSBH4D<T>*>(otherHistogram);
206 h += tmp->h;
207}
208
209template <class T>
210double BDSBH4D<T>::At(int x, int y, int z, int e)
211{
212 return h.at(x, y, z, e);
213}
214
215template <class T>
216double BDSBH4D<T>::LowBinEdgeAt(int x, int y, int z, int e)
217{
218 std::ostringstream os4;
219 for (auto&& i : indexed(h))
220 {
221 if (i.index(0) == x and i.index(1) == y and i.index(2) == z and i.index(3) == e)
222 {return i.bin(3).lower();}
223 }
224 return 0;
225}
226
227template <class T>
228double BDSBH4D<T>::HighBinEdgeAt(int x, int y, int z, int e)
229{
230 std::ostringstream os4;
231 for (auto&& i : indexed(h))
232 {
233 if (i.index(0) == x and i.index(1) == y and i.index(2) == z and i.index(3) == e)
234 {return i.bin(3).upper();}
235 }
236 return 0;
237}
238
239template <class T>
240double BDSBH4D<T>::AtError(int x, int y, int z, int e)
241{
242 return h_err.at(x, y, z, e);
243}
244
245template <class T>
246void BDSBH4D<T>::Print_BDSBH4D(bool with_zero_values)
247{
248 std::ostringstream os4;
249 for (auto&& x : indexed(this->h))
250 {
251 if (!with_zero_values and *x != 0)
252 {
253 os4 << boost::format("(%i, %i, %i, %i) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) %i\n")
254 % x.index(0) % x.index(1) % x.index(2) % x.index(3)
255 % x.bin(0).lower() % x.bin(0).upper()
256 % x.bin(1).lower() % x.bin(1).upper()
257 % x.bin(2).lower() % x.bin(2).upper()
258 % x.bin(3).lower() % x.bin(3).upper()
259 % *x;
260 }
261 else if (with_zero_values)
262 {
263 os4 << boost::format("(%i, %i, %i, %i) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) %i\n")
264 % x.index(0) % x.index(1) % x.index(2) % x.index(3)
265 % x.bin(0).lower() % x.bin(0).upper()
266 % x.bin(1).lower() % x.bin(1).upper()
267 % x.bin(2).lower() % x.bin(2).upper()
268 % x.bin(3).lower() % x.bin(3).upper()
269 % *x;
270 }
271 }
272 std::cout << os4.str() << std::flush;
273}
274
275template <class T>
276void BDSBH4D<T>::Print_BDSBH4D(int x, int y, int z, int e)
277{
278 std::ostringstream os4;
279 for (auto&& i : indexed(this->h))
280 {
281 if (i.index(0) == x and i.index(1) == y and i.index(2) ==z and i.index(3) ==e)
282 {
283 os4 << boost::format("(%i, %i, %i, %i) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) %i\n")
284 % i.index(0) % i.index(1) % i.index(2) % i.index(3)
285 % i.bin(0).lower() % i.bin(0).upper()
286 % i.bin(1).lower() % i.bin(1).upper()
287 % i.bin(2).lower() % i.bin(2).upper()
288 % i.bin(3).lower() % i.bin(3).upper()
289 % *i;
290 }
291 }
292 std::cout << os4.str() << std::flush;
293}
294
296template class BDSBH4D<boost_histogram_log>;
298
299#endif
Base class for the 4D histogram classes.
4D histogram classes with linear, logarithmic and user-defined energy binning.
Definition BDSBH4D.hh:40
STL namespace.