Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAAnetToolkit.hh
Go to the documentation of this file.
1 #ifndef __JAANET__JAANETTOOLKIT__
2 #define __JAANET__JAANETTOOLKIT__
3 
4 #include <cstdlib>
5 #include <algorithm>
6 
14 
15 #include "JLang/JException.hh"
18 #include "JGeometry3D/JAxis3D.hh"
19 #include "JGeometry3D/JTrack3D.hh"
20 #include "JGeometry3D/JTrack3E.hh"
21 #include "JGeometry3D/JVertex3D.hh"
23 #include "JPhysics/JTimeRange.hh"
24 #include "JAAnet/JParticleTypes.hh"
25 
26 
27 /**
28  * \file
29  *
30  * Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
31  * \author mdejong
32  */
33 namespace JAANET {}
34 namespace JPP { using namespace JAANET; }
35 
36 /**
37  * Extensions to AAnet data format.
38  */
39 namespace JAANET {
40 
50 
51  /**
52  * AAshower reconstruction type for AAnet.
53  */
54  static const int AASHOWER_RECONSTRUCTION_TYPE = 101;
55 
56  /**
57  * Enumeration of hit types based on km3 codes.
58  */
59  enum JHitType_t {
60  HIT_TYPE_MUON_DIRECT = +5, //!< Direct light from muon
61  HIT_TYPE_MUON_SCATTERED = -5, //!< Scattered light from muon
62  HIT_TYPE_DELTARAYS_DIRECT = +4, //!< Direct light from delta-rays
63  HIT_TYPE_DELTARAYS_SCATTERED = -4, //!< Scattered light from delta-rays
64  HIT_TYPE_BREMSSTRAHLUNG_DIRECT = +3, //!< Direct light from Bremsstrahlung
65  HIT_TYPE_BREMSSTRAHLUNG_SCATTERED = -3, //!< Scattered light from Bremsstrahlung
66  HIT_TYPE_SHOWER_DIRECT = +99, //!< Direct light from primary shower
67  HIT_TYPE_SHOWER_SCATTERED = -99, //!< Scattered light from primary shower
68  HIT_TYPE_NOISE = -1, //!< Random noise
69  HIT_TYPE_UNKNOWN = 0 //!< Unknown source
70  };
71 
72 
73  /**
74  * Get true time of hit.
75  *
76  * \param hit hit
77  * \return true time of hit
78  */
79  inline double getTime(const Hit& hit)
80  {
81  return hit.t;
82  }
83 
84 
85  /**
86  * Get true charge of hit.
87  *
88  * \param hit hit
89  * \return true charge of hit
90  */
91  inline double getNPE(const Hit& hit)
92  {
93  return hit.a;
94  }
95 
96 
97  /**
98  * Verify hit origin.
99  *
100  * \param hit hit
101  * \return true if noise; else false
102  */
103  inline bool is_noise(const Hit& hit)
104  {
105  return hit.type == HIT_TYPE_NOISE;
106  }
107 
108 
109  /**
110  * Get time range (i.e.\ time between earliest and latest hit) of Monte Carlo event.\n
111  * Note that the global event time in not taken into account.
112  *
113  * \param event event
114  * \return time range
115  */
116  inline JTimeRange getTimeRange(const Evt& event)
117  {
119 
120  for (std::vector<Hit>::const_iterator hit = event.mc_hits.begin(); hit != event.mc_hits.end(); ++hit) {
121  if (!is_noise(*hit)) {
122  time_range.include(getTime(*hit));
123  }
124  }
125 
126  return time_range;
127  }
128 
129 
130  /**
131  * Get time range (i.e.\ time between earliest and latest hit) of Monte Carlo event.\n
132  * The resulting time range is larger than or equal to the given time window.\n
133  * Note that the global event time in not taken into account.
134  *
135  * \param event event
136  * \param T_ns time window [ns]
137  * \return time range
138  */
139  inline JTimeRange getTimeRange(const Evt& event, const JTimeRange& T_ns)
140  {
141  JTimeRange time_range = getTimeRange(event);
142 
143  if (time_range.getLength() < T_ns.getLength()) {
144  time_range.setLength(T_ns.getLength());
145  }
146 
147  return time_range;
148  }
149 
150 
151  /**
152  * Get position.
153  *
154  * \param pos position
155  * \return position
156  */
157  inline JPosition3D getPosition(const Vec& pos)
158  {
159  return JPosition3D(pos.x, pos.y, pos.z);
160  }
161 
162 
163  /**
164  * Get position.
165  *
166  * \param pos position
167  * \return position
168  */
169  inline Vec getPosition(const JPosition3D& pos)
170  {
171  return Vec(pos.getX(), pos.getY(), pos.getZ());
172  }
173 
174 
175  /**
176  * Get position.
177  *
178  * \param track track
179  * \return position
180  */
181  inline JPosition3D getPosition(const Trk& track)
182  {
183  return getPosition(track.pos);
184  }
185 
186 
187  /**
188  * Get direction.
189  *
190  * \param dir direction
191  * \return direction
192  */
193  inline JDirection3D getDirection(const Vec& dir)
194  {
195  return JDirection3D(dir.x, dir.y, dir.z);
196  }
197 
198 
199  /**
200  * Get direction.
201  *
202  * \param dir direction
203  * \return direction
204  */
205  inline Vec getDirection(const JDirection3D& dir)
206  {
207  return Vec(dir.getDX(), dir.getDY(), dir.getDZ());
208  }
209 
210 
211  /**
212  * Get direction.
213  *
214  * \param track track
215  * \return direction
216  */
217  inline JDirection3D getDirection(const Trk& track)
218  {
219  return getDirection(track.dir);
220  }
221 
222 
223  /**
224  * Get axis.
225  *
226  * \param track track
227  * \return axis
228  */
229  inline JAxis3D getAxis(const Trk& track)
230  {
231  return JAxis3D(getPosition(track), getDirection(track));
232  }
233 
234 
235  /**
236  * Get track.
237  *
238  * \param track track
239  * \return track
240  */
241  inline JTrack3E getTrack(const Trk& track)
242  {
243  return JTrack3E(JTrack3D(getAxis(track), track.t), track.E);
244  }
245 
246 
247  /**
248  * Get vertex.
249  *
250  * \param track track
251  * \return vertex
252  */
253  inline JVertex3D getVertex(const Trk& track)
254  {
255  return JVertex3D(getPosition(track), track.t);
256  }
257 
258 
259  /**
260  * Get transformation.
261  *
262  * \param track track
263  * \return transformation
264  */
266  {
267  return JTransformation3D(getPosition(track), getDirection(track));
268  }
269 
270 
271  /**
272  * Get track information.
273  *
274  * \param track track
275  * \param index index
276  * \param value default value
277  * \return actual value
278  */
279  inline double getW(const Trk& track, const size_t index, const double value)
280  {
281  if (index < track.fitinf.size())
282  return track.fitinf[index];
283  else
284  return value;
285  }
286 
287 
288  /**
289  * Test whether given track is a photon or neutral pion.
290  *
291  * \param track track
292  * \return true if photon or neutral pion; else false
293  */
294  inline bool is_photon (const Trk& track) { return ( track.type == TRACK_TYPE_PHOTON ||
295  abs(track.type) == TRACK_TYPE_NEUTRAL_PION); }
296 
297  /**
298  * Test whether given track is a neutrino.
299  *
300  * \param track track
301  * \return true if neutrino; else false
302  */
303  inline bool is_neutrino(const Trk& track) { return (abs(track.type) == TRACK_TYPE_NUE ||
304  abs(track.type) == TRACK_TYPE_NUMU ||
305  abs(track.type) == TRACK_TYPE_NUTAU); }
306 
307  /**
308  * Test whether given track is a (anti-)electron.
309  *
310  * \param track track
311  * \return true if (anti-)electron; else false
312  */
313  inline bool is_electron(const Trk& track) { return (abs(track.type) == TRACK_TYPE_ELECTRON); }
314 
315  /**
316  * Test whether given track is a (anti-)muon.
317  *
318  * \param track track
319  * \return true if (anti-)muon; else false
320  */
321  inline bool is_muon (const Trk& track) { return (abs(track.type) == TRACK_TYPE_MUON); }
322 
323  /**
324  * Test whether given track is a (anti-)tau.
325  *
326  * \param track track
327  * \return true if (anti-)tau; else false
328  */
329  inline bool is_tau (const Trk& track) { return (abs(track.type) == TRACK_TYPE_TAU); }
330 
331  /**
332  * Test whether given track is a charged pion.
333  *
334  * \param track track
335  * \return true if charged pion; else false
336  */
337  inline bool is_pion (const Trk& track) { return (abs(track.type) == TRACK_TYPE_CHARGED_PION_PLUS); }
338 
339  /**
340  * Test whether given track is a (anti-)proton.
341  *
342  * \param track track
343  * \return true if (anti-)proton; else false
344  */
345  inline bool is_proton (const Trk& track) { return (abs(track.type) == TRACK_TYPE_PROTON); }
346 
347  /**
348  * Test whether given track is a (anti-)neutron.
349  *
350  * \param track track
351  * \return true if (anti-)neutron; else false
352  */
353  inline bool is_neutron (const Trk& track) { return (abs(track.type) == TRACK_TYPE_NEUTRON); }
354 
355  /**
356  * Test whether given track is a lepton
357  *
358  * \param track track
359  * \return true if lepton; else fails
360  */
361  inline bool is_lepton (const Trk& track) { return (is_neutrino(track) ||
362  is_electron(track) ||
363  is_muon (track) ||
364  is_tau (track)); }
365 
366  /**
367  * Test whether given track is a charged lepton
368  *
369  * \param track track
370  * \return true if lepton; else fails
371  */
372  inline bool is_charged_lepton (const Trk& track) { return (is_electron(track) ||
373  is_muon (track) ||
374  is_tau (track)); }
375 
376  /**
377  * Test whether given track is a hadron
378  *
379  * \param track track
380  * \return true if hadron; else fails
381  */
382  inline bool is_hadron (const Trk& track) { return (abs(track.type) != TRACK_TYPE_PHOTON &&
383  !is_lepton(track)); }
384 
385  /**
386  * Test whether given track is a meson (is hadron and third digit of PDG code is zero)
387  *
388  * \param track track
389  * \return true if hadron; else fails
390  */
391  inline bool is_meson (const Trk& track) { return (is_hadron(track) &&
392  ((int)(track.type / 1000)) % 10 == 0); }
393 
394  /**
395  * Test whether given track is a baryon (is hadron and third digit of PDG code is not zero)
396  *
397  * \param track track
398  * \return true if hadron; else fails
399  */
400  inline bool is_baryon (const Trk& track) { return (is_hadron(track) &&
401  ((int)(track.type / 1000)) % 10 != 0); }
402 
403  /**
404  * Test whether given track corresponds to given particle type.
405  *
406  * \param track track
407  * \param type particle type
408  * \return true if matches the corresponding particle; else false
409  */
410  inline bool has_particleID (const Trk& track, const int type) { return (track.type == type); }
411 
412  /**
413  * Test whether given event has an incoming neutrino.
414  *
415  * \param evt event
416  * \return true if neutrino; else false
417  */
418  inline bool has_neutrino(const Evt& evt)
419  {
420  return !evt.mc_trks.empty() && is_neutrino(evt.mc_trks[0]);
421  }
422 
423  /**
424  * Get incoming neutrino.
425  *
426  * \param evt event
427  * \return neutrino
428  */
429  inline const Trk& get_neutrino(const Evt& evt)
430  {
431  if (has_neutrino(evt))
432  return evt.mc_trks[0];
433  else
434  THROW(JIndexOutOfRange, "This event has no neutrino.");
435  }
436 
437  /**
438  * Count the number of electrons in a given event
439  *
440  * \param evt event
441  * \return number of electrons
442  */
443  inline int count_electrons(const Evt& evt)
444  {
445  return std::count_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_electron);
446  }
447 
448  /**
449  * Test whether given event has an electron.
450  *
451  * \param evt event
452  * \return true if event has electron; else false
453  */
454  inline bool has_electron(const Evt& evt)
455  {
456  return count_electrons(evt) != 0;
457  }
458 
459  /**
460  * Get first electron from the event tracklist
461  *
462  * \param evt event
463  * \return electron
464  */
465  inline const Trk& get_electron(const Evt& evt)
466  {
467  if (count_electrons(evt) > 0)
468  return *std::find_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_electron);
469  else
470  THROW(JIndexOutOfRange, "This event has no electron.");
471  }
472 
473  /**
474  * Test whether given hit was produced by an electron
475  *
476  * Warning: This method will only work with the output of KM3Sim.
477  * For JSirene or KM3, you should check if <tt>track.id == hit.origin</tt> instead.
478  *
479  * \param hit hit
480  * \return true if hit was produced by an electron; else false
481  */
482  inline bool from_electron(const Hit& hit)
483  {
484  return hit.type == GEANT4_TYPE_ELECTRON || hit.type == GEANT4_TYPE_ANTIELECTRON;
485  }
486 
487  /**
488  * Count the number of muons in a given event
489  *
490  * \param evt event
491  * \return number of muons
492  */
493  inline int count_muons(const Evt& evt)
494  {
495  return std::count_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_muon);
496  }
497 
498  /**
499  * Test whether given event has a muon.
500  *
501  * \param evt event
502  * \return true if event has muon; else false
503  */
504  inline bool has_muon(const Evt& evt)
505  {
506  return count_muons(evt) != 0;
507  }
508 
509  /**
510  * Get first muon from the event tracklist
511  *
512  * \param evt event
513  * \return muon
514  */
515  inline const Trk& get_muon(const Evt& evt)
516  {
517  if (count_muons(evt) > 0)
518  return *std::find_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_muon);
519  else
520  THROW(JIndexOutOfRange, "This event has no muon.");
521  }
522 
523  /**
524  * Test whether given hit was produced by a muon
525  *
526  * Warning: This method will only work with the output of KM3Sim.
527  * For JSirene or KM3, you should check if <tt>track.id == hit.origin</tt> instead.
528  *
529  * \param hit hit
530  * \return true if hit was produced by a muon; else false
531  */
532  inline bool from_muon(const Hit& hit)
533  {
534  return hit.type == GEANT4_TYPE_MUON || hit.type == GEANT4_TYPE_ANTIMUON;
535  }
536 
537  /**
538  * Count the number of taus in a given event
539  *
540  * \param evt event
541  * \return number of taus
542  */
543  inline int count_taus(const Evt& evt)
544  {
545  return std::count_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_tau);
546  }
547 
548  /**
549  * Test whether given event has a tau.
550  *
551  * \param evt event
552  * \return true if event has tau; else false
553  */
554  inline bool has_tau(const Evt& evt)
555  {
556  return count_taus(evt) != 0;
557  }
558 
559  /**
560  * Get first tau from the event tracklist
561  *
562  * \param evt event
563  * \return tau
564  */
565  inline const Trk& get_tau(const Evt& evt)
566  {
567  if (count_taus(evt) > 0)
568  return *std::find_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_tau);
569  else
570  THROW(JIndexOutOfRange, "This event has no tau.");
571  }
572 
573  /**
574  * Test whether given hit was produced by a tau
575  *
576  * Warning: This method will only work with the output of KM3Sim.
577  * For JSirene or KM3, you should check if track.id == hit.origin instead.
578  *
579  * \param hit hit
580  * \return true if hit was produced by a tau; else false
581  */
582  inline bool from_tau(const Hit& hit)
583  {
584  return hit.type == GEANT4_TYPE_TAU || hit.type == GEANT4_TYPE_ANTITAU;
585  }
586 
587  /**
588  * Count the number of hadrons in a given event (not including neutral pions)
589  *
590  * \param evt event
591  * \return number of hadrons
592  */
593  inline int count_hadrons(const Evt& evt)
594  {
595  return std::count_if(evt.mc_trks.begin(), evt.mc_trks.end(), is_hadron);
596  }
597 
598  /**
599  * Test whether given hit was produced by a hadronic shower
600  *
601  * Warning: This method will only work with the output of KM3Sim.
602  * For JSirene or KM3, you should check if track.id == hit.origin instead.
603  *
604  * \param hit hit
605  * \return true if hit was produced by a hadron; else false
606  */
607  inline bool from_hadron(const Hit& hit)
608  {
609  return (!from_electron(hit) && !from_muon(hit) && !from_tau(hit));
610  }
611 
612  /**
613  * Auxiliary function to get average direction and total energy of a hadronic cascade
614  *
615  * \param evt event
616  * \return cascade (default if there are no hadrons)
617  */
618  inline Trk get_hadronic_cascade(const Evt& evt)
619  {
620  Trk cascade;
621 
622  for (std::vector<Trk>::const_iterator track = evt.mc_trks.begin(); track != evt.mc_trks.end(); ++track) {
623  if (is_hadron(*track)) {
624  cascade.E += track->E;
625  cascade.dir += track->dir * track->E;
626  cascade.t = track->t;
627  cascade.pos = track->pos;
628  }
629  }
630 
631  cascade.dir.normalize();
632 
633  return cascade;
634  }
635 
636  /**
637  * Add time offset to mc event;
638  * according to current definition, the absolute time of the event is defined by the track "t" attribute;
639  * this could change in the future if the global attribute mc_t is assigned to this purpose.
640  *
641  * \param evt event
642  * \param tOff time offset [ns]
643  */
644  inline void add_time_offset(Evt& evt, const double tOff)
645  {
646  for (std::vector<Trk>::iterator p = evt.mc_trks.begin(); p != evt.mc_trks.end(); p++) {
647  p->t += tOff;
648  }
649  }
650 }
651 
652 #endif
bool from_tau(const Hit &hit)
Test whether given hit was produced by a tau.
JVertex3D getVertex(const Trk &track)
Get vertex.
static const JRange< double, std::less< double > > DEFAULT_RANGE
Default range.
Definition: JRange.hh:568
bool is_electron(const Trk &track)
Test whether given track is a (anti-)electron.
int count_electrons(const Evt &evt)
Count the number of electrons in a given event.
JHitType_t
Enumeration of hit types based on km3 codes.
Exceptions.
Scattered light from primary shower.
bool is_proton(const Trk &track)
Test whether given track is a (anti-)proton.
const Trk & get_muon(const Evt &evt)
Get first muon from the event tracklist.
Data structure for direction in three dimensions.
Definition: JDirection3D.hh:33
JTrack3E getTrack(const Trk &track)
Get track.
Direct light from delta-rays.
double t
track time [ns] (when the particle is at pos )
Definition: Trk.hh:18
double z
Definition: Vec.hh:14
Scattered light from muon.
JTransformation3D getTransformation(const Trk &track)
Get transformation.
void add_time_offset(Evt &evt, const double tOff)
Add time offset to mc event; according to current definition, the absolute time of the event is defin...
bool has_neutrino(const Evt &evt)
Test whether given event has an incoming neutrino.
bool is_muon(const Trk &track)
Test whether given track is a (anti-)muon.
bool has_electron(const Evt &evt)
Test whether given event has an electron.
JTOOLS::JRange< double > JTimeRange
Type definition for time range (unit [ns]).
int count_muons(const Evt &evt)
Count the number of muons in a given event.
Auxiliary methods for selection of reconstructed tracks.
Vec dir
track direction
Definition: Trk.hh:17
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
bool has_particleID(const Trk &track, const int type)
Test whether given track corresponds to given particle type.
bool is_charged_lepton(const Trk &track)
Test whether given track is a charged lepton.
Scattered light from Bremsstrahlung.
Direct light from Bremsstrahlung.
double getTime(const Hit &hit)
Get true time of hit.
bool is_baryon(const Trk &track)
Test whether given track is a baryon (is hadron and third digit of PDG code is not zero) ...
double E
Energy [GeV] (either MC truth or reconstructed)
Definition: Trk.hh:19
double y
Definition: Vec.hh:14
3D track with energy.
Definition: JTrack3E.hh:30
Direct light from primary shower.
static const int AASHOWER_RECONSTRUCTION_TYPE
AAshower reconstruction type for AAnet.
bool is_noise(const Hit &hit)
Verify hit origin.
JTimeRange getTimeRange(const Evt &event)
Get time range (i.e. time between earliest and latest hit) of Monte Carlo event.
bool is_neutrino(const Trk &track)
Test whether given track is a neutrino.
Axis object.
Definition: JAxis3D.hh:38
bool is_lepton(const Trk &track)
Test whether given track is a lepton.
double getW(const Trk &track, const size_t index, const double value)
Get track information.
double a
hit amplitude (in p.e.)
Definition: Hit.hh:24
double x
Definition: Vec.hh:14
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition: Vec.hh:12
JDirection3D getDirection(const Vec &dir)
Get direction.
Trk get_hadronic_cascade(const Evt &evt)
Auxiliary function to get average direction and total energy of a hadronic cascade.
const Trk & get_electron(const Evt &evt)
Get first electron from the event tracklist.
double getDY() const
Get y direction.
Definition: JVersor3D.hh:106
double getDX() const
Get x direction.
Definition: JVersor3D.hh:95
JAxis3D getAxis(const Trk &track)
Get axis.
JPosition3D getPosition(const Vec &pos)
Get position.
std::vector< double > fitinf
place to store additional fit info, for jgandalf, see JFitParameters.hh
Definition: Trk.hh:30
double getY() const
Get y position.
Definition: JVector3D.hh:104
Vec & normalize()
Normalise this vector.
Definition: Vec.hh:159
bool is_photon(const Trk &track)
Test whether given track is a photon or neutral pion.
bool from_hadron(const Hit &hit)
Test whether given hit was produced by a hadronic shower.
int type
MC: particle type in PDG encoding.
Definition: Trk.hh:23
bool is_pion(const Trk &track)
Test whether given track is a charged pion.
Vec pos
postion of the track at time t [m]
Definition: Trk.hh:16
int count_taus(const Evt &evt)
Count the number of taus in a given event.
Definition: Hit.hh:8
const Trk & get_tau(const Evt &evt)
Get first tau from the event tracklist.
Direct light from muon.
bool has_muon(const Evt &evt)
Test whether given event has a muon.
std::vector< Hit > mc_hits
MC: list of MC truth hits.
Definition: Evt.hh:45
double t
hit time (from tdc+calibration or MC truth)
Definition: Hit.hh:23
Scattered light from delta-rays.
Definition of particle types.
double getX() const
Get x position.
Definition: JVector3D.hh:94
bool has_tau(const Evt &evt)
Test whether given event has a tau.
double getNPE(const Hit &hit)
Get true charge of hit.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
bool is_neutron(const Trk &track)
Test whether given track is a (anti-)neutron.
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
const Trk & get_neutrino(const Evt &evt)
Get incoming neutrino.
bool is_hadron(const Trk &track)
Test whether given track is a hadron.
bool is_tau(const Trk &track)
Test whether given track is a (anti-)tau.
bool from_electron(const Hit &hit)
Test whether given hit was produced by an electron.
The Trk class represents a Monte Carlo (MC) particle as well as a reconstructed track/shower.
Definition: Trk.hh:13
int type
particle type or parametrisation used for hit (mc only)
Definition: Hit.hh:28
double getZ() const
Get z position.
Definition: JVector3D.hh:115
double getDZ() const
Get z direction.
Definition: JVersor3D.hh:117
std::vector< Trk > mc_trks
MC: list of MC truth tracks.
Definition: Evt.hh:46
bool is_meson(const Trk &track)
Test whether given track is a meson (is hadron and third digit of PDG code is zero) ...
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:19
int count_hadrons(const Evt &evt)
Count the number of hadrons in a given event (not including neutral pions)
bool from_muon(const Hit &hit)
Test whether given hit was produced by a muon.