Jpp  17.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"
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  static_cast<container_type&>(calibration[orientation->id]).push_back(element_type(orientation->t, getQuaternion(*orientation)));
176  }
177 
178  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
179  i->second.sort();
180  i->second.compile();
181  }
182  }
183 
184 
185  bool empty() const { return calibration.empty(); } //!< empty
186  size_t size() const { return calibration.size(); } //!< size
187  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
188  const_iterator end() const { return calibration.end(); } //!< end of calibration data
189  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
190  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
191 
192 
193  /**
194  * Get coverage.
195  *
196  * \param detector detector
197  * \param t1_s UTC time [s]
198  * \return coverage [0,1]
199  */
200  double getCoverage(const JDetector& detector, const double t1_s) const
201  {
202  int n0 = 0;
203  int n1 = 0;
204 
205  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
206 
207  if (module->getFloor() != 0 && !module->has(COMPASS_DISABLE)) {
208 
209  ++n0;
210 
211  if (calibration.has(module->getID()) && !calibration.get(module->getID()).empty()) {
212  if (calibration[module->getID()].getXmin() <= t1_s &&
213  calibration[module->getID()].getXmax() >= t1_s) {
214  ++n1;
215  }
216  }
217  }
218  }
219 
220  if (n0 != 0)
221  return (double) n1 / (double) n0;
222  else
223  return 0.0;
224  }
225 
226 
227  /**
228  * Calibrate given detector at given UTC time.
229  *
230  * \param detector detector (I/O)
231  * \param t1_s UTC time [s]
232  * \return true if updated; else false
233  */
234  bool update(JDetector& detector, const double t1_s)
235  {
236  using namespace std;
237  using namespace JPP;
238 
239  if (!calibration.empty()) {
240 
241  if (!in_range(t1_s)) {
242 
243  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
244 
245  if (module->getFloor() != 0 && !module->has(COMPASS_DISABLE) && calibration.has(module->getID()) && buffer.has(module->getID())) {
246 
247  const function_type& f1 = calibration.get(module->getID());
248 
249  if (!f1.empty()) {
250 
251  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
252 
253  JQuaternion3D Q0 = buffer[module->getID()];
254  JQuaternion3D Q1 = module->getQuaternion() * f1(t1_s);
255 
256  const JPosition3D center = module->getPosition();
257 
258  module->sub(center);
259 
260  module->rotate(Q1 * Q0.conjugate());
261 
262  module->add(center);
263 
264  buffer[module->getID()] = Q1;
265  }
266  }
267  }
268  }
269 
270  set(t1_s);
271 
272  return true;
273  }
274  }
275 
276  return false;
277  }
278 
279  protected:
282  };
283 
284 
285  /**
286  * Dynamic position calibration.
287  */
288  struct JPosition :
289  public JUTCTracker
290  {
291  enum {
292  NUMBER_OF_POINTS = 7, //!< number of points for interpolation
293  NUMBER_OF_DEGREES = 2 //!< number of degrees for interpolation
294  };
295 
297 
302 
304 
307 
308 
309  /**
310  * Constructor.
311  *
312  * \param detector detector
313  * \param Tmax_s applicability period of calibration [s]
314  */
316  const double Tmax_s) :
317  JUTCTracker(Tmax_s),
318  geometry(detector)
319  {
320  using namespace JPP;
321 
323  THROW(JValueOutOfRange, "Detector version " << detector.getVersion() << " < " << JDetectorVersion::V4);
324  }
325  }
326 
327 
328  /**
329  * Load calibration data.
330  *
331  * \param input calibration data
332  */
334  {
335  using namespace JPP;
336 
337  this->reset();
338 
339  while (input.hasNext()) {
340 
341  const JACOUSTICS::JEvt* evt = input.next();
342  const double t1_s = 0.5 * (evt->UNIXTimeStart + evt->UNIXTimeStop);
343 
344  for (JACOUSTICS::JEvt::const_iterator i = evt->begin(); i != evt->end(); ++i) {
345  calibration[i->id][t1_s] = getModel(*i);
346  }
347  }
348 
349  for (data_type::iterator i = calibration.begin(); i != calibration.end(); ++i) {
350  i->second.compile();
351  }
352  }
353 
354 
355  bool empty() const { return calibration.empty(); } //!< empty
356  size_t size() const { return calibration.size(); } //!< size
357  const_iterator begin() const { return calibration.begin(); } //!< begin of calibration data
358  const_iterator end() const { return calibration.end(); } //!< end of calibration data
359  const_reverse_iterator rbegin() const { return calibration.rbegin(); } //!< begin of reverse of calibration data
360  const_reverse_iterator rend() const { return calibration.rend(); } //!< begin of reverse of calibration data
361 
362 
363  /**
364  * Get coverage.
365  *
366  * \param detector detector
367  * \param t1_s UTC time [s]
368  * \return coverage [0,1]
369  */
370  double getCoverage(const JDetector& detector, const double t1_s) const
371  {
372  int n0 = 0;
373  int n1 = 0;
374 
375  std::set<int> string;
376 
377  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
378  if (module->getFloor() != 0) {
379  string.insert(module->getString());
380  }
381  }
382 
383  for (std::set<int>::const_iterator i = string.begin(); i != string.end(); ++i) {
384 
385  ++n0;
386 
387  if (calibration.has(*i) && !calibration.get(*i).empty()) {
388  if (calibration[*i].getXmin() <= t1_s &&
389  calibration[*i].getXmax() >= t1_s) {
390  ++n1;
391  }
392  }
393  }
394 
395  if (n0 != 0)
396  return (double) n1 / (double) n0;
397  else
398  return 0.0;
399  }
400 
401 
402  /**
403  * Get detector geometry.
404  *
405  * \return detector geometry
406  */
407  const JGeometry& getGeometry() const
408  {
409  return geometry;
410  }
411 
412 
413  /**
414  * Calibrate given detector at given UTC time.
415  *
416  * \param detector detector (I/O)
417  * \param t1_s UTC time [s]
418  * \return true if updated; else false
419  */
420  bool update(JDetector& detector, const double t1_s)
421  {
422  using namespace std;
423  using namespace JPP;
424 
425  if (!calibration.empty()) {
426 
427  if (!in_range(t1_s)) {
428 
430 
431  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
432 
433  if (module->getFloor() != 0) {
434 
435  if (!buffer.has(module->getString())) {
436 
437  if (calibration.has(module->getString())) {
438 
439  const function_type& f1 = calibration.get(module->getString());
440 
441  if (!f1.empty()) {
442  if (t1_s >= f1.getXmin() && t1_s <= f1.getXmax()) {
443  buffer[module->getString()] = f1(t1_s);
444  }
445  }
446  }
447  }
448 
449  if (buffer.has(module->getString())) {
450  module->set(geometry[module->getString()].getPosition(buffer[module->getString()], module->getFloor()) - getPiezoPosition());
451  }
452  }
453  }
454 
455  set(t1_s);
456 
457  return true;
458  }
459  }
460 
461  return false;
462  }
463 
464  protected:
467  };
468 
469 
470  /**
471  * Constructor.
472  *
473  * \param detector detector
474  * \param Tmax_s applicability period of calibration [s]
475  */
477  const double Tmax_s) :
478  JDetector (detector),
479  orientation(detector, Tmax_s),
480  position (detector, Tmax_s)
481  {
483  }
484 
485 
486  /**
487  * Get actual detector.
488  *
489  * \return detector
490  */
491  const JDetector& getDetector() const
492  {
493  return static_cast<const JDetector&>(*this);
494  }
495 
496 
497  /**
498  * Load calibration data.
499  *
500  * \param input calibration data
501  */
502  template<class JObjectIterator_t>
503  void load(JObjectIterator_t& input)
504  {
506 
507  try { orientation.load(dynamic_cast<JObjectIterator<JCOMPASS::JOrientation>&>(input)); } catch(const std::exception&) {}
508  try { position .load(dynamic_cast<JObjectIterator<JACOUSTICS::JEvt>&> (input)); } catch(const std::exception&) {}
509  }
510 
511 
512  /**
513  * Get detector calibrated at given time.
514  *
515  * \param t1_s UTC time [s]
516  * \return true if updated; else false
517  */
518  bool update(const double t1_s)
519  {
520  bool is_updated = false;
521 
522  if (!in_range(t1_s)) {
523 
525 
526  if (orientation.update(static_cast<JDetector&>(*this), t1_s)) { is_updated = true; }
527  if (position .update(static_cast<JDetector&>(*this), t1_s)) { is_updated = true; }
528 
529  range.join(orientation.getUTCTimeRange());
530  range.join(position .getUTCTimeRange());
531 
532  setUTCTimeRange(range);
533  }
534 
535  return is_updated;
536  }
537 
538 
539  /**
540  * Get detector calibrated at given time.
541  *
542  * \param chronometer chronometer
543  * \return true if updated; else false
544  */
545  bool update(const JDAQChronometer& chronometer)
546  {
547  return update(chronometer.getTimesliceStart().getUTCseconds());
548  }
549 
550 
551  /**
552  * Data structure for coverage of dynamic calibrations.
553  */
554  struct coverage_type {
555  double orientation; //!< coverage of detector by available orientation calibration [0,1]
556  double position; //!< coverage of detector by available position calibration [0,1]
557  };
558 
559 
560  /**
561  * Get coverage at given time.
562  *
563  * \param t1_s UTC time [s]
564  * \return coverage
565  */
566  coverage_type getCoverage(const double t1_s) const
567  {
568  return { orientation.getCoverage(*this, t1_s), position.getCoverage(*this, t1_s) };
569  }
570 
571 
572  /**
573  * Get coverage at given time.
574  *
575  * \param chronometer chronometer
576  * \return coverage
577  */
578  coverage_type getCoverage(const JDAQChronometer& chronometer) const
579  {
580  return getCoverage(chronometer.getTimesliceStart().getUTCseconds());
581  }
582 
583 
584  /**
585  * Get actual coverage.
586  *
587  * \return coverage
588  */
590  {
591  return getCoverage(0.5 * (getLowerLimit() + getUpperLimit()));
592  }
593 
594 
595  JOrientation orientation; //!< orientation calibration
596  JPosition position; //!< position calibration
597  };
598 }
599 
600 #endif
JTOOLS::JElement2D< double, JACOUSTICS::JMODEL::JString > element_type
Definition: JDynamics.hh:298
static const JRange< T, JComparator_t > DEFAULT_RANGE
Default range.
Definition: JRange.hh:555
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:360
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:578
coverage_type getCoverage(const double t1_s) const
Get coverage at given time.
Definition: JDynamics.hh:566
Dynamic orientation calibration.
Definition: JDynamics.hh:108
Dynamic position calibration.
Definition: JDynamics.hh:288
double position
coverage of detector by available position calibration [0,1]
Definition: JDynamics.hh:556
const JDetector & getDetector() const
Get actual detector.
Definition: JDynamics.hh:491
container_type::const_reverse_iterator const_reverse_iterator
Definition: JHashMap.hh:87
JDynamics(const JDetector &detector, const double Tmax_s)
Constructor.
Definition: JDynamics.hh:476
JTOOLS::JHashMap< int, function_type > data_type
Definition: JDynamics.hh:303
const_reverse_iterator rend() const
begin of reverse of calibration data
Definition: JDynamics.hh:190
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:596
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:359
JOrientation orientation
orientation calibration
Definition: JDynamics.hh:595
const_iterator begin() const
begin of calibration data
Definition: JDynamics.hh:187
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:306
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.
ROOT TTree parameter settings.
const JGeometry & getGeometry() const
Get detector geometry.
Definition: JDynamics.hh:407
number of points for interpolation
Definition: JDynamics.hh:112
const JQuaternion3D & getQuaternion() const
Get quaternion.
JACOUSTICS::JGeometry JGeometry
Definition: JDynamics.hh:296
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:555
void load(JObjectIterator_t &input)
Load calibration data.
Definition: JDynamics.hh:503
const_reverse_iterator rbegin() const
begin of reverse of calibration data
Definition: JDynamics.hh:189
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:305
Acoustic event fit.
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:333
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:358
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:315
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:589
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
virtual bool hasNext()=0
Check availability of next element.
JMODEL::JString getModel(const JFit &fit)
Get model parameters of string.
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:200
void load(JObjectIterator< JCOMPASS::JOrientation > &input)
Load calibration data.
Definition: JDynamics.hh:167
JUINT32_t getUTCseconds() const
Get 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:554
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:234
const_iterator end() const
end of calibration data
Definition: JDynamics.hh:188
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:518
Data structure for unit quaternion in three dimensions.
bool empty() const
empty
Definition: JDynamics.hh:355
bool update(const JDAQChronometer &chronometer)
Get detector calibrated at given time.
Definition: JDynamics.hh:545
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:356
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:357
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:420
2D Element.
Definition: JElement.hh:46
number of degrees for interpolation
Definition: JDynamics.hh:113
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:370
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:301
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