From 7f680c3b9da3f2831548ffe5f0a949d0f2b209f5 Mon Sep 17 00:00:00 2001
From: Hass AbouZeid <hass.abouzeid@cern.ch>
Date: Thu, 3 Aug 2017 12:52:32 +0000
Subject: [PATCH] Merge branch 'cherry-pick-c1c13f15' into '21.0'

Merge branch '21.1_add_new_trigdb_py_methods' into '21.0'

See merge request !3701
---
 .../python/frontier_client.py                 |  5 +-
 .../TrigConfigSvc/python/TrigConfFrontier.py  | 17 +++-
 .../python/TrigConfigSvcUtils.py              | 83 ++++++++++++++++++-
 3 files changed, 98 insertions(+), 7 deletions(-)

diff --git a/Trigger/TrigConfiguration/TrigConfDBConnection/python/frontier_client.py b/Trigger/TrigConfiguration/TrigConfDBConnection/python/frontier_client.py
index 97f94c21b59..648f944770c 100755
--- a/Trigger/TrigConfiguration/TrigConfDBConnection/python/frontier_client.py
+++ b/Trigger/TrigConfiguration/TrigConfDBConnection/python/frontier_client.py
@@ -83,7 +83,10 @@ class Session:
             self.__fieldNames     += [self.__session.assignString()]
             self.__fieldTypeStr     += [self.__session.assignString()]
             if self.__fieldTypeStr[-1].startswith("NUMBER"):
-                self.__fieldType += [int]
+                if ",0" in .__fieldTypeStr[-1]:
+                    self.__fieldType += [int]
+                else:
+                    self.__fieldType += [float]
             elif self.__fieldTypeStr[-1].startswith("VARCHAR"):
                 self.__fieldType += [str]
             else:
diff --git a/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfFrontier.py b/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfFrontier.py
index 3cc4abbc13c..a63215f09f8 100755
--- a/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfFrontier.py
+++ b/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfFrontier.py
@@ -50,7 +50,10 @@ class FrontierCursor2:
         for var,val in bindvars.items():
             if query.find(":%s" % var)<0:
                 raise NameError("variable '%s' is not a bound variable in this query: %s" % (var, query) )
-            query = query.replace(":%s" % var,"%r" % val)
+            if type(val) == long:
+                query = query.replace(":%s" % var,"%s" % val)
+            else:
+                query = query.replace(":%s" % var,"%r" % val)
             log.debug("Resolving bound variable '%s' with %r" % (var,val))
         log.debug("Resolved query: %s" % query)
         return query
@@ -218,7 +221,17 @@ Refresh cache:  %s""" % (self.url, self.refreshFlag)
                 fields = [x for i,x in enumerate(firstRow.split()) if i%2==0]
                 types = [x for i,x in enumerate(firstRow.split()) if i%2==1]
                 Nfields = len(fields)
-                ptypes = [int if t.startswith("NUMBER") else str for t in types]
+                ptypes = []
+                for t in types:
+                    if t.startswith("NUMBER"):
+                        if ",0" in t:
+                            ptypes.append(int)
+                        else:
+                            ptypes.append(float)
+                    else:
+                        ptypes.append(str)
+
+
                 log.debug("Fields      : %r" % fields)
                 log.debug("DB Types    : %r" % types)
                 log.debug("Python Types: %r" % ptypes)
diff --git a/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfigSvcUtils.py b/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfigSvcUtils.py
index 937b68c1ff0..ae53ebaca92 100644
--- a/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfigSvcUtils.py
+++ b/Trigger/TrigConfiguration/TrigConfigSvc/python/TrigConfigSvcUtils.py
@@ -610,9 +610,15 @@ def getChainsAndStreams(connection, smk):
 
 def getChains(connection, smk):
     cursor,schemaname = getTriggerDBCursor(connection)
-        
-    output = ['TC.HTC_ID', 'TC.HTC_CHAIN_COUNTER', 'TC.HTC_NAME', 'TC.HTC_L2_OR_EF']
+       
+    isrun2 = isRun2(cursor,schemaname)
+
+    output = []
     
+    if isrun2:
+        output = ['TC.HTC_ID', 'TC.HTC_CHAIN_COUNTER', 'TC.HTC_NAME']
+    else:
+        output = ['TC.HTC_ID', 'TC.HTC_CHAIN_COUNTER', 'TC.HTC_NAME', 'TC.HTC_L2_OR_EF']
     tables = {}
     tables['SM']    = 'SUPER_MASTER_TABLE'
     tables['M2C']   = 'HLT_TM_TO_TC'
@@ -631,8 +637,11 @@ def getChains(connection, smk):
     chainsl2 = {}
     chainsef = {}
     for x in res:
-        if x[3]=='L2': chainsl2[x[1]] = x[2]
-        else: chainsef[x[1]] = x[2]
+        if isrun2:
+            chainsef[x[1]] = x[2]
+        else:
+            if x[3]=='L2': chainsl2[x[1]] = x[2]
+            else: chainsef[x[1]] = x[2]
 
     return chainsl2, chainsef
 
@@ -802,6 +811,72 @@ def getExpressStreamPrescales(connection,psk):
     return name, [(r[1],r[3]) for r in res if r[0]=='express']
 
 
+def getHLTPrescalesRun2(connection,psk):
+    """returns set name, prescale and passthrough 
+    values for a given HLT prescale key 
+    @connection - connection string, e.g. TRIGGERDB
+    @psk - HLT prescale key
+    @return (ps name, [('L2/EF',chainId,prescale,pass-through),...])
+    """
+
+    res = queryHLTPrescaleTableRun2(connection,psk)
+
+    return [(r) for r in res if r[3]!='express']
+
+def getExpressStreamPrescalesRun2(connection,psk):
+    """returns the express stream prescales for a given HLT prescale key
+    @connection - connection string, e.g. TRIGGERDB
+    @psk - HLT prescale key
+    @return (ps name, [chainId,prescale),...])
+    """
+
+    res = queryHLTPrescaleTableRun2(connection,psk)
+
+    return [(r) for r in res if r[3]=='express']
+
+
+def queryHLTPrescaleTableRun2(connection,psk):
+
+    cursor,schemaname = getTriggerDBCursor(connection)
+
+    output = [ "HTC.HTC_NAME", "PS.HPR_CHAIN_COUNTER", "PS.HPR_TYPE", "PS.HPR_CONDITION" , "PS.HPR_VALUE"]
+
+    tables = {}
+    tables['PS'] = 'HLT_PRESCALE' 
+    tables['HTC'] = 'HLT_TRIGGER_CHAIN'
+
+    condition = [ "PS.HPR_PRESCALE_SET_ID = :psk",
+                  "HTC.HTC_CHAIN_COUNTER = PS.HPR_CHAIN_COUNTER"
+                  ]
+
+    bindvars = { "psk": psk }
+
+    res = executeQuery(cursor, output, condition, schemaname, tables, bindvars)
+
+    return res
+
+def getHLTPrescalesFromSMK(connection, smk):
+
+    cursor,schemaname = getTriggerDBCursor(connection)
+
+    tables = {}
+    tables['HPS']   = 'HLT_PRESCALE_SET'
+    tables['TM2PS'] = 'HLT_TM_TO_PS'
+    tables['HTM']  = 'HLT_TRIGGER_MENU'
+    tables['HMT']    = 'HLT_MASTER_TABLE'
+    tables['SM']    = 'SUPER_MASTER_TABLE'
+
+    output = ['TM2PS.HTM2PS_PRESCALE_SET_ID']
+    condition = [ "TM2PS.HTM2PS_TRIGGER_MENU_ID = HTM.HTM_ID",
+                  "HTM.HTM_ID = HMT.HMT_TRIGGER_MENU_ID",
+                  "HMT.HMT_ID = SM.SMT_HLT_MASTER_TABLE_ID",
+                  "SM.SMT_ID = %s" % smk ]
+
+    hltpsk  = executeQuery(cursor, output, condition, schemaname, tables)
+
+    return hltpsk
+
+
 def test():
     log.setLevel(logging.DEBUG)
 
-- 
GitLab