-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.go
267 lines (232 loc) · 5.85 KB
/
score.go
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
// Copyright 2018 The go-trackml Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package trackml
import (
"log"
"sort"
)
// Score computes the TrackML event score for a single event.
func Score(evt Event, trkIDs []int) float64 {
sum := 0.0
trks := analyzeTracks(evt.Mcs, evt.Hits, trkIDs)
for _, trk := range trks {
var (
majHits = float64(trk.MajHits)
nhits = float64(trk.Hits)
majPHits = float64(trk.MajPHits)
purityRec = majHits / nhits
purityMaj = majHits / majPHits
goodTrk = 0.5 < purityRec && 0.5 < purityMaj
)
if goodTrk {
sum += trk.MajWeight
}
}
return sum
}
func extractMcHitIDs(mcs []Truth) []int {
ids := make([]int, len(mcs))
for i, mc := range mcs {
ids[i] = mc.HitID
}
return ids
}
func extractHitIDs(hits []Hit) []int {
ids := make([]int, len(hits))
for i, hit := range hits {
ids[i] = hit.HitID
}
return ids
}
type ntuple struct {
mcs []Truth
hits []Hit
trkIDs []int
}
func (nt ntuple) print(beg, end int) {
mids := make([]int, len(nt.mcs[beg:end]))
pids := make([]int, len(nt.mcs[beg:end]))
hids := make([]int, len(nt.hits[beg:end]))
tids := make([]int, len(nt.trkIDs[beg:end]))
wgts := make([]float64, len(nt.mcs[beg:end]))
for i := range mids {
j := i + beg
mids[i] = nt.mcs[j].HitID
pids[i] = nt.mcs[j].PID
hids[i] = nt.hits[j].HitID
tids[i] = nt.trkIDs[j]
wgts[i] = nt.mcs[j].Weight
}
log.Printf("nt.slice[%d:%d]", beg, end)
for i := range mids {
log.Printf("%v\t%v\t%v\t%v\t%v", mids[i], hids[i], pids[i], wgts[i], tids[i])
}
}
func (nt ntuple) display(pid int) {
var (
mids []int
pids []int
hids []int
tids []int
wgts []float64
)
for i, mc := range nt.mcs {
if mc.PID != pid {
continue
}
mids = append(mids, mc.HitID)
pids = append(pids, mc.PID)
hids = append(hids, nt.hits[i].HitID)
tids = append(tids, nt.trkIDs[i])
wgts = append(wgts, mc.Weight)
}
log.Printf("nt.display(%d)", pid)
for i := range mids {
log.Printf("%v\t%v\t%v\t%v\t%v", mids[i], hids[i], pids[i], wgts[i], tids[i])
}
}
func analyzeTracks(mcs []Truth, hits []Hit, trkIDs []int) []recTrack {
// compute the true number of hits for each particle id
pids := make(map[int]int, len(mcs))
totalWeight := 0.0
for _, mc := range mcs {
pids[mc.PID]++
totalWeight += mc.Weight
}
nt := ntuple{mcs, hits, trkIDs}
// log.Printf("===========================")
// nt.print(0, 10)
// log.Printf("---------------------------")
// nt.print(len(mcs)-10, len(mcs))
// log.Printf("===========================")
invTotWeight := 1 / totalWeight
sort.Sort(byTrackAndPID{nt})
defer sort.Sort(byHitID{nt})
// log.Printf("")
// log.Printf("===========================")
// nt.print(0, 10)
// log.Printf("---------------------------")
// nt.print(len(mcs)-30, len(mcs))
// log.Printf("===========================")
// log.Printf("+++++++++++++++++++++++++++")
// nt.display(117108158840700928)
// log.Printf("+++++++++++++++++++++++++++")
type tuple struct {
pid int
nhits int
weight float64
}
var (
trks []recTrack
recTrkID = -1
recHits = 0
cur = tuple{-1, 0, 0}
maj = tuple{-1, 0, 0}
)
for i := range trkIDs {
tid := trkIDs[i]
pid := mcs[i].PID
// reached the next track: need to finalize the current one
if recTrkID != -1 && recTrkID != tid {
if maj.nhits < cur.nhits {
maj = cur
}
trks = append(trks, recTrack{
ID: recTrkID,
Hits: recHits,
MajPID: maj.pid,
MajPHits: pids[maj.pid],
MajHits: maj.nhits,
MajWeight: maj.weight * invTotWeight,
})
}
// set running values for next track (or first)
if recTrkID != tid {
recTrkID = tid
recHits = 1
cur.pid = pid
cur.nhits = 1
cur.weight = mcs[i].Weight
maj.pid = -1
maj.nhits = 0
maj.weight = 0
continue
}
// hit is part of the current reconstructed track
recHits++
// reached new particle within the same reconstructed track
if cur.pid != pid {
// check whether the last particle has more hits than the majority one
if maj.nhits < cur.nhits {
maj = cur
}
// reset running values for the current particle
cur.pid = pid
cur.nhits = 1
cur.weight = mcs[i].Weight
} else {
// hit belongs to the same particle within the same reconstructed track
cur.nhits++
cur.weight += mcs[i].Weight
}
}
// last track not handled inside the above loop
if maj.nhits < cur.nhits {
maj = cur
}
trks = append(trks, recTrack{
ID: recTrkID,
Hits: recHits,
MajPID: maj.pid,
MajPHits: pids[maj.pid],
MajHits: maj.nhits,
MajWeight: maj.weight * invTotWeight,
})
return trks
}
type recTrack struct {
ID int
Hits int
MajPID int
MajPHits int
MajHits int
MajWeight float64
}
type byTrackAndPID struct {
nt ntuple
}
func (evt byTrackAndPID) Len() int { return len(evt.nt.hits) }
func (evt byTrackAndPID) Swap(i, j int) {
evt.nt.hits[i], evt.nt.hits[j] = evt.nt.hits[j], evt.nt.hits[i]
evt.nt.mcs[i], evt.nt.mcs[j] = evt.nt.mcs[j], evt.nt.mcs[i]
evt.nt.trkIDs[i], evt.nt.trkIDs[j] = evt.nt.trkIDs[j], evt.nt.trkIDs[i]
}
func (evt byTrackAndPID) Less(i, j int) bool {
itrk := evt.nt.trkIDs[i]
jtrk := evt.nt.trkIDs[j]
ipid := evt.nt.mcs[i].PID
jpid := evt.nt.mcs[j].PID
switch {
case itrk < jtrk:
return true
case itrk == jtrk:
switch {
case ipid < jpid:
return true
case ipid == jpid:
return evt.nt.mcs[i].HitID < evt.nt.mcs[j].HitID
}
}
return false
}
type byHitID struct {
nt ntuple
}
func (evt byHitID) Len() int { return len(evt.nt.hits) }
func (evt byHitID) Swap(i, j int) {
evt.nt.hits[i], evt.nt.hits[j] = evt.nt.hits[j], evt.nt.hits[i]
evt.nt.mcs[i], evt.nt.mcs[j] = evt.nt.mcs[j], evt.nt.mcs[i]
evt.nt.trkIDs[i], evt.nt.trkIDs[j] = evt.nt.trkIDs[j], evt.nt.trkIDs[i]
}
func (evt byHitID) Less(i, j int) bool { return evt.nt.hits[i].HitID < evt.nt.hits[j].HitID }