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 16898186 : ParticleState::ParticleState(int id, double E, Vector3d pos, Vector3d dir): id(0), energy(0.), position(0.), direction(0.), pmass(0.), charge(0.)
15 : {
16 16898186 : setId(id);
17 16898186 : setEnergy(E);
18 16898186 : setPosition(pos);
19 16898186 : setDirection(dir);
20 16898186 : }
21 :
22 24183273 : void ParticleState::setPosition(const Vector3d &pos) {
23 : position = pos;
24 24183273 : }
25 :
26 15845136 : const Vector3d &ParticleState::getPosition() const {
27 15845136 : return position;
28 : }
29 :
30 17383427 : void ParticleState::setDirection(const Vector3d &dir) {
31 : direction = dir / dir.getR();
32 17383427 : }
33 :
34 493463 : const Vector3d &ParticleState::getDirection() const {
35 493463 : return direction;
36 : }
37 :
38 20401524 : void ParticleState::setEnergy(double newEnergy) {
39 20401524 : energy = std::max(0., newEnergy); // prevent negative energies
40 20401524 : }
41 :
42 598983 : double ParticleState::getEnergy() const {
43 598983 : return energy;
44 : }
45 :
46 2 : double ParticleState::getRigidity() const {
47 2 : return fabs(energy / charge);
48 : }
49 :
50 20402420 : void ParticleState::setId(int newId) {
51 20402420 : id = newId;
52 20402420 : pmass = particleMass(id);
53 20402420 : if (isNucleus(id)) {
54 178526 : charge = chargeNumber(id) * eplus;
55 178526 : if (id < 0)
56 4 : charge *= -1; // anti-nucleus
57 : } else {
58 20223894 : charge = HepPID::charge(id) * eplus;
59 : }
60 20402420 : }
61 :
62 226344 : int ParticleState::getId() const {
63 226344 : 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 6214 : double ParticleState::getLorentzFactor() const {
75 6214 : return energy / (pmass * c_squared);
76 : }
77 :
78 4167 : void ParticleState::setLorentzFactor(double lf) {
79 4167 : lf = std::max(0., lf); // prevent negative Lorentz factors
80 4167 : energy = lf * pmass * c_squared;
81 4167 : }
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
|