BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBunchEventGenerator.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 "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 <cmath>
35#include <map>
36#include <set>
37#include <sstream>
38#include <stdexcept>
39#include <string> // for stoi
40
41BDSBunchEventGenerator::BDSBunchEventGenerator():
42 BDSBunchFileBased("eventgenerator"),
43 eventGeneratorNEventsSkip(0),
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 BDSBunchFileBased::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
78
80 eventGeneratorMinX = beam.eventGeneratorMinX * CLHEP::m;
81 eventGeneratorMaxX = beam.eventGeneratorMaxX * CLHEP::m;
82 eventGeneratorMinY = beam.eventGeneratorMinY * CLHEP::m;
83 eventGeneratorMaxY = beam.eventGeneratorMaxY * CLHEP::m;
84 eventGeneratorMinZ = beam.eventGeneratorMinZ * CLHEP::m;
85 eventGeneratorMaxZ = beam.eventGeneratorMaxZ * CLHEP::m;
94 eventGeneratorMinT = beam.eventGeneratorMinT * CLHEP::s;
95 eventGeneratorMaxT = beam.eventGeneratorMaxT * CLHEP::s;
96 eventGeneratorMinEK = beam.eventGeneratorMinEK * CLHEP::GeV;
97 eventGeneratorMaxEK = beam.eventGeneratorMaxEK * CLHEP::GeV;
99 Rp0 = std::hypot(Xp0,Yp0);
100}
101
103{
106 {throw BDSException(__METHOD_NAME__, "eventGeneratorNEventsSkip < 0");}
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.insert(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.insert(particleDef->GetPDGEncoding());}
160 }
161 }
162 }
163 else
164 {testOnParticleType = false;}
165}
166
168 G4double rpOriginal,
169 G4double kineticEnergy,
170 G4int pdgID)
171{
172 if (firstTime)
174
175 G4bool x = coords.x >= eventGeneratorMinX+X0 && coords.x <= eventGeneratorMaxX+X0;
176 G4bool y = coords.y >= eventGeneratorMinY+Y0 && coords.y <= eventGeneratorMaxY+Y0;
177 G4bool z = coords.z >= eventGeneratorMinZ && coords.z <= eventGeneratorMaxZ;
178 G4bool xp = coords.xp >= eventGeneratorMinXp+Xp0 && coords.xp <= eventGeneratorMaxXp+Xp0;
179 G4bool yp = coords.yp >= eventGeneratorMinYp+Yp0 && coords.yp <= eventGeneratorMaxYp+Yp0;
180 G4bool zp = coords.zp >= eventGeneratorMinZp && coords.zp <= eventGeneratorMaxZp;
181 G4bool t = coords.T >= eventGeneratorMinT && coords.T+T0 <= eventGeneratorMaxT+T0;
182 G4bool ek = kineticEnergy >= eventGeneratorMinEK && kineticEnergy <= eventGeneratorMaxEK;
183 G4bool rp = rpOriginal >= eventGeneratorMinRp && rpOriginal < eventGeneratorMaxRp;
184
185 G4bool allowedParticle = true;
187 {allowedParticle = acceptedParticles.count(pdgID) == 1;}
188
189 return x && y && z && xp && yp && zp && rp && t && ek && allowedParticle;
190}
191
193{
195 {return G4RotationMatrix();}
196 else
197 {
198 G4RotationMatrix result;
199 result.rotateX(std::asin(-Yp0));
200 result.rotateY(std::asin(Xp0));
201 return result;
202 }
203}
G4RotationMatrix ReferenceBeamMomentumOffset() const
Get a rotation matrix according to Xp0 and Yp0.
G4double Rp0
Cache of limit.
G4double eventGeneratorMinRp
Cache of limit.
G4int eventGeneratorNEventsSkip
Cache of limit.
G4double eventGeneratorMaxZ
Cache of limit.
std::set< G4int > acceptedParticles
Vector (sorted) of permitted particles.
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.
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)
Pull out the relevant options and then pass through to BDSBunch::SetOptions().
G4double eventGeneratorMaxZp
Cache of limit.
G4bool AcceptParticle(const BDSParticleCoordsFull &coords, G4double rpOriginal, G4double kineticEnergy, G4int pdgID)
An intermediate layer for any bunch classes that are file based.
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Pull out the relevant options and then pass through to BDSBunch::SetOptions().
G4double Yp0
Centre of distributions.
Definition: BDSBunch.hh:177
G4double T0
Centre of distributions.
Definition: BDSBunch.hh:175
G4double X0
Centre of distributions.
Definition: BDSBunch.hh:171
G4double Xp0
Centre of distributions.
Definition: BDSBunch.hh:176
G4double Y0
Centre of distributions.
Definition: BDSBunch.hh:172
virtual void CheckParameters()
Definition: BDSBunch.cc:223
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++.
double eventGeneratorMaxYp
Event generator file filter.
Definition: beamBase.h:153
double eventGeneratorMaxXp
Event generator file filter.
Definition: beamBase.h:151
double eventGeneratorMaxEK
Event generator file filter.
Definition: beamBase.h:161
double eventGeneratorMaxZp
Event generator file filter.
Definition: beamBase.h:155
double eventGeneratorMinYp
Event generator file filter.
Definition: beamBase.h:152
double eventGeneratorMinEK
Event generator file filter.
Definition: beamBase.h:160
double eventGeneratorMaxZ
Event generator file filter.
Definition: beamBase.h:149
double eventGeneratorMinY
Event generator file filter.
Definition: beamBase.h:146
int eventGeneratorNEventsSkip
Event generator file filter.
Definition: beamBase.h:143
double eventGeneratorMaxRp
Event generator file filter.
Definition: beamBase.h:157
double eventGeneratorMinZ
Event generator file filter.
Definition: beamBase.h:148
double eventGeneratorMinXp
Event generator file filter.
Definition: beamBase.h:150
double eventGeneratorMinZp
Event generator file filter.
Definition: beamBase.h:154
std::string eventGeneratorParticles
Event generator file filter.
Definition: beamBase.h:162
double eventGeneratorMaxY
Event generator file filter.
Definition: beamBase.h:147
double eventGeneratorMinT
Event generator file filter.
Definition: beamBase.h:158
double eventGeneratorMaxT
Event generator file filter.
Definition: beamBase.h:159
double eventGeneratorMaxX
Event generator file filter.
Definition: beamBase.h:145
double eventGeneratorMinRp
Event generator file filter.
Definition: beamBase.h:156
double eventGeneratorMinX
Event generator file filter.
Definition: beamBase.h:144
Beam class.
Definition: beam.h:44
void PrintDefinedParticles()
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())