Jpp  18.0.0-rc.3
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"
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  * Dynamic detector calibration.
62  */
63  struct JDynamics :
64  public JDetector
65  {
66  /**
67  * Auxiliary data structure to track applicability period of calibration data.
68  */
69  struct JUTCTracker :
70  public JUTCTimeRange
71  {
72  /**
73  * Constructor.
74  *
75  * \param Tmax_s applicability period [s]
76  */
77  JUTCTracker(const double Tmax_s) :
78  JUTCTimeRange(-Tmax_s, +Tmax_s)
79  {}
80 
81 
82  /**
83  * Reset.
84  */
85  void reset()
86  {
87  set(0.0);
88  }
89 
90 
91  /**
92  * Set.
93  *
94  * \param t0_s UTC time [s]
95  */
96  void set(const double t0_s)
97  {
98  const double Tmax_s = 0.5 * (getUpperLimit() - getLowerLimit());
99 
100  setRange(t0_s - Tmax_s, t0_s + Tmax_s);
101  }
102  };
103 
104 
105  /**
106  * Dynamic orientation calibration.
107  */
108  struct JOrientation :
109  public JUTCTracker
110  {
111  enum {
112  NUMBER_OF_POINTS = 20, //!< number of points for interpolation
113  NUMBER_OF_DEGREES = 1 //!< number of degrees for interpolation
114  };
115 
120  typedef typename function_type::collection_type::container_type container_type;
121 
124 
127 
128 
129  /**
130  * Constructor.
131  *
132  * \param detector detector
133  * \param Tmax_s applicability period of calibration [s]
134  */
136  const double Tmax_s) :
137  JUTCTracker(Tmax_s)
138  {
139  using namespace JPP;
140 
142  THROW(JValueOutOfRange, "Detector version " << detector.getVersion() << " < " << JDetectorVersion::V4);
143  }
144 
145  if (hasDetectorAddressMap(detector.getID())) {
146 
147  const JDetectorAddressMap& demo = getDetectorAddressMap(detector.getID());
148 
149  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
150  if (module->getFloor() != 0) {
151  buffer[module->getID()] = getRotation(JDETECTOR::getModule(demo.get(module->getID())), *module);
152  }
153  }
154 
155  } else {
156 
157  THROW(JValueOutOfRange, "No detector address map for detector identier " << detector.getID());
158  }
159  }
160 
161 
162  /**
163  * Load calibration data.
164  *
165  * \param input calibration data
166  */
168  {
169  this->reset();
170 
171  while (input.hasNext()) {
172 
173  const JCOMPASS::JOrientation* orientation = input.next();
174 
175  if (buffer.has(orientation->id)) {
176  static_cast<container_type&>(calibration[orientation->id]).push_back(element_type(orientation->t, getQuaternion(*orientation)));
177  }
178  }
179 
180  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
181  i->second.sort();
182  i->second.compile();
183  }
184  }
185 
186 
187  bool empty() const { return calibration.empty(); } //!< empty
188  size_t size() const { return calibration.size(); } //!< size
189  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
190  const_iterator end() const { return calibration.end(); } //!< end of calibration data
191  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
192  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
193 
194 
195  /**
196  * Get coverage.
197  *
198  * \param detector detector
199  * \param t1_s UTC time [s]
200  * \return coverage [0,1]
201  */
202  double getCoverage(const JDetector& detector, const double t1_s) const
203  {
204  int n0 = 0;
205  int n1 = 0;
206 
207  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
208 
209  if (module->getFloor() != 0 && !module->has(COMPASS_DISABLE)) {
210 
211  ++n0;
212 
213  if (calibration.has(module->getID()) && !calibration.get(module->getID()).empty()) {
214  if (calibration[module->getID()].getXmin() <= t1_s &&
215  calibration[module->getID()].getXmax() >= t1_s) {
216  ++n1;
217  }
218  }
219  }
220  }
221 
222  if (n0 != 0)
223  return (double) n1 / (double) n0;
224  else
225  return 0.0;
226  }
227 
228 
229  /**
230  * Calibrate given detector at given UTC time.
231  *
232  * \param detector detector (I/O)
233  * \param t1_s UTC time [s]
234  * \return true if updated; else false
235  */
236  bool update(JDetector& detector, const double t1_s)
237  {
238  using namespace std;
239  using namespace JPP;
240 
241  if (!calibration.empty()) {
242 
243  if (!in_range(t1_s)) {
244 
245  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
246 
247  if (module->getFloor() != 0 && !module->has(COMPASS_DISABLE) && calibration.has(module->getID()) && buffer.has(module->getID())) {
248 
249  const function_type& f1 = calibration.get(module->getID());
250 
251  if (!f1.empty()) {
252 
253  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
254 
255  JQuaternion3D Q0 = buffer[module->getID()];
256  JQuaternion3D Q1 = module->getQuaternion() * f1(t1_s);
257 
258  const JPosition3D center = module->getPosition();
259 
260  module->sub(center);
261 
262  module->rotate(Q1 * Q0.conjugate());
263 
264  module->add(center);
265 
266  buffer[module->getID()] = Q1;
267  }
268  }
269  }
270  }
271 
272  set(t1_s);
273 
274  return true;
275  }
276  }
277 
278  return false;
279  }
280 
281  protected:
284  };
285 
286 
287  /**
288  * Dynamic position calibration.
289  */
290  struct JPosition :
291  public JUTCTracker
292  {
293  enum {
294  NUMBER_OF_POINTS = 7, //!< number of points for interpolation
295  NUMBER_OF_DEGREES = 2 //!< number of degrees for interpolation
296  };
297 
299 
304 
306 
309 
310 
311  /**
312  * Constructor.
313  *
314  * \param detector detector
315  * \param Tmax_s applicability period of calibration [s]
316  */
318  const double Tmax_s) :
319  JUTCTracker(Tmax_s),
320  geometry(detector)
321  {
322  using namespace JPP;
323 
325  THROW(JValueOutOfRange, "Detector version " << detector.getVersion() << " < " << JDetectorVersion::V4);
326  }
327  }
328 
329 
330  /**
331  * Load calibration data.
332  *
333  * \param input calibration data
334  */
336  {
337  using namespace JPP;
338 
339  this->reset();
340 
341  while (input.hasNext()) {
342 
343  const JACOUSTICS::JEvt* evt = input.next();
344  const double t1_s = 0.5 * (evt->UNIXTimeStart + evt->UNIXTimeStop);
345 
346  for (JACOUSTICS::JEvt::const_iterator i = evt->begin(); i != evt->end(); ++i) {
347  calibration[i->id][t1_s] = getString(*i);
348  }
349  }
350 
351  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
352  i->second.compile();
353  }
354  }
355 
356 
357  bool empty() const { return calibration.empty(); } //!< empty
358  size_t size() const { return calibration.size(); } //!< size
359  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
360  const_iterator end() const { return calibration.end(); } //!< end of calibration data
361  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
362  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
363 
364 
365  /**
366  * Get coverage.
367  *
368  * \param detector detector
369  * \param t1_s UTC time [s]
370  * \return coverage [0,1]
371  */
372  double getCoverage(const JDetector& detector, const double t1_s) const
373  {
374  int n0 = 0;
375  int n1 = 0;
376 
378 
379  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
380  if (module->getFloor() != 0) {
381  string.insert(module->getString());
382  }
383  }
384 
385  for (std::set<int>::const_iterator i = string.begin(); i != string.end(); ++i) {
386 
387  ++n0;
388 
389  if (calibration.has(*i) && !calibration.get(*i).empty()) {
390  if (calibration[*i].getXmin() <= t1_s &&
391  calibration[*i].getXmax() >= t1_s) {
392  ++n1;
393  }
394  }
395  }
396 
397  if (n0 != 0)
398  return (double) n1 / (double) n0;
399  else
400  return 0.0;
401  }
402 
403 
404  /**
405  * Get detector geometry.
406  *
407  * \return detector geometry
408  */
409  const JGeometry& getGeometry() const
410  {
411  return geometry;
412  }
413 
414 
415  /**
416  * Calibrate given detector at given UTC time.
417  *
418  * \param detector detector (I/O)
419  * \param t1_s UTC time [s]
420  * \return true if updated; else false
421  */
422  bool update(JDetector& detector, const double t1_s)
423  {
424  using namespace std;
425  using namespace JPP;
426 
427  if (!calibration.empty()) {
428 
429  if (!in_range(t1_s)) {
430 
432 
433  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
434 
435  if (module->getFloor() != 0) {
436 
437  if (!buffer.has(module->getString())) {
438 
439  if (calibration.has(module->getString())) {
440 
441  const function_type& f1 = calibration.get(module->getString());
442 
443  if (!f1.empty()) {
444  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
445  buffer[module->getString()] = f1(t1_s);
446  }
447  }
448  }
449  }
450 
451  if (buffer.has(module->getString())) {
452  module->set(geometry[module->getString()].getPosition(buffer[module->getString()], module->getFloor()) - getPiezoPosition());
453  }
454  }
455  }
456 
457  set(t1_s);
458 
459  return true;
460  }
461  }
462 
463  return false;
464  }
465 
466  protected:
469  };
470 
471 
472  /**
473  * Constructor.
474  *
475  * \param detector detector
476  * \param Tmax_s applicability period of calibration [s]
477  */
479  const double Tmax_s) :
480  JDetector (detector),
481  orientation(detector, Tmax_s),
482  position (detector, Tmax_s)
483  {
485  }
486 
487 
488  /**
489  * Get actual detector.
490  *
491  * \return detector
492  */
493  const JDetector& getDetector() const
494  {
495  return static_cast<const JDetector&>(*this);
496  }
497 
498 
499  /**
500  * Load calibration data.
501  *
502  * \param input calibration data
503  */
504  template<class JObjectIterator_t>
505  void load(JObjectIterator_t& input)
506  {
508 
509  try { orientation.load(dynamic_cast<JObjectIterator<JCOMPASS::JOrientation>&>(input)); } catch(const std::exception&) {}
510  try { position .load(dynamic_cast<JObjectIterator<JACOUSTICS::JEvt>&> (input)); } catch(const std::exception&) {}
511  }
512 
513 
514  /**
515  * Get detector calibrated at given time.
516  *
517  * \param t1_s UTC time [s]
518  * \return true if updated; else false
519  */
520  bool update(const double t1_s)
521  {
522  bool is_updated = false;
523 
524  if (!in_range(t1_s)) {
525 
527 
528  if (orientation.update(static_cast<JDetector&>(*this), t1_s)) { is_updated = true; }
529  if (position .update(static_cast<JDetector&>(*this), t1_s)) { is_updated = true; }
530 
531  range.join(orientation.getUTCTimeRange());
532  range.join(position .getUTCTimeRange());
533 
534  setUTCTimeRange(range);
535  }
536 
537  return is_updated;
538  }
539 
540 
541  /**
542  * Get detector calibrated at given time.
543  *
544  * \param chronometer chronometer
545  * \return true if updated; else false
546  */
547  bool update(const JDAQChronometer& chronometer)
548  {
549  return update(chronometer.getTimesliceStart().getUTCseconds());
550  }
551 
552 
553  /**
554  * Data structure for coverage of dynamic calibrations.
555  */
556  struct coverage_type {
557  double orientation; //!< coverage of detector by available orientation calibration [0,1]
558  double position; //!< coverage of detector by available position calibration [0,1]
559  };
560 
561 
562  /**
563  * Get coverage at given time.
564  *
565  * \param t1_s UTC time [s]
566  * \return coverage
567  */
568  coverage_type getCoverage(const double t1_s) const
569  {
570  return { orientation.getCoverage(*this, t1_s), position.getCoverage(*this, t1_s) };
571  }
572 
573 
574  /**
575  * Get coverage at given time.
576  *
577  * \param chronometer chronometer
578  * \return coverage
579  */
580  coverage_type getCoverage(const JDAQChronometer& chronometer) const
581  {
582  return getCoverage(chronometer.getTimesliceStart().getUTCseconds());
583  }
584 
585 
586  /**
587  * Get actual coverage.
588  *
589  * \return coverage
590  */
592  {
593  return getCoverage(0.5 * (getLowerLimit() + getUpperLimit()));
594  }
595 
596 
597  JOrientation orientation; //!< orientation calibration
598  JPosition position; //!< position calibration
599  };
600 }
601 
602 #endif
JTOOLS::JElement2D< double, JACOUSTICS::JMODEL::JString > element_type
Definition: JDynamics.hh:300
void set(const double t0_s)
Set.
Definition: JDynamics.hh:96
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:362
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:580
coverage_type getCoverage(const double t1_s) const
Get coverage at given time.
Definition: JDynamics.hh:568
Dynamic orientation calibration.
Definition: JDynamics.hh:108
Dynamic position calibration.
Definition: JDynamics.hh:290
double position
coverage of detector by available position calibration [0,1]
Definition: JDynamics.hh:558
const JDetector & getDetector() const
Get actual detector.
Definition: JDynamics.hh:493
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashMap.hh:87
JDynamics(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:478
JTOOLS::JHashMap< int, function_type > data_type
Definition: JDynamics.hh:305
const_reverse_iterator rend() const
begin of reverse of calibration data
Definition: JDynamics.hh:192
JQuaternion3D getQuaternion(const JQuaternion &Q)
Get quaternion.
JTOOLS::JPolfitFunction1D< NUMBER_OF_POINTS, NUMBER_OF_DEGREES, element_type, JTOOLS::JCollection > function_type
Definition: JDynamics.hh:119
function_type::collection_type::container_type container_type
Definition: JDynamics.hh:120
General purpose class for hash map of unique elements.
ROOT TTree parameter settings.
JPosition position
position calibration
Definition: JDynamics.hh:598
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:361
JOrientation orientation
orientation calibration
Definition: JDynamics.hh:597
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:189
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:308
Data structure for detector geometry and calibration.
JTOOLS::JHashMap< int, JGEOMETRY3D::JQuaternion3D > buffer_type
Definition: JDynamics.hh:122
const JModuleAddressMap & get(const int id) const
Get module address map.
JMODEL::JString getString(const JFit &fit)
Get model parameters of string.
ROOT TTree parameter settings.
const JGeometry & getGeometry() const
Get detector geometry.
Definition: JDynamics.hh:409
const JQuaternion3D & getQuaternion() const
Get quaternion.
JACOUSTICS::JGeometry JGeometry
Definition: JDynamics.hh:298
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:557
void load(JObjectIterator_t &input)
Load calibration data.
Definition: JDynamics.hh:505
const_reverse_iterator rbegin() const
begin of reverse of calibration data
Definition: JDynamics.hh:191
JUTCTracker(const double Tmax_s)
Constructor.
Definition: JDynamics.hh:77
void setUTCTimeRange(const JRange< double > &timerange)
Set UTC time range.
const JPolynome f1(1.0, 2.0, 3.0)
Function.
data_type::const_iterator const_iterator
Definition: JDynamics.hh:307
Acoustic event fit toolkit.
virtual mapped_type & get(typename JClass< key_type >::argument_type key) override
Get mapped value.
Definition: JHashMap.hh:146
double UNIXTimeStop
stop time
void load(JObjectIterator< JACOUSTICS::JEvt > &input)
Load calibration data.
Definition: JDynamics.hh:335
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:360
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:226
Acoustic event fit.
Version with quaternion and time offset per module.
JPosition(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:317
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:591
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
then awk string
virtual bool hasNext()=0
Check availability of next element.
number of points for interpolation
Definition: JDynamics.hh:112
General purpose class for a collection of sorted elements.
Compass event fit.
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
double getCoverage(const JDetector &detector, const double t1_s) const
Get coverage.
Definition: JDynamics.hh:202
void load(JObjectIterator< JCOMPASS::JOrientation > &input)
Load calibration data.
Definition: JDynamics.hh:167
JUINT32_t getUTCseconds() const
Get major time.
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
JTOOLS::JElement2D< double, JGEOMETRY3D::JQuaternion3D > element_type
Definition: JDynamics.hh:116
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:556
static const JGetDetectorVersion getDetectorVersion
Function object to map detector version to numerical value.
data_type::const_iterator const_iterator
Definition: JDynamics.hh:125
bool update(JDetector &detector, const double t1_s)
Calibrate given detector at given UTC time.
Definition: JDynamics.hh:236
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:190
data_type::const_reverse_iterator const_reverse_iterator
Definition: JDynamics.hh:126
bool update(const double t1_s)
Get detector calibrated at given time.
Definition: JDynamics.hh:520
Data structure for unit quaternion in three dimensions.
bool empty() const
empty
Definition: JDynamics.hh:357
bool update(const JDAQChronometer &chronometer)
Get detector calibrated at given time.
Definition: JDynamics.hh:547
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:358
Dynamic detector calibration.
Definition: JDynamics.hh:63
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:123
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:359
number of degrees for interpolation
Definition: JDynamics.hh:113
JOrientation(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:135
Template class for polynomial interpolation in 1D.
Definition: JPolfit.hh:149
bool update(JDetector &detector, const double t1_s)
Calibrate given detector at given UTC time.
Definition: JDynamics.hh:422
2D Element.
Definition: JElement.hh:46
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
static JRange< T, JComparator_t > DEFAULT_RANGE()
Default range.
Definition: JRange.hh:555
double getCoverage(const JDetector &detector, const double t1_s) const
Get coverage.
Definition: JDynamics.hh:372
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:303
Auxiliary data structure to track applicability period of calibration data.
Definition: JDynamics.hh:69
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