Jpp  17.2.1-pre0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  }
48 
49 
50  /**
51  * Constructor.
52  *
53  * \param name name of particle
54  * \param pdg PDG code of particle
55  * \param geant GEANT code of particle
56  * \param mass mass of particle
57  */
59  const int pdg,
60  const int geant,
61  const double mass)
62  {
63  this->name = name;
64  this->pdg = pdg;
65  this->geant = geant;
66  this->mass = mass;
67  }
68 
69 
70  /**
71  * Print particle.
72  *
73  * \param out output stream
74  * \param particle particle
75  * \return output stream
76  */
77  friend inline std::ostream& operator<<(std::ostream& out, const JParticle& particle)
78  {
79  using namespace std;
80  using namespace JPP;
81 
82  return out << setw(32) << left << particle.name << " "
83  << setw( 6) << right << particle.pdg << " "
84  << setw( 6) << right << particle.geant << " "
85  << FIXED(12,10) << particle.mass;
86  }
87 
88 
89  std::string name; //!< name of particle
90  int pdg; //!< PDG code of particle
91  int geant; //!< GEANT code of particle
92  double mass; //!< mass of particle [GeV]
93  };
94 
95 
96  /**
97  * Auxiliary macro for particle creation.
98  *
99  * \param PDG PDG code
100  * \param GEANT GEANT code
101  * \param MASS mass
102  */
103 #define MAKE_PARTICLE(PDG, GEANT, MASS) JParticle(#PDG, PDG, GEANT, MASS)
104 
105 
106  /**
107  * Collection of particles.
108  */
109  struct JPDB :
110  public std::vector<JParticle>
111  {
112  /**
113  * Default constructor.
114  */
116  {}
117 
118 
119  /**
120  * Get particle data book.
121  * This particle data book contains all known GEANT particle codes.
122  *
123  * \return particle data book
124  */
125  static const JPDB& getInstance()
126  {
127  using namespace JPP;
128 
129  static JPDB pdb;
130 
131  if (pdb.empty()) {
132 
133  TDatabasePDG::Instance()->ReadPDGTable();
134 
135  TIter next(TDatabasePDG::Instance()->ParticleList());
136 
137  while (TParticlePDG* p = (TParticlePDG*) next()) {
138 
139  const JParticle particle(p->GetName(), p->PdgCode(), convertPDGtoGEANT(p->PdgCode()), p->Mass());
140 
141  pdb.push_back(particle);
142  }
143  }
144 
145  return pdb;
146  }
147 
148 
149  /**
150  * Check if PDB has particle corresponding to given GEANT code.
151  *
152  * \param geant GEANT code
153  * \return true if available; else false
154  */
155  bool hasGEANT(const int geant) const
156  {
157  return (find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::geant, geant)) != this->end());
158  }
159 
160 
161  /**
162  * Get particle corresponding to given GEANT code.\n
163  * This method throws an error if the particle cannot be found.
164  *
165  * \param geant GEANT code
166  * \return particle
167  */
168  const JParticle& getGEANT(const int geant) const
169  {
170  const_iterator p = find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::geant, geant));
171 
172  if (p != this->end())
173  return *p;
174  else
175  THROW(JException, "Invalid GEANT particle code " << geant);
176  }
177 
178 
179  /**
180  * Retrieve GEANT code for a given PDG code.
181  *
182  * \param pdg PDG code
183  * \return GEANT code
184  */
185  static int convertPDGtoGEANT(const int pdg)
186  {
187  switch (pdg) {
188 
189  case TRACK_TYPE_NUE:
190  case TRACK_TYPE_NUMU:
191  case TRACK_TYPE_NUTAU:
192  case TRACK_TYPE_ANTINUE:
193  case TRACK_TYPE_ANTINUMU:
195  return GEANT4_TYPE_NEUTRINO;
196 
199 
202 
205 
208 
209  default:
210  return TDatabasePDG::Instance()->ConvertPdgToGeant3(pdg);
211  }
212  }
213 
214 
215  /**
216  * Check if PDB has particle corresponding to given PDG code.
217  *
218  * \param pdg PDG code
219  * \return true if available; else false
220  */
221  bool hasPDG(const int pdg) const
222  {
223  return (find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::pdg, pdg)) != this->end());
224  }
225 
226 
227  /**
228  * Get particle corresponding to given PDG code.\n
229  * This method throws an error if the particle cannot be found.
230  *
231  * \param pdg PDG code
232  * \return particle
233  */
234  const JParticle& getPDG(const int pdg) const
235  {
236  const_iterator p = find_if(this->begin(), this->end(), JLANG::make_predicate(&JParticle::pdg, pdg));
237 
238  if (p != this->end())
239  return *p;
240  else
241  THROW(JException, "Invalid PDG particle code " << pdg);
242  }
243 
244 
245  /**
246  * Print particle data book.
247  *
248  * \param out output stream
249  * \param pdb particle data book
250  * \return output stream
251  */
252  friend inline std::ostream& operator<<(std::ostream& out, const JPDB& pdb)
253  {
254  using namespace std;
255 
256  out << setw(32) << left << "particle" << " "
257  << setw( 6) << right << "PDG" << " "
258  << setw( 6) << right << "GEANT" << " "
259  << setw(12) << right << "Mass [GeV]" << endl;
260 
261  for (JPDB::const_iterator i = pdb.begin(); i != pdb.end(); ++i) {
262  out << *i << endl;
263  }
264 
265  return out;
266  }
267  };
268 }
269 
270 #endif
General exception.
Definition: JException.hh:23
std::string name
name of particle
Definition: JPDB.hh:89
Exceptions.
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
static int convertPDGtoGEANT(const int pdg)
Retrieve GEANT code for a given PDG code.
Definition: JPDB.hh:185
const JParticle & getGEANT(const int geant) const
Get particle corresponding to given GEANT code.
Definition: JPDB.hh:168
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
const JParticle & getPDG(const int pdg) const
Get particle corresponding to given PDG code.
Definition: JPDB.hh:234
JParticle()
Default constructor.
Definition: JPDB.hh:41
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
Collection of particles.
Definition: JPDB.hh:109
JPDB()
Default constructor.
Definition: JPDB.hh:115
static const JPDB & getInstance()
Get particle data book.
Definition: JPDB.hh:125
bool hasGEANT(const int geant) const
Check if PDB has particle corresponding to given GEANT code.
Definition: JPDB.hh:155
friend std::ostream & operator<<(std::ostream &out, const JPDB &pdb)
Print particle data book.
Definition: JPDB.hh:252
then awk string
Physics constants.
I/O manipulators.
Auxiliary class to handle particle name, codes and mass.
Definition: JPDB.hh:37
bool hasPDG(const int pdg) const
Check if PDB has particle corresponding to given PDG code.
Definition: JPDB.hh:221
double mass
mass of particle [GeV]
Definition: JPDB.hh:92
friend std::ostream & operator<<(std::ostream &out, const JParticle &particle)
Print particle.
Definition: JPDB.hh:77
int geant
GEANT code of particle.
Definition: JPDB.hh:91
Definition of particle types.
int pdg
PDG code of particle.
Definition: JPDB.hh:90
JParticle(const std::string &name, const int pdg, const int geant, const double mass)
Constructor.
Definition: JPDB.hh:58