BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBunchFactory.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 "BDSBunch.hh"
20#include "BDSBunchBox.hh"
21#include "BDSBunchCircle.hh"
22#include "BDSBunchComposite.hh"
23#include "BDSBunchCompositeSDE.hh"
24#include "BDSBunchEShell.hh"
25#include "BDSBunchEventGenerator.hh"
26#include "BDSBunchFactory.hh"
27#include "BDSBunchGauss.hh"
28#include "BDSBunchGaussMatrix.hh"
29#include "BDSBunchGaussMatrixSlowExt.hh"
30#include "BDSBunchGaussSlowExt.hh"
31#include "BDSBunchGaussTwiss.hh"
32#include "BDSBunchGaussTwissSlowExt.hh"
33#include "BDSBunchHalo.hh"
34#include "BDSBunchHaloFlatSigma.hh"
35#include "BDSBunchPtc.hh"
36#include "BDSBunchRing.hh"
37#include "BDSBunchSlowExt.hh"
38#include "BDSBunchSphere.hh"
39#include "BDSBunchSixTrack.hh"
40#include "BDSBunchSquare.hh"
41#include "BDSBunchType.hh"
42#include "BDSBunchUserFile.hh"
43#include "BDSDebug.hh"
44#include "BDSException.hh"
45#include "BDSUtilities.hh"
46
47#include "parser/beam.h"
48
49#include <utility>
50
51#ifdef USE_GZSTREAM
52#include "src-external/gzstream/gzstream.h"
53#endif
54
55#ifdef USE_PLASMA_HDF5
56#include "modules/PLASMA_HDF5/include/BDSBunchHDF5.hh"
57#endif
58
60 const GMAD::Beam& beam,
61 const G4Transform3D& beamlineTransform,
62 G4double beamlineS,
63 G4bool generatePrimariesOnlyIn)
64{
65 G4String distrName = G4String(beam.distrType);
66 if (BDS::StrContains(distrName, ":")) // must be eventgeneratorfile:subtype
67 {
68 std::pair<G4String, G4String> ba = BDS::SplitOnColon(distrName);
69 distrName = ba.first; // overwrite with just first bit
70 // we can't generate primaries only with event generator file distribution as this
71 // only works in BDSPrimaryGeneratorAction at run time - it's not really a bunch
72 if (generatePrimariesOnlyIn)
73 {throw BDSException(__METHOD_NAME__, "eventgeneratorfile will not work with generator primaries only.");}
74 }
75
76 // This will exit if no correct bunch type found.
77 BDSBunchType distrType = BDS::DetermineBunchType(distrName);
78
79 return CreateBunch(beamParticle, distrType, beam, beamlineTransform, beamlineS, generatePrimariesOnlyIn);
80}
81
83 BDSBunchType distrType,
84 const GMAD::Beam& beam,
85 const G4Transform3D& beamlineTransform,
86 G4double beamlineS,
87 G4bool generatePrimariesOnlyIn)
88{
89 BDSBunch* bdsBunch = nullptr;
90
91 switch (distrType.underlying())
92 {
93 case BDSBunchType::reference:
94 {bdsBunch = new BDSBunch(); break;}
95 case BDSBunchType::gauss:
96 {bdsBunch = new BDSBunchGauss(); break;}
97 case BDSBunchType::gaussmatrix:
98 {bdsBunch = new BDSBunchGaussMatrix(); break;}
99 case BDSBunchType::gausstwiss:
100 {bdsBunch = new BDSBunchGaussTwiss(); break;}
101 case BDSBunchType::gaussslowext:
102 {bdsBunch = new BDSBunchGaussSlowExt(); break;}
103 case BDSBunchType::gaussmatrixslowext:
104 {bdsBunch = new BDSBunchGaussMatrixSlowExt(); break;}
105 case BDSBunchType::gausstwissslowext:
106 {bdsBunch = new BDSBunchGaussTwissSlowExt(); break;}
107 case BDSBunchType::circle:
108 {bdsBunch = new BDSBunchCircle(); break;}
109 case BDSBunchType::square:
110 {bdsBunch = new BDSBunchSquare(); break;}
111 case BDSBunchType::ring:
112 {bdsBunch = new BDSBunchRing(); break;}
113 case BDSBunchType::eshell:
114 {bdsBunch = new BDSBunchEShell(); break;}
115 case BDSBunchType::halo:
116 {bdsBunch = new BDSBunchHalo(); break;}
117 case BDSBunchType::composite:
118 {bdsBunch = new BDSBunchComposite(); break;}
119 case BDSBunchType::compositesde:
120 {bdsBunch = new BDSBunchCompositeSDE(); break;}
121 case BDSBunchType::userfile:
122 {
123 G4String distrFile = G4String(beam.distrFile);
124 if(distrFile.rfind("gz") != std::string::npos)
125#ifdef USE_GZSTREAM
126 {
127 if (distrFile.find("tar") != std::string::npos)
128 {throw BDSException(__METHOD_NAME__, "Cannot load tar file -> only gzip compressed");}
129 bdsBunch = new BDSBunchUserFile<igzstream>();}
130#else
131 {
132 G4String message = beam.distrFile + " is a compressed file but BDSIM is compiled without GZIP.";
133 throw BDSException(__METHOD_NAME__, message);
134 }
135#endif
136 else
137 {bdsBunch = new BDSBunchUserFile<std::ifstream>();}
138 break;
139 }
140 case BDSBunchType::ptc:
141 {bdsBunch = new BDSBunchPtc(); break;}
142 case BDSBunchType::sixtrack:
143 {bdsBunch = new BDSBunchSixTrack(); break;}
144 case BDSBunchType::slowext:
145 {bdsBunch = new BDSBunchSlowExt(); break;}
146 case BDSBunchType::sphere:
147 {bdsBunch = new BDSBunchSphere(); break;}
148 case BDSBunchType::eventgeneratorfile:
149 case BDSBunchType::bdsimsampler:
150 {bdsBunch = new BDSBunchEventGenerator(); break;}
151 case BDSBunchType::box:
152 {bdsBunch = new BDSBunchBox(); break;}
153 case BDSBunchType::halosigma:
154 {bdsBunch = new BDSBunchHaloFlatSigma(); break;}
155#ifdef USE_PLASMA_HDF5
156 case BDSBunchType::hdf5:
157 {bdsBunch = new BDSBunchHDF5(); break;}
158#else
159 case BDSBunchType::hdf5:
160 {
161 G4String message = "BDSIM is not compiled with HDF5 support.";
162 throw BDSException(__METHOD_NAME__, message);
163 }
164#endif
165 default:
166 {bdsBunch = new BDSBunch(); break;}
167 }
168
169 bdsBunch->SetOptions(beamParticle, beam, distrType, beamlineTransform, beamlineS);
170 bdsBunch->SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
171 bdsBunch->CheckParameters();
172 bdsBunch->Initialise();
173
174 return bdsBunch;
175}
A bunch distribution that produces an uncorrelated uniform random distribution within a 6D box.
An uncorrelated uniform random distribution within a circle in each dimension.
A distribution that allows mixing of three different distributions in each primary dimension.
A distribution that allows mixing of three different distributions in each primary dimension.
An uncorrelated uniform random distribution within an elliptical shell.
A wrapper of BDSBunch to include a filter for the events loaded by an event generator.
static BDSBunch * CreateBunch(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const G4Transform3D &beamlineTransform=G4Transform3D::Identity, G4double beamlineS=0, G4bool generatePrimariesOnlyIn=false)
factory method
Gauss matrix with slow extraction P-T.
A 6D Gaussian distribution based on a covariance matrix.
Simple Gauss with slow extraction P-T.
Twiss Gauss with slow extraction P-T.
A bunch distribution according to the twiss parameterisation.
A 6D Gaussian distribution based on a covariance matrix.
Bunch halo distribution where the PDF is uniformly distribution in sigma.
A halo distribution based on both twiss parameters and sigmas.
A bunch distribution that reads a PTC inrays file.
A bunch distribution that produces an uncorrelated random uniform distribution along a circle in phas...
A bunch distribution that reads a SixTrack hits file.
A bunch distribution with a linear sweep in momentum in time.
A bunch distribution that produces an uncorrelated uniform random direction distribution over a spher...
A bunch distribution that produces an uncorrelated uniform random distribution within a square in pha...
A bunch distribution that reads a user specified column file.
The base class for bunch distribution generators.
Definition BDSBunch.hh:47
virtual void SetGeneratePrimariesOnly(G4bool generatePrimariesOnlyIn)
Definition BDSBunch.cc:289
virtual void Initialise()
Any initialisation - to be used after SetOptions, then CheckParameters.
Definition BDSBunch.cc:231
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition BDSBunch.cc:82
virtual void CheckParameters()
Definition BDSBunch.cc:223
General exception with possible name of object and message.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
type underlying() const
return underlying value (can be used in switch statement)
std::string distrFile
beam parameters
Definition beamBase.h:52
std::string distrType
beam parameters
Definition beamBase.h:45
Beam class.
Definition beam.h:44
std::pair< G4String, G4String > SplitOnColon(const G4String &formatAndPath)
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
BDSBunchType DetermineBunchType(G4String distrType)
Function that gives corresponding enum value for string (case-insensitive).