Jpp  17.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
examples/JAcoustics/JKatoomba.cc File Reference

Example application to test fit of model to simulated acoustic data. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include "TRandom3.h"
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TH2D.h"
#include "JLang/JPredicate.hh"
#include "JLang/JSTDToolkit.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JTripod.hh"
#include "JDetector/JTransmitter.hh"
#include "JDetector/JHydrophone.hh"
#include "JDetector/JModule.hh"
#include "JROOT/JManager.hh"
#include "JROOT/JRootToolkit.hh"
#include "JAcoustics/JSoundVelocity.hh"
#include "JAcoustics/JEmitter.hh"
#include "JAcoustics/JAcousticsToolkit.hh"
#include "JAcoustics/JAcousticsSupportkit.hh"
#include "JAcoustics/JHit.hh"
#include "JAcoustics/JFitParameters.hh"
#include "JAcoustics/JKatoomba.hh"
#include "JAcoustics/JTransmission_t.hh"
#include "Jeep/JContainer.hh"
#include "Jeep/JTimer.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

Example application to test fit of model to simulated acoustic data.

Author
mdejong

Definition in file examples/JAcoustics/JKatoomba.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 60 of file examples/JAcoustics/JKatoomba.cc.

61 {
62  using namespace std;
63  using namespace JPP;
64 
65  typedef JContainer< vector<JTripod> > tripods_container;
66  typedef JContainer< vector<JTransmitter> > transmitters_container;
67  typedef JContainer< vector<JHydrophone> > hydrophones_container;
68  typedef JContainer< map<int, JWaveform> > waveform_container;
69  typedef JContainer< set<JTransmission_t> > disable_container;
70 
71  string detectorFile;
72  string outputFile;
73  int numberOfEvents;
74  map<int, int> numberOfPings;
75  JSoundVelocity V = getSoundVelocity; // default sound velocity
76  tripods_container tripods; // tripods
77  transmitters_container transmitters; // transmitters
78  hydrophones_container hydrophones; // hydrophones
79  JTimeRange T_s(-1.0e-4, +1.0e-4); // time range of background [s]
80  JFitParameters parameters; // fit parameters
81  int fit; // type of fit
82  bool unify; // unify weighing of pings
83  disable_container disable; // disable tansmissions
84  waveform_container waveforms; // emitter power and frequency
85  UInt_t seed;
86  int debug;
87 
88  numberOfPings[WILDCARD] = NUMBER_OF_PINGS;
89  waveforms [WILDCARD] = EMITTER_WAVEFORM;
90 
91  try {
92 
93  JParser<> zap("Example application to test fit of model to simulated acoustic data.");
94 
95  zap['a'] = make_field(detectorFile);
96  zap['o'] = make_field(outputFile);
97  zap['n'] = make_field(numberOfEvents);
99  zap['N'] = make_field(numberOfPings, "number of pings per emitter") = JPARSER::initialised();
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['F'] = make_field(fit, "fit type") = linear_t, simplex_t, gandalf_t;
105  zap['u'] = make_field(unify, "unify weighing of pings");
106  zap['!'] = make_field(disable, "disable transmission") = JPARSER::initialised();
107  zap['W'] = make_field(waveforms, "emitter power and frequency") = JPARSER::initialised();
108  zap['S'] = make_field(seed) = 0;
109  zap['d'] = make_field(debug) = 1;
110 
111  zap(argc, argv);
112  }
113  catch(const exception &error) {
114  FATAL(error.what() << endl);
115  }
116 
117 
118  cout.tie(&cerr);
119 
120  gRandom->SetSeed(seed);
121 
122  if (numberOfEvents <= 0) { FATAL("Invalid number of events " << numberOfEvents << endl); }
123 
125 
126  try {
127  load(detectorFile, detector);
128  }
129  catch(const JException& error) {
130  FATAL(error);
131  }
132 
133  vector<JEmitter> emitters;
134 
135  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
136  emitters.push_back(JEmitter(i->getID(),
137  i->getUTMPosition() - detector.getUTMPosition()));
138  }
139 
140  for (transmitters_container::const_iterator i = transmitters.begin(); i != transmitters.end(); ++i) {
141  try {
142  emitters.push_back(JEmitter(i->getID(),
143  i->getPosition() + detector.getModule(i->getLocation()).getPosition()));
144  }
145  catch(const exception&) {
146  continue; // if no string available, discard transmitter
147  }
148  }
149 
150  if (detector.empty()) { FATAL("No modules in detector." << endl); }
151  if (emitters.empty()) { FATAL("No emitters in system." << endl); }
152 
153 
154  V.set(detector.getUTMZ()); // sound velocity at detector depth
155 
156  const double D_m = -detector.getUTMZ(); // depth [m]
157 
158 
159  const JGeometry geometry(detector, hydrophones);
160 
161  DEBUG(geometry);
162 
163  JKatoomba<JEstimator> estimator(geometry, V, parameters.option);
164  JKatoomba<JAbstractMinimiser> evaluator(geometry, V, parameters.option);
165  JKatoomba<JSimplex> simplex (geometry, V, parameters.option);
166  JKatoomba<JGandalf> gandalf (geometry, V, parameters.option);
167 
168  simplex.estimator.reset(getMEstimator(parameters.mestimator));
169  gandalf.estimator.reset(getMEstimator(parameters.mestimator));
170 
171  simplex.debug = debug;
172  gandalf.debug = debug;
173 
175 
176 
177  typedef JHit<JPDFGauss> hit_type;
178 
179 
180  TH1D h0("cpu", NULL, 100, 1.0, 7.0);
181  TH1D h1("chi2/NDF", NULL, 100, 0.0, 5.0);
182 
183  JManager<int, TH1D> H1(new TH1D("emitter[%]", NULL, 100, -5.0e-5, +5.0e-5));
184  JManager<int, TH2D> H2(new TH2D("string[%]", NULL, 100, -1.0, +1.0, 100, -1.0, +1.0));
185 
186 
187  JTimer timer;
188 
189  for (int number_of_events = 0, count = 0; number_of_events != numberOfEvents; ++number_of_events) {
190 
191  STATUS("event: " << setw(10) << number_of_events << '\r'); DEBUG(endl);
192 
193  JModel model; // randomised model data
194 
195  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
196 
197  if (model.string[module->getString()] == JMODEL::JString()) {
198 
199  model.string[module->getString()] = JMODEL::JString(gRandom->Uniform(-2.0e-2, +2.0e-2),
200  gRandom->Uniform(-2.0e-2, +2.0e-2));
201  }
202  }
203 
204  DEBUG("Model" << endl << model << endl);
205 
206  // set module positions according actual model data
207 
208  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
209  if (geometry.hasLocation(module->getLocation())) {
210  module->set(estimator.detector[module->getString()].getPosition(model.string[module->getString()], module->getFloor()));
211  }
212  }
213 
214 
215  // generate hits
216 
217  int minimum_number_of_pings = numeric_limits<int>::max();
218 
219  for (vector<JEmitter>::const_iterator emitter = emitters.begin(); emitter != emitters.end(); ++emitter) {
220  minimum_number_of_pings = min(minimum_number_of_pings, getValue(numberOfPings, emitter->getID(), numberOfPings[WILDCARD]));
221  }
222 
223  vector<hit_type> data;
224 
225  for (vector<JEmitter>::const_iterator emitter = emitters.begin(); emitter != emitters.end(); ++emitter) {
226 
227  const int number_of_pings = getValue(numberOfPings, emitter->getID(), numberOfPings[WILDCARD]);
228 
229  const double signal = (unify ? (double) minimum_number_of_pings / (double) number_of_pings : 1.0);
230 
231  for (int ping_counter = 0; ping_counter != number_of_pings; ++ping_counter) {
232 
233  ++count;
234 
235  const double toe_s = ping_counter * 10.0 + gRandom->Uniform(-1.0, +1.0);
236 
237  model.emitter[emitter->getID()][count] = toe_s;
238 
239  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
240 
241  if (disable.count(JTransmission_t(emitter->getID(), module->getID())) == 0) {
242 
243  if (geometry.hasLocation(module->getLocation())) {
244 
245  const JWaveform& waveform = getValue(waveforms, emitter->getID(), waveforms[WILDCARD]);
246 
247  const double d_m = getDistance(module->getPosition(), emitter->getPosition());
248  const double toa_s = toe_s + V.getTime(d_m, emitter->getZ(), module->getZ());
249  const double Q = waveform.getQ(D_m, d_m);
250 
251  if (Q >= parameters.Qmin) {
252 
253  double t1_s = toa_s;
254 
255  if (gRandom->Rndm() >= parameters.background)
256  t1_s = gRandom->Gaus(toa_s, parameters.sigma_s);
257  else
258  t1_s = gRandom->Uniform(toa_s + T_s.getLowerLimit(),
259  toa_s + T_s.getUpperLimit());
260 
261  const hit_type hit(*emitter,
262  count,
263  module->getLocation(),
264  JPDFGauss(t1_s, parameters.sigma_s, signal, parameters.background));
265 
266  DEBUG("hit: " << hit << ' ' << FIXED(7,1) << Q << endl);
267 
268  data.push_back(hit);
269  }
270  }
271  }
272  }
273  }
274  }
275 
276 
277  timer.reset();
278  timer.start();
279 
280  JModel result = estimator(data.begin(), data.end()); // pre-fit
281  double chi2 = numeric_limits<double>::max();
282 
283  switch (fit) {
284 
285  case linear_t:
286 
287  chi2 = evaluator(result, data.begin(), data.end());
288  break;
289 
290  case simplex_t:
291 
292  simplex.value = result; // start value
293 
294  chi2 = simplex(data.begin(), data.end()) / simplex.estimator->getRho(1.0);
295  result = simplex.value;
296  break;
297 
298  case gandalf_t:
299 
300  gandalf.value = result; // start value
301 
302  chi2 = gandalf(data.begin(), data.end()) / gandalf.estimator->getRho(1.0);
303  result = gandalf.value;
304  break;
305 
306  default:
307  break;
308  }
309 
310  timer.stop();
311 
312  double W = 0.0;
313 
314  for (vector<hit_type>::const_iterator hit = data.begin(); hit != data.end(); ++hit) {
315  W += hit->getWeight();
316  }
317 
318  const int ndf = data.size() - result.getN();
319 
320  DEBUG("Final values" << endl
321  << FIXED(9,3) << chi2 << '/' << ndf << endl
322  << result << endl);
323 
324 
325  h0.Fill(log10((double) timer.usec_wall));
326  h1.Fill(chi2 / (double) (W - result.getN()));
327 
328  for (JHashMap<int, JMODEL::JString>::const_iterator i = model.string.begin(); i != model.string.end(); ++i) {
329  H2[i->first]->Fill((i->second.tx - result.string [i->first].tx) * 1.0e3, // mrad
330  (i->second.ty - result.string [i->first].ty) * 1.0e3); // mrad
331  }
332 
333  for (JHashMap<JEKey, JMODEL::JEmitter>::const_iterator i = model.emitter.begin(); i != model.emitter.end(); ++i) {
334  H1[i->first.getID()]->Fill(i->second.t1 - result.emitter[i->first].t1);
335  }
336  }
337  STATUS(endl);
338 
339 
340  TFile out(outputFile.c_str(), "recreate");
341 
342  out << h0
343  << h1
344  << H1
345  << H2;
346 
347  out.Write();
348  out.Close();
349 }
Utility class to parse command line options.
Definition: JParser.hh:1500
size_t getN() const
Get number of fit parameters.
General exception.
Definition: JException.hh:23
Q(UTCMax_s-UTCMin_s)-livetime_s
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:89
Template specialisation of fit function of acoustic model based on linear approximation.
Definition: JKatoomba.hh:249
Template specialisation of fit function of acoustic model based on JSimplex minimiser.
Definition: JKatoomba.hh:364
*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 for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
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:66
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
string outputFile
Acoustics hit.
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
Linear fit.
Definition: JKatoomba.hh:47
Model for fit to acoustics data.
static const JSoundVelocity getSoundVelocity(1541.0,-17.0e-3,-2000.0)
Function object for velocity of sound.
Template specialisation of fit function of acoustic model based on JGandalf minimiser.
Definition: JKatoomba.hh:457
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
Detector file.
Definition: JHead.hh:224
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:1961
set_variable E_E log10(E_{fit}/E_{#mu})"
return result
Definition: JPolint.hh:743
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
JPosition3D getPosition(const Vec &pos)
Get position.
double getQ(const double D_m, const double d_m) const
Get quality at given distance.
int debug
debug level
Definition: JSirene.cc:66
Implementation for depth dependend velocity of sound.
#define FATAL(A)
Definition: JMessage.hh:67
JACOUSTICS::JModel::string_type string
JACOUSTICS::JModel::emitter_type emitter
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
std::vector< int > count
Definition: JAlgorithm.hh:180
Simple fit method based on Powell&#39;s algorithm, see reference: Numerical Recipes in C++...
Definition: JSimplex.hh:42
Template specialisation of fit function of acoustic model based on JAbstractMinimiser minimiser...
Definition: JKatoomba.hh:188
Custom probability density function of time-of-arrival.
container_type::const_iterator const_iterator
Definition: JHashMap.hh:86
JMEstimator * getMEstimator(const int type)
Get M-Estimator.
Definition: JMEstimator.hh:166
do echo n Creating graphics for string $STRING for((FLOOR=$FIRST_FLOOR;$FLOOR<=$LAST_FLOOR;FLOOR+=1))
do set_variable DETECTOR_TXT $WORKDIR detector
Acoustic transmission identifier.
Utility class for emitter power and frequency.
Simplex fit.
Definition: JKatoomba.hh:48
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Gandalf fit.
Definition: JKatoomba.hh:49