Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:24

0001 #!/usr/bin/python3
0002 import cx_Oracle
0003 import argparse
0004 import pprint
0005 import json
0006 import os
0007 
0008 RUNMIN = os.getenv("HCALDQM_RUNMIN")
0009 conn = os.getenv("HCALDQM_DBCONNECT")
0010 level1= ['HCAL_LEVEL_1']
0011 names = ['HCAL_HBHE', 'HCAL_HF', 'HCAL_HO',  'HCAL_LEVEL_1', 'HCAL_HBHE904', 'HCAL_HF904']
0012 
0013 ngrbxidmap="/nfshome0/akhukhun/hcaldqm/config/id2sn_rmcu.json"
0014 ngccmidmap="/nfshome0/akhukhun/hcaldqm/config/id2sn_ngccm.json"
0015 ngqieidmap="/nfshome0/akhukhun/hcaldqm/config/id2sn_qie.json"
0016 
0017 def listRuns(runmin, runmax):
0018     n=5
0019     db = cx_Oracle.connect(conn)
0020     cursor = db.cursor()
0021     p=dict();
0022     sql="select runnumber, string_value, name from  runsession_parameter where runnumber > {} and runnumber < {} and (".format(runmin, runmax)
0023     sql = sql + " or ".join(["name=:key"+str(i) for i in range(n*len(level1))])
0024     sql = sql + ") order by runnumber" 
0025     for i in range(len(level1)): 
0026         p["key"+str(n*i)]='CMS.'+level1[i]+":HCAL_TIME_OF_FM_START"
0027         p["key"+str(n*i+1)]='CMS.'+level1[i]+":FM_FULLPATH"
0028         p["key"+str(n*i+2)]='CMS.'+level1[i]+":LOCAL_RUNKEY_SELECTED"
0029         p["key"+str(n*i+3)]='CMS.'+level1[i]+":LOCAL_RUN_KEY" #for older runs
0030         p["key"+str(n*i+4)]='CMS.'+level1[i]+":EVENTS_TAKEN"
0031     cursor = cursor.execute(sql, p)
0032     out={}
0033     maxfm=0
0034     maxn=0
0035     for row in cursor: 
0036         k = row[2]
0037         n = row[1]
0038         r = row[0]
0039         if r not in out: out[r] = dict(time="", nevents=-1, fm="", key="")
0040         if(k.endswith("HCAL_TIME_OF_FM_START")): out[r]["time"]=n
0041         elif(k.endswith("FM_FULLPATH")): 
0042             fm=n.split("/")[-1]
0043             out[r]["fm"]=fm
0044             if(len(fm)>maxfm): maxfm=len(fm)
0045         elif(k.endswith("EVENTS_TAKEN")): 
0046             out[r]["nevents"] = int(n)
0047             if(len(n)>maxn): maxn = len(n)
0048         else: out[r]["key"]=n
0049     form="%s | %-24s | %{}d | %-{}s | %s".format(maxn, maxfm)
0050     for r,i in sorted(out.items()): print (form % (r, i["time"], i["nevents"], i["fm"], i["key"]))
0051     cursor.close()
0052     db.close()
0053 
0054 def dumpAvailableKeys(run):
0055     db = cx_Oracle.connect(conn)
0056     cursor = db.cursor()
0057     sql="select name from runsession_parameter where runnumber=:run"
0058     p=dict(run=run)
0059     cursor = cursor.execute(sql, p)
0060     res = set();
0061     for row in cursor: 
0062         res.add(row[0]);
0063     cursor.close()
0064     db.close()
0065     for i in sorted(res): print(i)
0066 
0067 def read(run, key):
0068     db = cx_Oracle.connect(conn)
0069     cursor = db.cursor()
0070     p=dict(run=run)
0071     OR= " or ".join(["name=:key"+str(i) for i in range(len(names))])
0072     sql= "".join(["select value from runsession_string where runsession_parameter_id=any(select id from runsession_parameter where (runnumber=:run and (", OR, " )))"])
0073     for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+key
0074     cursor = cursor.execute(sql, p)
0075     result = (cursor.fetchone()[0]).read()
0076     cursor.close();
0077     db.close()
0078     return result
0079 
0080 def dumpDate(run):
0081     date = read(run, "HCAL_TIME_OF_FM_START")
0082     v = date.split()[0].split("-") 
0083     m = int(v[1])
0084     d = int(v[2])
0085     month = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
0086     print("%s%02d" % (month[m-1], d))
0087 
0088 def dumpInfo(run):
0089     db = cx_Oracle.connect(conn)
0090     cursor = db.cursor()
0091     p=dict(run=run)
0092     OR= " or ".join(["name like :key"+str(i) for i in range(len(names))])
0093     sql= "".join(["select name, string_value from runsession_parameter where runnumber=:run and (", OR, " )"])
0094     for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+"ngRBXManager_Firmware_UniqueIDs_H%"
0095     cursor = cursor.execute(sql, p)
0096 
0097     row = cursor.fetchone()
0098     out = dict()
0099     while row:
0100         key="{0} ".format(str(row[0]).split("_")[-1])
0101         result = str(row[1])
0102         array = result.split(";")
0103         out[key]=array
0104         row = cursor.fetchone()
0105     for key in sorted(out):
0106         print ("-----------------------\n{0} ".format(key))
0107         for l in out[key]: print (l)
0108 
0109     cursor.close();
0110     db.close()
0111 
0112 def dumpIDs(run, isqie):
0113     db = cx_Oracle.connect(conn)
0114     cursor = db.cursor()
0115     p=dict(run=run)
0116     OR= " OR ".join(["name like :key"+str(i) for i in range(len(names))])
0117     sql= "".join(["select name, string_value from runsession_parameter where runnumber=:run and (", OR, " )"])
0118     for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+"ngRBXManager_Firmware_UniqueIDs_H%"
0119     cursor = cursor.execute(sql, p)
0120 
0121     row = cursor.fetchone()
0122     out = dict()
0123     while row:
0124         rbx = "{0} ".format(str(row[0]).split("_")[-1])
0125         n = 2
0126         if 'M' in rbx: n = 3;
0127         if 'P' in rbx: n = 3;
0128         key = "{:s}{:02d}".format(rbx[:n], int(rbx[n:])) 
0129         
0130         result = str(row[1])
0131         array = result.split(";")
0132         value=[]
0133         for line in array: 
0134             rl={}
0135             w = line.split()
0136             n = len(w);
0137             if isqie and w[0]=="QCard": 
0138                 rl['id0'] = w[6]
0139                 rl['id'] = w[7][:-2]
0140                 rl['rm'] = int(w[3][-1])
0141                 rl['qie'] = int(w[1])
0142             if isqie and w[0]=="CU": 
0143                 rl['id0'] = w[3] 
0144                 rl['id'] = w[4][:-2] 
0145                 rl['rm'] = 5
0146                 rl['qie'] = 1
0147             if not isqie and (w[0].startswith("ngCCMa") or w[0].startswith("ngCCMb")) and w[2]=="UID:": 
0148                 rl['id0'] = (w[11][1:], "-1")["ERROR" in line]
0149                 rl['id'] = (w[12][:-1], "-1")["ERROR" in line]
0150                 rl['rm'] = 0
0151                 rl['qie'] = 0
0152             if(rl): value.append(rl)
0153         if key.startswith("HB") or key.startswith("HE"):
0154             out[key] = {"value":value, "rbx":rbx}
0155         row = cursor.fetchone()
0156 
0157 
0158     #load id2sn
0159     data={}
0160     json_data=open(ngqieidmap).read()
0161     data.update(json.loads(json_data))
0162 
0163     for key in sorted(out):
0164         for q in out[key]["value"]:
0165             id0=q['id0']
0166             if out[key]["rbx"].startswith('HE'):
0167                 nid0=int(id0, 16);
0168                 id0='0x'+'%08x' % nid0
0169 
0170             k='_'.join((id0, q['id']))
0171             v=-1
0172             if k in data: v=data[k]
0173             print ("%-5s %d %d %-11s %-10s %6d" % (out[key]["rbx"], q['rm'], q['qie'], id0, q['id'], v))
0174 
0175     cursor.close();
0176     db.close()
0177 
0178 def dumpSNs(run):
0179     db = cx_Oracle.connect(conn)
0180     cursor = db.cursor()
0181     p=dict(run=run)
0182     OR= " OR ".join(["name like :key"+str(i) for i in range(len(names))])
0183     sql= "".join(["select name, string_value from runsession_parameter where runnumber=:run and (", OR, " )"])
0184     for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+"ngRBXManager_Firmware_UniqueIDs_HB%"
0185     cursor = cursor.execute(sql, p)
0186 
0187     row = cursor.fetchone()
0188     out = dict()
0189     hasRMinfo=False
0190     hasCCMinfo=False
0191     while row:
0192         rbx = "{0} ".format(str(row[0]).split("_")[-1])
0193         n = 2;
0194         if 'M' in rbx: n = 3;
0195         if 'P' in rbx: n = 3;
0196         key = "{:s}{:02d}".format(rbx[:n], int(rbx[n:])) 
0197 
0198         result = str(row[1])
0199         array = result.split(";")
0200         valueCCM=""
0201         value=""
0202         i=0
0203         for line in array: 
0204             w = line.split()
0205             n = len(w);
0206             if w[0]=="QCard": 
0207                 value += "{0}{1}".format(w[7][:-2], ("", " ")[i==3])
0208                 if(i==3): i=0
0209                 else: i=i+1
0210                 hasRMinfo=True
0211             if w[0]=="CU": value += " {0}".format(w[4][:-2]) 
0212             if w[0].startswith("ngCCM") and w[2]=="UID:": 
0213                 valueCCM += " {0}0{1} ".format(w[11][1:], w[12][2:-1]) 
0214                 hasCCMinfo=True
0215         out[key] = {"value":value+valueCCM, "rbx":rbx}
0216         row = cursor.fetchone()
0217 
0218     data={}
0219     for f in (ngrbxidmap, ngccmidmap): 
0220         json_data=open(f).read()
0221         data.update(json.loads(json_data))
0222     if(hasRMinfo and hasCCMinfo): print ("RBX   |  RM1  RM2  RM3  RM4   CU CCMa CCMb\n------|-----------------------------------")
0223     elif hasRMinfo: print ("RBX   |  RM1  RM2  RM3  RM4   CU\n------|-------------------------") 
0224     else: print("Cannot find published information on RM IDs."); return 
0225     for key in sorted(out):
0226         res = ""
0227         for k in out[key]["value"].split():
0228             if data.has_key(k): res = res + "{:4d} ".format(data[k])
0229             elif "NAC" in k: res = res + "   0 "
0230             else: res = res + "  -1 "
0231         print ("%-5s | %s" % (out[key]["rbx"].strip(), res))
0232 
0233     cursor.close();
0234     db.close()
0235 
0236         
0237 
0238 if __name__=="__main__":
0239     parser = argparse.ArgumentParser(description="With no arguments, return list of runs with a small summary after runmin={}".format(RUNMIN))
0240     parser.add_argument("--run", help="run number", type=int);
0241     parser.add_argument("--runmin", help="earliest run number", type=int, default=RUNMIN);
0242     parser.add_argument("--runmax", help="earliest run number", type=int, default=99999999999999999);
0243     parser.add_argument("--key", help="get value for a given key; special values are: sn, qie, ccm, date");
0244     a=parser.parse_args();
0245     if(a.run):
0246         if(a.key=="qie"): dumpIDs(a.run, True)
0247         elif(a.key=="ccm"): dumpIDs(a.run, False)
0248         elif(a.key=="raw"): dumpInfo(a.run)
0249         elif(a.key=="sn"): dumpSNs(a.run)
0250         elif(a.key=="date"): dumpDate(a.run)
0251         elif(a.key): print(read(a.run, a.key))
0252         else: dumpAvailableKeys(a.run)
0253     else:
0254         listRuns(a.runmin, a.runmax)