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