BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSSurvey.cc
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#include "BDSSurvey.hh"
19
20#include "BDSAcceleratorComponent.hh"
21#include "BDSBeamline.hh"
22#include "BDSBeamlineElement.hh"
23#include "BDSBeamPipeInfo.hh"
24#include "BDSDebug.hh"
25#include "BDSMagnet.hh"
26#include "BDSMagnetStrength.hh"
27#include "BDSVersion.hh"
28
29#include <fstream>
30#include <iomanip>
31#include <ctime>
32#include <string>
33
34using std::setw;
35
36BDSSurvey::BDSSurvey(const G4String& filename):
37 nullStrength(new BDSMagnetStrength()),
38 gp(14)
39{
40 magnetKeys = BDSMagnetStrength::AllKeys();
41
42 G4cout << __METHOD_NAME__ << "Generating Survey: " << filename << " ..." << G4endl;
43 survey.open(filename);
44 WriteHeader();
45}
46
47BDSSurvey::~BDSSurvey()
48{
49 if (survey.is_open())
50 {survey.close();}
51 delete nullStrength;
52}
53
55{
56 time_t currenttime;
57 time(&currenttime);
58 std::string timestring = asctime(localtime(&currenttime));
59 timestring = timestring.substr(0,timestring.size()-1);
60
61 survey << "# BDSIM survey - created at "<< timestring << " with BDSIM version " << BDSIM_GIT_VERSION << G4endl;
62 survey << "# Note: this information is written to the ROOT file and loadable with pybdsim -> this is the preferred path to this data." << G4endl;
63 survey << std::left
64 << setw(20) << "Type " << " "
65 << setw(40) << "Name " << " "
66 << setw(gp) << "SStart[m] " << " "
67 << setw(gp) << "SMid[m] " << " "
68 << setw(gp) << "SEnd[m] " << " "
69 << setw(gp) << "ChordLength[m]" << " "
70 << setw(gp) << "ArcLength[m] " << " "
71 << setw(gp) << "X[m] " << " "
72 << setw(gp) << "Y[m] " << " "
73 << setw(gp) << "Z[m] " << " "
74 << setw(gp) << "Phi[rad] " << " "
75 << setw(gp) << "Theta[rad] " << " "
76 << setw(gp) << "Psi[rad] " << " "
77 << setw(gp) << "Aper1[m] " << " "
78 << setw(gp) << "Aper2[m] " << " "
79 << setw(gp) << "Aper3[m] " << " "
80 << setw(gp) << "Aper4[m] " << " "
81 << setw(15) << "Aper_Type " << " "
82 << setw(gp) << "Angle[rad] ";
83
84 for (auto const& key : magnetKeys)
85 {
86 const G4String unit = BDSMagnetStrength::UnitName(key);
87 if (!unit.empty())
88 {survey << " " << setw(18) << key + "[" + unit + "]";}
89 else
90 {survey << " " << setw(18) << key;}
91 }
92 survey << G4endl;
93
94 survey.setf(std::ios::fixed, std::ios::floatfield);
95 survey.setf(std::ios::showpoint);
96 // print up to 7 significant numbers
97 survey.precision(7);
98}
99
101{
102 for (auto element : *beamline)
103 {Write(element);}
104 survey << "# Total length = " << beamline->GetTotalChordLength()/CLHEP::m << "m" << G4endl;
105 survey << "# Total arc length = " << beamline->GetTotalArcLength()/CLHEP::m << "m" << G4endl;
106 survey.close();
107}
108
110{
111 BDSAcceleratorComponent* acceleratorComponent = beamlineElement->GetAcceleratorComponent();
112
113 G4RotationMatrix* rm = beamlineElement->GetRotationMiddle();
114 G4double phi = rm->getPhi();
115 G4double theta = rm->getTheta();
116 G4double psi = rm->getPsi();
117
118 G4double sStart = beamlineElement->GetSPositionStart() /CLHEP::m;
119 G4double sMiddle = beamlineElement->GetSPositionMiddle()/CLHEP::m;
120 G4double sEnd = beamlineElement->GetSPositionEnd() /CLHEP::m;
121 G4ThreeVector pos = beamlineElement->GetPositionMiddle();
122
123 BDSBeamPipeInfo* beamPipeInfo = acceleratorComponent->GetBeamPipeInfo();
124
125 G4String ty = acceleratorComponent->GetType();
126 if (ty.empty())
127 {ty = "unknown";}
128 survey << std::left << std::setprecision(6) << std::fixed
129 << setw(20) << ty << " "
130 << setw(40) << acceleratorComponent->GetName() << " "
131 << setw(gp) << sStart << " "
132 << setw(gp) << sMiddle << " "
133 << setw(gp) << sEnd << " "
134 << setw(gp) << acceleratorComponent->GetChordLength()/CLHEP::m << " "
135 << setw(gp) << acceleratorComponent->GetArcLength()/CLHEP::m << " "
136 << setw(gp) << pos.x()/CLHEP::m << " "
137 << setw(gp) << pos.y()/CLHEP::m << " "
138 << setw(gp) << pos.z()/CLHEP::m << " "
139 << setw(gp) << phi/CLHEP::radian << " "
140 << setw(gp) << theta/CLHEP::radian << " "
141 << setw(gp) << psi/CLHEP::radian << " "
142 << setw(gp) << (beamPipeInfo ? beamPipeInfo->aper1/CLHEP::m : 0) << " "
143 << setw(gp) << (beamPipeInfo ? beamPipeInfo->aper2/CLHEP::m : 0) << " "
144 << setw(gp) << (beamPipeInfo ? beamPipeInfo->aper3/CLHEP::m : 0) << " "
145 << setw(gp) << (beamPipeInfo ? beamPipeInfo->aper4/CLHEP::m : 0) << " "
146 << setw(15) << (beamPipeInfo ? beamPipeInfo->beamPipeType : 0) << " "
147 << setw(gp) << acceleratorComponent->GetAngle();
148
149 BDSMagnetStrength const* st;
150 if (BDSMagnet* magCast = dynamic_cast<BDSMagnet*>(acceleratorComponent))
151 {
152 st = magCast->MagnetStrength();
153 if (!st)
154 {st = nullStrength;}
155 }
156 else
157 {st = nullStrength;}
158
160 survey << G4endl;
161}
Abstract class that represents a component of an accelerator.
virtual G4String GetName() const
The name of the component without modification.
virtual G4double GetChordLength() const
virtual G4double GetArcLength() const
BDSBeamPipeInfo * GetBeamPipeInfo() const
G4String GetType() const
Get a string describing the type of the component.
Holder class for all information required to describe a beam pipe model.
G4double aper3
Public member for direct access.
G4double aper1
Public member for direct access.
G4double aper4
Public member for direct access.
G4double aper2
Public member for direct access.
BDSBeamPipeType beamPipeType
Public member for direct access.
A class that holds a fully constructed BDSAcceleratorComponent as well as any information relevant to...
G4ThreeVector GetPositionMiddle() const
Accessor.
G4double GetSPositionEnd() const
Accessor.
BDSAcceleratorComponent * GetAcceleratorComponent() const
Accessor.
G4double GetSPositionMiddle() const
Accessor.
G4RotationMatrix * GetRotationMiddle() const
Accessor.
G4double GetSPositionStart() const
Accessor.
A vector of BDSBeamlineElement instances - a beamline.
G4double GetTotalChordLength() const
Get the total length of the beamline - the sum of the chord length of each element.
G4double GetTotalArcLength() const
Get the total ARC length for the beamline - ie the maximum s position.
Efficient storage of magnet strengths.
std::ostream & WriteValuesInSIUnitsForSuvey(std::ostream &out, const G4int precision=14) const
static const std::vector< G4String > & AllKeys()
Accessor to all keys.
static G4String UnitName(const G4String &key)
Access a unit name for a given key.
Abstract base class that implements features common to all magnets.
Definition BDSMagnet.hh:44
const int gp
General precision - number of characters / item.
Definition BDSSurvey.hh:67
BDSMagnetStrength const * nullStrength
Definition BDSSurvey.hh:62
std::ofstream survey
Output file stream.
Definition BDSSurvey.hh:57
std::vector< G4String > magnetKeys
Cache of all the possible magnet strength parameters.
Definition BDSSurvey.hh:65
void WriteHeader()
Write header.
Definition BDSSurvey.cc:54
void Write(BDSBeamlineElement *beamlineElement)
write line
Definition BDSSurvey.cc:109