CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
getInfo.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 import cx_Oracle
3 import argparse
4 import pprint
5 import json
6 import os
7 
8 RUNMIN = os.getenv("HCALDQM_RUNMIN")
9 conn = os.getenv("HCALDQM_DBCONNECT")
10 level1= ['HCAL_LEVEL_1']
11 names = ['HCAL_HBHE', 'HCAL_HF', 'HCAL_HO', 'HCAL_LEVEL_1', 'HCAL_HBHE904', 'HCAL_HF904']
12 
13 ngrbxidmap="/nfshome0/akhukhun/hcaldqm/config/id2sn_rmcu.json"
14 ngccmidmap="/nfshome0/akhukhun/hcaldqm/config/id2sn_ngccm.json"
15 ngqieidmap="/nfshome0/akhukhun/hcaldqm/config/id2sn_qie.json"
16 
17 def listRuns(runmin, runmax):
18  n=5
19  db = cx_Oracle.connect(conn)
20  cursor = db.cursor()
21  p=dict();
22  sql="select runnumber, string_value, name from runsession_parameter where runnumber > {} and runnumber < {} and (".format(runmin, runmax)
23  sql = sql + " or ".join(["name=:key"+str(i) for i in range(n*len(level1))])
24  sql = sql + ") order by runnumber"
25  for i in range(len(level1)):
26  p["key"+str(n*i)]='CMS.'+level1[i]+":HCAL_TIME_OF_FM_START"
27  p["key"+str(n*i+1)]='CMS.'+level1[i]+":FM_FULLPATH"
28  p["key"+str(n*i+2)]='CMS.'+level1[i]+":LOCAL_RUNKEY_SELECTED"
29  p["key"+str(n*i+3)]='CMS.'+level1[i]+":LOCAL_RUN_KEY" #for older runs
30  p["key"+str(n*i+4)]='CMS.'+level1[i]+":EVENTS_TAKEN"
31  cursor = cursor.execute(sql, p)
32  out={}
33  maxfm=0
34  maxn=0
35  for row in cursor:
36  k = row[2]
37  n = row[1]
38  r = row[0]
39  if r not in out: out[r] = dict(time="", nevents=-1, fm="", key="")
40  if(k.endswith("HCAL_TIME_OF_FM_START")): out[r]["time"]=n
41  elif(k.endswith("FM_FULLPATH")):
42  fm=n.split("/")[-1]
43  out[r]["fm"]=fm
44  if(len(fm)>maxfm): maxfm=len(fm)
45  elif(k.endswith("EVENTS_TAKEN")):
46  out[r]["nevents"] = int(n)
47  if(len(n)>maxn): maxn = len(n)
48  else: out[r]["key"]=n
49  form="%s | %-24s | %{}d | %-{}s | %s".format(maxn, maxfm)
50  for r,i in sorted(out.items()): print (form % (r, i["time"], i["nevents"], i["fm"], i["key"]))
51  cursor.close()
52  db.close()
53 
55  db = cx_Oracle.connect(conn)
56  cursor = db.cursor()
57  sql="select name from runsession_parameter where runnumber=:run"
58  p=dict(run=run)
59  cursor = cursor.execute(sql, p)
60  res = set();
61  for row in cursor:
62  res.add(row[0]);
63  cursor.close()
64  db.close()
65  for i in sorted(res): print(i)
66 
67 def read(run, key):
68  db = cx_Oracle.connect(conn)
69  cursor = db.cursor()
70  p=dict(run=run)
71  OR= " or ".join(["name=:key"+str(i) for i in range(len(names))])
72  sql= "".join(["select value from runsession_string where runsession_parameter_id=any(select id from runsession_parameter where (runnumber=:run and (", OR, " )))"])
73  for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+key
74  cursor = cursor.execute(sql, p)
75  result = (cursor.fetchone()[0]).read()
76  cursor.close();
77  db.close()
78  return result
79 
80 def dumpDate(run):
81  date = read(run, "HCAL_TIME_OF_FM_START")
82  v = date.split()[0].split("-")
83  m = int(v[1])
84  d = int(v[2])
85  month = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
86  print("%s%02d" % (month[m-1], d))
87 
88 def dumpInfo(run):
89  db = cx_Oracle.connect(conn)
90  cursor = db.cursor()
91  p=dict(run=run)
92  OR= " or ".join(["name like :key"+str(i) for i in range(len(names))])
93  sql= "".join(["select name, string_value from runsession_parameter where runnumber=:run and (", OR, " )"])
94  for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+"ngRBXManager_Firmware_UniqueIDs_H%"
95  cursor = cursor.execute(sql, p)
96 
97  row = cursor.fetchone()
98  out = dict()
99  while row:
100  key="{0} ".format(str(row[0]).split("_")[-1])
101  result = str(row[1])
102  array = result.split(";")
103  out[key]=array
104  row = cursor.fetchone()
105  for key in sorted(out):
106  print ("-----------------------\n{0} ".format(key))
107  for l in out[key]: print (l)
108 
109  cursor.close();
110  db.close()
111 
112 def dumpIDs(run, isqie):
113  db = cx_Oracle.connect(conn)
114  cursor = db.cursor()
115  p=dict(run=run)
116  OR= " OR ".join(["name like :key"+str(i) for i in range(len(names))])
117  sql= "".join(["select name, string_value from runsession_parameter where runnumber=:run and (", OR, " )"])
118  for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+"ngRBXManager_Firmware_UniqueIDs_H%"
119  cursor = cursor.execute(sql, p)
120 
121  row = cursor.fetchone()
122  out = dict()
123  while row:
124  rbx = "{0} ".format(str(row[0]).split("_")[-1])
125  n = 2
126  if 'M' in rbx: n = 3;
127  if 'P' in rbx: n = 3;
128  key = "{:s}{:02d}".format(rbx[:n], int(rbx[n:]))
129 
130  result = str(row[1])
131  array = result.split(";")
132  value=[]
133  for line in array:
134  rl={}
135  w = line.split()
136  n = len(w);
137  if isqie and w[0]=="QCard":
138  rl['id0'] = w[6]
139  rl['id'] = w[7][:-2]
140  rl['rm'] = int(w[3][-1])
141  rl['qie'] = int(w[1])
142  if isqie and w[0]=="CU":
143  rl['id0'] = w[3]
144  rl['id'] = w[4][:-2]
145  rl['rm'] = 5
146  rl['qie'] = 1
147  if not isqie and (w[0].startswith("ngCCMa") or w[0].startswith("ngCCMb")) and w[2]=="UID:":
148  rl['id0'] = (w[11][1:], "-1")["ERROR" in line]
149  rl['id'] = (w[12][:-1], "-1")["ERROR" in line]
150  rl['rm'] = 0
151  rl['qie'] = 0
152  if(rl): value.append(rl)
153  if key.startswith("HB") or key.startswith("HE"):
154  out[key] = {"value":value, "rbx":rbx}
155  row = cursor.fetchone()
156 
157 
158  #load id2sn
159  data={}
160  json_data=open(ngqieidmap).read()
161  data.update(json.loads(json_data))
162 
163  for key in sorted(out):
164  for q in out[key]["value"]:
165  id0=q['id0']
166  if out[key]["rbx"].startswith('HE'):
167  nid0=int(id0, 16);
168  id0='0x'+'%08x' % nid0
169 
170  k='_'.join((id0, q['id']))
171  v=-1
172  if k in data: v=data[k]
173  print ("%-5s %d %d %-11s %-10s %6d" % (out[key]["rbx"], q['rm'], q['qie'], id0, q['id'], v))
174 
175  cursor.close();
176  db.close()
177 
178 def dumpSNs(run):
179  db = cx_Oracle.connect(conn)
180  cursor = db.cursor()
181  p=dict(run=run)
182  OR= " OR ".join(["name like :key"+str(i) for i in range(len(names))])
183  sql= "".join(["select name, string_value from runsession_parameter where runnumber=:run and (", OR, " )"])
184  for i in range(len(names)): p["key"+str(i)]='CMS.'+names[i]+":"+"ngRBXManager_Firmware_UniqueIDs_HB%"
185  cursor = cursor.execute(sql, p)
186 
187  row = cursor.fetchone()
188  out = dict()
189  hasRMinfo=False
190  hasCCMinfo=False
191  while row:
192  rbx = "{0} ".format(str(row[0]).split("_")[-1])
193  n = 2;
194  if 'M' in rbx: n = 3;
195  if 'P' in rbx: n = 3;
196  key = "{:s}{:02d}".format(rbx[:n], int(rbx[n:]))
197 
198  result = str(row[1])
199  array = result.split(";")
200  valueCCM=""
201  value=""
202  i=0
203  for line in array:
204  w = line.split()
205  n = len(w);
206  if w[0]=="QCard":
207  value += "{0}{1}".format(w[7][:-2], ("", " ")[i==3])
208  if(i==3): i=0
209  else: i=i+1
210  hasRMinfo=True
211  if w[0]=="CU": value += " {0}".format(w[4][:-2])
212  if w[0].startswith("ngCCM") and w[2]=="UID:":
213  valueCCM += " {0}0{1} ".format(w[11][1:], w[12][2:-1])
214  hasCCMinfo=True
215  out[key] = {"value":value+valueCCM, "rbx":rbx}
216  row = cursor.fetchone()
217 
218  data={}
219  for f in (ngrbxidmap, ngccmidmap):
220  json_data=open(f).read()
221  data.update(json.loads(json_data))
222  if(hasRMinfo and hasCCMinfo): print ("RBX | RM1 RM2 RM3 RM4 CU CCMa CCMb\n------|-----------------------------------")
223  elif hasRMinfo: print ("RBX | RM1 RM2 RM3 RM4 CU\n------|-------------------------")
224  else: print("Cannot find published information on RM IDs."); return
225  for key in sorted(out):
226  res = ""
227  for k in out[key]["value"].split():
228  if data.has_key(k): res = res + "{:4d} ".format(data[k])
229  elif "NAC" in k: res = res + " 0 "
230  else: res = res + " -1 "
231  print ("%-5s | %s" % (out[key]["rbx"].strip(), res))
232 
233  cursor.close();
234  db.close()
235 
236 
237 
238 if __name__=="__main__":
239  parser = argparse.ArgumentParser(description="With no arguments, return list of runs with a small summary after runmin={}".format(RUNMIN))
240  parser.add_argument("--run", help="run number", type=int);
241  parser.add_argument("--runmin", help="earliest run number", type=int, default=RUNMIN);
242  parser.add_argument("--runmax", help="earliest run number", type=int, default=99999999999999999);
243  parser.add_argument("--key", help="get value for a given key; special values are: sn, qie, ccm, date");
244  a=parser.parse_args();
245  if(a.run):
246  if(a.key=="qie"): dumpIDs(a.run, True)
247  elif(a.key=="ccm"): dumpIDs(a.run, False)
248  elif(a.key=="raw"): dumpInfo(a.run)
249  elif(a.key=="sn"): dumpSNs(a.run)
250  elif(a.key=="date"): dumpDate(a.run)
251  elif(a.key): print(read(a.run, a.key))
252  else: dumpAvailableKeys(a.run)
253  else:
254  listRuns(a.runmin, a.runmax)
def dumpDate
Definition: getInfo.py:80
const uint16_t range(const Frame &aFrame)
def dumpAvailableKeys
Definition: getInfo.py:54
def dumpSNs
Definition: getInfo.py:178
def read
Definition: getInfo.py:67
def dumpInfo
Definition: getInfo.py:88
if(conf_.getParameter< bool >("UseStripCablingDB"))
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def listRuns
Definition: getInfo.py:17
#define str(s)
def dumpIDs
Definition: getInfo.py:112