Jpp - 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 
15 #include "JAcoustics/JModel.hh"
16 #include "JAcoustics/JGeometry.hh"
17 #include "JAcoustics/JEvt.hh"
18 
19 #include "JLang/JObjectIterator.hh"
20 #include "JLang/JException.hh"
21 
23 #include "JGeometry3D/JEigen3D.hh"
24 
25 #include "JTools/JElement.hh"
26 #include "JTools/JCollection.hh"
27 #include "JTools/JPolfit.hh"
28 #include "JTools/JHashMap.hh"
29 #include "JTools/JRange.hh"
30 
32 
33 
34 /**
35  * \file
36  *
37  * Dynamic detector calibration.
38  * \author mdejong
39  */
40 
41 namespace JDYNAMICS {}
42 namespace JPP { using namespace JDYNAMICS; }
43 
44 /**
45  * Main namespace for dynamic position and orientation calibration.
46  */
47 namespace JDYNAMICS {
48 
53  using JUTC::JUTCTimeRange;
54 
55 
56  /**
57  * Dynamic detector calibration.
58  */
59  struct JDynamics :
60  public JDetector
61  {
62  /**
63  * Auxiliary data structure to track applicability period of calibration data.
64  */
65  struct JUTCTracker :
66  public JUTCTimeRange
67  {
68  /**
69  * Constructor.
70  *
71  * \param Tmax_s applicability period [s]
72  */
73  JUTCTracker(const double Tmax_s) :
74  JUTCTimeRange(-Tmax_s, +Tmax_s)
75  {}
76 
77 
78  /**
79  * Reset.
80  */
81  void reset()
82  {
83  set(0.0);
84  }
85 
86 
87  /**
88  * Set.
89  *
90  * \param t0_s UTC time [s]
91  */
92  void set(const double t0_s)
93  {
94  const double Tmax_s = 0.5 * (getUpperLimit() - getLowerLimit());
95 
96  setRange(t0_s - Tmax_s, t0_s + Tmax_s);
97  }
98  };
99 
100 
101  /**
102  * Dynamic orientation calibration.
103  */
104  struct JOrientation :
105  public JUTCTracker
106  {
107  enum {
108  NUMBER_OF_POINTS = 20, //!< number of points for interpolation
109  NUMBER_OF_DEGREES = 1 //!< number of degrees for interpolation
110  };
111 
116  typedef typename function_type::collection_type::container_type container_type;
117 
120 
123 
124 
125  /**
126  * Constructor.
127  *
128  * \param detector detector
129  * \param Tmax_s applicability period of calibration [s]
130  */
132  const double Tmax_s) :
133  JUTCTracker(Tmax_s)
134  {
135  using namespace JPP;
136 
137  if (hasDetectorAddressMap(detector.getID())) {
138 
139  const JDetectorAddressMap& demo = getDetectorAddressMap(detector.getID());
140 
141  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
142  if (module->getFloor() != 0) {
143  buffer[module->getID()] = getRotation(JDETECTOR::getModule(demo.get(module->getID())), *module);
144  }
145  }
146 
147  } else {
148 
149  THROW(JValueOutOfRange, "No detector address map for detector identier " << detector.getID());
150  }
151  }
152 
153 
154  /**
155  * Load calibration data.
156  *
157  * \param input calibration data
158  */
160  {
161  this->reset();
162 
163  while (input.hasNext()) {
164 
165  const JCOMPASS::JOrientation* orientation = input.next();
166 
167  static_cast<container_type&>(calibration[orientation->id]).push_back(element_type(orientation->t, getQuaternion(*orientation)));
168  }
169 
170  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
171  i->second.sort();
172  i->second.compile();
173  }
174  }
175 
176 
177  bool empty() const { return calibration.empty(); } //!< empty
178  size_t size() const { return calibration.size(); } //!< size
179  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
180  const_iterator end() const { return calibration.end(); } //!< end of calibration data
181  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
182  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
183 
184 
185  /**
186  * Get coverage.
187  *
188  * \param detector detector
189  * \param t1_s UTC time [s]
190  * \return coverage [0,1]
191  */
192  double getCoverage(const JDetector& detector, const double t1_s) const
193  {
194  int n0 = 0;
195  int n1 = 0;
196 
197  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
198 
199  if (module->getFloor() != 0) {
200 
201  ++n0;
202 
203  if (calibration.has(module->getID()) && !calibration.get(module->getID()).empty()) {
204  if (calibration[module->getID()].getXmin() <= t1_s &&
205  calibration[module->getID()].getXmax() >= t1_s) {
206  ++n1;
207  }
208  }
209  }
210  }
211 
212  return (double) n1 / (double) n0;
213  }
214 
215 
216  /**
217  * Calibrate given detector at given UTC time.
218  *
219  * \param detector detector (I/O)
220  * \param t1_s UTC time [s]
221  */
222  void update(JDetector& detector, const double t1_s)
223  {
224  using namespace std;
225  using namespace JPP;
226 
227  if (!calibration.empty()) {
228 
229  if (!in_range(t1_s)) {
230 
231  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
232 
233  if (module->getFloor() != 0 && calibration.has(module->getID()) && buffer.has(module->getID())) {
234 
235  const function_type& f1 = calibration.get(module->getID());
236 
237  if (!f1.empty()) {
238 
239  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
240 
241  JQuaternion3D Q0 = buffer[module->getID()];
242  JQuaternion3D Q1 = module->getQuaternion() * f1(t1_s);
243 
244  const JPosition3D center = module->getPosition();
245 
246  module->sub(center);
247 
248  module->rotate(Q1 * Q0.conjugate());
249 
250  module->add(center);
251 
252  buffer[module->getID()] = Q1;
253  }
254  }
255  }
256  }
257 
258  set(t1_s);
259  }
260  }
261  }
262 
263  protected:
266  };
267 
268 
269  /**
270  * Dynamic position calibration.
271  */
272  struct JPosition :
273  public JUTCTracker
274  {
275  enum {
276  NUMBER_OF_POINTS = 7, //!< number of points for interpolation
277  NUMBER_OF_DEGREES = 2 //!< number of degrees for interpolation
278  };
279 
281 
286 
288 
291 
292 
293  /**
294  * Constructor.
295  *
296  * \param detector detector
297  * \param Tmax_s applicability period of calibration [s]
298  */
300  const double Tmax_s) :
301  JUTCTracker(Tmax_s),
302  geometry(detector)
303  {}
304 
305 
306  /**
307  * Load calibration data.
308  *
309  * \param input calibration data
310  */
312  {
313  using namespace JPP;
314 
315  this->reset();
316 
317  while (input.hasNext()) {
318 
319  const JACOUSTICS::JEvt* evt = input.next();
320  const double t1_s = 0.5 * (evt->UNIXTimeStart + evt->UNIXTimeStop);
321 
322  for (JACOUSTICS::JEvt::const_iterator i = evt->begin(); i != evt->end(); ++i) {
323  calibration[i->id][t1_s] = JMODEL::JString(i->tx, i->ty);
324  }
325  }
326 
327  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
328  i->second.compile();
329  }
330  }
331 
332 
333  bool empty() const { return calibration.empty(); } //!< empty
334  size_t size() const { return calibration.size(); } //!< size
335  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
336  const_iterator end() const { return calibration.end(); } //!< end of calibration data
337  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
338  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
339 
340 
341  /**
342  * Get coverage.
343  *
344  * \param detector detector
345  * \param t1_s UTC time [s]
346  * \return coverage [0,1]
347  */
348  double getCoverage(const JDetector& detector, const double t1_s) const
349  {
350  int n0 = 0;
351  int n1 = 0;
352 
353  std::set<int> string;
354 
355  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
356  if (module->getFloor() != 0) {
357  string.insert(module->getString());
358  }
359  }
360 
361  for (std::set<int>::const_iterator i = string.begin(); i != string.end(); ++i) {
362 
363  ++n0;
364 
365  if (calibration.has(*i) && !calibration.get(*i).empty()) {
366  if (calibration[*i].getXmin() <= t1_s &&
367  calibration[*i].getXmax() >= t1_s) {
368  ++n1;
369  }
370  }
371  }
372 
373  return (double) n1 / (double) n0;
374  }
375 
376 
377  /**
378  * Get detector geometry.
379  *
380  * \return detector geometry
381  */
382  const JGeometry& getGeometry() const
383  {
384  return geometry;
385  }
386 
387 
388  /**
389  * Calibrate given detector at given UTC time.
390  *
391  * \param detector detector (I/O)
392  * \param t1_s UTC time [s]
393  */
394  void update(JDetector& detector, const double t1_s)
395  {
396  using namespace std;
397  using namespace JPP;
398 
399  if (!calibration.empty()) {
400 
401  if (!in_range(t1_s)) {
402 
404 
405  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
406 
407  if (module->getFloor() != 0) {
408 
409  if (!buffer.has(module->getString())) {
410 
411  if (calibration.has(module->getString())) {
412 
413  const function_type& f1 = calibration.get(module->getString());
414 
415  if (!f1.empty()) {
416  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
417  buffer[module->getString()] = f1(t1_s);
418  }
419  }
420  }
421  }
422 
423  if (buffer.has(module->getString())) {
424  module->set(geometry[module->getString()].getPosition(buffer[module->getString()], module->getFloor()) - getPiezoPosition());
425  }
426  }
427  }
428 
429  set(t1_s);
430  }
431  }
432  }
433 
434  protected:
437  };
438 
439 
440  /**
441  * Constructor.
442  *
443  * \param detector detector
444  * \param Tmax_s applicability period of calibration [s]
445  */
447  const double Tmax_s) :
448  JDetector (detector),
449  orientation(detector, Tmax_s),
450  position (detector, Tmax_s)
451  {
453  }
454 
455 
456  /**
457  * Load calibration data.
458  *
459  * \param input calibration data
460  */
461  template<class JObjectIterator_t>
462  void load(JObjectIterator_t& input)
463  {
465 
466  try { orientation.load(dynamic_cast<JObjectIterator<JCOMPASS::JOrientation>&>(input)); } catch(const std::exception&) {}
467  try { position .load(dynamic_cast<JObjectIterator<JACOUSTICS::JEvt>&> (input)); } catch(const std::exception&) {}
468  }
469 
470 
471  /**
472  * Get detector calibrated at given time.
473  *
474  * \param t1_s UTC time [s]
475  * \return detector
476  */
477  const JDetector& update(const double t1_s)
478  {
479  if (!in_range(t1_s)) {
480 
482 
483  orientation.update(static_cast<JDetector&>(*this), t1_s);
484  position .update(static_cast<JDetector&>(*this), t1_s);
485 
486  range.join(orientation.getUTCTimeRange());
487  range.join(position .getUTCTimeRange());
488 
489  setUTCTimeRange(range);
490  }
491 
492  return static_cast<const JDetector&>(*this);
493  }
494 
495 
496  /**
497  * Get detector calibrated at given time.
498  *
499  * \param chronometer chronometer
500  * \return detector
501  */
502  const JDetector& update(const JDAQChronometer& chronometer)
503  {
504  return update(chronometer.getTimesliceStart().getUTCseconds());
505  }
506 
507 
508  /**
509  * Data structure for coverage of dynamic calibrations.
510  */
511  struct coverage_type {
512  double orientation; //!< coverage of detector by available orientation calibration [0,1]
513  double position; //!< coverage of detector by available position calibration [0,1]
514  };
515 
516 
517  /**
518  * Get coverage at given time.
519  *
520  * \param t1_s UTC time [s]
521  * \return coverage
522  */
523  coverage_type getCoverage(const double t1_s) const
524  {
525  return { orientation.getCoverage(*this, t1_s), position.getCoverage(*this, t1_s) };
526  }
527 
528 
529  /**
530  * Get coverage at given time.
531  *
532  * \param chronometer chronometer
533  * \return coverage
534  */
535  coverage_type getCoverage(const JDAQChronometer& chronometer) const
536  {
537  return getCoverage(chronometer.getTimesliceStart().getUTCseconds());
538  }
539 
540 
541  /**
542  * Get actual coverage.
543  *
544  * \return coverage
545  */
547  {
548  return getCoverage(0.5 * (getLowerLimit() + getUpperLimit()));
549  }
550 
551 
552  JOrientation orientation; //!< orientation calibration
553  JPosition position; //!< position calibration
554  };
555 }
556 
557 #endif
JTOOLS::JElement2D< double, JACOUSTICS::JMODEL::JString > element_type
Definition: JDynamics.hh:282
static const JRange< T, JComparator_t > DEFAULT_RANGE
Default range.
Definition: JRange.hh:568
void set(const double t0_s)
Set.
Definition: JDynamics.hh:92
number of points for interpolation
Definition: JDynamics.hh:108
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:338
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:535
void update(JDetector &detector, const double t1_s)
Calibrate given detector at given UTC time.
Definition: JDynamics.hh:394
coverage_type getCoverage(const double t1_s) const
Get coverage at given time.
Definition: JDynamics.hh:523
Dynamic orientation calibration.
Definition: JDynamics.hh:104
Dynamic position calibration.
Definition: JDynamics.hh:272
double position
coverage of detector by available position calibration [0,1]
Definition: JDynamics.hh:513
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashMap.hh:86
JDynamics(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:446
JTOOLS::JHashMap< int, function_type > data_type
Definition: JDynamics.hh:287
const_reverse_iterator rend() const
begin of reverse of calibration data
Definition: JDynamics.hh:182
JQuaternion3D getQuaternion(const JQuaternion &Q)
Get quaternion.
JTOOLS::JPolfitFunction1D< NUMBER_OF_POINTS, NUMBER_OF_DEGREES, element_type, JTOOLS::JCollection > function_type
Definition: JDynamics.hh:115
function_type::collection_type::container_type container_type
Definition: JDynamics.hh:116
General purpose class for hash map of unique elements.
JPosition position
position calibration
Definition: JDynamics.hh:553
Detector data structure.
Definition: JDetector.hh:80
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
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:337
const JDetector & update(const JDAQChronometer &chronometer)
Get detector calibrated at given time.
Definition: JDynamics.hh:502
JOrientation orientation
orientation calibration
Definition: JDynamics.hh:552
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:179
static JRotation getRotation
Function object to get rotation matrix to go from first to second module.
Lookup table for PMT addresses in detector.
Interface of object iteration for a single data type.
data_type::const_reverse_iterator const_reverse_iterator
Definition: JDynamics.hh:290
const JDetector & update(const double t1_s)
Get detector calibrated at given time.
Definition: JDynamics.hh:477
Data structure for detector geometry and calibration.
JTOOLS::JHashMap< int, JGEOMETRY3D::JQuaternion3D > buffer_type
Definition: JDynamics.hh:118
const JModuleAddressMap & get(const int id) const
Get module address map.
const JGeometry & getGeometry() const
Get detector geometry.
Definition: JDynamics.hh:382
const JQuaternion3D & getQuaternion() const
Get quaternion.
JACOUSTICS::JGeometry JGeometry
Definition: JDynamics.hh:280
double orientation
coverage of detector by available orientation calibration [0,1]
Definition: JDynamics.hh:512
void load(JObjectIterator_t &input)
Load calibration data.
Definition: JDynamics.hh:462
const_reverse_iterator rbegin() const
begin of reverse of calibration data
Definition: JDynamics.hh:181
JUTCTracker(const double Tmax_s)
Constructor.
Definition: JDynamics.hh:73
void setUTCTimeRange(const JRange< double > &timerange)
Set UTC time range.
data_type::const_iterator const_iterator
Definition: JDynamics.hh:289
virtual mapped_type & get(typename JClass< key_type >::argument_type key) override
Get mapped value.
Definition: JHashMap.hh:118
double UNIXTimeStop
stop time
void load(JObjectIterator< JACOUSTICS::JEvt > &input)
Load calibration data.
Definition: JDynamics.hh:311
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:336
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:196
Acoustic event fit.
JPosition(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:299
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:546
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:222
double getCoverage(const JDetector &detector, const double t1_s) const
Get coverage.
Definition: JDynamics.hh:192
void load(JObjectIterator< JCOMPASS::JOrientation > &input)
Load calibration data.
Definition: JDynamics.hh:159
JUINT32_t getUTCseconds() const
Get time.
JDetectorAddressMap & getDetectorAddressMap()
Get detector address map.
JTOOLS::JElement2D< double, JGEOMETRY3D::JQuaternion3D > element_type
Definition: JDynamics.hh:112
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:511
data_type::const_iterator const_iterator
Definition: JDynamics.hh:121
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi mv $WORKDIR/fit.root $MODULE_ROOT typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Definition: module-Z:fit.sh:84
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:180
data_type::const_reverse_iterator const_reverse_iterator
Definition: JDynamics.hh:122
Data structure for unit quaternion in three dimensions.
bool empty() const
empty
Definition: JDynamics.hh:333
UTC time range [s].
size_t size() const
size
Definition: JDynamics.hh:334
Dynamic detector calibration.
Definition: JDynamics.hh:59
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:119
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:335
JOrientation(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:131
Template class for polynomial interpolation in 1D.
Definition: JPolfit.hh:144
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:85
double getCoverage(const JDetector &detector, const double t1_s) const
Get coverage.
Definition: JDynamics.hh:348
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.
number of degrees for interpolation
Definition: JDynamics.hh:109
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:285
Auxiliary data structure to track applicability period of calibration data.
Definition: JDynamics.hh:65
range_type & join(const range_type &range)
Join ranges.
Definition: JRange.hh:416
JQuaternion3D & conjugate()
Conjugate quaternion.
container_type::iterator iterator
Definition: JHashMap.hh:87