Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAcoustics/JEvent.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JEVENT__
2 #define __JACOUSTICS__JEVENT__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <iomanip>
8 #include <vector>
9 
10 #include <TROOT.h>
11 #include <TObject.h>
12 
13 #include "JIO/JSerialisable.hh"
14 #include "JIO/JSTDIO.hh"
15 
16 #include "JAcoustics/JCounter.hh"
18 
19 
20 /**
21  * \file
22  *
23  * Acoustic event.
24  * \author mdejong
25  */
26 namespace JACOUSTICS {}
27 namespace JPP { using namespace JACOUSTICS; }
28 
29 namespace JACOUSTICS {
30 
31  using JIO::JSerialisable;
32  using JIO::JReader;
33  using JIO::JWriter;
34 
35  /**
36  * Acoustic event.
37  */
38  struct JEvent :
39  public JSerialisable,
40  public JCounter,
41  public std::vector<JTransmission>,
42  public TObject
43  {
44  /**
45  * Auxiliary class to determine value of acoustic events.\n
46  * This class can be used with JSUPPORT::JTreeScanner so to read acoustics events in order of time-of-emission.
47  */
48  struct JEvaluator {
49  /**
50  * Default constructor.
51  */
53  {}
54 
55 
56  /**
57  * Get value of object.
58  *
59  * \param event event
60  * \return value
61  */
62  inline double operator()(const JEvent& event) const
63  {
64  return event.begin()->getToE();
65  }
66  };
67 
68 
69  /**
70  * Default constructor.
71  */
72  JEvent() :
73  JCounter(),
74  overlays(0),
75  id (-1)
76  {}
77 
78 
79  /**
80  * Constructor.
81  *
82  * The transmissions will be sorted to ensure proper functioning of method JEvent::merge and class JEventOverlap.
83  *
84  * \param oid detector identifier
85  * \param counter counter
86  * \param id identifier
87  * \param __begin begin of data
88  * \param __end end of data
89  */
90  template<class T>
92  const int counter,
93  const int id,
94  T __begin,
95  T __end) :
96  JCounter(counter),
97  oid (oid),
98  overlays(0),
99  id (id)
100  {
101  using namespace std;
102 
103  for (T i = __begin; i != __end; ++i) {
104  push_back(*i);
105  }
106 
107  sort(this->begin(), this->end());
108  }
109 
110 
111  /**
112  * Virtual destructor.
113  */
114  virtual ~JEvent()
115  {}
116 
117 
118  /**
119  * Get detector identifier.
120  *
121  * \return detector identifier.
122  */
123  const std::string& getOID() const
124  {
125  return oid;
126  }
127 
128 
129  /**
130  * Get number of overlayed events.
131  *
132  * \return overlays
133  */
134  int getOverlays() const
135  {
136  return overlays;
137  }
138 
139 
140  /**
141  * Get emitter identifier.
142  *
143  * \return identifier
144  */
145  int getID() const
146  {
147  return id;
148  }
149 
150 
151  /**
152  * Merge event.
153  *
154  * It is assumed that the transmissions in both events are ordered
155  * according the default less-than operator.
156  *
157  * \param event event
158  */
159  void merge(const JEvent& event)
160  {
161  using namespace std;
162 
163  vector<JTransmission> buffer;
164 
165  const_iterator __hit1 = this ->begin();
166  const_iterator __end1 = this ->end();
167 
168  const_iterator __hit2 = event.begin();
169  const_iterator __end2 = event.end();
170 
171  buffer.resize(this->size() + event.size());
172 
173  iterator out = buffer.begin();
174 
175  while (__hit1 != __end1 && __hit2 != __end2) {
176 
177  if (*__hit1 < *__hit2) {
178 
179  *out = *__hit1;
180  ++__hit1;
181 
182  } else if (*__hit2 < *__hit1) {
183 
184  *out = *__hit2;
185  ++__hit2;
186 
187  } else {
188 
189  *out = *__hit1;
190 
191  ++__hit1;
192  ++__hit2;
193  }
194 
195  ++out;
196  }
197 
198  // append remaining hits from either set
199 
200  out = copy(__hit1, __end1, out);
201  out = copy(__hit2, __end2, out);
202 
203  buffer.resize(distance(buffer.begin(), out));
204 
205  this->swap(buffer);
206 
207  ++overlays;
208  }
209 
210 
211  /**
212  * Swap events.
213  *
214  * \param first first event
215  * \param second second event
216  */
217  friend inline void swap(JEvent& first, JEvent& second)
218  {
219  std::swap(first.counter, second.counter);
220  std::swap(first.oid, second.oid);
221  std::swap(first.overlays, second.overlays);
222  std::swap(first.id, second.id);
223 
224  static_cast<std::vector<JTransmission>&>(first).swap(static_cast<std::vector<JTransmission>&>(second));
225  }
226 
227 
228  /**
229  * Empty overlapping events.
230  *
231  * The events should be time sorted on input.\n
232  * The time window applies to the difference between the first transmission
233  * of an event and the last transmission of the previous event.
234  *
235  * \param p begin of events
236  * \param q end of events
237  * \param Tmax_s time window [s]
238  *
239  */
240  template<class T>
241  static inline void overlap(T p, T q, const double Tmax_s)
242  {
243  for (T __q = p, __p = __q++; __p != q && __q != q; __p = __q++) {
244 
245  if (!__p->empty() &&
246  !__q->empty() &&
247  __q->begin()->getToE() < __p->rbegin()->getToE() + Tmax_s) {
248 
249  __p->clear(); // clear first
250 
251  for (__p = __q++; __p != q && __q != q; __p = __q++) {
252 
253  if (__q->begin()->getToE() < __p->rbegin()->getToE() + Tmax_s)
254  __p->clear(); // clear intermediate
255  else
256  break;
257  }
258 
259  __p->clear(); // clear last
260  }
261  }
262  }
263 
264 
265  /**
266  * Less than operator for acoustics events.
267  *
268  * The less than operator is applied to the first hit in the events.\n
269  * If there are no hits in either event, the counter of the events is used.
270  *
271  * \param first first event
272  * \param second second event
273  * \return true if first event earliear than second; else false
274  */
275  friend inline bool operator<(const JEvent& first, const JEvent& second)
276  {
277  if (!first.empty() && !second.empty())
278  return first.begin()->getToE() < second.begin()->getToE();
279  else
280  return first.getCounter() < second.getCounter();
281  }
282 
283 
284  /**
285  * Write event to output stream.
286  *
287  * \param out output stream
288  * \param event event
289  * \return output stream
290  */
291  friend inline std::ostream& operator<<(std::ostream& out, const JEvent& event)
292  {
293  using namespace std;
294 
295  out << event.getOID() << endl;
296  out << setw(8) << event.getCounter() << endl;
297  out << setw(2) << event.getOverlays() << endl;
298  out << setw(3) << event.getID() << endl;
299 
300  for (const_iterator i = event.begin(); i != event.end(); ++i) {
301 
302  out << setw(10) << i->getID() << ' '
303  << setw(10) << i->getRunNumber() << ' '
304  << fixed << setw(12) << setprecision(6) << i->getToA() << ' '
305  << fixed << setw(12) << setprecision(6) << i->getToE() << ' '
306  << fixed << setw(8) << setprecision(0) << i->getQ() << ' '
307  << fixed << setw(8) << setprecision(0) << i->getW() << endl;
308  }
309 
310  return out;
311  }
312 
313 
314  /**
315  * Read from input.
316  *
317  * \param in reader
318  * \return reader
319  */
320  virtual JReader& read(JReader& in) override
321  {
322  in >> static_cast<JCounter&>(*this);
323  in >> this->oid;
324  in >> this->overlays;
325  in >> this->id;
326  in >> static_cast<std::vector<JTransmission>&>(*this);
327 
328  return in;
329  }
330 
331 
332  /**
333  * Write to output.
334  *
335  * \param out writer
336  * \return writer
337  */
338  virtual JWriter& write(JWriter& out) const override
339  {
340  out << static_cast<const JCounter&>(*this);
341  out << this->oid;
342  out << this->overlays;
343  out << this->id;
344  out << static_cast<const std::vector<JTransmission>&>(*this);
345 
346  return out;
347  }
348 
350 
351  protected:
353  int overlays;
354  int id;
355  };
356 }
357 
358 #endif
Acoustic counter.
ClassDefOverride(JEvent, 3)
int getOverlays() const
Get number of overlayed events.
Interface for binary output.
Acoustic counter.
void merge(const JEvent &event)
Merge event.
JEvaluator()
Default constructor.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
int getCounter() const
Get counter.
JEvent()
Default constructor.
Definition: JRoot.hh:19
friend std::ostream & operator<<(std::ostream &out, const JEvent &event)
Write event to output stream.
friend void swap(JEvent &first, JEvent &second)
Swap events.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Forward declaration of binary output.
const std::string & getOID() const
Get detector identifier.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
then awk string
virtual JWriter & write(JWriter &out) const override
Write to output.
Interface for binary input.
virtual ~JEvent()
Virtual destructor.
static void overlap(T p, T q, const double Tmax_s)
Empty overlapping events.
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
Acoustic transmission.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
Acoustic event.
JEvent(const std::string &oid, const int counter, const int id, T __begin, T __end)
Constructor.
virtual JReader & read(JReader &in) override
Read from input.
int getID() const
Get emitter identifier.
friend bool operator<(const JEvent &first, const JEvent &second)
Less than operator for acoustics events.
Auxiliary class to determine value of acoustic events.
double operator()(const JEvent &event) const
Get value of object.
STD extensions for binary I/O.