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