-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangles.py
More file actions
290 lines (234 loc) · 11.6 KB
/
angles.py
File metadata and controls
290 lines (234 loc) · 11.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import numpy as np
import itertools as it
#--------------------------------------------------------------------------------
def AppendGenJetVariableNames (name_list,Njets_recorded = 6,
keywords = ['Pt','Rapidity','Eta','Phi']) :
"""
This function appends jet variables to list of names.
params:
name_list : list - list of names
Njets_recorded : int - number of jet information available (default : 6)
keywords : list - list of variables names
(default : ['Pt','Rapidity','Eta','Phi'])
returns :
name_list : list - list of names where all the jet information
is added.
"""
for i in xrange(Njets_recorded) :
for key in keywords :
word = 'genJet2p5'+key+str(i)
name_list.append(word)
return name_list
#================================================================================
#--------------------------------------------------------------------------------
def AppendPhotonVariableNames (name_list) :
"""
This function appends the leading/subleading eta and phi to a list of names
params:
name_list : list - list of names
returns :
name_list : list - list of names where information about photon
eta and phi are added.
"""
name_list.append('genLeadEta')
name_list.append('genLeadPhi')
name_list.append('genSubleadEta')
name_list.append('genSubleadPhi')
return name_list
#================================================================================
#--------------------------------------------------------------------------------
def absCosDeltaPhi (phi1,phi2) :
"""
This fucntion returns the absolute cosine of the angular difference in
the transverse plane
"""
if (phi1==-999 or phi2==-999) :
return -999
else :
return abs(np.cos(phi1-phi2))
#================================================================================
#--------------------------------------------------------------------------------
def absCosDeltaAlpha (px,py,pz,qx,qy,qz) :
"""
This function returns the absolute cosine of the angular difference between
two object in 3-d according to their momenta.
"""
if (px==-999 or qx==-999) :
return -999
else :
scalar_product = px*qx+py*qy+pz*qz
p = np.sqrt(px**2+py**2+pz**2)
q = np.sqrt(qx**2+qy**2+qz**2)
return abs(scalar_product/(p*q))
#================================================================================
#--------------------------------------------------------------------------------
def cot (angle) :
"""
This function returns the cotangent
"""
return 1./np.tan(angle)
#================================================================================
#--------------------------------------------------------------------------------
def absCosDeltaAlphaPhotonJet (phi_Gamma,eta_Gamma, phi_Jet, eta_Jet) :
"""
This function returns the absolute cosine of the angular difference between
two object in 3-d according to their phi and eta (pseudo-rapidity).
Despite the name it can be also used for a generic combination of objects,
not only photon - jet.
"""
if (phi_Jet == -999) :
return -999
else :
"""go from eta to theta!!!"""
theta_Gamma = 2.* np.arctan(np.exp((-1)*eta_Gamma))
theta_Jet = 2.* np.arctan(np.exp((-1)*eta_Jet))
scalar_product = np.cos(phi_Gamma)*np.cos(phi_Jet) + np.sin(phi_Gamma)*np.sin(phi_Jet) + (cot(theta_Gamma)*cot(theta_Jet))
denominator_inv = np.sin(theta_Gamma)*np.sin(theta_Jet) # np.sqrt((1.+(cot(theta_Gamma)**2))*(1.+(cot(theta_Jet)**2)))
return abs(scalar_product*denominator_inv)
#--------------------------------------------------------------------------------
def DeltaRinEtaPhiPlane (phi_1,eta_1, phi_2, eta_2) :
if (phi_2 == -999 or phi_1 == -999) :
return -999
else :
# transform phi properly to [0, 2pi]
DeltaR2 = (np.arcsin(np.sin(phi_1-phi_2)))**2 + (eta_1 - eta_2)**2
return np.sqrt(DeltaR2)
#--------------------------------------------------------------------------------
def AntiKTDist (pT_1,pT_2,DeltaR_12) :
if (DeltaR_12 == -999) :
return -999
else :
#cone parameter of the anti-kt algorithm
R = 0.4
pT_pick = np.maximum(pT_1,pT_2)
return (DeltaR_12 / (pT_pick*R))**2
#--------------------------------------------------------------------------------
def AddAntiKTDistance(df) :
MaxJetRecorded = 6
# adding anti_KT distance between up to six jets
for i,j in it.combinations(np.arange(MaxJetRecorded),2) :
# adding Delta R between leading photon and jets
DistVar = 'DistAntiKT'+str(i)+str(j)
col1_pt = 'genJet2p5Pt'+str(i)
col2_pt = 'genJet2p5Pt'+str(j)
Delta_R = 'DeltaR'+str(i)+str(j)
df[DistVar] = df[[col1_pt,col2_pt,Delta_R]].apply(lambda x : AntiKTDist(*x),axis=1)
return df
#--------------------------------------------------------------------------------
def AddDistanceToDataFrame (df) :
MaxJetRecorded = 6
for i in xrange(MaxJetRecorded) :
# adding Delta R between leading photon and jets
DistVar = 'DeltaRLeadGamma'+str(i)
col1_phi = 'genLeadPhi'
col1_eta = 'genLeadEta'
col2_phi = 'genJet2p5Phi'+str(i)
col2_eta = 'genJet2p5Eta'+str(i)
df[DistVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : DeltaRinEtaPhiPlane(*x),axis=1)
#----------------------------------------------------------------------
# adding Delta R between subleading photon and jets
DistVar = 'DeltaRSubleadGamma'+str(i)
col1_phi = 'genSubleadPhi'
col1_eta = 'genSubleadEta'
col2_phi = 'genJet2p5Phi'+str(i)
col2_eta = 'genJet2p5Eta'+str(i)
df[DistVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : DeltaRinEtaPhiPlane(*x),axis=1)
#----------------------------------------------------------------------
# adding Delta R between leading photon and subleading photon
DistVar = 'DeltaRLeadGammaSubleadGamma'
col1_phi = 'genLeadPhi'
col1_eta = 'genLeadEta'
col2_phi = 'genSubleadPhi'
col2_eta = 'genSubleadEta'
df[DistVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : DeltaRinEtaPhiPlane(*x),axis=1)
#----------------------------------------------------------------------
# adding Delta R between up to six jets
for i,j in it.combinations(np.arange(MaxJetRecorded),2) :
# adding Delta R between leading photon and jets
DistVar = 'DeltaR'+str(i)+str(j)
col1_phi = 'genJet2p5Phi'+str(i)
col1_eta = 'genJet2p5Eta'+str(i)
col2_phi = 'genJet2p5Phi'+str(j)
col2_eta = 'genJet2p5Eta'+str(j)
df[DistVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : DeltaRinEtaPhiPlane(*x),axis=1)
return df
#--------------------------------------------------------------------------------
def AddAngularVariablesToDataFrame (df, JetAngles = True, JetGammaAngles=True, GammaGammaAngles=True) :
MaxJetRecorded = 6
if GammaGammaAngles :
# adding abs Delta phi between leading photon and subleading photon
phiVar = 'absCosDeltaPhiLeadGammaSubleadGamma'
col1 = 'genLeadPhi'
col2 = 'genSubleadPhi'
df[phiVar] = df[[col1,col2]].apply(lambda x : absCosDeltaPhi(*x),axis=1)
#----------------------------------------------------------------------
# adding abs Delta alpha between leading photon and subleading photon
alphaVar = 'absCosDeltaAlphaLeadGammaSubleadGamma'
col1_phi = 'genLeadPhi'
col1_eta = 'genLeadEta'
col2_phi = 'genSubleadPhi'
col2_eta = 'genSubleadEta'
df[alphaVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : absCosDeltaAlphaPhotonJet(*x),axis=1)
#----------------------------------------------------------------------
if JetAngles :
for i,j in it.combinations(np.arange(MaxJetRecorded),2) :
# adding abs Delta phi between jets
phiVar = 'absCosDeltaPhi'+str(i)+str(j)
col1 = 'genJet2p5Phi'+str(i)
col2 = 'genJet2p5Phi'+str(j)
#print phiVar
df[phiVar] = df[[col1,col2]].apply(lambda x : absCosDeltaPhi(*x),axis=1)
#----------------------------------------------------------------------
"""
# adding abs Delta alpha between jets
alphaVar = 'absCosDeltaAlpha'+str(i)+str(j)
col1_x = 'genJet2p5Px'+str(i)
col1_y = 'genJet2p5Py'+str(i)
col1_z = 'genJet2p5Pz'+str(i)
col2_x = 'genJet2p5Px'+str(j)
col2_y = 'genJet2p5Py'+str(j)
col2_z = 'genJet2p5Pz'+str(j)
df[alphaVar] = df[[col1_x,col1_y,col1_z,col2_x,col2_y,col2_z]].apply(lambda x : absCosDeltaAlpha(*x),axis=1)
#----------------------------------------------------------------------
"""
# checked and this way is correct as well
# adding abs Delta alpha between jets in another way
alphaVar = 'absCosDeltaAlpha'+str(i)+str(j)
col1_phi = 'genJet2p5Phi'+str(i)
col1_eta = 'genJet2p5Eta'+str(i)
col2_phi = 'genJet2p5Phi'+str(j)
col2_eta = 'genJet2p5Eta'+str(j)
df[alphaVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : absCosDeltaAlphaPhotonJet(*x),axis=1)
if JetGammaAngles :
for i in xrange(MaxJetRecorded) :
# adding abs Delta phi between leading photon and jets
phiVar = 'absCosDeltaPhiLeadGamma'+str(i)
col1 = 'genLeadPhi'
col2 = 'genJet2p5Phi'+str(i)
#print phiVar
df[phiVar] = df[[col1,col2]].apply(lambda x : absCosDeltaPhi(*x),axis=1)
#----------------------------------------------------------------------
# adding abs Delta phi between subleading photon and jets
phiVar = 'absCosDeltaPhiSubleadGamma'+str(i)
col1 = 'genSubleadPhi'
col2 = 'genJet2p5Phi'+str(i)
#print phiVar
df[phiVar] = df[[col1,col2]].apply(lambda x : absCosDeltaPhi(*x),axis=1)
#----------------------------------------------------------------------
# adding abs Delta alpha between leading photon and jets
alphaVar = 'absCosDeltaAlphaLeadGamma'+str(i)
col1_phi = 'genLeadPhi'
col1_eta = 'genLeadEta'
col2_phi = 'genJet2p5Phi'+str(i)
col2_eta = 'genJet2p5Eta'+str(i)
df[alphaVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : absCosDeltaAlphaPhotonJet(*x),axis=1)
#----------------------------------------------------------------------
# adding abs Delta alpha between leading photon and jets
alphaVar = 'absCosDeltaAlphaSubleadGamma'+str(i)
col1_phi = 'genSubleadPhi'
col1_eta = 'genSubleadEta'
col2_phi = 'genJet2p5Phi'+str(i)
col2_eta = 'genJet2p5Eta'+str(i)
df[alphaVar] = df[[col1_phi,col1_eta,col2_phi,col2_eta]].apply(lambda x : absCosDeltaAlphaPhotonJet(*x),axis=1)
#----------------------------------------------------------------------
return df