particle.py 1.73 KB
Newer Older
Rafael Pezzi's avatar
Rafael Pezzi committed
1
2
# -*- coding: utf-8 -*-
# Sample Particle class and function that creates N particles, returning a list of particles
3
4
import math
import random
Rafael Pezzi's avatar
Rafael Pezzi committed
5

6
# Basic particle class to store bascic information
Rafael Pezzi's avatar
Rafael Pezzi committed
7
class Particle:
8
    def __init__(self, index, x = 0, y = 0, z = 0, charge = 1, mass=0.94):
Rafael Pezzi's avatar
Rafael Pezzi committed
9
10
11
12
        self.iDx=index
        self.x=x
        self.y=y
        self.z=z
13
14
15
16
17
18
        self.charge=charge
        self.mass = mass
    def PrintPosition(self):
        print(str(self.x) + " ; " + str(self.y) + " ; " + str(self.z))
    def GetPosition(self):
        return self.x, self.y, self.z
Rafael Pezzi's avatar
Rafael Pezzi committed
19

20
21
22

# Derived class to computes the time evolution particle positions
class ParticlePropagator(Particle):
Rafael Pezzi's avatar
Rafael Pezzi committed
23
    def SetMagneticField(self, B = 0.5):
24
        self.B = B
25
26
    def SetProperties(self, Px, Py, Pz, charge = 1):
        self.charge = charge
27
28
29
30
31
32
33
34
35
36
37
38
39
        self.Px = Px # unit: Gev/c
        self.Py = Py # unit: Gev/c
        self.Pz = Pz # unit: Gev/c
        self.Velocity = 1/math.sqrt(1+self.mass*self.mass/(Px*Px+Py*Py+Pz*Pz)) # unit: []c
        self.LorentzFactor = 1 / math.sqrt( 1 - self.Velocity * self.Velocity )
        self.Vz = 300 * Pz / self.LorentzFactor / self.mass # unit: meters/micro seconds
    def Propagate(self, time):
        Rx = self.Px / (self.charge * self.B) * 3.335641 # unit conversion to meters
        Ry = self.Py / (self.charge * self.B) * 3.335641 # unit conversion to meters
        omega = self.charge * self.B / ( self.LorentzFactor * self.mass ) * 89.876 # Angular frequency (unit: radians/micro seconds)
        Xprop = self.x + Rx * math.sin(omega*time) - Ry * ( math.cos(omega*time) - 1 )
        Yprop = self.y + Ry * math.sin(omega*time) + Rx * ( math.cos(omega*time) - 1 )
        Zprop = self.z + self.Vz * time
40
        return Xprop, Yprop, Zprop