BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchComposite.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 "BDSBunchComposite.hh"
21#include "BDSBunchFactory.hh"
22#include "BDSDebug.hh"
23#include "BDSException.hh"
24#include "BDSParticleCoordsFull.hh"
25
26#include "parser/beam.h"
27
28BDSBunchComposite::BDSBunchComposite():
29BDSBunch("composite"),
30 xBunch(nullptr),
31 yBunch(nullptr),
32 zBunch(nullptr)
33{;}
34
35BDSBunchComposite::~BDSBunchComposite()
36{
37 delete xBunch;
38 delete yBunch;
39 delete zBunch;
40}
41
43 const GMAD::Beam& beam,
44 const BDSBunchType& distrType,
45 G4Transform3D beamlineTransformIn,
46 G4double beamlineSIn)
47{
48 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn,beamlineSIn);
49
50 delete xBunch;
51 delete yBunch;
52 delete zBunch;
53 BDSBunchType xType;
54 BDSBunchType yType;
55 BDSBunchType zType;
56 try
57 {xType = BDS::DetermineBunchType(beam.xDistrType);}
58 catch (BDSException& e)
59 {e.AppendToMessage("Error with x dimension composite beam distribution"); throw e;}
60 try
61 {yType = BDS::DetermineBunchType(beam.yDistrType);}
62 catch (BDSException& e)
63 {e.AppendToMessage("Error with y dimension composite beam distribution"); throw e;}
64 try
65 {zType = BDS::DetermineBunchType(beam.zDistrType);}
66 catch (BDSException& e)
67 {e.AppendToMessage("Error with z dimension composite beam distribution"); throw e;}
68
69 if (xType == BDSBunchType::composite ||
70 yType == BDSBunchType::composite ||
71 zType == BDSBunchType::composite)
72 {throw BDSException(__METHOD_NAME__, "x,y,z distributions cannot be 'composite'");}
73
74 // here we don't have generatePrimariesOnly bool but this will be overridden with the
75 // separate call to SetGeneratePrimariesOnly in BDSBunchFactory
76 xBunch = BDSBunchFactory::CreateBunch(beamParticle, xType, beam, beamlineTransformIn);
77 yBunch = BDSBunchFactory::CreateBunch(beamParticle, yType, beam, beamlineTransformIn);
78 zBunch = BDSBunchFactory::CreateBunch(beamParticle, zType, beam, beamlineTransformIn);
79
80 name = "composite: x: " + xBunch->Name() + ", y: " + yBunch->Name() + ", z: " + zBunch->Name();
81}
82
83void BDSBunchComposite::SetGeneratePrimariesOnly(G4bool generatePrimariesOnlyIn)
84{
85 BDSBunch::SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
86 xBunch->SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
87 yBunch->SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
88 zBunch->SetGeneratePrimariesOnly(generatePrimariesOnlyIn);
89}
90
92{
93 xBunch->CheckParameters();
94 yBunch->CheckParameters();
95 zBunch->CheckParameters();
96}
97
99{
100 auto x = xBunch->GetNextParticleLocal();
101 auto y = yBunch->GetNextParticleLocal();
102 auto z = zBunch->GetNextParticleLocal();
103
107
108 // TODO - the weight only comes from the x distribution here... should it be product of all?
109 BDSParticleCoordsFull result(x.x, y.y, z.z,
110 x.xp, y.yp, z.zp,
111 z.T, z.s,
112 z.totalEnergy,
113 x.weight);
114 return result;
115}
virtual BDSParticleCoordsFull GetNextParticleLocal()
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
virtual void SetGeneratePrimariesOnly(G4bool generatePrimariesOnlyIn)
virtual void CheckParameters()
static BDSBunch * CreateBunch(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const G4Transform3D &beamlineTransform=G4Transform3D::Identity, G4double beamlineS=0, G4bool generatePrimariesOnlyIn=false)
factory method
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
G4bool ParticleDefinitionHasBeenUpdated() const
Definition: BDSBunch.hh:128
G4bool particleDefinitionHasBeenUpdated
Definition: BDSBunch.hh:185
virtual void SetGeneratePrimariesOnly(G4bool generatePrimariesOnlyIn)
Definition: BDSBunch.cc:255
virtual BDSParticleCoordsFull GetNextParticleLocal()
Definition: BDSBunch.cc:244
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
G4String name
Name of distribution.
Definition: BDSBunch.hh:157
virtual void CheckParameters()
Definition: BDSBunch.cc:195
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++.
std::string zDistrType
beam parameters
Definition: beamBase.h:48
std::string yDistrType
beam parameters
Definition: beamBase.h:47
std::string xDistrType
beam parameters
Definition: beamBase.h:46
Beam class.
Definition: beam.h:44
BDSBunchType DetermineBunchType(G4String distrType)
Function that gives corresponding enum value for string (case-insensitive).
Definition: BDSBunchType.cc:55