Jpp  18.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JFremantle.cc File Reference

Application to make a global fit of the detector geometry to acoustic data. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <algorithm>
#include <limits>
#include <type_traits>
#include <functional>
#include <future>
#include <mutex>
#include <thread>
#include <queue>
#include "TROOT.h"
#include "TFile.h"
#include "JLang/JPredicate.hh"
#include "JLang/JComparator.hh"
#include "JLang/JComparison.hh"
#include "JLang/JFileStream.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JTripod.hh"
#include "JDetector/JTransmitter.hh"
#include "JDetector/JModule.hh"
#include "JDetector/JHydrophone.hh"
#include "JTools/JHashMap.hh"
#include "JTools/JRange.hh"
#include "JTools/JQuantile.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JSingleFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JMeta.hh"
#include "JAcoustics/JSoundVelocity.hh"
#include "JAcoustics/JEmitter.hh"
#include "JAcoustics/JAcousticsToolkit.hh"
#include "JAcoustics/JHit.hh"
#include "JAcoustics/JFitParameters.hh"
#include "JAcoustics/JKatoomba_t.hh"
#include "JAcoustics/JEvent.hh"
#include "JAcoustics/JSuperEvt.hh"
#include "JAcoustics/JSuperEvtToolkit.hh"
#include "JAcoustics/JSupport.hh"
#include "JAcoustics/JTransmission_t.hh"
#include "Jeep/JContainer.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Application to make a global fit of the detector geometry to acoustic data.


Author
mdejong

Definition in file JFremantle.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 221 of file JFremantle.cc.

222 {
223  using namespace std;
224  using namespace JPP;
225 
226  typedef JContainer< vector<JTripod> > tripods_container;
227  typedef JContainer< vector<JTransmitter> > transmitters_container;
228  typedef JContainer< vector<JHydrophone> > hydrophones_container;
229  typedef JContainer< set<JTransmission_t> > disable_container;
230 
233  string detectorFile;
234  JLimit_t& numberOfEvents = inputFile.getLimit();
235  JSoundVelocity V = getSoundVelocity; // default sound velocity
236  tripods_container tripods; // tripods
237  transmitters_container transmitters; // transmitters
238  hydrophones_container hydrophones; // hydrophones
239  JFitParameters parameters; // fit parameters
240  bool unify; // unify weighing of pings
241  disable_container disable; // disable tansmissions
242  size_t jobs; // number of parallel jobs
243  double Tmax_s; // deadtime [s]
244  int debug;
245 
246  try {
247 
248  JParser<> zap("Application to fit position calibration model to acoustic data.");
249 
250  zap['f'] = make_field(inputFile, "output of JAcousticEventBuilder[.sh]");
251  zap['o'] = make_field(outputFile) = "";
252  zap['n'] = make_field(numberOfEvents) = JLimit::max();
253  zap['a'] = make_field(detectorFile);
255  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
256  zap['T'] = make_field(tripods, "tripod data");
257  zap['Y'] = make_field(transmitters, "transmitter data") = JPARSER::initialised();
258  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
259  zap['M'] = make_field(getMechanics, "mechanics data") = JPARSER::initialised();
260  zap['u'] = make_field(unify, "unify weighing of pings");
261  zap['!'] = make_field(disable, "disable transmission") = JPARSER::initialised();
262  zap['N'] = make_field(jobs, "number of parallel jobs") = 1;
263  zap['D'] = make_field(Tmax_s, "deadtime [s]") = 100.0e-3;
264  zap['d'] = make_field(debug) = 1;
265 
266  zap(argc, argv);
267  }
268  catch(const exception &error) {
269  FATAL(error.what() << endl);
270  }
271 
272  ROOT::EnableThreadSafety();
273 
274  const int sleep_us = 100; // sleep time [us]
275 
277 
278  try {
279  load(detectorFile, detector);
280  }
281  catch(const JException& error) {
282  FATAL(error);
283  }
284 
285  JHashMap<int, JLocation> receivers;
286  JHashMap<int, JEmitter> emitters;
287 
288  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
289  receivers[i->getID()] = i->getLocation();
290  }
291 
292  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
293  emitters[i->getID()] = JEmitter(i->getID(),
294  i->getUTMPosition() - detector.getUTMPosition());
295  }
296 
297  for (transmitters_container::const_iterator i = transmitters.begin(); i != transmitters.end(); ++i) {
298  try {
299  emitters[i->getID()] = JEmitter(i->getID(),
300  i->getPosition() + detector.getModule(i->getLocation()).getPosition());
301  }
302  catch(const exception&) {
303  continue; // if no module available, discard transmitter
304  }
305  }
306 
307  V.set(detector.getUTMZ()); // sound velocity at detector depth
308 
309  JGeometry geometry(detector, hydrophones);
310  JKatoomba_t katoomba(geometry, V, parameters);
311 
313 
316 
317  if (inputFile.size() > 1u) { // sort input files
318 
319  map<double, string> zmap;
320 
321  for (const string& file_name : inputFile) {
322 
323  STATUS(file_name << '\r'); DEBUG(endl);
324 
325  for (JSingleFileScanner<JEvent> in(file_name, 1); in.hasNext(); ) {
326 
327  const JEvent* evt = in.next();
328 
329  if (JFremantle::oid == "")
330  JFremantle::oid = evt->getOID();
331  else if (JFremantle::oid != evt->getOID()) // consistency check
332  FATAL("Invalid detector identifier " << evt->getOID() << " != " << JFremantle::oid << endl);
333 
334  if (!evt->empty()) {
335  zmap[evt->begin()->getToE()] = file_name;
336  }
337  }
338  }
339  STATUS(endl);
340 
341  inputFile.clear();
342 
343  for (map<double, string>::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
344  inputFile.push_back(i->second);
345  }
346  }
347 
348  const JRange<double> unit(0.0, 1.0);
349 
350  if (outputFile.getFilename() != "" &&
351  outputFile.getFilename() != "--") {
352 
353  outputFile.open();
354 
355  JFremantle::output = &outputFile;
356  }
357  /*
358  outputFile.put(JMeta(argc, argv));
359  outputFile.put(parameters);
360  */
361 
362  try {
363 
364  JFremantle fremantle(katoomba, jobs);
365 
366  typedef deque<JEvent> buffer_type;
367 
368  for (buffer_type zbuf; inputFile.hasNext(); ) {
369 
370  STATUS(inputFile.getFilename() << '\r'); DEBUG(endl);
371 
372  // read one file at a time
373 
374  for (const string file_name = inputFile.getFilename(); inputFile.hasNext() && file_name == inputFile.getFilename(); ) {
375 
376  const JEvent* evt = inputFile.next();
377 
378  if (emitters.has(evt->getID())) {
379  zbuf.push_back(*evt);
380  }
381  }
382 
383  sort(zbuf.begin(), zbuf.end()); // sort according first time-of-emission
384 
385  for (buffer_type::iterator p = zbuf.begin(), q; p != zbuf.end(); p = q) {
386 
387  for (q = p; ++q != zbuf.end() && q->begin()->getToE() <= p->rbegin()->getToE() + parameters.Tmax_s; ) {}
388 
389  if (q == zbuf.end()) {
390 
391  if (inputFile.hasNext()) {
392 
393  zbuf.erase(zbuf.begin(), p); // remove processed data and continue reading
394 
395  break;
396  }
397  }
398 
399  JEvent::overlap(p, q, Tmax_s); // empty overlapping events
400 
401  if (getNumberOfEmitters(p,q) >= parameters.Nmin) {
402 
403  map<int, int> numberOfPings;
404 
405  for (buffer_type::const_iterator i = p; i != q; ++i) {
406  numberOfPings[i->getID()] += 1;
407  }
408 
409  int minimum_number_of_pings = numeric_limits<int>::max();
410 
411  for (map<int, int>::const_iterator i = numberOfPings.begin(); i != numberOfPings.end(); ++i) {
412  minimum_number_of_pings = min(minimum_number_of_pings, i->second);
413  }
414 
415  set<int> buffer;
416  data_type data;
417 
418  for (buffer_type::iterator evt = p; evt != q; ++evt) {
419 
420  sort(evt->begin(), evt->end(), JKatoomba<>::compare);
421 
422  JEvent::iterator __end = unique(evt->begin(), evt->end(), make_comparator(&JTransmission::getID, JComparison::eq()));
423 
424  const JEmitter& emitter = emitters[evt->getID()];
425  const double weight = (unify ? (double) minimum_number_of_pings / (double) numberOfPings[evt->getID()] : 1.0);
426 
427  for (JEvent::const_iterator i = evt->begin(); i != __end; ++i) {
428 
429  if (disable.count(JTransmission_t(evt->getID(), i->getID())) == 0 &&
430  disable.count(JTransmission_t(-1, i->getID())) == 0) {
431 
432  if (receivers.has(i->getID()) && geometry.hasLocation(receivers[i->getID()]) && i->getQ() >= parameters.Qmin * (unit(parameters.Qmin) ? i->getW() : 1.0)) {
433 
434  data.push_back(JHit(emitter,
435  distance(p,evt),
436  receivers[i->getID()],
437  i->getToA(),
438  parameters.sigma_s,
439  weight));
440 
441  buffer.insert(evt->getID());
442  }
443  }
444  }
445  }
446 
447  if (buffer.size() >= parameters.Nmin) {
448 
449  while (fremantle.backlog() > jobs) {
450  this_thread::sleep_for(chrono::microseconds(sleep_us));
451  }
452 
453  fremantle.enqueue(data);
454  }
455  }
456  }
457  }
458  STATUS(endl);
459  }
460  catch(const exception& error) {
461  FATAL("main " << error.what());
462  }
463  /*
464  JMultipleFileScanner<JMeta> io(inputFile);
465 
466  io >> outputFile;
467  */
468  outputFile.close();
469 
470  JFileOutputStream(3) << SCIENTIFIC(1,10) << JFremantle::Q.getMean(numeric_limits<float>::max()) << endl;
471 }
Worker class for complete fit procedure of acoustic model.
Definition: JKatoomba_t.hh:29
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1514
General exception.
Definition: JException.hh:23
Q(UTCMax_s-UTCMin_s)-livetime_s
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
static JDetectorMechanics getMechanics
Function object to get string mechanics.
Definition: JMechanics.hh:243
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:72
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:89
*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 fatal Number of tripods
Definition: JFootprint.sh:45
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
string outputFile
Acoustics hit.
size_t getNumberOfEmitters(T __begin, T __end)
Get number of emitters.
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
static const JSoundVelocity getSoundVelocity(1541.0,-17.0e-3,-2000.0)
Function object for velocity of sound.
Detector file.
Definition: JHead.hh:226
Acoustic emitter.
Definition: JEmitter.hh:27
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition: JContainer.hh:39
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
const std::string & getOID() const
Get detector identifier.
Streaming of output.
Definition: JFileStream.hh:46
JPosition3D getPosition(const Vec &pos)
Get position.
Implementation for depth dependend velocity of sound.
#define FATAL(A)
Definition: JMessage.hh:67
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
General purpose class for object reading from a list of file names.
Acoustic event.
Object reading from a list of files.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:84
bool overlap(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Test overlap between ranges.
Definition: JRange.hh:641
do echo n Creating graphics for string $STRING for((FLOOR=$FIRST_FLOOR;$FLOOR<=$LAST_FLOOR;FLOOR+=1))
do set_variable DETECTOR_TXT $WORKDIR detector
int getID() const
Get emitter identifier.
double u[N+1]
Definition: JPolint.hh:776
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
Acoustic transmission identifier.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
Template definition of fit function of acoustic model.
Definition: JKatoomba.hh:76
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62