BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchEventGenerator.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSBunchEventGenerator.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSParticleCoordsFull.hh"
23#include "BDSPhysicsUtilities.hh"
24#include "BDSUtilities.hh"
25#include "BDSWarning.hh"
26
27#include "parser/beam.h"
28
29#include "G4ParticleDefinition.hh"
30#include "G4ParticleTable.hh"
31
32#include "CLHEP/Units/SystemOfUnits.h"
33
34#include <algorithm>
35#include <cmath>
36#include <map>
37#include <sstream>
38#include <stdexcept>
39#include <string> // for stoi
40#include <vector>
41
42BDSBunchEventGenerator::BDSBunchEventGenerator():
43 BDSBunch("event generator"),
44 eventGeneratorMinX(0),
45 eventGeneratorMaxX(0),
46 eventGeneratorMinY(0),
47 eventGeneratorMaxY(0),
48 eventGeneratorMinZ(0),
49 eventGeneratorMaxZ(0),
50 eventGeneratorMinXp(0),
51 eventGeneratorMaxXp(0),
52 eventGeneratorMinYp(0),
53 eventGeneratorMaxYp(0),
54 eventGeneratorMinZp(0),
55 eventGeneratorMaxZp(0),
56 eventGeneratorMinRp(0),
57 eventGeneratorMaxRp(0),
58 eventGeneratorMinT(0),
59 eventGeneratorMaxT(0),
60 eventGeneratorMinEK(0),
61 eventGeneratorMaxEK(0),
62 Rp0(0),
63 firstTime(true),
64 testOnParticleType(true),
65 acceptedParticlesString("")
66{;}
67
68BDSBunchEventGenerator::~BDSBunchEventGenerator()
69{;}
70
72 const GMAD::Beam& beam,
73 const BDSBunchType& distrType,
74 G4Transform3D beamlineTransformIn,
75 const G4double beamlineSIn)
76{
77 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
78
79 eventGeneratorMinX = beam.eventGeneratorMinX * CLHEP::m;
80 eventGeneratorMaxX = beam.eventGeneratorMaxX * CLHEP::m;
81 eventGeneratorMinY = beam.eventGeneratorMinY * CLHEP::m;
82 eventGeneratorMaxY = beam.eventGeneratorMaxY * CLHEP::m;
83 eventGeneratorMinZ = beam.eventGeneratorMinZ * CLHEP::m;
84 eventGeneratorMaxZ = beam.eventGeneratorMaxZ * CLHEP::m;
93 eventGeneratorMinT = beam.eventGeneratorMinT * CLHEP::s;
94 eventGeneratorMaxT = beam.eventGeneratorMaxT * CLHEP::s;
95 eventGeneratorMinEK = beam.eventGeneratorMinEK * CLHEP::GeV;
96 eventGeneratorMaxEK = beam.eventGeneratorMaxEK * CLHEP::GeV;
98 Rp0 = std::hypot(Xp0,Yp0);
99
100 if (beam.matchDistrFileLength)
101 {BDS::Warning("The option matchDistrFileLength doesn't work with the eventgenerator distribution");}
102}
103
105{
108 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinX >= eventGeneratorMaxX");}
110 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinY >= eventGeneratorMaxY");}
112 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinZ >= eventGeneratorMaxZ");}
114 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinXp >= eventGeneratorMaxXp");}
116 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinYp >= eventGeneratorMaxYp");}
118 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinZp >= eventGeneratorMaxZp");}
120 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinT >= eventGeneratorMaxT");}
122 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinEK >= eventGeneratorMaxEK");}
123}
124
126{
127 if (!acceptedParticlesString.empty())
128 {
129 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
130 std::string particleIDStr;
131 std::stringstream ss(acceptedParticlesString);
132 while (ss >> particleIDStr)
133 {
134 try
135 {// try and see if it's an integer and therefore PDG ID, if not search by string
136 // we try this because std::stoi can throw a std::invalid_argument or
137 // std::out_of_range exception, both of which inherit std::logic_error
138 int particleID = std::stoi(particleIDStr);
139 // we don't use the G4ParticleTable->FindParticle(int) because it unnecessarily
140 // checks for physics readiness and throws an exception. here we just inspect
141 // the encoding dictionary ourselves. it's all typedeffed but it's
142 // std::map<G4int, G4ParticleDefinition*>
143 G4ParticleTable::G4PTblEncodingDictionary* encoding = G4ParticleTable::fEncodingDictionary;
144 auto search = encoding->find(particleID);
145 if (search != encoding->end())
146 {acceptedParticles.push_back(particleID);}
147 else
148 {throw BDSException(__METHOD_NAME__,"PDG ID \"" + particleIDStr + "not found in particle table");}
149 }
150 catch (const std::logic_error&) // else, usual way by string search
151 {
152 G4ParticleDefinition* particleDef = particleTable->FindParticle(particleIDStr);
153 if (!particleDef)
154 {
156 throw BDSException(__METHOD_NAME__, "Particle \"" + particleIDStr + "\" not found.");
157 }
158 else
159 {acceptedParticles.push_back(particleDef->GetPDGEncoding());}
160 }
161 }
162 std::sort(acceptedParticles.begin(), acceptedParticles.end());
163 }
164 else
165 {testOnParticleType = false;}
166}
167
169 G4double rpOriginal,
170 G4double kineticEnergy,
171 G4int pdgID)
172{
173 if (firstTime)
175
176 G4bool x = coords.x > eventGeneratorMinX+X0 && coords.x < eventGeneratorMaxX+X0;
177 G4bool y = coords.y > eventGeneratorMinY+Y0 && coords.y < eventGeneratorMaxY+Y0;
178 G4bool z = coords.z > eventGeneratorMinZ && coords.z < eventGeneratorMaxZ;
179 G4bool xp = coords.xp > eventGeneratorMinXp+Xp0 && coords.xp < eventGeneratorMaxXp+Xp0;
180 G4bool yp = coords.yp > eventGeneratorMinYp+Yp0 && coords.yp < eventGeneratorMaxYp+Yp0;
181 G4bool zp = coords.zp > eventGeneratorMinZp && coords.zp < eventGeneratorMaxZp;
182 G4bool t = coords.T > eventGeneratorMinT && coords.T+T0 < eventGeneratorMaxT+T0;
183 G4bool ek = kineticEnergy > eventGeneratorMinEK && kineticEnergy < eventGeneratorMaxEK;
184 G4bool rp = rpOriginal >= eventGeneratorMinRp && rpOriginal < eventGeneratorMaxRp;
185
186 G4bool allowedParticle = true;
188 {allowedParticle = std::binary_search(acceptedParticles.begin(), acceptedParticles.end(), pdgID);}
189
190 return x && y && z && xp && yp && zp && rp && t && ek && allowedParticle;
191}
192
194{
196 {return G4RotationMatrix();}
197 else
198 {
199 G4RotationMatrix result;
200 result.rotateX(std::asin(-Yp0));
201 result.rotateY(std::asin(Xp0));
202 return result;
203 }
204}
G4RotationMatrix ReferenceBeamMomentumOffset() const
Get a rotation matrix according to Xp0 and Yp0.
G4double Rp0
Cache of limit.
G4double eventGeneratorMinRp
Cache of limit.
G4double eventGeneratorMaxZ
Cache of limit.
G4double eventGeneratorMaxXp
Cache of limit.
G4double eventGeneratorMinXp
Cache of limit.
G4String acceptedParticlesString
Cache of string for parsing on first query.
G4double eventGeneratorMaxEK
Cache of limit.
G4double eventGeneratorMinY
Cache of limit.
G4double eventGeneratorMaxRp
Cache of limit.
G4double eventGeneratorMaxT
Cache of limit.
G4double eventGeneratorMinT
Cache of limit.
G4double eventGeneratorMaxYp
Cache of limit.
G4double eventGeneratorMinZp
Cache of limit.
G4double eventGeneratorMaxX
Cache of limit.
G4bool firstTime
Flag to prepare acceptedParticles on first call.
G4double eventGeneratorMinZ
Cache of limit.
G4double eventGeneratorMaxY
Cache of limit.
std::vector< G4int > acceptedParticles
Vector (sorted) of permitted particles.
G4bool testOnParticleType
Flag whether to bother applying search.
G4double eventGeneratorMinEK
Cache of limit.
G4double eventGeneratorMinYp
Cache of limit.
G4double eventGeneratorMinX
Cache of limit.
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, G4double beamlineS=0)
G4double eventGeneratorMaxZp
Cache of limit.
G4bool AcceptParticle(const BDSParticleCoordsFull &coords, G4double rpOriginal, G4double kineticEnergy, G4int pdgID)
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
G4double Yp0
Centre of distributions.
Definition: BDSBunch.hh:166
G4double T0
Centre of distributions.
Definition: BDSBunch.hh:164
G4double X0
Centre of distributions.
Definition: BDSBunch.hh:160
G4double Xp0
Centre of distributions.
Definition: BDSBunch.hh:165
G4double Y0
Centre of distributions.
Definition: BDSBunch.hh:161
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition: BDSBunch.cc:78
virtual void CheckParameters()
Definition: BDSBunch.cc:195
General exception with possible name of object and message.
Definition: BDSException.hh:35
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
bool matchDistrFileLength
beam parameters
Definition: beamBase.h:55
double eventGeneratorMaxYp
Event generator file filter.
Definition: beamBase.h:143
double eventGeneratorMaxXp
Event generator file filter.
Definition: beamBase.h:141
double eventGeneratorMaxEK
Event generator file filter.
Definition: beamBase.h:151
double eventGeneratorMaxZp
Event generator file filter.
Definition: beamBase.h:145
double eventGeneratorMinYp
Event generator file filter.
Definition: beamBase.h:142
double eventGeneratorMinEK
Event generator file filter.
Definition: beamBase.h:150
double eventGeneratorMaxZ
Event generator file filter.
Definition: beamBase.h:139
double eventGeneratorMinY
Event generator file filter.
Definition: beamBase.h:136
double eventGeneratorMaxRp
Event generator file filter.
Definition: beamBase.h:147
double eventGeneratorMinZ
Event generator file filter.
Definition: beamBase.h:138
double eventGeneratorMinXp
Event generator file filter.
Definition: beamBase.h:140
double eventGeneratorMinZp
Event generator file filter.
Definition: beamBase.h:144
std::string eventGeneratorParticles
Event generator file filter.
Definition: beamBase.h:152
double eventGeneratorMaxY
Event generator file filter.
Definition: beamBase.h:137
double eventGeneratorMinT
Event generator file filter.
Definition: beamBase.h:148
double eventGeneratorMaxT
Event generator file filter.
Definition: beamBase.h:149
double eventGeneratorMaxX
Event generator file filter.
Definition: beamBase.h:135
double eventGeneratorMinRp
Event generator file filter.
Definition: beamBase.h:146
double eventGeneratorMinX
Event generator file filter.
Definition: beamBase.h:134
Beam class.
Definition: beam.h:44
void PrintDefinedParticles()
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())