Jpp  19.1.0-rc.1
the software that should make you happy
JEditTransmitter.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <set>
5 
7 
8 #include "JSupport/JMeta.hh"
9 
10 #include "Jeep/JContainer.hh"
11 #include "Jeep/JPrint.hh"
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 
16 namespace {
17 
18  using namespace JPP;
19 
20  static const std::string set_t = "set"; //!< Set position
21  static const std::string add_t = "add"; //!< Add position
22  static const std::string sub_t = "sub"; //!< Subtract position
23  static const std::string rot_t = "rot"; //!< Rotate around z-axis by given value [rad]
24 
25  /**
26  * Auxiliary class to apply transmitter modifications.
27  */
28  struct JModifier {
29  /**
30  * Apply modification to given transmitter.
31  *
32  * \param transmitter transmitter
33  * \return true if valid action; else false
34  */
35  bool apply(JTransmitter& transmitter) const
36  {
37  switch (data.size()) {
38 
39  case 1:
40  return apply(transmitter, action, data[0]); // orientation calibration
41 
42  case 3:
43  return apply(transmitter, action, JVector3D(data[0], data[1], data[2])); // 3D position calibration
44 
45  default:
46  return false;
47  }
48  }
49 
50 
51  /**
52  * Read modifier from input.
53  *
54  * \param in input stream
55  * \param modifier modifier
56  * \return input stream
57  */
58  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
59  {
60  using namespace std;
61 
62  if (in >> modifier.id >> modifier.action) {
63 
64  modifier.data.clear();
65 
66  for (double x; in >> x; ) {
67  modifier.data.push_back(x);
68  }
69 
70  in.clear(ios_base::eofbit);
71  }
72 
73  return in;
74  }
75 
76 
77  /**
78  * Write modifier to output.
79  *
80  * \param out output stream
81  * \param modifier modifier
82  * \return output stream
83  */
84  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
85  {
86  out << modifier.id;
87  out << ' ';
88  out << modifier.action;
89 
90  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
91  out << ' ' << *i;
92  }
93 
94  return out;
95  }
96 
97 
98  int id;
99  std::string action;
101 
102 
103  private:
104  /**
105  * Apply orientation calibration to given transmitter.
106  *
107  * \param transmitter transmitter
108  * \param action action
109  * \param value value
110  * \return true if valid action; else false
111  */
112  static bool apply(JTransmitter& transmitter, const std::string& action, const double value)
113  {
114  if (action == rot_t)
115  transmitter.rotate(JRotation3Z(value));
116  else
117  return false;
118 
119  return true;
120  }
121 
122 
123  /**
124  * Apply position calibration to given transmitter.
125  *
126  * \param transmitter transmitter
127  * \param action action
128  * \param pos pos
129  * \return true if valid action; else false
130  */
131  static bool apply(JTransmitter& transmitter, const std::string& action, const JVector3D& pos)
132  {
133  if (action == set_t)
134  transmitter.setPosition(pos);
135  else if (action == add_t)
136  transmitter.add(pos);
137  else if (action == sub_t)
138  transmitter.sub(pos);
139  else
140  return false;
141 
142  return true;
143  }
144  };
145 }
146 
147 /**
148  * \file
149  *
150  * Auxiliary program to modify transmitter configuration.
151  *
152  * Syntax:
153  * <pre>
154  * -S "<string number> (set|add|sub) x y z"
155  * -S "<string number> (rot) phi"
156  * </pre>
157  *
158  * For option <tt>rot</tt>,
159  * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis.\n
160  * The rotation angle is defined in radians.
161  *
162  * The options
163  * <pre>
164  * -A "<identifier> <string number> <floor> x y z"
165  * -r "<string number>"
166  * </pre>
167  * can be used to add and remove a transmitter, respectively.
168  *
169  * \author mdejong
170  */
171 int main(int argc, char **argv)
172 {
173  using namespace std;
174  using namespace JPP;
175 
176  typedef JContainer< vector<JTransmitter> > container_type;
177 
178  string inputFile;
179  string outputFile;
180  vector<JModifier> mod;
182  set<int> keep;
183  set<int> rm;
184  bool squash;
185  int debug;
186 
187  try {
188 
189  JParser<> zap("Auxiliary program to modify transmitter configuration.");
190 
191  zap['f'] = make_field(inputFile, "transmitter input file") = "";
192  zap['o'] = make_field(outputFile, "transmitter output file");
193  zap['S'] = make_field(mod, "transmitter modifier") = JPARSER::initialised();
194  zap['A'] = make_field(add, "add transmitter") = JPARSER::initialised();
195  zap['k'] = make_field(keep, "keep transmitter[s]") = JPARSER::initialised();
196  zap['r'] = make_field(rm, "remove transmitter[s]") = JPARSER::initialised();
197  zap['q'] = make_field(squash, "squash meta data");
198  zap['d'] = make_field(debug, "debug level") = 2;
199 
200  zap(argc, argv);
201  }
202  catch(const exception &error) {
203  FATAL(error.what() << endl);
204  }
205 
206  container_type data;
207 
208  if (inputFile != "") {
209  try {
210  data.load(inputFile.c_str());
211  }
212  catch(const exception& error) {
213  //FATAL(error.what() << endl);
214  }
215  }
216 
217  if (squash) {
218  data.comment.clear();
219  }
220 
221  data.comment.add(JMeta(argc, argv));
222 
223  for (vector<JTransmitter>::const_iterator i = add.begin(); i != add.end(); ++i) {
224  data.push_back(*i);
225  }
226 
227  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
228 
229  for (container_type::iterator target = data.begin(); target != data.end(); ++target) {
230 
231  if (target->getString() == i->id) {
232 
233  DEBUG("Modifier" << ' '
234  << "(" << FILL(4,'0') << target->getString() << "," << FILL(2,'0') << target->getFloor() << FILL() << ")" << ' '
235  << "action" << ' ' << i->action << JEEPZ() << i->data << endl);
236 
237  if (!i->apply(*target)) {
238  ERROR("No valid action: " << *i << endl);
239  }
240  }
241  }
242  }
243 
244  for (container_type::iterator i = data.begin(); i != data.end(); ) {
245 
246  if ((keep.empty() || keep.count(i->getString()) != 0) &&
247  (rm .empty() || rm .count(i->getString()) == 0))
248  ++i;
249  else
250  i = data.erase(i);
251  }
252 
253  data.store(outputFile.c_str());
254 }
Container I/O.
string outputFile
int main(int argc, char **argv)
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1832
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
ROOT I/O of application specific meta data.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
I/O formatting auxiliaries.
Data structure for transmitter.
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:152
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:186
Rotation around Z-axis.
Definition: JRotation3D.hh:87
Data structure for vector in three dimensions.
Definition: JVector3D.hh:36
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:142
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
Utility class to parse command line options.
Definition: JParser.hh:1714
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:330
Target.
Definition: JHead.hh:300
Type definition of transmitter.
Definition: JTransmitter.hh:39
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:66
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition: JContainer.hh:42
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:72