Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFremantle.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <set>
6 #include <map>
7 #include <deque>
8 #include <algorithm>
9 #include <limits>
10 
11 #include <type_traits>
12 #include <functional>
13 #include <future>
14 #include <mutex>
15 #include <thread>
16 #include <vector>
17 #include <queue>
18 
19 #include "TROOT.h"
20 #include "TFile.h"
21 
22 #include "JLang/JPredicate.hh"
23 #include "JLang/JComparator.hh"
24 #include "JLang/JComparison.hh"
25 #include "JLang/JFileStream.hh"
26 
27 #include "JDetector/JDetector.hh"
29 #include "JDetector/JTripod.hh"
31 #include "JDetector/JModule.hh"
32 #include "JDetector/JHydrophone.hh"
33 
34 #include "JTools/JHashMap.hh"
35 #include "JTools/JRange.hh"
36 #include "JTools/JQuantile.hh"
37 
41 #include "JSupport/JMeta.hh"
42 
44 #include "JAcoustics/JEmitter.hh"
46 #include "JAcoustics/JHit.hh"
48 #include "JAcoustics/JGlobalfit.hh"
49 #include "JAcoustics/JEvent.hh"
50 #include "JAcoustics/JSuperEvt.hh"
52 #include "JAcoustics/JSupport.hh"
55 
56 #include "Jeep/JContainer.hh"
57 #include "Jeep/JParser.hh"
58 #include "Jeep/JMessage.hh"
59 
60 
61 /**
62  * \file
63  *
64  * Application to make a global fit of the detector geometry to acoustic data.\n
65  * \author mdejong
66  */
67 int main(int argc, char **argv)
68 {
69  using namespace std;
70  using namespace JPP;
71 
75  typedef JContainer< set<JTransmission_t> > disable_container;
76 
79  string detectorFile;
80  JLimit_t& numberOfEvents = inputFile.getLimit();
81  JSoundVelocity V = getSoundVelocity; // default sound velocity
82  tripods_container tripods; // tripods
83  transmitters_container transmitters; // transmitters
84  hydrophones_container hydrophones; // hydrophones
85  JFitParameters parameters; // fit parameters
86  disable_container disable; // disable tansmissions
87  size_t threads; // number of parallel threads
88  bool& squash = JFremantle::squash;
89  int debug;
90 
91  try {
92 
93  JParser<> zap("Application to fit position calibration model to acoustic data.");
94 
95  zap['f'] = make_field(inputFile, "output of JAcousticEventBuilder[.sh]");
96  zap['o'] = make_field(outputFile) = "";
97  zap['n'] = make_field(numberOfEvents) = JLimit::max();
98  zap['a'] = make_field(detectorFile);
100  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
101  zap['T'] = make_field(tripods, "tripod data");
102  zap['Y'] = make_field(transmitters, "transmitter data") = JPARSER::initialised();
103  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
104  zap['M'] = make_field(getMechanics, "mechanics data") = JPARSER::initialised();
105  zap['!'] = make_field(disable, "disable transmission") = JPARSER::initialised();
106  zap['N'] = make_field(threads, "number of threads") = 1;
107  zap['s'] = make_field(squash, "squash transmissions in output");
108  zap['d'] = make_field(debug) = 1;
109 
110  zap(argc, argv);
111  }
112  catch(const exception &error) {
113  FATAL(error.what() << endl);
114  }
115 
116  ROOT::EnableThreadSafety();
117 
119 
120  try {
121  load(detectorFile, detector);
122  }
123  catch(const JException& error) {
124  FATAL(error);
125  }
126 
127  JHashMap<int, JLocation> receivers;
128  JHashMap<int, JEmitter> emitters;
129 
130  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
131  receivers[i->getID()] = i->getLocation();
132  }
133 
134  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
135  emitters[i->getID()] = JEmitter(i->getID(), i->getUTMPosition() - detector.getUTMPosition());
136  }
137 
138  for (transmitters_container::const_iterator i = transmitters.begin(); i != transmitters.end(); ++i) {
139  try {
140  emitters[i->getID()] = JEmitter(i->getID(), i->getPosition() + detector.getModule(i->getLocation()).getPosition());
141  }
142  catch(const exception&) {} // if no module available, discard transmitter
143  }
144 
145  V.set(detector.getUTMZ()); // sound velocity at detector depth
146 
147  JGeometry geometry(detector, hydrophones);
148  JGlobalfit katoomba(geometry, V, parameters);
149 
151 
154 
155  if (inputFile.size() > 1u) { // sort input files
156 
157  map<double, string> zmap;
158 
159  for (const string& file_name : inputFile) {
160 
161  STATUS(file_name << '\r'); DEBUG(endl);
162 
163  for (JSingleFileScanner<JEvent> in(file_name, 1); in.hasNext(); ) {
164 
165  const JEvent* evt = in.next();
166 
167  if (JFremantle::oid == "")
168  JFremantle::oid = evt->getOID();
169  else if (JFremantle::oid != evt->getOID()) // consistency check
170  FATAL("Invalid detector identifier " << evt->getOID() << " != " << JFremantle::oid << endl);
171 
172  if (!evt->empty()) {
173  zmap[evt->begin()->getToE()] = file_name;
174  }
175  }
176  }
177  STATUS(endl);
178 
179  inputFile.clear();
180 
181  for (map<double, string>::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
182  inputFile.push_back(i->second);
183  }
184  }
185 
186  const JRange<double> unit(0.0, 1.0);
187 
188  if (outputFile.getFilename() != "" &&
189  outputFile.getFilename() != "--") {
190 
191  outputFile.open();
192 
193  JFremantle::output = &outputFile;
194  }
195  /*
196  outputFile.put(JMeta(argc, argv));
197  outputFile.put(parameters);
198  */
199 
200  try {
201 
202  JFremantle fremantle(geometry, V, parameters, threads, 2 * threads);
203 
204  typedef deque<JEvent> buffer_type;
205 
206  for (buffer_type zbuf; inputFile.hasNext(); ) {
207 
208  STATUS(inputFile.getFilename() << '\r'); DEBUG(endl);
209 
210  // read one file at a time
211 
212  for (const string file_name = inputFile.getFilename(); inputFile.hasNext() && file_name == inputFile.getFilename(); ) {
213 
214  const JEvent* evt = inputFile.next();
215 
216  if (!evt->empty() && emitters.has(evt->getID())) {
217  zbuf.push_back(*evt);
218  }
219  }
220 
221  sort(zbuf.begin(), zbuf.end()); // sort according first time-of-emission
222 
223  for (buffer_type::iterator p = zbuf.begin(), q; p != zbuf.end(); p = q) {
224 
225  for (q = p; ++q != zbuf.end() && q->begin()->getToE() <= p->rbegin()->getToE() + parameters.Tmax_s; ) {}
226 
227  if (q == zbuf.end()) {
228 
229  if (inputFile.hasNext()) {
230 
231  zbuf.erase(zbuf.begin(), p); // remove processed data and continue reading
232 
233  break;
234  }
235  }
236 
237  JEvent::overlap(p, q, parameters.deadTime_s); // empty overlapping events
238 
239  if (getNumberOfEmitters(p,q) >= parameters.Nmin) {
240 
241  const JWeight getWeight(p, q);
242 
243  set<int> buffer;
245 
246  for (buffer_type::iterator evt = p; evt != q; ++evt) {
247 
248  sort(evt->begin(), evt->end(), JKatoomba<>::compare);
249 
250  JEvent::iterator __end = unique(evt->begin(), evt->end(), make_comparator(&JTransmission::getID, JComparison::eq()));
251 
252  const JEmitter& emitter = emitters [evt->getID()];
253  const double weight = getWeight(evt->getID());
254 
255  for (JEvent::const_iterator i = evt->begin(); i != __end; ++i) {
256 
257  if (disable.count(JTransmission_t(evt->getID(), i->getID())) == 0 &&
258  disable.count(JTransmission_t(-1, i->getID())) == 0) {
259 
260  if (receivers.has(i->getID()) && geometry.hasLocation(receivers[i->getID()]) && i->getQ() >= parameters.Qmin * (unit(parameters.Qmin) ? i->getW() : 1.0)) {
261 
262  data.push_back(JHit(emitter,
263  distance(p,evt),
264  receivers[i->getID()],
265  i->getToA(),
266  parameters.sigma_s,
267  weight));
268 
269  buffer.insert(evt->getID());
270  }
271  }
272  }
273  }
274 
275  if (buffer.size() >= parameters.Nmin) {
276  fremantle.enqueue(data);
277  }
278  }
279  }
280  }
281  STATUS(endl);
282  }
283  catch(const exception& error) {
284  FATAL("main " << error.what());
285  }
286  /*
287  JMultipleFileScanner<JMeta> io(inputFile);
288 
289  io >> outputFile;
290  */
291  outputFile.close();
292 
293  JFileOutputStream(3) << SCIENTIFIC(1,10) << JFremantle::Q.getMean(numeric_limits<float>::max()) << endl;
294 }
Fit function of acoustic model.
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1514
Acoustic hit.
General exception.
Definition: JException.hh:24
Q(UTCMax_s-UTCMin_s)-livetime_s
int main(int argc, char *argv[])
Definition: Main.cc:15
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
double getWeight(T __begin, T __end)
Get total weight of data points.
Definition: JKatoomba_t.hh:61
Sound velocity.
JContainer< std::vector< JTransmitter > > transmitters_container
Definition: JSydney.cc:79
static JDetectorMechanics getMechanics
Function object to get string mechanics.
Definition: JMechanics.hh:242
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
Auxiliary data structure to unify weights of acoustics data according to the number of pings per emit...
General purpose class for hash map of unique elements.
#define STATUS(A)
Definition: JMessage.hh:63
ROOT TTree parameter settings.
Detector data structure.
Definition: JDetector.hh:89
Recording of objects on file according a format that follows from the file name extension.
*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
Acoustic event.
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
void enqueue(input_type &data)
Queue data.
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
string outputFile
Acoustic emitter.
Data structure for detector geometry and calibration.
std::vector< JHitW0 > buffer_type
hits
Definition: JPerth.cc:67
Acoustics hit.
Global fit of prameterised detector geometry to acoustics data.
Definition: JGlobalfit.hh:29
Data structure for hydrophone.
size_t getNumberOfEmitters(T __begin, T __end)
Get number of emitters.
Acoustic super event fit toolkit.
Acoustic fit parameters.
Scanning of objects from a single file according a format that follows from the extension of each fil...
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
Data structure for transmitter.
Acoustic emitter.
Definition: JEmitter.hh:27
Acoustics toolkit.
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.
Acoustic event fit.
Streaming of output.
Definition: JFileStream.hh:46
ROOT I/O of application specific meta data.
JPosition3D getPosition(const Vec &pos)
Get position.
JContainer< std::vector< JTripod > > tripods_container
Definition: JSydney.cc:77
General purpose messaging.
Implementation for depth dependend velocity of sound.
#define FATAL(A)
Definition: JMessage.hh:67
Scanning of objects from multiple files according a format that follows from the extension of each fi...
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Auxiliary class to define a range between two values.
General purpose class for object reading from a list of file names.
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:48
Utility class to parse command line options.
Acoustic transmission identifier.
Thread pool for global fits.
Definition: JFremantle_t.hh:31
bool hasLocation(const JLocation &location) const
Check if this detector has given location.
Definition: JGeometry.hh:588
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 set_variable DETECTOR_TXT $WORKDIR detector
int getID() const
Get emitter identifier.
double u[N+1]
Definition: JPolint.hh:865
Data structure for tripod.
Acoustic transmission identifier.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:486
Template definition of fit function of acoustic model.
Definition: JKatoomba_t.hh:146
Container I/O.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Data structure for optical module.