Jpp  18.4.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Namespaces | Functions
JAcousticsTriggerProcessor.cc File Reference

Main program to trigger acoustic data. More...

#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include "TROOT.h"
#include "TFile.h"
#include "JLang/JComparator.hh"
#include "JDB/JToAshort.hh"
#include "JDB/JSupport.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JHydrophone.hh"
#include "JLang/JPredicate.hh"
#include "JTools/JRange.hh"
#include "JTools/JQuantile.hh"
#include "JTools/JHashMap.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JMeta.hh"
#include "JAcoustics/JTransmission.hh"
#include "JAcoustics/JReceiver.hh"
#include "JAcoustics/JSoundVelocity.hh"
#include "JAcoustics/JAcousticsToolkit.hh"
#include "JAcoustics/JAcousticsSupportkit.hh"
#include "JAcoustics/JTriggerParameters.hh"
#include "JAcoustics/JEvent.hh"
#include "JAcoustics/JTriggerOutput.hh"
#include "JAcoustics/JSupport.hh"
#include "Jeep/JContainer.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "JTrigger/JMatch.hh"
#include "JTrigger/JAlgorithm.hh"

Go to the source code of this file.

Classes

struct  JACOUSTICS::hit_type
 Acoustic hit. More...
 
class  JACOUSTICS::JMatch3D
 3D match criterion for acoustic signals. More...
 
struct  JACOUSTICS::JEventOverlap
 Match of two events considering overlap in time. More...
 

Namespaces

 JACOUSTICS
 Auxiliary classes and methods for acoustic position calibration.
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Main program to trigger acoustic data.

An acoustic event is based on coincidences between times of arrival.
If the number of coincident times of arrival exceeds a preset minimum, the event is triggered and subsequently stored in the output file.

Author
mdejong

Definition in file JAcousticsTriggerProcessor.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 182 of file JAcousticsTriggerProcessor.cc.

183 {
184  using namespace std;
185  using namespace JPP;
186 
188 
190 
192  JLimit_t& numberOfEvents = inputFile.getLimit();
195  JSoundVelocity V = getSoundVelocity; // default sound velocity
196  string detectorFile;
197  hydrophones_container hydrophones; // hydrophones
198  double precision;
199  int ID;
200  set<int> waveforms;
201  int debug;
202 
203  try {
204 
205  JParser<> zap("Main program to trigger acoustic data.");
206 
207  zap['f'] = make_field(inputFile, "output of JConvertDB -q toashort");
208  zap['n'] = make_field(numberOfEvents) = JLimit::max();
209  zap['@'] = make_field(parameters, "trigger parameters");
210  zap['o'] = make_field(outputFile, "output file") = "event.root";
211  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
212  zap['a'] = make_field(detectorFile, "detector file");
213  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
214  zap['p'] = make_field(precision, "precision time-of-arrival") = 1.0e-6;
215  zap['D'] = make_field(ID) = 10000;
216  zap['w'] = make_field(waveforms) = JPARSER::initialised();
217  zap['d'] = make_field(debug) = 1;
218 
219  zap(argc, argv);
220  }
221  catch(const exception &error) {
222  FATAL(error.what() << endl);
223  }
224 
225 
227 
228  try {
229  load(detectorFile, detector);
230  }
231  catch(const JException& error) {
232  FATAL(error);
233  }
234 
235  V.set(detector.getUTMZ());
236 
237 
238  const int FACTORY_LIMIT = 10000;
239  const double TMaxExtra_s = 1.0e-3;
240 
241  const JMatch3D match(V, TMaxExtra_s);
242  const JEventOverlap overlap; // (parameters.TMax_s);
243 
244 
245  JHashMap<int, JReceiver> receivers;
246 
247  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
248 
249  JPosition3D pos(0.0, 0.0, 0.0);
250 
251  if (i->getFloor() == 0) { // get relative position of hydrophone
252 
253  try {
254  pos = getPosition(hydrophones.begin(),
255  hydrophones.end(),
256  make_predicate(&JHydrophone::getString, i->getString()));
257  }
258  catch(const exception&) {
259  continue; // if no position available, discard hydrophone
260  }
261  }
262 
263  receivers[i->getID()] = JReceiver(i->getID(),
264  i->getPosition() + pos,
265  i->getT0() * 1.0e-9);
266  }
267 
268 
269  outputFile.open();
270 
271  if (!outputFile.is_open()) {
272  FATAL("Error opening file " << outputFile << endl);
273  }
274 
275  outputFile.put(JMeta(argc, argv));
276  outputFile.put(parameters);
277 
278 
279  // input data
280 
281  string oid; // detector identifier
282 
283  typedef vector<hit_type> buffer_type; // data type
284 
285  map<int, buffer_type> f1; // receiver -> data
286 
287  while (inputFile.hasNext()) {
288 
289  if (inputFile.getCounter()%1000 == 0) {
290  STATUS("counter: " << setw(8) << inputFile.getCounter() << '\r' << flush); DEBUG(endl);
291  }
292 
293  JToAshort* parameters = inputFile.next();
294 
295  if (oid == "") {
296  oid = parameters->DETID;
297  }
298 
299  if (oid != parameters->DETID) { // consistency check
300  FATAL("Invalid detector identifier " << parameters->DETID << " != " << oid << endl);
301  }
302 
303  if (waveforms.empty() || waveforms.count(parameters->EMITTERID)) {
304 
305  if (receivers.has(parameters->DOMID)) {
306 
307  const JReceiver& receiver = receivers[parameters->DOMID];
308 
309  const JTransmission transmission(parameters->RUNNUMBER,
310  parameters->DOMID,
311  parameters->QUALITYFACTOR,
312  parameters->QUALITYNORMALISATION,
313  parameters->UNIXTIMEBASE + receiver.getT(parameters->TOA_S),
314  parameters->UNIXTIMEBASE + receiver.getT(parameters->TOA_S));
315 
316  f1[receiver.getID()].push_back(hit_type(receiver.getPosition(), transmission));
317  }
318  }
319  }
320  STATUS(endl);
321 
322 
323  // filter similar hits
324 
325  for (map< int, buffer_type>::iterator receiver = f1.begin(); receiver != f1.end(); ++receiver) {
326 
327  buffer_type& buffer = receiver->second;
328 
329  sort(buffer.begin(), buffer.end(), JTransmission::compare(precision));
330 
331  buffer.erase(unique(buffer.begin(), buffer.end(), JTransmission::equals(precision)), buffer.end());
332  }
333 
334 
336 
337  for (map<int, buffer_type>::iterator receiver = f1.begin(); receiver != f1.end(); ++receiver) {
338 
339  // selection based on quality
340 
341  buffer_type& f2 = receiver->second;
342  double Qmin = parameters.Q;
343 
344  if (parameters.Q > 0.0 && parameters.Q < 1.0) {
345 
346  const JQuantile Q1("quality", make_array(f2.begin(), f2.end(), &JTransmission::getQ), true);
347 
348  Qmin = Q1.getQuantile(parameters.Q);
349  }
350 
351  buffer_type::iterator __end = partition(f2.begin(), f2.end(), make_predicate(&JTransmission::getQ, Qmin, JComparison::gt()));
352 
353  copy(f2.begin(), __end, back_inserter(data));
354  }
355 
356 
357  sort(data.begin(), data.end(), make_comparator(&hit_type::getToA, JComparison::lt())); // sort according time-of-arrival
358 
359 
360  buffer_type buffer(FACTORY_LIMIT); // local buffer of hits
361  JEvent out[2]; // FIFO of events
362  int number_of_events = 0;
363 
364  for (buffer_type::const_iterator p = data.begin(); p != data.end(); ++p) {
365 
366  if (distance(data.cbegin(),p)%1000 == 0) {
367  STATUS("processed: " << FIXED(5,1) << (double) (100 * distance(data.cbegin(),p)) / (double) data.size() << "%" << '\r' << flush); DEBUG(endl);
368  }
369 
370  buffer_type::const_iterator q = p;
371 
372  while (++q != data.end() && q->getToA() - p->getToA() <= parameters.TMax_s) {}
373 
374  if (distance(p,q) >= parameters.numberOfHits) {
375 
376  if (distance(p,q) < FACTORY_LIMIT) {
377 
378  buffer_type::iterator root = buffer.begin();
379  buffer_type::iterator __p = buffer.begin();
380  buffer_type::iterator __q = buffer.begin();
381 
382  *root = *p;
383 
384  ++__p;
385  ++__q;
386 
387  for (buffer_type::const_iterator i = p; ++i != q; ) {
388  if (match(*p,*i)) {
389  *__q = *i;
390  ++__q;
391  }
392  }
393 
394  if (distance(root,__q) >= parameters.numberOfHits) {
395 
396  __q = clusterize(__p, __q, match, parameters.numberOfHits - 1);
397 
398  if (distance(root,__q) >= parameters.numberOfHits) {
399  out[1] = JEvent(oid, ++number_of_events, ID, root, __q);
400  }
401  }
402 
403  } else {
404 
405  out[1] = JEvent(oid, ++number_of_events, ID, p, q);
406  }
407 
408  if (!overlap(out[0],out[1])) {
409 
410  if (!out[0].empty()) {
411  outputFile.put(out[0]); // write
412  }
413 
414  out[0] = out[1]; // shift
415 
416  } else {
417 
418  out[0].merge(out[1]); // merge
419  }
420 
421  out[1].clear();
422  }
423  }
424 
425  if (!out[0].empty()) {
426  outputFile.put(out[0]); // write
427  }
428  STATUS(endl);
429 
430  STATUS("triggers: " << setw(7) << number_of_events << endl);
431 
432  JMultipleFileScanner<JMeta> io(inputFile);
433 
434  io >> outputFile;
435 
436  outputFile.close();
437 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
double UNIXTIMEBASE
[s]
Definition: JToAshort.hh:25
Utility class to parse command line options.
Definition: JParser.hh:1514
General exception.
Definition: JException.hh:24
Acoustic receiver.
Definition: JReceiver.hh:27
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
Greater than.
Definition: JComparison.hh:73
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
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
std::string DETID
constraint
Definition: JToAshort.hh:22
*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
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
static const int FACTORY_LIMIT
Bit indicating max nhits reached in trigger.
Definition: trigger.hh:16
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
string outputFile
JMODEL::JString getString(const JFit &fit)
Get model parameters of string.
std::vector< JHitW0 > buffer_type
hits
Definition: JPerth.cc:67
Type list.
Definition: JTypeList.hh:22
JFIT::JEvent JEvent
Definition: JHistory.hh:353
const JPolynome f1(1.0, 2.0, 3.0)
Function.
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.
Acoustic transmission.
Detector file.
Definition: JHead.hh:226
JContainer< std::vector< JHydrophone > > hydrophones_container
Definition: JSydney.cc:78
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 array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
int getID() const
Get identifier.
Definition: JObjectID.hh:50
JPosition3D getPosition(const Vec &pos)
Get position.
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR &dev null set_variable DETECTOR $JPP_DATA km3net_reference detx set_variable NUMBER_OF_STRINGS set_variable ID if do_usage *then usage $script[detector file[identifier]] fi case set_variable ID
Definition: JDetector.sh:24
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
double getQ(const double D_m, const double f_kHz, const double d_m)
Get relative quality for given frequency at given distance.
Implementation for depth dependend velocity of sound.
#define FATAL(A)
Definition: JMessage.hh:67
double getT(const double t_s) const
Get corrected time.
Definition: JReceiver.hh:72
Auxiliary data structure for average.
Definition: JKatoomba_t.hh:76
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.
Auxiliary class to compare transmissions.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
Acoustic event.
double TOA_S
[s]
Definition: JToAshort.hh:28
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:84
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
Definition: JMathToolkit.hh:86
bool overlap(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Test overlap between ranges.
Definition: JRange.hh:641
do set_variable DETECTOR_TXT $WORKDIR detector
Match of two events considering overlap in time.
int EMITTERID
waveform identifier
Definition: JToAshort.hh:27
static struct JTRIGGER::clusterize clusterize
3D match criterion.
Definition: JMatch3D.hh:29
do JPlot2D f $WORKDIR canberra[${EMITTER}] root
Definition: JCanberra.sh:136
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62