BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSScorerMeshInfo.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2024.
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 "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSExtent.hh"
22#include "BDSScorerMeshInfo.hh"
23#include "BDSUtilities.hh"
24#ifdef USE_BOOST
25#include "BDSBH4DTypeDefs.hh"
26#endif
27
28#include "G4Types.hh"
29
30#include "parser/scorermesh.h"
31
32#include "CLHEP/Units/SystemOfUnits.h"
33
34#include <algorithm>
35#include <fstream>
36#include <iterator>
37#include <vector>
38
39BDSScorerMeshInfo::BDSScorerMeshInfo(const GMAD::ScorerMesh& mesh)
40{
41 name = G4String(mesh.name);
42 geometryType = BDS::LowerCase(G4String(mesh.geometryType));
43 nBinsX = mesh.nx;
44 nBinsY = mesh.ny;
45 nBinsZ = mesh.nz;
46 nBinsR = mesh.nr;
47 nBinsPhi = mesh.nphi;
48 nBinsE = mesh.ne;
49
50 if (geometryType == "box")
51 {
52 if (!BDS::IsFinite(mesh.xsize))
53 {throw BDSException(__METHOD_NAME__, "xsize must be > 0 and finite in mesh \"" + mesh.name + "\"");}
54 if (!BDS::IsFinite(mesh.ysize))
55 {throw BDSException(__METHOD_NAME__, "ysize must be > 0 and finite in mesh \"" + mesh.name + "\"");}
56 if (!BDS::IsFinite(mesh.zsize))
57 {throw BDSException(__METHOD_NAME__, "zsize must be > 0 and finite in mesh \"" + mesh.name + "\"");}
58 if (!BDS::IsFinite(nBinsX))
59 {throw BDSException(__METHOD_NAME__, "nx must be > 0 and finite in mesh \"" + mesh.name + "\"");}
60 if (!BDS::IsFinite(nBinsY))
61 {throw BDSException(__METHOD_NAME__, "ny must be > 0 and finite in mesh \"" + mesh.name + "\"");}
62 if (!BDS::IsFinite(nBinsZ))
63 {throw BDSException(__METHOD_NAME__, "nz must be > 0 and finite in mesh \"" + mesh.name + "\"");}
64 }
65 else if (geometryType == "cylindrical")
66 {
67 if (!BDS::IsFinite(mesh.zsize))
68 {throw BDSException(__METHOD_NAME__, "zsize must be > 0 and finite in mesh \"" + mesh.name + "\"");}
69 if (!BDS::IsFinite(mesh.rsize))
70 {throw BDSException(__METHOD_NAME__, "rsize must be > 0 and finite in mesh \"" + mesh.name + "\"");}
71 if (!BDS::IsFinite(nBinsZ))
72 {throw BDSException(__METHOD_NAME__, "nz must be > 0 and finite in mesh \"" + mesh.name + "\"");}
73 if (!BDS::IsFinite(nBinsPhi))
74 {throw BDSException(__METHOD_NAME__, "nphi must be > 0 and finite in mesh \"" + mesh.name + "\"");}
75 if (!BDS::IsFinite(nBinsR))
76 {throw BDSException(__METHOD_NAME__, "nr must be > 0 and finite in mesh \"" + mesh.name + "\"");}
77 }
78
79 xLow = -0.5*mesh.xsize * CLHEP::m;
80 xHigh = 0.5*mesh.xsize * CLHEP::m;
81 yLow = -0.5*mesh.ysize * CLHEP::m;
82 yHigh = 0.5*mesh.ysize * CLHEP::m;
83 zLow = -0.5*mesh.zsize * CLHEP::m;
84 zHigh = 0.5*mesh.zsize * CLHEP::m;
85 rLow = 0 * CLHEP::m;
86 rHigh = mesh.rsize * CLHEP::m;
87 eLow = mesh.eLow* CLHEP::GeV;
88 eHigh = mesh.eHigh* CLHEP::GeV;
89 eScale = mesh.eScale;
90
91 extent = BDSExtent(xLow, xHigh,
92 yLow, yHigh,
93 zLow, zHigh);
94
95 if (eScale == "user")
96 {// In future we can move RBDS::BinLoader to a separate library and use that both here and in rebdsim
97 std::string const BinsEdgesFile(mesh.eBinsEdgesFilenamePath);
98 std::ifstream file(BinsEdgesFile.c_str());
99
100 if (file)
101 {
102 // Reading of the bins edges file.
103 std::istream_iterator<double> it(file);
104 std::istream_iterator<double> end;
105 std::back_insert_iterator<std::vector<double>> it2(eBinsEdges);
106
107 std::copy(it, end, it2);
108 }
109 else
110 {throw BDSException(__METHOD_NAME__, "eBinsEdgesFilenamePath must be the path to a .txt file");}
111
112 nBinsE = (G4int)eBinsEdges.size()-1;
113 eLow = eBinsEdges[0];
114 eHigh = eBinsEdges[nBinsE];
115 }
116
117 if (nBinsE > 1)
118 {
119#ifdef USE_BOOST
120 if (eScale == "linear")
121 {energyAxis = new boost_histogram_linear_axis(nBinsE, eLow, eHigh, "energy");}
122 else if (eScale == "log")
123 {energyAxis = new boost_histogram_log_axis(nBinsE, eLow, eHigh, "energy");}
124 else if (eScale == "user")
125 {
126 std::vector<double> eBinsEdgesEnergyAxis = eBinsEdges;
127 std::for_each(eBinsEdgesEnergyAxis.begin(), eBinsEdgesEnergyAxis.end(), [](double& el){el *= CLHEP::GeV;});
128 energyAxis = new boost_histogram_variable_axis(eBinsEdgesEnergyAxis, "energy");
129 }
130 else
131 {throw BDSException(__METHOD_NAME__, "eScale must be 'linear', 'log' or 'user' in mesh \"" + mesh.name + "\"");}
132#endif
133 }
134}
General exception with possible name of object and message.
Holder for +- extents in 3 dimensions.
Definition BDSExtent.hh:39
ScorerMesh class for parser.
Definition scorermesh.h:40
int ny
Number of bins in y.
Definition scorermesh.h:47
int ne
Number of bins in E.
Definition scorermesh.h:51
int nx
Number of bins in x.
Definition scorermesh.h:46
int nphi
Number of bins in Phi.
Definition scorermesh.h:50
int nz
Number of bins in z.
Definition scorermesh.h:48
std::string eScale
E scaling type.
Definition scorermesh.h:58
std::string name
Name of this placement.
Definition scorermesh.h:42
double xsize
X total width.
Definition scorermesh.h:52
double eHigh
E High limit.
Definition scorermesh.h:57
int nr
Number of bins in R.
Definition scorermesh.h:49
double eLow
E Low limit.
Definition scorermesh.h:56
double ysize
Y total width.
Definition scorermesh.h:53
std::string geometryType
Name of scorermesh geometry to use.
Definition scorermesh.h:44
double rsize
R total length.
Definition scorermesh.h:55
std::string eBinsEdgesFilenamePath
E bins edges filename path.
Definition scorermesh.h:59
double zsize
Z total width.
Definition scorermesh.h:54
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())