Jpp  15.0.1-rc.1-highqe
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 dynamical geometry of a detector string.\n
113  * In this,
114  * the position of floor > 0 corresponds to the piezo sensor which is mounted in the optical module and
115  * the position of floor = 0 to the hydrophone which is optionally mounted on the anchor.\n
116  * The reference position of a string corresponds to the top of the so-called T-bar located on the anchor.\n
117  * The position of a piezo sensor depends on the tilt of the string and the mechanical model.\n
118  * The position of the hydrophone is fixed.
119  * Its value 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 given position corresponds to the reference position of the string from
155  * which the positions of the piezo sensors and hydrophone are calculated.
156  *
157  * The template parameter should correspond to a data type which implements the following policy methods.
158  * <pre>
159  * int %getFloor();
160  * JGEOMETRY3d::JPosition3D %getPosition();
161  * </pre>
162  * In this, the position should correspond to the center of the optical module.
163  *
164  * Note that the position of the piezo is offset by JDETECTOR::getPiezoPosition with respect to the center of the optical module.\n
165  * The position of the hydrophone should separately be set.
166  *
167  * \param position position
168  * \param __begin begin of optical modules
169  * \param __end end of optical modules
170  * \param mechanics mechanical model parameters
171  */
172  template<class T>
173  JString(const JVector3D& position,
174  T __begin,
175  T __end,
176  const JMechanics& mechanics) :
177  JPosition3D(position),
178  has_hydrophone(false),
179  mechanics(mechanics)
180  {
181  for (T i = __begin; i != __end; ++i) {
182  (*this)[i->getFloor()] = this->getDistance(i->getPosition() + JDETECTOR::getPiezoPosition());
183  }
184  }
185 
186 
187  /**
188  * Check if this string has given floor.
189  *
190  * \param floor floor
191  * \return true if floor present; else false
192  */
193  bool hasFloor(int floor) const
194  {
195  if (floor == 0)
196  return has_hydrophone;
197  else
198  return this->has(floor);
199  }
200 
201 
202  /**
203  * Get height of given floor.
204  *
205  * \param floor floor
206  * \return height
207  */
208  double getHeight(const int floor) const
209  {
210  if (floor == 0) {
211 
212  if (has_hydrophone) {
213  return hydrophone.getZ();
214  }
215 
216  } else if (this->has(floor)) {
217 
218  return this->get(floor).getHeight();
219  }
220 
221  THROW(JValueOutOfRange, "Invalid floor " << floor);
222  }
223 
224 
225  /**
226  * Get position at given height according to given string model parameters.
227  *
228  * \param parameters parameters
229  * \param height height
230  * \return position
231  */
233  const double height) const
234  {
235  const double tx = parameters.tx;
236  const double ty = parameters.ty;
237  const double tz = sqrt(1.0 - tx*tx - ty*ty);
238  const double dz = this->mechanics.getHeight(height);
239 
240  return JPosition3D(this->getX() + tx * dz,
241  this->getY() + ty * dz,
242  this->getZ() + tz * height);
243  }
244 
245 
246  /**
247  * Get position of given floor according to given string model parameters.
248  *
249  * \param parameters parameters
250  * \param floor floor
251  * \return position
252  */
254  const int floor) const
255  {
256  if (floor == 0) {
257 
258  if (has_hydrophone) {
259  return getPosition() + hydrophone.getPosition();
260  }
261 
262  } else if (this->has(floor)) {
263 
264  return getPosition(parameters, this->get(floor).getHeight());
265  }
266 
267  THROW(JValueOutOfRange, "Invalid floor " << floor);
268  }
269 
270 
271  /**
272  * Get position at given height according to default string model parameters.
273  *
274  * \param height height
275  * \return position
276  */
277  JPosition3D getPosition(const double height) const
278  {
279  return getPosition(JMODEL::JString(), height);
280  }
281 
282 
283  /**
284  * Get position of given floor according to default string model parameters.
285  *
286  * \param floor floor
287  * \return position
288  */
289  JPosition3D getPosition(const int floor) const
290  {
291  return getPosition(JMODEL::JString(), floor);
292  }
293 
294 
295  /**
296  * Get distance between given position and floor according to given string model parameters.
297  *
298  * \param parameters parameters
299  * \param position position
300  * \param floor floor
301  * \return distance
302  */
304  const JVector3D& position,
305  const int floor) const
306  {
307  return this->getPosition(parameters, floor).getDistance(position);
308  }
309 
310 
311  /**
312  * Get model gradient of distance between given position and floor according to given string model parameters.
313  *
314  * \param parameters parameters
315  * \param position position
316  * \param floor floor
317  * \return gradient
318  */
320  const JVector3D& position,
321  const int floor) const
322  {
323  if (floor == 0) {
324 
325  return JMODEL::JString();
326 
327  } else if (this->has(floor)) {
328 
329  const double height = this->get(floor).getHeight();
330  const JPosition3D pos = this->getPosition(parameters, height);
331  const double hiswa = this->mechanics.getHeight(height);
332 
333  const double tx = parameters.tx;
334  const double ty = parameters.ty;
335  const double tz = sqrt(1.0 - tx*tx - ty*ty);
336 
337  const double dx = pos.getX() - position.getX();
338  const double dy = pos.getY() - position.getY();
339  const double dz = pos.getZ() - position.getZ();
340 
341  const double D = sqrt(dx*dx + dy*dy + dz*dz);
342 
343  return JMODEL::JString(hiswa * dx / D - height * (tx / tz) * dz / D,
344  hiswa * dy / D - height * (ty / tz) * dz / D);
345 
346  } else {
347 
348  THROW(JValueOutOfRange, "Invalid floor " << floor);
349  }
350  }
351 
352 
353  /**
354  * Write string parameters to output stream.
355  *
356  * \param out output stream
357  * \param string string
358  * \return output stream
359  */
360  friend inline std::ostream& operator<<(std::ostream& out, const JString& string)
361  {
362  using namespace std;
363 
364  for (JString::const_iterator i = string.begin(); i != string.end(); ++i) {
365  out << setw(2) << i->first << ' '
366  << FIXED(7,3) << i->second << " | "
367  << string.getPosition(i->first) << ' '
368  << string.mechanics << endl;
369  }
370 
371  return out;
372  }
373 
374 
375  /**
376  * Hydrophone.
377  *
378  * The position of the hydrophone is relative to the reference position of the string.
379  */
382 
383  /**
384  * Mechanical data.
385  */
387  };
388 
389 
390  /**
391  * Detector geometry.
392  */
393  struct JDetector :
394  public JHashMap<int, JString>
395  {
396  /**
397  * Default constructor.
398  */
400  {}
401 
402 
403  /**
404  * Constructor.
405  *
406  * Note that the positions of the base modules correspond to the reference position of the string.\n
407  * As a consequence, a base module (i.e.\ floor = 0) is required for each string.\n
408  * Missing base modules should therefore be added beforehand (e.g.\ using application JDetectorDB.cc).
409  *
410  * Note that if the position of a hydrophone is not available,
411  * it is assumed that there is no hydrophone on that string.\n
412  * If the position of the hydrophone is manually set,
413  * the corresponding parameter JString::has_hydrophone should be set to <tt>true</tt>.
414  *
415  * \param detector detector
416  * \param hydrophones container with data of hydrophones
417  */
420  {
421  using namespace std;
422  using namespace JPP;
423 
424  map<int, vector<module_type> > buffer; // string -> modules
425 
426  for (JDETECTOR::JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
427  buffer[module->getString()].push_back(module_type(module->getLocation(), module->getPosition()));
428  }
429 
430  for (map<int, vector<module_type> >::iterator i = buffer.begin(); i != buffer.end(); ++i) {
431 
432  sort(i->second.begin(), i->second.end(), make_comparator(&module_type::getFloor));
433 
434  if (i->second[0].getFloor() == 0) {
435 
436  vector<module_type>::iterator p = i->second.begin();
437 
438  ++p;
439 
440  (*this)[i->first] = JGEOMETRY::JString(i->second[0].getPosition(), p, i->second.end(), getMechanics(i->first));
441 
442  try {
443 
444  (*this)[i->first].hydrophone = getPosition(hydrophones.begin(),
445  hydrophones.end(),
447 
448  (*this)[i->first].has_hydrophone = true;
449  }
450  catch(const exception&) {
451  (*this)[i->first].has_hydrophone = false;
452  }
453 
454  } else {
455 
456  THROW(JNoValue, "No floor 0 in string " << i->first << "; use e.g. JDetectorDB -W.");
457  }
458  }
459  }
460 
461 
462  /**
463  * Check if this detector has given string.
464  *
465  * \param string string
466  * \return true if string present; else false
467  */
468  bool hasString(int string) const
469  {
470  return this->has(string);
471  }
472 
473 
474  /**
475  * Check if this detector has given location.
476  *
477  * \param location location
478  * \return true if location present; else false
479  */
480  bool hasLocation(const JLocation& location) const
481  {
482  return this->hasString(location.getString()) && (*this)[location.getString()].hasFloor(location.getFloor());
483  }
484 
485 
486  /**
487  * Write detector parameters to output stream.
488  *
489  * \param out output stream
490  * \param detector detector
491  * \return output stream
492  */
493  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
494  {
495  using namespace std;
496 
497  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
498  out << setw(4) << i->first << endl << i->second;
499  }
500 
501  return out;
502  }
503 
504 
505  /**
506  * Auxiliary data structure for module location and position.
507  */
508  struct module_type :
509  public JLocation,
510  public JPosition3D
511  {
512  /**
513  * Constructor.
514  *
515  * \param location module location
516  * \param position module position
517  */
518  module_type(const JLocation& location,
519  const JPosition3D& position) :
520  JLocation (location),
521  JPosition3D(position)
522  {}
523 
524 
525  /**
526  * Less-than operator.
527  *
528  * \param first first module
529  * \param second second module
530  * \return true if floor of first module less than that of second; else false
531  */
532  friend inline bool operator<(const module_type& first, const module_type& second)
533  {
534  return first.getFloor() < second.getFloor();
535  }
536  };
537  };
538  }
539 
540 
541  /**
542  * Type definition of detector geometry.
543  */
545 }
546 
547 #endif
Mechanical modelling of string.
Exceptions.
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:89
#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:173
*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:208
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:508
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:232
friend std::ostream & operator<<(std::ostream &out, const JString &string)
Write string parameters to output stream.
Definition: JGeometry.hh:360
bool hasString(int string) const
Check if this detector has given string.
Definition: JGeometry.hh:468
JGEOMETRY::JDetector JGeometry
Type definition of detector geometry.
Definition: JGeometry.hh:544
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:319
Exception for missing value.
Definition: JException.hh:198
module_type(const JLocation &location, const JPosition3D &position)
Constructor.
Definition: JGeometry.hh:518
Detector file.
Definition: JHead.hh:196
Acoustics support kit.
Detector support kit.
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:303
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:418
JPosition3D getPosition(const double height) const
Get position at given height according to default string model parameters.
Definition: JGeometry.hh:277
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:493
JPosition3D hydrophone
Hydrophone.
Definition: JGeometry.hh:380
JMechanics mechanics
Mechanical data.
Definition: JGeometry.hh:386
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:532
JDetector()
Default constructor.
Definition: JGeometry.hh:399
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:289
bool hasLocation(const JLocation &location) const
Check if this detector has given location.
Definition: JGeometry.hh:480
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.
do echo Generating $dir eval D
Definition: JDrawLED.sh:53
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:193
JPosition3D getPosition(const JMODEL::JString &parameters, const int floor) const
Get position of given floor according to given string model parameters.
Definition: JGeometry.hh:253
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:39
container_type::iterator iterator
Definition: JHashMap.hh:88