BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchFactory.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 "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 "BDSBunchHalo.hh"
28#include "BDSBunchHaloFlatSigma.hh"
29#include "BDSBunchPtc.hh"
30#include "BDSBunchRing.hh"
31#include "BDSBunchSphere.hh"
32#include "BDSBunchSigmaMatrix.hh"
33#include "BDSBunchSixTrack.hh"
34#include "BDSBunchSquare.hh"
35#include "BDSBunchTwiss.hh"
36#include "BDSBunchType.hh"
37#include "BDSBunchUserFile.hh"
38#include "BDSDebug.hh"
39#include "BDSException.hh"
40#include "BDSUtilities.hh"
41
42#include "parser/beam.h"
43
44#include <utility>
45
46#ifdef USE_GZSTREAM
47#include "src-external/gzstream/gzstream.h"
48#endif
49
51 const GMAD::Beam& beam,
52 const G4Transform3D& beamlineTransform,
53 G4double beamlineS,
54 G4bool generatePrimariesOnlyIn)
55{
56 G4String distrName = G4String(beam.distrType);
57 if (BDS::StrContains(distrName, ":")) // must be eventgeneratorfile:subtype
58 {
59 std::pair<G4String, G4String> ba = BDS::SplitOnColon(distrName);
60 distrName = ba.first; // overwrite with just first bit
61 // we can't generate primaries only with event generator file distribution as this
62 // only works in BDSPrimaryGeneratorAction at run time - it's not really a bunch
63 if (generatePrimariesOnlyIn)
64 {throw BDSException(__METHOD_NAME__, "eventgeneratorfile will not work with generator primaries only.");}
65 }
66
67 // This will exit if no correct bunch type found.
68 BDSBunchType distrType = BDS::DetermineBunchType(distrName);
69
70 return CreateBunch(beamParticle, distrType, beam, beamlineTransform, beamlineS, generatePrimariesOnlyIn);
71}
72
74 BDSBunchType distrType,
75 const GMAD::Beam& beam,
76 const G4Transform3D& beamlineTransform,
77 G4double beamlineS,
78 G4bool generatePrimariesOnlyIn)
79{
80 BDSBunch* bdsBunch = nullptr;
81
82 switch (distrType.underlying())
83 {
84 case BDSBunchType::reference:
85 {bdsBunch = new BDSBunch(); break;}
86 case BDSBunchType::gaussmatrix:
87 case BDSBunchType::gauss:
88 {bdsBunch = new BDSBunchSigmaMatrix(); break;}
89 case BDSBunchType::gausstwiss:
90 {bdsBunch = new BDSBunchTwiss(); break;}
91 case BDSBunchType::circle:
92 {bdsBunch = new BDSBunchCircle(); break;}
93 case BDSBunchType::square:
94 {bdsBunch = new BDSBunchSquare(); break;}
95 case BDSBunchType::ring:
96 {bdsBunch = new BDSBunchRing(); break;}
97 case BDSBunchType::eshell:
98 {bdsBunch = new BDSBunchEShell(); break;}
99 case BDSBunchType::halo:
100 {bdsBunch = new BDSBunchHalo(); break;}
101 case BDSBunchType::composite:
102 {bdsBunch = new BDSBunchComposite(); break;}
103 case BDSBunchType::compositesde:
104 {bdsBunch = new BDSBunchCompositeSDE(); break;}
105 case BDSBunchType::userfile:
106 {
107 G4String distrFile = G4String(beam.distrFile);
108 if(distrFile.rfind("gz") != std::string::npos)
109#ifdef USE_GZSTREAM
110 {
111 if (distrFile.find("tar") != std::string::npos)
112 {throw BDSException(__METHOD_NAME__, "Cannot load tar file -> only gzip compressed");}
113 bdsBunch = new BDSBunchUserFile<igzstream>();}
114#else
115 {
116 G4String message = beam.distrFile + " is a compressed file but BDSIM is compiled without GZIP.";
117 throw BDSException(__METHOD_NAME__, message);
118 }
119#endif
120 else
121 {bdsBunch = new BDSBunchUserFile<std::ifstream>();}
122 break;
123 }
124 case BDSBunchType::ptc:
125 {bdsBunch = new BDSBunchPtc(); break;}
126 case BDSBunchType::sixtrack:
127 {bdsBunch = new BDSBunchSixTrack(); break;}
128 case BDSBunchType::sphere:
129 {bdsBunch = new BDSBunchSphere(); break;}
130 case BDSBunchType::eventgeneratorfile:
131 case BDSBunchType::bdsimsampler:
132 {bdsBunch = new BDSBunchEventGenerator(); break;}
133 case BDSBunchType::box:
134 {bdsBunch = new BDSBunchBox(); break;}
135 case BDSBunchType::halosigma:
136 {bdsBunch = new BDSBunchHaloFlatSigma(); break;}
137 default:
138 {bdsBunch = new BDSBunch(); break;}
139 }
140
141 bdsBunch->SetOptions(beamParticle, beam, distrType, beamlineTransform, beamlineS);
142 bdsBunch->SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
143 bdsBunch->CheckParameters();
144 bdsBunch->Initialise();
145
146 return bdsBunch;
147}
A bunch distribution that produces an uncorrelated uniform random distribution within a 6D box.
Definition: BDSBunchBox.hh:36
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
Bunch halo distribution where the PDF is uniformly distribution in sigma.
A halo distribution based on both twiss parameters and sigmas.
Definition: BDSBunchHalo.hh:35
A bunch distribution that reads a PTC inrays file.
Definition: BDSBunchPtc.hh:35
A bunch distribution that produces an uncorrelated random uniform distribution along a circle in phas...
Definition: BDSBunchRing.hh:36
A 6D Gaussian distribution based on a covariance matrix.
A bunch distribution that reads a SixTrack hits file.
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 according to the twiss parameterisation.
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:255
virtual void Initialise()
Any initialisation - to be used after SetOptions, then CheckParameters.
Definition: BDSBunch.cc:203
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
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.
Definition: BDSUtilities.cc:66
BDSBunchType DetermineBunchType(G4String distrType)
Function that gives corresponding enum value for string (case-insensitive).
Definition: BDSBunchType.cc:55