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