ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/DatabaseRateMonitor.py
Revision: 1.46
Committed: Fri Jul 20 12:11:15 2012 UTC (12 years, 9 months ago) by awoodard
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-01-03
Changes since 1.45: +6 -3 lines
Log Message:
Adding configuration option to choose whether to warn on percent difference or sigma difference

File Contents

# User Rev Content
1 amott 1.1 #!/usr/bin/env python
2    
3 amott 1.15 #from AndrewGetRun import GetRun
4 amott 1.1 from ReadConfig import RateMonConfig
5     import sys
6     import os
7     import cPickle as pickle
8     import getopt
9     import time
10     from colors import *
11     from TablePrint import *
12     from AddTableInfo_db import MoreTableInfo
13     from math import *
14 grchrist 1.43 from DatabaseParser import *
15    
16 amott 1.1
17     WBMPageTemplate = "http://cmswbm/cmsdb/servlet/RunSummary?RUN=%s&DB=cms_omds_lb"
18     WBMRunInfoPage = "https://cmswbm/cmsdb/runSummary/RunSummary_1.html"
19 grchrist 1.34 RefRunNameTemplate = "RefRuns/%s/Run_%s.pk"
20 amott 1.1
21     # define a function that clears the terminal screen
22     def clear():
23     print("\x1B[2J")
24    
25    
26     def usage():
27     print sys.argv[0]+" [Options]"
28 awoodard 1.44 print "This script gets the current HLT trigger rates and compares them to a reference run or a fit to multiple runs"
29 amott 1.1 print "Options: "
30 awoodard 1.44 print "--AllowedPercDiff=<diff> Report only if difference in trigger rate is greater than <diff>%"
31     print "--AllowedSigmaDiff=<diff> Report only if difference in trigger rate is greater than <diff> standard deviations"
32 amott 1.1 print "--CompareRun=<Run #> Compare run <Run #> to the reference run (Default = Current Run)"
33     print "--FindL1Zeros Look for physics paths with 0 L1 rate"
34     print "--FirstLS=<ls> Specify the first lumisection to consider. This will set LSSlidingWindow to -1"
35     print "--NumberLS=<#> Specify the last lumisection to consider. Make sure LastLS > LSSlidingWindow"
36     print " or set LSSlidingWindow = -1"
37     print "--IgnoreLowRate=<rate> Ignore triggers with an actual and expected rate below <rate>"
38     print "--ListIgnoredPaths Prints the paths that are not compared by this script and their rate in the CompareRun"
39     print "--PrintLumi Prints Instantaneous, Delivered, and Live lumi by LS for the run"
40     print "--RefRun=<Run #> Specifies <Run #> as the reference run to use (Default in defaults.cfg)"
41     print "--ShowPSTriggers Show prescaled triggers in rate comparison"
42 amott 1.33 print "--sortBy=<field> Sort the triggers by field. Valid fields are: name, rate, rateDiff"
43 amott 1.2 print "--force Override the check for collisions run"
44 amott 1.1 print "--help Print this help"
45 grchrist 1.20
46     def pickYear():
47     global thisyear
48 grchrist 1.23 thisyear="2012"
49     ##print "Year set to ",thisyear
50 grchrist 1.20
51 amott 1.1 def main():
52 grchrist 1.21 pickYear()
53 amott 1.1 try:
54 awoodard 1.44 opt, args = getopt.getopt(sys.argv[1:],"",["AllowedPercDiff=","AllowedSigmaDiff=","CompareRun=","FindL1Zeros",\
55 amott 1.1 "FirstLS=","NumberLS=","IgnoreLowRate=","ListIgnoredPaths",\
56 amott 1.33 "PrintLumi","RefRun=","ShowPSTriggers","force","sortBy=","help"])
57 amott 1.1 except getopt.GetoptError, err:
58     print str(err)
59     usage()
60     sys.exit(2)
61    
62     Config = RateMonConfig(os.path.abspath(os.path.dirname(sys.argv[0])))
63     for o,a in opt:
64     if o=="--ConfigFile":
65     Config.CFGfile=a
66     Config.ReadCFG()
67 grchrist 1.35
68    
69     if "NoV" in Config.FitFileName:
70     Config.NoVersion=True
71 grchrist 1.43 #print "NoVersion=",Config.NoVersion
72 grchrist 1.23
73 awoodard 1.46 WarnOnSigmaDiff = Config.DefWarnOnSigmaDiff
74 awoodard 1.44 AllowedRatePercDiff = Config.DefAllowRatePercDiff
75     AllowedRateSigmaDiff = Config.DefAllowRateSigmaDiff
76 amott 1.1 CompareRunNum = ""
77     FindL1Zeros = False
78     FirstLS = 9999
79 amott 1.2 NumLS = -10
80 amott 1.1 IgnoreThreshold = Config.DefAllowIgnoreThresh
81     ListIgnoredPaths = False
82     PrintLumi = False
83     RefRunNum = int(Config.ReferenceRun)
84     ShowPSTriggers = True
85 amott 1.2 Force = False
86 amott 1.33 SortBy = ""
87     ShifterMode = int(Config.ShifterMode) # get this from the config, but can be overridden by other options
88 grchrist 1.23
89     ## if int(Config.ShifterMode):
90     ## print "ShifterMode!!"
91     ## else:
92     ## print "ExpertMode"
93 amott 1.1
94     if Config.LSWindow > 0:
95     NumLS = -1*Config.LSWindow
96    
97     for o,a in opt: # get options passed on the command line
98 awoodard 1.44 if o=="--AllowedPercDiff":
99     AllowedRatePercDiff = float(a)
100     elif o=="--AllowedSigmaDiff":
101     AllowedRateSigmaDiff = float(a)
102 amott 1.1 elif o=="--CompareRun":
103     CompareRunNum=int(a)
104 amott 1.33 ShifterMode = False
105 amott 1.1 elif o=="--FindL1Zeros":
106     FindL1Zeros = True
107     elif o=="--FirstLS":
108     FirstLS = int(a)
109 amott 1.33 ShifterMode = False
110 amott 1.1 elif o=="--NumberLS":
111     NumLS = int(a)
112     elif o=="--IgnoreLowRate":
113     IgnoreThreshold = float(a)
114     elif o=="--ListIgnoredPaths":
115     ListIgnoredPaths=True
116 amott 1.33 ShifterMode = False
117 amott 1.1 elif o=="--PrintLumi":
118     PrintLumi = True
119     elif o=="--RefRun":
120     RefRunNum=int(a)
121     elif o=="--ShowPSTriggers":
122     ShowPSTriggers=True
123 amott 1.33 elif o=="--sortBy":
124     SortBy = a
125 amott 1.2 elif o=="--force":
126     Force = True
127 amott 1.1 elif o=="--help":
128     usage()
129     sys.exit(0)
130     else:
131     print "Invalid Option "+a
132     sys.exit(1)
133    
134 grchrist 1.23
135 amott 1.1 RefLumisExists = False
136     """
137 grchrist 1.34 RefRunFile=RefRunNameTemplate % str(RefRunNum)
138 amott 1.1 if RefRunNum > 0:
139     RefRates = {}
140     for Iterator in range(1,100):
141     if RefLumisExists: ## Quits at the end of a run
142     if max(RefLumis[0]) <= (Iterator+1)*10:
143     break
144    
145     RefRunFile = RefRunNameTemplate % str( RefRunNum*100 + Iterator ) # place to save the reference run info
146     print "RefRunFile=",RefRunFile
147     if not os.path.exists(RefRunFile[:RefRunFile.rfind('/')]): # folder for ref run file must exist
148     print "Reference run folder does not exist, please create" # should probably create programmatically, but for now force user to create
149     print RefRunFile[:RefRunFile.rfind('/')]
150     sys.exit(0)
151    
152     if not os.path.exists(RefRunFile): # if the reference run is not saved, get it from wbm
153     print "Reference Run File for run "+str(RefRunNum)+" iterator "+str(Iterator)+" does not exist"
154     print "Creating ..."
155     try:
156     RefParser = GetRun(RefRunNum, RefRunFile, True, Iterator*10, (Iterator+1)*10)
157     print "parsing"
158     except:
159     print "GetRun failed from LS "+str(Iterator*10)+" to "+str((Iterator+1)*10)
160     continue
161    
162     else: # otherwise load it from the file
163     RefParser = pickle.load( open( RefRunFile ) )
164     print "loading"
165     if not RefLumisExists:
166     RefLumis = RefParser.LumiInfo
167     RefLumisExists = True
168    
169     try:
170     RefRates[Iterator] = RefParser.TriggerRates # get the trigger rates from the reference run
171     LastSuccessfulIterator = Iterator
172     except:
173     print "Failed to get rates from LS "+str(Iterator*10)+" to "+str((Iterator+1)*10)
174 grchrist 1.34
175 amott 1.1 """
176 grchrist 1.34 RefRunFile = RefRunNameTemplate % (thisyear,RefRunNum)
177 amott 1.1 RefParser = DatabaseParser()
178 grchrist 1.23 ##print "Reference Run: "+str(RefRunNum)
179 amott 1.1 if RefRunNum > 0:
180 grchrist 1.34 print "Geting RefRunFile",RefRunFile
181 amott 1.1 if not os.path.exists(RefRunFile[:RefRunFile.rfind('/')]): # folder for ref run file must exist
182     print "Reference run folder does not exist, please create" # should probably create programmatically, but for now force user to create
183     print RefRunFile[:RefRunFile.rfind('/')]
184 grchrist 1.27 sys.exit(0)
185 grchrist 1.34 s
186 amott 1.1 return
187     if not os.path.exists(RefRunFile):
188 grchrist 1.34 print "RefRunFile does not exist"
189 amott 1.1 # create the reference run file
190     try:
191     RefParser.RunNumber = RefRunNum
192     RefParser.ParseRunSetup()
193 grchrist 1.34 print "RefParser is setup"
194 amott 1.1 #RefParser.GetAllTriggerRatesByLS()
195     #RefParser.Save( RefRunFile )
196     except e:
197     print "PROBLEM GETTING REFERNCE RUN"
198     raise
199     else:
200     RefParser = pickle.load( open( RefRunFile ) )
201    
202     # OK, Got the Reference Run
203     # Now get the most recent run
204    
205     SaveRun = False
206     if CompareRunNum=="": # if no run # specified on the CL, get the most recent run
207 grchrist 1.22 CompareRunNum,isCol,isGood = GetLatestRunNumber()
208 grchrist 1.26
209    
210 grchrist 1.22 if not isGood:
211 grchrist 1.23 print "NO TRIGGER KEY FOUND for run ",CompareRunNum
212 grchrist 1.22
213 grchrist 1.23 ##sys.exit(0)
214 amott 1.1
215     if not isCol:
216     print "Most Recent run, "+str(CompareRunNum)+", is NOT collisions"
217 amott 1.16 print "Monitoring only stream A and Express"
218     #if not Force:
219     # sys.exit(0) # maybe we should walk back and try to find a collisions run, but for now just exit
220 grchrist 1.23
221     else:
222     print "Most Recent run is "+str(CompareRunNum)
223 amott 1.16 else:
224 grchrist 1.22 CompareRunNum,isCol,isGood = GetLatestRunNumber(CompareRunNum)
225     if not isGood:
226 grchrist 1.23 print "NO TRIGGER KEY FOUND for run ", CompareRunNum
227     ##sys.exit(0)
228 amott 1.17
229 grchrist 1.23
230 amott 1.17 HeadParser = DatabaseParser()
231     HeadParser.RunNumber = CompareRunNum
232 grchrist 1.23
233     try:
234     HeadParser.ParseRunSetup()
235     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,isCol)
236 grchrist 1.43 LastGoodLS=HeadParser.GetLastLS(isCol)+1
237     tempLastGoodLS=LastGoodLS
238 grchrist 1.23 CurrRun=CompareRunNum
239 grchrist 1.43 #print "done good"
240 grchrist 1.23 except:
241 grchrist 1.43 #print "exception"
242 grchrist 1.23 HeadLumiRange=[]
243     LastGoodLS=-1
244 grchrist 1.43 tempLastGoodLS=LastGoodLS-1
245 grchrist 1.23 CurrRun=CompareRunNum
246     isGood=0
247    
248     if len(HeadLumiRange) is 0:
249 grchrist 1.36 print "No lumisections that are taking physics data 0"
250 grchrist 1.23 HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,False)
251     if len(HeadLumiRange)>0:
252     isGood=1
253     isCol=0
254     ##sys.exit(0)
255 awoodard 1.44
256 awoodard 1.45 ## This reduces the sensitivity for a rate measurement to cause a warning during the beginning of a run
257 awoodard 1.44 if len(HeadLumiRange) < 10:
258     AllowedRateSigmaDiff = AllowedRateSigmaDiff*10 / len(HeadLumiRange)
259 grchrist 1.23
260 amott 1.1 if PrintLumi:
261     for LS in HeadParser.LumiInfo[0]:
262     try:
263     if (LS < FirstLS or LS > LastLS) and not FirstLS==999999:
264     continue
265     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))
266     except:
267     print "Lumisection "+str(LS-1)+" was not parsed from the LumiSections page"
268    
269 grchrist 1.23 sys.exit(0)
270 amott 1.1
271     if RefRunNum == 0:
272     RefRates = 0
273     RefLumis = 0
274     LastSuccessfulIterator = 0
275    
276     ### Now actually compare the rates, make tables and look at L1. Loops for ShifterMode
277     #CheckTriggerList(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config)
278 grchrist 1.23
279     ###isGood=1##if there is a trigger key
280 amott 1.1 try:
281     while True:
282 grchrist 1.23
283     if isGood:
284 grchrist 1.43 tempLastGoodLS=LastGoodLS
285 grchrist 1.23 LastGoodLS=HeadParser.GetLastLS(isCol)
286 grchrist 1.43 ##print "Last Good=",LastGoodLS, tempLastGoodLS
287     if LastGoodLS==tempLastGoodLS:
288     write(bcolors.FAIL)
289     print "Trying to get new Run"
290     write(bcolors.ENDC+"\n")
291     else:
292     RefMoreLumiArray = HeadParser.GetMoreLumiInfo()
293     isBeams=True
294     for lumisection in HeadLumiRange:
295     try:
296     if not (RefMoreLumiArray["b1pres"][lumisection] and RefMoreLumiArray["b2pres"][lumisection] and RefMoreLumiArray["b1stab"][lumisection] and RefMoreLumiArray["b2stab"][lumisection]):
297     isBeams=False
298     except:
299 grchrist 1.41 isBeams=False
300 grchrist 1.40
301 grchrist 1.43 if not (isCol and isBeams):
302 grchrist 1.23 ##clear()
303 grchrist 1.43 MoreTableInfo(HeadParser,HeadLumiRange,Config,False)
304 grchrist 1.25 else:
305 grchrist 1.43 if (len(HeadLumiRange)>0):
306 awoodard 1.46 RunComparison(HeadParser,RefParser,HeadLumiRange,ShowPSTriggers,AllowedRatePercDiff,AllowedRateSigmaDiff,IgnoreThreshold,Config,ListIgnoredPaths,SortBy,WarnOnSigmaDiff)
307 grchrist 1.43 if FindL1Zeros:
308 awoodard 1.44 CheckL1Zeros(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRatePercDiff,AllowedRateSigmaDiff,IgnoreThreshold,Config)
309 grchrist 1.43 else:
310     print "No lumisections that are taking physics data 1"
311 amott 1.33 if ShifterMode:
312 grchrist 1.23 #print "Shifter Mode. Continuing"
313     pass
314 amott 1.2 else:
315 grchrist 1.23 print "Expert Mode. Quitting."
316     sys.exit(0)
317 amott 1.2
318 amott 1.1
319     print "Sleeping for 1 minute before repeating "
320 amott 1.16 for iSleep in range(20):
321 amott 1.15 write(".")
322     sys.stdout.flush()
323 amott 1.16 time.sleep(3)
324 grchrist 1.23 write(" Updating\n")
325 amott 1.15 sys.stdout.flush()
326 grchrist 1.23
327     ##print "\nminLS=",min(HeadLumiRange),"Last LS=",HeadParser.GetLastLS(isCol),"run=",HeadParser.RunNumber
328     ###Get a new run if DAQ stops
329     ##print "\nLastGoodLS=",LastGoodLS
330    
331     ##### NEED PLACEHOLDER TO COMPARE CURRENT RUN TO LATEST RUN #####
332    
333     NewRun,isCol,isGood = GetLatestRunNumber(9999999) ## update to the latest run and lumi range
334    
335     try:
336     maxLumi=max(HeadLumiRange)
337     except:
338     maxLumi=0
339    
340     ##### THESE ARE CONDITIONS TO GET NEW RUN #####
341     if maxLumi>(LastGoodLS+1) or not isGood or NewRun!=CurrRun:
342     print "Trying to get new Run"
343     try:
344     HeadParser = DatabaseParser()
345     HeadParser.RunNumber = NewRun
346     HeadParser.ParseRunSetup()
347     CurrRun,isCol,isGood=GetLatestRunNumber(9999999)
348     FirstLS=9999
349     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,isCol)
350     if len(HeadLumiRange) is 0:
351     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,False)
352 grchrist 1.36 print "No lumisections that are taking physics data 2"
353 grchrist 1.23 if len(HeadLumiRange)>0:
354     isGood=1
355     isCol=0
356    
357 grchrist 1.43 #tempLastGoodLS=LastGoodLS
358     #LastGoodLS=HeadParser.GetLastLS(isCol)
359 grchrist 1.25 ##print CurrRun, isCol, isGood
360 grchrist 1.23 except:
361     isGood=0
362     isCol=0
363     print "failed"
364    
365    
366    
367    
368     ##CurrRun,isCol,isGood = GetLatestRunNumber(CurrRun) ## update to the latest run and lumi range
369     else:
370     try:
371     HeadParser.ParseRunSetup()
372     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,isCol)
373     if len(HeadLumiRange) is 0:
374     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,False)
375 grchrist 1.26 print "No lumisections that are taking physics data"
376 grchrist 1.23 if len(HeadLumiRange)>0:
377     isGood=1
378     isCol=0
379 grchrist 1.43 #LastGoodLS=HeadParser.GetLastLS(isCol)
380 grchrist 1.23
381     except:
382     isGood=0
383     isCol=0
384     clear()
385     print "NO TRIGGER KEY FOUND YET for run", NewRun ,"repeating search"
386    
387    
388     ## try:
389     ## HeadParser.GetLumiInfo()
390     ## if len(HeadParser.Active)>0:
391     ## isGood=1
392     ## ##print "setting to good"
393     ## except:
394     ## pass
395 grchrist 1.22
396 grchrist 1.23
397 amott 1.1 #end while True
398     #end try
399     except KeyboardInterrupt:
400     print "Quitting. Peace Out."
401    
402    
403 awoodard 1.46 def RunComparison(HeadParser,RefParser,HeadLumiRange,ShowPSTriggers,AllowedRatePercDiff,AllowedRateSigmaDiff,IgnoreThreshold,Config,ListIgnoredPaths,SortBy,WarnOnSigmaDiff):
404 amott 1.1 Data = []
405     Warn = []
406     IgnoredRates=[]
407 grchrist 1.23
408 abrinke1 1.9 [HeadAvInstLumi,HeadAvLiveLumi,HeadAvDeliveredLumi,HeadAvDeadTime,HeadPSCols] = HeadParser.GetAvLumiInfo(HeadLumiRange)
409 abrinke1 1.3 ##[HeadUnprescaledRates, HeadTotalPrescales, HeadL1Prescales, HeadTriggerRates] = HeadParser.UpdateRun(HeadLumiRange)
410     HeadUnprescaledRates = HeadParser.UpdateRun(HeadLumiRange)
411 abrinke1 1.9 [PSColumnByLS,InstLumiByLS,DeliveredLumiByLS,LiveLumiByLS,DeadTimeByLS,PhysicsByLS,ActiveByLS] = HeadParser.LumiInfo
412 grchrist 1.30 deadtimebeamactive=HeadParser.GetDeadTimeBeamActive(HeadLumiRange)
413 grchrist 1.18 try:
414     pkl_file = open(Config.FitFileName, 'rb')
415     FitInput = pickle.load(pkl_file)
416     pkl_file.close()
417 grchrist 1.29 ##print "fit file name=",Config.FitFileName
418 grchrist 1.31
419 grchrist 1.18 except:
420 grchrist 1.27 print "No fit file specified"
421 grchrist 1.20 sys.exit(2)
422    
423 grchrist 1.23 try:
424     refrunfile="RefRuns/%s/Rates_HLT_10LS_JPAP.pkl" % (thisyear)
425     pkl_file = open(refrunfile, 'rb')
426     RefRatesInput = pickle.load(pkl_file)
427     pkl_file.close()
428     except:
429 grchrist 1.24 RefRatesInput={}
430 grchrist 1.34 print "Didn't open ref file"
431 amott 1.1
432 grchrist 1.30
433     trig_list=Config.MonitorList
434    
435     if Config.NoVersion:
436     trig_list=[]
437     FitInputNoV={}
438    
439     for trigger in Config.MonitorList:
440     trig_list.append(StripVersion(trigger))
441     for trigger in FitInput.iterkeys():
442     FitInputNoV[StripVersion(trigger)]=FitInput[trigger]
443     FitInput=FitInputNoV
444 grchrist 1.31
445 grchrist 1.30
446     ##trig_list=Config.MonitorList
447 abrinke1 1.3 for HeadName in HeadUnprescaledRates:
448 grchrist 1.30
449     HeadNameNoV=StripVersion(HeadName)
450 grchrist 1.34
451 grchrist 1.42 if RefParser.RunNumber == 0: ## If not ref run then just use trigger list
452     pass
453     if Config.NoVersion:
454     if HeadNameNoV not in trig_list and not ListIgnoredPaths:
455     continue
456     if HeadNameNoV not in FitInput.keys() and not ListIgnoredPaths:
457     continue
458     else:
459     if HeadName not in trig_list and not ListIgnoredPaths:
460     continue
461     if HeadName not in FitInput.keys() and not ListIgnoredPaths:
462     continue
463 grchrist 1.41 ## else:
464     ## if HeadUnprescaledRates[HeadName][2]<0.5:
465     ## continue
466 grchrist 1.34
467     ##if HeadUnprescaledRates[HeadName][0]>1.9:
468     ## continue
469    
470 grchrist 1.24
471 grchrist 1.25 ##masked_triggers = ["AlCa_", "DST_", "HLT_L1", "HLT_L2", "HLT_Zero"]
472 grchrist 1.28 masked_triggers = ["AlCa_", "DST_", "HLT_L1", "HLT_Zero"]
473 abrinke1 1.9 masked_trig = False
474     for mask in masked_triggers:
475     if str(mask) in HeadName:
476     masked_trig = True
477     if masked_trig:
478     continue
479    
480 amott 1.1 skipTrig=False
481 abrinke1 1.3 TriggerRate = round(HeadUnprescaledRates[HeadName][2],2)
482 amott 1.1 if RefParser.RunNumber == 0: ## Use rate prediction functions
483 awoodard 1.44
484 grchrist 1.30 PSCorrectedExpectedRate = Config.GetExpectedRate(HeadName,FitInput,RefRatesInput,HeadAvLiveLumi,HeadAvDeliveredLumi,deadtimebeamactive)
485 abrinke1 1.9
486 amott 1.33 #if PSCorrectedExpectedRate[0] < 0: ##This means we don't have a prediction for this trigger
487     # continue
488 grchrist 1.32 try:
489     ExpectedRate = round((PSCorrectedExpectedRate[0] / HeadUnprescaledRates[HeadName][1]),2)
490 awoodard 1.44 sigma = round((sqrt(PSCorrectedExpectedRate[1]) / HeadUnprescaledRates[HeadName][1]),2)
491    
492 grchrist 1.32 except:
493     ExpectedRate=0.
494 grchrist 1.30
495 amott 1.1 PerDiff=0
496 awoodard 1.44 SigmaDiff=0
497 grchrist 1.37 VC = ""
498 amott 1.1 if ExpectedRate>0:
499     PerDiff = int(round( (TriggerRate-ExpectedRate)/ExpectedRate,2 )*100)
500 awoodard 1.44 SigmaDiff = round( (TriggerRate - ExpectedRate)/sigma, 2)
501 grchrist 1.37 else:
502     PerDiff=-999.
503 awoodard 1.44 SigmaDiff=-999
504 grchrist 1.37 if ExpectedRate==0:
505     VC="0 expected rate"
506     else:
507     VC="lt 0 expected rate"
508 amott 1.1
509 amott 1.33 if TriggerRate < IgnoreThreshold and (ExpectedRate < IgnoreThreshold and ExpectedRate!=0):
510 amott 1.1 continue
511    
512 awoodard 1.44 Data.append([HeadName, TriggerRate, ExpectedRate, PerDiff, SigmaDiff, round(HeadUnprescaledRates[HeadName][1],1),VC])
513 amott 1.1
514     else: ## Use a reference run
515     ## cheap trick to only get triggers in list when in shifter mode
516     #print "shifter mode=",int(Config.ShifterMode)
517 grchrist 1.34
518    
519    
520    
521     ##if not HeadParser.AvgL1Prescales[HeadParser.HLTSeed[HeadName]]==1:
522     ## continue
523 amott 1.1
524     RefInstLumi = 0
525     RefIterator = 0
526     RefStartIndex = ClosestIndex(HeadAvInstLumi,RefParser.GetAvLumiPerRange())
527     RefLen = -10
528 grchrist 1.34
529    
530 abrinke1 1.3 RefUnprescaledRates = RefParser.UpdateRun(RefParser.GetLSRange(RefStartIndex,RefLen))
531 amott 1.1 [RefAvInstLumi,RefAvLiveLumi,RefAvDeliveredLumi,RefAvDeadTime,RefPSCols] = RefParser.GetAvLumiInfo(RefParser.GetLSRange(RefStartIndex,RefLen))
532 grchrist 1.34 deadtimebeamactive=RefParser.GetDeadTimeBeamActive(RefParser.GetLSRange(RefStartIndex,RefLen))
533    
534 amott 1.1 RefRate = -1
535     for k,v in RefUnprescaledRates.iteritems():
536 grchrist 1.19 #if StripVersion(HeadName) == StripVersion(k): # versions may not match
537 grchrist 1.34 if HeadName==k:
538     RefRate = RefUnprescaledRates[k][2]
539    
540     try:
541     ScaledRefRate = round( (RefRate*HeadAvLiveLumi/RefAvLiveLumi*(1-deadtimebeamactive)), 2 )
542    
543     except ZeroDivisionError:
544     ScaledRefRate=0
545    
546     ##print HeadName,"ScaledRefRate=",ScaledRefRate
547 awoodard 1.44 SigmaDiff = 0
548 amott 1.1 if ScaledRefRate == 0:
549 awoodard 1.44 PerDiff = -999
550 amott 1.1 else:
551     PerDiff = int( round( (TriggerRate - ScaledRefRate)/ScaledRefRate , 2)*100)
552 awoodard 1.44
553 amott 1.1 if TriggerRate < IgnoreThreshold and ScaledRefRate < IgnoreThreshold:
554     continue
555    
556     VC = ""
557 awoodard 1.44 Data.append([HeadName,TriggerRate,ScaledRefRate,PerDiff,SigmaDiff,round((HeadUnprescaledRates[HeadName][1]),1),VC])
558 amott 1.1
559 amott 1.33 SortedData = []
560     if SortBy == "":
561     SortedData = Data # don't do any sorting
562 grchrist 1.35 if RefParser.RunNumber>0:
563 grchrist 1.34 SortedData=sorted(Data, key=lambda entry: abs(entry[3]),reverse=True)
564 amott 1.33 elif SortBy == "name":
565     SortedData=sorted(Data, key=lambda entry: entry[0])
566     elif SortBy == "rate":
567     SortedData=sorted(Data, key=lambda entry: entry[1],reverse=True)
568 awoodard 1.44 elif SortBy == "ratePercDiff":
569 amott 1.33 SortedData=sorted(Data, key=lambda entry: abs(entry[3]),reverse=True)
570 awoodard 1.44 elif SortBy == "rateSigmaDiff":
571     SortedData=sorted(Data, key=lambda entry: abs(entry[4]),reverse=True)
572 amott 1.33 else:
573     print "Invalid sorting option %s\n"%SortBy
574     SortedData = Data
575    
576     #check for triggers above the warning threshold
577     Warn=[]
578     for entry in SortedData:
579 awoodard 1.46 if abs(entry[3]) > AllowedRatePercDiff and WarnOnSigmaDiff == 0:
580     Warn.append(True)
581     elif abs(entry[4]) > AllowedRateSigmaDiff and WarnOnSigmaDiff == 1:
582 amott 1.33 Warn.append(True)
583     else:
584     Warn.append(False)
585    
586 awoodard 1.44 if RefParser.RunNumber == 0:
587 awoodard 1.45 Header = ["Trigger Name", "Actual", "Expected","Deviation", "Cur PS", "Comments"]
588     table_data = [[col[0], col[1], col[2], col[4], col[5], col[6]] for col in SortedData]
589 awoodard 1.44
590     else:
591     Header = ["Trigger Name", "Actual", "Expected", "% Difference", "Cur PS", "Comments"]
592     table_data = [[col[0], col[1], col[2], col[3], col[5], col[6]] for col in SortedData]
593    
594 awoodard 1.45 PrettyPrintTable(Header,table_data,[80,10,10,10,10,20],Warn)
595    
596 awoodard 1.44 if RefParser.RunNumber == 0:
597     print 'Deviation is the difference between the actual and expected rates, in units of the expected standard deviation.'
598 amott 1.1
599 amott 1.4 MoreTableInfo(HeadParser,HeadLumiRange,Config)
600 awoodard 1.44
601 grchrist 1.39 for warning in Warn:
602     if warning==True:
603     write(bcolors.WARNING)
604     print "If any trigger remains red for 3 minutes, CALL HLT DOC"
605     print "More instructions at https://twiki.cern.ch/twiki/bin/view/CMS/TriggerShiftHLTGuide"
606     write(bcolors.ENDC+"\n")
607     break
608    
609 awoodard 1.44 def CheckTriggerList(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedPercRateDiff,IgnoreThreshold,Config):
610 amott 1.1 print "checking trigger list"
611    
612 awoodard 1.44 def CheckL1Zeros(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedPercRateDiff,IgnoreThreshold,Config):
613 amott 1.1 L1Zeros=[]
614     IgnoreBits = ["L1_PreCollisions","L1_InterBunch_Bsc","L1_BeamHalo","L1_BeamGas_Hf"]
615     for key in HeadParser.TriggerRates:
616     ## Skip events in the skip list
617     skipTrig=False
618     ##for trig in Config.ExcludeList:
619     ##if not trigN.find(trig) == -1:
620     ##skipTrig=True
621     ##break
622     if skipTrig:
623     continue
624     ## if no events pass the L1, add it to the L1Zeros list if not already there
625     if HeadParser.TriggerRates[key][1]==0 and not HeadParser.TriggerRates[key][4] in L1Zeros:
626     if HeadParser.TriggerRates[key][4].find('L1_BeamHalo')==-1 and HeadParser.TriggerRates[key][4].find('L1_PreCollisions')==-1 and HeadParser.TriggerRates[key][4].find('L1_InterBunch_Bsc')==-1:
627    
628     L1Zeros.append(HeadParser.TriggerRates[key][4])
629     print "L1Zeros=", L1Zeros
630    
631     if len(L1Zeros) == 0:
632     #print "It looks like no masked L1 bits seed trigger paths"
633     pass
634     else:
635     print "The following seeds are used to seed HLT bits but accept 0 events:"
636     #print "The average lumi of this run is: "+str(round(HeadParser.LumiInfo[6],1))+"e30"
637     for Seed in L1Zeros:
638     print Seed
639    
640     if __name__=='__main__':
641 grchrist 1.20 global thisyear
642 amott 1.1 main()