-
Notifications
You must be signed in to change notification settings - Fork 6
/
makeBrilNTs.py
144 lines (102 loc) · 4.22 KB
/
makeBrilNTs.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
141
142
143
144
import re
import operator
from operator import itemgetter
from itertools import groupby
# list of json files with goodLS
listFiles = ["goodLS_pltzerov1.json","goodLS_bcm1fv1.json","goodLS_hfocv1.json"]
allData = [] #det_type, runNo, lsFrom, lsTo
runRange = {} #runNo ==> LS numbers
# open the files
for file in listFiles:
with open(file,'r') as f:
lines = f.readlines()
for line in lines:
temp = re.findall('\w+',line)
if temp:
allData.append(temp)
if temp[1] in runRange.keys():
runRange[temp[1]].extend(range(int(temp[2]),int(temp[3])+1))
else:
runRange[temp[1]] = range(int(temp[2]),int(temp[3])+1)
# make a unique runNO ==> LS list
uRunLS = {}
for key,val in runRange.iteritems():
uRunLS[key] = list(set(val))
# create normtag, give higher preference as:
# smaller index==> higher preference
print "\n\n"
print "=======Enter detector preference order================================"
print "=======Options::::: pbh, phb, bhp,bph,hpb,hbp ========================"
print "=======Example:pbh: pltzerov1, bcm1fv1,hfocv1========================="
order = ""
det_order = ["pbh", "phb", "bhp", "bph", "hpb", "hbp"]
while order not in det_order:
order = raw_input("Enter det. order: ")
if order == "pbh":
preference = ["pltzerov1","bcm1fv1","hfocv1"]
elif order == "phb":
preference = ["pltzerov1","hfocv1","bcm1fv1"]
elif order == "bhp":
preference = ["bcm1fv1","hfocv1","pltzerov1"]
elif order == "bph":
preference = ["bcm1fv1","pltzerov1","hfocv1"]
elif order == "hpb":
preference = ["hfocv1","pltzerov1","bcm1fv1"]
else:
preference = ["hfocv1","bcm1fv1","pltzerov1"]
print "Det order picked: " ,preference
perDetType = {}
# Now that we know unique list of LS for each run,
# we go through all the runs (according to preference)
# order, cross the LS used for normtag
for pref in preference:
perDetType[pref] = []
# perDetType == det_type=>[runNo, LsFrom, LsTo]
for items in allData:
if items[0] in perDetType.keys():
perDetType[items[0]].append(items[1:])
else:
perDetType[items[0]] = items[1:]
normTag = []
# Now we have allRunLS in uRunLS and per-detector
# runLS in perDetType, we begin generating NormTag files.
# Main idea is to go through det_type in given order
# of preference, and then crossing off LS from uRunLS
# for each run
for ind in range(0,3):
det_type = preference[ind]
# print det_type, ind, '\n\n'
for runLS in perDetType[det_type]:
#print runLS
#runLS == [runNo, LsFrom, LsTo]
runNo, LsFrom, LsTo = runLS[0],runLS[1],runLS[2]
# LS still to be tagged for this run
remLS = set(uRunLS[runNo])
# LS in this det_type+run
stripLS = set(range(int(LsFrom),int(LsTo)+1))
# intersection of available LS, this LS
intersectLS = list(remLS.intersection(list(stripLS)))
# LS to tag, still
diffLS = list(remLS - stripLS)
# update uRunLS with remaining LS to be tagged
uRunLS[runNo] = diffLS
# if intersection is non-empty==> tag this range
if intersectLS:
# if intersectLS is not continuous list of numbers, group them
for a,b in groupby(enumerate(intersectLS), lambda(i,x):i-x):
tmp = map(itemgetter(1),b)
normTag.append([det_type, int(runNo),tmp[0], tmp[-1]])
# normTag.append([det_type, int(runNo),min(intersectLS), max(intersectLS)])
#print "^LS: ", runNo, intersectLS,'\n',min(intersectLS), max(intersectLS),'\n'
else:
pass
# Now sort normTag, which is a list of lists, by runNo, LsFrom
normTag.sort(key = operator.itemgetter(1,2))
#Finally, create that json file
normtag_file = 'normtagv1' + '.json'
f = open(normtag_file,'w')
for x in range(0,len(normTag) -1 ):
f.write("[\"" + normTag[x][0] + "\",{\"" + str(normTag[x][1]) + "\":[[" + str(normTag[x][2]) + "," + str(normTag[x][3]) + "]]}]"+",\n")
f.write("[\"" + normTag[len(normTag) - 1][0] + "\",{\"" + str(normTag[len(normTag) - 1][1]) + "\":[[" + str(normTag[len(normTag) - 1][2]) + "," + str(normTag[len(normTag) - 1][3]) + "]]}]" +"\n")
f.close()
print "Normtag file generated!"