Jpp  15.0.5
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JModule.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JMODULE__
2 #define __JDETECTOR__JMODULE__
3 
4 #include <istream>
5 #include <ostream>
6 #include <vector>
7 
9 #include "JDetector/JLocation.hh"
10 #include "JDetector/JPMT.hh"
13 #include "Jeep/JStatus.hh"
14 
19 
20 #include "JLang/JException.hh"
21 #include "JMath/JMatrix3S.hh"
22 #include "JMath/JMath.hh"
23 
24 #include "JIO/JSerialisable.hh"
25 
26 
27 /**
28  * \file
29  *
30  * Data structure for optical module.
31  * \author mdejong
32  */
33 namespace JDETECTOR {}
34 namespace JPP { using namespace JDETECTOR; }
35 
36 namespace JDETECTOR {
37 
45  using JEEP::JStatus;
47  using JIO::JReader;
48  using JIO::JWriter;
49 
50 
51  /**
52  * Data structure for a composite optical module.
53  *
54  * A module consists of a set of PMTs. A JPMT object is used for each PMT.\n
55  * The index of the PMT in the module corresponds to the readout channel (TDC).\n
56  * The quaternion data and time offset correspond to the calibration of the compass and piezo sensor inside the module, respectively.\n
57  * There are no PMTs and piezo sensor in the base module. The time offset then corresponds to the hydrophone.
58  * Note that the positions of the PMTs are absolute in space (i.e.\ not relative to the position of the module).
59  *
60  * The I/O of the position, quaternion data and time offset of the module depends on the detector version.\n
61  * The member method JModule::compile is used to set the position, quaternion data and time offset
62  * for detector versions for which these are not defined.\n
63  * In this, the position of the module is determined from the intersection point of the PMT axes.
64  *
65  * Note finally that the positions of the reference modules may not exactly be at the origin
66  * due to the finite accuracy of the PMT axes.\n
67  */
68  class JModule :
69  public JModuleIdentifier,
70  public JLocation,
71  public JPosition3D,
72  public JQuaternion3D,
73  public JCalibration,
74  public JStatus,
75  public std::vector<JPMT>
76  {
77  public:
78 
79  using JStatus::has;
80  using JStatus::set;
81  using JStatus::reset;
82 
83  /**
84  * Default constructor.
85  */
86  JModule() :
88  JLocation(),
89  JPosition3D(),
90  JQuaternion3D(),
91  JCalibration(),
92  JStatus(),
93  std::vector<JPMT>()
94  {}
95 
96 
97  /**
98  * Constructor.
99  *
100  * \param id identifier
101  * \param location location
102  */
103  JModule(const int id,
104  const JLocation& location) :
105  JModuleIdentifier(id),
106  JLocation(location),
107  JPosition3D(),
108  JQuaternion3D(),
109  JCalibration(),
110  JStatus(),
111  std::vector<JPMT>()
112  {}
113 
114 
115  /**
116  * Get detector version.
117  */
119  {
120  static JDetectorVersion version;
121 
122  return version;
123  }
124 
125 
126  /**
127  * Set detector version.
128  *
129  * \param version version
130  */
131  static void setVersion(const JVersion& version)
132  {
133  getVersion() = JDetectorVersion(version);
134  }
135 
136 
137  /**
138  * Compare modules.
139  *
140  * The comparison only covers the orientations of the modules.
141  *
142  * \param first first module
143  * \param second second module
144  * \param precision precision
145  * \return true if two modules are equal; else false
146  */
147  static inline bool compare(const JModule& first,
148  const JModule& second,
149  const double precision = 1.0e-3)
150  {
151  if (first.size() == second.size()) {
152 
153  for (size_t i = 0; i != first.size(); ++i) {
154  if (first[i].getDirection().getDot(second[i].getDirection()) < 1.0 - precision) {
155  return false;
156  }
157  }
158 
159  return true;
160  }
161 
162  return false;
163  }
164 
165 
166 
167  /**
168  * Get PMT.
169  *
170  * \param index readout channel (TDC)
171  * \return PMT at given index
172  */
173  const JPMT& getPMT(const int index) const
174  {
175  return at(index);
176  }
177 
178 
179  /**
180  * Get PMT.
181  *
182  * \param index readout channel (TDC)
183  * \return PMT at given index
184  */
185  JPMT& getPMT(const int index)
186  {
187  return at(index);
188  }
189 
190 
191  /**
192  * Set PMT.
193  *
194  * \param index readout channel (TDC)
195  * \param pmt PMT
196  */
197  void setPMT(const int index, const JPMT& pmt)
198  {
199  if (index >= (int) size()) {
200  resize(index + 1);
201  }
202 
203  (*this)[index] = pmt;
204  }
205 
206 
207  /**
208  * Get center of module based on crossing point of PMT axes.
209  *
210  * This method perform a fit of the crossing point of the PMT axes.\n
211  * A general purpose implementation of such a fit is available in JFIT::JEstimator<JPoint3D>.
212  *
213  * \return center
214  */
216  {
217  using namespace JPP;
218 
219  if (this->size() > 1u) {
220 
221  double x = 0;
222  double y = 0;
223  double z = 0;
224 
225  JMatrix3S V;
226 
227  for (const_iterator pmt = this->begin(); pmt != this->end(); ++pmt) {
228 
229  const double xx = 1.0 - pmt->getDX() * pmt->getDX();
230  const double yy = 1.0 - pmt->getDY() * pmt->getDY();
231  const double zz = 1.0 - pmt->getDZ() * pmt->getDZ();
232 
233  const double xy = -pmt->getDX() * pmt->getDY();
234  const double xz = -pmt->getDX() * pmt->getDZ();
235  const double yz = -pmt->getDY() * pmt->getDZ();
236 
237  V.a00 += xx;
238  V.a01 += xy;
239  V.a02 += xz;
240 
241  V.a11 += yy;
242  V.a12 += yz;
243 
244  V.a22 += zz;
245 
246  x += xx * pmt->getX() + xy * pmt->getY() + xz * pmt->getZ();
247  y += xy * pmt->getX() + yy * pmt->getY() + yz * pmt->getZ();
248  z += xz * pmt->getX() + yz * pmt->getY() + zz * pmt->getZ();
249  }
250 
251  V.a10 = V.a01;
252  V.a20 = V.a02;
253  V.a21 = V.a12;
254 
255  V.invert();
256 
257  return JVector3D(V.a00 * x + V.a01 * y + V.a02 * z,
258  V.a10 * x + V.a11 * y + V.a12 * z,
259  V.a20 * x + V.a21 * y + V.a22 * z);
260 
261  } else {
262  throw JValueOutOfRange("JModule::getCenter(): Not enough PMTs.");
263  }
264  }
265 
266 
267  /**
268  * Compile module data.
269  *
270  * For detector versions before JDetectorVersion::V4,
271  * the position,
272  * quaternion data and
273  * time offset of the module should be set.\n
274  * The position is set to the intersection point of the PMT axes (or their average position if this is not possible).\n
275  * The quaternion data are maintained.\n
276  * For an optical module (i.e.\ floor > 0),
277  * the time offset is set to the average time offset of the PMTs, and then corrected for known delay of the piezo sensor (PIEZO_DELAYTIME_US).\n
278  * For a base module (i.e.\ floor = 0),
279  * the time offset is set to zero, and then corrected for the known delay of the hydrophone (HYDROPHONE_DELAYTIME_US).
280  */
281  void compile()
282  {
283  using namespace std;
284  using namespace JPP;
285 
286  if (!this->empty()) {
287 
288  JPosition3D& pos = this->getPosition();
289 
290  try {
291  pos = this->getCenter();
292  }
293  catch(const exception&) {
294 
295  pos = JPosition3D(0.0, 0.0, 0.0);
296 
297  for (iterator i = this->begin(); i != this->end(); ++i) {
298  pos.add(i->getPosition());
299  }
300 
301  pos.div(size());
302  }
303  }
304 
305  const double t0 = getAverage(make_array(this->begin(), this->end(), &JModule::getT0), 0.0);
306 
307  if (this->getFloor() == 0)
308  this->setCalibration(t0 - HYDROPHONE_DELAYTIME_US * 1.0e+3);
309  else
310  this->setCalibration(t0 - PIEZO_DELAYTIME_US * 1.0e+3);
311  }
312 
313 
314  /**
315  * Rotate module.
316  *
317  * \param R rotation matrix
318  */
319  void rotate(const JRotation3D& R)
320  {
322 
323  for (iterator i = this->begin(); i != this->end(); ++i) {
324  i->rotate(R);
325  }
326  }
327 
328 
329  /**
330  * Rotate back module.
331  *
332  * \param R rotation matrix
333  */
334  void rotate_back(const JRotation3D& R)
335  {
337 
338  for (iterator i = this->begin(); i != this->end(); ++i) {
339  i->rotate_back(R);
340  }
341  }
342 
343 
344  /**
345  * Transformation of geometry (see method JGEOMETRY3D::JPosition3D::transform(const JRotation3D&, const JVector3D&)).
346  *
347  * \param R rotation matrix
348  * \param pos position of origin (after rotation)
349  */
350  void transform(const JRotation3D& R,
351  const JVector3D& pos)
352  {
353  JPosition3D::transform(R, pos);
354 
355  for (iterator i = this->begin(); i != this->end(); ++i) {
356  i->transform(R, pos);
357  }
358  }
359 
360 
361  /**
362  * Transformation of geometry.
363  *
364  * \param T transformation
365  */
367  {
369 
370  for (iterator i = this->begin(); i != this->end(); ++i) {
371  i->transform(T);
372  }
373  }
374 
375 
376  /**
377  * Rotate module.
378  *
379  * \param Q quaternion
380  */
381  void rotate(const JQuaternion3D& Q)
382  {
384 
385  for (iterator i = this->begin(); i != this->end(); ++i) {
386  i->rotate(Q);
387  }
388  }
389 
390 
391  /**
392  * Rotate back module.
393  *
394  * \param Q quaternion
395  */
397  {
399 
400  for (iterator i = this->begin(); i != this->end(); ++i) {
401  i->rotate_back(Q);
402  }
403  }
404 
405 
406  /**
407  * Set position.
408  *
409  * \param pos position
410  * \return this module
411  */
412  JModule& set(const JVector3D& pos)
413  {
414  return add(pos - this->getPosition());
415  }
416 
417 
418  /**
419  * Add position.
420  *
421  * \param pos position
422  * \return this module
423  */
424  JModule& add(const JVector3D& pos)
425  {
426  for (iterator i = begin(); i != end(); ++i) {
427  i->add(pos);
428  }
429 
430  JPosition3D::add(pos);
431 
432  return *this;
433  }
434 
435 
436  /**
437  * Subtract position.
438  *
439  * \param pos position
440  * \return this module
441  */
442  JModule& sub(const JVector3D& pos)
443  {
444  for (iterator i = begin(); i != end(); ++i) {
445  i->sub(pos);
446  }
447 
448  JPosition3D::sub(pos);
449 
450  return *this;
451  }
452 
453 
454  /**
455  * Set time offset.
456  *
457  * \param t0 time offset [ns]
458  * \return this module
459  */
460  JModule& set(const double t0)
461  {
462  for (iterator i = begin(); i != end(); ++i) {
463  i->setT0(t0);
464  }
465 
466  return *this;
467  }
468 
469 
470  /**
471  * Add time offset.
472  *
473  * \param t0 time offset [ns]
474  * \return this module
475  */
476  JModule& add(const double t0)
477  {
478  for (iterator i = begin(); i != end(); ++i) {
479  i->addT0(t0);
480  }
481 
482  return *this;
483  }
484 
485 
486  /**
487  * Subtract time offset.
488  *
489  * \param t0 time offset [ns]
490  * \return this module
491  */
492  JModule& sub(const double t0)
493  {
494  for (iterator i = begin(); i != end(); ++i) {
495  i->subT0(t0);
496  }
497 
498  return *this;
499  }
500 
501 
502  /**
503  * Add position.
504  *
505  * \param pos position
506  * \return this module
507  */
509  {
510  return this->add(pos);
511  }
512 
513 
514  /**
515  * Subtract position.
516  *
517  * \param pos position
518  * \return this module
519  */
521  {
522  return this->sub(pos);
523  }
524 
525 
526  /**
527  * Read module from input.
528  *
529  * \param in input stream
530  * \param module module
531  * \return input stream
532  */
533  friend inline std::istream& operator>>(std::istream& in, JModule& module)
534  {
535  module = JModule();
536 
537  in >> static_cast<JModuleIdentifier&>(module);
538  in >> static_cast<JLocation&> (module);
539 
541  in >> static_cast<JPosition3D&> (module);
542  in >> static_cast<JQuaternion3D&>(module);
543  in >> static_cast<JCalibration&> (module);
544  }
545 
547  in >> static_cast<JStatus&> (module);
548  }
549 
550  unsigned int n;
551 
552  in >> n;
553 
554  for (JPMT pmt; n != 0 && in >> pmt; --n) {
555  module.push_back(pmt);
556  }
557 
559  module.compile();
560  }
561 
562  return in;
563  }
564 
565 
566  /**
567  * Write module to output.
568  *
569  * \param out output stream
570  * \param module module
571  * \return output stream
572  */
573  friend inline std::ostream& operator<<(std::ostream& out, const JModule& module)
574  {
575  using namespace std;
576 
577  out << setw(10);
578  out << static_cast<const JModuleIdentifier&>(module);
579  out << ' ';
580  out << static_cast<const JLocation&> (module);
581 
583  out << ' ';
584  out << static_cast<const JPosition3D&> (module);
585  out << ' ';
586  out << static_cast<const JQuaternion3D&>(module);
587  out << ' ';
588  out << static_cast<const JCalibration&> (module);
589  }
590 
592  out << ' ';
593  out << static_cast<const JStatus&> (module);
594  }
595 
596  out << ' ' << module.size() << endl;
597 
598  for (const_iterator i = module.begin(); i != module.end(); ++i) {
599  out << ' ' << *i << endl;;
600  }
601 
602  return out;
603  }
604 
605 
606  /**
607  * Read module from input.
608  *
609  * \param in reader
610  * \param module module
611  * \return rrreader
612  */
613  friend inline JReader& operator>>(JReader& in, JModule& module)
614  {
615  module = JModule();
616 
617  in >> static_cast<JModuleIdentifier&>(module);
618  in >> static_cast<JLocation&> (module);
619 
621  in >> static_cast<JPosition3D&> (module);
622  in >> static_cast<JQuaternion3D&>(module);
623  in >> static_cast<JCalibration&> (module);
624  }
625 
627  in >> static_cast<JStatus&> (module);
628  }
629 
630  int n;
631 
632  in >> n;
633 
634  for (JPMT pmt; n != 0; --n) {
635 
636  in >> pmt;
637 
638  module.push_back(pmt);
639  }
640 
642  module.compile();
643  }
644 
645  return in;
646  }
647 
648 
649  /**
650  * Write module to output.
651  *
652  * \param out writer
653  * \param module module
654  * \return writer
655  */
656  friend inline JWriter& operator<<(JWriter& out, const JModule& module)
657  {
658  out << static_cast<const JModuleIdentifier&>(module);
659  out << static_cast<const JLocation&> (module);
660 
662  out << static_cast<const JPosition3D&> (module);
663  out << static_cast<const JQuaternion3D&>(module);
664  out << static_cast<const JCalibration&> (module);
665  }
666 
668  out << static_cast<const JStatus&> (module);
669  }
670 
671  int n = module.size();
672 
673  out << n;
674 
675  for (const_iterator i = module.begin(); i != module.end(); ++i) {
676  out << *i;
677  }
678 
679  return out;
680  }
681  };
682 }
683 
684 #endif
friend JReader & operator>>(JReader &in, JModule &module)
Read module from input.
Definition: JModule.hh:613
Module support kit.
Exceptions.
Interface for binary output.
Q(UTCMax_s-UTCMin_s)-livetime_s
static void setVersion(const JVersion &version)
Set detector version.
Definition: JModule.hh:131
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Data structure for a composite optical module.
Definition: JModule.hh:68
static const double HYDROPHONE_DELAYTIME_US
Hydrophone delay time [us].
JPosition3D & rotate_back(const JRotation3D &R)
Rotate back.
Definition: JPosition3D.hh:200
JModule & operator-=(const JVector3D &pos)
Subtract position.
Definition: JModule.hh:520
JModule & add(const double t0)
Add time offset.
Definition: JModule.hh:476
Rotation matrix.
Definition: JRotation3D.hh:111
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition: JMath.hh:497
void subT0(const double t0)
Subtract time offset.
void reset(const int bit)
Reset PMT status.
Definition: JStatus.hh:130
Data structure for time calibration.
double getDot(const JAngle3D &angle) const
Get dot product.
void transform(const JRotation3D &R, const JVector3D &pos)
Transform position.
Definition: JPosition3D.hh:331
friend JWriter & operator<<(JWriter &out, const JModule &module)
Write module to output.
Definition: JModule.hh:656
void rotate(const JQuaternion3D &Q)
Rotate module.
Definition: JModule.hh:381
JModule()
Default constructor.
Definition: JModule.hh:86
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Axis object.
Definition: JAxis3D.hh:38
JModule & sub(const JVector3D &pos)
Subtract position.
Definition: JModule.hh:442
const int n
Definition: JPolint.hh:660
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
void transform(const JTransformation3D &T)
Transformation of geometry.
Definition: JModule.hh:366
JDirection3D getDirection(const Vec &dir)
Get direction.
static const double PIEZO_DELAYTIME_US
Piezo delay time [us].
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
Version with quaternion and time offset per module.
JModule & sub(const double t0)
Subtract time offset.
Definition: JModule.hh:492
Logical location of module.
Definition: JLocation.hh:37
bool has(const int bit) const
Test PMT status.
Definition: JStatus.hh:108
void set(const int bit)
Set PMT status.
Definition: JStatus.hh:119
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for handling status.
Definition: JStatus.hh:37
Data structure for PMT geometry, calibration and status.
Definition: JPMT.hh:43
void compile()
Compile module data.
Definition: JModule.hh:281
Data structure for detector version.
void rotate_back(const JQuaternion3D &Q)
Rotate back module.
Definition: JModule.hh:396
JModule(const int id, const JLocation &location)
Constructor.
Definition: JModule.hh:103
void setPMT(const int index, const JPMT &pmt)
Set PMT.
Definition: JModule.hh:197
void rotate_back(const JRotation3D &R)
Rotate back module.
Definition: JModule.hh:334
JVector3D getCenter() const
Get center of module based on crossing point of PMT axes.
Definition: JModule.hh:215
Interface for binary input.
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
const JPMT & getPMT(const int index) const
Get PMT.
Definition: JModule.hh:173
void rotate(const JRotation3D &R)
Rotate module.
Definition: JModule.hh:319
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:43
JModule & operator+=(const JVector3D &pos)
Add position.
Definition: JModule.hh:508
Logical location of module.
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
const JRotation3D & getRotation() const
Get rotation.
Definition: JRotation3D.hh:272
Data structure for unit quaternion in three dimensions.
static JDetectorVersion & getVersion()
Get detector version.
Definition: JModule.hh:118
friend std::ostream & operator<<(std::ostream &out, const JModule &module)
Write module to output.
Definition: JModule.hh:573
friend std::istream & operator>>(std::istream &in, JModule &module)
Read module from input.
Definition: JModule.hh:533
void setT0(const double t0)
Set time offset.
Data structure for PMT geometry and calibration.
Auxiliary class for object identification.
Definition: JObjectID.hh:22
JPosition3D()
Default constructor.
Definition: JPosition3D.hh:48
void setCalibration(const JCalibration &cal)
Set calibration.
void transform(const JRotation3D &R, const JVector3D &pos)
Transformation of geometry (see method JGEOMETRY3D::JPosition3D::transform(const JRotation3D&amp;, const JVector3D&amp;)).
Definition: JModule.hh:350
JModule & set(const double t0)
Set time offset.
Definition: JModule.hh:460
JPMT & getPMT(const int index)
Get PMT.
Definition: JModule.hh:185
Base class for data structures with artithmetic capabilities.
void addT0(const double t0)
Add time offset.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
JVector3D & div(const double factor)
Scale vector.
Definition: JVector3D.hh:190
Data structure for normalised vector in three dimensions.
Definition: JVersor3D.hh:26
Version with module status field.
double u[N+1]
Definition: JPolint.hh:739
Auxiliary class for version identifier.
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:186
JModule & set(const JVector3D &pos)
Set position.
Definition: JModule.hh:412
static bool compare(const JModule &first, const JModule &second, const double precision=1.0e-3)
Compare modules.
Definition: JModule.hh:147
version
Definition: JCalibratePMT.sh:7
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:142
JModule & add(const JVector3D &pos)
Add position.
Definition: JModule.hh:424
JVector3D()
Default constructor.
Definition: JVector3D.hh:41
double getT0() const
Get time offset.