BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBunchUserFile.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
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 "BDSBunchFileBased.hh"
23
24#include <fstream>
25#include <list>
26#include <regex>
27#include <set>
28#include <sstream>
29#include <string>
30
31#ifdef USE_GZSTREAM
32#include "src-external/gzstream/gzstream.h"
33#endif
34
37
44template <class T>
46{
47public:
49 virtual ~BDSBunchUserFile();
50 virtual void SetOptions(const BDSParticleDefinition* beamParticle,
51 const GMAD::Beam& beam,
52 const BDSBunchType& distrType,
53 G4Transform3D beamlineTransformIn = G4Transform3D::Identity,
54 const G4double beamlineS = 0);
55 virtual void CheckParameters();
56
61 virtual void RecreateAdvanceToEvent(G4int eventOffset);
62
68
69 virtual G4bool DistributionIsFinished() const {return endOfFileReached;}
70
73
75 virtual G4bool ExpectChangingParticleType() const {return true;}
76
77private:
78 G4String distrFile;
79 G4String distrFilePath;
80 G4String bunchFormat;
81 G4long nlinesIgnore;
82 G4long nlinesSkip;
83 G4long nLinesValidData;
84 G4double particleMass;
89 G4bool endOfFileReached;
90
91 void ParseFileFormat();
92 void OpenBunchFile();
93
96 void SkipNLinesIgnoreIntoFile(G4bool usualPrintOut=true);
97
100 void SkipNLinesSkip(G4bool usualPrintOut=true);
101
102 void CloseBunchFile();
103
106 void skip(std::stringstream& stream, G4int nvalues);
107
110
113 template <typename Type> void ReadValue(std::stringstream& stream, Type& value);
114
116 struct Doublet {
117 G4String name;
118 G4double unit;
119 };
120
122 std::list<Doublet> fields;
123
125 G4bool SkippableLine(const std::string& line) const;
126
128 void CheckConflictingParameters(const std::set<G4String>& s) const;
129
130 template <typename U>
131 void CheckAndParseUnits(const G4String& name, const G4String& rest, U unitParser);
132
138
140 virtual void Initialise();
141
144 void EndOfFileAction();
145
146 G4double ffact;
147 std::regex comment;
148 G4bool matchDistrFileLength;
149};
150
151namespace BDS
152{
155 G4double ParseEnergyUnit(const G4String& fmt);
156 G4double ParseLengthUnit(const G4String& fmt);
157 G4double ParseAngleUnit(const G4String& fmt);
158 G4double ParseTimeUnit(const G4String& fmt);
160}
161#endif
An intermediate layer for any bunch classes that are file based.
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)
G4int lineCounter
Line counter.
G4long CountNLinesValidDataInFile()
void SkipNLinesSkip(G4bool usualPrintOut=true)
T InputBunchFile
The file handler. Templated as could be std::ifstream or igzstream for example.
G4bool SkippableLine(const std::string &line) const
Return true if a line is all whitespace or is commented out (starts with '#').
virtual BDSParticleCoordsFullGlobal GetNextParticleValid(G4int maxTries)
G4bool changingParticleType
Whether the particle type is a column.
void skip(std::stringstream &stream, G4int nvalues)
void SkipNLinesIgnoreIntoFile(G4bool usualPrintOut=true)
void ParseFileFormat()
Parse the column tokens and units factors.
G4long nlinesIgnore
Number of lines that will be ignored at the start the file.
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Pull out the relevant options and then pass through to BDSBunch::SetOptions().
G4String distrFilePath
Bunch file including absolute path.
G4String bunchFormat
Format of the file.
G4long nlinesSkip
Number of lines that will be skipped after the nlinesIgnore.
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.
G4double beamlineS
Beamline initial S position.
Definition: BDSBunch.hh:219
G4String name
Name of distribution.
Definition: BDSBunch.hh:168
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.