Jpp  master_rocky
the software that should make you happy
JPDB.hh
Go to the documentation of this file.
1 #ifndef __JAANET__JPDB__
2 #define __JAANET__JPDB__
3 
4 #include <string>
5 #include <ostream>
6 #include <iomanip>
7 #include <vector>
8 #include <algorithm>
9 
10 #include "JAAnet/JParticleTypes.hh"
11 #include "JPhysics/JConstants.hh"
12 
13 #include "JLang/JPredicate.hh"
14 #include "JLang/JException.hh"
15 #include "JLang/JManip.hh"
16 
17 #include "TDatabasePDG.h"
18 #include "TParticlePDG.h"
19 #include "TCollection.h"
20 #include "THashList.h"
21 
22 
23 /**
24  * \author mdejong
25  */
26 
27 namespace JAANET {}
28 namespace JPP { using namespace JAANET; }
29 
30 namespace JAANET {
31 
32  using JLANG::JException;
33 
34  /**
35  * Auxiliary class to handle particle name, codes and mass.
36  */
37  struct JParticle {
38  /**
39  * Default constructor.
40  */
42  {
43  this->name = "";
44  this->pdg = 0;
45  this->geant = 0;
46  this->mass = 0.0;
47  this->charge = 0;
48  }
49 
50 
51  /**
52  * Constructor.
53  *
54  * \param name name of particle
55  * \param pdg PDG code of particle
56  * \param geant GEANT code of particle
57  * \param mass mass of particle
58  * \param charge charge of particle
59  */
60  JParticle(const std::string& name,
61  const int pdg,
62  const int geant,
63  const double mass,
64  const int charge)
65  {
66  this->name = name;
67  this->pdg = pdg;
68  this->geant = geant;
69  this->mass = mass;
70  this->charge = charge;
71  }
72 
73 
74  /**
75  * Print particle.
76  *
77  * \param out output stream
78  * \param particle particle
79  * \return output stream
80  */
81  friend inline std::ostream& operator<<(std::ostream& out, const JParticle& particle)
82  {
83  using namespace std;
84  using namespace JPP;
85 
86  return out << setw(32) << left << particle.name << " "
87  << setw( 8) << right << particle.pdg << " "
88  << setw( 6) << right << particle.geant << " "
89  << FIXED(16,10) << particle.mass << " "
90  << setw( 5) << right << particle.charge;
91  }
92 
93 
94  std::string name; //!< name of particle
95  int pdg; //!< PDG code of particle
96  int geant; //!< GEANT code of particle
97  double mass; //!< mass of particle [GeV]
98  int charge; //!< charge of particle [|e|/3]
99  };
100 
101 
102  /**
103  * Auxiliary macro for particle creation.
104  *
105  * \param PDG PDG code
106  * \param GEANT GEANT code
107  * \param MASS mass
108  */
109 #define MAKE_PARTICLE(PDG, GEANT, MASS, CHARGE) JParticle(#PDG, PDG, GEANT, MASS, CHARGE)
110 
111 
112  /**
113  * Collection of particles.
114  */
115  struct JPDB :
116  public std::vector<JParticle>
117  {
118  /**
119  * Default constructor.
120  */
122  {}
123 
124 
125  /**
126  * Get particle data book.
127  * This particle data book contains all known GEANT particle codes.
128  *
129  * \return particle data book
130  */
131  static const JPDB& getInstance()
132  {
133  using namespace JPP;
134 
135  static JPDB pdb;
136 
137  if (pdb.empty()) {
138 
139  TDatabasePDG::Instance()->ReadPDGTable();
140 
141  TIter next(TDatabasePDG::Instance()->ParticleList());
142 
143  while (TParticlePDG* p = (TParticlePDG*) next()) {
144 
145  const JParticle particle(p->GetName(), p->PdgCode(), convertPDGtoGEANT(p->PdgCode()), p->Mass(), p->Charge());
146 
147  pdb.push_back(particle);
148  }
149  }
150 
151  return pdb;
152  }
153 
154 
155  /**
156  * Check if PDB has particle corresponding to given GEANT code.
157  *
158  * \param geant GEANT code
159  * \return true if available; else false
160  */
161  bool hasGEANT(const int geant) const
162  {
163  return (find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::geant, geant)) != this->end());
164  }
165 
166 
167  /**
168  * Get particle corresponding to given GEANT code.\n
169  * This method throws an error if the particle cannot be found.
170  *
171  * \param geant GEANT code
172  * \return particle
173  */
174  const JParticle& getGEANT(const int geant) const
175  {
176  const_iterator p = find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::geant, geant));
177 
178  if (p != this->end())
179  return *p;
180  else
181  THROW(JException, "Invalid GEANT particle code " << geant);
182  }
183 
184 
185  /**
186  * Retrieve GEANT code for a given PDG code.
187  *
188  * \param pdg PDG code
189  * \return GEANT code
190  */
191  static int convertPDGtoGEANT(const int pdg)
192  {
193  switch (pdg) {
194 
195  case TRACK_TYPE_NUE:
196  case TRACK_TYPE_NUMU:
197  case TRACK_TYPE_NUTAU:
198  case TRACK_TYPE_ANTINUE:
199  case TRACK_TYPE_ANTINUMU:
201  return GEANT4_TYPE_NEUTRINO;
202 
205 
208 
211 
214 
215  default:
216  return TDatabasePDG::Instance()->ConvertPdgToGeant3(pdg);
217  }
218  }
219 
220 
221  /**
222  * Check if PDB has particle corresponding to given PDG code.
223  *
224  * \param pdg PDG code
225  * \return true if available; else false
226  */
227  bool hasPDG(const int pdg) const
228  {
229  return (find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::pdg, pdg)) != this->end());
230  }
231 
232 
233  /**
234  * Get particle corresponding to given PDG code.\n
235  * This method throws an error if the particle cannot be found.
236  *
237  * \param pdg PDG code
238  * \return particle
239  */
240  const JParticle& getPDG(const int pdg) const
241  {
242  const_iterator p = find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::pdg, pdg));
243 
244  if (p != this->end())
245  return *p;
246  else
247  THROW(JException, "Invalid PDG particle code " << pdg);
248  }
249 
250 
251  /**
252  * Print particle data book.
253  *
254  * \param out output stream
255  * \param pdb particle data book
256  * \return output stream
257  */
258  friend inline std::ostream& operator<<(std::ostream& out, const JPDB& pdb)
259  {
260  using namespace std;
261 
262  out << setw(32) << left << "particle" << " "
263  << setw( 8) << right << "PDG" << " "
264  << setw( 6) << right << "GEANT" << " "
265  << setw(16) << right << "Mass [GeV]" << " "
266  << setw( 5) << right << "Charge [|e|/3]" << endl;
267 
268  for (JPDB::const_iterator i = pdb.begin(); i != pdb.end(); ++i) {
269  out << *i << endl;
270  }
271 
272  return out;
273  }
274  };
275 }
276 
277 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
I/O manipulators.
Definition of particle types.
Physics constants.
General exception.
Definition: JException.hh:24
Extensions to Evt data format.
@ GEANT4_TYPE_NEUTRAL_ANTISIGMA
@ GEANT4_TYPE_ANTISIGMA_PLUS
@ GEANT4_TYPE_NEUTRINO
@ GEANT4_TYPE_ANTISIGMA_MINUS
@ GEANT4_TYPE_NEUTRAL_ANTIXI
@ TRACK_TYPE_NUTAU
@ TRACK_TYPE_ANTINUMU
@ TRACK_TYPE_NEUTRAL_ANTISIGMA
@ TRACK_TYPE_ANTINUE
@ TRACK_TYPE_ANTINUTAU
@ TRACK_TYPE_NUE
@ TRACK_TYPE_ANTISIGMA_MINUS
@ TRACK_TYPE_NEUTRAL_ANTIXI
@ TRACK_TYPE_ANTISIGMA_PLUS
@ TRACK_TYPE_NUMU
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448
Collection of particles.
Definition: JPDB.hh:117
JPDB()
Default constructor.
Definition: JPDB.hh:121
bool hasGEANT(const int geant) const
Check if PDB has particle corresponding to given GEANT code.
Definition: JPDB.hh:161
const JParticle & getPDG(const int pdg) const
Get particle corresponding to given PDG code.
Definition: JPDB.hh:240
static int convertPDGtoGEANT(const int pdg)
Retrieve GEANT code for a given PDG code.
Definition: JPDB.hh:191
friend std::ostream & operator<<(std::ostream &out, const JPDB &pdb)
Print particle data book.
Definition: JPDB.hh:258
const JParticle & getGEANT(const int geant) const
Get particle corresponding to given GEANT code.
Definition: JPDB.hh:174
static const JPDB & getInstance()
Get particle data book.
Definition: JPDB.hh:131
bool hasPDG(const int pdg) const
Check if PDB has particle corresponding to given PDG code.
Definition: JPDB.hh:227
Auxiliary class to handle particle name, codes and mass.
Definition: JPDB.hh:37
int geant
GEANT code of particle.
Definition: JPDB.hh:96
JParticle()
Default constructor.
Definition: JPDB.hh:41
std::string name
name of particle
Definition: JPDB.hh:94
double mass
mass of particle [GeV]
Definition: JPDB.hh:97
int charge
charge of particle [|e|/3]
Definition: JPDB.hh:98
JParticle(const std::string &name, const int pdg, const int geant, const double mass, const int charge)
Constructor.
Definition: JPDB.hh:60
friend std::ostream & operator<<(std::ostream &out, const JParticle &particle)
Print particle.
Definition: JPDB.hh:81
int pdg
PDG code of particle.
Definition: JPDB.hh:95