BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchUserFile.hh
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#ifndef BDSBUNCHUSERFILE_H
20#define BDSBUNCHUSERFILE_H
21
22#include "BDSBunch.hh"
23
24#include <fstream>
25#include <list>
26#include <set>
27#include <sstream>
28#include <string>
29
30#ifdef USE_GZSTREAM
31#include "src-external/gzstream/gzstream.h"
32#endif
33
36
43template <class T>
45{
46public:
48 virtual ~BDSBunchUserFile();
49 virtual void SetOptions(const BDSParticleDefinition* beamParticle,
50 const GMAD::Beam& beam,
51 const BDSBunchType& distrType,
52 G4Transform3D beamlineTransformIn = G4Transform3D::Identity,
53 const G4double beamlineS = 0);
54 virtual void CheckParameters();
55
60 virtual void RecreateAdvanceToEvent(G4int eventOffset);
61
67
70
72 virtual G4bool ExpectChangingParticleType() const {return true;}
73
74private:
75 G4String distrFile;
76 G4String distrFilePath;
77 G4String bunchFormat;
79 G4int nlinesSkip;
80 G4double particleMass;
85
86 void ParseFileFormat();
87 void OpenBunchFile();
88 void SkipLines();
89 void CloseBunchFile();
90
93 void skip(std::stringstream& stream, G4int nvalues);
94
97
100 template <typename Type> void ReadValue(std::stringstream& stream, Type& value);
101
103 struct Doublet {
104 G4String name;
105 G4double unit;
106 };
107
109 std::list<Doublet> fields;
110
112 void CheckConflictingParameters(const std::set<G4String>& s) const;
113
114 template <typename U>
115 void CheckAndParseUnits(const G4String& name, const G4String& rest, U unitParser);
116
118 G4int CountLinesInFile();
119
121 virtual void Initialise();
122
125 void EndOfFileAction();
126
127 G4double ffact;
128 G4bool matchDistrFileLength;
129};
130
131namespace BDS
132{
135 G4double ParseEnergyUnit(const G4String& fmt);
136 G4double ParseLengthUnit(const G4String& fmt);
137 G4double ParseAngleUnit(const G4String& fmt);
138 G4double ParseTimeUnit(const G4String& fmt);
140}
141#endif
A bunch distribution that reads a user specified column file.
void CheckConflictingParameters(const std::set< G4String > &s) const
Check conflicting columns aren't specified in file, e.g. P and Ek. Throw exception if wrong.
virtual void RecreateAdvanceToEvent(G4int eventOffset)
G4double ffact
Cache of flip factor from global constants.
void ReadValue(std::stringstream &stream, Type &value)
void SkipLines()
Read lines according to nlinesIgnore.
G4int lineCounter
Line counter.
G4int nlinesIgnore
Number of lines that will be ignored at the start the file.
G4int CountLinesInFile()
Open the file, skip lines, then count number of lines, then close file again.
T InputBunchFile
The file handler. Templated as could be std::ifstream or igzstream for example.
G4int nlinesSkip
Number of lines that will be skipped after the nlinesIgnore.
virtual BDSParticleCoordsFullGlobal GetNextParticleValid(G4int maxTries)
G4bool changingParticleType
Whether the particle type is a column.
void skip(std::stringstream &stream, G4int nvalues)
void ParseFileFormat()
Parse the column tokens and units factors.
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
G4String distrFilePath
Bunch file including absolute path.
G4String bunchFormat
Format of the file.
G4bool printedOutFirstTime
Whether we've printed out opening the file the first time.
G4double particleMass
Cache of nominal beam particle mass.
std::list< Doublet > fields
List of variables to parse on each line.
void CloseBunchFile()
Close the file handler.
virtual void CheckParameters()
virtual BDSParticleCoordsFull GetNextParticleLocal()
Get the next particle.
virtual void Initialise()
Open the file and skip lines.
G4String distrFile
Bunch file.
virtual G4bool ExpectChangingParticleType() const
For this class we generally can expect a few extra particle types.
void OpenBunchFile()
Open the file and check it's open.
G4bool anEnergyCoordinateInUse
Whether Et, Ek or P are in the columns.
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
G4double beamlineS
Beamline initial S position.
Definition: BDSBunch.hh:201
G4String name
Name of distribution.
Definition: BDSBunch.hh:157
A set of particle coordinates in both local and global.
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
Beam class.
Definition: beam.h:44
Return either G4Tubs or G4CutTubs depending on flat face.
G4double ParseTimeUnit(const G4String &fmt)
G4double ParseEnergyUnit(const G4String &fmt)
G4double ParseAngleUnit(const G4String &fmt)
G4double ParseLengthUnit(const G4String &fmt)
Struct for name and unit pair.
G4double unit
relative to SI units, i.e. mm=0.001 etc.