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