def test_velocity(mom_input, mass_input, expected): """ Checks that the velocity method returns the correct velocity with a given mass and momentum """ particle = Particle(momentum=mom_input, mass=mass_input) assert (particle.velocity() == expected).all()
def test_kinetic_energy(mom_input, mass_input, expected): """ Checks that the kinetic energy method returns the correct kinetic energy with a given mass and momentum """ particle = Particle(momentum=mom_input, mass=mass_input) assert particle.kinetic_energy() == expected
def test_vector_between(pos1_input, pos2_input, expected): """ Checks that the vector_between method calculates the correct vector """ particle1 = Particle(position=pos1_input) particle2 = Particle(position=pos2_input) assert (np.abs(Particle.vector_between(particle1, particle2) - expected).value < [1e-15, 1e-15, 1e-15]).all()
def test_get_potential_energy(): """ Tests grav_field.get_potential_energy() """ particle1 = Particle(position=[0, 0, 3], mass=3.0) particle2 = Particle(position=[0, 4, 0], mass=5.0) pot_energy = get_potential_energy(particle1, particle2) # Floating point arithmetic has small rounding errors assert np.abs(pot_energy - G * -3 * (u.kg ** 2 / u.m)).value < 1e-15
def test_get_force(power, expected): """ Tests grav_field.get_force() """ particle1 = Particle(position=[0, 0, 3 * power], mass=3 * power) particle2 = Particle(position=[0, 4 * power, 0], mass=5 * power) force = get_force(particle1, particle2) # Floating point arithmetic has small rounding errors assert(np.abs(force - G * np.array(expected) * (u.kg ** 2 / u.m ** 2)).value < [1e-16, 1e-16, 1e-16]).all()
def test_apply_force(force_input, expected): """ Checks that the apply force method correctly updates the momentum Test particle has a mass of 1.0 kg starts stationary at the origin Test force is applied for 0.5 s """ particle = Particle() particle.apply_force(force_input, 0.5 * u.s) assert (np.abs(particle.momentum - expected).value < [1e-16, 1e-16, 1e-16 ]).all()
def test_move_particle(mom_input, time_input, expected): """ Checks that the move_particle method correctly updates the position Test particle has a mass of 1.0 kg starts at the origin with momentum mom_input Test force is applied for time_input s """ particle = Particle(momentum=mom_input) particle.move_particle(time_input) assert (np.abs(particle.position - expected).value < [1e-15, 1e-15, 1e-15 ]).all()
def get_particles(particle_count): """ Calculates the locations of the particles in the simulation and returns a list of them """ particles = [] total_mass = 3e48 size = 1e25 row_length = int(np.round(particle_count**(1 / 3))) particle_mass = total_mass / particle_count seperation = size / (row_length) centre = np.array([size, size, size]) / 2 for i in range(row_length): for j in range(row_length): for k in range(row_length): particle_pos_x = seperation * (i + 0.5 + (random() - 0.5)) particle_pos_y = seperation * (j + 0.5 + (random() - 0.5)) particle_pos_z = seperation * (k + 0.5 + (random() - 0.5)) particle_pos = np.array( [particle_pos_x, particle_pos_y, particle_pos_z]) - centre particles.append( Particle(position=particle_pos, mass=particle_mass)) # Check particle_count is cubic assert len(particles) == particle_count return particles
def get_potential_energy(particle1, particle2): """ Calculates the potential energy of particle1 in the field of particle2 Returns the potential energy in J """ pos_diff = Particle.vector_between(particle1, particle2) return -1 * (G * particle1.mass * particle2.mass) / np.sqrt( np.dot(pos_diff, pos_diff))
def w_mass_semi_leptonic(ecm, tree, histdict, reco_level): '''Run over the events and fill the histograms''' from_tree = TreeInfo() nDiscarded = 0 for iev, evt in enumerate(tree): if iev % 5000 == 0: print("Processing event {} on {} ".format(iev, tree.GetEntries())) # Object from_tree to access the rootfile information from_tree.set_evt(evt, reco_level) if from_tree.get_dim("lepton") != 1 or from_tree.get_dim("jet") != 2: nDiscarded += 1 continue if not check_jet_compo(from_tree): nDiscarded += 1 continue #### Leptonic decay part lepton = Particle(from_tree.get_p4("lepton")) misenergy = Particle(from_tree.get_p4("misenergy")) # Compute W mass from the lepton and the missing energy lep_decay = LeptonicDecay(lepton, misenergy) lep_decay.set_true_mass(from_tree.get_w_mass("leptonic_W")) lep_decay.compute() #### hadronic decay part jets = [Jet() for i in range(from_tree.get_dim("jet"))] for jid, jet in enumerate(jets): njet = str("jet" + str(jid + 1)) jet.set_p4(from_tree.get_p4(njet)) # Compute W mass from the jet and their angle dijet = Dijet(jets[0], jets[1]) dijet.set_true_mass(from_tree.get_w_mass("hadronic_W")) dijet.compute() fill_histograms(ecm, histdict, dijet, lep_decay) print nDiscarded
def coefficients(tree, histdict): '''Computation of the rescaling coefficient, beta, theta and phi uncertainties''' from_tree = TreeInfo() # Run on events for iev, evt in enumerate(tree): if iev % 5000 == 0: print("Processing events {} over {}".format( iev, tree.GetEntries())) # Object from_tree to access the rootfile information from_tree.set_evt(evt) if from_tree.get_dim("reco_jet") != 2: continue if from_tree.get_dim("quark") != 2: continue if from_tree.get_pdgid("reco_lepton") < -90: continue # if evt.gen_jet2_theta < -1. or evt.gen_jet2_theta > 1.: # continue ######### Leptonic part g_lepton = Particle(from_tree.get_p4("gen_lepton")) r_lepton = Particle(from_tree.get_p4("reco_lepton")) if not r_lepton: continue histdict["h_uncert_energy"].Fill(r_lepton.get_E() - g_lepton.get_E()) ######## Hadronic part partons = [Particle() for i in range(from_tree.get_dim("quark"))] for iparton, parton in enumerate(partons): nparton = str("quark" + str(iparton + 1)) parton.set_p4(from_tree.get_p4(nparton)) gen_jets = [Jet() for i in range(from_tree.get_dim("gen_jet"))] for ijet, jet in enumerate(gen_jets): njet = str("gen_jet" + str(ijet + 1)) jet.set_p4(from_tree.get_p4(njet)) rec_jets = [Jet() for i in range(from_tree.get_dim("reco_jet"))] for ijet, jet in enumerate(rec_jets): njet = str("reco_jet" + str(ijet + 1)) jet.set_p4(from_tree.get_p4(njet)) if not rec_jets: continue association(partons, rec_jets, gen_jets, histdict)
def coefficients(tree, histdict, reco_type=''): '''Computation of the rescaling coefficient, theta and phi uncertainty''' from_tree = TreeInfo() # Run on events for iev, evt in enumerate(tree): if iev < 50000: if iev%5000 == 0: print("Processing events {} over {}".format(iev, tree.GetEntries())) # Object from_tree to access the rootfile information from_tree.set_evt(evt) if from_tree.get_dim("reco_jet{}".format(reco_type)) != 4: continue if from_tree.get_dim("quark") != 4: continue partons = [Particle() for i in range(from_tree.get_dim("quark"))] for iparton, parton in enumerate(partons): nparton = str("quark" + str(iparton + 1)) parton.set_p4(from_tree.get_p4(nparton)) gen_jets = [Jet()for i in range(from_tree.get_dim("gen_jet{}".format(reco_type)))] for ijet, jet in enumerate(gen_jets): njet = str("gen_jet" + str(ijet + 1) + reco_type) if from_tree.get_p4(njet).E() < 0: continue jet.set_p4(from_tree.get_p4(njet)) rec_jets = [Jet()for i in range(from_tree.get_dim("reco_jet{}".format(reco_type)))] for ijet, jet in enumerate(rec_jets): njet = str("reco_jet" + str(ijet + 1) + reco_type) if from_tree.get_p4(njet).E() < 0: continue jet.set_p4(from_tree.get_p4(njet)) if not rec_jets: continue association(partons, rec_jets, gen_jets, histdict) else: break
def get_force(particle1, particle2): """ Calculates the force on particle1 by particle2 F_g = (G m_1 m_2)/(r.r) * unit vec r Returns force in N """ pos_diff = Particle.vector_between(particle1, particle2) distance = np.sqrt(pos_diff.dot(pos_diff)) # If particles in the exact same place return 0 if (pos_diff == 0.0).all(): return np.array([0.0] * 3) force = ((G * particle1.mass * particle2.mass) / (distance**3)) * pos_diff # Soften force if particles are close if distance.value < 1e19: force = force * (distance / (1e19 * u.m))**3 return force
def get_locations(self): """ Calculates the locations for the particles and returns a list of particles in those locations """ particles = [] for _ in range(self.particle_count): phi = random.random() * 2 * np.pi costheta = random.random() * 2 - 1 theta = np.arccos(costheta) distance = random.random()**0.7 * self.radius x_pos = distance * np.sin(theta) * np.cos(phi) y_pos = distance * np.sin(theta) * np.sin(phi) z_pos = distance * np.cos(theta) particle_pos = np.array([x_pos, y_pos, z_pos]) particle = Particle(particle_pos, mass=self.particle_mass) particles.append(particle) return particles
def coefficients(tree, histdict, reco_type=''): '''Computation of the rescaling coefficient, theta and phi uncertainty''' from_tree = TreeInfo() # Run on events for iev, evt in enumerate(tree): if iev % 5000 == 0: print("Processing events {} over {}".format( iev, tree.GetEntries())) # Object from_tree to access the rootfile information from_tree.set_evt(evt) #value for normalisation histweight = from_tree.get_file_type() if (histweight == 1 or histweight == 2 or histweight == 3): print histweight #value distinguish ZZllll from ZZqqll from ZZqqqq in ZZall ZZqqll = 0 ZZqqqq = 0 if (histweight == 3 & from_tree.get_dim("quark") == 2): ZZqqll = 1 if (histweight == 3 & from_tree.get_dim("quark") == 4): ZZqqqq = 1 #ecarte les evenements purement leptoniques du background ZZAll #if (histweight == 3 & from_tree.get_dim("quark") == 0): # continue #if (histweight==0 or histweight ==3): #if from_tree.get_dim("reco_jet{}".format(reco_type)) != 4: # continue #if(histweight==1 or histweight ==2): # if from_tree.get_dim("reco_jet{}".format(reco_type)) != 2: # continue #if from_tree.get_dim("quark") != 4: # continue partons = [Particle() for i in range(from_tree.get_dim("quark"))] #print "range partons "+str(from_tree.get_dim("quark")) for iparton, parton in enumerate(partons): nparton = str("quark" + str(iparton + 1)) #print "nparton "+str(nparton) parton.set_p4(from_tree.get_p4(nparton)) gen_jets = [ Jet() for i in range(from_tree.get_dim("gen_jet{}".format(reco_type))) ] #print "gen_jets"+str(from_tree.get_dim("gen_jet{}".format(reco_type))) for ijet, jet in enumerate(gen_jets): #print "range ijet "+str(gen_jets) njet = str("gen_jet" + str(ijet + 1) + reco_type) #print njet if from_tree.get_p4(njet).E() < 0: continue jet.set_p4(from_tree.get_p4(njet)) rec_jets = [ Jet() for i in range(from_tree.get_dim("reco_jet{}".format(reco_type))) ] for ijet, jet in enumerate(rec_jets): njet = str("reco_jet" + str(ijet + 1) + reco_type) if from_tree.get_p4(njet).E() < 0: continue jet.set_p4(from_tree.get_p4(njet)) if not rec_jets: print "No rec_jets" continue #print iev association(partons, rec_jets, gen_jets, histdict, histweight, ZZqqll, ZZqqqq)