Jpp  16.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDynamics.hh
Go to the documentation of this file.
1 #ifndef __JDYNAMICS__JDYNAMICS__
2 #define __JDYNAMICS__JDYNAMICS__
3 
4 #include <ostream>
5 #include <set>
6 
7 #include "JDetector/JDetector.hh"
10 #include "JUTC/JUTCTimeRange.hh"
11 
12 #include "JCompass/JEvt.hh"
13 #include "JCompass/JEvtToolkit.hh"
14 #include "JCompass/JSupport.hh"
15 
16 #include "JAcoustics/JModel.hh"
17 #include "JAcoustics/JGeometry.hh"
18 #include "JAcoustics/JEvt.hh"
19 #include "JAcoustics/JSupport.hh"
20 
21 #include "JLang/JObjectIterator.hh"
22 #include "JLang/JException.hh"
23 
25 #include "JGeometry3D/JEigen3D.hh"
26 
27 #include "JTools/JElement.hh"
28 #include "JTools/JCollection.hh"
29 #include "JTools/JPolfit.hh"
30 #include "JTools/JHashMap.hh"
31 #include "JTools/JRange.hh"
32 
35 
36 
37 /**
38  * \file
39  *
40  * Dynamic detector calibration.
41  * \author mdejong
42  */
43 
44 namespace JDYNAMICS {}
45 namespace JPP { using namespace JDYNAMICS; }
46 
47 /**
48  * Main namespace for dynamic position and orientation calibration.
49  */
50 namespace JDYNAMICS {
51 
56  using JUTC::JUTCTimeRange;
57 
58 
59  /**
60  * Dynamic detector calibration.
61  */
62  struct JDynamics :
63  public JDetector
64  {
65  /**
66  * Auxiliary data structure to track applicability period of calibration data.
67  */
68  struct JUTCTracker :
69  public JUTCTimeRange
70  {
71  /**
72  * Constructor.
73  *
74  * \param Tmax_s applicability period [s]
75  */
76  JUTCTracker(const double Tmax_s) :
77  JUTCTimeRange(-Tmax_s, +Tmax_s)
78  {}
79 
80 
81  /**
82  * Reset.
83  */
84  void reset()
85  {
86  set(0.0);
87  }
88 
89 
90  /**
91  * Set.
92  *
93  * \param t0_s UTC time [s]
94  */
95  void set(const double t0_s)
96  {
97  const double Tmax_s = 0.5 * (getUpperLimit() - getLowerLimit());
98 
99  setRange(t0_s - Tmax_s, t0_s + Tmax_s);
100  }
101  };
102 
103 
104  /**
105  * Dynamic orientation calibration.
106  */
107  struct JOrientation :
108  public JUTCTracker
109  {
110  enum {
111  NUMBER_OF_POINTS = 20, //!< number of points for interpolation
112  NUMBER_OF_DEGREES = 1 //!< number of degrees for interpolation
113  };
114 
119  typedef typename function_type::collection_type::container_type container_type;
120 
123 
126 
127 
128  /**
129  * Constructor.
130  *
131  * \param detector detector
132  * \param Tmax_s applicability period of calibration [s]
133  */
135  const double Tmax_s) :
136  JUTCTracker(Tmax_s)
137  {
138  using namespace JPP;
139 
141  THROW(JValueOutOfRange, "Detector version " << detector.getVersion() << " < " << JDetectorVersion::V4);
142  }
143 
144  if (hasDetectorAddressMap(detector.getID())) {
145 
146  const JDetectorAddressMap& demo = getDetectorAddressMap(detector.getID());
147 
148  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
149  if (module->getFloor() != 0) {
150  buffer[module->getID()] = getRotation(JDETECTOR::getModule(demo.get(module->getID())), *module);
151  }
152  }
153 
154  } else {
155 
156  THROW(JValueOutOfRange, "No detector address map for detector identier " << detector.getID());
157  }
158  }
159 
160 
161  /**
162  * Load calibration data.
163  *
164  * \param input calibration data
165  */
167  {
168  this->reset();
169 
170  while (input.hasNext()) {
171 
172  const JCOMPASS::JOrientation* orientation = input.next();
173 
174  static_cast<container_type&>(calibration[orientation->id]).push_back(element_type(orientation->t, getQuaternion(*orientation)));
175  }
176 
177  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
178  i->second.sort();
179  i->second.compile();
180  }
181  }
182 
183 
184  bool empty() const { return calibration.empty(); } //!< empty
185  size_t size() const { return calibration.size(); } //!< size
186  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
187  const_iterator end() const { return calibration.end(); } //!< end of calibration data
188  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
189  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
190 
191 
192  /**
193  * Get coverage.
194  *
195  * \param detector detector
196  * \param t1_s UTC time [s]
197  * \return coverage [0,1]
198  */
199  double getCoverage(const JDetector& detector, const double t1_s) const
200  {
201  int n0 = 0;
202  int n1 = 0;
203 
204  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
205 
206  if (module->getFloor() != 0 && !module->has(COMPASS_DISABLE)) {
207 
208  ++n0;
209 
210  if (calibration.has(module->getID()) && !calibration.get(module->getID()).empty()) {
211  if (calibration[module->getID()].getXmin() <= t1_s &&
212  calibration[module->getID()].getXmax() >= t1_s) {
213  ++n1;
214  }
215  }
216  }
217  }
218 
219  if (n0 != 0)
220  return (double) n1 / (double) n0;
221  else
222  return 0.0;
223  }
224 
225 
226  /**
227  * Calibrate given detector at given UTC time.
228  *
229  * \param detector detector (I/O)
230  * \param t1_s UTC time [s]
231  */
232  void update(JDetector& detector, const double t1_s)
233  {
234  using namespace std;
235  using namespace JPP;
236 
237  if (!calibration.empty()) {
238 
239  if (!in_range(t1_s)) {
240 
241  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
242 
243  if (module->getFloor() != 0 && !module->has(COMPASS_DISABLE) && calibration.has(module->getID()) && buffer.has(module->getID())) {
244 
245  const function_type& f1 = calibration.get(module->getID());
246 
247  if (!f1.empty()) {
248 
249  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
250 
251  JQuaternion3D Q0 = buffer[module->getID()];
252  JQuaternion3D Q1 = module->getQuaternion() * f1(t1_s);
253 
254  const JPosition3D center = module->getPosition();
255 
256  module->sub(center);
257 
258  module->rotate(Q1 * Q0.conjugate());
259 
260  module->add(center);
261 
262  buffer[module->getID()] = Q1;
263  }
264  }
265  }
266  }
267 
268  set(t1_s);
269  }
270  }
271  }
272 
273  protected:
276  };
277 
278 
279  /**
280  * Dynamic position calibration.
281  */
282  struct JPosition :
283  public JUTCTracker
284  {
285  enum {
286  NUMBER_OF_POINTS = 7, //!< number of points for interpolation
287  NUMBER_OF_DEGREES = 2 //!< number of degrees for interpolation
288  };
289 
291 
296 
298 
301 
302 
303  /**
304  * Constructor.
305  *
306  * \param detector detector
307  * \param Tmax_s applicability period of calibration [s]
308  */
310  const double Tmax_s) :
311  JUTCTracker(Tmax_s),
312  geometry(detector)
313  {
314  using namespace JPP;
315 
317  THROW(JValueOutOfRange, "Detector version " << detector.getVersion() << " < " << JDetectorVersion::V4);
318  }
319  }
320 
321 
322  /**
323  * Load calibration data.
324  *
325  * \param input calibration data
326  */
328  {
329  using namespace JPP;
330 
331  this->reset();
332 
333  while (input.hasNext()) {
334 
335  const JACOUSTICS::JEvt* evt = input.next();
336  const double t1_s = 0.5 * (evt->UNIXTimeStart + evt->UNIXTimeStop);
337 
338  for (JACOUSTICS::JEvt::const_iterator i = evt->begin(); i != evt->end(); ++i) {
339  calibration[i->id][t1_s] = JMODEL::JString(i->tx, i->ty);
340  }
341  }
342 
343  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
344  i->second.compile();
345  }
346  }
347 
348 
349  bool empty() const { return calibration.empty(); } //!< empty
350  size_t size() const { return calibration.size(); } //!< size
351  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
352  const_iterator end() const { return calibration.end(); } //!< end of calibration data
353  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
354  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
355 
356 
357  /**
358  * Get coverage.
359  *
360  * \param detector detector
361  * \param t1_s UTC time [s]
362  * \return coverage [0,1]
363  */
364  double getCoverage(const JDetector& detector, const double t1_s) const
365  {
366  int n0 = 0;
367  int n1 = 0;
368 
369  std::set<int> string;
370 
371  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
372  if (module->getFloor() != 0) {
373  string.insert(module->getString());
374  }
375  }
376 
377  for (std::set<int>::const_iterator i = string.begin(); i != string.end(); ++i) {
378 
379  ++n0;
380 
381  if (calibration.has(*i) && !calibration.get(*i).empty()) {
382  if (calibration[*i].getXmin() <= t1_s &&
383  calibration[*i].getXmax() >= t1_s) {
384  ++n1;
385  }
386  }
387  }
388 
389  if (n0 != 0)
390  return (double) n1 / (double) n0;
391  else
392  return 0.0;
393  }
394 
395 
396  /**
397  * Get detector geometry.
398  *
399  * \return detector geometry
400  */
401  const JGeometry& getGeometry() const
402  {
403  return geometry;
404  }
405 
406 
407  /**
408  * Calibrate given detector at given UTC time.
409  *
410  * \param detector detector (I/O)
411  * \param t1_s UTC time [s]
412  */
413  void update(JDetector& detector, const double t1_s)
414  {
415  using namespace std;
416  using namespace JPP;
417 
418  if (!calibration.empty()) {
419 
420  if (!in_range(t1_s)) {
421 
423 
424  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
425 
426  if (module->getFloor() != 0) {
427 
428  if (!buffer.has(module->getString())) {
429 
430  if (calibration.has(module->getString())) {
431 
432  const function_type& f1 = calibration.get(module->getString());
433 
434  if (!f1.empty()) {
435  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
436  buffer[module->getString()] = f1(t1_s);
437  }
438  }
439  }
440  }
441 
442  if (buffer.has(module->getString())) {
443  module->set(geometry[module->getString()].getPosition(buffer[module->getString()], module->getFloor()) - getPiezoPosition());
444  }
445  }
446  }
447 
448  set(t1_s);
449  }
450  }
451  }
452 
453  protected:
456  };
457 
458 
459  /**
460  * Constructor.
461  *
462  * \param detector detector
463  * \param Tmax_s applicability period of calibration [s]
464  */
466  const double Tmax_s) :
467  JDetector (detector),
468  orientation(detector, Tmax_s),
469  position (detector, Tmax_s)
470  {
472  }
473 
474 
475  /**
476  * Get actual detector.
477  *
478  * \return detector
479  */
480  const JDetector& getDetector() const
481  {
482  return static_cast<const JDetector&>(*this);
483  }
484 
485 
486  /**
487  * Load calibration data.
488  *
489  * \param input calibration data
490  */
491  template<class JObjectIterator_t>
492  void load(JObjectIterator_t& input)
493  {
495 
496  try { orientation.load(dynamic_cast<JObjectIterator<JCOMPASS::JOrientation>&>(input)); } catch(const std::exception&) {}
497  try { position .load(dynamic_cast<JObjectIterator<JACOUSTICS::JEvt>&> (input)); } catch(const std::exception&) {}
498  }
499 
500 
501  /**
502  * Get detector calibrated at given time.
503  *
504  * \param t1_s UTC time [s]
505  * \return detector
506  */
507  const JDetector& update(const double t1_s)
508  {
509  if (!in_range(t1_s)) {
510 
512 
513  orientation.update(static_cast<JDetector&>(*this), t1_s);
514  position .update(static_cast<JDetector&>(*this), t1_s);
515 
516  range.join(orientation.getUTCTimeRange());
517  range.join(position .getUTCTimeRange());
518 
519  setUTCTimeRange(range);
520  }
521 
522  return getDetector();
523  }
524 
525 
526  /**
527  * Get detector calibrated at given time.
528  *
529  * \param chronometer chronometer
530  * \return detector
531  */
532  const JDetector& update(const JDAQChronometer& chronometer)
533  {
534  return update(chronometer.getTimesliceStart().getUTCseconds());
535  }
536 
537 
538  /**
539  * Data structure for coverage of dynamic calibrations.
540  */
541  struct coverage_type {
542  double orientation; //!< coverage of detector by available orientation calibration [0,1]
543  double position; //!< coverage of detector by available position calibration [0,1]
544  };
545 
546 
547  /**
548  * Get coverage at given time.
549  *
550  * \param t1_s UTC time [s]
551  * \return coverage
552  */
553  coverage_type getCoverage(const double t1_s) const
554  {
555  return { orientation.getCoverage(*this, t1_s), position.getCoverage(*this, t1_s) };
556  }
557 
558 
559  /**
560  * Get coverage at given time.
561  *
562  * \param chronometer chronometer
563  * \return coverage
564  */
565  coverage_type getCoverage(const JDAQChronometer& chronometer) const
566  {
567  return getCoverage(chronometer.getTimesliceStart().getUTCseconds());
568  }
569 
570 
571  /**
572  * Get actual coverage.
573  *
574  * \return coverage
575  */
577  {
578  return getCoverage(0.5 * (getLowerLimit() + getUpperLimit()));
579  }
580 
581 
582  JOrientation orientation; //!< orientation calibration
583  JPosition position; //!< position calibration
584  };
585 }
586 
587 #endif
JTOOLS::JElement2D< double, JACOUSTICS::JMODEL::JString > element_type
Definition: JDynamics.hh:292
static const JRange< T, JComparator_t > DEFAULT_RANGE
Default range.
Definition: JRange.hh:555
void set(const double t0_s)
Set.
Definition: JDynamics.hh:95
Exceptions.
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
JDAQUTCExtended getTimesliceStart() const
Get start of timeslice.
int id
module identifier
General purpose class for collection of elements, see: &lt;a href=&quot;JTools.PDF&quot;;&gt;Collection of elements...
Definition: JCollection.hh:73
const_reverse_iterator rend() const
begin of reverse of calibration data
Definition: JDynamics.hh:354
The elements in a collection are sorted according to their abscissa values and a given distance opera...
coverage_type getCoverage(const JDAQChronometer &chronometer) const
Get coverage at given time.
Definition: JDynamics.hh:565
void update(JDetector &detector, const double t1_s)
Calibrate given detector at given UTC time.
Definition: JDynamics.hh:413
coverage_type getCoverage(const double t1_s) const
Get coverage at given time.
Definition: JDynamics.hh:553
Dynamic orientation calibration.
Definition: JDynamics.hh:107
Dynamic position calibration.
Definition: JDynamics.hh:282
double position
coverage of detector by available position calibration [0,1]
Definition: JDynamics.hh:543
const JDetector & getDetector() const
Get actual detector.
Definition: JDynamics.hh:480
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashMap.hh:87
JDynamics(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:465
JTOOLS::JHashMap< int, function_type > data_type
Definition: JDynamics.hh:297
const_reverse_iterator rend() const
begin of reverse of calibration data
Definition: JDynamics.hh:189
JQuaternion3D getQuaternion(const JQuaternion &Q)
Get quaternion.
JTOOLS::JPolfitFunction1D< NUMBER_OF_POINTS, NUMBER_OF_DEGREES, element_type, JTOOLS::JCollection > function_type
Definition: JDynamics.hh:118
function_type::collection_type::container_type container_type
Definition: JDynamics.hh:119
General purpose class for hash map of unique elements.
ROOT TTree parameter settings.
JPosition position
position calibration
Definition: JDynamics.hh:583
Detector data structure.
Definition: JDetector.hh:89
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Acoustic geometries.
bool hasDetectorAddressMap(const int id)
Check if detector address map is available.
const_reverse_iterator rbegin() const
begin of reverse of calibration data
Definition: JDynamics.hh:353
const JDetector & update(const JDAQChronometer &chronometer)
Get detector calibrated at given time.
Definition: JDynamics.hh:532
JOrientation orientation
orientation calibration
Definition: JDynamics.hh:582
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:186
static JRotation getRotation
Function object to get rotation matrix to go from first to second module.
Lookup table for PMT addresses in detector.
const std::string & getVersion() const
Get version.
Interface of object iteration for a single data type.
data_type::const_reverse_iterator const_reverse_iterator
Definition: JDynamics.hh:300
const JDetector & update(const double t1_s)
Get detector calibrated at given time.
Definition: JDynamics.hh:507
Data structure for detector geometry and calibration.
JTOOLS::JHashMap< int, JGEOMETRY3D::JQuaternion3D > buffer_type
Definition: JDynamics.hh:121
const JModuleAddressMap & get(const int id) const
Get module address map.
ROOT TTree parameter settings.
const JGeometry & getGeometry() const
Get detector geometry.
Definition: JDynamics.hh:401
number of points for interpolation
Definition: JDynamics.hh:111
const JQuaternion3D & getQuaternion() const
Get quaternion.
JACOUSTICS::JGeometry JGeometry
Definition: JDynamics.hh:290
static const int COMPASS_DISABLE
Enable (disable) use of compass if this status bit is 0 (1);.
double orientation
coverage of detector by available orientation calibration [0,1]
Definition: JDynamics.hh:542
void load(JObjectIterator_t &input)
Load calibration data.
Definition: JDynamics.hh:492
const_reverse_iterator rbegin() const
begin of reverse of calibration data
Definition: JDynamics.hh:188
JUTCTracker(const double Tmax_s)
Constructor.
Definition: JDynamics.hh:76
void setUTCTimeRange(const JRange< double > &timerange)
Set UTC time range.
data_type::const_iterator const_iterator
Definition: JDynamics.hh:299
virtual mapped_type & get(typename JClass< key_type >::argument_type key) override
Get mapped value.
Definition: JHashMap.hh:119
double UNIXTimeStop
stop time
void load(JObjectIterator< JACOUSTICS::JEvt > &input)
Load calibration data.
Definition: JDynamics.hh:327
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:352
Detector specific mapping between logical positions and readout channels of PMTs in optical modules...
virtual const pointer_type & next()=0
Get next element.
Detector file.
Definition: JHead.hh:224
Acoustic event fit.
Version with quaternion and time offset per module.
JPosition(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:309
void setRange(const range_type &range)
Set range.
Definition: JRange.hh:146
int getID() const
Get identifier.
Definition: JObjectID.hh:50
coverage_type getCoverage() const
Get actual coverage.
Definition: JDynamics.hh:576
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
virtual bool hasNext()=0
Check availability of next element.
General purpose class for a collection of sorted elements.
Compass event fit.
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
void update(JDetector &detector, const double t1_s)
Calibrate given detector at given UTC time.
Definition: JDynamics.hh:232
double getCoverage(const JDetector &detector, const double t1_s) const
Get coverage.
Definition: JDynamics.hh:199
void load(JObjectIterator< JCOMPASS::JOrientation > &input)
Load calibration data.
Definition: JDynamics.hh:166
JUINT32_t getUTCseconds() const
Get time.
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
JTOOLS::JElement2D< double, JGEOMETRY3D::JQuaternion3D > element_type
Definition: JDynamics.hh:115
bool in_range(argument_type x) const
Test whether value is inside range.
Definition: JRange.hh:323
Data structure for coverage of dynamic calibrations.
Definition: JDynamics.hh:541
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
data_type::const_iterator const_iterator
Definition: JDynamics.hh:124
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:187
data_type::const_reverse_iterator const_reverse_iterator
Definition: JDynamics.hh:125
Data structure for unit quaternion in three dimensions.
bool empty() const
empty
Definition: JDynamics.hh:349
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
UTC time range [s].
size_t size() const
size
Definition: JDynamics.hh:350
Dynamic detector calibration.
Definition: JDynamics.hh:62
double UNIXTimeStart
start time
Auxiliary class to define a range between two values.
JPosition3D getPiezoPosition()
Get relative position of piezo in optical module.
JTOOLS::JHashMap< int, function_type > data_type
Definition: JDynamics.hh:122
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:351
JOrientation(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:134
Template class for polynomial interpolation in 1D.
Definition: JPolfit.hh:149
2D Element.
Definition: JElement.hh:46
number of degrees for interpolation
Definition: JDynamics.hh:112
Orientation of module.
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
Compass event data types.
container_type::const_iterator const_iterator
Definition: JHashMap.hh:86
double getCoverage(const JDetector &detector, const double t1_s) const
Get coverage.
Definition: JDynamics.hh:364
Model for fit to acoutsics data.
const JUTCTimeRange & getUTCTimeRange() const
Get UTC time range.
Acoustic event fit.
bool has(const T &value) const
Test whether given value is present.
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:142
const JModule & getModule(const JModuleAddressMap &memo, const int id=-1, const JLocation &location=JLocation())
Get module according module address map.
JTOOLS::JPolfitFunction1D< NUMBER_OF_POINTS, NUMBER_OF_DEGREES, element_type, JTOOLS::JCollection > function_type
Definition: JDynamics.hh:295
Auxiliary data structure to track applicability period of calibration data.
Definition: JDynamics.hh:68
then usage $script< detector file >< tripodfile >< stage > input file nInput files correspond to the output of JAcousticsEventBuilder[.sh] nFirst stage eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY eval JPrintDetector a $DETECTOR O CAN source JAcoustics sh $DETECTOR_ID typeset A CONFIGURATION for key in Tmax_s
range_type & join(const range_type &range)
Join ranges.
Definition: JRange.hh:415
JQuaternion3D & conjugate()
Conjugate quaternion.
container_type::iterator iterator
Definition: JHashMap.hh:88