ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/AndrewRateMonitor.py
Revision: 1.1
Committed: Wed Nov 30 18:02:13 2011 UTC (13 years, 5 months ago) by abrinke1
Content type: text/x-python
Branch: MAIN
Log Message:
Combination of Alex's and G&A's code

File Contents

# User Rev Content
1 abrinke1 1.1 #!/usr/bin/env python
2    
3     from AndrewGetRun import GetRun
4     from AndrewWBMParser import AndrewWBMParser
5     from ReadConfig import RateMonConfig
6     import sys
7     import os
8     import cPickle as pickle
9     import getopt
10     import time
11     from colors import *
12     from TablePrint import *
13     from math import *
14    
15     WBMPageTemplate = "http://cmswbm/cmsdb/servlet/RunSummary?RUN=%s&DB=cms_omds_lb"
16     WBMRunInfoPage = "https://cmswbm/cmsdb/runSummary/RunSummary_1.html"
17    
18     RefRunNameTemplate = "RefRuns/Run_%s.pk"
19    
20     def usage():
21     print sys.argv[0]+" [Options]"
22     print "This script gets the current HLT trigger rates and compares them to a reference run"
23     print "Options: "
24     print "--AllowedDiff=<diff> Report only if difference in trigger rate is greater than <diff>% (Default = "+str(DefaultAllowedRateDiff*100)+"% )"
25     print "--CompareRun=<Run #> Compare run <Run #> to the reference run (Default = Current Run)"
26     print "--FindL1Zeros Look for physics paths with 0 L1 rate"
27     print "--FirstLS=<ls> Specify the first lumisection to consider. This will set LSSlidingWindow to -1"
28     print "--LastLS=<ls> Specify the last lumisection to consider. Make sure LastLS > LSSlidingWindow"
29     print " or set LSSlidingWindow = -1"
30     print "--IgnoreLowRate=<rate> Ignore triggers with an actual and expected rate below <rate> (Default = "+str(DefaultIgnoreThreshold)+")"
31     print "--ListIgnoredPaths Prints the paths that are not compared by this script and their rate in the CompareRun"
32     print "--PrintLumi Prints Instantaneous, Delivered, and Live lumi by LS for the run"
33     print "--RefRun=<Run #> Specifies <Run #> as the reference run to use (Default in defaults.cfg)"
34     print "--ShowPSTriggers Show prescaled triggers in rate comparison"
35     print "--help Print this help"
36    
37     def main():
38     try:
39     opt, args = getopt.getopt(sys.argv[1:],"",["AllowedDiff=","CompareRun=","FindL1Zeros",\
40     "FirstLS=","LastLS=","IgnoreLowRate=","ListIgnoredPaths",\
41     "PrintLumi","RefRun=","ShowPSTriggers","help"])
42     except getopt.GetoptError, err:
43     print str(err)
44     usage()
45     sys.exit(2)
46    
47     Config = RateMonConfig(os.path.abspath(os.path.dirname(sys.argv[0])))
48     for o,a in opt:
49     if o=="--ConfigFile":
50     Config.CFGfile=a
51     Config.ReadCFG()
52    
53     AllowedRateDiff = Config.DefAllowRateDiff
54     CompareRunNum = ""
55     FindL1Zeros = True
56     FirstLS = 999999
57     LastLS = 111111
58     IgnoreThreshold = Config.DefAllowIgnoreThresh
59     ListIgnoredPaths = False
60     PrintLumi = False
61     RefRunNum = int(Config.ReferenceRun)
62     ShowPSTriggers = True
63    
64     if Config.LSWindow > 0:
65     FirstLS = -1*Config.LSWindow
66    
67     for o,a in opt: # get options passed on the command line
68     if o=="--AllowedDiff":
69     AllowedRateDiff = float(a)/100.0
70     elif o=="--CompareRun":
71     CompareRunNum=int(a)
72     elif o=="--FindL1Zeros":
73     FindL1Zeros = True
74     elif o=="--FirstLS":
75     FirstLS = int(a)
76     elif o=="--LastLS":
77     LastLS = int(a)
78     elif o=="--IgnoreLowRate":
79     IgnoreThreshold = float(a)
80     elif o=="--ListIgnoredPaths":
81     ListIgnoredPaths=True
82     elif o=="--PrintLumi":
83     PrintLumi = True
84     elif o=="--RefRun":
85     RefRunNum=int(a)
86     elif o=="--ShowPSTriggers":
87     ShowPSTriggers=True
88     elif o=="--help":
89     usage()
90     sys.exit(0)
91     else:
92     print "Invalid Option "+a
93     sys.exit(1)
94    
95     RefLumisExists = False
96     if RefRunNum > 0:
97     RefRates = {}
98     for Iterator in range(1,100):
99    
100     if RefLumisExists: ## Quits at the end of a run
101     if max(RefLumis[0]) <= (Iterator+1)*10:
102     break
103    
104     RefRunFile = RefRunNameTemplate % str( RefRunNum*100 + Iterator ) # place to save the reference run info
105    
106     if not os.path.exists(RefRunFile[:RefRunFile.rfind('/')]): # folder for ref run file must exist
107     print "Reference run folder does not exist, please create" # should probably create programmatically, but for now force user to create
108     print RefRunFile[:RefRunFile.rfind('/')]
109     sys.exit(0)
110    
111     if not os.path.exists(RefRunFile): # if the reference run is not saved, get it from wbm
112     print "Reference Run File for run "+str(RefRunNum)+" iterator "+str(Iterator)+" does not exist"
113     print "Creating ..."
114     try:
115     RefParser = GetRun(RefRunNum, RefRunFile, True, Iterator*10, (Iterator+1)*10)
116     except:
117     print "GetRun failed from LS "+str(Iterator*10)+" to "+str((Iterator+1)*10)
118     continue
119    
120     else: # otherwise load it from the file
121     RefParser = pickle.load( open( RefRunFile ) )
122    
123     if not RefLumisExists:
124     RefLumis = RefParser.LumiInfo
125     RefLumisExists = True
126    
127     try:
128     RefRates[Iterator] = RefParser.TriggerRates # get the trigger rates from the reference run
129     LastSuccessfulIterator = Iterator
130     except:
131     print "Failed to get rates from LS "+str(Iterator*10)+" to "+str((Iterator+1)*10)
132    
133    
134     # OK, Got the Reference Run
135     # Now get the most recent run
136    
137     SaveRun = False
138     if CompareRunNum=="": # if no run # specified on the CL, get the most recent run
139     RunListParser = AndrewWBMParser()
140    
141     RunListParser._Parse(WBMRunInfoPage) # this is the page that lists all the runs in the last 24 hours with at least 1 trigger
142     RunListPage = RunListParser.ParsePage1()
143     if RunListPage == '': # this will be '' if the mode of the most recent run is not l1_hlt_collisions/v*
144     print "Most Recent run, "+str(RunListParser.RunNumber)+", is NOT collisions"
145     sys.exit(0) # maybe we should walk back and try to find a collisions run, but for now just exit
146     CompareRunNum = RunListParser.RunNumber
147     print "Most Recent run is "+CompareRunNum
148    
149     HeadRunFile = RefRunNameTemplate % CompareRunNum
150    
151     if os.path.exists(HeadRunFile): # check if a run file for the run we want to compare already exists, it probably won't but just in case we don't have to interrogate WBM
152     HeadParser = pickle.load( open( HeadRunFile ) )
153     else:
154     HeadParser = GetRun(CompareRunNum,HeadRunFile,SaveRun,FirstLS,LastLS)
155    
156     if PrintLumi:
157     for LS in HeadParser.LumiInfo[0]:
158     try:
159     if (LS < FirstLS or LS > LastLS) and not FirstLS==999999:
160     continue
161     print str(LS)+' '+str(round(HeadParser.LumiInfo[2][LS],1))+' '+str(round((HeadParser.LumiInfo[3][LS] - HeadParser.LumiInfo[3][LS-1])*1000/23.3,0))+' '+str(round((HeadParser.LumiInfo[4][LS] - HeadParser.LumiInfo[4][LS-1])*1000/23.3,0))
162     except:
163     print "Lumisection "+str(LS-1)+" was not parsed from the LumiSections page"
164    
165     sys.exit(0)
166     if RefRunNum == 0:
167     RefRates = 0
168     RefLumis = 0
169     LastSuccessfulIterator = 0
170    
171     RunComparison(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config)
172    
173     if FindL1Zeros:
174     L1Zeros=[]
175     for key in HeadParser.TriggerRates:
176     ## Skip events in the skip list
177     skipTrig=False
178     ##for trig in Config.ExcludeList:
179     ##if not trigN.find(trig) == -1:
180     ##skipTrig=True
181     ##break
182     if skipTrig:
183     continue
184     ## if no events pass the L1, add it to the L1Zeros list if not already there
185     if HeadParser.TriggerRates[key][1]==0 and not HeadParser.TriggerRates[key][4] in L1Zeros:
186     L1Zeros.append(HeadParser.TriggerRates[key][4])
187     if len(L1Zeros) == 0:
188     print "It looks like no masked L1 bits seed trigger paths"
189     else:
190     print "The following seeds are used to seed HLT bits but accept 0 events:"
191     print "The average lumi of this run is: "+str(round(HeadParser.LumiInfo[6],1))+"e30"
192     for Seed in L1Zeros:
193     print Seed
194    
195    
196     def RunComparison(HeadParser,RefRunNum,RefRates,RefLumis,Iterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config):
197    
198     Header = ["Trigger Name","Actual","Expected","% Inc","Cur PS","Comments"]
199     Data = []
200     Warn = []
201     IgnoredRates=[]
202    
203     LumiInfo = HeadParser.LumiInfo
204     TriggerRates = HeadParser.TriggerRates
205     TriggerInfo = HeadParser.TriggerInfo
206     PrescaleValues = HeadParser.PrescaleValues
207     TotalPSInfo = HeadParser.TotalPSInfo
208     CorrectedPSInfo = HeadParser.CorrectedPSInfo
209    
210     [LumiSection,PSColumnByLS,InstLumiByLS,DeliveredLumiByLS,LiveLumiByLS,AvInstLumi,AvDeliveredLumi,AvLiveLumi] = LumiInfo
211     [L1TriggerMode, HLTTriggerMode, HLTSeed] = TriggerInfo
212     [L1Prescale,HLTPrescale,MissingPrescale] = PrescaleValues
213     [L1_zero,HLT_zero,IdealPrescale,IdealHLTPrescale,n1,n2,L1,L2,H1,H2] = TotalPSInfo
214     [RealPrescale,xLS,L1,L2,H1,H2] = CorrectedPSInfo
215    
216     for key in TriggerRates:
217     ## SKIP triggers in the skip list
218     skipTrig=False
219     ##for trig in ExcludeList:
220     ##if not headTrigN.find(trig) == -1:
221     ##skipTrig=True
222     ##break
223     ##if skipTrig:
224     ##IgnoredRates.append([headTrigN,headTrigRate]) # get the current rates of the ignored paths
225     ##continue
226    
227     if L1_zero[key] == True or HLT_zero[key] == True:
228     continue
229    
230     [TriggerRate,L1Pass,PSPass,RealHLTPrescale,Seed,StartLS,EndLS] = TriggerRates[key]
231    
232     OverallPrescale = RealPrescale[key]
233    
234     PSCorrectedRate = TriggerRate * OverallPrescale
235     #print str(key)+" made it here with TriggerRate = "+str(TriggerRate)+", PSCorrectedRate = "+str(PSCorrectedRate)
236    
237    
238     if RefRunNum == 0: ## Use rate prediction functions
239     PSCorrectedExpectedRate = Config.GetExpectedRate(key,AvLiveLumi)
240     if PSCorrectedExpectedRate < 0: ##This means we don't have a prediction for this trigger
241     continue
242     ExpectedRate = round((PSCorrectedExpectedRate / OverallPrescale),2)
243     PerDiff=0
244     if ExpectedRate>0:
245     PerDiff = int(round( (TriggerRate-ExpectedRate)/ExpectedRate,2 )*100)
246     if abs(PerDiff) > AllowedRateDiff/max(sqrt(TriggerRate),sqrt(ExpectedRate)):
247     Warn.append(True)
248     else:
249     Warn.append(False)
250    
251     if TriggerRate < IgnoreThreshold and ExpectedRate < IgnoreThreshold:
252     continue
253    
254     VC = ""
255     Data.append([key,TriggerRate,ExpectedRate,PerDiff,OverallPrescale,VC])
256     continue
257    
258     else: ## Use a reference run
259    
260     if not L1Prescale[HLTSeed[key]][StartLS] == 1 or not L1Prescale[HLTSeed[key]][EndLS] == 1:
261     continue
262    
263     RefInstLumi = 0
264     RefIterator = 0
265    
266     for Iterator2 in range(1,Iterator+1):
267     if abs(RefLumis[2][Iterator2*10] - AvInstLumi) < abs(RefInstLumi - AvInstLumi):
268     RefInstLumi = RefLumis[2][Iterator2*10]
269     RefIterator = int(Iterator2)
270    
271     RefRate = RefRates[RefIterator]
272     RefAvDeliveredLumi = (RefLumis[3][(RefIterator+1)*10] - RefLumis[3][RefIterator*10])*1000/(23.3*10)
273     RefAvLiveLumi = (RefLumis[4][(RefIterator+1)*10] - RefLumis[4][RefIterator*10])*1000/(23.3*10)
274    
275     try:
276     ScaledRefRate = round( ((RefRate[key][0]*RefRate[key][3])/OverallPrescale)*(AvLiveLumi/RefAvLiveLumi), 2)
277     except:
278     print "Maybe trigger "+str(key)+" does not exist in the reference run?"
279    
280     if ScaledRefRate == 0:
281     PerDiff = 100
282     else:
283     PerDiff = int( round( (TriggerRate - ScaledRefRate)/ScaledRefRate , 2)*100)
284    
285     if TriggerRate < IgnoreThreshold and ScaledRefRate < IgnoreThreshold:
286     continue
287    
288     if abs(PerDiff) > AllowedRateDiff/max(sqrt(TriggerRate),sqrt(ScaledRefRate)):
289     Warn.append(True)
290     else:
291     Warn.append(False)
292     VC = ""
293     Data.append([key,TriggerRate,ScaledRefRate,PerDiff,OverallPrescale,VC])
294     continue
295    
296     PrettyPrintTable(Header,Data,[80,10,10,10,10,20],Warn)
297    
298    
299     if __name__=='__main__':
300     main()