Jpp  17.3.1
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 <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 "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 "JSupport/JMultipleFileScanner.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.hh"
#include "JAcoustics/JEvent.hh"
#include "JAcoustics/JEvt.hh"
#include "JAcoustics/JEvtToolkit.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 214 of file JFremantle.cc.

215 {
216  using namespace std;
217  using namespace JPP;
218 
219  typedef JContainer< vector<JTripod> > tripods_container;
220  typedef JContainer< vector<JTransmitter> > transmitters_container;
221  typedef JContainer< vector<JHydrophone> > hydrophones_container;
222  typedef JContainer< set<JTransmission_t> > disable_container;
223 
226  string detectorFile;
227  JLimit_t& numberOfEvents = inputFile.getLimit();
228  JSoundVelocity V = getSoundVelocity; // default sound velocity
229  tripods_container tripods; // tripods
230  transmitters_container transmitters; // transmitters
231  hydrophones_container hydrophones; // hydrophones
232  JFitParameters parameters; // fit parameters
233  bool unify; // unify weighing of pings
234  disable_container disable; // disable tansmissions
235  size_t jobs; // number of parallel jobs
236  int sleep_us; // sleep time [us]
237  double Tmax_s; // deadtime [s]
238  int debug;
239 
240  try {
241 
242  JParser<> zap("Application to fit position calibration model to acoustic data.");
243 
244  zap['f'] = make_field(inputFile, "output of JAcousticEventBuilder[.sh]");
245  zap['o'] = make_field(outputFile);
246  zap['n'] = make_field(numberOfEvents) = JLimit::max();
247  zap['a'] = make_field(detectorFile);
249  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
250  zap['T'] = make_field(tripods, "tripod data");
251  zap['Y'] = make_field(transmitters, "transmitter data") = JPARSER::initialised();
252  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
253  zap['M'] = make_field(getMechanics, "mechanics data") = JPARSER::initialised();
254  zap['u'] = make_field(unify, "unify weighing of pings");
255  zap['!'] = make_field(disable, "disable transmission") = JPARSER::initialised();
256  zap['N'] = make_field(jobs, "number of parallel jobs") = 1;
257  zap['s'] = make_field(sleep_us, "sleep time [us]") = 100;
258  zap['D'] = make_field(Tmax_s, "deadtime [s]") = 100.0e-3;
259  zap['d'] = make_field(debug) = 1;
260 
261  zap(argc, argv);
262  }
263  catch(const exception &error) {
264  FATAL(error.what() << endl);
265  }
266 
267  ROOT::EnableThreadSafety();
268 
269 
271 
272  try {
273  load(detectorFile, detector);
274  }
275  catch(const JException& error) {
276  FATAL(error);
277  }
278 
279  JHashMap<int, JLocation> receivers;
280  JHashMap<int, JEmitter> emitters;
281 
282  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
283  receivers[i->getID()] = i->getLocation();
284  }
285 
286  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
287  emitters[i->getID()] = JEmitter(i->getID(),
288  i->getUTMPosition() - detector.getUTMPosition());
289  }
290 
291  for (transmitters_container::const_iterator i = transmitters.begin(); i != transmitters.end(); ++i) {
292  try {
293  emitters[i->getID()] = JEmitter(i->getID(),
294  i->getPosition() + detector.getModule(i->getLocation()).getPosition());
295  }
296  catch(const exception&) {
297  continue; // if no module available, discard transmitter
298  }
299  }
300 
301  V.set(detector.getUTMZ()); // sound velocity at detector depth
302 
303  JGeometry geometry(detector, hydrophones);
304  JKatoomba_t katoomba(geometry, V, parameters);
305 
308 
311 
312  DEBUG(geometry);
313 
314  string oid; // detector identifier
315 
316  { // sort input files
317  map<double, string> zmap;
318 
319  for (const string& file_name : inputFile) {
320 
321  STATUS(file_name << '\r'); DEBUG(endl);
322 
323  for (JMultipleFileScanner<JEvent> in(file_name, 1); in.hasNext(); ) {
324 
325  const JEvent* evt = in.next();
326 
327  if (oid == "")
328  oid = evt->getOID();
329  else if (oid != evt->getOID()) // consistency check
330  FATAL("Invalid detector identifier " << evt->getOID() << " != " << oid << endl);
331 
332  if (!evt->empty()) {
333  zmap[evt->begin()->getToE()] = file_name;
334  }
335  }
336  }
337  STATUS(endl);
338 
339  inputFile.clear();
340 
341  for (map<double, string>::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
342  inputFile.push_back(i->second);
343  }
344  }
345 
346  const JRange<double> unit(0.0, 1.0);
347 
348  outputFile.open();
349 
350  outputFile.put(JMeta(argc, argv));
351  outputFile.put(parameters);
352 
353  try {
354 
355  JFremantle fremantle(oid, katoomba, outputFile, jobs);
356 
357  typedef deque<JEvent> buffer_type;
358 
359  for (buffer_type zbuf; inputFile.hasNext(); ) {
360 
361  STATUS(inputFile.getFilename() << '\r'); DEBUG(endl);
362 
363  // read one file at a time
364 
365  for (const string file_name = inputFile.getFilename(); inputFile.hasNext() && file_name == inputFile.getFilename(); ) {
366 
367  const JEvent* evt = inputFile.next();
368 
369  if (emitters.has(evt->getID())) {
370  zbuf.push_back(*evt);
371  }
372  }
373 
374  sort(zbuf.begin(), zbuf.end()); // sort according first time-of-emission
375 
376  for (buffer_type::iterator p = zbuf.begin(), q; p != zbuf.end(); p = q) {
377 
378  for (q = p; ++q != zbuf.end() && q->begin()->getToE() <= p->rbegin()->getToE() + parameters.Tmax_s; ) {}
379 
380  if (q == zbuf.end()) {
381 
382  if (inputFile.hasNext()) {
383 
384  zbuf.erase(zbuf.begin(), p); // remove processed data and continue reading
385 
386  break;
387  }
388  }
389 
390  overlap(p, q, Tmax_s); // empty overlapping events
391 
392  if (getNumberOfEmitters(p,q) >= parameters.Nmin) {
393 
394  map<int, int> numberOfPings;
395 
396  for (buffer_type::const_iterator i = p; i != q; ++i) {
397  numberOfPings[i->getID()] += 1;
398  }
399 
400  for (map<int, int>::const_iterator i = numberOfPings.begin(); i != numberOfPings.end(); ++i) {
401  DEBUG("Number of pings " << setw(2) << i->first << ' ' << setw(3) << i->second << endl);
402  }
403 
404  int minimum_number_of_pings = numeric_limits<int>::max();
405 
406  for (map<int, int>::const_iterator i = numberOfPings.begin(); i != numberOfPings.end(); ++i) {
407  minimum_number_of_pings = min(minimum_number_of_pings, i->second);
408  }
409 
410  set<int> buffer;
411  data_type data;
412 
413  for (buffer_type::iterator evt = p; evt != q; ++evt) {
414 
415  sort(evt->begin(), evt->end(), compare);
416 
417  JEvent::iterator __end = unique(evt->begin(), evt->end(), make_comparator(&JTransmission::getID, JComparison::eq()));
418 
419  const JEmitter& emitter = emitters[evt->getID()];
420  const double signal = (unify ? (double) minimum_number_of_pings / (double) numberOfPings[evt->getID()] : 1.0);
421 
422  for (JEvent::const_iterator i = evt->begin(); i != __end; ++i) {
423 
424  if (disable.count(JTransmission_t(evt->getID(), i->getID())) == 0 &&
425  disable.count(JTransmission_t(-1, i->getID())) == 0) {
426 
427  if (receivers.has(i->getID()) && geometry.hasLocation(receivers[i->getID()]) && i->getQ() >= parameters.Qmin * (unit(parameters.Qmin) ? i->getW() : 1.0)) {
428 
429  data.push_back(hit_type(emitter,
430  distance(zbuf.begin(),evt),
431  receivers[i->getID()],
432  JPDFGauss(i->getToA(), parameters.sigma_s, signal, parameters.background)));
433 
434  buffer.insert(evt->getID());
435  }
436  }
437  }
438  }
439 
440  if (buffer.size() >= parameters.Nmin) {
441 
442  while (fremantle.backlog() > jobs) {
443  this_thread::sleep_for(chrono::microseconds(sleep_us));
444  }
445 
446  fremantle.enqueue(data);
447  }
448  }
449  }
450  }
451  STATUS(endl);
452  }
453  catch(const exception& error) {
454  FATAL("main " << error.what());
455  }
456 
457  JMultipleFileScanner<JMeta> io(inputFile);
458 
459  io >> outputFile;
460 
461  outputFile.close();
462 }
Worker class for fit function of acoustic model.
Definition: JKatoomba.hh:870
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1517
General exception.
Definition: JException.hh:23
void overlap(T p, T q, const double Tmax_s)
Empty overlapping events.
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
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:1993
const std::string & getOID() const
Get detector identifier.
JPosition3D getPosition(const Vec &pos)
Get position.
static struct JACOUSTICS::@4 compare
Auxiliary data structure to sort transmissions.
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.
Custom probability density function of time-of-arrival.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
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 identifier.
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.
Template definition of fit function of acoustic model.
Definition: JKatoomba.hh:116
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62