-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.cpp
More file actions
191 lines (144 loc) · 5.07 KB
/
particle.cpp
File metadata and controls
191 lines (144 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "particle.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
Particle::Particle(std::string const& name, double px, double py, double pz)
: fPx{px}, fPy{py}, fPz{pz} {
auto index = FindParticle(name);
// Converting unsigned int in signed int to make comparisons without narrowing
auto size = GetSize();
if (index == size) {
std::cerr << "No matches found for particle \'" << name << "\'\n";
} else {
fIndex = index;
}
assert(fIndex != size);
}
Particle::Particle() { fIndex = -1; }
std::vector<ParticleType*> Particle::fParticleType{};
int Particle::FindParticle(std::string const& particleName) {
auto it = std::find_if(
fParticleType.begin(), fParticleType.end(),
[&](ParticleType* p) { return particleName == p->GetName(); });
// Findng iterator position
return it - fParticleType.begin();
}
int Particle::GetIndex() const { return fIndex; }
void Particle::AddParticleType(std::string const& name, double mass, int charge,
double width) {
auto index = FindParticle(name);
auto size = GetSize();
if (index != size) {
std::cerr << "Particle \'" << name << "\' already inserted\n";
} else {
ResonanceType* resT = new ResonanceType{name, mass, charge, width};
fParticleType.push_back(resT);
std::cout << "Inserted particle \'" << name << "\' in index " << index
<< '\n';
}
}
void Particle::SetIndex(int index) {
auto size = GetSize();
// < -1 is used because default constructor sets fIndex to -1
if (index >= size || index < -1) {
std::cerr << "Particle not found\n";
} else {
fIndex = index;
}
}
void Particle::SetIndex(std::string const& name) {
SetIndex(FindParticle(name));
}
void Particle::PrintParticle() {
std::for_each(fParticleType.begin(), fParticleType.end(),
[](ParticleType* p) { p->Print(); });
}
void Particle::PrintIndex() const {
using namespace std;
// Printing data with same spacing
cout << left << setw(10) << "\nIndex:" << fIndex << left << setw(10)
<< "\nName" << fParticleType[fIndex]->GetName() << left << setw(10)
<< "\nPx:" << fPx << left << setw(10) << "\nPy:" << fPy << left
<< setw(10) << "\nPz:" << fPz << '\n';
}
double Particle::GetPx() const { return fPx; }
double Particle::GetPy() const { return fPy; }
double Particle::GetPz() const { return fPz; }
double Particle::GetMass() const { return fParticleType[fIndex]->GetMass(); }
double Particle::GetEnergy() const {
return sqrt(GetMass() * GetMass() + (fPx * fPx + fPy * fPy + fPz * fPz));
}
double Particle::GetInvMass(Particle const& p) const {
return sqrt((GetEnergy() + p.GetEnergy()) * (GetEnergy() + p.GetEnergy()) -
((fPx + p.fPx) * (fPx + p.fPx) + (fPy + p.fPy) * (fPy + p.fPy) +
(fPz + p.fPz) * (fPz + p.fPz)));
}
void Particle::SetP(double px, double py, double pz) {
fPx = px;
fPy = py;
fPz = pz;
}
int Particle::GetSize() {
int size = fParticleType.size();
return size;
}
int Particle::GetCharge() const { return fParticleType[fIndex]->GetCharge(); }
int Particle::Decay2Body(Particle& dau1, Particle& dau2) const {
if (GetMass() == 0.) {
printf("Decayment cannot be preformed if mass is zero\n");
return 1;
}
double massMot = GetMass();
double massDau1 = dau1.GetMass();
double massDau2 = dau2.GetMass();
if (fIndex > -1) { // add width effect
// gaussian random numbers
float x1, x2, w, y1, y2;
double invnum = 1. / RAND_MAX;
do {
x1 = 2.0 * rand() * invnum - 1.0;
x2 = 2.0 * rand() * invnum - 1.0;
w = x1 * x1 + x2 * x2;
} while (w >= 1.0);
w = sqrt((-2.0 * log(w)) / w);
y1 = x1 * w;
y2 = x2 * w;
massMot += fParticleType[fIndex]->GetWidth() * y1;
}
if (massMot < massDau1 + massDau2) {
printf(
"Decayment cannot be preformed because mass is too low in this "
"channel\n");
return 2;
}
double pout =
sqrt(
(massMot * massMot - (massDau1 + massDau2) * (massDau1 + massDau2)) *
(massMot * massMot - (massDau1 - massDau2) * (massDau1 - massDau2))) /
massMot * 0.5;
double norm = 2 * M_PI / RAND_MAX;
double phi = rand() * norm;
double theta = rand() * norm * 0.5 - M_PI / 2.;
dau1.SetP(pout * sin(theta) * cos(phi), pout * sin(theta) * sin(phi),
pout * cos(theta));
dau2.SetP(-pout * sin(theta) * cos(phi), -pout * sin(theta) * sin(phi),
-pout * cos(theta));
double energy = sqrt(fPx * fPx + fPy * fPy + fPz * fPz + massMot * massMot);
double bx = fPx / energy;
double by = fPy / energy;
double bz = fPz / energy;
dau1.Boost(bx, by, bz);
dau2.Boost(bx, by, bz);
return 0;
}
void Particle::Boost(double bx, double by, double bz) {
double energy = GetEnergy();
// Boost this Lorentz vector
double b2 = bx * bx + by * by + bz * bz;
double gamma = 1.0 / sqrt(1.0 - b2);
double bp = bx * fPx + by * fPy + bz * fPz;
double gamma2 = b2 > 0 ? (gamma - 1.0) / b2 : 0.0;
fPx += gamma2 * bp * bx + gamma * bx * energy;
fPy += gamma2 * bp * by + gamma * by * energy;
fPz += gamma2 * bp * bz + gamma * bz * energy;
}