Jpp
 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
70  * the fixed reference point of the parent string.
71  *
72  * \param height height
73  */
74  JFloor(const double height) :
75  height(height)
76  {}
77 
78 
79  /**
80  * Get height of this floor.\n
81  * The height refers to the maximal distance from
82  * the fixed reference point of the parent string.
83  *
84  * \return height
85  */
86  double getHeight() const
87  {
88  return height;
89  }
90 
91 
92  /**
93  * Write floor parameters to output stream.
94  *
95  * \param out output stream
96  * \param floor floor
97  * \return output stream
98  */
99  friend inline std::ostream& operator<<(std::ostream& out, const JFloor& floor)
100  {
101  return out << FIXED(7,2) << floor.getHeight();
102  }
103 
104 
105  protected:
106  double height;
107  };
108 
109 
110  /**
111  * String geometry.
112  *
113  * This data structure provides for the implementation of the model dependent geometry of a detector string.\n
114  * The position of a string corresponds to the top of the so-called T-bar which is mounted on the anchor.\n
115  * A piezo sensor is mounted in each optical module.\n
116  * The position of the piezo sensor is determined by
117  * the floor number of the optical module and
118  * the actual string parameters.\n
119  * A hydrophone is optionally mounted on the anchor which corresponds to floor zero.\n
120  * It has a fixed position which is relative to the reference position of the string.
121  */
122  struct JString :
123  public JPosition3D,
124  public JHashMap<int, JFloor>
125  {
128 
129 
130  /**
131  * Default constructor.
132  */
134  has_hydrophone(false)
135  {}
136 
137 
138  /**
139  * Constructor.
140  *
141  * The given position corresponds to the reference point of the string from
142  * which the positions of the piezo sensors and hydrophone are calculated.
143  *
144  * \param position position
145  */
146  JString(const JVector3D& position) :
147  JPosition3D(position),
148  has_hydrophone(false)
149  {}
150 
151 
152  /**
153  * Constructor.
154  *
155  * The template parameter should correspond to a data type which implements the following policy methods.
156  * <pre>
157  * int getFloor();
158  * JPosition3D getPosition();
159  * </pre>
160  *
161  * The given position corresponds to the reference point of the string from
162  * which the positions of the piezo sensors and hydrophone are calculated.
163  *
164  * Note that the position of the piezo is offset by JDETECTOR::getPiezoPosition and
165  * that the position of the hydrophone should manually 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 effective height.
204  *
205  * \param height height
206  * \return height
207  */
208  double getHeight(const double height) const
209  {
210  return this->mechanics.getHeight(height);
211  }
212 
213 
214  /**
215  * Get effective height of given floor
216  *
217  * \param floor floor
218  * \return height
219  */
220  double getHeight(const int floor) const
221  {
222  if (floor == 0) {
223 
224  if (has_hydrophone) {
225  return hydrophone.getZ();
226  }
227 
228  } else if (this->has(floor)) {
229 
230  return getHeight(this->get(floor).getHeight());
231  }
232 
233  THROW(JValueOutOfRange, "Invalid floor " << floor);
234  }
235 
236 
237  /**
238  * Get position at given height according to actual string parameters.
239  *
240  * \param parameters parameters
241  * \param height height
242  * \return position
243  */
245  const double height) const
246  {
247  const double tx = parameters.tx;
248  const double ty = parameters.ty;
249  const double tz = sqrt(1.0 - tx*tx - ty*ty);
250 
251  return JPosition3D(this->getX() + tx * this->mechanics.getHeight(height),
252  this->getY() + ty * this->mechanics.getHeight(height),
253  this->getZ() + tz * height);
254  }
255 
256 
257  /**
258  * Get position of given floor according to actual string parameters.
259  *
260  * \param parameters parameters
261  * \param floor floor
262  * \return position
263  */
265  const int floor) const
266  {
267  if (floor == 0) {
268 
269  if (has_hydrophone) {
270  return getPosition() + hydrophone.getPosition();
271  }
272 
273  } else if (this->has(floor)) {
274 
275  return getPosition(parameters, this->get(floor).getHeight());
276  }
277 
278  THROW(JValueOutOfRange, "Invalid floor " << floor);
279  }
280 
281 
282  /**
283  * Get position at given height according to default string parameters.
284  *
285  * \param height height
286  * \return position
287  */
288  JPosition3D getPosition(const double height) const
289  {
290  return getPosition(JMODEL::JString(), height);
291  }
292 
293 
294  /**
295  * Get position of given floor according to default string parameters.
296  *
297  * \param floor floor
298  * \return position
299  */
300  JPosition3D getPosition(const int floor) const
301  {
302  return getPosition(JMODEL::JString(), floor);
303  }
304 
305 
306  /**
307  * Get distance between given position and floor according to actual string parameters.
308  *
309  * \param parameters parameters
310  * \param position position
311  * \param floor floor
312  * \return distance
313  */
315  const JVector3D& position,
316  const int floor) const
317  {
318  return this->getPosition(parameters, floor).getDistance(position);
319  }
320 
321 
322  /**
323  * Get model gradient of distance between given position and floor according to actual string parameters.
324  *
325  * \param parameters parameters
326  * \param position position
327  * \param floor floor
328  * \return gradient
329  */
331  const JVector3D& position,
332  const int floor) const
333  {
334  if (floor == 0) {
335 
336  return JMODEL::JString();
337 
338  } else if (this->has(floor)) {
339 
340  const double height = this->get(floor).getHeight();
341  const JPosition3D pos = this->getPosition(parameters, height);
342 
343  const double tx = parameters.tx;
344  const double ty = parameters.ty;
345  const double tz = sqrt(1.0 - tx*tx - ty*ty);
346 
347  const double dx = pos.getX() - position.getX();
348  const double dy = pos.getY() - position.getY();
349  const double dz = pos.getZ() - position.getZ();
350 
351  const double D = sqrt(dx*dx + dy*dy + dz*dz);
352 
353  return JMODEL::JString(height * dx / D - (tx / tz) * dz / D,
354  height * dy / D - (ty / tz) * dz / D);
355 
356  } else {
357 
358  THROW(JValueOutOfRange, "Invalid floor " << floor);
359  }
360  }
361 
362 
363  /**
364  * Write string parameters to output stream.
365  *
366  * \param out output stream
367  * \param string string
368  * \return output stream
369  */
370  friend inline std::ostream& operator<<(std::ostream& out, const JString& string)
371  {
372  using namespace std;
373 
374  for (JString::const_iterator i = string.begin(); i != string.end(); ++i) {
375  out << setw(2) << i->first << ' '
376  << FIXED(7,3) << i->second << " | "
377  << string.getPosition(i->first) << ' '
378  << string.mechanics << endl;
379  }
380 
381  return out;
382  }
383 
384 
385  /**
386  * Hydrophone.
387  *
388  * The position of the hydrophone is relative to the reference position of the string.
389  */
392 
393  /**
394  * Mechanical data.
395  */
397  };
398 
399 
400  /**
401  * Detector geometry.
402  */
403  struct JDetector :
404  public JHashMap<int, JString>
405  {
406  /**
407  * Default constructor.
408  */
410  {}
411 
412 
413  /**
414  * Constructor.
415  *
416  * If position of hydrophone is not available, it is assumed that there is no hydrophone.\n
417  * If the position of hydrophone is manually set,
418  * the corresponding parameter JString::has_hydrophone should be set to <tt>true</tt>.
419  *
420  * Missing base modules (i.e.\ floor 0) in the detector can be added using application JDetectorDB.cc.
421  *
422  * \param detector detector
423  * \param hydrophones container with data of hydrophones
424  */
427  {
428  using namespace std;
429  using namespace JPP;
430 
431  map<int, vector<module_type> > buffer; // string -> modules
432 
433  for (JDETECTOR::JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
434  buffer[module->getString()].push_back(module_type(module->getLocation(), module->getPosition()));
435  }
436 
437  for (map<int, vector<module_type> >::iterator i = buffer.begin(); i != buffer.end(); ++i) {
438 
439  sort(i->second.begin(), i->second.end(), make_comparator(&module_type::getFloor));
440 
441  if (i->second[0].getFloor() == 0) {
442 
443  vector<module_type>::iterator p = i->second.begin();
444 
445  ++p;
446 
447  (*this)[i->first] = JGEOMETRY::JString(i->second[0].getPosition(), p, i->second.end(), getMechanics[detector.getID()][i->first]);
448 
449  try {
450 
451  (*this)[i->first].hydrophone = getPosition(hydrophones.begin(),
452  hydrophones.end(),
454 
455  (*this)[i->first].has_hydrophone = true;
456  }
457  catch(const exception&) {
458  (*this)[i->first].has_hydrophone = false;
459  }
460 
461  } else {
462 
463  THROW(JNoValue, "No floor 0 in string " << i->first << "; use e.g. JDetectorDB -W.");
464  }
465  }
466  }
467 
468 
469  /**
470  * Check if this detector has given string.
471  *
472  * \param string string
473  * \return true if string present; else false
474  */
475  bool hasString(int string) const
476  {
477  return this->has(string);
478  }
479 
480 
481  /**
482  * Check if this detector has given location.
483  *
484  * \param location location
485  * \return true if location present; else false
486  */
487  bool hasLocation(const JLocation& location) const
488  {
489  return this->hasString(location.getString()) && (*this)[location.getString()].hasFloor(location.getFloor());
490  }
491 
492 
493  /**
494  * Write detector parameters to output stream.
495  *
496  * \param out output stream
497  * \param detector detector
498  * \return output stream
499  */
500  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
501  {
502  using namespace std;
503 
504  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
505  out << setw(4) << i->first << endl << i->second;
506  }
507 
508  return out;
509  }
510 
511 
512  /**
513  * Auxiliary data structure for module location and position.
514  */
515  struct module_type :
516  public JLocation,
517  public JPosition3D
518  {
519  /**
520  * Constructor.
521  *
522  * \param location module location
523  * \param position module position
524  */
525  module_type(const JLocation& location,
526  const JPosition3D& position) :
527  JLocation (location),
528  JPosition3D(position)
529  {}
530 
531 
532  /**
533  * Less-than operator.
534  *
535  * \param first first module
536  * \param second second module
537  * \return true if floor of first module less than that of second; else false
538  */
539  friend inline bool operator<(const module_type& first, const module_type& second)
540  {
541  return first.getFloor() < second.getFloor();
542  }
543  };
544  };
545  }
546 
547 
548  /**
549  * Type definition of detector geometry.
550  */
552 }
553 
554 #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
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:71
friend std::ostream & operator<<(std::ostream &out, const JFloor &floor)
Write floor parameters to output stream.
Definition: JGeometry.hh:99
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: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:133
double getHeight(const double height) const
Get effective height.
Definition: JGeometry.hh:208
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:445
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:270
double getHeight(const int floor) const
Get effective height of given floor.
Definition: JGeometry.hh:220
JFloor()
Default constructor.
Definition: JGeometry.hh:62
JString(const JVector3D &position)
Constructor.
Definition: JGeometry.hh:146
Data structure for detector geometry and calibration.
Auxiliary data structure for module location and position.
Definition: JGeometry.hh:515
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:74
JPosition3D getPosition(const JMODEL::JString &parameters, const double height) const
Get position at given height according to actual string parameters.
Definition: JGeometry.hh:244
JACOUSTICS::JGetMechanics getMechanics
friend std::ostream & operator<<(std::ostream &out, const JString &string)
Write string parameters to output stream.
Definition: JGeometry.hh:370
bool hasString(int string) const
Check if this detector has given string.
Definition: JGeometry.hh:475
JGEOMETRY::JDetector JGeometry
Type definition of detector geometry.
Definition: JGeometry.hh:551
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 actual string parameters...
Definition: JGeometry.hh:330
Exception for missing value.
Definition: JException.hh:198
module_type(const JLocation &location, const JPosition3D &position)
Constructor.
Definition: JGeometry.hh:525
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 actual string parameters.
Definition: JGeometry.hh:314
do set_variable OUTPUT_DIRECTORY $WORKDIR T
int getID() const
Get identifier.
Definition: JObjectID.hh:50
double getHeight(const double height) const
Get effective height.
Definition: JMechanics.hh:59
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:425
JPosition3D getPosition(const double height) const
Get position at given height according to default string parameters.
Definition: JGeometry.hh:288
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:500
JPosition3D hydrophone
Hydrophone.
Definition: JGeometry.hh:390
JMechanics mechanics
Mechanical data.
Definition: JGeometry.hh:396
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:539
JDetector()
Default constructor.
Definition: JGeometry.hh:409
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 parameters.
Definition: JGeometry.hh:300
bool hasLocation(const JLocation &location) const
Check if this detector has given location.
Definition: JGeometry.hh:487
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
double getHeight() const
Get height of this floor.
Definition: JGeometry.hh:86
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:85
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:193
JPosition3D getPosition(const JMODEL::JString &parameters, const int floor) const
Get position of given floor according to actual string parameters.
Definition: JGeometry.hh:264
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:30
container_type::iterator iterator
Definition: JHashMap.hh:87