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