OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
TrackSave.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: TrackSave.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: TrackSave
10 // The class for the OPAL TSAVE command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/27 09:33:47 $
15 // $Author: Andreas Adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Track/TrackSave.h"
21 #include "Attributes/Attributes.h"
22 #include "Track/Track.h"
24 #include <fstream>
25 #include <iomanip>
26 
27 
28 // Class TrackSave
29 // ------------------------------------------------------------------------
30 
31 
33  Action(1, "TSAVE",
34  "The \"TSAVE\" sub-command saves the defined particles "
35  "on the given file.") {
37  ("FILE", "Name of file to be written", "TRACKSAVE");
38 
40 }
41 
42 
43 TrackSave::TrackSave(const std::string &name, TrackSave *parent):
44  Action(name, parent)
45 {}
46 
47 
49 {}
50 
51 
52 TrackSave *TrackSave::clone(const std::string &name) {
53  return new TrackSave(name, this);
54 }
55 
56 
58  // open output file.
59  std::string file = Attributes::getString(itsAttr[0]);
60  std::ofstream os(file.c_str());
61 
62  if(os.bad()) {
63  throw OpalException("TrackSave::execute()",
64  "Unable to open output file \"" + file + "\".");
65  }
66 
67  os << "\nSaved particle positions:\n";
68  std::streamsize old_prec = os.precision(8);
69  os.setf(std::ios::fixed, std::ios::floatfield);
71 
72  for(unsigned int i = 0; i < bunch->getLocalNum(); i++) {
73  OpalParticle part = bunch->get_part(i);
74  os << part.x() << ' ' << part.px() << ' '
75  << part.y() << ' ' << part.py() << ' '
76  << part.t() << ' ' << part.pt() << '\n';
77  }
78 
79  os << std::flush;
80  os.precision(old_prec);
81  os.setf(std::ios::fixed, std::ios::floatfield);
82 }
double & py()
Get reference to vertical momentum (no dimension).
Definition: OpalParticle.h:122
The base class for all OPAL actions.
Definition: Action.h:30
The base class for all OPAL exceptions.
Definition: OpalException.h:28
double & x()
Get reference to horizontal position in m.
Definition: OpalParticle.h:110
The TSAVE command.
Definition: TrackSave.h:28
virtual ~TrackSave()
Definition: TrackSave.cpp:48
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
OpalParticle get_part(int ii)
virtual TrackSave * clone(const std::string &name)
Make clone.
Definition: TrackSave.cpp:52
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
static Track * block
The block of track data.
Definition: Track.h:63
size_t getLocalNum() const
PartBunchBase< double, 3 > * bunch
The particle bunch to be tracked.
Definition: Track.h:49
double & pt()
Get reference to relative momentum error (no dimension).
Definition: OpalParticle.h:125
TrackSave()
Exemplar constructor.
Definition: TrackSave.cpp:32
const std::string name
double & y()
Get reference to vertical displacement in m.
Definition: OpalParticle.h:113
virtual void execute()
Execute the command.
Definition: TrackSave.cpp:57
double & t()
Get reference to longitudinal displacement c*t in m.
Definition: OpalParticle.h:116
double & px()
Get reference to horizontal momentum (no dimension).
Definition: OpalParticle.h:119
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
OpalParticle position.
Definition: OpalParticle.h:38
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307