-
Notifications
You must be signed in to change notification settings - Fork 20
/
hgcalHelpers.py
140 lines (113 loc) · 5.28 KB
/
hgcalHelpers.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
133
134
135
136
137
138
139
140
import numpy as np
from HGCalImagingAlgo import recHitAboveThreshold
import math
from scipy.spatial import cKDTree
import pandas
def getRecHitDetIds(rechits):
recHitsList = []
for rHit in rechits:
recHitsList.append(rHit.detid())
# print "RecHits -"*10
# print recHitsList
recHits = np.array(recHitsList)
return recHits
def getHitList(simClus, recHitDetIds):
sClusHitsList = []
for DetId in simClus.hits():
sClusHitsList.append(DetId)
sClusHits = np.array(sClusHitsList)
# thanks to http://stackoverflow.com/questions/11483863/python-intersection-indices-numpy-array
recHitIndices = np.nonzero(np.in1d(recHitDetIds, sClusHits))
return recHitIndices
# get list of rechist associated to sim-cluster hits
def getRecHitsSimAssoc(rechits_raw, simcluster, dependSensor=True, ecut=3, verbosityLevel=0):
# get sim-cluster associations
nSimClus = 0
simClusHitAssoc = []
recHitDetIds = getRecHitDetIds(rechits_raw)
for simClusIndex, simClus in enumerate(simcluster):
simClusHitAssoc.append(getHitList(simClus, recHitDetIds))
nSimClus += 1
# get list of rechist associated to simhits
rHitsSimAssoc = [[] for k in range(0, nSimClus)]
for simClusIndex, simCl in enumerate(simcluster):
if (verbosityLevel >= 1):
print "Sim-cluster index: ", simClusIndex, ", pT: ", simCl.pt(), ", E: ", simCl.energy(), ", phi: ", simCl.phi(), ", eta: ", simCl.eta()
# loop over sim clusters and then rechits
rHitsSimAssocTemp = []
for hitIndexArray in simClusHitAssoc[simClusIndex]:
for hitIndex in hitIndexArray:
thisHit = rechits_raw[hitIndex]
if(not recHitAboveThreshold(thisHit, ecut, dependSensor)[1]):
continue
# independent of sim cluster, after cleaning
rHitsSimAssocTemp.append(thisHit)
rHitsSimAssoc[simClusIndex] = rHitsSimAssocTemp
return rHitsSimAssoc
# get list of rechist associated to sim-cluster which is within dR<0.2 of the genParticle
def deltaRSquared(obj1, obj2):
# return (obj1.eta() - obj2.eta())**2 + (obj1.phi() - obj2.phi())**2
return (obj1.eta - obj2.eta())**2 + (obj1.phi - obj2.phi())**2
def deltaR(obj1, obj2):
return math.sqrt(deltaRSquared(obj1, obj2))
def getRecHitsSimAssocPUP(rechits_raw, simcluster, genparticles, pidSelected, GEN_engpt):
# get sim-cluster associations
# nSimClus = 0
# simClusHitAssoc = []
recHitDetIds = getRecHitDetIds(rechits_raw)
rHitsSimAssoc = [[] for k in range(0, len(simcluster))]
for simClusIndex, simClus in enumerate(simcluster):
for genPartIndex, genPart in enumerate(genparticles):
if (genPart.pid() == pidSelected and math.sqrt(math.fabs(genPart.eta() - simClus.eta())**2 + math.fabs(genPart.phi() - simClus.phi())**2) < 0.1 and simClus.pt() > 0.7 * GEN_engpt):
rHitsSimAssocTemp = []
for hitIndexArray in getHitList(simClus, recHitDetIds):
for hitIndex in hitIndexArray:
thisHit = rechits_raw[hitIndex]
# if(not recHitAboveThreshold(thisHit, ecut, dependSensor)[1]): continue
rHitsSimAssocTemp.append(thisHit)
rHitsSimAssoc[simClusIndex] = rHitsSimAssocTemp
return rHitsSimAssoc
def getIndicesWithinRadius(ref_xy, obj_xy, radius=0.1):
'''Match all objects within given DeltaR'''
kdtree = cKDTree(obj_xy)
matched_indices = {}
for index, row in ref_xy.iterrows():
matched = kdtree.query_ball_point([row.x, row.y], radius)
matched_indices[index] = matched
return matched_indices
def getHighestEnergyObjectIndex(ref_etaphi, obj_etaphi, obj_energy, deltaR=0.1):
'''Match object with highest energy within given DeltaR'''
kdtree = cKDTree(obj_etaphi)
matched_indices = {}
for index, row in ref_etaphi.iterrows():
matched = kdtree.query_ball_point([row.eta, row.phi], deltaR)
# Handle the -pi pi transition
matched_sym = kdtree.query_ball_point([row.eta, row.phi-np.sign(row.phi)*2.*math.pi], deltaR)
matched = np.unique(np.concatenate((matched, matched_sym))).astype(int)
if len(matched) > 0:
best_match = np.argmax(obj_energy[matched])
matched_indices[index] = best_match
return matched_indices
def getClosestObjectIndices(ref_etaphi, obj_etaphi, deltaR=0.1):
'''Match object with smallest DeltaR'''
kdtree = cKDTree(obj_etaphi)
matched_indices = {}
for index, row in ref_etaphi.iterrows():
closest = kdtree.query([row.eta, row.phi], 1)
# Handle the -pi pi transition, do the matching again
closest_sym = kdtree.query([row.eta, row.phi-np.sign(row.phi)*2.*math.pi], 1)
# keep only unique indices
matched = [closest, closest_sym]
# Choose the match with minimum deltaR
best_match = min(matched, key=lambda k: k[0])
# print best_match
if (best_match[0] < deltaR):
matched_indices[index] = best_match[1]
return matched_indices
def convertToXY(eta, phi, z):
t = math.exp(-1. * eta)
x = z * 2. * t * math.cos(phi) / (1. - t*t)
y = z * 2. * t * math.sin(phi) / (1. - t*t)
d = {'x': [x], 'y': [y]}
df = pandas.DataFrame(data=d)
return df