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) 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 "BDSBunch.hh"
19#include "BDSBunchBox.hh"
20#include "BDSBunchCircle.hh"
21#include "BDSBunchComposite.hh"
22#include "BDSBunchCompositeSDE.hh"
23#include "BDSBunchEShell.hh"
24#include "BDSBunchEventGenerator.hh"
25#include "BDSBunchFactory.hh"
26#include "BDSBunchGauss.hh"
27#include "BDSBunchGaussMatrix.hh"
28#include "BDSBunchGaussMatrixSlowExt.hh"
29#include "BDSBunchGaussSlowExt.hh"
30#include "BDSBunchGaussTwiss.hh"
31#include "BDSBunchGaussTwissSlowExt.hh"
32#include "BDSBunchHalo.hh"
33#include "BDSBunchHaloFlatSigma.hh"
34#include "BDSBunchPtc.hh"
35#include "BDSBunchRing.hh"
36#include "BDSBunchSlowExt.hh"
37#include "BDSBunchSphere.hh"
38#include "BDSBunchSixTrack.hh"
39#include "BDSBunchSquare.hh"
40#include "BDSBunchType.hh"
41#include "BDSBunchUserFile.hh"
42#include "BDSDebug.hh"
43#include "BDSException.hh"
44#include "BDSUtilities.hh"
45
46#include "parser/beam.h"
47
48#include <utility>
49
50#ifdef USE_GZSTREAM
51#include "src-external/gzstream/gzstream.h"
52#endif
53
54#ifdef USE_PLASMA_HDF5
55#include "modules/PLASMA_HDF5/include/BDSBunchHDF5.hh"
56#endif
57
59 const GMAD::Beam& beam,
60 const G4Transform3D& beamlineTransform,
61 G4double beamlineS,
62 G4bool generatePrimariesOnlyIn)
63{
64 G4String distrName = G4String(beam.distrType);
65 if (BDS::StrContains(distrName, ":")) // must be eventgeneratorfile:subtype
66 {
67 std::pair<G4String, G4String> ba = BDS::SplitOnColon(distrName);
68 distrName = ba.first; // overwrite with just first bit
69 // we can't generate primaries only with event generator file distribution as this
70 // only works in BDSPrimaryGeneratorAction at run time - it's not really a bunch
71 if (generatePrimariesOnlyIn)
72 {throw BDSException(__METHOD_NAME__, "eventgeneratorfile will not work with generator primaries only.");}
73 }
74
75 // This will exit if no correct bunch type found.
76 BDSBunchType distrType = BDS::DetermineBunchType(distrName);
77
78 return CreateBunch(beamParticle, distrType, beam, beamlineTransform, beamlineS, generatePrimariesOnlyIn);
79}
80
82 BDSBunchType distrType,
83 const GMAD::Beam& beam,
84 const G4Transform3D& beamlineTransform,
85 G4double beamlineS,
86 G4bool generatePrimariesOnlyIn)
87{
88 BDSBunch* bdsBunch = nullptr;
89
90 switch (distrType.underlying())
91 {
92 case BDSBunchType::reference:
93 {bdsBunch = new BDSBunch(); break;}
94 case BDSBunchType::gauss:
95 {bdsBunch = new BDSBunchGauss(); break;}
96 case BDSBunchType::gaussmatrix:
97 {bdsBunch = new BDSBunchGaussMatrix(); break;}
98 case BDSBunchType::gausstwiss:
99 {bdsBunch = new BDSBunchGaussTwiss(); break;}
100 case BDSBunchType::gaussslowext:
101 {bdsBunch = new BDSBunchGaussSlowExt(); break;}
102 case BDSBunchType::gaussmatrixslowext:
103 {bdsBunch = new BDSBunchGaussMatrixSlowExt(); break;}
104 case BDSBunchType::gausstwissslowext:
105 {bdsBunch = new BDSBunchGaussTwissSlowExt(); break;}
106 case BDSBunchType::circle:
107 {bdsBunch = new BDSBunchCircle(); break;}
108 case BDSBunchType::square:
109 {bdsBunch = new BDSBunchSquare(); break;}
110 case BDSBunchType::ring:
111 {bdsBunch = new BDSBunchRing(); break;}
112 case BDSBunchType::eshell:
113 {bdsBunch = new BDSBunchEShell(); break;}
114 case BDSBunchType::halo:
115 {bdsBunch = new BDSBunchHalo(); break;}
116 case BDSBunchType::composite:
117 {bdsBunch = new BDSBunchComposite(); break;}
118 case BDSBunchType::compositesde:
119 {bdsBunch = new BDSBunchCompositeSDE(); break;}
120 case BDSBunchType::userfile:
121 {
122 G4String distrFile = G4String(beam.distrFile);
123 if(distrFile.rfind("gz") != std::string::npos)
124#ifdef USE_GZSTREAM
125 {
126 if (distrFile.find("tar") != std::string::npos)
127 {throw BDSException(__METHOD_NAME__, "Cannot load tar file -> only gzip compressed");}
128 bdsBunch = new BDSBunchUserFile<igzstream>();}
129#else
130 {
131 G4String message = beam.distrFile + " is a compressed file but BDSIM is compiled without GZIP.";
132 throw BDSException(__METHOD_NAME__, message);
133 }
134#endif
135 else
136 {bdsBunch = new BDSBunchUserFile<std::ifstream>();}
137 break;
138 }
139 case BDSBunchType::ptc:
140 {bdsBunch = new BDSBunchPtc(); break;}
141 case BDSBunchType::sixtrack:
142 {bdsBunch = new BDSBunchSixTrack(); break;}
143 case BDSBunchType::slowext:
144 {bdsBunch = new BDSBunchSlowExt(); break;}
145 case BDSBunchType::sphere:
146 {bdsBunch = new BDSBunchSphere(); break;}
147 case BDSBunchType::eventgeneratorfile:
148 case BDSBunchType::bdsimsampler:
149 {bdsBunch = new BDSBunchEventGenerator(); break;}
150 case BDSBunchType::box:
151 {bdsBunch = new BDSBunchBox(); break;}
152 case BDSBunchType::halosigma:
153 {bdsBunch = new BDSBunchHaloFlatSigma(); break;}
154#ifdef USE_PLASMA_HDF5
155 case BDSBunchType::hdf5:
156 {bdsBunch = new BDSBunchHDF5(); break;}
157#else
158 case BDSBunchType::hdf5:
159 {
160 G4String message = "BDSIM is not compiled with HDF5 support.";
161 throw BDSException(__METHOD_NAME__, message);
162 }
163#endif
164 default:
165 {bdsBunch = new BDSBunch(); break;}
166 }
167
168 bdsBunch->SetOptions(beamParticle, beam, distrType, beamlineTransform, beamlineS);
169 bdsBunch->SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
170 bdsBunch->CheckParameters();
171 bdsBunch->Initialise();
172
173 return bdsBunch;
174}
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:46
virtual void SetGeneratePrimariesOnly(G4bool generatePrimariesOnlyIn)
Definition BDSBunch.cc:288
virtual void Initialise()
Any initialisation - to be used after SetOptions, then CheckParameters.
Definition BDSBunch.cc:230
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition BDSBunch.cc:81
virtual void CheckParameters()
Definition BDSBunch.cc:222
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:51
std::string distrType
beam parameters
Definition beamBase.h:44
Beam class.
Definition beam.h:43
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).