Line data Source code
1 : #include "crpropa/ParticleState.h"
2 : #include "crpropa/Units.h"
3 : #include "crpropa/Common.h"
4 : #include "crpropa/ParticleID.h"
5 : #include "crpropa/ParticleMass.h"
6 :
7 : #include "HepPID/ParticleIDMethods.hh"
8 :
9 : #include <cstdlib>
10 : #include <sstream>
11 :
12 : namespace crpropa {
13 :
14 16896991 : ParticleState::ParticleState(int id, double E, Vector3d pos, Vector3d dir): id(0), energy(0.), position(0.), direction(0.), pmass(0.), charge(0.)
15 : {
16 16896991 : setId(id);
17 16896991 : setEnergy(E);
18 16896991 : setPosition(pos);
19 16896991 : setDirection(dir);
20 16896991 : }
21 :
22 24181831 : void ParticleState::setPosition(const Vector3d &pos) {
23 : position = pos;
24 24181831 : }
25 :
26 15842561 : const Vector3d &ParticleState::getPosition() const {
27 15842561 : return position;
28 : }
29 :
30 17382232 : void ParticleState::setDirection(const Vector3d &dir) {
31 : direction = dir / dir.getR();
32 17382232 : }
33 :
34 493694 : const Vector3d &ParticleState::getDirection() const {
35 493694 : return direction;
36 : }
37 :
38 20400377 : void ParticleState::setEnergy(double newEnergy) {
39 20400377 : energy = std::max(0., newEnergy); // prevent negative energies
40 20400377 : }
41 :
42 599589 : double ParticleState::getEnergy() const {
43 599589 : return energy;
44 : }
45 :
46 2 : double ParticleState::getRigidity() const {
47 2 : return fabs(energy / charge);
48 : }
49 :
50 20401045 : void ParticleState::setId(int newId) {
51 20401045 : id = newId;
52 20401045 : pmass = particleMass(id);
53 20401045 : if (isNucleus(id)) {
54 178656 : charge = chargeNumber(id) * eplus;
55 178656 : if (id < 0)
56 4 : charge *= -1; // anti-nucleus
57 : } else {
58 20222389 : charge = HepPID::charge(id) * eplus;
59 : }
60 20401045 : }
61 :
62 229029 : int ParticleState::getId() const {
63 229029 : return id;
64 : }
65 :
66 7 : double ParticleState::getMass() const {
67 7 : return pmass;
68 : }
69 :
70 960340 : double ParticleState::getCharge() const {
71 960340 : return charge;
72 : }
73 :
74 7720 : double ParticleState::getLorentzFactor() const {
75 7720 : return energy / (pmass * c_squared);
76 : }
77 :
78 4626 : void ParticleState::setLorentzFactor(double lf) {
79 4626 : lf = std::max(0., lf); // prevent negative Lorentz factors
80 4626 : energy = lf * pmass * c_squared;
81 4626 : }
82 :
83 1 : Vector3d ParticleState::getVelocity() const {
84 1 : return direction * c_light;
85 : }
86 :
87 7 : Vector3d ParticleState::getMomentum() const {
88 7 : return direction * (energy / c_light);
89 : }
90 :
91 0 : std::string ParticleState::getDescription() const {
92 0 : std::stringstream ss;
93 0 : ss << "Particle " << id << ", ";
94 0 : ss << "E = " << energy / EeV << " EeV, ";
95 0 : ss << "x = " << position / Mpc << " Mpc, ";
96 0 : ss << "p = " << direction;
97 0 : return ss.str();
98 0 : }
99 :
100 : } // namespace crpropa
|