-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLepton.cpp
More file actions
206 lines (183 loc) · 6.69 KB
/
Lepton.cpp
File metadata and controls
206 lines (183 loc) · 6.69 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Description: Models particles with attributes like type, mass, charge, and four-momentum.
// Author: Leo Feasby
// Date: 15/03/2024
#include "Lepton.h"
#include <iostream>
#include <vector>
// Speed of light
const double Lepton::light_speed = 2.99792458e8;
// Default constructor initializing default values for a Lepton object
Lepton::Lepton()
: particle_type("electron"), rest_mass(0.511), charge(-1), four_momentum(new std::vector<double>(4, 0.0))
{
std::cout << "Default Lepton constructor called. Initialized with type: " << particle_type << ", mass: " << rest_mass << ", charge: " << charge << ", and four_momentum: [0, 0, 0, 0]" << std::endl;
}
// Parameterized constructor for custom initialization of a Lepton object
Lepton::Lepton(const std::string& type, double mass, int charge, double energy, double px, double py, double pz)
: particle_type(type), rest_mass(mass), charge(charge)
{
// Initializing four_momentum with provided parameters
four_momentum = new std::vector<double>{energy, px, py, pz};
std::cout << "Parameterized Lepton constructor called. Initialized with type: " << particle_type << ", mass: " << rest_mass << ", charge: " << charge << ", and four_momentum: [" << energy << ", " << px << ", " << py << ", " << pz << "]" << std::endl;
}
// Copy constructor for creating a deep copy of another Lepton object
Lepton::Lepton(const Lepton& other)
: particle_type(other.particle_type), rest_mass(other.rest_mass), charge(other.charge), four_momentum(new std::vector<double>(*other.four_momentum))
{
std::cout << "Calling Copy Constructor" << std::endl;
}
// Copy assignment operator for assigning one Lepton object to another
Lepton& Lepton::operator=(const Lepton& other)
{
// Logging the use of the assignment operator
std::cout << "Calling Assignment Operator" << std::endl;
if(this != &other)
{
particle_type = other.particle_type;
rest_mass = other.rest_mass;
charge = other.charge;
delete four_momentum; // Deleting existing four_momentum before assignment
four_momentum = new std::vector<double>(*other.four_momentum);
}
return *this;
}
// Move constructor for transferring ownership of resources from one Lepton object to another
Lepton::Lepton(Lepton&& other) noexcept
: particle_type(std::move(other.particle_type)), rest_mass(other.rest_mass), charge(other.charge), four_momentum(other.four_momentum)
{
// Logging the use of the move constructor
std::cout << "Calling Move Constructor" << std::endl;
other.four_momentum = nullptr; // Ensuring the moved-from object is left in a valid state
}
// Move assignment operator for transferring ownership of resources between Lepton objects
Lepton& Lepton::operator=(Lepton&& other) noexcept
{
std::cout << "Calling Move Assignment Operator" << std::endl;
if(this != &other)
{
delete four_momentum; // Deleting existing four_momentum before assignment
particle_type = std::move(other.particle_type);
rest_mass = other.rest_mass;
charge = other.charge;
four_momentum = other.four_momentum;
other.four_momentum = nullptr; // Ensuring the moved-from object is left in a valid state
}
return *this;
}
// Destructor for cleaning up dynamically allocated memory
Lepton::~Lepton()
{
std::cout << "Calling Destructor" << std::endl;
delete four_momentum;
}
// Setter methods for updating Lepton object attributes
void Lepton::set_particle_type(const std::string& type)
{
particle_type = type;
std::cout << "Particle type set to: " << type << std::endl;
}
void Lepton::set_rest_mass(double mass)
{
if(mass > 0)
{
rest_mass = mass;
std::cout << "Rest mass set to: " << mass << std::endl;
}
else
{
std::cout << "Invalid mass. It must be positive.\n";
}
}
void Lepton::set_charge(int charge)
{
this->charge = charge;
std::cout << "Charge set to: " << charge << std::endl;
}
void Lepton::set_four_momentum(double energy, double px, double py, double pz)
{
if(energy >= 0)
{
(*four_momentum)[0] = energy;
(*four_momentum)[1] = px;
(*four_momentum)[2] = py;
(*four_momentum)[3] = pz;
std::cout << "Four_momentum set to: [" << energy << ", " << px << ", " << py << ", " << pz << "]" << std::endl;
}
else
{
std::cout << "Invalid energy. It must be non-negative.\n";
}
}
// Getter methods for accessing Lepton object attributes
double Lepton::get_e() const
{
if(four_momentum != nullptr)
{
std::cout << "Getting energy: " << (*four_momentum)[0] << std::endl;
return (*four_momentum)[0];
}
else
{
std::cerr << "four_momentum is not initialized.\n";
return 0.0;
}
}
double Lepton::get_px() const
{
std::cout << "Getting Px: " << (*four_momentum)[1] << std::endl;
return (*four_momentum)[1];
}
double Lepton::get_py() const
{
std::cout << "Getting Py: " << (*four_momentum)[2] << std::endl;
return (*four_momentum)[2];
}
double Lepton::get_pz() const
{
std::cout << "Getting Pz: " << (*four_momentum)[3] << std::endl;
return (*four_momentum)[3];
}
std::string Lepton::get_particle_type() const
{
std::cout << "Getting particle type: " << particle_type << std::endl;
return particle_type;
}
double Lepton::get_rest_mass() const
{
std::cout << "Getting rest mass: " << rest_mass << std::endl;
return rest_mass;
}
int Lepton::get_charge() const
{
std::cout << "Getting charge: " << charge << std::endl;
return charge;
}
// Method for printing detailed information about the Lepton object
void Lepton::print_info() const
{
std::cout << "Particle Type: " << particle_type
<< "\nRest Mass (MeV): " << rest_mass
<< "\nCharge: " << charge
<< "\nEnergy (MeV): " << get_e()
<< "\nMomentum px (MeV/c): " << get_px()
<< "\nMomentum py (MeV/c): " << get_py()
<< "\nMomentum pz (MeV/c): " << get_pz() << '\n';
}
// Overloaded "+" operator for summing the four-momenta of two Lepton objects
Lepton Lepton::operator+(const Lepton& other) const
{
std::vector<double> summed_four_momentum(4);
for(size_t i = 0; i < 4; ++i)
{
summed_four_momentum[i] = (*this->four_momentum)[i] + (*other.four_momentum)[i];
}
return Lepton(this->particle_type, this->rest_mass, this->charge, summed_four_momentum[0], summed_four_momentum[1], summed_four_momentum[2], summed_four_momentum[3]);
}
// Method for calculating the dot product of the four-momenta of two Lepton objects
double Lepton::dotProduct(const Lepton& other) const
{
return + (*this->four_momentum)[0] * (*other.four_momentum)[0] // Energy component
+ (*this->four_momentum)[1] * (*other.four_momentum)[1] // X component
+ (*this->four_momentum)[2] * (*other.four_momentum)[2] // Y component
+ (*this->four_momentum)[3] * (*other.four_momentum)[3]; // Z component
}