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

Main program to trigger events in acoustic data. More...

#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include "TROOT.h"
#include "TFile.h"
#include "km3net-dataformat/definitions/module_status.hh"
#include "JLang/JComparator.hh"
#include "JDB/JToAshort.hh"
#include "JDB/JSupport.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JTripod.hh"
#include "JDetector/JTransmitter.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/JEmitterID.hh"
#include "JAcoustics/JTransceiver.hh"
#include "JAcoustics/JTransmission.hh"
#include "JAcoustics/JAcousticsToolkit.hh"
#include "JAcoustics/JAcousticsSupportkit.hh"
#include "JAcoustics/JTriggerParameters.hh"
#include "JAcoustics/JEvent.hh"
#include "JAcoustics/JEventOverlap.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"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Main program to trigger events in acoustic data.

An acoustic event is based on a coincidence of estimated times of emission from the same emitter.
If the number of coincident times of emission exceeds a preset minimum, the event is triggered and subsequently stored in the output file.
Note that an event counter is added which can be used to disambiguate the different cycles from the same emitter within one "super" event that is used for the global fit.
In this, a super event refers to the coincidence of events from a variety of emitters.

Author
mdejong

Definition in file JAcousticsEventBuilder.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 62 of file JAcousticsEventBuilder.cc.

63 {
64  using namespace std;
65  using namespace JPP;
66 
68 
72 
74  JLimit_t& numberOfEvents = inputFile.getLimit();
77  JSoundVelocity V = getSoundVelocity; // default sound velocity
78  string detectorFile;
79  tripods_container tripods; // tripods
80  transmitters_container transmitters; // transmitters
81  hydrophones_container hydrophones; // hydrophones
82  double precision;
83  int debug;
84 
85  try {
86 
87  JParser<> zap("Main program to trigger events in acoustic data.");
88 
89  zap['f'] = make_field(inputFile, "output of JConvertDB -q toashort");
90  zap['n'] = make_field(numberOfEvents) = JLimit::max();
91  zap['@'] = make_field(parameters, "trigger parameters");
92  zap['o'] = make_field(outputFile, "output file") = "event.root";
93  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
94  zap['a'] = make_field(detectorFile, "detector file");
95  zap['T'] = make_field(tripods, "tripod data");
96  zap['Y'] = make_field(transmitters, "transmitter data") = JPARSER::initialised();
97  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
98  zap['W'] = make_field(getEmitterID, "waveform identification data") = JPARSER::initialised();
99  zap['p'] = make_field(precision, "precision time-of-arrival") = 1.0e-6;
100  zap['d'] = make_field(debug) = 1;
101 
102  zap(argc, argv);
103  }
104  catch(const exception &error) {
105  FATAL(error.what() << endl);
106  }
107 
108 
110 
111  try {
112  load(detectorFile, detector);
113  }
114  catch(const JException& error) {
115  FATAL(error);
116  }
117 
118  V.set(detector.getUTMZ());
119 
120  JHashMap<int, JReceiver> receivers;
121  JHashMap<int, JEmitter> emitters;
122 
123  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
124 
125  if ((i->getFloor() == 0 && !i->has(HYDROPHONE_DISABLE)) ||
126  (i->getFloor() != 0 && !i->has(PIEZO_DISABLE))) {
127 
128  JPosition3D pos(0.0, 0.0, 0.0);
129 
130  if (i->getFloor() == 0) { // get relative position of hydrophone
131 
132  try {
133  pos = getPosition(hydrophones.begin(),
134  hydrophones.end(),
135  make_predicate(&JHydrophone::getString, i->getString()));
136  }
137  catch(const exception&) {
138  continue; // if no position available, discard hydrophone
139  }
140  }
141 
142  receivers[i->getID()] = JReceiver(i->getID(),
143  i->getPosition() + pos,
144  i->getT0() * 1.0e-9);
145  }
146  }
147 
148  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
149  emitters[i->getID()] = JEmitter(i->getID(),
150  i->getUTMPosition() - detector.getUTMPosition());
151  }
152 
153  for (transmitters_container::const_iterator i = transmitters.begin(); i != transmitters.end(); ++i) {
154  try {
155  emitters[i->getID()] = JEmitter(i->getID(),
156  i->getPosition() + detector.getModule(i->getLocation()).getPosition());
157  }
158  catch(const exception&) {
159  continue; // if no string available, discard transmitter
160  }
161  }
162 
163  const JRange<double> unit(0.0, 1.0);
164 
165  outputFile.open();
166 
167  if (!outputFile.is_open()) {
168  FATAL("Error opening file " << outputFile << endl);
169  }
170 
171  outputFile.put(JMeta(argc, argv));
172  outputFile.put(parameters);
173 
174  // statistics
175 
176  struct map_type :
177  public map<int, JQuantile>
178  {
179  long long int sum() const
180  {
181  long long int count = 0;
182 
183  for (const_iterator i = this->begin(); i != this->end(); ++i) {
184  count += i->second.getCount();
185  }
186 
187  return count;
188  }
189  };
190 
191  map_type number_of_entries;
192  map_type number_of_aliens;
193  map_type number_of_misses;
194  map_type number_of_triggers;
195  map_type number_of_events;
196 
197  // input data
198 
199  string oid; // detector identifier
200 
201  typedef vector<JTransmission> buffer_type; // data type
202 
203  map<int, map< int, buffer_type > > f1; // emitter -> receiver -> data
204 
205  while (inputFile.hasNext()) {
206 
207  if (inputFile.getCounter()%10000 == 0) {
208  STATUS("counter: " << setw(8) << inputFile.getCounter() << '\r' << flush); DEBUG(endl);
209  }
210 
211  JToAshort* parameters = inputFile.next();
212 
213  if (oid == "") {
214  oid = parameters->DETID;
215  }
216 
217  if (oid != parameters->DETID) { // consistency check
218  FATAL("Invalid detector identifier " << parameters->DETID << " != " << oid << endl);
219  }
220 
221  try {
222 
223  const int id = getEmitterID(parameters->EMITTERID);
224 
225  number_of_entries[parameters->EMITTERID].put(1);
226 
227  if (emitters.has(id)) {
228 
229  if (receivers.has(parameters->DOMID)) {
230 
231  const JTransceiver transceiver(emitters[id], receivers[parameters->DOMID]);
232 
233  f1[transceiver.emitter.getID()][transceiver.receiver.getID()].push_back(transceiver.getTransmission(*parameters, V));
234  }
235 
236  } else {
237  number_of_misses[id].put(1);
238  }
239  }
240  catch(const exception&) {
241  number_of_aliens[parameters->EMITTERID].put(1);
242  }
243  }
244  STATUS(endl);
245 
246  for (map_type::const_iterator i = number_of_entries.begin(); i != number_of_entries.end(); ++i) {
247  STATUS("number of entries: " << setw(3) << i->first << ' ' << setw(8) << i->second.getCount() << endl);
248  }
249 
250  for (map_type::const_iterator i = number_of_aliens.begin(); i != number_of_aliens.end(); ++i) {
251  STATUS("number of aliens: " << setw(3) << i->first << ' ' << setw(8) << i->second.getCount() << endl);
252  }
253 
254  for (map_type::const_iterator i = number_of_misses.begin(); i != number_of_misses.end(); ++i) {
255  STATUS("number of misses: " << setw(3) << i->first << ' ' << setw(8) << i->second.getCount() << endl);
256  }
257 
258  const JEventOverlap match(parameters.TMax_s);
259 
260  for (map<int, map< int, buffer_type> >::iterator i = f1.begin(); i != f1.end(); ++i) {
261 
262  buffer_type buffer;
263 
264  for (map< int, buffer_type>::iterator receiver = i->second.begin(); receiver != i->second.end(); ++receiver) {
265 
266  // filter similar hits
267 
268  sort(receiver->second.begin(), receiver->second.end(), JTransmission::compare(precision));
269 
270  buffer_type::iterator __end = unique(receiver->second.begin(), receiver->second.end(), JTransmission::equals(precision));
271 
272  // selection based on quality
273 
274  for (buffer_type::const_iterator p = receiver->second.begin(); p != __end; ++p) {
275  if (p->getQ() >= parameters.Q * (unit(parameters.Q) ? p->getW() : 1.0)) {
276  buffer.push_back(*p);
277  }
278  }
279  }
280 
281  sort(buffer.begin(), buffer.end()); // sort according time-of-emisson
282 
283  JQuantile Q; // monitor of trigger
284  JEvent out; // temporary storage of event(s)
285  JTriggerOutput data; //
286 
287  for (buffer_type::const_iterator p = buffer.begin(); p != buffer.end(); ++p) {
288 
289  buffer_type::const_iterator q = p;
290 
291  // basic correlator
292 
293  while (++q != buffer.end() && q->getToE() - p->getToE() <= parameters.TMax_s) {}
294 
295  Q.put(distance(p,q));
296 
297  if (distance(p,q) >= parameters.numberOfHits) {
298 
299  JEvent event(oid, number_of_triggers.sum(), i->first, p, q);
300 
301  number_of_triggers[i->first].put(event.size());
302 
303  DEBUG("trigger: " << endl << event);
304 
305  if (out.empty()) {
306 
307  out = event;
308 
309  } else if (match(out, event)) {
310 
311  out.merge(event);
312 
313  } else {
314 
315  data.push_back(out);
316 
317  out = event;
318  }
319  }
320  }
321 
322  if (!out.empty()) {
323  data.push_back(out); // pending event
324  }
325 
326  STATUS("number of toes: " << setw(3) << i->first << ' ' << setw(8) << buffer.size());
327  if (Q.getCount() > 0) {
328  STATUS(' ' << FIXED(6,1) << Q.getMean() <<
329  ' ' << FIXED(6,1) << Q.getXmin() <<
330  ' ' << FIXED(6,1) << Q.getXmax());
331  }
332  STATUS(endl);
333 
334  for (JTriggerOutput::const_iterator event = data.begin(); event != data.end(); ++event) {
335 
336  number_of_events[event->getID()].put(event->size());
337 
338  outputFile.put(*event);
339  }
340  }
341  STATUS(endl);
342 
343  for (map_type::const_iterator i = number_of_triggers.begin(); i != number_of_triggers.end(); ++i) {
344  STATUS("number of triggers: " << setw(3) << i->first << ' ' << setw(8) << i->second.getCount() << endl);
345  }
346 
347  for (map_type::const_iterator i = number_of_events.begin(); i != number_of_events.end(); ++i) {
348  STATUS("number of events: " << setw(3) << i->first << ' ' << setw(8) << i->second.getCount() << ' ' << FIXED(7,3) << i->second.getMean() << endl);
349  }
350 
351  JMultipleFileScanner<JMeta> io(inputFile);
352 
353  io >> outputFile;
354 
355  outputFile.close();
356 
357  return 0;
358 }
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:1514
General exception.
Definition: JException.hh:24
Acoustic receiver.
Definition: JReceiver.hh:27
Q(UTCMax_s-UTCMin_s)-livetime_s
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
JContainer< std::vector< JTransmitter > > transmitters_container
Definition: JSydney.cc:79
void merge(const JEvent &event)
Merge event.
std::map< int, buffer_type > map_type
string -&gt; hits
Definition: JPerth.cc:68
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
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
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
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
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.
Detector file.
Definition: JHead.hh:226
JContainer< std::vector< JHydrophone > > hydrophones_container
Definition: JSydney.cc:78
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
JPosition3D getPosition(const Vec &pos)
Get position.
Acoustic transceiver.
Definition: JTransceiver.hh:29
JContainer< std::vector< JTripod > > tripods_container
Definition: JSydney.cc:77
Implementation for depth dependend velocity of sound.
#define FATAL(A)
Definition: JMessage.hh:67
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.
static const int PIEZO_DISABLE
Enable (disable) use of piezo if this status bit is 0 (1);.
Auxiliary class to compare transmissions.
Acoustic event.
static const int HYDROPHONE_DISABLE
Enable (disable) use of hydrophone if this status bit is 0 (1);.
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
static JEmitterID getEmitterID
Function object for emitter identification.
Definition: JEmitterID.hh:119
do set_variable DETECTOR_TXT $WORKDIR detector
Match of two events considering overlap in time.
int EMITTERID
waveform identifier
Definition: JToAshort.hh:27
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62