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