ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/MenuCompare.py
Revision: 1.1
Committed: Thu Aug 9 08:56:19 2012 UTC (12 years, 8 months ago) by muell149
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-01-07, V00-01-06
Log Message:
hlt menu comparison tool added

File Contents

# User Rev Content
1 muell149 1.1 #!/usr/bin/env python
2     import sys
3     import os
4     import subprocess
5     import getopt
6     import pickle
7     import re
8     import csv
9    
10     #########################################################################################
11     #### This file needs: DatabaseRatePredictor_dev.py
12     #### DatabaseRateMonitor.py
13     ####
14     #### Charlie Mueller 8/8/2012
15     #########################################################################################
16     def usage():
17     print "This tool compares old menu rates to new menu rates in addition to googledoc and TMD predicions"
18     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 "
19     print "the only optional argument is '--json='"
20    
21     def main():
22     try:
23     try:
24     opt,args = getopt.getopt(sys.argv[1:],"",["TriggerList=","json=","oldMenu=","LS1=","nLS1=","newMenu=","LS2=","nLS2=","scaleFac=","googleDoc=","TMDpred="])
25     except getopt.GetoptError, err:
26     print "Error"
27     usage()
28     sys.exit(2)
29    
30     #init inputs
31     triggerlist = ""
32     json = ""
33     LS1 = ""
34     LS2 = ""
35     nLS1 = ""
36     nLS2 = ""
37     gDoc = ""
38     TMDpred = ""
39     for o,a in opt:
40     if o == "--TriggerList":
41     triggerlist = a
42     elif o =="--json":
43     json = a
44     elif o =="--oldMenu":
45     oldMenu = a
46     elif o =="--LS1":
47     LS1 = a
48     elif o =="--nLS1":
49     nLS1 = a
50     elif o =="--newMenu":
51     newMenu = a
52     elif o =="--LS2":
53     LS2 = a
54     elif o =="--nLS2":
55     nLS2 = a
56     elif o =="--googleDoc":
57     gDoc = a
58     elif o =="--TMDpred":
59     TMDpred = a
60     elif o =="--scaleFac":
61     scaleFac = float(a)
62    
63     if oldMenu.find('-') != -1:#oldMenu is a run range
64     rrangeOld = oldMenu.split('-')
65     if newMenu.find('-') != -1:#newMenu is a run range
66     rrangeNew = newMenu.split('-')
67    
68     run1 = rrangeOld[0]
69     run2 = rrangeOld[1]
70     run3 = rrangeNew[0]
71     run4 = rrangeNew[1]
72    
73     save_string = "MenuComparison_"+run2+"-"+run4+".csv"
74     trigger_string = "--TriggerList="+triggerlist
75    
76     if json == "":
77     json_string = json
78     else:
79     json_string = "--json="+json
80    
81     print " "
82     cmd1 = "env DISPLAY= python DatabaseRatePredictor.py --makeFits "+trigger_string+" --NoVersion "+json_string+" --maxdt=0.10 "+oldMenu
83     cmd2 = "sed -i '/TriggerToMonitorList=/ c\TriggerToMonitorList="+triggerlist+"' defaults.cfg"
84     cmd3 = "sed -i '/FitFileName=/ c\FitFileName=Fits/2012/Fit_HLT_NoV_10LS_Run"+run1+"to"+run2+".pkl' defaults.cfg"
85     cmd4 = "sed -i '/prettyCSVwriter(/ c\ prettyCSVwriter(\"rateMon_oldmenu.csv\",[80,10,10,10,10,20,20],Header,SortedData,Warn)' DatabaseRateMonitor.py"
86     cmd5 = "python DatabaseRateMonitor.py --CompareRun="+run2+" --FirstLS="+LS1+" --NumberLS="+nLS1
87     cmd6 = "env DISPLAY= python DatabaseRatePredictor.py --makeFits "+trigger_string+" --NoVersion --maxdt=0.10 "+newMenu
88     cmd7 = "sed -i '/FitFileName=/ c\FitFileName=Fits/2012/Fit_HLT_NoV_10LS_Run"+run3+"to"+run4+".pkl' defaults.cfg"
89     cmd8 = "sed -i '/prettyCSVwriter(/ c\ prettyCSVwriter(\"rateMon_newmenu.csv\",[80,10,10,10,10,20,20],Header,SortedData,Warn)' DatabaseRateMonitor.py"
90     cmd9 = "python DatabaseRateMonitor.py --CompareRun="+run4+" --FirstLS="+LS2+" --NumberLS="+nLS2
91     cmds = [cmd1,cmd2,cmd3,cmd4,cmd5,cmd6,cmd7,cmd8,cmd9]
92     for cmd in cmds:
93     try:
94     subprocess.call(cmd, shell=True)
95     except:
96     print "Command Error:",cmd
97     sys.exit(2)
98    
99     compareFiles("rateMon_newmenu.csv","rateMon_oldmenu.csv",TMDpred,gDoc,scaleFac,save_string)
100    
101     except KeyboardInterrupt:
102     print "Exiting..."
103    
104    
105     def StripVersion(name):
106     if re.match('.*_v[0-9]+',name):
107     name = name[:name.rfind('_')]
108     return name
109    
110     def StripPm(name1):
111     if re.match('.*[+-]',name1):
112     name1 = name1[:name1.rfind('+-')]
113     return name1
114    
115     def compareFiles(new,old,tmd,gdoct,scaler,save_file):
116     f1 = open(new,"rU")#new menu, the order is important!
117     f2 = open(old,"rU")#old menu
118     f3 = open(tmd,"rU")#TMD
119     f4 = open(gdoct,"rU")#gDoc
120     saveF = open(save_file,"wb")
121     r1 = csv.reader(f1)
122     r2 = csv.reader(f2)
123     r3 = csv.reader(f3)
124     r4 = csv.reader(f4)
125     w1 = csv.writer(saveF)
126    
127     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"])
128    
129     list2 = [iter2 for iter2 in r2]
130     list3 = [iter3 for iter3 in r3]
131     list4 = [iter4 for iter4 in r4]
132    
133     #row1 = newmenu
134     #row2 = oldmenu
135     #row3 = TMD
136     #row4 = googledoc
137    
138     for row1 in r1:
139     for row2 in list2:
140     if StripVersion(str(row1[0])) == StripVersion(str(row2[0])):
141     for row3 in list3:
142     if StripVersion(str(row3[0])) == StripVersion(str(row2[0])):
143     row_write = False
144     for row4 in list4:
145     if StripVersion(str(row4[0])) == StripVersion(str(row3[0])):
146     w1.writerow([str(row1[0]),str(float(row2[1])*scaler),str(float(row2[2])*scaler),str(row2[5]),str(float(row1[1])*scaler),str(float(row1[2])*scaler),str(row1[5]),str(row4[3]),str(row3[4]),str(row4[32])])
147     row_write = True
148     break
149    
150     if row_write == False: #if no gDoc match
151     w1.writerow([str(row1[0]),str(float(row2[1])*scaler),str(float(row2[2])*scaler),str(row2[5]),str(float(row1[1])*scaler),str(float(row1[2])*scaler),str(row1[5]),"none",str(row3[4]),"none"])
152    
153    
154     f1.close()
155     f2.close()
156     f3.close()
157     f4.close()
158     print "output comparison file is ",save_file
159     saveF.close()
160    
161    
162     if __name__=='__main__':
163     main()