ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/DatabaseParser.py
Revision: 1.31
Committed: Wed Apr 4 08:57:55 2012 UTC (13 years, 1 month ago) by grchrist
Content type: text/x-python
Branch: MAIN
Changes since 1.30: +48 -7 lines
Log Message:
put an exception message in GetLatestRunNumber where there is no trigger key

File Contents

# User Rev Content
1 amott 1.1 import cx_Oracle
2     import cPickle as pickle
3     import os, sys
4     import time
5 amott 1.5 import re
6 amott 1.20
7     try: ## set is builtin in python 2.6.4 and sets is deprecated
8     set
9     except NameError:
10     from sets import Set
11 amott 1.5
12 amott 1.1
13     class DatabaseParser:
14    
15     def __init__(self):
16 amott 1.24 self.curs = ConnectDB()
17 amott 1.1 ##-- Defined in ParsePage1 --##
18     self.RunNumber = 0
19    
20     ##-- Defined in ParseRunPage --##
21     self.Date=''
22     self.L1_HLT_Key=''
23     self.HLT_Key=''
24     self.GTRS_Key=''
25     self.TSC_Key=''
26     self.GT_Key=''
27     self.ConfigId=0
28    
29     ##-- Defined in ParseHLTSummaryPage --##
30 amott 1.5 self.HLTRatesByLS = {}
31     self.HLTPSByLS = {}
32 amott 1.1
33 amott 1.4 self.nAlgoBits=0
34 amott 1.1 self.L1PrescaleTable=[]
35     self.AvgL1Prescales=[] ## contains the average L1 prescales for the current LS range range
36     self.HLTList=[]
37 amott 1.4 self.AvgTotalPrescales={}
38 amott 1.1 self.HLTPrescaleTable=[] ## can't fill this yet
39 amott 1.4 self.UnprescaledRates={}
40     self.PrescaledRates={}
41 amott 1.1 ##-- Defined in ParseLumiPage --##
42     self.LastLSParsed=-1
43 amott 1.4 self.InstLumiByLS = {}
44     self.DeliveredLumiByLS = {}
45     self.LiveLumiByLS = {}
46     self.PSColumnByLS = {}
47 amott 1.1 self.AvInstLumi = 0
48     self.AvDeliveredLumi = 0
49     self.AvLiveLumi = 0
50 amott 1.5 self.AvDeadTime = 0
51 amott 1.4 self.LumiInfo = {} ##Returns
52     self.DeadTime = {}
53     self.Physics = {}
54 amott 1.5 self.Active = {}
55 grchrist 1.21
56 grchrist 1.22
57     self.B1Pres = {}
58     self.B2Pres = {}
59     self.B1Stab = {}
60     self.B2Stab = {}
61 grchrist 1.21 self.EBP = {}
62     self.EBM = {}
63     self.EEP = {}
64     self.EEM = {}
65     self.HBHEA = {}
66     self.HBHEB = {}
67     self.HBHEC = {}
68     self.HF = {}
69     self.RPC = {}
70     self.DT0 = {}
71     self.DTP = {}
72     self.DTM = {}
73     self.CSCP = {}
74     self.CSCM = {}
75     self.TOB = {}
76     self.TIBTID= {}
77     self.TECP = {}
78     self.TECM = {}
79     self.BPIX = {}
80     self.FPIX = {}
81     self.ESP = {}
82     self.ESM = {}
83 grchrist 1.26
84     self.DeadTimeBeamActive = {}
85 grchrist 1.21
86 amott 1.5
87 amott 1.1 ##-- Defined in ParsePSColumnPage (not currently used) --##
88     self.PSColumnChanges=[] ##Returns
89    
90     ##-- Defined in ParseTriggerModePage --##
91 amott 1.2 self.L1TriggerMode={} ##
92     self.HLTTriggerMode={} ##
93 amott 1.4 self.HLTSeed={}
94 amott 1.1 self.HLTSequenceMap=[]
95     self.TriggerInfo = [] ##Returns
96    
97     ##-- Defined in AssemblePrescaleValues --##
98 amott 1.4 self.L1Prescale={}
99     self.L1IndexNameMap={}
100 amott 1.1 self.HLTPrescale=[]
101     self.MissingPrescale=[]
102     self.PrescaleValues=[] ##Returns
103    
104     ##-- Defined in ComputeTotalPrescales --##
105 amott 1.2 self.TotalPSInfo = [] ##Returns # #collection
106 amott 1.1
107     ##-- Defined in CorrectForPrescaleChange --##
108     self.CorrectedPSInfo = [] ##Returns
109    
110     ##-- In the current Parser.py philosophy, only RunNumber is set globally
111     ## - LS range is set from the outside for each individual function
112     #self.FirstLS = -1
113     #self.LastLS = -1
114    
115     def GetRunInfo(self):
116     ## This query gets the L1_HLT Key (A), the associated HLT Key (B) and the Config number for that key (C)
117     KeyQuery = """
118     SELECT A.TRIGGERMODE, B.HLT_KEY, B.GT_RS_KEY, B.TSC_KEY, C.CONFIGID, D.GT_KEY FROM
119     CMS_WBM.RUNSUMMARY A, CMS_L1_HLT.L1_HLT_CONF B, CMS_HLT.CONFIGURATIONS C, CMS_TRG_L1_CONF.TRIGGERSUP_CONF D WHERE
120     B.ID = A.TRIGGERMODE AND C.CONFIGDESCRIPTOR = B.HLT_KEY AND D.TS_Key = B.TSC_Key AND A.RUNNUMBER=%d
121     """ % (self.RunNumber,)
122 grchrist 1.31 try:
123     self.curs.execute(KeyQuery)
124     self.L1_HLT_Key,self.HLT_Key,self.GTRS_Key,self.TSC_Key,self.ConfigId,self.GT_Key = self.curs.fetchone()
125     except:
126     print "Unable to get L1 and HLT keys for this run"
127    
128 amott 1.1
129     def UpdateRateTable(self): # lets not rebuild the rate table every time, rather just append new LSs
130     pass
131    
132 amott 1.5 def GetHLTRates(self,LSRange):
133 abrinke1 1.17
134 amott 1.1 sqlquery = """SELECT SUM(A.L1PASS),SUM(A.PSPASS),SUM(A.PACCEPT)
135     ,SUM(A.PEXCEPT),(SELECT L.NAME FROM CMS_HLT.PATHS L WHERE L.PATHID=A.PATHID) PATHNAME
136 abrinke1 1.17 FROM CMS_RUNINFO.HLT_SUPERVISOR_TRIGGERPATHS A WHERE RUNNUMBER=%s AND A.LSNUMBER IN %s
137 amott 1.1 GROUP BY A.LSNUMBER,A.PATHID"""
138    
139 abrinke1 1.17 LSRangeSTR = str(LSRange)
140     LSRangeSTR = LSRangeSTR.replace("[","(")
141     LSRangeSTR = LSRangeSTR.replace("]",")")
142    
143     StartLS = LSRange[0]
144     EndLS = LSRange[-1]
145 amott 1.6
146 abrinke1 1.10 AvgL1Prescales = [0]*self.nAlgoBits
147    
148 amott 1.5 #print "Getting HLT Rates for LS from %d to %d" % (LSRange[0],LSRange[-1],)
149 abrinke1 1.17
150     query = sqlquery % (self.RunNumber,LSRangeSTR)
151 amott 1.1 self.curs.execute(query)
152 amott 1.5
153     TriggerRates = {}
154 amott 1.1 for L1Pass,PSPass,HLTPass,HLTExcept,name in self.curs.fetchall():
155 abrinke1 1.10 if not self.HLTSeed.has_key(name):
156     continue
157    
158 amott 1.1 rate = HLTPass/23.3
159 abrinke1 1.10 hltps = 0
160 amott 1.1 if PSPass:
161 abrinke1 1.10 hltps = float(L1Pass)/PSPass
162 amott 1.6 if not TriggerRates.has_key(name):
163 abrinke1 1.10 try:
164     psi = self.PSColumnByLS[LSRange[0]]
165 grchrist 1.25 ##print "psi=",psi
166 abrinke1 1.10 #except:
167     #psi = self.PSColumnByLS[1]
168     #if not psi:
169     except:
170 grchrist 1.22 print "HLT in: Cannot figure out PSI for LS "+str(StartLS)+" setting to 0"
171 abrinke1 1.10 print "The value of LSRange[0] is:"
172     print str(LSRange[0])
173     psi = 0
174    
175     if self.L1IndexNameMap.has_key(self.HLTSeed[name]):
176     l1ps = self.L1PrescaleTable[self.L1IndexNameMap[self.HLTSeed[name]]][psi]
177     else:
178 grchrist 1.25 ##print "zero ",LSRange[0], self.PSColumnByLS[LSRange[0]]
179 abrinke1 1.10 AvL1Prescales = self.CalculateAvL1Prescales([LSRange[0]])
180     l1ps = self.UnwindORSeed(self.HLTSeed[name],AvL1Prescales)
181     #l1ps = 1
182     #print "L1IndexNameMap has no key for "+str(self.HLTSeed[name])
183    
184     ps = l1ps*hltps
185 grchrist 1.22
186     ###if ps < 1: ### want PS=0 too!
187 abrinke1 1.10 #print "Oops! somehow ps for "+str(name)+" = "+str(ps)+", where L1 PS = "+str(l1ps)+" and HLT PS = "+str(hltps)
188 grchrist 1.22 # ps = 1
189 abrinke1 1.10 psrate = ps*rate
190     TriggerRates[name]= [ps,rate,psrate,1]
191 amott 1.6 else:
192 abrinke1 1.10 [ops,orate,opsrate,on] = TriggerRates[name]
193     try:
194     psi = self.PSColumnByLS[LSRange[on]]
195     #except:
196     #psi = self.PSColumnByLS[1]
197     #if not psi:
198     except:
199 grchrist 1.22 print "HLT out: Cannot figure out PSI for index "+str(on)+" setting to 0"
200 abrinke1 1.10 print "The value of LSRange[on] is:"
201     print str(LSRange[on])
202     psi = 0
203    
204     if self.L1IndexNameMap.has_key(self.HLTSeed[name]):
205     l1ps = self.L1PrescaleTable[self.L1IndexNameMap[self.HLTSeed[name]]][psi]
206     else:
207 grchrist 1.25 ##print "on ",
208 abrinke1 1.10 AvL1Prescales = self.CalculateAvL1Prescales([LSRange[on]])
209     l1ps = self.UnwindORSeed(self.HLTSeed[name],AvL1Prescales)
210     #l1ps = 1
211     #print "L1IndexNameMap has no key for "+str(self.HLTSeed[name])
212    
213     ps = l1ps*hltps
214 grchrist 1.22 #if ps < 1: ###want PS=0 too!
215 abrinke1 1.10 ##print "Oops! somehow ps for "+str(name)+" = "+str(ps)+", where L1 PS = "+str(l1ps)+" and HLT PS = "+str(hltps)
216 grchrist 1.22 # ps = 1
217 abrinke1 1.10 psrate = ps*rate
218     TriggerRates[name]= [ops+ps,orate+rate,opsrate+psrate,on+1]
219 grchrist 1.22
220 amott 1.6 for name,val in TriggerRates.iteritems():
221 abrinke1 1.10 [ps,rate,psrate,n] = val
222     avps = ps/n
223     try:
224     ps = psrate/rate
225     except:
226     #print "Rate = 0 for "+str(name)+", setting ps to 1"
227 abrinke1 1.13 ps = avps
228 abrinke1 1.10 TriggerRates[name] = [avps,ps,rate/n,psrate/n]
229 grchrist 1.22
230    
231    
232 amott 1.5 return TriggerRates
233 amott 1.1
234 amott 1.5 def GetTriggerRatesByLS(self,triggerName):
235     sqlquery = """SELECT A.LSNUMBER, A.PACCEPT
236 amott 1.3 FROM CMS_RUNINFO.HLT_SUPERVISOR_TRIGGERPATHS A, CMS_HLT.PATHS B
237 amott 1.5 WHERE RUNNUMBER=%s AND B.NAME = \'%s\' AND A.PATHID = B.PATHID
238 amott 1.11 """ % (self.RunNumber,triggerName,)
239 amott 1.3
240     self.curs.execute(sqlquery)
241 amott 1.5 r={}
242     for ls,accept in self.curs.fetchall():
243     r[ls] = accept/23.3
244     return r
245    
246     def GetAllTriggerRatesByLS(self):
247     for hltName in self.HLTSeed:
248     self.HLTRatesByLS[hltName] = self.GetTriggerRatesByLS(hltName)
249 amott 1.3
250 amott 1.1 def GetLumiInfo(self):
251 amott 1.5 sqlquery="""SELECT RUNNUMBER,LUMISECTION,PRESCALE_INDEX,INSTLUMI,LIVELUMI,DELIVLUMI,DEADTIME
252 grchrist 1.22 ,DCSSTATUS,PHYSICS_FLAG,CMS_ACTIVE
253 amott 1.1 FROM CMS_RUNTIME_LOGGER.LUMI_SECTIONS A,CMS_GT_MON.LUMI_SECTIONS B WHERE A.RUNNUMBER=%s
254     AND B.RUN_NUMBER(+)=A.RUNNUMBER AND B.LUMI_SECTION(+)=A.LUMISECTION AND A.LUMISECTION > %d
255     ORDER BY A.RUNNUMBER,A.LUMISECTION"""
256 abrinke1 1.13
257 amott 1.1 ## Get the lumi information for the run, just update the table, don't rebuild it every time
258     query = sqlquery % (self.RunNumber,self.LastLSParsed)
259     self.curs.execute(query)
260 grchrist 1.21
261 amott 1.1 pastLSCol=-1
262 grchrist 1.22 for run,ls,psi,inst,live,dlive,dt,dcs,phys,active in self.curs.fetchall():
263 amott 1.4 self.PSColumnByLS[ls]=psi
264     self.InstLumiByLS[ls]=inst
265     self.LiveLumiByLS[ls]=live
266     self.DeliveredLumiByLS[ls]=dlive
267     self.DeadTime[ls]=dt
268     self.Physics[ls]=phys
269 amott 1.5 self.Active[ls]=active
270 grchrist 1.22
271 amott 1.1 if pastLSCol!=-1 and ls!=pastLSCol:
272     self.PSColumnChanges.append([ls,psi])
273     pastLSCol=ls
274     if ls>self.LastLSParsed:
275     self.LastLSParsed=ls
276 grchrist 1.22 self.LumiInfo = [self.PSColumnByLS, self.InstLumiByLS, self.DeliveredLumiByLS, self.LiveLumiByLS, self.DeadTime, self.Physics, self.Active]
277 amott 1.1
278 abrinke1 1.13 return self.LumiInfo
279    
280 grchrist 1.19 def GetMoreLumiInfo(self):
281 grchrist 1.22 sqlquery="""SELECT RUNNUMBER,LUMISECTION,BEAM1_PRESENT, BEAM2_PRESENT, BEAM1_STABLE, BEAM2_STABLE, EBP_READY,EBM_READY,EEP_READY,EEM_READY,HBHEA_READY,HBHEB_READY,HBHEC_READY,HF_READY,RPC_READY,DT0_READY,DTP_READY,DTM_READY,CSCP_READY,CSCM_READY,TOB_READY,TIBTID_READY,TECP_READY,TECM_READY,BPIX_READY,FPIX_READY,ESP_READY,ESM_READY
282 grchrist 1.19 FROM CMS_RUNTIME_LOGGER.LUMI_SECTIONS A,CMS_GT_MON.LUMI_SECTIONS B WHERE A.RUNNUMBER=%s
283     AND B.RUN_NUMBER(+)=A.RUNNUMBER AND B.LUMI_SECTION(+)=A.LUMISECTION AND A.LUMISECTION > %d
284     ORDER BY A.RUNNUMBER,A.LUMISECTION"""
285    
286     ## Get the lumi information for the run, just update the table, don't rebuild it every time
287     query = sqlquery % (self.RunNumber,self.LastLSParsed)
288     self.curs.execute(query)
289 grchrist 1.21 pastLSCol=-1
290 grchrist 1.22 for run,ls,b1pres,b2pres,b1stab,b2stab,ebp,ebm,eep,eem,hbhea,hbheb,hbhec,hf,rpc,dt0,dtp,dtm,cscp,cscm,tob,tibtid,tecp,tecm,bpix,fpix,esp,esm in self.curs.fetchall():
291 grchrist 1.21
292 grchrist 1.22 self.B1Pres[ls]=b1pres
293     self.B2Pres[ls]=b2pres
294     self.B1Stab[ls]=b1stab
295     self.B2Stab[ls]=b2stab
296 grchrist 1.21 self.EBP[ls]= ebp
297     self.EBM[ls] = ebm
298     self.EEP[ls] = eep
299     self.EEM[ls] = eem
300     self.HBHEA[ls] = hbhea
301     self.HBHEB[ls] = hbheb
302     self.HBHEC[ls] = hbhec
303     self.HF[ls] = hf
304     self.RPC[ls] = rpc
305     self.DT0[ls] = dt0
306     self.DTP[ls] = dtp
307     self.DTM[ls] = dtm
308     self.CSCP[ls] = cscp
309     self.CSCM[ls] = cscm
310     self.TOB[ls] = tob
311     self.TIBTID[ls]= tibtid
312     self.TECP[ls] = tecp
313     self.TECM[ls] = tecm
314     self.BPIX[ls] = bpix
315     self.FPIX[ls] = fpix
316     self.ESP[ls] = esp
317     self.ESM[ls] = esm
318 grchrist 1.19
319 grchrist 1.21
320 grchrist 1.19 pastLSCol=ls
321     if ls>self.LastLSParsed:
322     self.LastLSParsed=ls
323    
324 grchrist 1.27
325 grchrist 1.22 self.MoreLumiInfo ={'b1pres':self.B1Pres,'b2pres':self.B2Pres,'b1stab':self.B1Stab,'b2stab':self.B2Stab,'ebp':self.EBP,'ebm':self.EBM,'eep':self.EEP,'eem':self.EEM,'hbhea':self.HBHEA,'hbheb':self.HBHEB,'hbhec':self.HBHEC,'hf':self.HF,'rpc':self.RPC,'dt0':self.DT0,'dtp':self.DTP,'dtm':self.DTM,'cscp':self.CSCP,'cscm':self.CSCM,'tob':self.TOB,'tibtid':self.TIBTID,'tecp':self.TECP,'tecm':self.TECM,'bpix':self.BPIX,'fpix':self.FPIX,'esp':self.ESP,'esm':self.ESM}
326 grchrist 1.21
327 grchrist 1.19 return self.MoreLumiInfo
328    
329 grchrist 1.26 def GetDeadTimeBeamActive(self,LSRange):
330     sqlquery=""" select FRACTION
331     from
332     CMS_GT_MON.V_SCALERS_TCS_DEADTIME
333     where
334 grchrist 1.27 RUN_NUMBER=%s and
335     LUMI_SECTION in %s and
336     SCALER_NAME='DeadtimeBeamActive'"""
337    
338 grchrist 1.26
339 grchrist 1.27 LSRangeSTR = str(LSRange)
340     LSRangeSTR = LSRangeSTR.replace("[","(")
341     LSRangeSTR = LSRangeSTR.replace("]",")")
342    
343     query=sqlquery %(self.RunNumber,LSRangeSTR)
344     #print query
345     self.curs.execute(query)
346 grchrist 1.26
347 grchrist 1.27 deadtimeba_sum=0
348     ii=0
349     for deadtimebeamactive in self.curs.fetchall():
350     try:
351     deadtimeba_sum=deadtimeba_sum+deadtimebeamactive[0]
352     except:
353     print "no dtba for run ",self.RunNumber, ", ls ",LSRange[ii], "using dt"
354     deadtimeba_sum=deadtimeba_sum+self.GetDeadTime(LSRange[ii])
355     ii=ii+1
356     deadtimeba_av=deadtimeba_sum/len(LSRange)
357    
358     return deadtimeba_av
359    
360    
361    
362     def GetDeadTime(self,LS):
363     sqlquery=""" select FRACTION
364     from
365     CMS_GT_MON.V_SCALERS_TCS_DEADTIME
366     where
367     RUN_NUMBER=%s and
368     LUMI_SECTION=%s and
369     SCALER_NAME='Deadtime'"""
370    
371     query=sqlquery %(self.RunNumber,LS)
372     #print query
373 grchrist 1.26 self.curs.execute(query)
374    
375 grchrist 1.27 for deadtime in self.curs.fetchall():
376     try:
377     dt=deadtime[0]
378     print "dt=",dt
379     except:
380     print "no dt for run ",self.RunNumber, ", ls ",LS
381     dt=1.0
382    
383    
384     return dt
385 grchrist 1.19
386 amott 1.5 def GetAvLumiInfo(self,LSRange):
387     nLS=0;
388     AvInstLumi=0
389 amott 1.6 try:
390     StartLS = LSRange[0]
391     EndLS = LSRange[-1]
392 grchrist 1.26
393     #print "startls=",StartLS, "endls=",EndLS
394 amott 1.23 try: ## Cosmics won't have lumi info
395     maxlive = self.LiveLumiByLS[EndLS]
396     maxdelivered = self.DeliveredLumiByLS[EndLS]
397     for iterator in LSRange:
398     if self.LiveLumiByLS[iterator] > maxlive:
399     maxlive = self.LiveLumiByLS[iterator]
400     if self.DeliveredLumiByLS[iterator] > maxdelivered:
401     maxdelivered = self.DeliveredLumiByLS[iterator]
402     AvLiveLumi=maxlive-self.LiveLumiByLS[StartLS]
403     AvDeliveredLumi=maxdelivered-self.DeliveredLumiByLS[StartLS]
404     except:
405     AvLiveLumi=0
406     AvDeliveredLumi=0
407 abrinke1 1.15
408 abrinke1 1.14 if AvDeliveredLumi > 0:
409     AvDeadTime = 1 - AvLiveLumi/AvDeliveredLumi
410     else:
411     if AvLiveLumi > 0:
412     print "Live Lumi > 0 but Delivered <= 0: problem"
413     AvDeadTime = 0.0
414 abrinke1 1.17 PSCols=[]
415 amott 1.6 for ls in LSRange:
416     try:
417 amott 1.23 try:
418     AvInstLumi+=self.InstLumiByLS[ls]
419     except:
420     pass
421 abrinke1 1.17 PSCols.append(self.PSColumnByLS[ls])
422 amott 1.6 nLS+=1
423     except:
424     print "ERROR: Lumi section "+str(ls)+" not in bounds"
425     return [0.,0.,0.,0.,[]]
426 abrinke1 1.18 return [AvInstLumi/nLS,(1000.0/23.3)*AvLiveLumi/(EndLS-StartLS),(1000.0/23.3)*AvDeliveredLumi/(EndLS-StartLS), AvDeadTime,PSCols]
427 amott 1.6 except:
428 grchrist 1.26 if LSRange[0] == LSRange[-1]:
429 abrinke1 1.13 AvInstLumi = self.InstLumiByLS[StartLS]
430     try:
431     AvLiveLumi = self.LiveLumiByLS[StartLS]-self.LiveLumiByLS[StartLS-1]
432     AvDeliveredLumi = self.DeliveredLumiByLS[StartLS]-self.DeliveredLumiByLS[StartLS-1]
433 grchrist 1.26
434 abrinke1 1.13 except:
435 grchrist 1.27 try:
436     AvLiveLumi = self.LiveLumiByLS[StartLS+1]-self.LiveLumiByLS[StartLS]
437     AvDeliveredLumi = self.DeliveredLumiByLS[StartLS+1]-self.DeliveredLumiByLS[StartLS]
438     except:
439     print "missing live/delivered run ",self.RunNumber, "ls ",LSRange
440     AvLiveLumi = 0
441     AvDeliveredLumi = 0
442 abrinke1 1.14 if AvDeliveredLumi > 0:
443     AvDeadTime = 1 - AvLiveLumi/AvDeliveredLumi
444 grchrist 1.26
445 grchrist 1.27 elif AvLiveLumi > 0:
446     print "Live Lumi > 0 but Delivered <= 0: problem run ",self.RunNumber, " ls ",LSRange
447     AvDeadTime = 1.0
448 abrinke1 1.14 else:
449 grchrist 1.27 AvDeadTime=1.0
450 abrinke1 1.13 PSCols = [self.PSColumnByLS[StartLS]]
451 abrinke1 1.18 return [AvInstLumi,(1000.0/23.3)*AvLiveLumi,(1000.0/23.3)*AvDeliveredLumi,AvDeadTime,PSCols]
452 abrinke1 1.13 else:
453 abrinke1 1.18 return [0.,0.,0.,0.,[]]
454 amott 1.6
455 amott 1.1 def ParsePSColumnPage(self): ## this is now done automatically when we read the db
456     pass
457    
458     def GetL1NameIndexAssoc(self):
459     ## get the L1 algo names associated with each algo bit
460     AlgoNameQuery = """SELECT ALGO_INDEX, ALIAS FROM CMS_GT.L1T_MENU_ALGO_VIEW
461     WHERE MENU_IMPLEMENTATION IN (SELECT L1T_MENU_FK FROM CMS_GT.GT_SETUP WHERE ID='%s')
462     ORDER BY ALGO_INDEX""" % (self.GT_Key,)
463     self.curs.execute(AlgoNameQuery)
464     for index,name in self.curs.fetchall():
465 amott 1.4 self.L1IndexNameMap[name] = index
466 amott 1.1
467     def GetL1AlgoPrescales(self):
468     L1PrescalesQuery= """
469     SELECT
470     PRESCALE_FACTOR_ALGO_000,PRESCALE_FACTOR_ALGO_001,PRESCALE_FACTOR_ALGO_002,PRESCALE_FACTOR_ALGO_003,PRESCALE_FACTOR_ALGO_004,PRESCALE_FACTOR_ALGO_005,
471     PRESCALE_FACTOR_ALGO_006,PRESCALE_FACTOR_ALGO_007,PRESCALE_FACTOR_ALGO_008,PRESCALE_FACTOR_ALGO_009,PRESCALE_FACTOR_ALGO_010,PRESCALE_FACTOR_ALGO_011,
472     PRESCALE_FACTOR_ALGO_012,PRESCALE_FACTOR_ALGO_013,PRESCALE_FACTOR_ALGO_014,PRESCALE_FACTOR_ALGO_015,PRESCALE_FACTOR_ALGO_016,PRESCALE_FACTOR_ALGO_017,
473     PRESCALE_FACTOR_ALGO_018,PRESCALE_FACTOR_ALGO_019,PRESCALE_FACTOR_ALGO_020,PRESCALE_FACTOR_ALGO_021,PRESCALE_FACTOR_ALGO_022,PRESCALE_FACTOR_ALGO_023,
474     PRESCALE_FACTOR_ALGO_024,PRESCALE_FACTOR_ALGO_025,PRESCALE_FACTOR_ALGO_026,PRESCALE_FACTOR_ALGO_027,PRESCALE_FACTOR_ALGO_028,PRESCALE_FACTOR_ALGO_029,
475     PRESCALE_FACTOR_ALGO_030,PRESCALE_FACTOR_ALGO_031,PRESCALE_FACTOR_ALGO_032,PRESCALE_FACTOR_ALGO_033,PRESCALE_FACTOR_ALGO_034,PRESCALE_FACTOR_ALGO_035,
476     PRESCALE_FACTOR_ALGO_036,PRESCALE_FACTOR_ALGO_037,PRESCALE_FACTOR_ALGO_038,PRESCALE_FACTOR_ALGO_039,PRESCALE_FACTOR_ALGO_040,PRESCALE_FACTOR_ALGO_041,
477     PRESCALE_FACTOR_ALGO_042,PRESCALE_FACTOR_ALGO_043,PRESCALE_FACTOR_ALGO_044,PRESCALE_FACTOR_ALGO_045,PRESCALE_FACTOR_ALGO_046,PRESCALE_FACTOR_ALGO_047,
478     PRESCALE_FACTOR_ALGO_048,PRESCALE_FACTOR_ALGO_049,PRESCALE_FACTOR_ALGO_050,PRESCALE_FACTOR_ALGO_051,PRESCALE_FACTOR_ALGO_052,PRESCALE_FACTOR_ALGO_053,
479     PRESCALE_FACTOR_ALGO_054,PRESCALE_FACTOR_ALGO_055,PRESCALE_FACTOR_ALGO_056,PRESCALE_FACTOR_ALGO_057,PRESCALE_FACTOR_ALGO_058,PRESCALE_FACTOR_ALGO_059,
480     PRESCALE_FACTOR_ALGO_060,PRESCALE_FACTOR_ALGO_061,PRESCALE_FACTOR_ALGO_062,PRESCALE_FACTOR_ALGO_063,PRESCALE_FACTOR_ALGO_064,PRESCALE_FACTOR_ALGO_065,
481     PRESCALE_FACTOR_ALGO_066,PRESCALE_FACTOR_ALGO_067,PRESCALE_FACTOR_ALGO_068,PRESCALE_FACTOR_ALGO_069,PRESCALE_FACTOR_ALGO_070,PRESCALE_FACTOR_ALGO_071,
482     PRESCALE_FACTOR_ALGO_072,PRESCALE_FACTOR_ALGO_073,PRESCALE_FACTOR_ALGO_074,PRESCALE_FACTOR_ALGO_075,PRESCALE_FACTOR_ALGO_076,PRESCALE_FACTOR_ALGO_077,
483     PRESCALE_FACTOR_ALGO_078,PRESCALE_FACTOR_ALGO_079,PRESCALE_FACTOR_ALGO_080,PRESCALE_FACTOR_ALGO_081,PRESCALE_FACTOR_ALGO_082,PRESCALE_FACTOR_ALGO_083,
484     PRESCALE_FACTOR_ALGO_084,PRESCALE_FACTOR_ALGO_085,PRESCALE_FACTOR_ALGO_086,PRESCALE_FACTOR_ALGO_087,PRESCALE_FACTOR_ALGO_088,PRESCALE_FACTOR_ALGO_089,
485     PRESCALE_FACTOR_ALGO_090,PRESCALE_FACTOR_ALGO_091,PRESCALE_FACTOR_ALGO_092,PRESCALE_FACTOR_ALGO_093,PRESCALE_FACTOR_ALGO_094,PRESCALE_FACTOR_ALGO_095,
486     PRESCALE_FACTOR_ALGO_096,PRESCALE_FACTOR_ALGO_097,PRESCALE_FACTOR_ALGO_098,PRESCALE_FACTOR_ALGO_099,PRESCALE_FACTOR_ALGO_100,PRESCALE_FACTOR_ALGO_101,
487     PRESCALE_FACTOR_ALGO_102,PRESCALE_FACTOR_ALGO_103,PRESCALE_FACTOR_ALGO_104,PRESCALE_FACTOR_ALGO_105,PRESCALE_FACTOR_ALGO_106,PRESCALE_FACTOR_ALGO_107,
488     PRESCALE_FACTOR_ALGO_108,PRESCALE_FACTOR_ALGO_109,PRESCALE_FACTOR_ALGO_110,PRESCALE_FACTOR_ALGO_111,PRESCALE_FACTOR_ALGO_112,PRESCALE_FACTOR_ALGO_113,
489     PRESCALE_FACTOR_ALGO_114,PRESCALE_FACTOR_ALGO_115,PRESCALE_FACTOR_ALGO_116,PRESCALE_FACTOR_ALGO_117,PRESCALE_FACTOR_ALGO_118,PRESCALE_FACTOR_ALGO_119,
490     PRESCALE_FACTOR_ALGO_120,PRESCALE_FACTOR_ALGO_121,PRESCALE_FACTOR_ALGO_122,PRESCALE_FACTOR_ALGO_123,PRESCALE_FACTOR_ALGO_124,PRESCALE_FACTOR_ALGO_125,
491     PRESCALE_FACTOR_ALGO_126,PRESCALE_FACTOR_ALGO_127
492     FROM CMS_GT.GT_FDL_PRESCALE_FACTORS_ALGO A, CMS_GT.GT_RUN_SETTINGS_PRESC_VIEW B
493     WHERE A.ID=B.PRESCALE_FACTORS_ALGO_FK AND B.ID='%s'
494     """ % (self.GTRS_Key,)
495     self.curs.execute(L1PrescalesQuery)
496     ## This is pretty horrible, but this how you get them!!
497     tmp = self.curs.fetchall()
498     for ps in tmp[0]: #build the prescale table initially
499     self.L1PrescaleTable.append([ps])
500     for line in tmp[1:]: # now fill it
501     for ps,index in zip(line,range(len(line))):
502     self.L1PrescaleTable[index].append(ps)
503 amott 1.4 self.nAlgoBits=128
504 amott 1.1
505     def GetHLTIndex(self,name):
506     for i,n in enumerate(self.HLTList):
507     if n.find(name)!=-1:
508     return i
509 amott 1.2 #print name
510 amott 1.1 return -1
511    
512     def GetHLTPrescaleMatrix(self,cursor):
513     ##NOT WORKING 1/19/2012
514     return
515     SequencePathQuery ="""
516     SELECT F.SEQUENCENB,J.VALUE TRIGGERNAME
517     FROM CMS_HLT.CONFIGURATIONSERVICEASSOC A
518     , CMS_HLT.SERVICES B
519     , CMS_HLT.SERVICETEMPLATES C
520     , CMS_HLT.SUPERIDVECPARAMSETASSOC D
521     , CMS_HLT.VECPARAMETERSETS E
522     , CMS_HLT.SUPERIDPARAMSETASSOC F
523     , CMS_HLT.PARAMETERSETS G
524     , CMS_HLT.SUPERIDPARAMETERASSOC H
525     , CMS_HLT.PARAMETERS I
526     , CMS_HLT.STRINGPARAMVALUES J
527     WHERE A.CONFIGID= %d
528     AND A.SERVICEID=B.SUPERID
529     AND B.TEMPLATEID=C.SUPERID
530     AND C.NAME='PrescaleService'
531     AND B.SUPERID=D.SUPERID
532     AND D.VPSETID=E.SUPERID
533     AND E.NAME='prescaleTable'
534     AND D.VPSETID=F.SUPERID
535     AND F.PSETID=G.SUPERID
536     AND G.SUPERID=H.SUPERID
537     AND I.PARAMID=H.PARAMID
538     AND I.NAME='pathName'
539     AND J.PARAMID=H.PARAMID
540     ORDER BY F.SEQUENCENB
541     """ % (self.ConfigId,)
542    
543     cursor.execute(SequencePathQuery)
544     self.HLTSequenceMap = [0]*len(self.HLTList)
545     for seq,name in cursor.fetchall():
546     name = name.lstrip('"').rstrip('"')
547     try:
548     self.HLTSequenceMap[self.GetHLTIndex(name)]=seq
549     except:
550     print "couldn't find "+name
551    
552     for i,seq in enumerate(self.HLTSequenceMap):
553     if seq==0:
554     print self.HLTList[i]
555    
556     SequencePrescaleQuery="""
557     SELECT F.SEQUENCENB,J.SEQUENCENB,J.VALUE
558     FROM CMS_HLT.CONFIGURATIONSERVICEASSOC A
559     , CMS_HLT.SERVICES B
560     , CMS_HLT.SERVICETEMPLATES C
561     , CMS_HLT.SUPERIDVECPARAMSETASSOC D
562     , CMS_HLT.VECPARAMETERSETS E
563     , CMS_HLT.SUPERIDPARAMSETASSOC F
564     , CMS_HLT.PARAMETERSETS G
565     , CMS_HLT.SUPERIDPARAMETERASSOC H
566     , CMS_HLT.PARAMETERS I
567     , CMS_HLT.VUINT32PARAMVALUES J
568     WHERE A.CONFIGID=%d
569     AND A.SERVICEID=B.SUPERID
570     AND B.TEMPLATEID=C.SUPERID
571     AND C.NAME='PrescaleService'
572     AND B.SUPERID=D.SUPERID
573     AND D.VPSETID=E.SUPERID
574     AND E.NAME='prescaleTable'
575     AND D.VPSETID=F.SUPERID
576     AND F.PSETID=G.SUPERID
577     AND G.SUPERID=H.SUPERID
578     AND I.PARAMID=H.PARAMID
579     AND I.NAME='prescales'
580     AND J.PARAMID=H.PARAMID
581     ORDER BY F.SEQUENCENB,J.SEQUENCENB
582     """ % (self.ConfigId,)
583    
584 amott 1.2 #print self.HLTSequenceMap
585 amott 1.1 cursor.execute(SequencePrescaleQuery)
586     self.HLTPrescaleTable=[ [] ]*len(self.HLTList)
587     lastIndex=-1
588     lastSeq=-1
589     row = []
590     for seq,index,val in cursor.fetchall():
591     if lastIndex!=index-1:
592     self.HLTPrescaleTable[self.HLTSequenceMap.index(lastSeq)].append(row)
593     row=[]
594     lastSeq=seq
595     lastIndex=index
596     row.append(val)
597    
598     def GetHLTSeeds(self):
599     ## This is a rather delicate query, but it works!
600     ## Essentially get a list of paths associated with the config, then find the module of type HLTLevel1GTSeed associated with the path
601     ## Then find the parameter with field name L1SeedsLogicalExpression and look at the value
602     ##
603     ## NEED TO BE LOGGED IN AS CMS_HLT_R
604 amott 1.24 tmpcurs = ConnectDB('hlt')
605 amott 1.1 sqlquery ="""
606     SELECT I.NAME,A.VALUE
607     FROM
608     CMS_HLT.STRINGPARAMVALUES A,
609     CMS_HLT.PARAMETERS B,
610     CMS_HLT.SUPERIDPARAMETERASSOC C,
611     CMS_HLT.MODULETEMPLATES D,
612     CMS_HLT.MODULES E,
613     CMS_HLT.PATHMODULEASSOC F,
614     CMS_HLT.CONFIGURATIONPATHASSOC G,
615     CMS_HLT.CONFIGURATIONS H,
616     CMS_HLT.PATHS I
617     WHERE
618     A.PARAMID = C.PARAMID AND
619     B.PARAMID = C.PARAMID AND
620     B.NAME = 'L1SeedsLogicalExpression' AND
621     C.SUPERID = F.MODULEID AND
622     D.NAME = 'HLTLevel1GTSeed' AND
623     E.TEMPLATEID = D.SUPERID AND
624     F.MODULEID = E.SUPERID AND
625     F.PATHID=G.PATHID AND
626     I.PATHID=G.PATHID AND
627     G.CONFIGID=H.CONFIGID AND
628     H.CONFIGDESCRIPTOR='%s'
629 amott 1.2 ORDER BY A.VALUE
630 amott 1.1 """ % (self.HLT_Key,)
631     tmpcurs.execute(sqlquery)
632     for HLTPath,L1Seed in tmpcurs.fetchall():
633 amott 1.4 if not self.HLTSeed.has_key(HLTPath): ## this should protect us from L1_SingleMuOpen
634     self.HLTSeed[HLTPath] = L1Seed.lstrip('"').rstrip('"')
635 amott 1.1 #self.GetHLTPrescaleMatrix(tmpcurs)
636    
637     def ParseRunSetup(self):
638     #queries that need to be run only once per run
639     self.GetRunInfo()
640     self.GetL1NameIndexAssoc()
641     self.GetL1AlgoPrescales()
642     self.GetHLTSeeds()
643 amott 1.5 self.GetLumiInfo()
644 grchrist 1.21 self.LastLSParsed=-1
645     self.GetMoreLumiInfo()
646 grchrist 1.27 self.LastLsParsed=-1
647     #self.GetDeadTimeBeamActive()
648 amott 1.5
649     def UpdateRun(self,LSRange):
650     self.GetLumiInfo()
651     TriggerRates = self.GetHLTRates(LSRange)
652 abrinke1 1.10 #L1Prescales = self.CalculateAvL1Prescales(LSRange)
653     #TotalPrescales = self.CalculateTotalPrescales(TriggerRates,L1Prescales)
654     #UnprescaledRates = self.UnprescaleRates(TriggerRates,TotalPrescales)
655 amott 1.5
656 abrinke1 1.10 #return [UnprescaledRates, TotalPrescales, L1Prescales, TriggerRates]
657     return TriggerRates
658    
659 amott 1.23 def GetLSRange(self,StartLS, NLS,reqPhysics=True):
660 amott 1.5 """
661     returns an array of valid LumiSections
662     if NLS < 0, count backwards from StartLS
663     """
664 amott 1.1 self.GetLumiInfo()
665 amott 1.5 LS=[]
666     curLS=StartLS
667     step = NLS/abs(NLS)
668     NLS=abs(NLS)
669     while len(LS)<NLS:
670     if (curLS<0 and step<0) or (curLS>=self.LastLSParsed and step>0):
671     break
672 amott 1.30 if curLS>=0 and curLS<self.LastLSParsed-1:
673 amott 1.23 if (not self.Physics.has_key(curLS) or not self.Active.has_key(curLS)) and reqPhysics:
674 amott 1.5 break
675 amott 1.23 if not reqPhysics or (self.Physics[curLS] and self.Active[curLS]):
676 amott 1.5 if step>0:
677     LS.append(curLS)
678     else:
679     LS.insert(0,curLS)
680     curLS += step
681     return LS
682    
683 amott 1.8 def GetLastLS(self,phys=False):
684 amott 1.7 self.GetLumiInfo()
685     try:
686 amott 1.8 if not phys:
687     return max(self.Physics.keys())
688     else:
689     maxLS=-1
690     for ls,phys in self.Physics.iteritems():
691     if phys and self.Active[ls] and ls > maxLS:
692     maxLS=ls
693     if maxLS==-1:
694     return 0
695     else:
696     return maxLS
697 amott 1.7 except:
698     return 0
699    
700 amott 1.5 def CalculateAvL1Prescales(self,LSRange):
701     AvgL1Prescales = [0]*self.nAlgoBits
702     for index in LSRange:
703 amott 1.1 psi = self.PSColumnByLS[index]
704     if not psi:
705 grchrist 1.25 #print "L1: Cannot figure out PSI for LS "+str(index)+" setting to 0"
706 amott 1.5 psi = 0
707 amott 1.4 for algo in range(self.nAlgoBits):
708 amott 1.5 AvgL1Prescales[algo]+=self.L1PrescaleTable[algo][psi]
709     for i in range(len(AvgL1Prescales)):
710 amott 1.6 try:
711     AvgL1Prescales[i] = AvgL1Prescales[i]/len(LSRange)
712     except:
713     AvgL1Prescales[i] = AvgL1Prescales[i]
714 amott 1.5 return AvgL1Prescales
715    
716     def CalculateTotalPrescales(self,TriggerRates, L1Prescales):
717     AvgTotalPrescales={}
718     for hltName,v in TriggerRates.iteritems():
719 amott 1.4 if not self.HLTSeed.has_key(hltName):
720     continue
721 amott 1.1 hltPS=0
722 amott 1.4 if len(v)>0:
723     hltPS = v[0]
724     l1Index=-1
725     if self.L1IndexNameMap.has_key(self.HLTSeed[hltName]):
726     l1Index = self.L1IndexNameMap[self.HLTSeed[hltName]]
727    
728 amott 1.1 l1PS=0
729     if l1Index==-1:
730 amott 1.5 l1PS = self.UnwindORSeed(self.HLTSeed[hltName],L1Prescales)
731 amott 1.1 else:
732 amott 1.5 l1PS = L1Prescales[l1Index]
733     AvgTotalPrescales[hltName]=l1PS*hltPS
734     return AvgTotalPrescales
735 amott 1.1
736 amott 1.5 def UnwindORSeed(self,expression,L1Prescales):
737 amott 1.4 """
738     Figures out the effective prescale for the OR of several seeds
739     we take this to be the *LOWEST* prescale of the included seeds
740     """
741 amott 1.2 if expression.find(" OR ") == -1:
742     return -1 # Not an OR of seeds
743     seedList = expression.split(" OR ")
744     if len(seedList)==1:
745     return -1 # Not an OR of seeds, really shouldn't get here...
746     minPS = 99999999999
747     for seed in seedList:
748 amott 1.4 if not self.L1IndexNameMap.has_key(seed):
749     continue
750 amott 1.5 ps = L1Prescales[self.L1IndexNameMap[seed]]
751 amott 1.4 if ps:
752     minPS = min(ps,minPS)
753 amott 1.2 if minPS==99999999999:
754     return 0
755     else:
756     return minPS
757    
758 amott 1.5 def UnprescaleRates(self,TriggerRates,TotalPrescales):
759     UnprescaledRates = {}
760     for hltName,v in TriggerRates.iteritems():
761     if TotalPrescales.has_key(hltName):
762     ps = TotalPrescales[hltName]
763 amott 1.4 if ps:
764 amott 1.5 UnprescaledRates[hltName] = v[1]*ps
765 amott 1.4 else:
766 amott 1.5 UnprescaledRates[hltName] = v[1]
767 amott 1.4 else:
768 amott 1.5 UnprescaledRates[hltName] = v[1]
769     return UnprescaledRates
770 abrinke1 1.13
771 amott 1.11 def GetTotalL1Rates(self):
772     query = "SELECT LUMISEGMENTNR, L1ASPHYSICS/23.3 FROM CMS_WBM.LEVEL1_TRIGGER_CONDITIONS WHERE RUNNUMBER=%s" % self.RunNumber
773     self.curs.execute(query)
774     L1Rate = {}
775     for LS,rate in self.curs.fetchall():
776     psi = self.PSColumnByLS.get(LS,0)
777     lumi = self.InstLumiByLS.get(LS,0)
778     L1Rate[LS] = [rate,psi,lumi]
779     return L1Rate
780 abrinke1 1.13
781 amott 1.1 def AssemblePrescaleValues(self): ##Depends on output from ParseLumiPage and ParseTriggerModePage
782     return ## WHAT DOES THIS FUNCTION DO???
783     MissingName = "Nemo"
784     for key in self.L1TriggerMode:
785     self.L1Prescale[key] = {}
786     for n in range(min(self.LSByLS),max(self.LSByLS)+1): #"range()" excludes the last element
787     try:
788     self.L1Prescale[key][n] = self.L1TriggerMode[key][self.PSColumnByLS[n]]
789     except:
790     if not key == MissingName:
791     self.MissingPrescale.append(key)
792     MissingName = key
793     if not n < 2:
794     print "LS "+str(n)+" of key "+str(key)+" is missing from the LumiSections page"
795    
796     for key in self.HLTTriggerMode:
797     self.HLTPrescale[key] = {}
798     for n in range(min(self.LSByLS),max(self.LSByLS)+1): #"range" excludes the last element
799     try:
800     self.HLTPrescale[key][n] = self.HLTTriggerMode[key][self.PSColumnByLS[n]]
801     except:
802     if not key == MissingName:
803     self.MissingPrescale.append(key)
804     MissingName = key
805     if not n < 2:
806     print "LS "+str(n)+" of key "+str(key)+" is missing from the LumiSections page"
807    
808     self.PrescaleValues = [self.L1Prescale,self.HLTPrescale,self.MissingPrescale]
809     return self.PrescaleValues
810    
811     def ComputeTotalPrescales(self,StartLS,EndLS):
812     return ## WHAT DOES THIS FUNCTION DO??
813     IdealHLTPrescale = {}
814     IdealPrescale = {}
815     L1_zero = {}
816     HLT_zero = {}
817     n1 = {}
818     n2 = {}
819     L1 = {}
820     L2 = {}
821     H1 = {}
822     H2 = {}
823     InitialColumnIndex = self.PSColumnByLS[int(StartLS)]
824    
825     for key in self.HLTTriggerMode:
826     try:
827     DoesThisPathHaveAValidL1SeedWithPrescale = self.L1Prescale[self.HLTSeed[key]][StartLS]
828     except:
829     L1_zero[key] = True
830     HLT_zero[key] = False
831     continue
832    
833     IdealHLTPrescale[key] = 0.0
834     IdealPrescale[key] = 0.0
835     n1[key] = 0
836     L1_zero[key] = False
837     HLT_zero[key] = False
838    
839     for LSIterator in range(StartLS,EndLS+1): #"range" excludes the last element
840     if self.L1Prescale[self.HLTSeed[key]][LSIterator] > 0 and self.HLTPrescale[key][LSIterator] > 0:
841     IdealPrescale[key]+=1.0/(self.L1Prescale[self.HLTSeed[key]][LSIterator]*self.HLTPrescale[key][LSIterator])
842     else:
843     IdealPrescale[key]+=1.0 ##To prevent a divide by 0 error later
844     if self.L1Prescale[self.HLTSeed[key]][LSIterator] < 0.1:
845     L1_zero[key] = True
846     if self.HLTPrescale[key][LSIterator] < 0.1:
847     HLT_zero[key] = True
848     if self.PSColumnByLS[LSIterator] == InitialColumnIndex:
849     n1[key]+=1
850    
851     if L1_zero[key] == True or HLT_zero[key] == True:
852     continue
853    
854     IdealPrescale[key] = (EndLS + 1 - StartLS)/IdealPrescale[key]
855    
856     n2[key] = float(EndLS + 1 - StartLS - n1[key])
857     L1[key] = float(self.L1Prescale[self.HLTSeed[key]][StartLS])
858     L2[key] = float(self.L1Prescale[self.HLTSeed[key]][EndLS])
859     H1[key] = float(self.HLTPrescale[key][StartLS])
860     H2[key] = float(self.HLTPrescale[key][EndLS])
861    
862     IdealHLTPrescale[key] = ((n1[key]/L1[key])+(n2[key]/L2[key]))/((n1[key]/(L1[key]*H1[key]))+(n2[key]/(L2[key]*H2[key])))
863    
864     self.TotalPSInfo = [L1_zero,HLT_zero,IdealPrescale,IdealHLTPrescale,n1,n2,L1,L2,H1,H2]
865    
866     return self.TotalPSInfo
867    
868    
869     def CorrectForPrescaleChange(self,StartLS,EndLS):
870     [L1_zero,HLT_zero,IdealPrescale,IdealHLTPrescale,n1,n2,L1,L2,H1,H2] = self.TotalPSInfo
871     xLS = {}
872     RealPrescale = {}
873    
874     for key in self.HLTTriggerMode:
875     if L1_zero[key] == True or HLT_zero[key] == True:
876     continue
877     [TriggerRate,L1Pass,PSPass,PS,Seed,StartLS,EndLS] = self.TriggerRates[key]
878     if PS > 0.95 * IdealHLTPrescale[key] and PS < 1.05 * IdealHLTPrescale[key]:
879     RealPrescale[key] = IdealPrescale[key]
880     continue
881    
882     if H1[key] == H2[key] and L1[key] == L2[key] and not EndLS > max(self.LSByLS) - 1: ##Look for prescale change into the next LS
883     H2[key] = float(self.HLTPrescale[key][EndLS+1])
884     L2[key] = float(self.L1Prescale[self.HLTSeed[key]][EndLS+1])
885     if H1[key] == H2[key] and L1[key] == L2[key] and not StartLS < 3:
886     H1[key] = float(self.HLTPrescale[key][StartLS-1])
887     L1[key] = float(self.L1Prescale[self.HLTSeed[key]][StartLS-1])
888     if H1[key] == H2[key]:
889     xLS[key] = 0
890     else:
891     xLS[key] = ((-(PS/IdealHLTPrescale[key])*(L2[key]*n1[key]+L1[key]*n2[key])*(H2[key]*L2[key]*n1[key]+H1[key]*L1[key]*n2[key]))+((H2[key]*L2[key]*n1[key]+H1[key]*L1[key]*n2[key])*(L2[key]*n1[key]+L1[key]*n2[key])))/(((PS/IdealHLTPrescale[key])*(L2[key]*n1[key]+L1[key]*n2[key])*(H1[key]*L1[key]-H2[key]*L2[key]))+((H2[key]*L2[key]*n1[key]+H1[key]*L1[key]*n2[key])*(L2[key]-L1[key])))
892    
893     if xLS[key] > 1:
894     xLS[key] = 1
895     if xLS[key] < -1:
896     xLS[key] = -1
897     RealPrescale[key] = (n1[key] + n2[key])/(((n1[key] - xLS[key])/(H1[key]*L1[key]))+(n2[key]+xLS[key])/(H2[key]*L2[key]))
898    
899     self.CorrectedPSInfo = [RealPrescale,xLS,L1,L2,H1,H2]
900    
901     return self.CorrectedPSInfo
902    
903 amott 1.4 def GetAvLumiPerRange(self, NMergeLumis=10):
904     """
905 amott 1.5 This function returns a per-LS table of the average lumi of the previous NMergeLumis LS
906 amott 1.4 """
907 amott 1.5 AvLumiRange = []
908     AvLumiTable = {}
909     for ls,lumi in self.InstLumiByLS.iteritems():
910     try:
911     AvLumiRange.append(int(lumi))
912     except:
913     continue
914     if len(AvLumiRange) == NMergeLumis:
915     AvLumiRange = AvLumiRange[1:]
916     AvLumiTable[ls] = sum(AvLumiRange)/NMergeLumis
917 amott 1.4 return AvLumiTable
918    
919 amott 1.9 def GetTriggerVersion(self,triggerName):
920     for key in self.HLTSeed.iterkeys():
921     if StripVersion(key)==triggerName:
922     return key
923     return ""
924    
925 amott 1.1 def Save(self, fileName):
926     dir = os.path.dirname(fileName)
927     if not os.path.exists(dir):
928     os.makedirs(dir)
929     pickle.dump( self, open( fileName, 'w' ) )
930    
931     def Load(self, fileName):
932     self = pickle.load( open( fileName ) )
933 amott 1.5
934 amott 1.24 def ConnectDB(user='trg'):
935 amott 1.30 host = os.uname()[1]
936     offline = 1 if host.startswith('lxplus') else 0
937     print offline
938     trg = ['~centraltspro/secure/cms_trg_r.txt','~/secure/cms_trg_r.txt']
939     hlt = ['~hltpro/secure/cms_hlt_r.txt','~/secure/cms_hlt_r.txt']
940    
941 amott 1.24 if user == 'trg':
942 amott 1.30 cmd = 'cat %s' % (trg[offline],)
943 amott 1.24 elif user == 'hlt':
944 amott 1.30 cmd='cat %s' % (hlt[offline],)
945    
946     try:
947     line=os.popen(cmd).readlines()
948     except:
949     print "ERROR Getting the database password!"
950     print "They should be in %s and %s" % (trg[offline],hlt[offline],)
951     print "You may need to copy them from the online machines"
952     sys.exit(0)
953 amott 1.5 magic = line[0].rstrip("\n\r")
954 amott 1.24 connect = 'cms_%s_r/%s@cms_omds_lb' % (user,magic,)
955 amott 1.5 orcl = cx_Oracle.connect(connect)
956 amott 1.24 return orcl.cursor()
957    
958     def GetLatestRunNumber(runNo=9999999):
959     curs = ConnectDB()
960 amott 1.23 if runNo==9999999:
961 grchrist 1.28
962     ##
963     ##SELECT MAX(RUNNUMBER) FROM CMS_RUNINFO.RUNNUMBERTBL
964     ##SELECT MAX(RUNNUMBER) CMS_WBM.RUNSUMMARY WHERE TRIGGERS>0
965     ## RunNoQuery="""
966     ## SELECT MAX(A.RUNNUMBER) FROM CMS_RUNINFO.RUNNUMBERTBL A, CMS_WBM.RUNSUMMARY B WHERE A.RUNNUMBER=B.RUNNUMBER AND B.TRIGGERS>0
967     ## """
968    
969     RunNoQuery="""SELECT MAX(A.RUNNUMBER)
970     FROM CMS_RUNINFO.RUNNUMBERTBL A, CMS_RUNTIME_LOGGER.LUMI_SECTIONS B WHERE B.RUNNUMBER=A.RUNNUMBER AND B.LUMISECTION > 0
971 amott 1.23 """
972 grchrist 1.31 try:
973     curs.execute(RunNoQuery)
974     r, = curs.fetchone()
975     print "r=",r
976     except:
977     print "not able to get run"
978 grchrist 1.28
979 grchrist 1.31 ## RunNoQuery="""SELECT MAX(RUNNUMBER) FROM CMS_RUNINFO.RUNNUMBERTBL"""
980     ## try:
981     ## curs.execute(RunNoQuery)
982     ## ra, = curs.fetchone()
983     ## print "ra=",ra
984     ## except:
985     ## print "not able to get ra"
986    
987    
988     ## RunNoQuery="""SELECT MAX(RUNNUMBER) FROM CMS_WBM.RUNSUMMARY WHERE TRIGGERS>0"""
989     ## try:
990     ## curs.execute(RunNoQuery)
991     ## rb, = curs.fetchone()
992     ## print "rb=",rb
993     ## except:
994     ## print "not able to get rb"
995    
996     ## RunNoQuery="""SELECT MAX(RUNNUMBER) FROM CMS_RUNTIME_LOGGER.LUMI_SECTIONS WHERE LUMISECTION > 0 """
997     ## try:
998     ## curs.execute(RunNoQuery)
999     ## rc, = curs.fetchone()
1000     ## print "rc=",rc
1001     ## except:
1002     ## print "not able to get rc"
1003    
1004 amott 1.23 else:
1005     r = runNo
1006 grchrist 1.29
1007 grchrist 1.28 isCol=0
1008 amott 1.5 TrigModeQuery = """
1009     SELECT TRIGGERMODE FROM CMS_WBM.RUNSUMMARY WHERE RUNNUMBER = %d
1010     """ % r
1011     curs.execute(TrigModeQuery)
1012 grchrist 1.29 try:
1013     trigm, = curs.fetchone()
1014     except:
1015     pass
1016 amott 1.5 isCol=0
1017 grchrist 1.31 isGood=1
1018    
1019 grchrist 1.29
1020     try:
1021 grchrist 1.31 if trigm is None:
1022     isGood=0
1023    
1024     elif trigm.find('l1_hlt_collisions')!=-1:
1025 grchrist 1.29 isCol=1
1026 grchrist 1.31
1027 grchrist 1.29 except:
1028 grchrist 1.31 print "trigm exception. This should not happen! Post to HLT on-call elog\nwith run number."
1029     isGood=0
1030    
1031     return (r,isCol,isGood,)
1032 amott 1.5
1033     def ClosestIndex(value,table):
1034     diff = 999999999;
1035     index = 0
1036     for i,thisVal in table.iteritems():
1037     if abs(thisVal-value)<diff:
1038     diff = abs(thisVal-value)
1039     index =i
1040     return index
1041    
1042    
1043     def StripVersion(name):
1044     if re.match('.*_v[0-9]+',name):
1045     name = name[:name.rfind('_')]
1046     return name