ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/MenuCompare.py
Revision: 1.4
Committed: Tue Oct 16 18:24:14 2012 UTC (12 years, 6 months ago) by muell149
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-02-06, V00-02-05, V00-02-04, HEAD
Changes since 1.3: +67 -25 lines
Log Message:
updated so output file shows no duplicates, mismatched paths are included in output, all rates are scaled to rateMon rates

File Contents

# Content
1 #!/usr/bin/env python
2 import sys
3 import os
4 import string
5 import subprocess
6 import getopt
7 import pickle
8 import re
9 import csv
10
11 #########################################################################################
12 #### This file uses: DatabaseRatePredictor.py
13 #### DatabaseRateMonitor.py
14 #### to compare rates across hlt menus.
15 ####
16 #### Instructions: https://twiki.cern.ch/twiki/bin/viewauth/CMS/MenuComparison_Tutorial
17 ####
18 #### Charlie Mueller 8/8/2012
19 #########################################################################################
20 def usage():
21 print "This tool compares old menu rates to new menu rates in addition to googledoc and TMD predicions"
22 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"
23 print "the only optional argument is '--json='"
24
25 def main():
26 try:
27 try:
28 opt,args = getopt.getopt(sys.argv[1:],"",["TriggerList=","json=","oldMenu=","LS1=","nLS1=","newMenu=","LS2=","nLS2=","TMDlumi=","gDoclumi=","lumi=","googleDoc=","TMDpred="])
29 except getopt.GetoptError, err:
30 print "Error"
31 usage()
32 sys.exit(2)
33
34 #init inputs
35 triggerlist = ""
36 json = ""
37 LS1 = ""
38 LS2 = ""
39 nLS1 = ""
40 nLS2 = ""
41 gDoc = ""
42 TMDpred = ""
43 for o,a in opt:
44 if o == "--TriggerList":
45 triggerlist = a
46 elif o =="--json":
47 json = a
48 elif o =="--oldMenu":
49 oldMenu = a
50 elif o =="--LS1":
51 LS1 = a
52 elif o =="--nLS1":
53 nLS1 = a
54 elif o =="--newMenu":
55 newMenu = a
56 elif o =="--LS2":
57 LS2 = a
58 elif o =="--nLS2":
59 nLS2 = a
60 elif o =="--googleDoc":
61 gDoc = a
62 elif o =="--TMDpred":
63 TMDpred = a
64 elif o =="--TMDlumi":
65 TMDlumi = float(a)
66 elif o =="--gDoclumi":
67 gDoclumi = float(a)
68 elif o =="--lumi":
69 lumi = float(a)
70
71
72 TMDscaler=lumi/TMDlumi
73 gDocscaler=lumi/gDoclumi
74
75 if oldMenu.find('-') != -1:#oldMenu is a run range
76 rrangeOld = oldMenu.split('-')
77 if newMenu.find('-') != -1:#newMenu is a run range
78 rrangeNew = newMenu.split('-')
79
80 run1 = rrangeOld[0]
81 run2 = rrangeOld[1]
82 run3 = rrangeNew[0]
83 run4 = rrangeNew[1]
84
85 save_string = "MenuComparison_"+run2+"-"+run4+".csv"
86 trigger_string = "--TriggerList="+triggerlist
87
88 if json == "":
89 json_string = json
90 else:
91 json_string = "--json="+json
92
93 print " "
94 cmd1 = "env DISPLAY= python DatabaseRatePredictor.py --makeFits "+trigger_string+" --NoVersion "+json_string+" --maxdt=0.10 "+oldMenu
95 cmd2 = "sed -i '/TriggerToMonitorList=/ c\TriggerToMonitorList="+triggerlist+"' defaults.cfg"
96 cmd3 = "sed -i '/FitFileName=/ c\FitFileName=Fits/2012/Fit_HLT_NoV_10LS_Run"+run1+"to"+run2+".pkl' defaults.cfg"
97 cmd4 = "sed -i '/prettyCSVwriter(/ c\ prettyCSVwriter(\"rateMon_oldmenu.csv\",[80,10,10,10,10,20,20],Header,core_data,Warn)' DatabaseRateMonitor.py"
98 cmd5 = "python DatabaseRateMonitor.py --write --CompareRun="+run2+" --FirstLS="+LS1+" --NumberLS="+nLS1
99 cmd6 = "env DISPLAY= python DatabaseRatePredictor.py --makeFits "+trigger_string+" --NoVersion --maxdt=0.10 "+newMenu
100 cmd7 = "sed -i '/FitFileName=/ c\FitFileName=Fits/2012/Fit_HLT_NoV_10LS_Run"+run3+"to"+run4+".pkl' defaults.cfg"
101 cmd8 = "sed -i '/prettyCSVwriter(/ c\ prettyCSVwriter(\"rateMon_newmenu.csv\",[80,10,10,10,10,20,20],Header,core_data,Warn)' DatabaseRateMonitor.py"
102 cmd9 = "python DatabaseRateMonitor.py --write --CompareRun="+run4+" --FirstLS="+LS2+" --NumberLS="+nLS2
103
104 cmds = [cmd1,cmd2,cmd3,cmd4,cmd5,cmd6,cmd7,cmd8,cmd9]
105 for cmd in cmds:
106 try:
107 subprocess.call(cmd, shell=True)
108 except:
109 print "Command Error:",cmd
110 sys.exit(2)
111
112 compareFiles("rateMon_newmenu.csv","rateMon_oldmenu.csv",TMDpred,gDoc,TMDscaler,gDocscaler,save_string)
113
114 except KeyboardInterrupt:
115 print "Exiting..."
116
117
118 def StripVersion(name):
119 if re.match('.*_v[0-9]+',name):
120 name = name[:name.rfind('_')]
121 name = string.strip(name)
122 return name
123
124 def StripPm(name1):
125 if re.match('.*[+-]',name1):
126 name1 = name1[:name1.rfind('+-')]
127 return name1
128
129 def StripPmEr(name2):
130 if re.match('.*[+-]',name2):
131 name2 = name2[name2.rfind('+-')+2:]
132 return name2
133
134
135 def compareFiles(new,old,tmd,gdoct,TMDscaler,gDocscaler,save_file):
136 f1 = open(new,"rU")#new menu, the order is important!
137 f2 = open(old,"rU")#old menu
138 f3 = open(tmd,"rU")#TMD
139 f4 = open(gdoct,"rU")#gDoc
140 saveF = open(save_file,"wb")
141 r1 = csv.reader(f1)
142 r2 = csv.reader(f2)
143 r3 = csv.reader(f3)
144 r4 = csv.reader(f4)
145 w1 = csv.writer(saveF)
146
147 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"])
148
149 list2 = [iter2 for iter2 in r2]
150 list3 = [iter3 for iter3 in r3]
151 list4 = [iter4 for iter4 in r4]
152 #row1 = newmenu
153 #row2 = oldmenu
154 #row3 = TMD
155 #row4 = gdoc
156
157 list2_diff=list2
158 list1_diff=[]
159 x_dup=[]#list to make sure no duplicate paths are written to output csv
160
161 for row1 in r1:
162 find_match=False
163 for row2 in list2:
164 if StripVersion(str(row1[0])) == StripVersion(str(row2[0])):#new and old menus match
165 list2_diff.remove(row2)
166 find_match=True
167 for row3 in list3:
168 try:
169 val=StripPm(row3[4])*TMDscaler
170 er=float(StripPmEr(row3[4]))*TMDscaler
171 tmd_scaled_rt=str(val)+"+-"+str(er)
172 except:
173 tmd_scaled_rt=str(row3[4])
174 if StripVersion(row2[0]) not in x_dup:
175 if StripVersion(str(row3[0])) == StripVersion(str(row2[0])):
176 x_dup.append(StripVersion(str(row2[0])))
177 row_write=False
178 for row4 in list4:
179 if (StripVersion(str(row4[0])) == StripVersion(str(row3[0]))) and not row_write:#if gdoc and tmd match
180 gDoc_scaled_rt = str(gDocscaler)+" X "+str(row4[3])
181 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])])
182 row_write = True
183 break
184
185 if row_write == False: #if no gDoc match
186 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"])
187 if not find_match: list1_diff.append(row1)
188
189
190 #Write Mismatched paths into csv output
191 if len(list1_diff)>0:
192 for row1_diff in list1_diff:
193 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"])
194
195
196 if len(list2_diff)>0:
197 for row2_diff in list2_diff:
198 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"])
199
200
201 f1.close()
202 f2.close()
203 f3.close()
204 f4.close()
205 print "output comparison file is ",save_file
206 saveF.close()
207
208
209 if __name__=='__main__':
210 main()