Jpp  18.2.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JKatoomba_t.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JKATOOMBA_T__
2 #define __JACOUSTICS__JKATOOMBA_T__
3 
4 #include <vector>
5 #include <memory>
6 #include <mutex>
7 
8 #include "TMatrixTSym.h"
9 #include "TDecompSVD.h"
10 
11 #include "JFit/JEstimator.hh"
12 #include "JFit/JRegressor.hh"
13 #include "JFit/JSimplex.hh"
14 #include "JFit/JMEstimator.hh"
15 #include "JMath/JMatrixNS.hh"
16 #include "JLang/JType.hh"
17 #include "JLang/JSharedPointer.hh"
18 #include "JLang/JException.hh"
19 #include "JMath/JMatrixNS.hh"
20 #include "Jeep/JMessage.hh"
21 
24 #include "JAcoustics/JEKey.hh"
25 #include "JAcoustics/JGeometry.hh"
26 #include "JAcoustics/JModel.hh"
27 #include "JAcoustics/JHit.hh"
29 
30 
31 /**
32  * \file
33  *
34  * Fit functions of acoustic model.
35  * \author mdejong
36  */
37 namespace JACOUSTICS {}
38 namespace JPP { using namespace JACOUSTICS; }
39 
40 namespace JACOUSTICS {
41 
42  using JLANG::JType;
45  using JMATH::JMath;
46  using JFIT::JEstimator;
48  using JFIT::JSimplex;
49  using JFIT::JMEstimator;
50 
51 
52  /**
53  * Get total weight of data points.
54  *
55  * \param __begin begin of data
56  * \param __end end of data
57  * \return total weight
58  */
59  template<class T>
60  inline double getWeight(T __begin, T __end)
61  {
62  double W = 0;
63 
64  for (T i = __begin; i != __end; ++i) {
65  W += i->getWeight();
66  }
67 
68  return W;
69  }
70 
71 
72  /**
73  * Auxiliary data structure for average.
74  */
75  struct JQuantile {
76  /**
77  * Default constructor.
78  */
80  {
81  reset();
82  }
83 
84 
85  /**
86  * Reset.
87  */
88  void reset()
89  {
90  total = 0.0;
91  count = 0;
92  }
93 
94 
95  /**
96  * Put value.
97  *
98  * \param x value
99  */
100  void put(const double x)
101  {
102  total += x;
103  count += 1;
104  }
105 
106 
107  /**
108  * Get mean value.
109  *
110  * \return mean value
111  */
112  long double getMean() const
113  {
114  if (count != 0.0)
115  return total / count;
116  else
117  THROW(JDivisionByZero, "JQuantile::getMean()");
118  }
119 
120 
121  /**
122  * Get mean value.
123  *
124  * \param value default value
125  * \return mean value
126  */
127  long double getMean(const double value) const
128  {
129  if (count != 0.0)
130  return getMean();
131  else
132  return value;
133  }
134 
135  private:
136  long double total;
137  long long int count;
138  };
139 
140 
141  /**
142  * Template definition of fit function of acoustic model.
143  */
144  template<template<class T> class JMinimiser_t = JType>
145  struct JKatoomba;
146 
147 
148  /**
149  * Auxiliary base class for fit function of acoustic model.
150  */
151  template<>
152  struct JKatoomba<JType> {
153 
154  typedef std::shared_ptr<JMEstimator> estimator_type;
155 
156 
157  /**
158  * Auxiliary data structure to sort transmissions.
159  */
160  static const struct compare {
161  /**
162  * Sort transmissions in following order.
163  * - ascending identifier;
164  * - descending quality;
165  * - ascending time-of-arrival;
166  *
167  * \param first first transmission
168  * \param second second transmission
169  * \return true if first transmission before second; else false
170  */
171  inline bool operator()(const JTransmission& first, const JTransmission& second) const
172  {
173  if (first.getID() == second.getID()) {
174 
175  if (first.getQ() == second.getQ())
176  return first.getToA() < second.getToA();
177  else
178  return first.getQ() > second.getQ();
179 
180  } else {
181 
182  return first.getID() < second.getID();
183  }
184  }
185  } compare;
186 
187 
188  /**
189  * Constructor
190  *
191  * \param geometry detector geometry
192  * \param velocity sound velocity
193  * \param option option
194  */
195  JKatoomba(const JGeometry& geometry,
196  const JSoundVelocity& velocity,
197  const int option) :
198  geometry(geometry),
199  velocity(velocity),
200  option (option)
201  {};
202 
203 
204  /**
205  * Get estimated time-of-arrival for given hit.
206  *
207  * \param model model
208  * \param hit hit
209  * \return time-of-arrival
210  */
211  double getToA(const JModel& model, const JHit& hit) const
212  {
213  const JGEOMETRY::JString& string = geometry[hit.getString()];
214  const JMODEL ::JString& parameters = model.string[hit.getString()];
215  const JPosition3D position = string.getPosition(parameters, hit.getFloor());
216 
217  const double D = hit.getDistance(position);
218  const double Vi = velocity.getInverseVelocity(D, hit.getZ(), position.getZ());
219 
220  return model.emission[hit.getEKey()].t1 + D * Vi;
221  }
222 
223 
224  /**
225  * Get estimated time-of-emission for given hit.
226  *
227  * \param model model
228  * \param hit hit
229  * \return time-of-arrival
230  */
231  double getToE(const JModel& model, const JHit& hit) const
232  {
233  const JGEOMETRY::JString& string = geometry[hit.getString()];
234  const JMODEL ::JString& parameters = model.string[hit.getString()];
235  const JPosition3D position = string.getPosition(parameters, hit.getFloor());
236 
237  const double D = hit.getDistance(position);
238  const double Vi = velocity.getInverseVelocity(D, hit.getZ(), position.getZ());
239 
240  return hit.getValue() - D * Vi;
241  }
242 
243 
244  const JGeometry& geometry; //!< detector geometry
245  JSoundVelocity velocity; //!< sound velocity
246  int option; //!< fit option
247  estimator_type estimator; //!< M-Estimator function
248 
249  protected:
250  /**
251  * H-equation as per hit.
252  */
253  struct H_t :
254  public JMODEL::JEmission,
255  public JMODEL::JString,
256  public JMath<H_t>
257  {
258  /**
259  * Default constructor.
260  */
261  H_t() :
262  JMODEL::JEmission(),
263  JMODEL::JString ()
264  {}
265 
266 
267  /**
268  * Constructor.
269  *
270  * \param emission emission
271  * \param string string
272  */
273  H_t(const JMODEL::JEmission& emission,
274  const JMODEL::JString& string) :
275  JMODEL::JEmission(emission),
276  JMODEL::JString (string)
277  {}
278 
279 
280  /**
281  * Scale H-equation.
282  *
283  * \param factor multiplication factor
284  * \return this H-equation
285  */
286  H_t& mul(const double factor)
287  {
288  static_cast<JMODEL::JEmission&>(*this).mul(factor);
289  static_cast<JMODEL::JString&> (*this).mul(factor);
290 
291  return *this;
292  }
293  };
294 
295 
296  /**
297  * Indices of H-equation in global model.
298  */
299  struct I_t
300  {
301  /**
302  * Default constructor.
303  */
304  I_t() :
305  t1 (0),
306  tx (0),
307  ty (0),
308  tx2(0),
309  ty2(0),
310  vs (0)
311  {}
312 
313 
314  int t1;
315  int tx;
316  int ty;
317  int tx2;
318  int ty2;
319  int vs;
320  };
321  };
322 
323 
324  /**
325  * Template specialisation of fit function of acoustic model based on JAbstractMinimiser minimiser.\n
326  * This class can be used to evaluate the chi2.
327  */
328  template<>
330  public JAbstractMinimiser<JModel>,
331  public JKatoomba<>
332  {
333  typedef double result_type;
334 
335 
336  /**
337  * Constructor
338  *
339  * \param geometry detector geometry
340  * \param velocity sound velocity
341  * \param option option
342  */
343  JKatoomba(const JGeometry& geometry,
344  const JSoundVelocity& velocity,
345  const int option) :
346  JKatoomba<>(geometry, velocity, option)
347  {};
348 
349 
350  /**
351  * Fit function.\n
352  * This method is used to determine the chi2 of given hit with respect to actual model.
353  *
354  * \param model model
355  * \param hit hit
356  * \return chi2
357  */
358  result_type operator()(const JModel& model, const JHit& hit) const
359  {
360  const double toa_s = this->getToA(model, hit);
361  const double u = (toa_s - hit.getValue()) / hit.getSigma();
362 
363  return estimator->getRho(u) * hit.getWeight();
364  }
365 
366 
367  /**
368  * Fit.
369  *
370  * \param __begin begin of hits
371  * \param __end end of hits
372  * \return chi2
373  */
374  template<class T>
375  result_type operator()(T __begin, T __end)
376  {
377  this->value.setOption(this->option);
378 
379  return JAbstractMinimiser<JModel>::operator()(*this, __begin, __end);
380  }
381 
382 
383  /**
384  * Fit.
385  *
386  * \param model model
387  * \param __begin begin of hits
388  * \param __end end of hits
389  * \return chi2
390  */
391  template<class T>
392  result_type operator()(const JModel& model, T __begin, T __end)
393  {
394  this->value = model;
395 
396  return (*this)(__begin, __end);
397  }
398  };
399 
400 
401  /**
402  * Algorithm for matrix inversion.
403  */
404  enum JMatrix_t {
405  SVD_t = 1, //!< SVD
406  LDU_t = 2 //!< LDU
407  };
408 
409 
410  /**
411  * Auxiliary data structure for compatibility of symmetric matrix.
412  * Inversion is based on SVD algorithm.
413  */
414  struct TMatrixDS :
415  public TMatrixD
416  {
417  static constexpr double TOLERANCE = 1.0e-8; //!< Tolerance for SVD
418 
419  /**
420  * Resize matrix.
421  */
422  void resize(const size_t size)
423  {
424  ResizeTo(size, size);
425  }
426 
427  /**
428  * Set matrix to the null matrix.
429  */
430  void reset()
431  {
432  Zero();
433  }
434 
435  /**
436  * Invert matrix according SVD decomposition.
437  */
438  void invert()
439  {
440 #ifdef THREAD_SAFE
441  using namespace std;
442 
443  unique_lock<mutex> lock(mtx);
444 #endif
445  TDecompSVD svd(*this, TOLERANCE);
446 
447  Bool_t status;
448 
449  static_cast<TMatrixD&>(*this) = svd.Invert(status);
450  }
451 
452  /**
453  * Get solution of equation <tt>A x = b</tt>.
454  *
455  * \param u column vector; b on input, x on output(I/O)
456  */
457  template<class JVectorND_t>
458  void solve(JVectorND_t& u)
459  {
460 #ifdef THREAD_SAFE
461  using namespace std;
462 
463  unique_lock<mutex> lock(mtx);
464 #endif
465  TDecompSVD svd(*this, TOLERANCE);
466 
467  TVectorD b(u.size());
468 
469  for (size_t i = 0; i != u.size(); ++i) {
470  b[i] = u[i];
471  }
472 
473  svd.Solve(b);
474 
475  for (size_t i = 0; i != u.size(); ++i) {
476  u[i] = b[i];
477  }
478  }
479 
480  static std::mutex mtx; //!< TDecompSVD
481  };
482 
483 
484  /**
485  * Using declaration for compatibility of symmetric matrix.
486  * Inversion is based on LDU algorithm.
487  */
488  using JMATH::JMatrixNS;
489 
490 
491  /**
492  * Template specialisation of fit function of acoustic model based on linear approximation.
493  */
494  template<>
496  public JKatoomba<>
497  {
498  /**
499  * Constructor
500  *
501  * \param geometry detector geometry
502  * \param velocity sound velocity
503  * \param option option
504  */
505  JKatoomba(const JGeometry& geometry,
506  const JSoundVelocity& velocity,
507  const int option) :
508  JKatoomba<>(geometry, velocity, option)
509  {};
510 
511 
512  /**
513  * Get start values of string parameters.\n
514  * Note that this method may throw an exception.
515  *
516  * \param __begin begin of hits
517  * \param __end end of hits
518  * \return model
519  */
520  template<class T>
521  JModel& operator()(T __begin, T __end) const
522  {
523  switch (MATRIX_INVERSION) {
524 
525  case SVD_t:
526  return this->evaluate(__begin, __end, svd);
527 
528  case LDU_t:
529  return this->evaluate(__begin, __end, ldu);
530 
531  default:
532  THROW(JValueOutOfRange, "Invalid matrix type " << MATRIX_INVERSION);
533  }
534  }
535 
536 
537  static JMatrix_t MATRIX_INVERSION; // matrix inversion
538 
539  private:
540  mutable JMatrixNS ldu;
541  mutable TMatrixDS svd;
542 
544 
545  /**
546  * Get start values of string parameters.\n
547  * Note that this method may throw an exception.
548  *
549  * \param __begin begin of hits
550  * \param __end end of hits
551  * \param V matrix
552  * \return model
553  */
554  template<class T, class JMatrixNS_t>
555  JModel& evaluate(T __begin, T __end, JMatrixNS_t& V) const
556  {
557  using namespace std;
558  using namespace JPP;
559  using namespace JGEOMETRY;
560 
561  value = JModel(__begin, __end); // set up global model according data
562 
563  if (this->option == JMODEL::FIT_EMITTERS_ONLY_t)
564  value.setOption(JMODEL::FIT_EMITTERS_ONLY_t);
565  else
567 
568  H_t H; // H-equation as per hit
569  I_t i; // indices of H-equation in global model
570 
571  const size_t N = value.getN();
572 
573  V.resize(N);
574  Y.resize(N);
575 
576  V.reset();
577 
578  for (vector<double>::iterator i = Y.begin(); i != Y.end(); ++i) {
579  *i = 0.0;
580  }
581 
582  for (T hit = __begin; hit != __end; ++hit) {
583 
584  const JString& string = geometry[hit->getString()];
585  const JPosition3D position = string.getPosition(hit->getFloor());
586  const double Vi = velocity.getInverseVelocity(hit->getDistance(position), hit->getZ(), position.getZ());
587 
588  const double h1 = string.getHeight(hit->getFloor());
589  const JPosition3D p1 = string.getPosition() - hit->getPosition();
590  const double ds = sqrt(p1.getLengthSquared() + h1*h1 + 2.0*p1.getZ()*h1);
591  const double y = hit->getValue() - Vi*ds;
592  const double W = sqrt(hit->getWeight());
593 
594  H.t1 = W * 1.0;
595  H.tx = W * Vi * p1.getX() * h1 / ds;
596  H.ty = W * Vi * p1.getY() * h1 / ds;
597 
598  i.t1 = value.getIndex(hit->getEKey(), &H_t::t1);
599  i.tx = value.getIndex(hit->getString(), &H_t::tx);
600  i.ty = value.getIndex(hit->getString(), &H_t::ty);
601 
602  V(i.t1, i.t1) += H.t1 * H.t1;
603 
604  Y[i.t1] += W * H.t1 * y;
605 
606  if (hit->getFloor() != 0) {
607 
608  if (this->option != JMODEL::FIT_EMITTERS_ONLY_t) {
609 
610  V(i.t1, i.tx) += H.t1 * H.tx; V(i.t1, i.ty) += H.t1 * H.ty;
611  V(i.tx, i.t1) += H.tx * H.t1; V(i.ty, i.t1) += H.ty * H.t1;
612 
613  V(i.tx, i.tx) += H.tx * H.tx; V(i.tx, i.ty) += H.tx * H.ty;
614  V(i.ty, i.tx) += H.ty * H.tx; V(i.ty, i.ty) += H.ty * H.ty;
615 
616  Y[i.tx] += W * H.tx * y;
617  Y[i.ty] += W * H.ty * y;
618  }
619  }
620  }
621 
622  // solve A x = b
623 
624  V.solve(Y);
625 
626  for (size_t row = 0; row != N; ++row) {
627  value[row] += Y[row];
628  }
629 
630  return value;
631  }
632 
633 
634  mutable JModel value;
635  };
636 
637 
638  /**
639  * Template specialisation of fit function of acoustic model based on JSimplex minimiser.
640  */
641  template<>
642  struct JKatoomba<JSimplex> :
643  public JSimplex<JModel>,
644  public JKatoomba<>
645  {
646  /**
647  * Constructor
648  *
649  * \param geometry detector geometry
650  * \param velocity sound velocity
651  * \param option option
652  */
653  JKatoomba(const JGeometry& geometry,
654  const JSoundVelocity& velocity,
655  const int option) :
656  JKatoomba<>(geometry, velocity, option)
657  {};
658 
659 
660  /**
661  * Fit function.\n
662  * This method is used to determine the chi2 of given hit with respect to actual model.
663  *
664  * \param model model
665  * \param hit hit
666  * \return chi2
667  */
668  double operator()(const JModel& model, const JHit& hit) const
669  {
670  const double toa_s = this->getToA(model, hit);
671  const double u = (toa_s - hit.getValue()) / hit.getSigma();
672 
673  return estimator->getRho(u);
674  }
675 
676 
677  /**
678  * Fit.
679  *
680  * \param __begin begin of hits
681  * \param __end end of hits
682  * \return chi2
683  */
684  template<class T>
685  double operator()(T __begin, T __end)
686  {
687  this->step.clear();
688 
689  for (JModel::string_type::const_iterator i = this->value.string.begin(); i != this->value.string.end(); ++i) {
690 
691  JModel model;
692 
693  switch (this->option) {
694 
696  model.string[i->first] = JMODEL::JString(0.0, 0.0, 0.0, 0.0, 1.0e-6); this->step.push_back(model);
697 
699  model.string[i->first] = JMODEL::JString(0.0, 0.0, 1.0e-6, 0.0, 0.0); this->step.push_back(model);
700  model.string[i->first] = JMODEL::JString(0.0, 0.0, 0.0, 1.0e-6, 0.0); this->step.push_back(model);
701 
703  model.string[i->first] = JMODEL::JString(1.0e-3, 0.0, 0.0, 0.0, 0.0); this->step.push_back(model);
704  model.string[i->first] = JMODEL::JString(0.0, 1.0e-3, 0.0, 0.0, 0.0); this->step.push_back(model);
705  break;
706 
707  default:
708  break;
709  }
710  }
711 
712  for (JModel::emission_type::const_iterator i = this->value.emission.begin(); i != this->value.emission.end(); ++i) {
713 
714  JModel model;
715 
716  model.emission[i->first] = JMODEL::JEmission(5.0e-5); this->step.push_back(model);
717  }
718 
719  return JSimplex<JModel>::operator()(*this, __begin, __end);
720  }
721  };
722 
723 
724  /**
725  * Place holder for custom implementation.
726  */
727  template<class T>
728  class JGandalf {};
729 
730 
731  /**
732  * Template specialisation of fit function of acoustic model based on JGandalf minimiser.
733  */
734  template<>
735  struct JKatoomba<JGandalf> :
736  public JKatoomba<>
737  {
738  typedef double result_type;
739 
740  /**
741  * Type definition internal data structure.
742  */
744 
745 
746  /**
747  * Constructor
748  *
749  * \param geometry detector geometry
750  * \param velocity sound velocity
751  * \param option option
752  */
753  JKatoomba(const JGeometry& geometry,
754  const JSoundVelocity& velocity,
755  const int option) :
756  JKatoomba<>(geometry, velocity, option)
757  {};
758 
759 
760  /**
761  * Fit.
762  *
763  * \param __begin begin of hits
764  * \param __end end of hits
765  * \return chi2 and gradient
766  */
767  template<class T>
768  result_type operator()(T __begin, T __end)
769  {
770  using namespace std;
771  using namespace JPP;
772 
773  value.setOption(this->option);
774 
775  const int N = value.getN();
776 
777  V.resize(N);
778  Y.resize(N);
779  h.resize(N);
780 
781  data_type data;
782 
783  for (T hit = __begin; hit != __end; ++hit) {
784  data[hit->getLocation()][hit->getEmitter()].push_back(*hit);
785  }
786 
787  lambda = LAMBDA_MIN;
788 
789  result_type precessor = numeric_limits<double>::max();
790 
791  for (numberOfIterations = 0; numberOfIterations != MAXIMUM_ITERATIONS; ++numberOfIterations) {
792 
793  DEBUG("step: " << numberOfIterations << endl);
794 
795  evaluate(data);
796 
797  DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
798  DEBUG("chi2: " << FIXED(12,5) << successor << endl);
799 
800  if (successor < precessor) {
801 
802  if (numberOfIterations != 0) {
803 
804  if (fabs(precessor - successor) < EPSILON*fabs(precessor)) {
805  return successor;
806  }
807 
808  if (lambda > LAMBDA_MIN) {
809  lambda /= LAMBDA_DOWN;
810  }
811  }
812 
813  precessor = successor;
814  previous = value;
815 
816  } else {
817 
818  value = previous;
819  lambda *= LAMBDA_UP;
820 
821  if (lambda > LAMBDA_MAX) {
822  return precessor; // no improvement found
823  }
824 
825  evaluate(data);
826  }
827 
828  // force definite positiveness
829 
830  for (int i = 0; i != N; ++i) {
831 
832  if (V(i,i) < PIVOT) {
833  V(i,i) = PIVOT;
834  }
835 
836  h[i] = 1.0 / sqrt(V(i,i));
837  }
838 
839  // normalisation
840 
841  for (int i = 0; i != N; ++i) {
842  for (int j = 0; j != i; ++j) {
843  V(j,i) *= h[i] * h[j];
844  V(i,j) = V(j,i);
845  }
846  }
847 
848  for (int i = 0; i != N; ++i) {
849  V(i,i) = 1.0 + lambda;
850  }
851 
852 
853  // solve A x = b
854 
855  for (int col = 0; col != N; ++col) {
856  Y[col] *= h[col];
857  }
858 
859  try {
860  V.solve(Y);
861  }
862  catch (const exception& error) {
863 
864  ERROR("JGandalf: " << error.what() << endl << V << endl);
865 
866  break;
867  }
868 
869  // update value
870 
871  for (int row = 0; row != N; ++row) {
872 
873  DEBUG("u[" << noshowpos << setw(3) << row << "] = " << showpos << FIXED(20,5) << value[row]);
874 
875  value[row] -= h[row] * Y[row];
876 
877  DEBUG(" -> " << FIXED(20,10) << value[row] << noshowpos << endl);
878  }
879  }
880 
881  return precessor;
882  }
883 
884  static int debug; //!< debug level
885  static int MAXIMUM_ITERATIONS; //!< maximal number of iterations
886  static double EPSILON; //!< maximal distance to minimum
887  static double LAMBDA_MIN; //!< minimal value control parameter
888  static double LAMBDA_MAX; //!< maximal value control parameter
889  static double LAMBDA_UP; //!< multiplication factor control parameter
890  static double LAMBDA_DOWN; //!< multiplication factor control parameter
891  static double PIVOT; //!< minimal value diagonal element of matrix
892 
893  double lambda;
897 
898  private:
899  /**
900  * Evaluation of fit.
901  *
902  * \param data data
903  */
904  inline void evaluate(const data_type& data)
905  {
906  using namespace std;
907  using namespace JPP;
908 
909  successor = 0.0;
910 
911  V.reset();
912 
913  for (std::vector<double>::iterator i = Y.begin(); i != Y.end(); ++i) {
914  *i = 0.0;
915  }
916 
917  for (data_type::const_iterator p = data.begin(); p != data.end(); ++p) {
918 
919  const JGEOMETRY::JString& string = geometry [p->first.getString()];
920  const JMODEL ::JString& parameters = value.string[p->first.getString()];
921  const JPosition3D position = string.getPosition(parameters, p->first.getFloor());
922 
923  for (data_type::mapped_type::const_iterator emitter = p->second.begin(); emitter != p->second.end(); ++emitter) {
924 
925  const double D = emitter->first.getDistance(position);
926  const double Vi = velocity.getInverseVelocity(D, emitter->first.getZ(), position.getZ());
927 
928  const H_t H0(1.0, string.getGradient(parameters, emitter->first.getPosition(), p->first.getFloor()) * Vi);
929 
930  for (data_type::mapped_type::mapped_type::const_iterator hit = emitter->second.begin(); hit != emitter->second.end(); ++hit) {
931 
932  const double toa_s = value.emission[hit->getEKey()].t1 + D * Vi;
933 
934  const double u = (toa_s - hit->getValue()) / hit->getSigma();
935  const double W = sqrt(hit->getWeight());
936 
937  successor += (W*W) * estimator->getRho(u);
938 
939  const H_t H = H0 * (W * estimator->getPsi(u) / hit->getSigma());
940 
941  I_t i;
942 
943  i.t1 = value.getIndex(hit->getEKey(), &H_t::t1);
944  i.tx = value.getIndex(hit->getString(), &H_t::tx);
945  i.ty = value.getIndex(hit->getString(), &H_t::ty);
946  i.tx2 = value.getIndex(hit->getString(), &H_t::tx2);
947  i.ty2 = value.getIndex(hit->getString(), &H_t::ty2);
948  i.vs = value.getIndex(hit->getString(), &H_t::vs);
949 
950  V(i.t1, i.t1) += H.t1 * H.t1;
951 
952  Y[i.t1] += W * H.t1;
953 
954  if (hit->getFloor() != 0) {
955 
956  switch (this->option) {
957 
959  V(i.t1, i.vs) += H.t1 * H.vs; V(i.tx, i.vs) += H.tx * H.vs; V(i.ty, i.vs) += H.ty * H.vs; V(i.tx2, i.vs) += H.tx2 * H.vs; V(i.ty2, i.vs) += H.ty2 * H.vs;
960 
961  V(i.vs, i.t1) = V(i.t1, i.vs);
962  V(i.vs, i.tx) = V(i.tx, i.vs);
963  V(i.vs, i.ty) = V(i.ty, i.vs);
964  V(i.vs, i.tx2) = V(i.tx2, i.vs);
965  V(i.vs, i.ty2) = V(i.ty2, i.vs);
966 
967  V(i.vs, i.vs) += H.vs * H.vs;
968 
969  Y[i.vs] += W * H.vs;
970 
972  V(i.t1, i.tx2) += H.t1 * H.tx2; V(i.tx, i.tx2) += H.tx * H.tx2; V(i.ty, i.tx2) += H.ty * H.tx2;
973 
974  V(i.tx2, i.t1) = V(i.t1, i.tx2);
975  V(i.tx2, i.tx) = V(i.tx, i.tx2);
976  V(i.tx2, i.ty) = V(i.ty, i.tx2);
977 
978  V(i.t1, i.ty2) += H.t1 * H.ty2; V(i.tx, i.ty2) += H.tx * H.ty2; V(i.ty, i.ty2) += H.ty * H.ty2;
979 
980  V(i.ty2, i.t1) = V(i.t1, i.ty2);
981  V(i.ty2, i.tx) = V(i.tx, i.ty2);
982  V(i.ty2, i.ty) = V(i.ty, i.ty2);
983 
984  V(i.tx2, i.tx2) += H.tx2 * H.tx2; V(i.tx2, i.ty2) += H.tx2 * H.ty2;
985  V(i.ty2, i.tx2) = V(i.tx2, i.ty2); V(i.ty2, i.ty2) += H.ty2 * H.ty2;
986 
987  Y[i.tx2] += W * H.tx2;
988  Y[i.ty2] += W * H.ty2;
989 
991  V(i.t1, i.tx) += H.t1 * H.tx; V(i.t1, i.ty) += H.t1 * H.ty;
992  V(i.tx, i.t1) = V(i.t1, i.tx); V(i.ty, i.t1) = V(i.t1, i.ty);
993 
994  V(i.tx, i.tx) += H.tx * H.tx; V(i.tx, i.ty) += H.tx * H.ty;
995  V(i.ty, i.tx) = V(i.tx, i.ty); V(i.ty, i.ty) += H.ty * H.ty;
996 
997  Y[i.tx] += W * H.tx;
998  Y[i.ty] += W * H.ty;
999  break;
1000 
1001  default:
1002  break;
1003  }
1004  }
1005  }
1006  }
1007  }
1008  }
1009 
1010 
1011  std::vector<double> Y; // gradient
1014  std::vector<double> h; // normalisation vector
1015  };
1016 }
1017 
1018 #endif
Linear fit methods.
Acoustic hit.
General purpose data regression method.
Exceptions.
JString & mul(const double factor)
Scale string.
void solve(JVectorND_t &u)
Get solution of equation A x = b.
Definition: JKatoomba_t.hh:458
Wrapper class around STL string class.
Definition: JString.hh:27
void resize(const size_t size)
Resize matrix.
Definition: JKatoomba_t.hh:422
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:109
JModel & evaluate(T __begin, T __end, JMatrixNS_t &V) const
Get start values of string parameters.
Definition: JKatoomba_t.hh:555
double getQ() const
Get quality.
TPaveText * p1
void reset()
Reset.
Definition: JKatoomba_t.hh:88
double getWeight(T __begin, T __end)
Get total weight of data points.
Definition: JKatoomba_t.hh:60
Sound velocity.
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Template definition of linear fit.
Definition: JEstimator.hh:25
static double LAMBDA_UP
multiplication factor control parameter
Definition: JKatoomba_t.hh:889
long long int count
Definition: JKatoomba_t.hh:137
Interface for maximum likelihood estimator (M-estimator).
Definition: JMEstimator.hh:20
double getWeight() const
Get weight.
const JGeometry & geometry
detector geometry
Definition: JKatoomba_t.hh:244
bool operator()(const JTransmission &first, const JTransmission &second) const
Sort transmissions in following order.
Definition: JKatoomba_t.hh:171
result_type operator()(const JFunction_t &fit, T __begin, T __end)
Get chi2.
Definition: JRegressor.hh:46
static std::mutex mtx
TDecompSVD.
Definition: JKatoomba_t.hh:480
fit times of emission of emitters and tilt angles of strings with second order correction ...
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Acoustic geometries.
static const double H
Planck constant [eV s].
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
static constexpr double TOLERANCE
Tolerance for SVD.
Definition: JKatoomba_t.hh:417
H_t & mul(const double factor)
Scale H-equation.
Definition: JKatoomba_t.hh:286
JEKey getEKey() const
Get emitter hash key of this hit.
double getValue() const
Get expectation value of time-of-arrival.
Auxiliary class for a type holder.
Definition: JType.hh:19
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
Abstract minimiser.
Definition: JRegressor.hh:25
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:270
JQuantile()
Default constructor.
Definition: JKatoomba_t.hh:79
Acoustics hit.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Place holder for custom implementation.
Definition: JKatoomba_t.hh:728
Acoustic fit parameters.
Model for fit to acoustics data.
JKatoomba(const JGeometry &geometry, const JSoundVelocity &velocity, const int option)
Constructor.
Definition: JKatoomba_t.hh:195
Acoustic transmission.
JMatrix_t
Algorithm for matrix inversion.
Definition: JKatoomba_t.hh:404
static double EPSILON
maximal distance to minimum
Definition: JKatoomba_t.hh:886
double operator()(const JModel &model, const JHit &hit) const
Fit function.
Definition: JKatoomba_t.hh:668
result_type operator()(T __begin, T __end)
Fit.
Definition: JKatoomba_t.hh:768
std::vector< double > vs
JKatoomba(const JGeometry &geometry, const JSoundVelocity &velocity, const int option)
Constructor.
Definition: JKatoomba_t.hh:653
fit times of emission of emitters and tilt angles of strings
static double LAMBDA_MIN
minimal value control parameter
Definition: JKatoomba_t.hh:887
long double getMean(const double value) const
Get mean value.
Definition: JKatoomba_t.hh:127
static int debug
debug level
Definition: JKatoomba_t.hh:884
do set_variable OUTPUT_DIRECTORY $WORKDIR T
double operator()(T __begin, T __end)
Fit.
Definition: JKatoomba_t.hh:685
result_type operator()(T __begin, T __end)
Fit.
Definition: JKatoomba_t.hh:375
JACOUSTICS::JModel::emission_type emission
#define ERROR(A)
Definition: JMessage.hh:66
fit only times of emission of emitters
JKatoomba(const JGeometry &geometry, const JSoundVelocity &velocity, const int option)
Constructor.
Definition: JKatoomba_t.hh:753
double getY() const
Get y position.
Definition: JVector3D.hh:104
std::map< JLocation, std::map< JEmitter, std::vector< JHit > > > data_type
Type definition internal data structure.
Definition: JKatoomba_t.hh:743
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
N x N symmetric matrix.
Definition: JMatrixNS.hh:28
double getLengthSquared() const
Get length squared.
Definition: JVector3D.hh:235
General purpose messaging.
Implementation for depth dependend velocity of sound.
static double LAMBDA_MAX
maximal value control parameter
Definition: JKatoomba_t.hh:888
static int MAXIMUM_ITERATIONS
maximal number of iterations
Definition: JKatoomba_t.hh:885
static JMatrix_t MATRIX_INVERSION
Matrix inversion.
Definition: JKatoomba_t.hh:537
void invert()
Invert matrix according SVD decomposition.
Definition: JKatoomba_t.hh:438
JEmission & mul(const double factor)
Scale emission.
double operator()(const JFunction_t &fit, T __begin, T __end)
Multi-dimensional fit.
Definition: JSimplex.hh:71
Emitter hash key.
Auxiliary data structure for compatibility of symmetric matrix.
Definition: JKatoomba_t.hh:414
double getToE(const JModel &model, const JHit &hit) const
Get estimated time-of-emission for given hit.
Definition: JKatoomba_t.hh:231
JACOUSTICS::JModel::string_type string
JSoundVelocity velocity
sound velocity
Definition: JKatoomba_t.hh:245
JModel & operator()(T __begin, T __end) const
Get start values of string parameters.
Definition: JKatoomba_t.hh:521
std::shared_ptr< JMEstimator > estimator_type
Definition: JKatoomba_t.hh:154
H_t(const JMODEL::JEmission &emission, const JMODEL::JString &string)
Constructor.
Definition: JKatoomba_t.hh:273
int getString() const
Get string number.
Definition: JLocation.hh:134
Auxiliary data structure for average.
Definition: JKatoomba_t.hh:75
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
static double LAMBDA_DOWN
multiplication factor control parameter
Definition: JKatoomba_t.hh:890
Exception for division by zero.
Definition: JException.hh:286
result_type operator()(const JModel &model, const JHit &hit) const
Fit function.
Definition: JKatoomba_t.hh:358
Simple fit method based on Powell&#39;s algorithm, see reference: Numerical Recipes in C++...
Definition: JSimplex.hh:42
result_type operator()(const JModel &model, T __begin, T __end)
Fit.
Definition: JKatoomba_t.hh:392
double getToA() const
Get calibrated time of arrival.
void put(const double x)
Put value.
Definition: JKatoomba_t.hh:100
Acoustic transmission.
double getToA(const JModel &model, const JHit &hit) const
Get estimated time-of-arrival for given hit.
Definition: JKatoomba_t.hh:211
static double PIVOT
minimal value diagonal element of matrix
Definition: JKatoomba_t.hh:891
int getID() const
Get identifier.
double getX() const
Get x position.
Definition: JVector3D.hh:94
fit times of emission of emitters and tilt angles of strings with second order correction and stretch...
int j
Definition: JPolint.hh:703
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:178
container_type::const_iterator const_iterator
Definition: JHashMap.hh:86
long double getMean() const
Get mean value.
Definition: JKatoomba_t.hh:112
double u[N+1]
Definition: JPolint.hh:776
JKatoomba(const JGeometry &geometry, const JSoundVelocity &velocity, const int option)
Constructor.
Definition: JKatoomba_t.hh:343
Model for fit to acoutsics data.
estimator_type estimator
M-Estimator function.
Definition: JKatoomba_t.hh:247
void evaluate(const data_type &data)
Evaluation of fit.
Definition: JKatoomba_t.hh:904
void reset()
Set matrix to the null matrix.
Definition: JKatoomba_t.hh:430
do echo Generating $dir eval D
Definition: JDrawLED.sh:53
double getZ() const
Get z position.
Definition: JVector3D.hh:115
JKatoomba(const JGeometry &geometry, const JSoundVelocity &velocity, const int option)
Constructor.
Definition: JKatoomba_t.hh:505
Maximum likelihood estimator (M-estimators).
Template definition of fit function of acoustic model.
Definition: JKatoomba_t.hh:145
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
double getSigma() const
Get resolution of time-of-arrival.