Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JKatoomba.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JKATOOMBA__
2 #define __JACOUSTICS__JKATOOMBA__
3 
4 #include <vector>
5 
6 #include "JFit/JEstimator.hh"
7 #include "JFit/JRegressor.hh"
8 #include "JFit/JMEstimator.hh"
9 #include "JMath/JMatrixNS.hh"
10 #include "JLang/JSharedPointer.hh"
11 
13 #include "JAcoustics/JEKey.hh"
14 #include "JAcoustics/JGeometry.hh"
15 #include "JAcoustics/JModel.hh"
16 #include "JAcoustics/JHit.hh"
17 
18 
19 /**
20  * \file
21  *
22  * Fit functions of acoustic model.
23  * \author mdejong
24  */
25 namespace JACOUSTICS {}
26 namespace JPP { using namespace JACOUSTICS; }
27 
28 namespace JACOUSTICS {
29 
30  using JFIT::JEstimator;
32  using JFIT::JMEstimator;
33 
34 
35  /**
36  * Auxiliary base class for fit function of acoustic model.
37  */
38  struct JKatoomba_t :
39  public JGeometry
40  {
41  /**
42  * Constructor
43  *
44  * \param detector detector
45  * \param velocity sound velocity
46  */
48  const JSoundVelocity& velocity) :
49  detector(detector),
50  velocity(velocity)
51  {};
52 
53 
54  /**
55  * Get estimated time-of-arrival for given hit.
56  *
57  * \param model model
58  * \param hit hit
59  * \return time-of-arrival
60  */
61  template<class JPDF_t>
62  double getToA(const JModel& model, const JHit<JPDF_t>& hit) const
63  {
64  const JString& string = detector[hit.getString()];
65  const JModel::JString& parameters = model.string[hit.getString()];
66  const JPosition3D position = string.getPosition(parameters, hit.getFloor());
67 
68  const double D = hit.getDistance(position);
69  const double Vi = velocity.getInverseVelocity(D, hit.getZ(), position.getZ());
70 
71  return model.emitter[hit.getEKey()].t1 + D * Vi;
72  }
73 
74 
77 
78  JLANG::JSharedPointer<JMEstimator> estimator; //!< M-Estimator function
79  };
80 
81 
82  /**
83  * Template definition of fit function of acoustic model.
84  */
85  template<template<class T> class JMinimiser_t>
86  struct JKatoomba;
87 
88 
89  /**
90  * Template specialisation of fit function of acoustic model based on JAbstractMinimiser minimiser.\n
91  * This class can be used to evaluate the chi2.
92  */
93  template<>
95  public JAbstractMinimiser<JModel>,
96  public JKatoomba_t
97  {
98  typedef double result_type;
99 
100 
101  /**
102  * Constructor
103  *
104  * \param detector detector
105  * \param velocity sound velocity
106  */
108  const JSoundVelocity& velocity) :
109  JKatoomba_t(detector, velocity)
110  {};
111 
112 
113  /**
114  * Fit function.\n
115  * This method is used to determine the chi2 of given hit with respect to actual model.
116  *
117  * \param model model
118  * \param hit hit
119  * \return chi2 and gradient
120  */
121  template<class JPDF_t>
122  result_type operator()(const JModel& model, const JHit<JPDF_t>& hit) const
123  {
124  const double toa_s = this->getToA(model, hit);
125  const double u = (toa_s - hit.getValue()) / hit.sigma;
126 
127  return estimator->getRho(u);
128  }
129 
130 
131  /**
132  * Fit.
133  *
134  * \param model model
135  * \param __begin begin of hits
136  * \param __end end of hits
137  * \return chi2
138  */
139  template<class T>
140  result_type operator()(const JModel& model, T __begin, T __end)
141  {
142  this->value = model;
143 
144  return JAbstractMinimiser<JModel>::operator()(*this, __begin, __end);
145  }
146  };
147 
148 
149  /**
150  * Template specialisation of fit function of acoustic model based on linear approximation.
151  */
152  template<>
154  public JKatoomba_t
155  {
156  /**
157  * Constructor
158  *
159  * \param detector detector
160  * \param velocity sound velocity
161  */
163  const JSoundVelocity& velocity) :
164  JKatoomba_t(detector, velocity)
165  {};
166 
167 
168  /**
169  * Get start values of string parameters.\n
170  * Note that this method may throw an exception.
171  *
172  * \param __begin begin of hits
173  * \param __end end of hits
174  * \return model
175  */
176  template<class T>
177  const JModel& operator()(T __begin, T __end) const
178  {
179  using namespace std;
180  using namespace JPP;
181 
182  value = JModel(__begin, __end); // set up global model according data
183 
184  struct H_t : // H-equation as per hit
185  public JModel::JString,
186  public JModel::JEmitter
187  {} H;
188 
189  struct { int t1; int tx; int ty; } i; // indices of H-equation in global model
190 
191  JMatrixNS V(value.getN()); // V = H^T x H
192  vector<double> Y(value.getN(), 0.0); // Y = H^T x y
193 
194 
195  for (T hit = __begin; hit != __end; ++hit) {
196 
197  const JString& string = detector[hit->getString()];
198  const JPosition3D position = string.getPosition(hit->getFloor());
199  const double Vi = velocity.getInverseVelocity(hit->getDistance(position), hit->getZ(), position.getZ());
200 
201  const double h1 = string[hit->getFloor()].getHeight();
202  const JPosition3D p1 = string.getPosition() - hit->getPosition();
203  const double ds = sqrt(p1.getLengthSquared() + h1*h1 + 2.0*p1.getZ()*h1);
204  const double y = hit->getValue() - Vi*ds;
205 
206  H.t1 = 1.0;
207  H.tx = Vi * p1.getX() * h1 / ds;
208  H.ty = Vi * p1.getY() * h1 / ds;
209 
210  i.t1 = value.getIndex(hit->getEKey(), &H_t::t1);
211  i.tx = value.getIndex(hit->getString(), &H_t::tx);
212  i.ty = value.getIndex(hit->getString(), &H_t::ty);
213 
214  V(i.t1, i.t1) += H.t1 * H.t1;
215  V(i.t1, i.tx) += H.t1 * H.tx;
216  V(i.t1, i.ty) += H.t1 * H.ty;
217 
218  V(i.tx, i.t1) += H.tx * H.t1;
219  V(i.ty, i.t1) += H.ty * H.t1;
220 
221  V(i.tx, i.tx) += H.tx * H.tx;
222  V(i.tx, i.ty) += H.tx * H.ty;
223 
224  V(i.ty, i.tx) += H.ty * H.tx;
225 
226  V(i.ty, i.ty) += H.ty * H.ty;
227 
228  Y[i.t1] += H.t1 * y;
229  Y[i.tx] += H.tx * y;
230  Y[i.ty] += H.ty * y;
231  }
232 
233  // evaluate (H^T x H)^-1 x H^T x y
234 
235  V.invert();
236 
237  for (size_t row = 0; row != value.getN(); ++row) {
238  for (size_t col = 0; col != value.getN(); ++col) {
239  value[row] += V(row,col) * Y[col];
240  }
241  }
242 
243  return value;
244  }
245 
246  private:
247  mutable JModel value;
248  };
249 }
250 
251 #endif
static const double H
Planck constant [eV s].
Definition: JConstants.hh:25
Auxiliary base class for fit function of acoustic model.
Definition: JKatoomba.hh:38
Linear fit methods.
Acoustic hit.
JKatoomba(const JDetector &detector, const JSoundVelocity &velocity)
Constructor.
Definition: JKatoomba.hh:107
General purpose data regression method.
do echo Generating $dir eval D
Definition: JDrawLED.sh:50
TPaveText * p1
Sound velocity.
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Interface for maximum likelihood estimator (M-estimator).
Definition: JMEstimator.hh:20
result_type operator()(const JFunction_t &fit, T __begin, T __end)
Get chi2.
Definition: JRegressor.hh:46
Acoustic geometries.
*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
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
JEKey getEKey() const
Get emitter hash key of this hot.
Abstract minimiser.
Definition: JRegressor.hh:25
Template definition of linear fit.
Definition: JEstimator.hh:25
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:269
Acoustics hit.
JACOUSTICS::JModel::emitter_map emitter
Model for fit to acoustics data.
JACOUSTICS::JModel::string_map string
virtual double getInverseVelocity(const double D_m, const double z1, const double z2) const
Get inverse velocity of sound.
The template JSharedPointer class can be used to share a pointer to an object.
Auxiliary data structure to encapsulate different geometries.
Definition: JGeometry.hh:37
JLANG::JSharedPointer< JMEstimator > estimator
M-Estimator function.
Definition: JKatoomba.hh:78
JKatoomba_t(const JDetector &detector, const JSoundVelocity &velocity)
Constructor.
Definition: JKatoomba.hh:47
do set_variable OUTPUT_DIRECTORY $WORKDIR T
result_type operator()(const JModel &model, const JHit< JPDF_t > &hit) const
Fit function.
Definition: JKatoomba.hh:122
double getY() const
Get y position.
Definition: JVector3D.hh:103
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:129
double getLengthSquared() const
Get length squared.
Definition: JVector3D.hh:234
Implementation for velocity of sound.
Emitter hash key.
int getString() const
Get string number.
Definition: JLocation.hh:134
const JModel & operator()(T __begin, T __end) const
Get start values of string parameters.
Definition: JKatoomba.hh:177
result_type operator()(const JModel &model, T __begin, T __end)
Fit.
Definition: JKatoomba.hh:140
double getToA(const JModel &model, const JHit< JPDF_t > &hit) const
Get estimated time-of-arrival for given hit.
Definition: JKatoomba.hh:62
double getX() const
Get x position.
Definition: JVector3D.hh:93
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
const JDetector & detector
Definition: JKatoomba.hh:75
const JSoundVelocity & velocity
Definition: JKatoomba.hh:76
double u[N+1]
Definition: JPolint.hh:706
Model for fit to acoutsics data.
double getZ() const
Get z position.
Definition: JVector3D.hh:114
Maximum likelihood estimator (M-estimators).
JKatoomba(const JDetector &detector, const JSoundVelocity &velocity)
Constructor.
Definition: JKatoomba.hh:162
Template definition of fit function of acoustic model.
Definition: JKatoomba.hh:86