Jpp  17.0.0
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  static constexpr double PRECISION_M = 1.0e-4; //!< precision of height evaluation [m]
129 
130  /**
131  * Get approximate length of string.
132  *
133  * \param parameters parameters
134  * \param mechanics mechanics
135  * \param height height
136  * \return length
137  */
138  static inline double getLength(const JMODEL::JString& parameters,
139  const JMechanics& mechanics,
140  const double height)
141  {
142  const double t2 = parameters.getLengthSquared();
143 
144  return 0.5*t2*mechanics.b * log(1.0 - mechanics.a*height) + sqrt(1.0 + t2) * height;
145  }
146 
147 
148  /**
149  * Get approximate height of string.
150  *
151  * \param parameters parameters
152  * \param mechanics mechanics
153  * \param length length
154  * \param precision precision
155  * \return height
156  */
157  static inline double getHeight(const JMODEL::JString& parameters,
158  const JMechanics& mechanics,
159  const double length,
160  const double precision = PRECISION_M)
161  {
162  const size_t MAXIMUM_NUMBER_OF_ITERATIONS = 10;
163 
164  const double ts = 0.5 * parameters.getLengthSquared();
165 
166  double z = length; // start value
167 
168  for (size_t i = 0; i != MAXIMUM_NUMBER_OF_ITERATIONS; ++i) {
169 
170  const double ls = getLength(parameters, mechanics, z);
171 
172  if (fabs(ls - length) <= precision) {
173  break;
174  }
175 
176  z -= (ls - length) / (1.0 + ts * (1.0 - mechanics.a*mechanics.b / (1.0 - mechanics.a*z)));
177  }
178 
179  return z;
180  }
181 
182  /**
183  * Get position at given height according to given string model parameters and mechanics.
184  *
185  * \param parameters parameters
186  * \param mechanics mechanics
187  * \param height height
188  * \return position
189  */
191  const JMechanics& mechanics,
192  const double height)
193  {
194  const double h1 = height * (1.0 + parameters.vs);
195  const double z1 = mechanics.getHeight(h1);
196 
197  return JPosition3D(parameters.tx * z1 + parameters.tx2 * h1*h1,
198  parameters.ty * z1 + parameters.ty2 * h1*h1,
199  getHeight(parameters, mechanics, h1));
200  }
201 
202 
203  /**
204  * Default constructor.
205  */
207  has_hydrophone(false)
208  {}
209 
210 
211  /**
212  * Constructor.
213  *
214  * The given position corresponds to the reference point of the string from
215  * which the positions of the piezo sensors and hydrophone are calculated.
216  *
217  * \param position position
218  */
219  JString(const JVector3D& position) :
220  JPosition3D(position),
221  has_hydrophone(false)
222  {}
223 
224 
225  /**
226  * Constructor.
227  *
228  * The given position corresponds to the reference position of the string from
229  * which the positions of the piezo sensors and hydrophone are calculated.
230  *
231  * The template parameter should correspond to a data type which implements the following policy methods.
232  * <pre>
233  * int %getFloor();
234  * JGEOMETRY3d::JPosition3D %getPosition();
235  * </pre>
236  * In this, the position should correspond to the center of the optical module.
237  *
238  * Note that the position of the piezo is offset by JDETECTOR::getPiezoPosition with respect to the center of the optical module.\n
239  * The position of the hydrophone should separately be set.
240  *
241  * \param position position
242  * \param __begin begin of optical modules
243  * \param __end end of optical modules
244  * \param mechanics mechanical model parameters
245  */
246  template<class T>
247  JString(const JVector3D& position,
248  T __begin,
249  T __end,
250  const JMechanics& mechanics) :
251  JPosition3D(position),
252  has_hydrophone(false),
253  mechanics(mechanics)
254  {
255  for (T i = __begin; i != __end; ++i) {
256  (*this)[i->getFloor()] = this->getDistance(i->getPosition() + JDETECTOR::getPiezoPosition());
257  }
258  }
259 
260 
261  /**
262  * Check if this string has given floor.
263  *
264  * \param floor floor
265  * \return true if floor present; else false
266  */
267  bool hasFloor(int floor) const
268  {
269  if (floor == 0)
270  return has_hydrophone;
271  else
272  return this->has(floor);
273  }
274 
275 
276  /**
277  * Get height of given floor.
278  *
279  * \param floor floor
280  * \return height
281  */
282  double getHeight(const int floor) const
283  {
284  if (floor == 0) {
285 
286  if (has_hydrophone) {
287  return hydrophone.getZ();
288  }
289 
290  } else if (this->has(floor)) {
291 
292  return this->get(floor).getHeight();
293  }
294 
295  THROW(JValueOutOfRange, "Invalid floor " << floor);
296  }
297 
298 
299  /**
300  * Get position of given floor according to given string model parameters.
301  *
302  * \param parameters parameters
303  * \param floor floor
304  * \return position
305  */
307  const int floor) const
308  {
309  if (floor == 0) {
310 
311  if (has_hydrophone) {
312  return this->getPosition() + hydrophone.getPosition();
313  }
314 
315  } else {
316 
317  if (this->has(floor)) {
318  return this->getPosition() + getPosition(parameters, mechanics, this->get(floor).getHeight());
319  }
320  }
321 
322  THROW(JValueOutOfRange, "Invalid floor " << floor);
323  }
324 
325 
326  /**
327  * Get position of given floor according to default string model parameters.
328  *
329  * \param floor floor
330  * \return position
331  */
332  JPosition3D getPosition(const int floor) const
333  {
334  return getPosition(JMODEL::JString(), floor);
335  }
336 
337 
338  /**
339  * Get distance between given position and floor according to given string model parameters.
340  *
341  * \param parameters parameters
342  * \param position position
343  * \param floor floor
344  * \return distance
345  */
347  const JVector3D& position,
348  const int floor) const
349  {
350  return this->getPosition(parameters, floor).getDistance(position);
351  }
352 
353 
354  /**
355  * Get model gradient of distance between given position and floor according to given string model parameters.
356  *
357  * \param parameters parameters
358  * \param position position
359  * \param floor floor
360  * \return gradient
361  */
363  const JVector3D& position,
364  const int floor) const
365  {
366  if (floor == 0) {
367 
368  return JMODEL::JString();
369 
370  } else if (this->has(floor)) {
371 
372  const JPosition3D pos = this->getPosition(parameters, floor);
373  const double height = this->get(floor).getHeight();
374  const double h1 = height * (1.0 + parameters.vs);
375  const double z1 = this->mechanics.getHeight(h1);
376 
377  const double tx = parameters.tx;
378  const double ty = parameters.ty;
379  const double tz = sqrt(1.0 - tx*tx - ty*ty);
380 
381  const double dx = pos.getX() - position.getX();
382  const double dy = pos.getY() - position.getY();
383  const double dz = pos.getZ() - position.getZ();
384 
385  const double D = sqrt(dx*dx + dy*dy + dz*dz);
386  const double vw = 1.0 - mechanics.a * mechanics.b / (1.0 - mechanics.a*h1);
387  const double vs = 1.0 + 0.5 * parameters.getLengthSquared() * vw;
388 
389  return JMODEL::JString(z1 * dx / D - height * (tx / tz) * dz / D,
390  z1 * dy / D - height * (ty / tz) * dz / D,
391  h1*h1 * dx / D,
392  h1*h1 * dy / D,
393  height * vw * (tx * dx + ty * dy) / D + h1 * (dz / vs) / D);
394 
395  } else {
396 
397  THROW(JValueOutOfRange, "Invalid floor " << floor);
398  }
399  }
400 
401 
402  /**
403  * Write string parameters to output stream.
404  *
405  * \param out output stream
406  * \param string string
407  * \return output stream
408  */
409  friend inline std::ostream& operator<<(std::ostream& out, const JString& string)
410  {
411  using namespace std;
412 
413  for (JString::const_iterator i = string.begin(); i != string.end(); ++i) {
414  out << setw(2) << i->first << ' '
415  << FIXED(7,3) << i->second << " | "
416  << string.getPosition(i->first) << ' '
417  << string.mechanics << endl;
418  }
419 
420  return out;
421  }
422 
423 
424  /**
425  * Hydrophone.
426  *
427  * The position of the hydrophone is relative to the reference position of the string.
428  */
431 
432  /**
433  * Mechanical data.
434  */
436  };
437 
438 
439  /**
440  * Detector geometry.
441  */
442  struct JDetector :
443  public JHashMap<int, JString>
444  {
445  /**
446  * Default constructor.
447  */
449  {}
450 
451 
452  /**
453  * Constructor.
454  *
455  * Note that the positions of the base modules correspond to the reference position of the string.\n
456  * As a consequence, a base module (i.e.\ floor = 0) is required for each string.\n
457  * Missing base modules should therefore be added beforehand (e.g.\ using application JDetectorDB.cc).
458  *
459  * Note that if the position of a hydrophone is not available,
460  * it is assumed that there is no hydrophone on that string.\n
461  * If the position of the hydrophone is manually set,
462  * the corresponding parameter JString::has_hydrophone should be set to <tt>true</tt>.
463  *
464  * \param detector detector
465  * \param hydrophones container with data of hydrophones
466  */
469  {
470  using namespace std;
471  using namespace JPP;
472 
473  map<int, vector<module_type> > buffer; // string -> modules
474 
475  for (JDETECTOR::JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
476  buffer[module->getString()].push_back(module_type(module->getLocation(), module->getPosition()));
477  }
478 
479  for (map<int, vector<module_type> >::iterator i = buffer.begin(); i != buffer.end(); ++i) {
480 
481  sort(i->second.begin(), i->second.end(), make_comparator(&module_type::getFloor));
482 
483  if (i->second[0].getFloor() == 0) {
484 
485  vector<module_type>::iterator p = i->second.begin();
486 
487  ++p;
488 
489  (*this)[i->first] = JGEOMETRY::JString(i->second[0].getPosition(), p, i->second.end(), getMechanics(i->first));
490 
491  try {
492 
493  (*this)[i->first].hydrophone = getPosition(hydrophones.begin(),
494  hydrophones.end(),
496 
497  (*this)[i->first].has_hydrophone = true;
498  }
499  catch(const exception&) {
500  (*this)[i->first].has_hydrophone = false;
501  }
502 
503  } else {
504 
505  THROW(JNoValue, "No floor 0 in string " << i->first << "; use e.g. JDetectorDB -W.");
506  }
507  }
508  }
509 
510 
511  /**
512  * Check if this detector has given string.
513  *
514  * \param string string
515  * \return true if string present; else false
516  */
517  bool hasString(int string) const
518  {
519  return this->has(string);
520  }
521 
522 
523  /**
524  * Check if this detector has given location.
525  *
526  * \param location location
527  * \return true if location present; else false
528  */
529  bool hasLocation(const JLocation& location) const
530  {
531  return this->hasString(location.getString()) && (*this)[location.getString()].hasFloor(location.getFloor());
532  }
533 
534 
535  /**
536  * Write detector parameters to output stream.
537  *
538  * \param out output stream
539  * \param detector detector
540  * \return output stream
541  */
542  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
543  {
544  using namespace std;
545 
546  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
547  out << setw(4) << i->first << endl << i->second;
548  }
549 
550  return out;
551  }
552 
553 
554  /**
555  * Auxiliary data structure for module location and position.
556  */
557  struct module_type :
558  public JLocation,
559  public JPosition3D
560  {
561  /**
562  * Constructor.
563  *
564  * \param location module location
565  * \param position module position
566  */
567  module_type(const JLocation& location,
568  const JPosition3D& position) :
569  JLocation (location),
570  JPosition3D(position)
571  {}
572 
573 
574  /**
575  * Less-than operator.
576  *
577  * \param first first module
578  * \param second second module
579  * \return true if floor of first module less than that of second; else false
580  */
581  friend inline bool operator<(const module_type& first, const module_type& second)
582  {
583  return first.getFloor() < second.getFloor();
584  }
585  };
586  };
587  }
588 
589 
590  /**
591  * Type definition of detector geometry.
592  */
594 }
595 
596 #endif
static double getHeight(const JMODEL::JString &parameters, const JMechanics &mechanics, const double length, const double precision=PRECISION_M)
Get approximate height of string.
Definition: JGeometry.hh:157
then cat $TRIPOD_INITIAL<< EOF1 256877.5 4743716.7-2438.42 256815.5 4743395.0-2435.53 257096.2 4743636.0-2439.5EOFfiJEditDetector-a $DETECTOR_INITIAL-s"-1 addz -6.9"-o $DETECTOReval`JPrintDetector-a $DETECTOR-O SUMMARY`for STRING in ${STRINGS[*]};do set_variable MODULE`getModule-a $DETECTOR-L"$STRING 0"`JEditDetector-a $DETECTOR-M"$MODULE setz -2.9"-o $DETECTORdonecp-p $TRIPOD_INITIAL $TRIPODJAcoustics.sh $DETECTOR_IDcat > acoustics_trigger_parameters txt<< EOFQ=0.0;TMax_s=0.020;numberOfHits=90;EOFJAcousticsEventBuilder.sh $DETECTOR $RUNS[*]INPUT_FILES=(`ls KM3NeT_ ${(l:8::0::0:) DETECTOR_ID}_0 *${^RUNS}_event.root`) cd $WORKDIRif[!$HOMEDIR-ef $WORKDIR];then cp-p $HOMEDIR/$DETECTOR $WORKDIR cp-p $HOMEDIR/$TRIPOD $WORKDIR cp-p $HOMEDIR/${^INPUT_FILES}$WORKDIR cp-p $HOMEDIR/{acoustics_fit_parameters, acoustics_trigger_parameters, disable, hydrophone, mechanics, sound_velocity, tripod, waveform}.txt $WORKDIRfisource $JPP_DIR/examples/JAcoustics/acoustics-fit-toolkit.shtimer_startinitialise stage_1B > &stage log
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 JPosition3D getPosition(const JMODEL::JString &parameters, const JMechanics &mechanics, const double height)
Get position at given height according to given string model parameters and mechanics.
Definition: JGeometry.hh:190
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:696
JString(const JVector3D &position, T __begin, T __end, const JMechanics &mechanics)
Constructor.
Definition: JGeometry.hh:247
*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:206
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
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:282
JFloor()
Default constructor.
Definition: JGeometry.hh:62
JString(const JVector3D &position)
Constructor.
Definition: JGeometry.hh:219
Data structure for detector geometry and calibration.
Auxiliary data structure for module location and position.
Definition: JGeometry.hh:557
static constexpr double PRECISION_M
precision of height evaluation [m]
Definition: JGeometry.hh:128
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
friend std::ostream & operator<<(std::ostream &out, const JString &string)
Write string parameters to output stream.
Definition: JGeometry.hh:409
bool hasString(int string) const
Check if this detector has given string.
Definition: JGeometry.hh:517
JGEOMETRY::JDetector JGeometry
Type definition of detector geometry.
Definition: JGeometry.hh:593
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:362
Exception for missing value.
Definition: JException.hh:198
module_type(const JLocation &location, const JPosition3D &position)
Constructor.
Definition: JGeometry.hh:567
Detector file.
Definition: JHead.hh:224
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:346
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static double getLength(const JMODEL::JString &parameters, const JMechanics &mechanics, const double height)
Get approximate length of string.
Definition: JGeometry.hh:138
double a
0 &lt;= a &lt; (maximal height)⁻1; [m^-1]
Definition: JMechanics.hh:100
double getHeight(const double height) const
Get effective height for given actual height.
Definition: JMechanics.hh:68
double getLengthSquared() const
Get length squared.
double getY() const
Get y position.
Definition: JVector3D.hh:104
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
double getLength() const
Get length.
Definition: JVector3D.hh:246
Logical location of module.
I/O manipulators.
double b
0 &lt;= b; [m]
Definition: JMechanics.hh:101
JDetector(const JDETECTOR::JDetector &detector, const std::vector< JDETECTOR::JHydrophone > &hydrophones=std::vector< JDETECTOR::JHydrophone >())
Constructor.
Definition: JGeometry.hh:467
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:542
JPosition3D hydrophone
Hydrophone.
Definition: JGeometry.hh:429
JMechanics mechanics
Mechanical data.
Definition: JGeometry.hh:435
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:581
JDetector()
Default constructor.
Definition: JGeometry.hh:448
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:332
bool hasLocation(const JLocation &location) const
Check if this detector has given location.
Definition: JGeometry.hh:529
Auxiliary data structure to list files in directory.
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:267
JPosition3D getPosition(const JMODEL::JString &parameters, const int floor) const
Get position of given floor according to given string model parameters.
Definition: JGeometry.hh:306
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:39
container_type::iterator iterator
Definition: JHashMap.hh:88