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) 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 "BDSBunchEventGenerator.hh"
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSParticleCoordsFull.hh"
22#include "BDSPhysicsUtilities.hh"
23#include "BDSUtilities.hh"
24#include "BDSWarning.hh"
25
26#include "parser/beam.h"
27
28#include "G4ParticleDefinition.hh"
29#include "G4ParticleTable.hh"
30
31#include "CLHEP/Units/SystemOfUnits.h"
32
33#include <cmath>
34#include <map>
35#include <set>
36#include <sstream>
37#include <stdexcept>
38#include <string> // for stoi
39
40BDSBunchEventGenerator::BDSBunchEventGenerator():
41 BDSBunchFileBased("eventgenerator"),
42 eventGeneratorNEventsSkip(0),
43 eventGeneratorMinX(0),
44 eventGeneratorMaxX(0),
45 eventGeneratorMinY(0),
46 eventGeneratorMaxY(0),
47 eventGeneratorMinZ(0),
48 eventGeneratorMaxZ(0),
49 eventGeneratorMinXp(0),
50 eventGeneratorMaxXp(0),
51 eventGeneratorMinYp(0),
52 eventGeneratorMaxYp(0),
53 eventGeneratorMinZp(0),
54 eventGeneratorMaxZp(0),
55 eventGeneratorMinRp(0),
56 eventGeneratorMaxRp(0),
57 eventGeneratorMinT(0),
58 eventGeneratorMaxT(0),
59 eventGeneratorMinEK(0),
60 eventGeneratorMaxEK(0),
61 Rp0(0),
62 firstTime(true),
63 testOnParticleType(true),
64 acceptedParticlesString("")
65{;}
66
67BDSBunchEventGenerator::~BDSBunchEventGenerator()
68{;}
69
71 const GMAD::Beam& beam,
72 const BDSBunchType& distrType,
73 G4Transform3D beamlineTransformIn,
74 const G4double beamlineSIn)
75{
76 BDSBunchFileBased::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
77
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
102{
105 {throw BDSException(__METHOD_NAME__, "eventGeneratorNEventsSkip < 0");}
107 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinX >= eventGeneratorMaxX");}
109 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinY >= eventGeneratorMaxY");}
111 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinZ >= eventGeneratorMaxZ");}
113 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinXp >= eventGeneratorMaxXp");}
115 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinYp >= eventGeneratorMaxYp");}
117 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinZp >= eventGeneratorMaxZp");}
119 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinT >= eventGeneratorMaxT");}
121 {throw BDSException(__METHOD_NAME__, "eventGeneratorMinEK >= eventGeneratorMaxEK");}
122}
123
125{
126 if (!acceptedParticlesString.empty())
127 {
128 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
129 std::string particleIDStr;
130 std::stringstream ss(acceptedParticlesString);
131 while (ss >> particleIDStr)
132 {
133 try
134 {// try and see if it's an integer and therefore PDG ID, if not search by string
135 // we try this because std::stoi can throw a std::invalid_argument or
136 // std::out_of_range exception, both of which inherit std::logic_error
137 int particleID = std::stoi(particleIDStr);
138 // we don't use the G4ParticleTable->FindParticle(int) because it unnecessarily
139 // checks for physics readiness and throws an exception. here we just inspect
140 // the encoding dictionary ourselves. it's all typedeffed but it's
141 // std::map<G4int, G4ParticleDefinition*>
142 G4ParticleTable::G4PTblEncodingDictionary* encoding = G4ParticleTable::fEncodingDictionary;
143 auto search = encoding->find(particleID);
144 if (search != encoding->end())
145 {acceptedParticles.insert(particleID);}
146 else
147 {throw BDSException(__METHOD_NAME__,"PDG ID \"" + particleIDStr + "\" not found in particle table");}
148 }
149 catch (const std::logic_error&) // else, usual way by string search
150 {
151 G4ParticleDefinition* particleDef = particleTable->FindParticle(particleIDStr);
152 if (!particleDef)
153 {
155 throw BDSException(__METHOD_NAME__, "Particle \"" + particleIDStr + "\" not found.");
156 }
157 else
158 {acceptedParticles.insert(particleDef->GetPDGEncoding());}
159 }
160 }
161 }
162 else
163 {testOnParticleType = false;}
164}
165
167 G4double rpOriginal,
168 G4double kineticEnergy,
169 G4int pdgID)
170{
171 if (firstTime)
173
174 G4bool x = coords.x >= eventGeneratorMinX+X0 && coords.x <= eventGeneratorMaxX+X0;
175 G4bool y = coords.y >= eventGeneratorMinY+Y0 && coords.y <= eventGeneratorMaxY+Y0;
176 G4bool z = coords.z >= eventGeneratorMinZ && coords.z <= eventGeneratorMaxZ;
177 G4bool xp = coords.xp >= eventGeneratorMinXp+Xp0 && coords.xp <= eventGeneratorMaxXp+Xp0;
178 G4bool yp = coords.yp >= eventGeneratorMinYp+Yp0 && coords.yp <= eventGeneratorMaxYp+Yp0;
179 G4bool zp = coords.zp >= eventGeneratorMinZp && coords.zp <= eventGeneratorMaxZp;
180 G4bool t = coords.T >= eventGeneratorMinT && coords.T+T0 <= eventGeneratorMaxT+T0;
181 G4bool ek = kineticEnergy >= eventGeneratorMinEK && kineticEnergy <= eventGeneratorMaxEK;
182 G4bool rp = rpOriginal >= eventGeneratorMinRp && rpOriginal < eventGeneratorMaxRp;
183
184 G4bool allowedParticle = true;
186 {allowedParticle = acceptedParticles.count(pdgID) == 1;}
187
188 return x && y && z && xp && yp && zp && rp && t && ek && allowedParticle;
189}
190
192{
194 {return G4RotationMatrix();}
195 else
196 {
197 G4RotationMatrix result;
198 result.rotateX(std::asin(-Yp0));
199 result.rotateY(std::asin(Xp0));
200 return result;
201 }
202}
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:176
G4double T0
Centre of distributions.
Definition BDSBunch.hh:174
G4double X0
Centre of distributions.
Definition BDSBunch.hh:170
G4double Xp0
Centre of distributions.
Definition BDSBunch.hh:175
G4double Y0
Centre of distributions.
Definition BDSBunch.hh:171
virtual void CheckParameters()
Definition BDSBunch.cc:222
General exception with possible name of object and message.
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
fo event generator file filter
Definition beamBase.h:153
double eventGeneratorMaxXp
fo event generator file filter
Definition beamBase.h:151
double eventGeneratorMaxEK
fo event generator file filter
Definition beamBase.h:161
double eventGeneratorMaxZp
fo event generator file filter
Definition beamBase.h:155
double eventGeneratorMinYp
fo event generator file filter
Definition beamBase.h:152
double eventGeneratorMinEK
fo event generator file filter
Definition beamBase.h:160
double eventGeneratorMaxZ
fo event generator file filter
Definition beamBase.h:149
double eventGeneratorMinY
fo event generator file filter
Definition beamBase.h:146
int eventGeneratorNEventsSkip
fo event generator file filter
Definition beamBase.h:143
double eventGeneratorMaxRp
fo event generator file filter
Definition beamBase.h:157
double eventGeneratorMinZ
fo event generator file filter
Definition beamBase.h:148
double eventGeneratorMinXp
fo event generator file filter
Definition beamBase.h:150
double eventGeneratorMinZp
fo event generator file filter
Definition beamBase.h:154
std::string eventGeneratorParticles
fo event generator file filter
Definition beamBase.h:162
double eventGeneratorMaxY
fo event generator file filter
Definition beamBase.h:147
double eventGeneratorMinT
fo event generator file filter
Definition beamBase.h:158
double eventGeneratorMaxT
fo event generator file filter
Definition beamBase.h:159
double eventGeneratorMaxX
fo event generator file filter
Definition beamBase.h:145
double eventGeneratorMinRp
fo event generator file filter
Definition beamBase.h:156
double eventGeneratorMinX
fo event generator file filter
Definition beamBase.h:144
Beam class.
Definition beam.h:43
void PrintDefinedParticles()
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())