Jpp - 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 <map>
#include <algorithm>
#include "TRandom3.h"
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TH2D.h"
#include "JLang/JPredicate.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JTripod.hh"
#include "JDetector/JModule.hh"
#include "JDetector/JHydrophone.hh"
#include "JROOT/JManager.hh"
#include "JROOT/JRootToolkit.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 "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 44 of file examples/JAcoustics/JKatoomba.cc.

45 {
46  using namespace std;
47  using namespace JPP;
48 
49  typedef JContainer< vector<JTripod> > tripods_container;
50  typedef JContainer< vector<JHydrophone> > hydrophones_container;
51 
52  string detectorFile;
53  string outputFile;
54  int numberOfEvents;
55  map<int, int> numberOfPings;
56  JSoundVelocity V = getSoundVelocity; // default sound velocity
57  tripods_container tripods; // tripods
58  hydrophones_container hydrophones; // hydrophones
59  JTimeRange T_s(-1.0e-4, +1.0e-4); // time range of background [s]
60  JFitParameters parameters; // fit parameters
61  int fit; // type of fit
62  bool unique; // one ping per cycle
63  UInt_t seed;
64  int debug;
65 
66  numberOfPings[-1] = 11; // default
67 
68  try {
69 
70  JParser<> zap("Example application to test fit of model to simukated acoustic data.");
71 
72  zap['a'] = make_field(detectorFile);
73  zap['o'] = make_field(outputFile);
74  zap['n'] = make_field(numberOfEvents);
76  zap['N'] = make_field(numberOfPings, "number of pings per emitter") = JPARSER::initialised();
77  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
78  zap['T'] = make_field(tripods, "tripod data");
79  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
80  zap['F'] = make_field(fit) = linear_t, simplex_t, gandalf_t;
81  zap['u'] = make_field(unique, "one ping per cycle");
82  zap['S'] = make_field(seed) = 0;
83  zap['d'] = make_field(debug) = 1;
84 
85  zap(argc, argv);
86  }
87  catch(const exception &error) {
88  FATAL(error.what() << endl);
89  }
90 
91 
92  cout.tie(&cerr);
93 
94  gRandom->SetSeed(seed);
95 
96  if (numberOfEvents <= 0) { FATAL("Invalid number of events " << numberOfEvents << endl); }
97 
99 
100  try {
101  load(detectorFile, detector);
102  }
103  catch(const JException& error) {
104  FATAL(error);
105  }
106 
107  vector<JEmitter> emitters;
108 
109  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
110  emitters.push_back(JEmitter(i->getID(), i->getUTMPosition() - detector.getUTMPosition()));
111  }
112 
113  if (detector.empty()) { FATAL("No modules in detector." << endl); }
114  if (emitters.empty()) { FATAL("No emitters in system." << endl); }
115 
116 
117  const JGeometry geometry(detector, hydrophones);
118 
119  DEBUG(geometry);
120 
121 
122  JKatoomba<JEstimator> estimator(geometry, V);
123  JKatoomba<JAbstractMinimiser> evaluator(geometry, V);
124  JKatoomba<JSimplex> simplex (geometry, V);
125  JKatoomba<JGandalf> gandalf (geometry, V);
126 
127  simplex.estimator.reset(getMEstimator(parameters.mestimator));
128  gandalf.estimator.reset(getMEstimator(parameters.mestimator));
129 
130  simplex.debug = debug;
131  gandalf.debug = debug;
132 
134 
135 
136  typedef JHit<JPDFGauss> hit_type;
137 
138 
139  TH1D h0("cpu", NULL, 100, 1.0, 7.0);
140  TH1D h1("chi2/NDF", NULL, 100, 0.0, 5.0);
141 
142  JManager<int, TH1D> H1(new TH1D("emitter[%]", NULL, 100, -5.0e-5, +5.0e-5));
143  JManager<int, TH2D> H2(new TH2D("string[%]", NULL, 100, -1.0, +1.0, 100, -1.0, +1.0));
144 
145 
146  JTimer timer;
147 
148  for (int number_of_events = 0, count = 0; number_of_events != numberOfEvents; ++number_of_events) {
149 
150  STATUS("event: " << setw(10) << number_of_events << '\r'); DEBUG(endl);
151 
152  JModel model; // randomised model data
153 
154  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
155 
156  if (model.string[module->getString()] == JMODEL::JString()) {
157 
158  model.string[module->getString()] = JMODEL::JString(gRandom->Uniform(-2.0e-2, +2.0e-2),
159  gRandom->Uniform(-2.0e-2, +2.0e-2));
160  }
161  }
162 
163  DEBUG(model);
164 
165  // set module positions according actual model data
166 
167  for (JDetector::iterator module = detector.begin(); module != detector.end(); ++module) {
168  if (geometry.hasLocation(module->getLocation())) {
169  module->set(estimator.detector[module->getString()].getPosition(model.string[module->getString()], module->getFloor()));
170  }
171  }
172 
173 
174  // generate hits
175 
176  vector<hit_type> data;
177 
178  for (vector<JEmitter>::const_iterator emitter = emitters.begin(); emitter != emitters.end(); ++emitter) {
179 
180  int number_of_pings = numberOfPings[-1];
181 
182  if (numberOfPings.find(emitter->getID()) != numberOfPings.end()) {
183  number_of_pings = numberOfPings[emitter->getID()];
184  }
185 
186  int weight = numeric_limits<int>::max();
187 
188  for (map<int, int>::const_iterator i = numberOfPings.begin(); i != numberOfPings.end(); ++i) {
189  if (i->second < weight) {
190  weight = i->second;
191  }
192  }
193 
194  const double signal = (unique ? (double) weight / (double) number_of_pings : 1.0);
195 
196  for (int ping_counter = 0; ping_counter != number_of_pings; ++ping_counter) {
197 
198  ++count;
199 
200  const double toe_s = ping_counter * 10.0 + gRandom->Uniform(-1.0, +1.0);
201 
202  model.emitter[emitter->getID()][count] = toe_s;
203 
204  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
205 
206  if (geometry.hasLocation(module->getLocation())) {
207 
208  const double toa_s = toe_s + V.getTime(getDistance(module->getPosition(), emitter->getPosition()), emitter->getZ(), module->getZ());
209 
210  double t1_s = toa_s;
211 
212  if (gRandom->Rndm() >= parameters.background)
213  t1_s = gRandom->Gaus(toa_s, parameters.sigma_s);
214  else
215  t1_s = gRandom->Uniform(toa_s + T_s.getLowerLimit(),
216  toa_s + T_s.getUpperLimit());
217 
218  data.push_back(hit_type(*emitter,
219  count,
220  module->getLocation(),
221  JPDFGauss(t1_s, parameters.sigma_s, signal, parameters.background)));
222  }
223  }
224  }
225  }
226 
227  DEBUG("Model" << endl << model << endl);
228 
229  for (vector<hit_type>::const_iterator hit = data.begin(); hit != data.end(); ++hit) {
230  DEBUG("hit: " << *hit << endl);
231  }
232 
233 
234  timer.reset();
235  timer.start();
236 
237  JModel result = estimator(data.begin(), data.end()); // pre-fit
238  double chi2 = numeric_limits<double>::max();
239 
240  switch (fit) {
241 
242  case linear_t:
243 
244  chi2 = evaluator(result, data.begin(), data.end());
245  break;
246 
247  case simplex_t:
248 
249  simplex.value = result; // start value
250 
251  chi2 = simplex(data.begin(), data.end()) / simplex.estimator->getRho(1.0);
252  result = simplex.value;
253  break;
254 
255  case gandalf_t:
256 
257  gandalf.value = result; // start value
258 
259  chi2 = gandalf(data.begin(), data.end()) / gandalf.estimator->getRho(1.0);
260  result = gandalf.value;
261  break;
262 
263  default:
264  break;
265  }
266 
267  timer.stop();
268 
269  double W = 0.0;
270 
271  for (vector<hit_type>::const_iterator hit = data.begin(); hit != data.end(); ++hit) {
272  W += hit->getWeight();
273  }
274 
275  const int ndf = data.size() - model.getN();
276 
277  DEBUG("Final values" << endl
278  << FIXED(9,3) << chi2 << '/' << ndf << endl
279  << result << endl);
280 
281 
282  h0.Fill(log10((double) timer.usec_wall));
283  h1.Fill(chi2 / (double) (W - model.getN()));
284 
285  for (JHashMap<int, JMODEL::JString>::const_iterator i = model.string.begin(); i != model.string.end(); ++i) {
286  H2[i->first]->Fill((i->second.tx - result.string [i->first].tx) * 1.0e3, // mrad
287  (i->second.ty - result.string [i->first].ty) * 1.0e3); // mrad
288  }
289 
290  for (JHashMap<JEKey, JMODEL::JEmitter>::const_iterator i = model.emitter.begin(); i != model.emitter.end(); ++i) {
291  H1[i->first.getID()]->Fill(i->second.t1 - result.emitter[i->first].t1);
292  }
293  }
294  STATUS(endl);
295 
296 
297  TFile out(outputFile.c_str(), "recreate");
298 
299  out << h0
300  << h1
301  << H1
302  << H2;
303 
304  out.Write();
305  out.Close();
306 }
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
do rm f tmp H1
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:80
Template specialisation of fit function of acoustic model based on linear approximation.
Definition: JKatoomba.hh:285
Template specialisation of fit function of acoustic model based on JSimplex minimiser.
Definition: JKatoomba.hh:385
*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:69
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
string outputFile
Acoustics hit.
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
Linear fit.
Definition: JKatoomba.hh:45
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:466
do for((RUN=${RANGE%%-*};$RUN<=${RANGE##*-};RUN+=1))
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
Detector file.
Definition: JHead.hh:196
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
return result
Definition: JPolint.hh:727
Auxiliary class for CPU timing and usage.
Definition: JTimer.hh:32
int debug
debug level
Definition: JSirene.cc:63
Implementation for 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:184
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:226
Custom probability density function of time-of-arrival.
container_type::const_iterator const_iterator
Definition: JHashMap.hh:85
JMEstimator * getMEstimator(const int type)
Get M-Estimator.
Definition: JMEstimator.hh:166
do set_variable DETECTOR_TXT $WORKDIR detector
Simplex fit.
Definition: JKatoomba.hh:46
std::vector< double > weight
Definition: JAlgorithm.hh:428
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Gandalf fit.
Definition: JKatoomba.hh:47