Jpp  18.0.0-rc.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMoveDetector.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <vector>
5 
6 #include "JDetector/JDetector.hh"
9 #include "JIO/JByteArrayIO.hh"
10 
11 #include "Jeep/JeepToolkit.hh"
12 #include "Jeep/JPrint.hh"
13 #include "Jeep/JParser.hh"
14 #include "Jeep/JMessage.hh"
15 
16 namespace {
17 
18  using namespace JPP;
19 
20  /**
21  * Wild card for string identifier or module identifier.
22  */
23  static const int WILDCARD = -1;
24 
25  static const std::string setx_t = "setx"; //!< Set x-position
26  static const std::string sety_t = "sety"; //!< Set y-position
27  static const std::string setz_t = "setz"; //!< Set z-position
28  static const std::string addx_t = "addx"; //!< Add x-position
29  static const std::string addy_t = "addy"; //!< Add y-position
30  static const std::string addz_t = "addz"; //!< Add z-position
31  static const std::string subx_t = "subx"; //!< Subtract x-position
32  static const std::string suby_t = "suby"; //!< Subtract y-position
33  static const std::string subz_t = "subz"; //!< Subtract z-position
34  static const std::string mul_t = "mul"; //!< Multiply z-position by (1 + value)
35  static const std::string div_t = "div"; //!< Divide z-position by (1 + value)
36 
37 
38  /**
39  * Auxiliary class for module modifications.
40  */
41  struct JModifier {
42  /**
43  * Default constructor.
44  */
45  JModifier()
46  {}
47 
48 
49  /**
50  * Apply action to given module depending on number of values.
51  *
52  * \param module module
53  * \return true if valid action; else false
54  */
55  bool apply(JModule& module) const
56  {
57  if (action == setx_t) {
58 
59  module.set(JVector3D(value, module.getY(), module.getZ()));
60 
61  } else if (action == addx_t) {
62 
63  module.add(JVector3D(value, 0.0, 0.0));
64 
65  } else if (action == subx_t) {
66 
67  module.sub(JVector3D(value, 0.0, 0.0));
68 
69  } else if (action == sety_t) {
70 
71  module.set(JVector3D(module.getX(), value, module.getZ()));
72 
73  } else if (action == addy_t) {
74 
75  module.add(JVector3D(0.0, value, 0.0));
76 
77  } else if (action == suby_t) {
78 
79  module.sub(JVector3D(0.0, value, 0.0));
80 
81  } else if (action == setz_t) {
82 
83  module.set(JVector3D(module.getX(), module.getY(), value));
84 
85  } else if (action == addz_t) {
86 
87  module.add(JVector3D(0.0, 0.0, value));
88 
89  } else if (action == subz_t) {
90 
91  module.sub(JVector3D(0.0, 0.0, value));
92 
93  } else if (action == mul_t) {
94 
95  JVector3D center;
96 
97  if (value > 0.0)
98  center = JVector3D(module.getPosition().getX(),
99  module.getPosition().getY(),
100  module.getPosition().getZ() * (1.0 + value));
101  else
102  center = JVector3D(module.getPosition().getX(),
103  module.getPosition().getY(),
104  module.getPosition().getZ() / (1.0 - value));
105 
106  module.set(center);
107 
108  } else if (action == div_t) {
109 
110  JVector3D center;
111 
112  if (value > 0.0)
113  center = JVector3D(module.getPosition().getX(),
114  module.getPosition().getY(),
115  module.getPosition().getZ() / (1.0 + value));
116  else
117  center = JVector3D(module.getPosition().getX(),
118  module.getPosition().getY(),
119  module.getPosition().getZ() * (1.0 - value));
120 
121  module.set(center);
122 
123  } else { //
124 
125  return false;
126  }
127 
128  return true;
129  }
130 
131 
132  /**
133  * Read modifier from input.
134  *
135  * \param in input stream
136  * \param modifier modifier
137  * \return input stream
138  */
139  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
140  {
141  return in >> modifier.action >> modifier.value;
142  }
143 
144 
145  /**
146  * Write modifier to output.
147  *
148  * \param out output stream
149  * \param modifier modifier
150  * \return output stream
151  */
152  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
153  {
154  return out << modifier.action << ' ' << modifier.value;
155  }
156 
157 
158  std::string action;
159  double value;
160  };
161 
162 
163  /**
164  * Print module modification.
165  *
166  * \param out output stream
167  * \param module module
168  * \param modifier modifier
169  */
170  inline void print(std::ostream& out, const JModule& module, const JModifier& modifier)
171  {
172  using namespace std;
173  using namespace JPP;
174 
175  out << "Modifier" << ' '
176  << getLabel(module.getLocation()) << ' '
177  << setw(10) << module.getID() << ' '
178  << "action " << modifier << endl;
179  }
180 }
181 
182 
183 /**
184  * \file
185  *
186  * Auxiliary program to modify detector calibration.
187  *
188  * Syntax:
189  * <pre>
190  * -M "<module identifier> (setx|addx|subx|sety|addy|suby|setz|addz|subz) value"
191  * -(S|s) "<string number> (setx|addx|subx|sety|addy|suby|setz|addz|subz) value"
192  * </pre>
193  * Options <tt>-M</tt> and <tt>-S</tt> refer to a module and a string, respectively.\n
194  * The values provided for a string modification coherently apply to the modules of the specified string number.\n
195  * The option <tt>-s</tt> is equivalent to option <tt>-S</tt> except that
196  * the action applies only to the optical modules in the string and not the base module.
197  *
198  * If the module identifier or string number is -1,
199  * the action is applied to all modules or strings in the detector, respectively.
200  *
201  * For options <tt>(set|add|sub)(x|y|z)</tt>, the value corresponds to last character of the the quoted action.
202  *
203  * The units of all positions are <tt>m</tt>.
204  *
205  * Multiple options <tt>-M</tt>, <tt>-S</tt> or <tt>-s</tt> will be processed in order of appearance.
206  *
207  * \author mdejong
208  */
209 int main(int argc, char **argv)
210 {
211  using namespace std;
212  using namespace JPP;
213 
215 
216  string detectorFile;
217  vector<pair_type> mod;
218  vector<pair_type> str;
219  vector<pair_type> dos;
220  size_t size;
221  int debug;
222 
223  try {
224 
225  JParser<> zap("Auxiliary program to modify detector.");
226 
227  zap['a'] = make_field(detectorFile, "detector file I/O");
228  zap['M'] = make_field(mod, "module modification") = JPARSER::initialised();
229  zap['S'] = make_field(str, "string modification (optical modules and base module") = JPARSER::initialised();
230  zap['s'] = make_field(dos, "string modification (optical modules only)") = JPARSER::initialised();
231  zap['N'] = make_field(size, "internal buffer size") = 500000;
232  zap['d'] = make_field(debug, "debug level") = 2;
233 
234  zap(argc, argv);
235  }
236  catch(const exception &error) {
237  FATAL(error.what() << endl);
238  }
239 
240 
241  if (getFilenameExtension(detectorFile) != BINARY_DETECTOR_FILE_FORMAT[1]) {
242  FATAL("Wrong file name extension - should be " << BINARY_DETECTOR_FILE_FORMAT[1] << endl);
243  }
244 
246 
247  JByteArrayWriter buffer;
248 
249  buffer.resize(size);
250 
251  fstream io;
252 
253  io.open(detectorFile.c_str(), ios::binary | ios_base::in | ios_base::out);
254 
255  if (!io) {
256  FATAL("Error opening file " << detectorFile << endl);
257  }
258 
259  size_t len = 0;
260 
261  for ( ; ; ) {
262 
263  io.read(buffer.data() + len, buffer.size() - len);
264 
265  len += io.gcount();
266 
267  if (io.eof())
268  break;
269  else
270  buffer.resize(2 * buffer.size());
271  }
272 
273  buffer.resize(len);
274 
275  JByteArrayReader in(buffer.data(), buffer.size());
276 
277  detector.read(in);
278 
279  detector.comment.clear();
280  detector.comment.add(argv[0]);
281 
282  for (vector<pair_type>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
283 
284  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
285 
286  if (module->getID() == i->first || i->first == WILDCARD ){
287 
288  if (debug >= debug_t) {
289  print(cout, *module, i->second);
290  }
291 
292  if (!i->second.apply(*module)) {
293  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
294  }
295  }
296  }
297  }
298 
299 
300  for (vector<pair_type>::const_iterator i = str.begin(); i != str.end(); ++i) {
301 
302  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
303 
304  if (module->getString() == i->first || i->first == WILDCARD) {
305 
306  if (debug >= debug_t) {
307  print(cout, *module, i->second);
308  }
309 
310  if (!i->second.apply(*module)) {
311  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
312  }
313  }
314  }
315  }
316 
317 
318  for (vector<pair_type>::const_iterator i = dos.begin(); i != dos.end(); ++i) {
319 
320  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
321 
322  if (module->getFloor() != 0) {
323 
324  if (module->getString() == i->first || i->first == WILDCARD) {
325 
326  if (debug >= debug_t) {
327  print(cout, *module, i->second);
328  }
329 
330  if (!i->second.apply(*module)) {
331  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
332  }
333  }
334  }
335  }
336  }
337 
338  buffer.clear();
339 
340  detector.write(buffer);
341 
342  io.clear();
343  io.seekp(0, ios_base::beg);
344 
345  io.write(buffer.data(), buffer.size());
346 
347  io.close();
348 }
Utility class to parse command line options.
Definition: JParser.hh:1514
JCombinatorics::pair_type pair_type
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Data structure for a composite optical module.
Definition: JModule.hh:68
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
Definition: JLocation.hh:246
Detector data structure.
Definition: JDetector.hh:89
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
static const char *const BINARY_DETECTOR_FILE_FORMAT[]
JIO binary file format.
Data structure for detector geometry and calibration.
JModule & sub(const JVector3D &pos)
Subtract position.
Definition: JModule.hh:438
I/O formatting auxiliaries.
Detector file.
Definition: JHead.hh:226
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
const JLocation & getLocation() const
Get location.
Definition: JLocation.hh:69
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Byte array binary input.
Definition: JByteArrayIO.hh:25
Auxiliary methods for handling file names, type names and environment.
int getID() const
Get identifier.
Definition: JObjectID.hh:50
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
double getY() const
Get y position.
Definition: JVector3D.hh:104
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
print
Definition: JConvertDusj.sh:44
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
int getString() const
Get string number.
Definition: JLocation.hh:134
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1814
Utility class to parse command line options.
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e. part after last JEEP::FILENAME_SEPARATOR if any.
Definition: JeepToolkit.hh:109
double getX() const
Get x position.
Definition: JVector3D.hh:94
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Byte array binary output.
do set_variable DETECTOR_TXT $WORKDIR detector
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
JModule & set(const JVector3D &pos)
Set position.
Definition: JModule.hh:408
double getZ() const
Get z position.
Definition: JVector3D.hh:115
int debug
debug level
JModule & add(const JVector3D &pos)
Add position.
Definition: JModule.hh:420