Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHydrophone.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 
4 #include "TROOT.h"
5 #include "TFile.h"
6 #include "TH1D.h"
7 
8 #include "JDB/JToAshort.hh"
9 #include "JDB/JSupport.hh"
10 
11 #include "JDetector/JDetector.hh"
13 #include "JDetector/JTripod.hh"
14 #include "JDetector/JHydrophone.hh"
15 
17 #include "JSupport/JTreeScanner.hh"
18 
19 #include "JTools/JHashMap.hh"
20 
21 #include "JROOT/JManager.hh"
22 
23 #include "JAcoustics/JEmitterID.hh"
25 #include "JAcoustics/JEmitter.hh"
26 #include "JAcoustics/JReceiver.hh"
30 #include "JAcoustics/JHit.hh"
31 #include "JAcoustics/JEvent.hh"
32 #include "JAcoustics/JSupport.hh"
33 
34 #include "Jeep/JContainer.hh"
35 #include "Jeep/JPrint.hh"
36 #include "Jeep/JParser.hh"
37 #include "Jeep/JMessage.hh"
38 
39 namespace {
40 
41  /**
42  * Auxiliary data structure for organistation of histograms.
43  */
44  struct key_type :
45  public std::pair<int, int>
46  {
47  /**
48  * Constructor.
49  *
50  * \param first first value
51  * \param second second value
52  */
53  key_type(const int first,
54  const int second) :
55  std::pair<int, int>(first, second)
56  {}
57 
58 
59  /**
60  * Read key from input.
61  *
62  * \param in input stream
63  * \param key key
64  * \return input stream
65  */
66  friend inline std::istream& operator>>(std::istream& in, key_type& key)
67  {
68  in >> key.first;
69  in >> key.second;
70 
71  return in;
72  }
73 
74 
75  /**
76  * Write key to output.
77  *
78  * \param out output stream
79  * \param key key
80  * \return output stream
81  */
82  friend inline std::ostream& operator<<(std::ostream& out, const key_type& key)
83  {
84  out << key.first;
85  out << '.';
86  out << key.second;
87 
88  return out;
89  }
90 
91  /**
92  * Less=than operator.
93  *
94  * \param first first key
95  * \param second second key
96  * \return true if first key less than second key; else false
97  */
98  friend inline bool operator<(const key_type& first, const key_type& second)
99  {
100  if (first.first == second.first)
101  return first.second < second.second;
102  else
103  return first.first < second.first;
104  }
105  };
106 }
107 
108 
109 /**
110  * \file
111  *
112  * Example program to plot hydrophone data.
113  * \author mdejong
114  */
115 int main(int argc, char **argv)
116 {
117  using namespace std;
118  using namespace JPP;
119 
120  typedef JContainer< vector<JTripod> > tripods_container;
121  typedef JContainer< vector<JHydrophone> > hydrophones_container;
122 
124  JMultipleFileScanner<JToAshort> toashortFile;
125  string detectorFile;
126  JLimit_t& numberOfEvents = inputFile.getLimit();
127  string outputFile;
128  JSoundVelocity V = getSoundVelocity; // default sound velocity
129  tripods_container tripods; // tripods
130  hydrophones_container hydrophones; // hydrophones
131  int debug;
132 
133  try {
134 
135  JParser<> zap("Example program to plot hydrophone data.");
136 
137  zap['f'] = make_field(inputFile);
138  zap['n'] = make_field(numberOfEvents) = JLimit::max();
139  zap['i'] = make_field(toashortFile);
140  zap['o'] = make_field(outputFile) = "hydrophone.root";
141  zap['a'] = make_field(detectorFile);
142  zap['V'] = make_field(V, "sound velocity") = JPARSER::initialised();
143  zap['T'] = make_field(tripods, "tripod data");
144  zap['H'] = make_field(hydrophones, "hydrophone data") = JPARSER::initialised();
145  zap['W'] = make_field(getEmitterID, "waveform identification data") = JPARSER::initialised();
146  zap['d'] = make_field(debug) = 2;
147 
148  zap(argc, argv);
149  }
150  catch(const exception &error) {
151  FATAL(error.what() << endl);
152  }
153 
154 
156 
157  try {
158  load(detectorFile, detector);
159  }
160  catch(const JException& error) {
161  FATAL(error);
162  }
163 
164  V.set(detector.getUTMZ());
165 
166  JHashMap<int, JReceiver> receivers;
167  JHashMap<int, JEmitter> emitters;
168 
169  for (hydrophones_container::const_iterator i = hydrophones.begin(); i != hydrophones.end(); ++i) {
170 
171  for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
172 
173  if (i->getLocation() == module->getLocation()) {
174 
175  receivers[module->getID()] = JReceiver(module->getID(),
176  module->getPosition() + i->getPosition(),
177  module->getT0() * 1.0e-9);
178  break;
179  }
180  }
181  }
182 
183  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
184  emitters[i->getID()] = JEmitter(i->getID(),
185  i->getUTMPosition() - detector.getUTMPosition());
186  }
187 
188 
189  map<int, vector<double> > buffer; // emitter identifier -> times-of-emission
190 
191  while (inputFile.hasNext()) {
192 
193  STATUS("event: " << setw(10) << inputFile.getCounter() << '\r'); DEBUG(endl);
194 
195  const JEvent* evt = inputFile.next();
196 
197  if (!evt->empty()) {
198  buffer[evt->getID()].push_back((*evt)[0].getToE());
199  }
200  }
201  STATUS(endl);
202 
203 
204  JManager<key_type, TH1D> H1(new TH1D("[%]", NULL, 2000, -2.0e-3, +2.0e-3));
205 
206  for (int counter = 0; toashortFile.hasNext(); ++counter) {
207 
208  if (counter%1000 == 0) {
209  STATUS("counter: " << setw(8) << counter << '\r' << flush); DEBUG(endl);
210  }
211 
212  const JToAshort* parameters = toashortFile.next();
213 
214  const int id = getEmitterID(parameters->EMITTERID);
215 
216  if (emitters.has(id) && receivers.has(parameters->DOMID) && !buffer[id].empty()) {
217 
218  const JTransceiver transceiver(emitters[id], receivers[parameters->DOMID]);
219 
220  const JTransmission transmission = transceiver.getTransmission(*parameters, V);
221 
222  double t1 = numeric_limits<double>::max();
223  double w1 = 1.0 / (double) buffer[id].size();
224 
225  for (vector<double>::const_iterator i = buffer[id].begin(); i != buffer[id].end(); ++i) {
226 
227  const double ti = transmission.getToE() - *i;
228 
229  if (fabs(ti) < fabs(t1)) {
230  t1 = ti;
231  }
232  }
233 
234  H1[key_type(parameters->DOMID, id)]->Fill(t1, w1);
235 
236  H1->Fill(t1);
237  }
238  }
239  STATUS(endl);
240 
241 
242  H1.Write(outputFile.c_str(), true);
243 }
Utility class to parse command line options.
Definition: JParser.hh:1500
Acoustic hit.
General exception.
Definition: JException.hh:23
Acoustic receiver.
Definition: JReceiver.hh:27
int main(int argc, char *argv[])
Definition: Main.cc:15
Sound velocity.
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:71
bool operator<(const Head &first, const Head &second)
Less than operator.
Definition: JHead.hh:1603
do rm f tmp H1
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:80
*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.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Dynamic ROOT object management.
string outputFile
Acoustic emitter.
double getToE() const
Get estimated time of emission.
Data structure for detector geometry and calibration.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Data structure for hydrophone.
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.
Auxiliary class to manage set of compatible ROOT objects (e.g. histograms) using unique keys...
Definition: JManager.hh:43
I/O formatting auxiliaries.
Detector file.
Definition: JHead.hh:196
Acoustics toolkit.
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:1961
void Write(TDirectory &out, const bool wm=false)
Write objects to file.
Definition: JManager.hh:295
Acoustic transceiver.
Definition: JTransceiver.hh:29
Emitter identification.
ROOT TTree parameter settings.
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
Implementation for 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...
Acoustic receiver.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1618
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.
Utility class to parse command line options.
JTransmission getTransmission(const JToAshort &data, const JAbstractSoundVelocity &V=getSoundVelocity) const
Get transmission.
Definition: JTransceiver.hh:62
Acoustic transceiver.
Acoustic event.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
static JEmitterID getEmitterID
Function object for emitter identification.
Definition: JEmitterID.hh:119
do set_variable DETECTOR_TXT $WORKDIR detector
int getID() const
Get identifier.
Data structure for tripod.
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
int EMITTERID
waveform identifier
Definition: JToAshort.hh:27
Container I/O.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62