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) 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#ifndef BDSBUNCHUSERFILE_H
19#define BDSBUNCHUSERFILE_H
20
21#include "BDSBunchFileBased.hh"
22
23#include <fstream>
24#include <list>
25#include <regex>
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
68 virtual G4bool DistributionIsFinished() const {return endOfFileReached;}
69
72
74 virtual G4bool ExpectChangingParticleType() const {return true;}
75
76private:
77 G4String distrFile;
78 G4String distrFilePath;
79 G4String bunchFormat;
80 G4long nlinesIgnore;
81 G4long nlinesSkip;
82 G4long nLinesValidData;
83 G4double particleMass;
88 G4bool endOfFileReached;
89
90 void ParseFileFormat();
91 void OpenBunchFile();
92
95 void SkipNLinesIgnoreIntoFile(G4bool usualPrintOut=true);
96
99 void SkipNLinesSkip(G4bool usualPrintOut=true);
100
101 void CloseBunchFile();
102
105 void skip(std::stringstream& stream, G4int nvalues);
106
109
112 template <typename Type> void ReadValue(std::stringstream& stream, Type& value);
113
115 struct Doublet {
116 G4String name;
117 G4double unit;
118 };
119
121 std::list<Doublet> fields;
122
124 G4bool SkippableLine(const std::string& line) const;
125
127 void CheckConflictingParameters(const std::set<G4String>& s) const;
128
129 template <typename U>
130 void CheckAndParseUnits(const G4String& name, const G4String& rest, U unitParser);
131
137
139 virtual void Initialise();
140
143 void EndOfFileAction();
144
145 G4double ffact;
146 std::regex comment;
147 G4bool matchDistrFileLength;
148};
149
150namespace BDS
151{
154 G4double ParseEnergyUnit(const G4String& fmt);
155 G4double ParseLengthUnit(const G4String& fmt);
156 G4double ParseAngleUnit(const G4String& fmt);
157 G4double ParseTimeUnit(const G4String& fmt);
159}
160#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:218
G4String name
Name of distribution.
Definition BDSBunch.hh:167
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:43
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.