ATLAS Offline Software
Loading...
Searching...
No Matches
TrigConfigSvcUtils.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4from xml.dom import minidom
5import re
6import os
7
8from AthenaCommon.Logging import logging
9from AthenaCommon.Utils.unixtools import FindFile
10
11log = logging.getLogger( "TrigConfigSvcUtils.py" )
12if log.level==0: log.setLevel(logging.INFO)
13
14#**
15# In this section:
16#
17# interpretation of DB alias and creating DB connection when running Reco transforms
18#**
19def _getFileLocalOrPath(filename, pathenv):
20 """looks for filename in local directory and then in all paths specified in environment variable 'pathenv'
21 returns path/filename if existing, otherwise None
22 """
23 if os.path.exists(filename):
24 log.info( "Using local file %s", filename)
25 return filename
26
27 pathlist = os.getenv(pathenv,'').split(os.pathsep)
28 resolvedfilename = FindFile(filename, pathlist, os.R_OK)
29 if resolvedfilename:
30 return resolvedfilename
31
32 log.fatal("No file %s found locally nor in %s" % (filename, os.getenv('CORAL_DBLOOKUP_PATH')) )
33 return None
34
35
37
38 connectionServices = None # list of services
39
40 dblookupfilename = _getFileLocalOrPath('dblookup.xml','CORAL_DBLOOKUP_PATH')
41 if dblookupfilename is None: return None
42
43 doc = minidom.parse(dblookupfilename)
44 for ls in doc.getElementsByTagName('logicalservice'):
45 if ls.attributes['name'].value != alias: continue
46 connectionServices = [str(s.attributes['name'].value) for s in ls.getElementsByTagName('service')]
47 doc.unlink()
48
49 log.info( "For alias '%s' found list of connections %r", alias,connectionServices )
50 if connectionServices is None:
51 log.fatal("Trigger connection alias '%s' is not defined in %s" % (alias,dblookupfilename))
52 return connectionServices
53
54
56 """ read authentication.xml, first from local directory, then from all paths specified in CORAL_AUTH_PATH
57
58 returns dictionary d with d[connection] -> (user,pw)
59 """
60
61 authDict = {}
62
63 dbauthfilename = _getFileLocalOrPath('authentication.xml','CORAL_AUTH_PATH')
64 if dbauthfilename is None: return authDict
65
66 doc = minidom.parse(dbauthfilename)
67 for cn in doc.getElementsByTagName('connection'):
68 user = ""
69 pw = ""
70 for p in cn.getElementsByTagName('parameter'):
71 if p.attributes['name'].value == 'user': user = p.attributes['value'].value
72 if p.attributes['name'].value == 'password': pw = p.attributes['value'].value
73 authDict[cn.attributes['name'].value] = (user,pw)
74 doc.unlink()
75 return authDict
76
77
78
79authDict = None
80
82 connection = str(connection)
83 connectionParameters = {}
84
85 if connection.startswith("sqlite_file:"):
86 filename = connection[connection.find(':')+1:]
87 connectionParameters["techno"] = "sqlite"
88 connectionParameters["filename"] = filename if os.path.exists(filename) else None
89
90 elif connection.startswith("oracle://"):
91 if connection.count(';') == 2:
92 pattern = "(.*)://(.*)/(.*);username=(.*);password=(.*)"
93 m = re.match(pattern,connection)
94 #print 'Groups ', m
95 if not m:
96 log.fatal("connection string '%s' doesn't match the pattern '%s'?" % (connection,pattern))
97 (techno, server, schema, user, passwd) = m.groups()
98 #print (techno, server, schema, user, passwd)
99 else:
100 global authDict
101 if not authDict: authDict = _readAuthentication()
102 pattern = "oracle://(.*)/(.*)"
103 m = re.match(pattern,connection)
104 if not m:
105 log.fatal("connection string '%s' doesn't match the pattern '%s'?" % (connection,pattern))
106 (server, schema) = m.groups()
107 (user,passwd) = authDict[connection]
108 #info from auth file also unicode
109 connectionParameters["techno"] = 'oracle'
110 connectionParameters["server"] = str(server)
111 connectionParameters["schema"] = str(schema)
112 connectionParameters["user" ] = str(user)
113 connectionParameters["passwd"] = str(passwd)
114
115 elif connection.startswith("mysql://"):
116 pattern = "mysql://(.*);dbname=(.*);user=(.*);passwd=(.*);"
117 m = re.match(pattern,connection)
118 if not m:
119 log.fatal("connection string '%s' doesn't match the pattern '%s'?" % (connection,pattern) )
120 (server, dbname, user, passwd) = m.groups()
121 connectionParameters["techno"] = 'mysql'
122 connectionParameters["server"] = server
123 connectionParameters["dbname"] = dbname
124 connectionParameters["user" ] = user
125 connectionParameters["passwd"] = passwd
126
127 elif connection.startswith("frontier://"):
128 pattern = r"frontier://ATLF/\‍(\‍)/(.*)"
129 m = re.match(pattern,connection)
130 if not m:
131 log.fatal("connection string '%s' doesn't match the pattern '%s'?" % (connection,pattern) )
132 (schema, ) = m.groups()
133 connectionParameters["techno"] = 'frontier'
134 connectionParameters["schema"] = schema
135
136 return connectionParameters
137
138
139def interpretConnection(connection, debug=False, resolveAlias=True):
140 """connection needs to be of the following format (this is also the order of checking)
141 <ALIAS> -- any string without a colon ':' will be checked for in the dblookup.xml file
142 type:<detail> -- no dblookup will be used, type has to be oracle, mysql, or sqlite_file
143 sqlite_file:filename.db -- an sqlite file, no authentication needed, will be opened in read-only mode
144 oracle://ATLR/ATLAS_CONF_TRIGGER_V2 -- a service description without user and password, requires lookup in authentication.xml
145 oracle://ATLR/ATLAS_CONF_TRIGGER_V2;username=ATLAS_CONF_TRIGGER_V2_R;password=<...> -- a service description with user and password
146 """
147
148 log.info("Specified connection string '%s'", connection)
149
150 # not needed any longer
151 # connection = connection.lstrip("dblookup://")
152 # connection = connection.lstrip("dblookup:")
153 # connection = connection.rstrip(";")
154 #print connection
155
156 # what to return
157 connectionParameters = {}
158 connection = str(connection)
159
160 # connection explicitly specified (no DB alias)
161 if ':' in connection:
162 connectionParameters = _getConnectionParameters( connection )
163 return connectionParameters
164
165
166 # connection is a DB alias
167 connectionParameters["alias"] = connection
168 if not resolveAlias:
169 return connectionParameters
170
171 connectionServices = _getConnectionServicesForAlias( connection ) # alias resolution via dblookup
172 if connectionServices is None:
173 return connectionParameters
174
175 # SQLite
176 sqliteconnections = [conn for conn in connectionServices if conn.startswith("sqlite_file")]
177 if len(sqliteconnections)>0:
178 for conn in sqliteconnections:
179 connectionParameters = _getConnectionParameters( conn )
180 if connectionParameters["filename"] is not None:
181 break # stop at the first sqlite file that exists
182 if connectionParameters["filename"] is not None:
183 log.info("Using sqlite connection %s", connectionParameters)
184 return connectionParameters
185 else:
186 if 'ATLAS_TRIGGERDB_FORCESQLITE' in os.environ:
187 log.fatal("environment ATLAS_TRIGGERDB_FORCESQLITE is defined but non of the sqlite files defined in dblookup.xml exists" )
188 else:
189 if 'ATLAS_TRIGGERDB_FORCESQLITE' in os.environ:
190 log.fatal("environment ATLAS_TRIGGERDB_FORCESQLITE is defined but no sqlite connection defined in dblookup.xml" )
191
192 # replicaList
193 from CoolConvUtilities.AtlCoolLib import replicaList
194 serverlist=['ATLAS_CONFIG' if s=='ATLAS_COOLPROD' else s for s in replicaList()] # replicaList is for COOL, I need ATLAS_CONFIG instead of ATLAS_COOLPROD
195 #serverlist=['ATLF']
196 log.info("Trying these servers in order %r", serverlist)
197 for server in serverlist:
198 log.info("Trying server %s", server)
199
200 if server=='ATLF':
201 frontierconnections = [conn for conn in connectionServices if conn.startswith("frontier")]
202 if len(frontierconnections) == 0:
203 log.debug("FroNTier connection not defined for alias %s in dblookup", connection )
204 continue
205 log.info("Environment FRONTIER_SERVER: %s", os.getenv('FRONTIER_SERVER','not defined'))
206 frontierServer = os.getenv('FRONTIER_SERVER',None)
207 if not frontierServer:
208 log.debug("No environment variable FRONTIER_SERVER" )
209 continue
210 connectionParameters = _getConnectionParameters( frontierconnections[0] )
211 connectionParameters['url'] = frontierServer
212 log.info("Using frontier connection %s", frontierconnections[0])
213 #connstr='frontier://ATLF/%s;schema=%s;dbname=TRIGCONF' % (connectionParameters['url'],connectionParameters["schema"])
214 break
215 elif server=='atlas_dd': continue
216 else:
217 oracleconnections = [conn for conn in connectionServices if conn.lower().startswith("oracle://%s/" % server.lower())]
218 if len(oracleconnections) == 0:
219 log.debug("Oracle connection not defined for server %s in dblookup", server )
220 continue
221 connectionParameters = _getConnectionParameters( oracleconnections[0] )
222 log.info("Using oracle connection %s", oracleconnections[0])
223 #connstr='oracle://%s;schema=ATLAS_%s;dbname=TRIGCONF' % (connectionParameters["server"],connectionParameters["schema"])
224 break
225
226 return connectionParameters
227
228
229def getTriggerDBCursor(connection):
230
231 connectionParameters = interpretConnection(connection)
232
233 technology = connectionParameters["techno"]
234
235 if technology == 'sqlite':
236 cursor = _get_sqlite_cursor(connectionParameters["filename"])
237 schema = ''
238
239 elif technology == 'oracle':
240 cursor = _get_oracle_cursor(connectionParameters["server"], connectionParameters["user"], connectionParameters["passwd"])
241 schema = connectionParameters["schema"].rstrip('.') + '.'
242
243 elif technology == 'frontier':
244 from TrigConfigSvc.TrigConfFrontier import getFrontierCursor
245 schema = connectionParameters["schema"].rstrip('.')
246 cursor = getFrontierCursor( urls = connectionParameters['url'], schema = schema, loglevel = logging.getLogger("TrigConfigSvcUtils.py").level)
247 schema = schema + '.'
248
249 elif technology == 'mysql':
250 cursor = _get_mysql_cursor(connectionParameters["server"], connectionParameters["dbname"], connectionParameters["user"], connectionParameters["passwd"]),''
251 schema = ''
252
253 return cursor,schema
254
255def _get_sqlite_cursor (filename):
256 import sqlite3
257 os.lstat(filename)
258 connection = sqlite3.connect(filename)
259 return connection.cursor()
260
261def _get_oracle_cursor (tns, user, passwd=""):
262 if passwd=="":
263 from getpass import getpass
264 passwd = getpass("[Oracle] database password for %s@%s: " % (user, tns))
265
266 from cx_Oracle import connect
267 connection = connect (user, passwd, tns, threaded=True)
268 return connection.cursor()
269
270
271def _get_mysql_cursor (host, db, user, passwd=""):
272 if passwd=="":
273 from getpass import getpass
274 passwd = getpass("[MySQL] `%s' database password for %s@%s: " % (db, user, host))
275
276 from MySQLdb import connect
277 connection = connect(host=host, user=user, passwd=passwd, db=db, connect_timeout=10)
278 return connection.cursor()
279
280def getUsedTables(output, condition, schemaname, tables):
281 usedtables = set()
282 for o in output:
283 usedtables.add(o.split('.')[0])
284 for c in condition:
285 for p in c.split():
286 if '.' in p and '\'' not in p: usedtables.add(p.split('.')[0].lstrip('('))
287 return ["%s%s %s" % (schemaname,tables[t],t) for t in usedtables]
288
289
290def isRun2(cursor,schemaname):
291 import cx_Oracle
292 if not hasattr(cursor,'connection') or type(cursor.connection)!=cx_Oracle.Connection:
293 log.warning('Detection of DB schema only supported for Oracle. Will assume run-2')
294 return True
295
296 owner = schemaname.rstrip('.')
297 query = "select table_name from all_tables where table_name='ACTIVE_MASTERS' and owner='%s'" % owner
298 cursor.execute(query)
299 return (len(cursor.fetchall())>0)
300
301def executeQuery(cursor, output, condition, schemaname, tables, bindvars=()):
302 query = 'select distinct %s from %s' % \
303 (', '.join(output),
304 ', '.join(getUsedTables(output, condition, schemaname, tables)))
305
306 if condition:
307 query += ' where ' + ' and '.join(condition)
308
309 if len(bindvars)==0:
310 log.debug("Executing query %s", query)
311 cursor.execute(str(query))
312 else:
313 log.debug("Executing query %s with bound variables %r", query, bindvars)
314 cursor.execute(str(query),bindvars)
315 return cursor.fetchall()
316
317
318
319def getAlgorithmsForMenu(connection, smk):
320 cursor,schemaname = getTriggerDBCursor(connection)
321
322 output = ['TC.HTC_L2_OR_EF', 'C.HCP_NAME', 'C.HCP_ALIAS' ]
323
324 tables = {}
325 tables['SM'] = 'SUPER_MASTER_TABLE'
326 tables['HM'] = 'HLT_MASTER_TABLE'
327 tables['TM'] = 'HLT_TRIGGER_MENU'
328 tables['M2C'] = 'HLT_TM_TO_TC'
329 tables['TC'] = 'HLT_TRIGGER_CHAIN'
330 tables['C2S'] = 'HLT_TC_TO_TS'
331 tables['S2TE'] = 'HLT_TS_TO_TE'
332 tables['TE2C'] = 'HLT_TE_TO_CP'
333 tables['TE2TE'] = 'HLT_TE_TO_TE'
334 tables['C'] = 'HLT_COMPONENT'
335
336 condition = [ "SM.SMT_ID = '%i'" % smk,
337 'SM.SMT_HLT_MASTER_TABLE_ID = HM.HMT_ID',
338 'HM.HMT_TRIGGER_MENU_ID = M2C.HTM2TC_TRIGGER_MENU_ID',
339 'M2C.HTM2TC_TRIGGER_CHAIN_ID = TC.HTC_ID',
340 'M2C.HTM2TC_TRIGGER_CHAIN_ID = C2S.HTC2TS_TRIGGER_CHAIN_ID',
341 'C2S.HTC2TS_TRIGGER_SIGNATURE_ID = S2TE.HTS2TE_TRIGGER_SIGNATURE_ID',
342 'S2TE.HTS2TE_TRIGGER_ELEMENT_ID = TE2C.HTE2CP_TRIGGER_ELEMENT_ID',
343 'TE2C.HTE2CP_COMPONENT_ID = C.HCP_ID' ]
344
345 res = executeQuery(cursor, output, condition, schemaname, tables)
346
347 l2algs=[]
348 efalgs=[]
349 for x in res:
350 if x[0]=='L2':
351 l2algs += ["%s/%s" % (x[1],x[2])]
352 else:
353 efalgs += ["%s/%s" % (x[1],x[2])]
354
355 return l2algs, efalgs
356
357
358
359
360def getAlgorithmsForMenuRun2(connection, smk):
361 cursor,schemaname = getTriggerDBCursor(connection)
362
363 output = [ 'C.HCP_NAME', 'C.HCP_ALIAS' ]
364
365 tables = {}
366 tables['SM'] = 'SUPER_MASTER_TABLE'
367 tables['HM'] = 'HLT_MASTER_TABLE'
368 tables['TM'] = 'HLT_TRIGGER_MENU'
369 tables['M2C'] = 'HLT_TM_TO_TC'
370 tables['TC'] = 'HLT_TRIGGER_CHAIN'
371 tables['C2S'] = 'HLT_TC_TO_TS'
372 tables['S2TE'] = 'HLT_TS_TO_TE'
373 tables['TE2C'] = 'HLT_TE_TO_CP'
374 tables['TE2TE'] = 'HLT_TE_TO_TE'
375 tables['C'] = 'HLT_COMPONENT'
376
377 condition = [ "SM.SMT_ID = '%i'" % smk,
378 'SM.SMT_HLT_MASTER_TABLE_ID = HM.HMT_ID',
379 'HM.HMT_TRIGGER_MENU_ID = M2C.HTM2TC_TRIGGER_MENU_ID',
380 'M2C.HTM2TC_TRIGGER_CHAIN_ID = TC.HTC_ID',
381 'M2C.HTM2TC_TRIGGER_CHAIN_ID = C2S.HTC2TS_TRIGGER_CHAIN_ID',
382 'C2S.HTC2TS_TRIGGER_SIGNATURE_ID = S2TE.HTS2TE_TRIGGER_SIGNATURE_ID',
383 'S2TE.HTS2TE_TRIGGER_ELEMENT_ID = TE2C.HTE2CP_TRIGGER_ELEMENT_ID',
384 'TE2C.HTE2CP_COMPONENT_ID = C.HCP_ID' ]
385
386 res = executeQuery(cursor, output, condition, schemaname, tables)
387
388 allalgs=[]
389 for x in res:
390 allalgs += ["%s/%s" % (x[0],x[1])]
391
392 return allalgs
393
394
395
396def getPropertyFromDB(connection, smk, component, parameter):
397 """Get property value from DB. smk can be a single SMK or a list/tuple of SMKs.
398 SQL wildcards (%) can be used in both component and parameter names.
399 Return [(SMK,Component,Parameter,Value,Level)]
400 """
401
402 cursor,schemaname = getTriggerDBCursor(connection)
403
404 isrun2 = isRun2(cursor,schemaname)
405 output = ['SM.SMT_ID', 'HCP.HCP_NAME', 'PAR.HPA_NAME', 'PAR.HPA_VALUE', 'HS.HST_ID']
406 output += ['HM.HMT_SETUP_ID' if isrun2 else 'HM.HMT_L2_SETUP_ID']
407
408 tables = {'SM' : 'SUPER_MASTER_TABLE',
409 'HM' : 'HLT_MASTER_TABLE',
410 'HCP': 'HLT_COMPONENT',
411 'HS' : 'HLT_SETUP',
412 'HST2CP' : 'HLT_ST_TO_CP',
413 'HCP2PA' : 'HLT_CP_TO_PA',
414 'PAR' : 'HLT_PARAMETER'
415 }
416
417 if type(smk)!=list and type(smk)!=tuple:
418 smk = [smk]
419
420 condition = ["SM.SMT_ID IN (%s)" % ",".join([str(i) for i in smk]),
421 'SM.SMT_HLT_MASTER_TABLE_ID = HM.HMT_ID',
422 ('HM.HMT_SETUP_ID = HS.HST_ID' if isrun2 else '(HM.HMT_L2_SETUP_ID = HS.HST_ID or HM.HMT_EF_SETUP_ID = HS.HST_ID)'),
423 'HST2CP.HST2CP_SETUP_ID = HS.HST_ID',
424 'HST2CP.HST2CP_COMPONENT_ID = HCP2PA.HCP2PA_COMPONENT_ID',
425 'HST2CP.HST2CP_COMPONENT_ID = HCP.HCP_ID',
426 'HCP2PA.HCP2PA_PARAMETER_ID = PAR.HPA_ID',
427 "HCP.HCP_NAME like '%s'" % component,
428 "PAR.HPA_NAME like '%s'" % parameter]
429
430 res = executeQuery(cursor, output, condition, schemaname, tables)
431
432 if isrun2: return [ tuple(x[:4]+("HLT",) ) for x in res ]
433 else: return [ tuple(x[:4]+("L2" if x[4]==x[5] else "EF",) ) for x in res ]
434
435def getMenuNameFromDB(connection, hltprescalekey):
436
437 cursor,schemaname = getTriggerDBCursor(connection)
438
439 tables = { 'HPS' : 'HLT_PRESCALE_SET' }
440
441 output = ['HPS.HPS_NAME']
442
443 condition = [ "HPS.HPS_ID = '%i'" % hltprescalekey ]
444
445 res = executeQuery(cursor, output, condition, schemaname, tables)
446
447 # now we need to do some logic, related to the
448
449 print(res)
450
451 hltpsName = str(res[0][0])
452
453 # temporarily here, but should use the function from TMP
454 m = re.match( "(.*)_default_prescale", hltpsName)
455 menuName = m.group(1) if m else hltpsName
456
457 log.info("Interpreting menu name from HLT prescale key %i: %s", hltprescalekey, menuName)
458
459 return menuName
460
461def getKeysFromName(connection, name, MCOnly=False):
462 cursor,schemaname = getTriggerDBCursor(connection)
463
464 output = ['SM.SMT_ID', 'SM.SMT_NAME']
465
466 tables = {}
467 tables['SM'] = 'SUPER_MASTER_TABLE'
468
469 condition = [ "SM.SMT_NAME like '%s'" % name ]
470
471 res = executeQuery(cursor, output, condition, schemaname, tables)
472 return res
473
474def getKeysFromNameRelease(connection, name, release, l1only):
475
476 cursor,schemaname = getTriggerDBCursor(connection)
477
478 smname = name.split('__')[0]
479 print('SM name %s' % smname)
480 print('PS name %s' % name)
481 print('release %s' % release)
482 keys = []
483
484 #Find the Release id
485 tables = {}
486 tables['RE'] = 'HLT_RELEASE'
487
488 output = ['RE.HRE_ID']
489 condition = [ "RE.HRE_NAME like '%s'" %release ]
490
491 rel = executeQuery(cursor, output, condition, schemaname, tables)
492 relid = (str(rel[-1])).lstrip('(').rstrip(')').split(',')[0]
493
494 #Find the supermaster key
495 tables = {}
496 tables['SM'] = 'SUPER_MASTER_TABLE'
497 tables['SM2RE'] = 'HLT_SMT_TO_HRE'
498
499 output = ['SM.SMT_ID']
500 condition = [ "SM.SMT_NAME like '%s'" % smname,
501 "SM.SMT_ID = SM2RE.SMT2RE_SUPER_MASTER_TABLE_ID",
502 "SM2RE.SMT2RE_RELEASE_ID = '%s'" % relid]
503
504 smk = executeQuery(cursor, output, condition, schemaname, tables)
505 smid = (str(smk[-1])).lstrip('(').rstrip(')').split(',')[0]
506 #NB no check if we return more than one smk! (take the first here with the split!)
507
508 #Find the L1 prescale key with the name and linked to this L1 master
509 tables = {}
510 tables['LPS'] = 'L1_PRESCALE_SET'
511 tables['PS2RE'] = 'L1_HRE_TO_PS'
512 tables['RE'] = 'HLT_RELEASE'
513
514 output = ['LPS.L1PS_ID']
515 condition = [ "PS2RE.L1RE2PS_RELEASE_ID = '%s'" % relid,
516 "PS2RE.L1RE2PS_PRESCALE_ID = LPS.L1PS_ID",
517 "LPS.L1PS_NAME like '%s'" % name]
518
519 l1k = executeQuery(cursor, output, condition, schemaname, tables)
520 l1id = (str(l1k[-1])).lstrip('(').rstrip(')').split(',')[0]
521
522 #Check the PSkey returned matches the SMK
523 tables = {}
524 tables['LPS'] = 'L1_PRESCALE_SET'
525 tables['TM2PS'] = 'L1_TM_TO_PS'
526 tables['LTM'] = 'L1_TRIGGER_MENU'
527 tables['LMT'] = 'L1_MASTER_TABLE'
528 tables['SM'] = 'SUPER_MASTER_TABLE'
529
530 output = ['SM.SMT_ID']
531 condition = [ "LPS.L1PS_ID = '%s'" % l1id,
532 "TM2PS.L1TM2PS_PRESCALE_SET_ID = '%s'" % l1id,
533 "TM2PS.L1TM2PS_TRIGGER_MENU_ID = LTM.L1TM_ID",
534 "LTM.L1TM_ID = LMT.L1MT_TRIGGER_MENU_ID",
535 "LMT.L1MT_ID = SM.SMT_ID"]
536
537 l1chk = executeQuery(cursor, output, condition, schemaname, tables)
538 smk_chk = (str(l1chk[-1])).lstrip('(').rstrip(')').split(',')[0]
539 if smk_chk != smid:
540 log.fatal("SMK returned by release, SMK '%s', does not match the one returned by L1PS key, SMK '%s' " %(smid,smk_chk) )
541
542 if not l1only:
543 #Find the HLT prescale key with the name and linked to this HLT master
544 tables = {}
545 tables['HPS'] = 'HLT_PRESCALE_SET'
546 tables['PS2RE'] = 'HLT_HRE_TO_PS'
547 tables['RE'] = 'HLT_RELEASE'
548
549 output = ['HPS.HPS_ID']
550 condition = [ "PS2RE.HLTRE2PS_RELEASE_ID = '%s'" % relid,
551 "PS2RE.HLTRE2PS_PRESCALE_ID = HPS.HPS_ID",
552 "HPS.HPS_NAME like '%s'" % name]
553
554 hltk = executeQuery(cursor, output, condition, schemaname, tables)
555 hltid = (str(hltk[-1])).lstrip('(').rstrip(')').split(',')[0]
556 print('HLT PS gotten %s' % hltid)
557
558 keys = [int(smid), int(l1id), int(hltid)]
559
560 #Check the PSkey returned matches the SMK
561 tables = {}
562 tables['HPS'] = 'HLT_PRESCALE_SET'
563 tables['TM2PS'] = 'HLT_TM_TO_PS'
564 tables['HTM'] = 'HLT_TRIGGER_MENU'
565 tables['HMT'] = 'HLT_MASTER_TABLE'
566 tables['SM'] = 'SUPER_MASTER_TABLE'
567
568 output = ['SM.SMT_ID']
569 condition = [ "HPS.HPS_ID = '%s'" % hltid,
570 "TM2PS.HTM2PS_PRESCALE_SET_ID = '%s'" % hltid,
571 "TM2PS.HTM2PS_TRIGGER_MENU_ID = HTM.HTM_ID",
572 "HTM.HTM_ID = HMT.HMT_TRIGGER_MENU_ID",
573 "HMT.HMT_ID = SM.SMT_ID"]
574
575 hltchk = executeQuery(cursor, output, condition, schemaname, tables)
576 smk_chk = (str(hltchk[-1])).lstrip('(').rstrip(')').split(',')[0]
577 if smk_chk != smid:
578 log.fatal("SMK returned by release, SMK '%s', does not match the one returned by L1PS key, SMK '%s' " %(smid,smk_chk) )
579
580
581 else:
582 keys = [int(smid), int(l1id)]
583
584 return keys
585
586def getChainsAndStreams(connection, smk):
587 l2, ef = getChains(connection, smk)
588 strm = getStreams(connection, smk)
589 return l2, ef, strm
590
591
592def getChains(connection, smk):
593 cursor,schemaname = getTriggerDBCursor(connection)
594
595 isrun2 = isRun2(cursor,schemaname)
596
597 output = []
598
599 if isrun2:
600 output = ['TC.HTC_ID', 'TC.HTC_CHAIN_COUNTER', 'TC.HTC_NAME']
601 else:
602 output = ['TC.HTC_ID', 'TC.HTC_CHAIN_COUNTER', 'TC.HTC_NAME', 'TC.HTC_L2_OR_EF']
603 tables = {}
604 tables['SM'] = 'SUPER_MASTER_TABLE'
605 tables['M2C'] = 'HLT_TM_TO_TC'
606 tables['TC'] = 'HLT_TRIGGER_CHAIN'
607 tables['MT'] = 'HLT_MASTER_TABLE'
608
609 condition = [ "SM.SMT_ID = :smk",
610 'SM.SMT_HLT_MASTER_TABLE_ID = MT.HMT_ID',
611 'MT.HMT_TRIGGER_MENU_ID = M2C.HTM2TC_TRIGGER_MENU_ID',
612 'M2C.HTM2TC_TRIGGER_CHAIN_ID = TC.HTC_ID' ]
613
614 bindvars = { "smk": smk }
615
616 res = executeQuery(cursor, output, condition, schemaname, tables, bindvars)
617
618 chainsl2 = {}
619 chainsef = {}
620 for x in res:
621 if isrun2:
622 chainsef[x[1]] = x[2]
623 else:
624 if x[3]=='L2': chainsl2[x[1]] = x[2]
625 else: chainsef[x[1]] = x[2]
626
627 return chainsl2, chainsef
628
629
630def getChainsWithLowerChainNames(connection, smk):
631 cursor,schemaname = getTriggerDBCursor(connection)
632
633 isrun2 = isRun2(cursor,schemaname)
634
635 output = []
636 chainshlt = {}
637
638 if isrun2:
639 output = ['TC.HTC_ID', 'TC.HTC_CHAIN_COUNTER', 'TC.HTC_NAME', 'TC.HTC_LOWER_CHAIN_NAME']
640 else:
641 log.error("This method is compatibly with Run2 only")
642 return chainshlt
643
644 tables = {}
645 tables['SM'] = 'SUPER_MASTER_TABLE'
646 tables['M2C'] = 'HLT_TM_TO_TC'
647 tables['TC'] = 'HLT_TRIGGER_CHAIN'
648 tables['MT'] = 'HLT_MASTER_TABLE'
649
650 condition = [ "SM.SMT_ID = :smk",
651 'SM.SMT_HLT_MASTER_TABLE_ID = MT.HMT_ID',
652 'MT.HMT_TRIGGER_MENU_ID = M2C.HTM2TC_TRIGGER_MENU_ID',
653 'M2C.HTM2TC_TRIGGER_CHAIN_ID = TC.HTC_ID' ]
654
655 bindvars = { "smk": smk }
656
657 res = executeQuery(cursor, output, condition, schemaname, tables, bindvars)
658
659 if res == [()]: # happens when no HLT menu is attached
660 return chainshlt
661
662 for x in res:
663 chainshlt[x[1]] = (x[2],x[3])
664
665 return chainshlt
666
667
668def getStreams(connection, smk):
669 cursor,schemaname = getTriggerDBCursor(connection)
670
671 # now the streams
672 output = ['TS.HTR_NAME']
673 tables = {}
674 tables['SM'] = 'SUPER_MASTER_TABLE'
675 tables['M2C'] = 'HLT_TM_TO_TC'
676 tables['TC'] = 'HLT_TRIGGER_CHAIN'
677 tables['MT'] = 'HLT_MASTER_TABLE'
678 tables['TS'] = 'HLT_TRIGGER_STREAM'
679 tables['C2S'] = 'HLT_TC_TO_TR'
680
681
682 condition = [ "SM.SMT_ID = :smk",
683 'SM.SMT_HLT_MASTER_TABLE_ID = MT.HMT_ID',
684 'MT.HMT_TRIGGER_MENU_ID = M2C.HTM2TC_TRIGGER_MENU_ID',
685 'M2C.HTM2TC_TRIGGER_CHAIN_ID = C2S.HTC2TR_TRIGGER_CHAIN_ID',
686 'C2S.HTC2TR_TRIGGER_STREAM_ID = TS.HTR_ID' ]
687
688 bindvars = { "smk": smk }
689
690 res = executeQuery(cursor, output, condition, schemaname, tables, bindvars)
691 streams = [x[0] for x in res]
692
693
694 return streams
695
696def getL1Items(connection, smk):
697
698 cursor,schemaname = getTriggerDBCursor(connection)
699
700 output = ['TI.L1TI_NAME', 'TI.L1TI_CTP_ID' ]
701 tables = { 'SM' : 'SUPER_MASTER_TABLE',
702 'M' : 'L1_MASTER_TABLE',
703 'M2I' : 'L1_TM_TO_TI',
704 'TI' : 'L1_TRIGGER_ITEM'
705 }
706 condition = [ "SM.SMT_ID = :smk",
707 'SM.SMT_L1_MASTER_TABLE_ID = M.L1MT_ID',
708 'M.L1MT_TRIGGER_MENU_ID = M2I.L1TM2TI_TRIGGER_MENU_ID',
709 'M2I.L1TM2TI_TRIGGER_ITEM_ID = TI.L1TI_ID' ]
710 bindvars = { "smk": smk }
711
712 res = executeQuery( cursor, output, condition, schemaname, tables, bindvars)
713
714 items = {}
715 for r in res:
716 items[r[0]] = r[1]
717 return items
718
719def getBunchGroupContent(connection, bgsk):
720
721 cursor,schemaname = getTriggerDBCursor(connection)
722
723 output = [ "BGS.L1BGS2BG_INTERNAL_NUMBER","BG.L1BG2B_BUNCH_NUMBER" ]
724
725 tables = { 'BGS' : 'L1_BGS_TO_BG',
726 'BG' : 'L1_BG_TO_B'}
727
728 condition = [ "BGS.L1BGS2BG_BUNCH_GROUP_SET_ID = :bgsk",
729 "BGS.L1BGS2BG_BUNCH_GROUP_ID=BG.L1BG2B_BUNCH_GROUP_ID" ]
730
731
732 bindvars = { "bgsk": bgsk }
733
734 res = executeQuery(cursor, output, condition, schemaname, tables, bindvars)
735
736 bg = dict( enumerate( [[] for x in range(16)] ) )
737
738 for e in res:
739 bg[e[0]] += [e[1]]
740
741 return bg
742
743
744def getL1Prescales(connection, l1prescalekey):
745
746 cursor,schemaname = getTriggerDBCursor(connection)
747 isrun2 = isRun2(cursor,schemaname)
748
749 maxitems = 512 if isrun2 else 256
750 tables = { 'L' : 'L1_PRESCALE_SET' }
751
752 output = ['L.L1PS_NAME'] + ['L.L1PS_VAL%i' % i for i in range(1,maxitems+1)]
753
754 condition = [ "L.L1PS_ID = '%i'" % l1prescalekey ]
755
756 res = executeQuery(cursor, output, condition, schemaname, tables)
757
758 name,prescales = res[0][0], res[0][1:maxitems+1]
759 if isrun2: prescales = map(getPrescaleFromCut, prescales)
760
761 return (name,prescales)
762
764 """Convert (run-2) prescale cuts into prescale value"""
765 sign = -1 if cut<0 else 1
766 ucut = abs(cut)
767 return (sign*0xFFFFFF ) / float( 0x1000000 - ucut )
768
769def queryHLTPrescaleTable(connection,psk):
770 """returns content of prescale set table and prescale table for a
771 given HLT prescale key
772 @connection - connection string, e.g. TRIGGERDB
773 @psk - HLT prescale key
774
775 @return (ps name, [('L2/EF/express',chainId,prescale,pass-through),...])
776 In case of 'express', the express stream prescale is returned in the position of the pass-through
777 """
778
779 cursor,schemaname = getTriggerDBCursor(connection)
780
781 tables = { 'S' : 'HLT_PRESCALE_SET'
782 }
783 output = [ 'S.HPS_NAME' ]
784
785 condition = [
786 "S.HPS_ID = '%i'" % psk
787 ]
788
789 res = executeQuery(cursor, output, condition, schemaname, tables)
790
791 name = res[0][0]
792
793 tables = { 'PS': 'HLT_PRESCALE'
794 }
795 output = ['PS.HPR_L2_OR_EF', 'PS.HPR_CHAIN_COUNTER', 'PS.HPR_PRESCALE', 'PS.HPR_PASS_THROUGH_RATE']
796
797 condition = [
798 "PS.HPR_PRESCALE_SET_ID = '%i'" % psk
799 ]
800
801 res = executeQuery(cursor, output, condition, schemaname, tables)
802
803 return name, res
804
805
806def getHLTPrescales(connection,psk):
807 """returns set name, prescale and passthrough values for a
808 given HLT prescale key
809 @connection - connection string, e.g. TRIGGERDB
810 @psk - HLT prescale key
811
812 @return (ps name, [('L2/EF',chainId,prescale,pass-through),...])
813 """
814
815 name, res = queryHLTPrescaleTable(connection,psk)
816
817 return name, [r for r in res if r[0]!='express']
818
819
820def getExpressStreamPrescales(connection,psk):
821 """returns the express stream prescales for a
822 given HLT prescale key
823 @connection - connection string, e.g. TRIGGERDB
824 @psk - HLT prescale key
825
826 @return (ps name, [chainId,prescale),...])
827 """
828
829 name, res = queryHLTPrescaleTable(connection,psk)
830
831 return name, [(r[1],r[3]) for r in res if r[0]=='express']
832
833
834def getHLTPrescalesRun2(connection,psk,smk):
835 """returns set name, prescale and passthrough
836 values for a given HLT prescale key
837 @connection - connection string, e.g. TRIGGERDB
838 @psk - HLT prescale key
839 @smk - Supermaster key
840 @return (ps name, [('L2/EF',chainId,prescale,pass-through),...])
841 """
842
843 res = queryHLTPrescaleTableRun2(connection,psk,smk)
844
845 if res == 0:
846 return res
847
848 return [(r) for r in res if r[3]!='express']
849
850def getExpressStreamPrescalesRun2(connection,psk,smk):
851 """returns the express stream prescales for a given HLT prescale key
852 @connection - connection string, e.g. TRIGGERDB
853 @psk - HLT prescale key
854 @smk - Supermaster key
855 @return (ps name, [chainId,prescale),...])
856 """
857
858 res = queryHLTPrescaleTableRun2(connection,psk, smk)
859
860 if res == 0:
861 return res
862
863 return [(r) for r in res if r[3]=='express']
864
865
866
867def getReRunPrescalesRun2(connection,psk,smk):
868 """returns the express stream prescales for a given HLT prescale key
869 @connection - connection string, e.g. TRIGGERDB
870 @psk - HLT prescale key
871 @smk - Supermaster key
872 @return (ps name, [chainId,prescale),...])
873 """
874
875 res = queryHLTPrescaleTableRun2(connection,psk, smk)
876
877 if res == 0:
878 return res
879
880 return [(r) for r in res if r[2]=='ReRun']
881
882
883
884def queryHLTPrescaleTableRun2(connection,psk,smk):
885
886 prescales = getHLTPrescalesFromSMK(connection, smk)
887
888 valid = False
889 for entry in prescales:
890 if psk in entry:
891 valid = True
892
893 if not valid:
894 log.warning("Selected HLT Prescale Key not associated with Supermaster key")
895 return 0
896
897 cursor,schemaname = getTriggerDBCursor(connection)
898
899 output = [ "HTC.HTC_NAME", "PS.HPR_CHAIN_COUNTER", "PS.HPR_TYPE", "PS.HPR_CONDITION" , "PS.HPR_VALUE"]
900
901 tables = {}
902 tables['PS'] = 'HLT_PRESCALE'
903 tables['HTC'] = 'HLT_TRIGGER_CHAIN'
904 tables['TM2TC'] = 'HLT_TM_TO_TC'
905 tables['HTM'] = 'HLT_TRIGGER_MENU'
906 tables['HMT'] = 'HLT_MASTER_TABLE'
907 tables['SM'] = 'SUPER_MASTER_TABLE'
908
909 condition = [ "PS.HPR_PRESCALE_SET_ID = :psk",
910 "HTC.HTC_CHAIN_COUNTER = PS.HPR_CHAIN_COUNTER",
911 "TM2TC.HTM2TC_TRIGGER_CHAIN_ID = HTC.HTC_ID",
912 "TM2TC.HTM2TC_TRIGGER_MENU_ID = HTM.HTM_ID",
913 "HTM.HTM_ID = HMT.HMT_TRIGGER_MENU_ID",
914 "HMT.HMT_ID = SM.SMT_HLT_MASTER_TABLE_ID",
915 "SM.SMT_ID = %s" % smk
916 ]
917
918 bindvars = { "psk": psk }
919
920 res = executeQuery(cursor, output, condition, schemaname, tables, bindvars)
921
922 return res
923
924def getHLTPrescalesFromSMK(connection, smk):
925
926 cursor,schemaname = getTriggerDBCursor(connection)
927
928 tables = {}
929 tables['HPS'] = 'HLT_PRESCALE_SET'
930 tables['TM2PS'] = 'HLT_TM_TO_PS'
931 tables['HTM'] = 'HLT_TRIGGER_MENU'
932 tables['HMT'] = 'HLT_MASTER_TABLE'
933 tables['SM'] = 'SUPER_MASTER_TABLE'
934
935 output = ['TM2PS.HTM2PS_PRESCALE_SET_ID']
936 condition = [ "TM2PS.HTM2PS_TRIGGER_MENU_ID = HTM.HTM_ID",
937 "HTM.HTM_ID = HMT.HMT_TRIGGER_MENU_ID",
938 "HMT.HMT_ID = SM.SMT_HLT_MASTER_TABLE_ID",
939 "SM.SMT_ID = %s" % smk ]
940
941 hltpsk = executeQuery(cursor, output, condition, schemaname, tables)
942
943 return hltpsk
944
945
946def test():
947 log.setLevel(logging.DEBUG)
948
949
950 print("""####################################
951##
952## Testing ORACLE
953##
954####################################""")
955 l2, ef = getChains("TRIGGERDBREPR", 539)
956 strm_oracle = getStreams("TRIGGERDBREPR", 539)
957 print("\nList of Streams in SMK 539 %s\n" % strm_oracle)
958
959
960 print("""####################################
961##
962## Testing FRONTIER
963##
964####################################""")
965 strm_frontier = getStreams("TRIGGERDBREPR", 539)
966 print("\nList of Streams in SMK 539 %s\n" % strm_frontier)
967
968 if strm_oracle == strm_frontier:
969 print("""####################################
970##
971## ORACLE and FRONTIER give IDENTICAL results
972##
973####################################""")
974 else:
975 print("""####################################
976##
977## ERROR: ORACLE and FRONTIER give DIFFERENT results
978##
979####################################""")
980
981
982def test1():
983 getMenuNameFromDB("TRIGGERDBMC",321)
984
985def test2():
986 log.setLevel(logging.WARNING)
987
988 connections = [ "TRIGGERDB",
989 "TRIGGERDB_RUN1",
990 "TRIGGERDBV1",
991 "TRIGGERDBMC_RUN1",
992 "TRIGGERDBMC",
993 "TRIGGERDBDEV1",
994 "TRIGGERDBDEV2",
995 "TRIGGERDBATN",
996 "TRIGGERDBREPR",
997 "TRIGGERDB_JOERG"
998 ]
999
1000
1001 for c in connections:
1002 print("\nConnecting to alias %s" % c)
1003 cursor, schema = getTriggerDBCursor(c)
1004 if cursor:
1005 print("SUCCESS : %s" % schema)
1006 else:
1007 print("FAILURE")
1008
1009
1010
1011if __name__=="__main__":
1012 import sys
1013 sys.exit(test1())
void print(char *figname, TCanvas *c1)
STL class.
STL class.
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
getExpressStreamPrescales(connection, psk)
getKeysFromName(connection, name, MCOnly=False)
_get_mysql_cursor(host, db, user, passwd="")
getReRunPrescalesRun2(connection, psk, smk)
getL1Prescales(connection, l1prescalekey)
getMenuNameFromDB(connection, hltprescalekey)
getExpressStreamPrescalesRun2(connection, psk, smk)
getChainsWithLowerChainNames(connection, smk)
getAlgorithmsForMenu(connection, smk)
getUsedTables(output, condition, schemaname, tables)
queryHLTPrescaleTable(connection, psk)
queryHLTPrescaleTableRun2(connection, psk, smk)
getKeysFromNameRelease(connection, name, release, l1only)
_getFileLocalOrPath(filename, pathenv)
_get_oracle_cursor(tns, user, passwd="")
interpretConnection(connection, debug=False, resolveAlias=True)
getAlgorithmsForMenuRun2(connection, smk)
getHLTPrescalesRun2(connection, psk, smk)
getChainsAndStreams(connection, smk)
getBunchGroupContent(connection, bgsk)
getPropertyFromDB(connection, smk, component, parameter)
getHLTPrescalesFromSMK(connection, smk)
executeQuery(cursor, output, condition, schemaname, tables, bindvars=())