forked from cms-tsg-fog/RateMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuCompare.py
executable file
·210 lines (178 loc) · 8.27 KB
/
MenuCompare.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
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
#!/usr/bin/env python
import sys
import os
import string
import subprocess
import getopt
import pickle
import re
import csv
#########################################################################################
#### This file uses: DatabaseRatePredictor.py
#### DatabaseRateMonitor.py
#### to compare rates across hlt menus.
####
#### Instructions: https://twiki.cern.ch/twiki/bin/viewauth/CMS/MenuComparison_Tutorial
####
#### Charlie Mueller 8/8/2012
#########################################################################################
def usage():
print "This tool compares old menu rates to new menu rates in addition to googledoc and TMD predicions"
print "EXAMPLE: python MenuCompare.py --TriggerList=monitorlist_all_v4.list --json=Cert_190456-199429_8TeV_PromptReco_Collisions12_JSON.txt --oldMenu=199318-199429 --LS1=2 --nLS1=23 --newMenu=199751-199754 --LS2=54 --nLS2=20 --googleDoc=googledoc_7e33_v3.csv --TMDpred=TMDpred_7e33_v4.csv --TMDlumi=4.2e33 --gDoclumi=7e33 --lumi=4.6e33"
print "the only optional argument is '--json='"
def main():
try:
try:
opt,args = getopt.getopt(sys.argv[1:],"",["TriggerList=","json=","oldMenu=","LS1=","nLS1=","newMenu=","LS2=","nLS2=","TMDlumi=","gDoclumi=","lumi=","googleDoc=","TMDpred="])
except getopt.GetoptError, err:
print "Error"
usage()
sys.exit(2)
#init inputs
triggerlist = ""
json = ""
LS1 = ""
LS2 = ""
nLS1 = ""
nLS2 = ""
gDoc = ""
TMDpred = ""
for o,a in opt:
if o == "--TriggerList":
triggerlist = a
elif o =="--json":
json = a
elif o =="--oldMenu":
oldMenu = a
elif o =="--LS1":
LS1 = a
elif o =="--nLS1":
nLS1 = a
elif o =="--newMenu":
newMenu = a
elif o =="--LS2":
LS2 = a
elif o =="--nLS2":
nLS2 = a
elif o =="--googleDoc":
gDoc = a
elif o =="--TMDpred":
TMDpred = a
elif o =="--TMDlumi":
TMDlumi = float(a)
elif o =="--gDoclumi":
gDoclumi = float(a)
elif o =="--lumi":
lumi = float(a)
TMDscaler=lumi/TMDlumi
gDocscaler=lumi/gDoclumi
if oldMenu.find('-') != -1:#oldMenu is a run range
rrangeOld = oldMenu.split('-')
if newMenu.find('-') != -1:#newMenu is a run range
rrangeNew = newMenu.split('-')
run1 = rrangeOld[0]
run2 = rrangeOld[1]
run3 = rrangeNew[0]
run4 = rrangeNew[1]
save_string = "MenuComparison_"+run2+"-"+run4+".csv"
trigger_string = "--TriggerList="+triggerlist
if json == "":
json_string = json
else:
json_string = "--json="+json
print " "
cmd1 = "env DISPLAY= python DatabaseRatePredictor.py --makeFits "+trigger_string+" --NoVersion "+json_string+" --maxdt=0.10 "+oldMenu
cmd2 = "sed -i '/TriggerToMonitorList=/ c\TriggerToMonitorList="+triggerlist+"' defaults.cfg"
cmd3 = "sed -i '/FitFileName=/ c\FitFileName=Fits/2012/Fit_HLT_NoV_10LS_Run"+run1+"to"+run2+".pkl' defaults.cfg"
cmd4 = "sed -i '/prettyCSVwriter(/ c\ prettyCSVwriter(\"rateMon_oldmenu.csv\",[80,10,10,10,10,20,20],Header,core_data,Warn)' DatabaseRateMonitor.py"
cmd5 = "python DatabaseRateMonitor.py --write --CompareRun="+run2+" --FirstLS="+LS1+" --NumberLS="+nLS1
cmd6 = "env DISPLAY= python DatabaseRatePredictor.py --makeFits "+trigger_string+" --NoVersion --maxdt=0.10 "+newMenu
cmd7 = "sed -i '/FitFileName=/ c\FitFileName=Fits/2012/Fit_HLT_NoV_10LS_Run"+run3+"to"+run4+".pkl' defaults.cfg"
cmd8 = "sed -i '/prettyCSVwriter(/ c\ prettyCSVwriter(\"rateMon_newmenu.csv\",[80,10,10,10,10,20,20],Header,core_data,Warn)' DatabaseRateMonitor.py"
cmd9 = "python DatabaseRateMonitor.py --write --CompareRun="+run4+" --FirstLS="+LS2+" --NumberLS="+nLS2
cmds = [cmd1,cmd2,cmd3,cmd4,cmd5,cmd6,cmd7,cmd8,cmd9]
for cmd in cmds:
try:
subprocess.call(cmd, shell=True)
except:
print "Command Error:",cmd
sys.exit(2)
compareFiles("rateMon_newmenu.csv","rateMon_oldmenu.csv",TMDpred,gDoc,TMDscaler,gDocscaler,save_string)
except KeyboardInterrupt:
print "Exiting..."
def StripVersion(name):
if re.match('.*_v[0-9]+',name):
name = name[:name.rfind('_')]
name = string.strip(name)
return name
def StripPm(name1):
if re.match('.*[+-]',name1):
name1 = name1[:name1.rfind('+-')]
return name1
def StripPmEr(name2):
if re.match('.*[+-]',name2):
name2 = name2[name2.rfind('+-')+2:]
return name2
def compareFiles(new,old,tmd,gdoct,TMDscaler,gDocscaler,save_file):
f1 = open(new,"rU")#new menu, the order is important!
f2 = open(old,"rU")#old menu
f3 = open(tmd,"rU")#TMD
f4 = open(gdoct,"rU")#gDoc
saveF = open(save_file,"wb")
r1 = csv.reader(f1)
r2 = csv.reader(f2)
r3 = csv.reader(f3)
r4 = csv.reader(f4)
w1 = csv.writer(saveF)
w1.writerow(["Trigger","Old Menu Rate (Actual)"," Old Menu Rate (Expected)","Old Menu PS","New Menu Rate (Actual)","New Menu Rate (Expected)","New Menu PS","Google Doc Rate","TMD Pred Rate","contact"])
list2 = [iter2 for iter2 in r2]
list3 = [iter3 for iter3 in r3]
list4 = [iter4 for iter4 in r4]
#row1 = newmenu
#row2 = oldmenu
#row3 = TMD
#row4 = gdoc
list2_diff=list2
list1_diff=[]
x_dup=[]#list to make sure no duplicate paths are written to output csv
for row1 in r1:
find_match=False
for row2 in list2:
if StripVersion(str(row1[0])) == StripVersion(str(row2[0])):#new and old menus match
list2_diff.remove(row2)
find_match=True
for row3 in list3:
try:
val=StripPm(row3[4])*TMDscaler
er=float(StripPmEr(row3[4]))*TMDscaler
tmd_scaled_rt=str(val)+"+-"+str(er)
except:
tmd_scaled_rt=str(row3[4])
if StripVersion(row2[0]) not in x_dup:
if StripVersion(str(row3[0])) == StripVersion(str(row2[0])):
x_dup.append(StripVersion(str(row2[0])))
row_write=False
for row4 in list4:
if (StripVersion(str(row4[0])) == StripVersion(str(row3[0]))) and not row_write:#if gdoc and tmd match
gDoc_scaled_rt = str(gDocscaler)+" X "+str(row4[3])
w1.writerow([str(row1[0]),str(float(row2[1])),str(float(row2[2])),str(row2[5]),str(float(row1[1])),str(float(row1[2])),str(row1[5]),gDoc_scaled_rt,tmd_scaled_rt,str(row4[32])])
row_write = True
break
if row_write == False: #if no gDoc match
w1.writerow([str(row1[0]),str(float(row2[1])),str(float(row2[2])),str(row2[5]),str(float(row1[1])),str(float(row1[2])),str(row1[5]),"none",tmd_scaled_rt,"none"])
if not find_match: list1_diff.append(row1)
#Write Mismatched paths into csv output
if len(list1_diff)>0:
for row1_diff in list1_diff:
w1.writerow([str(row1_diff[0]),"none","none","none",str(float(row1_diff[1])),str(float(row1_diff[2])),str(row1_diff[5]),"NOT FOUND IN NEW MENU"])
if len(list2_diff)>0:
for row2_diff in list2_diff:
w1.writerow([str(row2_diff[0]),str(float(row2_diff[1])),str(float(row2_diff[2])),str(row2_diff[5]),"none","none","none","NOT FOUND IN OLD MENU"])
f1.close()
f2.close()
f3.close()
f4.close()
print "output comparison file is ",save_file
saveF.close()
if __name__=='__main__':
main()