Jpp  18.0.1-rc.1
the software that should make you happy
 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 #include <map>
6 #include <algorithm>
7 
8 #include "TRandom3.h"
9 
10 #include "JDetector/JDetector.hh"
17 #include "JDetector/JPMTStatus.hh"
21 #include "JGeometry3D/JVector3D.hh"
22 #include "JMath/JConstants.hh"
23 #include "JMath/JMath.hh"
24 #include "JTools/JRange.hh"
25 #include "JLang/JException.hh"
26 #include "JLang/JToken.hh"
27 #include "JLang/JComparator.hh"
28 #include "JLang/JComparison.hh"
29 #include "JSupport/JMeta.hh"
30 
31 #include "Jeep/JeepToolkit.hh"
32 #include "Jeep/JPrint.hh"
33 #include "Jeep/JParser.hh"
34 #include "Jeep/JMessage.hh"
35 
36 
37 namespace {
38 
39  using namespace JPP;
40 
41  /**
42  * Wild card for string identifier, module identifier or PMT address.
43  */
44  static const int WILDCARD = -1;
45 
46  static const std::string reset_t = "reset"; //!< Reset time offset, position and PMT status bit
47  static const std::string set_t = "set"; //!< Set time offset, position or PMT status bit
48  static const std::string setx_t = "setx"; //!< Set x-position
49  static const std::string sety_t = "sety"; //!< Set y-position
50  static const std::string setz_t = "setz"; //!< Set z-position
51  static const std::string addx_t = "addx"; //!< Add x-position
52  static const std::string addy_t = "addy"; //!< Add y-position
53  static const std::string addz_t = "addz"; //!< Add z-position
54  static const std::string subx_t = "subx"; //!< Subtract x-position
55  static const std::string suby_t = "suby"; //!< Subtract y-position
56  static const std::string subz_t = "subz"; //!< Subtract z-position
57  static const std::string add_t = "add"; //!< Add time offset or position
58  static const std::string sub_t = "sub"; //!< Subtract time offset or position
59  static const std::string rot_t = "rot"; //!< Rotate around z-axis by value [rad]
60  static const std::string mul_t = "mul"; //!< Multiply z-position by (1 + value)
61  static const std::string div_t = "div"; //!< Divide z-position by (1 + value)
62  static const std::string tilt_t = "tilt"; //!< Tilt string
63  static const std::string swap_t = "swap"; //!< Swap PMTs
64 
65  static const std::string assign_t = "assign"; //!< Assign module identifier
66  static const std::string locate_t = "locate"; //!< Locate module identifier
67  static const std::string string_t = "string"; //!< Assign string number
68 
69  static const std::string rand_t = "rand"; //!< Random value(s)
70  static const std::string randset_t = rand_t + set_t; //!< Set time offset or position
71  static const std::string randadd_t = rand_t + add_t; //!< Add time offset or position
72  static const std::string randsub_t = rand_t + sub_t; //!< Subtract time offset or position
73  static const std::string randrot_t = rand_t + rot_t; //!< Rotate around z-axis by value [rad]
74  static const std::string randmul_t = rand_t + mul_t; //!< Multiply z-position by (1 + value)
75  static const std::string randdiv_t = rand_t + div_t; //!< Divide z-position by (1 + value)
76  static const std::string randtilt_t = rand_t + tilt_t; //!< Tilt string
77 
78  static const std::string RESET_t = "RESET"; //!< Reset time offset of piezo/hydrophone and quaternion calibration of compass
79  static const std::string SET_t = "SET"; //!< Set time offset of piezo/hydrophone or quaternion calibration of compass
80  static const std::string ADD_t = "ADD"; //!< Add time offset of piezo/hydrophone or quaternion calibration of compass
81  static const std::string SUB_t = "SUB"; //!< Subtract time offset of piezo/hydrophone or quaternion calibration of compass
82  static const std::string ROT_t = "ROT"; //!< Rotate quaternion calibration of compass around z-axis by value [rad]
83 
84 
85  /**
86  * Apply action to given module.
87  *
88  * \param module module (I/O)
89  * \param action action
90  * \return true if valid action; else false
91  */
92  inline bool apply(JModule& module, const std::string& action)
93  {
94  if (action == reset_t) {
95 
96  // position does not depend on module but may not exactly be at origin
97 
98  module.setCalibration(JCalibration());
99  module.setStatus(JStatus());
100  module.setQuaternion(JQuaternion3D());
101  module.set(getModule<JKM3NeT_t>(1).getPosition());
102 
103  for (JModule::iterator pmt = module.begin(); pmt != module.end(); ++pmt) {
105  pmt->setStatus(JStatus());
106  }
107 
108  } else if (action == RESET_t) {
109 
110  module.setCalibration(JCalibration());
111  module.setQuaternion(JQuaternion3D());
112  module.setCalibration(getAverage(make_array(module.begin(), module.end(), &JPMT::getT0), 0.0));
113 
114  } else {
115 
116  return false;
117  }
118 
119  return true;
120  }
121 
122 
123  /**
124  * Apply action to given module.
125  *
126  * \param module module (I/O)
127  * \param action action
128  * \param value value
129  * \return true if valid action; else false
130  */
131  inline bool apply(JModule& module, const std::string& action, const double value)
132  {
133  if (action == set_t) { // actions with fixed values
134 
135  module.set(value);
136 
137  } else if (action == setx_t) {
138 
139  module.set(JVector3D(value, module.getY(), module.getZ()));
140 
141  } else if (action == addx_t) {
142 
143  module.add(JVector3D(value, 0.0, 0.0));
144 
145  } else if (action == subx_t) {
146 
147  module.sub(JVector3D(value, 0.0, 0.0));
148 
149  } else if (action == sety_t) {
150 
151  module.set(JVector3D(module.getX(), value, module.getZ()));
152 
153  } else if (action == addy_t) {
154 
155  module.add(JVector3D(0.0, value, 0.0));
156 
157  } else if (action == suby_t) {
158 
159  module.sub(JVector3D(0.0, value, 0.0));
160 
161  } else if (action == setz_t) {
162 
163  module.set(JVector3D(module.getX(), module.getY(), value));
164 
165  } else if (action == addz_t) {
166 
167  module.add(JVector3D(0.0, 0.0, value));
168 
169  } else if (action == subz_t) {
170 
171  module.sub(JVector3D(0.0, 0.0, value));
172 
173  } else if (action == add_t) {
174 
175  module.add(value);
176 
177  } else if (action == sub_t) {
178 
179  module.sub(value);
180 
181  } else if (action == rot_t) {
182 
183  const JVector3D center = module.getPosition();
184 
185  module.sub(center);
186 
187  module.rotate(JRotation3Z(value));
188 
189  module.add(center);
190 
191  } else if (action == mul_t) {
192 
193  JVector3D center;
194 
195  if (value > 0.0)
196  center = JVector3D(module.getPosition().getX(),
197  module.getPosition().getY(),
198  module.getPosition().getZ() * (1.0 + value));
199  else
200  center = JVector3D(module.getPosition().getX(),
201  module.getPosition().getY(),
202  module.getPosition().getZ() / (1.0 - value));
203 
204  module.set(center);
205 
206  } else if (action == div_t) {
207 
208  JVector3D center;
209 
210  if (value > 0.0)
211  center = JVector3D(module.getPosition().getX(),
212  module.getPosition().getY(),
213  module.getPosition().getZ() / (1.0 + value));
214  else
215  center = JVector3D(module.getPosition().getX(),
216  module.getPosition().getY(),
217  module.getPosition().getZ() * (1.0 - value));
218 
219  module.set(center);
220 
221  } else if (action == SET_t) {
222 
223  module.getCalibration().setT0(value);
224 
225  } else if (action == ADD_t) {
226 
227  module.getCalibration().addT0(value);
228 
229  } else if (action == SUB_t) {
230 
231  module.getCalibration().subT0(value);
232 
233  } else if (action == ROT_t) {
234 
235  module.setQuaternion(JQuaternion3Z(value) * module.getQuaternion());
236 
237  } else if (action == randadd_t) { // actions with random values
238 
239  module.add(gRandom->Gaus(0.0, value));
240 
241  } else if (action == randsub_t) {
242 
243  module.sub(gRandom->Gaus(0.0, value));
244 
245  } else if (action == randrot_t){
246 
247  const JVector3D center = module.getPosition();
248 
249  module.sub(center);
250 
251  module.rotate(JRotation3Z(gRandom->Gaus(0.0, value)));
252 
253  module.add(center);
254 
255  } else if (action == randmul_t) {
256 
257  const JVector3D center(module.getPosition().getX(),
258  module.getPosition().getY(),
259  module.getPosition().getZ() * gRandom->Gaus(1.0, value));
260 
261  module.set(center);
262 
263  } else if (action == randdiv_t) {
264 
265  const JVector3D center(module.getPosition().getX(),
266  module.getPosition().getY(),
267  module.getPosition().getZ() / gRandom->Gaus(1.0, value));
268 
269  module.set(center);
270 
271  } else if (action == assign_t) { // action with assigments
272 
273  module.setID((int) value);
274 
275  } else if (action == string_t) {
276 
277  module.setLocation(JLocation((int) value, module.getFloor()));
278 
279  } else { //
280 
281  return false;
282  }
283 
284  return true;
285  }
286 
287 
288  /**
289  * Apply action to given module.
290  *
291  * \param module module (I/O)
292  * \param action action
293  * \param first first value
294  * \param second second value
295  * \return true if valid action; else false
296  */
297  inline bool apply(JModule& module, const std::string& action, const double first, const double second)
298  {
299  if (action == tilt_t) { // actions with fixed values
300 
301  const double Tx = first;
302  const double Ty = second;
303  const double Tz = sqrt(1.0 - Tx*Tx - Ty*Ty);
304 
305  const double x = Tx * module.getZ() + module.getX();
306  const double y = Ty * module.getZ() + module.getY();
307  const double z = Tz * module.getZ();
308 
309  module.set(JPosition3D(x,y,z));
310 
311  } else if (action == locate_t) {
312 
313  module.setLocation(JLocation((int) first, (int) second));
314 
315  } else if (action == swap_t) {
316 
317  std::swap(module[(int) first], module[(int) second]);
318 
319  } else { //
320 
321  return false;
322  }
323 
324  return true;
325  }
326 
327 
328  /**
329  * Apply action to given module.
330  *
331  * \param module module (I/O)
332  * \param action action
333  * \param pos pos
334  * \return true if valid action; else false
335  */
336  inline bool apply(JModule& module, const std::string& action, const JVector3D& pos)
337  {
338  const JVector3D randpos(gRandom->Gaus(0.0, pos.getX()),
339  gRandom->Gaus(0.0, pos.getY()),
340  gRandom->Gaus(0.0, pos.getZ()));
341 
342  if (action == set_t) // actions with fixed values
343  module.set(pos);
344  else if (action == add_t)
345  module.add(pos);
346  else if (action == sub_t)
347  module.sub(pos);
348  else if (action == randset_t) // actions with random values
349  module.set(randpos);
350  else if (action == randadd_t)
351  module.add(randpos);
352  else if (action == randsub_t)
353  module.sub(randpos);
354  else
355  return false;
356 
357  return true;
358  }
359 
360 
361  /**
362  * Apply action to given module.
363  *
364  * \param module module (I/O)
365  * \param action action
366  * \param Q quaternion
367  * \return true if valid action; else false
368  */
369  inline bool apply(JModule& module, const std::string& action, const JQuaternion3D& Q)
370  {
371  if (action == SET_t)
372  module.setQuaternion(Q);
373  else if (action == ADD_t)
374  module.setQuaternion(Q * module.getQuaternion());
375  else if (action == SUB_t)
376  module.setQuaternion(Q.getConjugate() * module.getQuaternion());
377  else
378  return false;
379 
380  module.getQuaternion().normalise();
381 
382  return true;
383  }
384 
385 
386  /**
387  * Apply action to given module.
388  *
389  * \param module module (I/O)
390  * \param action action
391  * \param value value
392  * \return true if valid action; else false
393  */
394  inline bool apply(JModule& module, const std::string& action, const std::string& value)
395  {
396  try {
397 
398  if (action == set_t)
399  module.getStatus().set (getModuleStatusBit(value));
400  else if (action == reset_t)
401  module.getStatus().reset(getModuleStatusBit(value));
402  else
403  return false;
404 
405  return true;
406  }
407  catch(const std::exception&) {
408  return false;
409  }
410  }
411 
412 
413  /**
414  * Apply action to given PMT.
415  *
416  * \param pmt PMT (I/O)
417  * \param action action
418  * \param value value
419  * \return true if valid action; else false
420  */
421  inline bool apply(JPMT& pmt, const std::string& action, const std::string& value)
422  {
423  try {
424 
425  if (action == set_t)
426  pmt.getStatus().set (getPMTStatusBit(value));
427  else if (action == reset_t)
428  pmt.getStatus().reset(getPMTStatusBit(value));
429  else
430  return false;
431 
432  return true;
433  }
434  catch(const std::exception&) {
435  return false;
436  }
437  }
438 
439 
440  /**
441  * Auxiliary class for module modifications.
442  */
443  struct JModifier {
444  /**
445  * Default constructor.
446  */
447  JModifier()
448  {}
449 
450 
451  /**
452  * Check validity.
453  *
454  * \return true if valid modifier; else false
455  */
456  bool is_valid() const
457  {
458  return (action != "" && !data.empty());
459  }
460 
461 
462  /**
463  * Apply action to given module depending on number of values.
464  *
465  * \param module module
466  * \return true if valid action; else false
467  */
468  bool apply(JModule& module) const
469  {
470  switch (data.size()) {
471 
472  case 0:
473  return ::apply(module, action);
474 
475  case 1:
476  return ::apply(module, action, data[0]);
477 
478  case 2:
479  return ::apply(module, action, data[0], data[1]);
480 
481  case 3:
482  return ::apply(module, action, JVector3D(data[0], data[1], data[2]));
483 
484  case 4:
485  return ::apply(module, action, JQuaternion3D(data[0], data[1], data[2], data[3]));
486 
487  default:
488  return false;
489  }
490  }
491 
492 
493  /**
494  * Read modifier from input.
495  *
496  * \param in input stream
497  * \param modifier modifier
498  * \return input stream
499  */
500  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
501  {
502  if (in >> modifier.action) {
503 
504  modifier.data.clear();
505 
506  for (double x; in >> x; ) {
507  modifier.data.push_back(x);
508  }
509 
510  in.clear(std::ios_base::eofbit);
511  }
512 
513  return in;
514  }
515 
516 
517  /**
518  * Write modifier to output.
519  *
520  * \param out output stream
521  * \param modifier modifier
522  * \return output stream
523  */
524  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
525  {
526  out << modifier.action;
527 
528  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
529  out << ' ' << *i;
530  }
531 
532  return out;
533  }
534 
535 
536  std::string action;
538  };
539 
540 
541  /**
542  * Get modifier for given string.
543  *
544  * For actions <tt>ranXXX</tt>, the corresponding action <tt>XXX</tt> is made the same for all modules in the given string.\n
545  * For all other options, the input action is maintained.
546  *
547  * \param id string number
548  * \param modifier modifier
549  * \return modifier
550  */
551  inline const JModifier& getModifier(const int id, const JModifier& modifier)
552  {
553  using namespace std;
554 
556 
557  const string::size_type pos = modifier.action.find(rand_t);
558 
559  if (pos != string::npos) {
560 
561  JModifier& result = buffer[id][modifier.action][modifier.data.size()];
562 
563  if (!result.is_valid()) {
564 
565  result.action = modifier.action.substr(pos + rand_t.length());
566 
567  for (size_t i = 0; i != modifier.data.size(); ++i) {
568  result.data.push_back(gRandom->Gaus(0.0, modifier.data[i]));
569  }
570  }
571 
572  return result;
573 
574  } else {
575 
576  return modifier;
577  }
578  }
579 
580 
581  /**
582  * Auxiliary class for module status modifications.
583  */
584  struct JModuleModifier {
585  /**
586  * Default constructor.
587  */
588  JModuleModifier()
589  {}
590 
591 
592  /**
593  * Apply action to given module.
594  *
595  * \param module module
596  * \return true if valid action; else false
597  */
598  bool apply(JModule& module) const
599  {
600  return ::apply(module, action, value);
601  }
602 
603 
604  /**
605  * Read module modifier from input.
606  *
607  * \param in input stream
608  * \param modifier modifier
609  * \return input stream
610  */
611  friend inline std::istream& operator>>(std::istream& in, JModuleModifier& modifier)
612  {
613  return in >> modifier.action >> modifier.value;
614  }
615 
616 
617  /**
618  * Write module modifier to output.
619  *
620  * \param out output stream
621  * \param modifier modifier
622  * \return output stream
623  */
624  friend inline std::ostream& operator<<(std::ostream& out, const JModuleModifier& modifier)
625  {
626  out << modifier.action;
627  out << ' ';
628  out << modifier.value;
629 
630  return out;
631  }
632 
633 
634  std::string action;
635  std::string value;
636  };
637 
638 
639  /**
640  * Auxiliary class for PMT status modifications.
641  */
642  struct JPMTModifier {
643  /**
644  * Default constructor.
645  */
646  JPMTModifier()
647  {}
648 
649 
650  /**
651  * Apply action to given PMT.
652  *
653  * \param pmt PMT
654  * \return true if valid action; else false
655  */
656  bool apply(JPMT& pmt) const
657  {
658  return ::apply(pmt, action, value);
659  }
660 
661 
662  /**
663  * Read PMT modifier from input.
664  *
665  * \param in input stream
666  * \param modifier modifier
667  * \return input stream
668  */
669  friend inline std::istream& operator>>(std::istream& in, JPMTModifier& modifier)
670  {
671  return in >> modifier.action >> modifier.value;
672  }
673 
674 
675  /**
676  * Write PMT modifier to output.
677  *
678  * \param out output stream
679  * \param modifier modifier
680  * \return output stream
681  */
682  friend inline std::ostream& operator<<(std::ostream& out, const JPMTModifier& modifier)
683  {
684  out << modifier.action;
685  out << ' ';
686  out << modifier.value;
687 
688  return out;
689  }
690 
691 
692  std::string action;
693  std::string value;
694  };
695 
696 
697  /**
698  * Range of string numbers.
699  *
700  * The input format could be a single number or two numbers separated by JRange_t::SEPARATOR.
701  */
702  struct JRange_t :
703  public JRange<int>
704  {
705  /**
706  * Separator between two identifier values.
707  */
708  static const char SEPARATOR = '-';
709 
710  /**
711  * Default constructor.
712  */
713  JRange_t() :
714  JRange(-1, -1)
715  {}
716 
717 
718  /**
719  * Read range from input.
720  *
721  * \param in input stream
722  * \param range range
723  * \return input stream
724  */
725  friend inline std::istream& operator>>(std::istream& in, JRange_t& range)
726  {
727  if (in >> range.first) {
728 
729  range.second = range.first;
730 
731  if (in.peek() == (int) JRange_t::SEPARATOR) {
732 
733  in.get();
734 
735  in >> range.second;
736 
737  } else {
738 
739  in.clear();
740  }
741  }
742 
743  return in;
744  }
745 
746 
747  /**
748  * Write range to output.
749  *
750  * \param out output stream
751  * \param range range
752  * \return output stream
753  */
754  friend inline std::ostream& operator<<(std::ostream& out, const JRange_t& range)
755  {
756  return out << range.first << JRange_t::SEPARATOR << range.second;
757  }
758  };
759 
760 
761  /**
762  * Print module modification.
763  *
764  * \param out output stream
765  * \param module module
766  * \param modifier modifier
767  */
768  inline void print(std::ostream& out, const JModule& module, const JModifier& modifier)
769  {
770  using namespace std;
771  using namespace JPP;
772 
773  out << "Modifier" << ' '
774  << getLabel(module.getLocation()) << ' '
775  << setw(10) << module.getID() << ' '
776  << "action " << modifier << endl;
777  }
778 
779 
780  /**
781  * Print module modification.
782  *
783  * \param out output stream
784  * \param id module identifier
785  * \param modifier modifier
786  */
787  inline void print(std::ostream& out, const JModuleIdentifier& id, const JModuleModifier& modifier)
788  {
789  using namespace std;
790  using namespace JPP;
791 
792  out << "module modifier" << ' '
793  << "(" << setw(10) << id.getID() << ")" << ' '
794  << "action" << ' ' << modifier.action << ' '
795  << "value" << ' ' << modifier.value << endl;
796  }
797 
798 
799  /**
800  * Print PMT modification.
801  *
802  * \param out output stream
803  * \param pmt PMT identifier
804  * \param modifier modifier
805  */
806  inline void print(std::ostream& out, const JPMTIdentifier& pmt, const JPMTModifier& modifier)
807  {
808  using namespace std;
809  using namespace JPP;
810 
811  out << "PMT modifier" << ' '
812  << "(" << setw(10) << pmt.getID() << "," << setw(2) << pmt.getPMTAddress() << ")" << ' '
813  << "action" << ' ' << modifier.action << ' '
814  << "value" << ' ' << modifier.value << endl;
815  }
816 }
817 
818 
819 /**
820  * \file
821  *
822  * Auxiliary program to modify detector calibration.
823  *
824  * Syntax:
825  * <pre>
826  * -M "<module identifier> (set|add|sub|randset|randadd|randsub) x0 [x1 x2]"
827  * -(S|s) "<string number> (set|add|sub|randset|randadd|randsub) x0 [x1 x2]"
828  * -M "<module identifier> (setx|addx|subx|sety|addy|suby|setz|addz|subz) value"
829  * -(S|s) "<string number> (setx|addx|subx|sety|addy|suby|setz|addz|subz) value"
830  * -M "<module identifier> (rot|randrot) phi"
831  * -(S|s) "<string number> (rot|randrot) phi"
832  * -M "<module identifier> (mul|randmul) factor"
833  * -(S|s) "<string number> (mul|randmul) factor"
834  * -M "<module identifier> (div|randdiv) factor"
835  * -(S|s) "<string number> (div|randdiv) factor"
836  * -M "<module identifier> (reset)"
837  * -(S|s) "<string number> (reset)"
838  * -M "<module identifier> (assign) identifier"
839  * -M "<module identifier> (locate) <string> <floor>"
840  * -M "<module identifier> (swap) <PMT> <PMT>"
841  * -M "<module identifier> (SET|ADD|SUB|) x0 [x1 x2 x3]"
842  * -(S|s) "<string number> (SET|ADD|SUB|) x0 [x1 x2 x3]"
843  * -M "<module identifier> (ROT) phi"
844  * -(S|s) "<string number> (ROT) phi"
845  * -(S|s) "<string number> (tilt|randtilt) Tx Ty"
846  * -W "<module identifier> (set|reset) (MODULE_DISABLE|COMPASS_DISABLE|HYDROPHONE_DISABLE|PIEZO_DISABLE|MODULE_OUT_OF_SYNC)"
847  * -P "<PMT identifier> (set|reset) (PMT_DISABLE|HIGH_RATE_VETO_DISABLE|FIFO_FULL_DISABLE|UDP_COUNTER_DISABLE|UDP_TRAILER_DISABLE|OUT_OF_SYNC)"
848  * -p "<PMT physical address> (set|reset) (PMT_DISABLE|HIGH_RATE_VETO_DISABLE|FIFO_FULL_DISABLE|UDP_COUNTER_DISABLE|UDP_TRAILER_DISABLE|OUT_OF_SYNC)"
849  * -k "<string number>[-<string number>]"
850  * -r "<string number>[-<string number>]"
851  * -m "<module identifier>"
852  * -D "<string number> <floor>"
853  * -\@ "<key>=<value>[;<key>=<value>"
854  * </pre>
855  * Options <tt>-M</tt> and <tt>-S</tt> refer to a module and a string, respectively.\n
856  * The values provided for a string modification coherently apply to the modules of the specified string number.\n
857  * The option <tt>-s</tt> is equivalent to option <tt>-S</tt> except that
858  * the action applies only to the optical modules in the string and not the base module.
859  *
860  * The options <tt>randxxx</tt> correspond to a randomisation of the specified option.
861  *
862  * If the module identifier or string number is -1,
863  * the action is applied to all modules or strings in the detector, respectively.
864  *
865  * For options <tt>[rand]set</tt>, <tt>[rand]add</tt> and <tt>[rand]sub</tt>,
866  * the number of values apply to position or time calibration in the following way:
867  * -# time calibration <tt>(t = x0)</tt>
868  * -# invalid
869  * -# position calibration <tt>(x = x0, y = x1, z = x2)</tt>
870  *
871  * For options <tt>(set|add|sub)(x|y|z)</tt>, the value corresponds to last character of the the quoted action.
872  *
873  * For options <tt>[rand]rot</tt>,
874  * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis.\n
875  * The rotation angle is defined in radians.
876  *
877  * For options <tt>[rand]mul</tt> and <tt>[rand]div</tt>,
878  * the multiplication/division <tt>factor</tt> (a.k.a.\ "stretching") applies to the z-coordinates of the modules.\n
879  * The factor is defined as a fraction; the actual multiplication/division factor is <tt>(1 + factor)</tt>.
880  *
881  * For options <tt>SET</tt>, <tt>ADD</tt> and <tt>SUB</tt>,
882  * the number of values apply to time or quaternion calibration of the module in the following way:
883  * -# time calibration of piezo sensor or hydrophone <tt>(t = x0)</tt>
884  * -# invalid
885  * -# invalid
886  * -# quaternion calibration of compass <tt>(qa = x0, qb = x1, qc = x2, qd = x3)</tt>
887  *
888  * For options <tt>ROT</tt>,
889  * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis of the quaternion calibration of the compass.\n
890  * The rotation angle is defined in radians.
891  *
892  * Note that to correct the time calibration for the delay time of the piezo sensor and hydrophone,
893  * application JDetectorDB.cc can be used (option <tt>-WW</tt>).
894  *
895  * The units of all positions and time values are <tt>m</tt> and <tt>ns</tt>, respectively.
896  *
897  * Note that for string modifiers with option <tt>randxxx</tt>,
898  * the action is coherently applied to the modules in the specified string.\n
899  * Only one type of action (defined by <tt>xxx</tt> and the number of values) is then allowed per string.
900  *
901  * Option <tt>-\@</tt> refers to the header information.\n
902  * The list of possible keys can be obtained using JPrintDetector.cc with option <tt>-O header</tt>.
903  *
904  * Multiple options <tt>-M</tt>, <tt>-S</tt>, <tt>-s</tt> or <tt>-\@</tt> will be processed in order of appearance.
905  *
906  * Options <tt>-k</tt> and <tt>-r</tt> can be used to keep and remove (a range of) string numbers, respectively.
907  *
908  * The options <tt>-m</tt> and <tt>-D</tt> can be used to maintain a specific module (and remove all others) and
909  * to delete a floor from a string, respectively.
910  *
911  * Note finally that if the output file name is the same as the input file name,
912  * the original file will be overwritten.
913  *
914  * \author mdejong
915  */
916 int main(int argc, char **argv)
917 {
918  using namespace std;
919  using namespace JPP;
920 
921  typedef JToken<';'> JToken_t;
922 
923  string inputFile;
924  string outputFile;
925  vector<JToken_t> hdr;
929  vector< pair<int,
930  JModuleModifier> > wip;
932  JPMTModifier> > pmt;
934  JPMTModifier> > alt;
935  vector<JRange_t> keep;
937  vector<int> id;
938  multimap<int, int> del;
939  int option;
940  bool squash;
941  int debug;
942 
943  try {
944 
945  JParser<> zap("Auxiliary program to modify detector.");
946 
947  zap['a'] = make_field(inputFile);
948  zap['o'] = make_field(outputFile);
949  zap['@'] = make_field(hdr, "header modification") = JPARSER::initialised();
950  zap['M'] = make_field(mod, "module modification") = JPARSER::initialised();
951  zap['S'] = make_field(str, "string modification (optical modules and base modules)") = JPARSER::initialised();
952  zap['s'] = make_field(dos, "string modification (optical modules only)") = JPARSER::initialised();
953  zap['W'] = make_field(wip, "module status modification") = JPARSER::initialised();
954  zap['P'] = make_field(pmt, "PMT status modification by PMT logical address") = JPARSER::initialised();
955  zap['p'] = make_field(alt, "PMT status modification by PMT physical address") = JPARSER::initialised();
956  zap['k'] = make_field(keep, "keep string[s]") = JPARSER::initialised();
957  zap['r'] = make_field(rm, "remove string[s]") = JPARSER::initialised();
958  zap['m'] = make_field(id, "remove module by identifier") = JPARSER::initialised();
959  zap['D'] = make_field(del, "remove module by location") = JPARSER::initialised();
960  zap['O'] = make_field(option, "sort modules: "\
961  "0 -> no sort; 1 -> module identifier; 2 -> module location") = 0, 1, 2;
962  zap['q'] = make_field(squash, "squash meta data");
963  zap['d'] = make_field(debug, "debug level") = 2;
964 
965  zap(argc, argv);
966  }
967  catch(const exception &error) {
968  FATAL(error.what() << endl);
969  }
970 
971  gRandom->SetSeed(0);
972 
973  const int ns = ((keep.empty() ? 0 : 1) +
974  (rm .empty() ? 0 : 1) +
975  (id .empty() ? 0 : 1) +
976  (del .empty() ? 0 : 1));
977 
978  if (ns > 1) {
979  FATAL("Use either option -k, -r, -m or -D." << endl);
980  }
981 
983 
984  try {
985  load(inputFile, detector);
986  }
987  catch(const JException& error) {
988  FATAL(error);
989  }
990 
991  if (squash) {
992  detector.comment.clear();
993  }
994 
995  detector.comment.add(JMeta(argc,argv));
996 
997  if (detector.setToLatestVersion()) {
998  NOTICE("Set detector version to " << detector.getVersion() << endl);
999  }
1000 
1001 
1002  if (!hdr.empty()) {
1003 
1004  int id = -1;
1005 
1006  JProperties helper = detector.getProperties();
1007 
1008  helper["id"] = id;
1009 
1010  for (vector<JToken_t>::const_iterator i = hdr.begin(); i != hdr.end(); ++i) {
1011 
1012  istringstream is(*i);
1013 
1014  is >> helper;
1015  }
1016 
1017  if (id != -1) {
1018  detector.setID(id);
1019  }
1020  }
1021 
1022 
1023  if (ns != 0) {
1024 
1025  for (JDetector::iterator module = detector.begin(); module != detector.end(); ) {
1026 
1027  bool __rm__ = !keep.empty() && rm.empty();
1028 
1029  for (vector<JRange_t>::const_iterator i = keep.begin(); i != keep.end(); ++i) {
1030  if (module->getString() >= i->first && module->getString() <= i->second) {
1031  __rm__ = false;
1032  }
1033  }
1034 
1035  for (vector<JRange_t>::const_iterator i = rm.begin(); i != rm.end(); ++i) {
1036  if (module->getString() >= i->first && module->getString() <= i->second) {
1037  __rm__ = true;
1038  }
1039  }
1040 
1041  if (!id.empty()) {
1042  __rm__ = find(id.begin(), id.end(), module->getID()) == id.end();
1043  }
1044 
1045  const auto range = del.equal_range(module->getString());
1046 
1047  for (auto i = range.first; i != range.second; ++i) {
1048  if (i->second == module->getFloor()) {
1049  __rm__ = true;
1050  }
1051  }
1052 
1053  if (__rm__)
1054  module = detector.erase(module);
1055  else
1056  ++module;
1057  }
1058  }
1059 
1060 
1061  for (vector< pair<int, JModifier> >::const_iterator i = mod.begin(); i != mod.end(); ++i) {
1062 
1063  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
1064 
1065  if (module->getID() == i->first || i->first == WILDCARD ){
1066 
1067  if (debug >= debug_t) {
1068  print(cout, *module, i->second);
1069  }
1070 
1071  if (!i->second.apply(*module)) {
1072  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1073  }
1074  }
1075  }
1076  }
1077 
1078 
1079  for (vector< pair<int, JModifier> >::const_iterator i = str.begin(); i != str.end(); ++i) {
1080 
1081  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
1082 
1083  if (module->getString() == i->first || i->first == WILDCARD) {
1084 
1085  const JModifier modifier = getModifier(module->getString(), i->second);
1086 
1087  if (debug >= debug_t) {
1088  print(cout, *module, i->second);
1089  }
1090 
1091  if (!modifier.apply(*module)) {
1092  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1093  }
1094  }
1095  }
1096  }
1097 
1098 
1099  for (vector< pair<int, JModifier> >::const_iterator i = dos.begin(); i != dos.end(); ++i) {
1100 
1101  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
1102 
1103  if (module->getFloor() != 0) {
1104 
1105  if (module->getString() == i->first || i->first == WILDCARD) {
1106 
1107  const JModifier modifier = getModifier(module->getString(), i->second);
1108 
1109  if (debug >= debug_t) {
1110  print(cout, *module, i->second);
1111  }
1112 
1113  if (!modifier.apply(*module)) {
1114  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1115  }
1116  }
1117  }
1118  }
1119  }
1120 
1121 
1122  for (vector< pair<int, JModuleModifier> >::const_iterator i = wip.begin(); i != wip.end(); ++i) {
1123 
1124  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
1125 
1126  if (module->getID() == i->first || i->first == WILDCARD ){
1127 
1128  if (debug >= debug_t) {
1129  print(cout, *module, i->second);
1130  }
1131 
1132  if (!i->second.apply(*module)) {
1133  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1134  }
1135  }
1136  }
1137  }
1138 
1139 
1140  for (vector< pair<JPMTIdentifier, JPMTModifier> >::const_iterator i = pmt.begin(); i != pmt.end(); ++i) {
1141 
1142  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
1143 
1144  if (module->getID() == i->first.getModuleID() || i->first.getModuleID() == WILDCARD) {
1145 
1146  if (debug >= debug_t) {
1147  print(cout, i->first, i->second);
1148  }
1149 
1150  if (i->first.getPMTAddress() == WILDCARD) {
1151 
1152  for (int pmt = 0; pmt != getNumberOfPMTs(*module); ++pmt) {
1153  if (!i->second.apply(module->getPMT(pmt))) {
1154  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1155  }
1156  }
1157 
1158  } else if (i->first.getPMTAddress() >= 0 &&
1159  i->first.getPMTAddress() < getNumberOfPMTs(*module) &&
1160  !i->second.apply(module->getPMT(i->first.getPMTAddress()))) {
1161  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1162  }
1163  }
1164  }
1165  }
1166 
1167 
1168  if (!alt.empty()) {
1169 
1170  if (!hasDetectorAddressMap(detector.getID())) {
1171  FATAL("Invalid detector identifier " << detector.getID() << endl);
1172  }
1173 
1174  const JDetectorAddressMap& demo = getDetectorAddressMap(detector.getID());
1175 
1176  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
1177 
1178  const JModuleAddressMap memo = demo.get(module->getID());
1179 
1180  for (vector< pair<JPMTPhysicalAddress, JPMTModifier> >::const_iterator i = alt.begin(); i != alt.end(); ++i) {
1181 
1182  const JPMTIdentifier id(module->getID(), memo.getAddressTranslator(i->first).tdc);
1183 
1184  if (debug >= debug_t) {
1185  print(cout, id, i->second);
1186  }
1187 
1188  if (!i->second.apply(module->getPMT(id.getPMTAddress()))) {
1189  ERROR("No valid action: " << i->first << ' ' << i->second << endl);
1190  }
1191  }
1192  }
1193  }
1194 
1195 
1196  switch (option) {
1197  case 1:
1198  sort(detector.begin(), detector.end(), make_comparator(&JModule::getID));
1199  break;
1200 
1201  case 2:
1202  sort(detector.begin(), detector.end(), make_comparator(&JModule::getLocation));
1203  break;
1204 
1205  default:
1206  break;
1207  };
1208 
1209 
1210  try {
1212  }
1213  catch(const JException& error) {
1214  FATAL(error);
1215  }
1216 }
friend std::ostream & operator<<(std::ostream &out, const JPair< JKey_t, JValue_t > &pair)
Write pair to output.
Definition: JPair.hh:81
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1514
General exception.
Definition: JException.hh:23
static const JGetPMTStatusBit getPMTStatusBit
Function object to map key to PMT status bit.
Definition: JPMTStatus.hh:65
Exceptions.
Q(UTCMax_s-UTCMin_s)-livetime_s
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Data structure for a composite optical module.
Definition: JModule.hh:68
JQuaternion3D getConjugate() const
Get conjugate of this quaternion.
void setLocation(const JLocation &location)
Set location.
Definition: JLocation.hh:91
const JCalibration & getCalibration() const
Get calibration.
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
Definition: JLocation.hh:246
bool is_valid(const json &js)
Check validity of JSon data.
Detector data structure.
Definition: JDetector.hh:89
std::vector< size_t > ns
bool hasDetectorAddressMap(const int id)
Check if detector address map is available.
Utility class to parse parameter values.
Definition: JProperties.hh:496
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition: JMath.hh:494
int getNumberOfPMTs(const JModule &module)
Get number of PMTs.
void subT0(const double t0)
Subtract time offset.
Lookup table for PMT addresses in detector.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Data structure for time calibration.
static const JGetModuleStatusBit getModuleStatusBit
Function object to map key to module status bit.
JRange_t()
Default constructor.
Definition: JHead.hh:47
string outputFile
is
Definition: JDAQCHSM.chsm:167
Data structure for detector geometry and calibration.
const JModuleAddressMap & get(const int id) const
Get module address map.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
friend std::istream & operator>>(std::istream &in, JPair< JKey_t, JValue_t > &pair)
Read pair from input.
Definition: JPair.hh:65
const JQuaternion3D & getQuaternion() const
Get quaternion.
JValue_t second
Definition: JPair.hh:129
Rotation around Z-axis.
Definition: JRotation3D.hh:85
JModule & sub(const JVector3D &pos)
Subtract position.
Definition: JModule.hh:438
Lookup table for PMT addresses in optical module.
JKey_t first
Definition: JPair.hh:128
int getStatus() const
Get status.
Definition: JStatus.hh:63
Type definition of range.
Definition: JHead.hh:41
Detector specific mapping between logical positions and readout channels of PMTs in optical modules...
then rm
Definition: sftpput.zsh:30
I/O formatting auxiliaries.
Detector file.
Definition: JHead.hh:226
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
void setQuaternion(const JQuaternion3D &quaternion)
Set quaternion.
Logical location of module.
Definition: JLocation.hh:37
const JLocation & getLocation() const
Get location.
Definition: JLocation.hh:69
Mathematical constants.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
Auxiliary methods for handling file names, type names and environment.
int getID() const
Get identifier.
Definition: JObjectID.hh:50
Auxiliary class for handling status.
Definition: JStatus.hh:37
This class represents a rotation around the z-axis.
Data structure for PMT geometry, calibration and status.
Definition: JPMT.hh:43
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
ROOT I/O of application specific meta data.
JPosition3D getPosition(const Vec &pos)
Get position.
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
int getPMTAddress() const
Get PMT address (= TDC).
double getY() const
Get y position.
Definition: JVector3D.hh:104
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
print
Definition: JConvertDusj.sh:44
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:173
void rotate(const JRotation3D &R)
Rotate module.
Definition: JModule.hh:315
Range of values.
Definition: JRange.hh:38
General purpose messaging.
void setStatus(const JStatus &status)
Set status.
Definition: JStatus.hh:97
#define FATAL(A)
Definition: JMessage.hh:67
Data structure for unit quaternion in three dimensions.
Direct access to module in detector data structure.
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
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
const JPMTAddressTranslator & getAddressTranslator(const int tdc) const
Get PMT address translator.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
void setT0(const double t0)
Set time offset.
Auxiliary class to define a range between two values.
Utility class to parse command line options.
Auxiliary class for object identification.
Definition: JObjectID.hh:22
Wrapper class around string.
Definition: JToken.hh:23
void setCalibration(const JCalibration &cal)
Set calibration.
void setID(const int id)
Set identifier.
Definition: JObjectID.hh:72
double getX() const
Get x position.
Definition: JVector3D.hh:94
Base class for data structures with artithmetic capabilities.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
void addT0(const double t0)
Add time offset.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
Data structure for PMT physical address.
JQuaternion3D & normalise()
Normalise quaternion.
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
double getT0() const
Get time offset.
static const std::string string_t
string
Definition: JSydney.cc:81