ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/DatabaseRateMonitor.py
Revision: 1.47
Committed: Fri Jul 20 13:05:14 2012 UTC (12 years, 9 months ago) by awoodard
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-01-05, V00-01-04
Changes since 1.46: +11 -6 lines
Log Message:
Adding option to show sigma and percent error in rate monitor table

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