Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGeometry.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JGEOMETRY__
2 #define __JACOUSTICS__JGEOMETRY__
3 
4 #include <ostream>
5 #include <iomanip>
6 #include <vector>
7 #include <map>
8 #include <algorithm>
9 #include <cmath>
10 
11 #include "JLang/JException.hh"
12 #include "JLang/JPredicate.hh"
13 #include "JLang/JComparator.hh"
14 #include "JLang/JManip.hh"
15 
17 
18 #include "JTools/JHashMap.hh"
19 
20 #include "JDetector/JLocation.hh"
21 #include "JDetector/JHydrophone.hh"
22 #include "JDetector/JDetector.hh"
24 
27 #include "JAcoustics/JMechanics.hh"
28 #include "JAcoustics/JModel.hh"
29 
30 
31 /**
32  * \file
33  *
34  * Acoustic geometries.
35  * \author mdejong
36  */
37 namespace JACOUSTICS {}
38 namespace JPP { using namespace JACOUSTICS; }
39 
40 namespace JACOUSTICS {
41 
43  using JLANG::JNoValue;
46  using JTOOLS::JHashMap;
48 
49 
50  /**
51  * Auxiliary namespace to encapsulate different geometries.\n
52  */
53  namespace JGEOMETRY {
54 
55  /**
56  * Floor geometry.
57  */
58  struct JFloor {
59  /**
60  * Default constructor.
61  */
62  JFloor() :
63  height(0.0)
64  {}
65 
66 
67  /**
68  * Constructor.\n
69  * The height refers to the maximal distance from the top of the so-called T-bar of the parent string to the actual sensor.
70  *
71  * \param height height
72  */
73  JFloor(const double height) :
74  height(height)
75  {}
76 
77 
78  /**
79  * Get height of this floor.\n
80  * The height refers to the maximal distance from
81  * the fixed reference point of the parent string.
82  *
83  * \return height
84  */
85  double getHeight() const
86  {
87  return height;
88  }
89 
90 
91  /**
92  * Write floor parameters to output stream.
93  *
94  * \param out output stream
95  * \param floor floor
96  * \return output stream
97  */
98  friend inline std::ostream& operator<<(std::ostream& out, const JFloor& floor)
99  {
100  return out << FIXED(7,2) << floor.getHeight();
101  }
102 
103 
104  protected:
105  double height;
106  };
107 
108 
109  /**
110  * String geometry.
111  *
112  * This data structure provides for the implementation of the model dependent geometry of a detector string.\n
113  * The position of a string corresponds to the top of the so-called T-bar which is mounted on the anchor.\n
114  * A piezo sensor is mounted in each optical module.\n
115  * The position of the piezo sensor is determined by
116  * the floor number of the optical module and
117  * the actual string parameters.\n
118  * A hydrophone is optionally mounted on the anchor which corresponds to floor zero.\n
119  * It has a fixed position which is relative to the reference position of the string.
120  */
121  struct JString :
122  public JPosition3D,
123  public JHashMap<int, JFloor>
124  {
127 
128 
129  /**
130  * Default constructor.
131  */
133  has_hydrophone(false)
134  {}
135 
136 
137  /**
138  * Constructor.
139  *
140  * The given position corresponds to the reference point of the string from
141  * which the positions of the piezo sensors and hydrophone are calculated.
142  *
143  * \param position position
144  */
145  JString(const JVector3D& position) :
146  JPosition3D(position),
147  has_hydrophone(false)
148  {}
149 
150 
151  /**
152  * Constructor.
153  *
154  * The template parameter should correspond to a data type which implements the following policy methods.
155  * <pre>
156  * int getFloor();
157  * JPosition3D getPosition();
158  * </pre>
159  *
160  * The given position corresponds to the reference point of the string from
161  * which the positions of the piezo sensors and hydrophone are calculated.
162  *
163  * Note that the position of the piezo is offset by JDETECTOR::getPiezoPosition and
164  * that the position of the hydrophone should manually be set.
165  *
166  * \param position position
167  * \param __begin begin of optical modules
168  * \param __end end of optical modules
169  * \param mechanics mechanical model parameters
170  */
171  template<class T>
172  JString(const JVector3D& position,
173  T __begin,
174  T __end,
175  const JMechanics& mechanics) :
176  JPosition3D(position),
177  has_hydrophone(false),
178  mechanics(mechanics)
179  {
180  for (T i = __begin; i != __end; ++i) {
181  (*this)[i->getFloor()] = this->getDistance(i->getPosition() + JDETECTOR::getPiezoPosition());
182  }
183  }
184 
185 
186  /**
187  * Check if this string has given floor.
188  *
189  * \param floor floor
190  * \return true if floor present; else false
191  */
192  bool hasFloor(int floor) const
193  {
194  if (floor == 0)
195  return has_hydrophone;
196  else
197  return this->has(floor);
198  }
199 
200 
201  /**
202  * Get height of given floor.
203  *
204  * \param floor floor
205  * \return height
206  */
207  double getHeight(const int floor) const
208  {
209  if (floor == 0) {
210 
211  if (has_hydrophone) {
212  return hydrophone.getZ();
213  }
214 
215  } else if (this->has(floor)) {
216 
217  return this->get(floor).getHeight();
218  }
219 
220  THROW(JValueOutOfRange, "Invalid floor " << floor);
221  }
222 
223 
224  /**
225  * Get position at given height according to given string model parameters.
226  *
227  * \param parameters parameters
228  * \param height height
229  * \return position
230  */
232  const double height) const
233  {
234  const double tx = parameters.tx;
235  const double ty = parameters.ty;
236  const double tz = sqrt(1.0 - tx*tx - ty*ty);
237  const double dz = this->mechanics.getHeight(height);
238 
239  return JPosition3D(this->getX() + tx * dz,
240  this->getY() + ty * dz,
241  this->getZ() + tz * height);
242  }
243 
244 
245  /**
246  * Get position of given floor according to given string model parameters.
247  *
248  * \param parameters parameters
249  * \param floor floor
250  * \return position
251  */
253  const int floor) const
254  {
255  if (floor == 0) {
256 
257  if (has_hydrophone) {
258  return getPosition() + hydrophone.getPosition();
259  }
260 
261  } else if (this->has(floor)) {
262 
263  return getPosition(parameters, this->get(floor).getHeight());
264  }
265 
266  THROW(JValueOutOfRange, "Invalid floor " << floor);
267  }
268 
269 
270  /**
271  * Get position at given height according to default string model parameters.
272  *
273  * \param height height
274  * \return position
275  */
276  JPosition3D getPosition(const double height) const
277  {
278  return getPosition(JMODEL::JString(), height);
279  }
280 
281 
282  /**
283  * Get position of given floor according to default string model parameters.
284  *
285  * \param floor floor
286  * \return position
287  */
288  JPosition3D getPosition(const int floor) const
289  {
290  return getPosition(JMODEL::JString(), floor);
291  }
292 
293 
294  /**
295  * Get distance between given position and floor according to given string model parameters.
296  *
297  * \param parameters parameters
298  * \param position position
299  * \param floor floor
300  * \return distance
301  */
303  const JVector3D& position,
304  const int floor) const
305  {
306  return this->getPosition(parameters, floor).getDistance(position);
307  }
308 
309 
310  /**
311  * Get model gradient of distance between given position and floor according to given string model parameters.
312  *
313  * \param parameters parameters
314  * \param position position
315  * \param floor floor
316  * \return gradient
317  */
319  const JVector3D& position,
320  const int floor) const
321  {
322  if (floor == 0) {
323 
324  return JMODEL::JString();
325 
326  } else if (this->has(floor)) {
327 
328  const double height = this->get(floor).getHeight();
329  const JPosition3D pos = this->getPosition(parameters, height);
330  const double hiswa = this->mechanics.getHeight(height);
331 
332  const double tx = parameters.tx;
333  const double ty = parameters.ty;
334  const double tz = sqrt(1.0 - tx*tx - ty*ty);
335 
336  const double dx = pos.getX() - position.getX();
337  const double dy = pos.getY() - position.getY();
338  const double dz = pos.getZ() - position.getZ();
339 
340  const double D = sqrt(dx*dx + dy*dy + dz*dz);
341 
342  return JMODEL::JString(hiswa * dx / D - height * (tx / tz) * dz / D,
343  hiswa * dy / D - height * (ty / tz) * dz / D);
344 
345  } else {
346 
347  THROW(JValueOutOfRange, "Invalid floor " << floor);
348  }
349  }
350 
351 
352  /**
353  * Write string parameters to output stream.
354  *
355  * \param out output stream
356  * \param string string
357  * \return output stream
358  */
359  friend inline std::ostream& operator<<(std::ostream& out, const JString& string)
360  {
361  using namespace std;
362 
363  for (JString::const_iterator i = string.begin(); i != string.end(); ++i) {
364  out << setw(2) << i->first << ' '
365  << FIXED(7,3) << i->second << " | "
366  << string.getPosition(i->first) << ' '
367  << string.mechanics << endl;
368  }
369 
370  return out;
371  }
372 
373 
374  /**
375  * Hydrophone.
376  *
377  * The position of the hydrophone is relative to the reference position of the string.
378  */
381 
382  /**
383  * Mechanical data.
384  */
386  };
387 
388 
389  /**
390  * Detector geometry.
391  */
392  struct JDetector :
393  public JHashMap<int, JString>
394  {
395  /**
396  * Default constructor.
397  */
399  {}
400 
401 
402  /**
403  * Constructor.
404  *
405  * If position of hydrophone is not available, it is assumed that there is no hydrophone.\n
406  * If the position of hydrophone is manually set,
407  * the corresponding parameter JString::has_hydrophone should be set to <tt>true</tt>.
408  *
409  * Missing base modules (i.e.\ floor 0) in the detector can be added using application JDetectorDB.cc.
410  *
411  * \param detector detector
412  * \param hydrophones container with data of hydrophones
413  */
416  {
417  using namespace std;
418  using namespace JPP;
419 
420  map<int, vector<module_type> > buffer; // string -> modules
421 
422  for (JDETECTOR::JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
423  buffer[module->getString()].push_back(module_type(module->getLocation(), module->getPosition()));
424  }
425 
426  for (map<int, vector<module_type> >::iterator i = buffer.begin(); i != buffer.end(); ++i) {
427 
428  sort(i->second.begin(), i->second.end(), make_comparator(&module_type::getFloor));
429 
430  if (i->second[0].getFloor() == 0) {
431 
432  vector<module_type>::iterator p = i->second.begin();
433 
434  ++p;
435 
436  (*this)[i->first] = JGEOMETRY::JString(i->second[0].getPosition(), p, i->second.end(), getMechanics(i->first));
437 
438  try {
439 
440  (*this)[i->first].hydrophone = getPosition(hydrophones.begin(),
441  hydrophones.end(),
443 
444  (*this)[i->first].has_hydrophone = true;
445  }
446  catch(const exception&) {
447  (*this)[i->first].has_hydrophone = false;
448  }
449 
450  } else {
451 
452  THROW(JNoValue, "No floor 0 in string " << i->first << "; use e.g. JDetectorDB -W.");
453  }
454  }
455  }
456 
457 
458  /**
459  * Check if this detector has given string.
460  *
461  * \param string string
462  * \return true if string present; else false
463  */
464  bool hasString(int string) const
465  {
466  return this->has(string);
467  }
468 
469 
470  /**
471  * Check if this detector has given location.
472  *
473  * \param location location
474  * \return true if location present; else false
475  */
476  bool hasLocation(const JLocation& location) const
477  {
478  return this->hasString(location.getString()) && (*this)[location.getString()].hasFloor(location.getFloor());
479  }
480 
481 
482  /**
483  * Write detector parameters to output stream.
484  *
485  * \param out output stream
486  * \param detector detector
487  * \return output stream
488  */
489  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
490  {
491  using namespace std;
492 
493  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
494  out << setw(4) << i->first << endl << i->second;
495  }
496 
497  return out;
498  }
499 
500 
501  /**
502  * Auxiliary data structure for module location and position.
503  */
504  struct module_type :
505  public JLocation,
506  public JPosition3D
507  {
508  /**
509  * Constructor.
510  *
511  * \param location module location
512  * \param position module position
513  */
514  module_type(const JLocation& location,
515  const JPosition3D& position) :
516  JLocation (location),
517  JPosition3D(position)
518  {}
519 
520 
521  /**
522  * Less-than operator.
523  *
524  * \param first first module
525  * \param second second module
526  * \return true if floor of first module less than that of second; else false
527  */
528  friend inline bool operator<(const module_type& first, const module_type& second)
529  {
530  return first.getFloor() < second.getFloor();
531  }
532  };
533  };
534  }
535 
536 
537  /**
538  * Type definition of detector geometry.
539  */
541 }
542 
543 #endif
Mechanical modelling of string.
Exceptions.
do echo Generating $dir eval D
Definition: JDrawLED.sh:50
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
Definition: JComparator.hh:185
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
static JDetectorMechanics getMechanics
Function object to get string mechanics.
Definition: JMechanics.hh:243
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:72
friend std::ostream & operator<<(std::ostream &out, const JFloor &floor)
Write floor parameters to output stream.
Definition: JGeometry.hh:98
General purpose class for hash map of unique elements.
Detector data structure.
Definition: JDetector.hh:80
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JString(const JVector3D &position, T __begin, T __end, const JMechanics &mechanics)
Constructor.
Definition: JGeometry.hh:172
*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
JString()
Default constructor.
Definition: JGeometry.hh:132
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:270
double getHeight(const int floor) const
Get height of given floor.
Definition: JGeometry.hh:207
JFloor()
Default constructor.
Definition: JGeometry.hh:62
JString(const JVector3D &position)
Constructor.
Definition: JGeometry.hh:145
Data structure for detector geometry and calibration.
Auxiliary data structure for module location and position.
Definition: JGeometry.hh:504
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Data structure for hydrophone.
JFloor(const double height)
Constructor.
Definition: JGeometry.hh:73
JPosition3D getPosition(const JMODEL::JString &parameters, const double height) const
Get position at given height according to given string model parameters.
Definition: JGeometry.hh:231
friend std::ostream & operator<<(std::ostream &out, const JString &string)
Write string parameters to output stream.
Definition: JGeometry.hh:359
bool hasString(int string) const
Check if this detector has given string.
Definition: JGeometry.hh:464
JGEOMETRY::JDetector JGeometry
Type definition of detector geometry.
Definition: JGeometry.hh:540
JMODEL::JString getGradient(const JMODEL::JString &parameters, const JVector3D &position, const int floor) const
Get model gradient of distance between given position and floor according to given string model param...
Definition: JGeometry.hh:318
Exception for missing value.
Definition: JException.hh:198
module_type(const JLocation &location, const JPosition3D &position)
Constructor.
Definition: JGeometry.hh:514
Detector file.
Definition: JHead.hh:196
Acoustics toolkit.
Detector toolkit.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
Logical location of module.
Definition: JLocation.hh:37
Acoustics toolkit.
double getDistance(const JMODEL::JString &parameters, const JVector3D &position, const int floor) const
Get distance between given position and floor according to given string model parameters.
Definition: JGeometry.hh:302
do set_variable OUTPUT_DIRECTORY $WORKDIR T
double getHeight(const double height) const
Get effective height for given actual height.
Definition: JMechanics.hh:68
double getY() const
Get y position.
Definition: JVector3D.hh:104
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
Logical location of module.
I/O manipulators.
JDetector(const JDETECTOR::JDetector &detector, const std::vector< JDETECTOR::JHydrophone > &hydrophones=std::vector< JDETECTOR::JHydrophone >())
Constructor.
Definition: JGeometry.hh:414
JPosition3D getPosition(const double height) const
Get position at given height according to default string model parameters.
Definition: JGeometry.hh:276
int getString() const
Get string number.
Definition: JLocation.hh:134
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector parameters to output stream.
Definition: JGeometry.hh:489
JPosition3D hydrophone
Hydrophone.
Definition: JGeometry.hh:379
JMechanics mechanics
Mechanical data.
Definition: JGeometry.hh:385
JPosition3D getPiezoPosition()
Get relative position of piezo in optical module.
JPosition3D()
Default constructor.
Definition: JPosition3D.hh:48
friend bool operator<(const module_type &first, const module_type &second)
Less-than operator.
Definition: JGeometry.hh:528
JDetector()
Default constructor.
Definition: JGeometry.hh:398
double getX() const
Get x position.
Definition: JVector3D.hh:94
JPosition3D getPosition(const int floor) const
Get position of given floor according to default string model parameters.
Definition: JGeometry.hh:288
bool hasLocation(const JLocation &location) const
Check if this detector has given location.
Definition: JGeometry.hh:476
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
double getHeight() const
Get height of this floor.
Definition: JGeometry.hh:85
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
container_type::const_iterator const_iterator
Definition: JHashMap.hh:86
Model for fit to acoutsics data.
bool has(const T &value) const
Test whether given value is present.
double getZ() const
Get z position.
Definition: JVector3D.hh:115
bool hasFloor(int floor) const
Check if this string has given floor.
Definition: JGeometry.hh:192
JPosition3D getPosition(const JMODEL::JString &parameters, const int floor) const
Get position of given floor according to given string model parameters.
Definition: JGeometry.hh:252
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:39
container_type::iterator iterator
Definition: JHashMap.hh:88