-
Notifications
You must be signed in to change notification settings - Fork 13
/
utilFunctions.py
132 lines (109 loc) · 3.46 KB
/
utilFunctions.py
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
import numpy as np
import math
from pyspark.sql import Row
"""
Implementation of Lorentz vector
"""
class LorentzVector(object):
def __init__(self, *args):
if len(args)>0:
self.x = args[0]
self.y = args[1]
self.z = args[2]
self.t = args[3]
def SetPtEtaPhiM(self, pt, eta, phi, mass):
pt = abs(pt)
self.SetXYZM(pt*math.cos(phi), pt*math.sin(phi), pt*math.sinh(eta), mass)
def SetXYZM(self, x, y, z, m):
self.x = x;
self.y = y
self.z = z
if (m>=0):
self.t = math.sqrt(x*x + y*y + z*z + m*m)
else:
self.t = math.sqrt(max(x*x + y*y + z*z - m*m, 0))
def E(self):
return self.t
def Px(self):
return self.x
def Py(self):
return self.y
def Pz(self):
return self.z
def Pt(self):
return math.sqrt(self.x*self.x + self.y*self.y)
def Eta(self):
cosTheta = self.CosTheta()
if cosTheta*cosTheta<1:
return -0.5*math.log((1.0 - cosTheta)/(1.0 + cosTheta))
if self.z == 0: return 0
def mag(self):
return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
def CosTheta(self):
return 1.0 if self.mag()==0.0 else self.z/self.mag()
def Phi(self):
return math.atan2(self.y, self.x)
def DeltaR(self, other):
deta = self.Eta() - other.Eta()
dphi = self.Phi() - other.Phi()
pi = math.pi
while dphi > pi: dphi -= 2*pi
while dphi < -pi: dphi += 2*pi
return math.sqrt(deta*deta + dphi*dphi)
"""
Functions used to return the Pt map of selected tracks, neutrals and photons
"""
def ChPtMapp(DR, event):
pTmap = []
for h in event.EFlowTrack:
if h.PT<= 0.5: continue
pTmap.append([h.Eta, h.Phi, h.PT])
return np.asarray(pTmap)
def NeuPtMapp(DR, event):
pTmap = []
for h in event.EFlowNeutralHadron:
if h.ET<= 1.0: continue
pTmap.append([h.Eta, h.Phi, h.ET])
return np.asarray(pTmap)
def PhotonPtMapp(DR, event):
pTmap = []
for h in event.EFlowPhoton:
if h.ET<= 1.0: continue
pTmap.append([h.Eta, h.Phi, h.ET])
return np.asarray(pTmap)
"""
Functions used to return the Pt map of selected tracks, neutrals and photons
Versions used for the optimized filtering with Spark SQL and HOF
"""
# get the selected tracks
def ChPtMapp2(Tracks):
#pTmap = []
pTmap = np.zeros((len(Tracks), 3))
for i, h in enumerate(Tracks):
pTmap[i] = [h["Eta"], h["Phi"], h["PT"]]
return pTmap
# get the selected neutrals
def NeuPtMapp2(NeutralHadrons):
pTmap = np.zeros((len(NeutralHadrons), 3))
for i, h in enumerate(NeutralHadrons):
pTmap[i] = [h["Eta"], h["Phi"], h["ET"]]
return pTmap
# get the selected photons
def PhotonPtMapp2(Photons):
pTmap = np.zeros((len(Photons), 3))
for i, h in enumerate(Photons):
pTmap[i] = [h["Eta"], h["Phi"], h["ET"]]
return pTmap
"""
Get the particle ISO
"""
def PFIso(p, DR, PtMap, subtractPt):
if p.Pt() <= 0.: return 0.
DeltaEta = PtMap[:,0] - p.Eta()
DeltaPhi = PtMap[:,1] - p.Phi()
twopi = 2.* math.pi
DeltaPhi = DeltaPhi - twopi*(DeltaPhi > twopi) + twopi*(DeltaPhi < -1.*twopi)
isInCone = DeltaPhi*DeltaPhi + DeltaEta*DeltaEta < DR*DR
Iso = PtMap[isInCone, 2].sum()/p.Pt()
if subtractPt: Iso = Iso -1
return float(Iso)