ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/DatabaseRateMonitor.py
Revision: 1.23
Committed: Thu Apr 5 05:03:24 2012 UTC (13 years ago) by grchrist
Content type: text/x-python
Branch: MAIN
Changes since 1.22: +136 -45 lines
Log Message:
rate monitoring should run on 2012 fine

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 DatabaseParser import *
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 AddTableInfo_db import MoreTableInfo
14     from math import *
15    
16     WBMPageTemplate = "http://cmswbm/cmsdb/servlet/RunSummary?RUN=%s&DB=cms_omds_lb"
17     WBMRunInfoPage = "https://cmswbm/cmsdb/runSummary/RunSummary_1.html"
18    
19     RefRunNameTemplate = "RefRuns/Run_%s.pk"
20    
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     print "This script gets the current HLT trigger rates and compares them to a reference run"
29     print "Options: "
30     print "--AllowedDiff=<diff> Report only if difference in trigger rate is greater than <diff>%"
31     print "--CompareRun=<Run #> Compare run <Run #> to the reference run (Default = Current Run)"
32     print "--FindL1Zeros Look for physics paths with 0 L1 rate"
33     print "--FirstLS=<ls> Specify the first lumisection to consider. This will set LSSlidingWindow to -1"
34     print "--NumberLS=<#> Specify the last lumisection to consider. Make sure LastLS > LSSlidingWindow"
35     print " or set LSSlidingWindow = -1"
36     print "--IgnoreLowRate=<rate> Ignore triggers with an actual and expected rate below <rate>"
37     print "--ListIgnoredPaths Prints the paths that are not compared by this script and their rate in the CompareRun"
38     print "--PrintLumi Prints Instantaneous, Delivered, and Live lumi by LS for the run"
39     print "--RefRun=<Run #> Specifies <Run #> as the reference run to use (Default in defaults.cfg)"
40     print "--ShowPSTriggers Show prescaled triggers in rate comparison"
41 amott 1.2 print "--force Override the check for collisions run"
42 amott 1.1 print "--help Print this help"
43 grchrist 1.20
44     def pickYear():
45     global thisyear
46 grchrist 1.23 thisyear="2012"
47     ##print "Year set to ",thisyear
48 grchrist 1.20
49 amott 1.1 def main():
50 grchrist 1.21 pickYear()
51 amott 1.1 try:
52     opt, args = getopt.getopt(sys.argv[1:],"",["AllowedDiff=","CompareRun=","FindL1Zeros",\
53     "FirstLS=","NumberLS=","IgnoreLowRate=","ListIgnoredPaths",\
54 amott 1.2 "PrintLumi","RefRun=","ShowPSTriggers","force","help"])
55 amott 1.1 except getopt.GetoptError, err:
56     print str(err)
57     usage()
58     sys.exit(2)
59    
60     Config = RateMonConfig(os.path.abspath(os.path.dirname(sys.argv[0])))
61     for o,a in opt:
62     if o=="--ConfigFile":
63     Config.CFGfile=a
64     Config.ReadCFG()
65 grchrist 1.12
66 grchrist 1.23
67    
68 amott 1.1 AllowedRateDiff = Config.DefAllowRateDiff
69     CompareRunNum = ""
70     FindL1Zeros = False
71     FirstLS = 9999
72 amott 1.2 NumLS = -10
73 amott 1.1 IgnoreThreshold = Config.DefAllowIgnoreThresh
74     ListIgnoredPaths = False
75     PrintLumi = False
76     RefRunNum = int(Config.ReferenceRun)
77     ShowPSTriggers = True
78 amott 1.2 Force = False
79 amott 1.1
80 grchrist 1.23
81     ## if int(Config.ShifterMode):
82     ## print "ShifterMode!!"
83     ## else:
84     ## print "ExpertMode"
85 amott 1.1
86     if Config.LSWindow > 0:
87     NumLS = -1*Config.LSWindow
88    
89     for o,a in opt: # get options passed on the command line
90     if o=="--AllowedDiff":
91     AllowedRateDiff = float(a)/100.0
92     elif o=="--CompareRun":
93     CompareRunNum=int(a)
94     elif o=="--FindL1Zeros":
95     FindL1Zeros = True
96     elif o=="--FirstLS":
97     FirstLS = int(a)
98     elif o=="--NumberLS":
99     NumLS = int(a)
100     elif o=="--IgnoreLowRate":
101     IgnoreThreshold = float(a)
102     elif o=="--ListIgnoredPaths":
103     ListIgnoredPaths=True
104     elif o=="--PrintLumi":
105     PrintLumi = True
106     elif o=="--RefRun":
107     RefRunNum=int(a)
108     elif o=="--ShowPSTriggers":
109     ShowPSTriggers=True
110 amott 1.2 elif o=="--force":
111     Force = True
112 amott 1.1 elif o=="--help":
113     usage()
114     sys.exit(0)
115     else:
116     print "Invalid Option "+a
117     sys.exit(1)
118    
119 grchrist 1.23
120 amott 1.1 RefLumisExists = False
121    
122     """
123     if RefRunNum > 0:
124     RefRates = {}
125     for Iterator in range(1,100):
126     if RefLumisExists: ## Quits at the end of a run
127     if max(RefLumis[0]) <= (Iterator+1)*10:
128     break
129    
130     RefRunFile = RefRunNameTemplate % str( RefRunNum*100 + Iterator ) # place to save the reference run info
131     print "RefRunFile=",RefRunFile
132     if not os.path.exists(RefRunFile[:RefRunFile.rfind('/')]): # folder for ref run file must exist
133     print "Reference run folder does not exist, please create" # should probably create programmatically, but for now force user to create
134     print RefRunFile[:RefRunFile.rfind('/')]
135     sys.exit(0)
136    
137     if not os.path.exists(RefRunFile): # if the reference run is not saved, get it from wbm
138     print "Reference Run File for run "+str(RefRunNum)+" iterator "+str(Iterator)+" does not exist"
139     print "Creating ..."
140     try:
141     RefParser = GetRun(RefRunNum, RefRunFile, True, Iterator*10, (Iterator+1)*10)
142     print "parsing"
143     except:
144     print "GetRun failed from LS "+str(Iterator*10)+" to "+str((Iterator+1)*10)
145     continue
146    
147     else: # otherwise load it from the file
148     RefParser = pickle.load( open( RefRunFile ) )
149     print "loading"
150     if not RefLumisExists:
151     RefLumis = RefParser.LumiInfo
152     RefLumisExists = True
153    
154     try:
155     RefRates[Iterator] = RefParser.TriggerRates # get the trigger rates from the reference run
156     LastSuccessfulIterator = Iterator
157     except:
158     print "Failed to get rates from LS "+str(Iterator*10)+" to "+str((Iterator+1)*10)
159     """
160    
161     RefRunFile = RefRunNameTemplate % RefRunNum
162     RefParser = DatabaseParser()
163 grchrist 1.23 ##print "Reference Run: "+str(RefRunNum)
164 amott 1.1 if RefRunNum > 0:
165     if not os.path.exists(RefRunFile[:RefRunFile.rfind('/')]): # folder for ref run file must exist
166     print "Reference run folder does not exist, please create" # should probably create programmatically, but for now force user to create
167     print RefRunFile[:RefRunFile.rfind('/')]
168     sys.exit(0)
169     return
170     if not os.path.exists(RefRunFile):
171     # create the reference run file
172     try:
173     RefParser.RunNumber = RefRunNum
174     RefParser.ParseRunSetup()
175     #RefParser.GetAllTriggerRatesByLS()
176     #RefParser.Save( RefRunFile )
177     except e:
178     print "PROBLEM GETTING REFERNCE RUN"
179     raise
180     else:
181     RefParser = pickle.load( open( RefRunFile ) )
182    
183     # OK, Got the Reference Run
184     # Now get the most recent run
185    
186     SaveRun = False
187     if CompareRunNum=="": # if no run # specified on the CL, get the most recent run
188 grchrist 1.22 CompareRunNum,isCol,isGood = GetLatestRunNumber()
189     if not isGood:
190 grchrist 1.23 print "NO TRIGGER KEY FOUND for run ",CompareRunNum
191 grchrist 1.22
192 grchrist 1.23 ##sys.exit(0)
193 amott 1.1
194     if not isCol:
195     print "Most Recent run, "+str(CompareRunNum)+", is NOT collisions"
196 amott 1.16 print "Monitoring only stream A and Express"
197     #if not Force:
198     # sys.exit(0) # maybe we should walk back and try to find a collisions run, but for now just exit
199 grchrist 1.23
200     else:
201     print "Most Recent run is "+str(CompareRunNum)
202 amott 1.16 else:
203 grchrist 1.22 CompareRunNum,isCol,isGood = GetLatestRunNumber(CompareRunNum)
204     if not isGood:
205 grchrist 1.23 print "NO TRIGGER KEY FOUND for run ", CompareRunNum
206     ##sys.exit(0)
207 amott 1.17
208 grchrist 1.23
209 amott 1.17 HeadParser = DatabaseParser()
210     HeadParser.RunNumber = CompareRunNum
211 grchrist 1.23
212     try:
213     HeadParser.ParseRunSetup()
214     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,isCol)
215     LastGoodLS=HeadParser.GetLastLS(isCol)
216     CurrRun=CompareRunNum
217     except:
218     HeadLumiRange=[]
219     LastGoodLS=-1
220     CurrRun=CompareRunNum
221     isGood=0
222    
223     if len(HeadLumiRange) is 0:
224     print "No lumisections that are taking physics data"
225     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,False)
226     if len(HeadLumiRange)>0:
227     isGood=1
228     isCol=0
229     ##sys.exit(0)
230    
231    
232 amott 1.1 if PrintLumi:
233     for LS in HeadParser.LumiInfo[0]:
234     try:
235     if (LS < FirstLS or LS > LastLS) and not FirstLS==999999:
236     continue
237     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))
238     except:
239     print "Lumisection "+str(LS-1)+" was not parsed from the LumiSections page"
240    
241 grchrist 1.23 sys.exit(0)
242 amott 1.1
243     if RefRunNum == 0:
244     RefRates = 0
245     RefLumis = 0
246     LastSuccessfulIterator = 0
247    
248     ### Now actually compare the rates, make tables and look at L1. Loops for ShifterMode
249     #CheckTriggerList(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config)
250 grchrist 1.23
251     ###isGood=1##if there is a trigger key
252 amott 1.1 try:
253     while True:
254 grchrist 1.23
255     if isGood:
256     LastGoodLS=HeadParser.GetLastLS(isCol)
257     if not isCol:
258     ##clear()
259    
260     MoreTableInfo(HeadParser,HeadLumiRange,Config,False)
261     else:
262    
263     RunComparison(HeadParser,RefParser,HeadLumiRange,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config,ListIgnoredPaths)
264     if FindL1Zeros:
265     CheckL1Zeros(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config)
266     if int(Config.ShifterMode):
267     #print "Shifter Mode. Continuing"
268     pass
269 amott 1.2 else:
270 grchrist 1.23 print "Expert Mode. Quitting."
271     sys.exit(0)
272 amott 1.2
273 amott 1.1
274     print "Sleeping for 1 minute before repeating "
275 amott 1.16 for iSleep in range(20):
276 amott 1.15 write(".")
277     sys.stdout.flush()
278 amott 1.16 time.sleep(3)
279 grchrist 1.23 write(" Updating\n")
280 amott 1.15 sys.stdout.flush()
281 grchrist 1.23
282     ##print "\nminLS=",min(HeadLumiRange),"Last LS=",HeadParser.GetLastLS(isCol),"run=",HeadParser.RunNumber
283     ###Get a new run if DAQ stops
284     ##print "\nLastGoodLS=",LastGoodLS
285    
286     ##### NEED PLACEHOLDER TO COMPARE CURRENT RUN TO LATEST RUN #####
287    
288     NewRun,isCol,isGood = GetLatestRunNumber(9999999) ## update to the latest run and lumi range
289    
290     try:
291     maxLumi=max(HeadLumiRange)
292     except:
293     maxLumi=0
294    
295     ##### THESE ARE CONDITIONS TO GET NEW RUN #####
296     if maxLumi>(LastGoodLS+1) or not isGood or NewRun!=CurrRun:
297     print "Trying to get new Run"
298     try:
299     HeadParser = DatabaseParser()
300     HeadParser.RunNumber = NewRun
301     HeadParser.ParseRunSetup()
302     CurrRun,isCol,isGood=GetLatestRunNumber(9999999)
303     FirstLS=9999
304     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,isCol)
305     if len(HeadLumiRange) is 0:
306     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,False)
307     if len(HeadLumiRange)>0:
308     isGood=1
309     isCol=0
310    
311     LastGoodLS=HeadParser.GetLastLS(isCol)
312     print CurrRun, isCol, isGood
313     except:
314     isGood=0
315     isCol=0
316     print "failed"
317    
318    
319    
320    
321     ##CurrRun,isCol,isGood = GetLatestRunNumber(CurrRun) ## update to the latest run and lumi range
322     else:
323     try:
324     HeadParser.ParseRunSetup()
325     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,isCol)
326     if len(HeadLumiRange) is 0:
327     HeadLumiRange = HeadParser.GetLSRange(FirstLS,NumLS,False)
328     if len(HeadLumiRange)>0:
329     isGood=1
330     isCol=0
331     LastGoodLS=HeadParser.GetLastLS(isCol)
332    
333     except:
334     isGood=0
335     isCol=0
336     clear()
337     print "NO TRIGGER KEY FOUND YET for run", NewRun ,"repeating search"
338    
339    
340     ## try:
341     ## HeadParser.GetLumiInfo()
342     ## if len(HeadParser.Active)>0:
343     ## isGood=1
344     ## ##print "setting to good"
345     ## except:
346     ## pass
347 grchrist 1.22
348 grchrist 1.23
349 amott 1.1 #end while True
350     #end try
351     except KeyboardInterrupt:
352     print "Quitting. Peace Out."
353    
354    
355 grchrist 1.12 def RunComparison(HeadParser,RefParser,HeadLumiRange,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config,ListIgnoredPaths):
356 amott 1.1
357     Header = ["Trigger Name","Actual","Expected","% Inc","Cur PS","Comments"]
358     Data = []
359     Warn = []
360     IgnoredRates=[]
361 grchrist 1.23
362 abrinke1 1.9 [HeadAvInstLumi,HeadAvLiveLumi,HeadAvDeliveredLumi,HeadAvDeadTime,HeadPSCols] = HeadParser.GetAvLumiInfo(HeadLumiRange)
363 abrinke1 1.3 ##[HeadUnprescaledRates, HeadTotalPrescales, HeadL1Prescales, HeadTriggerRates] = HeadParser.UpdateRun(HeadLumiRange)
364     HeadUnprescaledRates = HeadParser.UpdateRun(HeadLumiRange)
365 abrinke1 1.9 [PSColumnByLS,InstLumiByLS,DeliveredLumiByLS,LiveLumiByLS,DeadTimeByLS,PhysicsByLS,ActiveByLS] = HeadParser.LumiInfo
366    
367 grchrist 1.18 try:
368    
369     pkl_file = open(Config.FitFileName, 'rb')
370     FitInput = pickle.load(pkl_file)
371     pkl_file.close()
372     except:
373 grchrist 1.20 "No fit file specified"
374     sys.exit(2)
375    
376 grchrist 1.23 try:
377     refrunfile="RefRuns/%s/Rates_HLT_10LS_JPAP.pkl" % (thisyear)
378     pkl_file = open(refrunfile, 'rb')
379     RefRatesInput = pickle.load(pkl_file)
380     pkl_file.close()
381     except:
382     pass
383 amott 1.1
384 abrinke1 1.3 for HeadName in HeadUnprescaledRates:
385 amott 1.1 ## SKIP triggers in the skip list
386 abrinke1 1.3 ## if not HeadTotalPrescales.has_key(HeadName): ## for whatever reason we have no prescale here, so skip (calibration paths)
387     ## continue
388     ## if not HeadTotalPrescales[HeadName]: ## prescale is thought to be 0
389     ## continue
390 grchrist 1.12
391     ## unless we are Listing Ignored paths only look at triggers in the .list file specifed in defaults.cfg
392 grchrist 1.19
393     #if StripVersion(HeadName) not in Config.MonitorList and not ListIgnoredPaths:
394    
395     if HeadName not in Config.MonitorList and not ListIgnoredPaths:
396 grchrist 1.12 continue
397    
398 abrinke1 1.9 masked_triggers = ["AlCa_", "DST_", "HLT_L1", "HLT_L2", "HLT_Zero"]
399     masked_trig = False
400     for mask in masked_triggers:
401     if str(mask) in HeadName:
402     masked_trig = True
403     if masked_trig:
404     continue
405    
406 amott 1.1 skipTrig=False
407 abrinke1 1.3 TriggerRate = round(HeadUnprescaledRates[HeadName][2],2)
408 abrinke1 1.9
409 amott 1.1 if RefParser.RunNumber == 0: ## Use rate prediction functions
410    
411 abrinke1 1.9 ##PSCorrectedExpectedRate = Config.GetExpectedRate(StripVersion(HeadName),HeadAvInstLumi)
412 grchrist 1.19 PSCorrectedExpectedRate = Config.GetExpectedRate(HeadName,FitInput,RefRatesInput,HeadAvLiveLumi,HeadAvDeliveredLumi)
413 abrinke1 1.9
414     if PSCorrectedExpectedRate[0] < 0: ##This means we don't have a prediction for this trigger
415 amott 1.1 continue
416 abrinke1 1.3 ## if not HeadTotalPrescales[HeadName]:
417     ## print HeadName+ " has total prescale 0"
418     ## continue
419 abrinke1 1.9 ExpectedRate = round((PSCorrectedExpectedRate[0] / HeadUnprescaledRates[HeadName][1]),2)
420 amott 1.1 PerDiff=0
421     if ExpectedRate>0:
422     PerDiff = int(round( (TriggerRate-ExpectedRate)/ExpectedRate,2 )*100)
423 grchrist 1.13 if abs(PerDiff) > max(AllowedRateDiff/max(sqrt(TriggerRate),sqrt(ExpectedRate)),AllowedRateDiff/2.0):
424 abrinke1 1.9 Warn.append(True)
425     else:
426     Warn.append(False)
427 amott 1.1 else:
428     Warn.append(False)
429    
430     if TriggerRate < IgnoreThreshold and ExpectedRate < IgnoreThreshold:
431     continue
432    
433     VC = ""
434    
435 abrinke1 1.3 Data.append([HeadName,TriggerRate,ExpectedRate,PerDiff,round(HeadUnprescaledRates[HeadName][1],1),VC])
436 amott 1.1
437     else: ## Use a reference run
438     ## cheap trick to only get triggers in list when in shifter mode
439     #print "shifter mode=",int(Config.ShifterMode)
440     if int(Config.ShifterMode)==1:
441     if not HeadParser.AvgL1Prescales[HeadParser.HLTSeed[HeadName]]==1:
442     continue
443    
444     RefInstLumi = 0
445     RefIterator = 0
446    
447     RefStartIndex = ClosestIndex(HeadAvInstLumi,RefParser.GetAvLumiPerRange())
448     RefLen = -10
449    
450 abrinke1 1.3 ##[RefUnprescaledRates, RefTotalPrescales, RefL1Prescales, RefTriggerRates] = RefParser.UpdateRun(RefParser.GetLSRange(RefStartIndex,RefLen))
451     RefUnprescaledRates = RefParser.UpdateRun(RefParser.GetLSRange(RefStartIndex,RefLen))
452 amott 1.1 [RefAvInstLumi,RefAvLiveLumi,RefAvDeliveredLumi,RefAvDeadTime,RefPSCols] = RefParser.GetAvLumiInfo(RefParser.GetLSRange(RefStartIndex,RefLen))
453     RefRate = -1
454     for k,v in RefUnprescaledRates.iteritems():
455 grchrist 1.19 #if StripVersion(HeadName) == StripVersion(k): # versions may not match
456     RefRate = v
457 amott 1.1
458 abrinke1 1.3 ScaledRefRate = round( RefRate*HeadAvLiveLumi/RefAvLiveLumi/(HeadUnprescaledRates[HeadName][1]), 2 )
459 amott 1.1
460     if ScaledRefRate == 0:
461     PerDiff = 100
462     else:
463     PerDiff = int( round( (TriggerRate - ScaledRefRate)/ScaledRefRate , 2)*100)
464    
465     if TriggerRate < IgnoreThreshold and ScaledRefRate < IgnoreThreshold:
466     continue
467    
468     if abs(PerDiff) > AllowedRateDiff:
469     Warn.append(True)
470     else:
471     Warn.append(False)
472     VC = ""
473 amott 1.15 Data.append([HeadName,TriggerRate,ScaledRefRate,PerDiff,round((HeadUnprescaledRates[HeadName][1]),1),VC])
474 amott 1.1
475 grchrist 1.18
476 amott 1.1 PrettyPrintTable(Header,Data,[80,10,10,10,10,20],Warn)
477    
478 amott 1.4 MoreTableInfo(HeadParser,HeadLumiRange,Config)
479 amott 1.1
480     def CheckTriggerList(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config):
481     print "checking trigger list"
482    
483     def CheckL1Zeros(HeadParser,RefRunNum,RefRates,RefLumis,LastSuccessfulIterator,ShowPSTriggers,AllowedRateDiff,IgnoreThreshold,Config):
484     L1Zeros=[]
485     IgnoreBits = ["L1_PreCollisions","L1_InterBunch_Bsc","L1_BeamHalo","L1_BeamGas_Hf"]
486     for key in HeadParser.TriggerRates:
487     ## Skip events in the skip list
488     skipTrig=False
489     ##for trig in Config.ExcludeList:
490     ##if not trigN.find(trig) == -1:
491     ##skipTrig=True
492     ##break
493     if skipTrig:
494     continue
495     ## if no events pass the L1, add it to the L1Zeros list if not already there
496     if HeadParser.TriggerRates[key][1]==0 and not HeadParser.TriggerRates[key][4] in L1Zeros:
497     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:
498    
499     L1Zeros.append(HeadParser.TriggerRates[key][4])
500     print "L1Zeros=", L1Zeros
501    
502     if len(L1Zeros) == 0:
503     #print "It looks like no masked L1 bits seed trigger paths"
504     pass
505     else:
506     print "The following seeds are used to seed HLT bits but accept 0 events:"
507     #print "The average lumi of this run is: "+str(round(HeadParser.LumiInfo[6],1))+"e30"
508     for Seed in L1Zeros:
509     print Seed
510    
511     if __name__=='__main__':
512 grchrist 1.20 global thisyear
513 amott 1.1 main()