Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditDetector.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <vector>
5 
6 #include "TRandom3.h"
7 
8 #include "JDetector/JDetector.hh"
13 #include "JGeometry3D/JVector3D.hh"
14 #include "JTools/JConstants.hh"
15 #include "JLang/JException.hh"
16 #include "JSupport/JMeta.hh"
17 
18 #include "Jeep/JeepToolkit.hh"
19 #include "Jeep/JParser.hh"
20 #include "Jeep/JMessage.hh"
21 
22 
23 namespace {
24 
25  using namespace JPP;
26 
27 
28  /**
29  * Auxiliary class to apply detector modifications.
30  *
31  * Note that the internal identifier may apply to a module as well as a string.
32  */
33  class JModifier {
34  public:
35  /**
36  * Wild card for module and string identifier.
37  */
38  static const int WILD_CARD = -1;
39 
40 
41  /**
42  * Default constructor.
43  */
44  JModifier()
45  {}
46 
47 
48  /**
49  * Apply modification to given module.
50  *
51  * \param module module
52  * \return true if valid action; else false
53  */
54  bool apply(JModule& module) const
55  {
56  switch (data.size()) {
57 
58  case 1:
59  return apply(module, action, data[0]); // time/orientation/height calibration
60 
61  case 2:
62  return apply(module, action, JVector3D(data[0], data[1], 0.0)); // 2D position calibration
63 
64  case 3:
65  return apply(module, action, JVector3D(data[0], data[1], data[2])); // 3D position calibration
66 
67  default:
68  return false;
69  }
70  }
71 
72 
73  /**
74  * Read modifier from input.
75  *
76  * \param in input stream
77  * \param modifier modifier
78  * \return input stream
79  */
80  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
81  {
82  using namespace std;
83 
84  if (in >> modifier.id >> modifier.action) {
85 
86  modifier.data.clear();
87 
88  for (double x; in >> x; ) {
89  modifier.data.push_back(x);
90  }
91 
92  if (!modifier.data.empty())
93  in.clear(ios_base::eofbit);
94  else
95  in.setstate(ios_base::badbit);
96  }
97 
98  return in;
99  }
100 
101 
102  /**
103  * Write modifier to output.
104  *
105  * \param out output stream
106  * \param modifier modifier
107  * \return output stream
108  */
109  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
110  {
111  out << modifier.id;
112  out << ' ';
113  out << modifier.action;
114 
115  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
116  out << ' ' << *i;
117  }
118 
119  return out;
120  }
121 
122 
123  int id;
124  std::string action;
125  std::vector<double> data;
126 
127  private:
128 
129  /**
130  * Apply time/orientation calibration to given module.
131  *
132  * \param module module
133  * \param action action
134  * \param value value
135  * \return true if valid action; else false
136  */
137  static bool apply(JModule& module, const std::string& action, const double value)
138  {
139  if (action == "set") { // actions with fixed values
140 
141  module.set(value);
142 
143  } else if (action == "add") {
144 
145  module.add(value);
146 
147  } else if (action == "sub") {
148 
149  module.sub(value);
150 
151  } else if (action == "rot") {
152 
153  const JVector3D center = module.getPosition();
154 
155  module.sub(center);
156 
157  module.rotate(JRotation3Z(value));
158 
159  module.add(center);
160 
161  } else if (action == "mul") {
162 
163  const JVector3D center(module.getPosition().getX(),
164  module.getPosition().getY(),
165  module.getPosition().getZ() * value);
166 
167  module.set(center);
168 
169  } else if (action == "randadd") { // actions with random values
170 
171  module.add(gRandom->Gaus(0.0, value));
172 
173  } else if (action == "randsub") {
174 
175  module.sub(gRandom->Gaus(0.0, value));
176 
177  } else if (action == "randrot"){
178 
179  const JVector3D center = module.getPosition();
180 
181  module.sub(center);
182 
183  module.rotate(JRotation3Z(gRandom->Gaus(0.0, value)));
184 
185  module.add(center);
186 
187  } else if (action == "randmul") {
188 
189  const JVector3D center(module.getPosition().getX(),
190  module.getPosition().getY(),
191  module.getPosition().getZ() * gRandom->Gaus(1.0, value));
192 
193  module.set(center);
194 
195  } else {
196 
197  return false;
198  }
199 
200  return true;
201  }
202 
203 
204  /**
205  * Apply position calibration to given module.
206  *
207  * \param module module
208  * \param action action
209  * \param pos pos
210  * \return true if valid action; else false
211  */
212  static bool apply(JModule& module, const std::string& action, const JVector3D& pos)
213  {
214  const JVector3D randpos(gRandom->Gaus(0.0, pos.getX()),
215  gRandom->Gaus(0.0, pos.getY()),
216  gRandom->Gaus(0.0, pos.getZ()));
217 
218  if (action == "set") // actions with fixed values
219  module.set(pos);
220  else if (action == "add")
221  module.add(pos);
222  else if (action == "sub")
223  module.sub(pos);
224  else if (action == "randset") // actions with random values
225  module.set(randpos);
226  else if (action == "randadd")
227  module.add(randpos);
228  else if (action == "randsub")
229  module.sub(randpos);
230  else
231  return false;
232 
233  return true;
234  }
235  };
236 
237 
238  /**
239  * Auxiliary class to apply PMT status modifications.
240  */
241  class JPMTModifier :
242  public JPMTIdentifier
243  {
244  public:
245  /**
246  * Default constructor.
247  */
248  JPMTModifier()
249  {}
250 
251 
252  /**
253  * Apply modification to given PMT.
254  *
255  * \param pmt PMT
256  * \return true if valid action; else false
257  */
258  bool apply(JPMT& pmt) const
259  {
260  try {
261 
262  if (action == "set") {
263 
264  pmt.set(getPMTStatusBit(value));
265 
266  } else if (action == "reset") {
267 
268  pmt.reset(getPMTStatusBit(value));
269 
270  } else {
271 
272  return false;
273  }
274 
275  return true;
276  }
277  catch(const std::exception&) {}
278 
279  return false;
280  }
281 
282 
283  /**
284  * Read PMT modifier from input.
285  *
286  * \param in input stream
287  * \param modifier modifier
288  * \return input stream
289  */
290  friend inline std::istream& operator>>(std::istream& in, JPMTModifier& modifier)
291  {
292  return in >> static_cast<JPMTIdentifier&>(modifier) >> modifier.action >> modifier.value;
293  }
294 
295 
296  /**
297  * Write modifier to output.
298  *
299  * \param out output stream
300  * \param modifier modifier
301  * \return output stream
302  */
303  friend inline std::ostream& operator<<(std::ostream& out, const JPMTModifier& modifier)
304  {
305  out << static_cast<const JPMTIdentifier&>(modifier);
306  out << ' ';
307  out << modifier.action;
308  out << ' ';
309  out << modifier.value;
310 
311  return out;
312  }
313 
314 
315  int id;
316  std::string action;
317  std::string value;
318  };
319 }
320 
321 
322 /**
323  * \file
324  *
325  * Auxiliary program to modify detector calibration.
326  *
327  * Syntax:
328  * <pre>
329  * -M "<module identifier> (set|add|sub|randset|randadd|randsub) x0 [x1 [x2]]"
330  * -S "<string identifier> (set|add|sub|randset|randadd|randsub) x0 [x1 [x2]]"
331  * -M "<module identifier> (rot|randrot) phi"
332  * -S "<string identifier> (rot|randrot) phi"
333  * -M "<module identifier> (mul|randmul) factor"
334  * -S "<string identifier> (mul|randmul) factor"
335  * -P "<PMT identifier> (set|reset) PMT_STATUS"
336  * -\@ "<key>=<value>[;<key>=<value>"
337  * </pre>
338  * Options <tt>-M</tt> and <tt>-S</tt> refer to a module and a string, respectively.\n
339  *
340  * If module identifier or string identifier is -1, the action takes place on all modules or strings in the detector, respectively.\n
341  * The number of values apply to position or time calibration in the following way:
342  * -# time calibration <tt>(t = x0)</tt>
343  * -# position calibration <tt>(x = x0, y = x1, z = 0)</tt>
344  * -# position calibration <tt>(x = x0, y = x1, z = x2)</tt>
345  *
346  * The rotation is anti-clockwise around the z-axis.\n
347  * The multiplication applies to the z-coordinates.
348  *
349  * Note that the angles are defined in radians.\n
350  *
351  * Option <tt>-\@</tt> refers to the header information.\n
352  * The list of possible keys can be obtained using JPrintDetector.cc with option <tt>-O header</tt>.
353  *
354  * Multiple options <tt>-M</tt>, <tt>-S</tt> or <tt>-\@</tt> will be processed in order of appearance.
355  *
356  * Note that if the output file name is the same as the input file name,
357  * the original file will be overwritten.
358  * \author mdejong
359  */
360 int main(int argc, char **argv)
361 {
362  using namespace std;
363  using namespace JPP;
364 
365  string inputFile;
366  string outputFile;
367  vector<JModifier> mod;
368  vector<JModifier> str;
369  vector<string> hdr;
371  int debug;
372 
373  try {
374 
375  JParser<> zap("Auxiliary program to modify detector.");
376 
377  zap['a'] = make_field(inputFile);
378  zap['M'] = make_field(mod) = JPARSER::initialised();
379  zap['S'] = make_field(str) = JPARSER::initialised();
380  zap['@'] = make_field(hdr) = JPARSER::initialised();
381  zap['P'] = make_field(pmt) = JPARSER::initialised();
382  zap['o'] = make_field(outputFile);
383  zap['d'] = make_field(debug) = 2;
384 
385  zap(argc, argv);
386  }
387  catch(const exception &error) {
388  FATAL(error.what() << endl);
389  }
390 
392 
393  try {
394  load(inputFile, detector);
395  }
396  catch(const JException& error) {
397  FATAL(error);
398  }
399 
400 
401  gRandom->SetSeed(0);
402 
403 
404  detector.comment.add(JMeta(argc,argv));
405 
406 
407  if (!hdr.empty()) {
408 
409  JProperties helper = detector.getProperties();
410 
411  for (vector<string>::const_iterator i = hdr.begin(); i != hdr.end(); ++i) {
412 
413  istringstream is(*i);
414 
415  is >> helper;
416  }
417  }
418 
419 
420  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
421 
422  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
423 
424  if (module->getID() == i->id || i->id == JModifier::WILD_CARD ){
425 
426  DEBUG("Modifier" << ' '
427  << "(" << setw(3) << module->getString() << "," << setw(2) << module->getFloor() << ")" << ' '
428  << setw(8) << module->getID() << ' '
429  << "action" << ' ' << i->action << endl);
430 
431  if (!i->apply(*module)) {
432  ERROR("No valid action: " << *i << endl);
433  }
434  }
435  }
436  }
437 
438 
439  for (vector<JModifier>::const_iterator i = str.begin(); i != str.end(); ++i) {
440 
441  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
442 
443  if (module->getString() == i->id || i->id == JModifier::WILD_CARD) {
444 
445  DEBUG("Modifier" << ' '
446  << "(" << setw(3) << module->getString() << "," << setw(2) << module->getFloor() << ")" << ' '
447  << setw(8) << module->getID() << ' '
448  << "action" << ' ' << i->action << endl);
449 
450  if (!i->apply(*module)) {
451  ERROR("No valid action: " << *i << endl);
452  }
453  }
454  }
455  }
456 
457 
458  for (vector<JPMTModifier>::const_iterator i = pmt.begin(); i != pmt.end(); ++i) {
459 
460  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
461 
462  if (module->getID() == i->getModuleID() || i->getModuleID() == JModifier::WILD_CARD) {
463 
464  DEBUG("PMT modifier" << ' '
465  << "(" << setw(8) << module->getID() << "," << setw(2) << i->getPMTAddress() << ")" << ' '
466  << "action" << ' ' << i->action << ' '
467  << "value" << ' ' << i->value << endl);
468 
469  if (i->getPMTAddress() < 0 ||
470  i->getPMTAddress() >= getNumberOfPMTs(*module) ||
471  !i->apply(module->getPMT(i->getPMTAddress()))) {
472  ERROR("No valid action: " << *i << endl);
473  }
474  }
475  }
476  }
477 
478  try {
480  }
481  catch(const JException& error) {
482  FATAL(error);
483  }
484 }
void set(const JPMTStatusBits_t bit)
Set PMT status.
Definition: JPMT.hh:154
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:71
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
static const JGetPMTStatusBit getPMTStatusBit
Function object to map key to PMT status bit.
Definition: JPMT.hh:365
Exceptions.
std::istream & operator>>(std::istream &in, JHead &header)
Read header from input.
Data structure for a composite optical module.
Definition: JModule.hh:47
Detector data structure.
Definition: JDetector.hh:77
Utility class to parse parameter values.
Definition: JProperties.hh:484
int getNumberOfPMTs(const JModule &module)
Get number of PMTs.
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:64
string outputFile
Data structure for detector geometry and calibration.
Rotation around Z-axis.
Definition: JRotation3D.hh:82
JModule & sub(const JVector3D &pos)
Subtract position.
Definition: JModule.hh:275
int getFloor() const
Get floor number.
int getString() const
Get string number.
Constants.
Detector file.
Definition: JHead.hh:126
Data structure for vector in three dimensions.
Definition: JVector3D.hh:32
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
Auxiliary methods for handling file names, type names and environment.
int getID() const
Get identifier.
Definition: JObjectID.hh:54
Data structure for PMT geometry and calibration.
Definition: JPMT.hh:52
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:64
double getY() const
Get y position.
Definition: JVector3D.hh:102
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:59
void reset(const JPMTStatusBits_t bit)
Reset PMT status.
Definition: JPMT.hh:165
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:129
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:141
void rotate(const JRotation3D &R)
Rotate module.
Definition: JModule.hh:182
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:65
Direct access to module in detector data structure.
Utility class to parse command line options.
static const char WILD_CARD
Definition: JDAQTags.hh:34
double getX() const
Get x position.
Definition: JVector3D.hh:92
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
void store(const JString &file_name, const JDetector &detector)
Store detector to output file.
JModule & set(const JVector3D &pos)
Set position.
Definition: JModule.hh:245
double getZ() const
Get z position.
Definition: JVector3D.hh:113
JModule & add(const JVector3D &pos)
Add position.
Definition: JModule.hh:257
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
int main(int argc, char *argv[])
Definition: Main.cpp:15