CMS 3D CMS Logo

AlCaHLTBitMon_QueryRunRegistry.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 #-----------------------------------------------------
4 # original author: Andrea Lucaroni
5 # Revision: $Revision: 1.3 $
6 # Last update: $Date: 2012/05/22 08:22:04 $
7 # by: $Author: taroni $
8 #-----------------------------------------------------
9 
10 from __future__ import print_function
11 from xml.dom import minidom
12 import re
13 import json
14 import os
15 import stat
16 import sys
17 
18 #include DBS
19 from DBSAPI.dbsApiException import DbsException
20 import DBSAPI.dbsApi
21 from DBSAPI.dbsApiException import *
22 
23 # include XML-RPC client library
24 # RR API uses XML-RPC webservices interface for data access
25 import xmlrpclib
26 
27 import array
28 import pickle as pk
29 
30 from optparse import OptionParser
31 
32 DEBUG=0
33 import inspect
34 
35 def lineno():
36  """Returns the current line number in our program."""
37  return inspect.currentframe().f_back.f_lineno
38 
39 #size file
40 def filesize1(n):
41  info = os.stat(n)
42  sz = info[stat.ST_SIZE]
43  return sz
44 
45 
46 def printLumi(file,namefile):
47  if(filesize1(file) != 0):
48  string= "lumiCalc2.py -c frontier://LumiCalc/CMS_LUMI_PROD -i "
49  string1= " --nowarning overview >"
50  string2= string + file + string1 + namefile
51  data = os.system(string2)
52  else:
53  data = ""
54  print("0 lumi are not avaible")
55  return data
56 
57 
58 def DBSquery(dataset,site,run):
59 
60  url = "http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
61  if DEBUG:
62  print(lineno())
63  args = {}
64  args['url'] = url
65  args['level'] = 'CRITICAL'
66  if DEBUG:
67  print(lineno())
68  api = DBSAPI.dbsApi.DbsApi(args)
69  if DEBUG:
70  print(lineno())
71  try:
72  files = api.listFiles(path=dataset,tier_list =site,runNumber=run)
73  except DbsApiException as ex:
74  print("Caught API Exception %s: %s " % (ex.getClassName(), ex.getErrorMessage() ))
75  files = ""
76  if ex.getErrorCode() not in (None, ""):
77  print("DBS Exception Error Code: ", ex.getErrorCode())
78  if DEBUG:
79  print(lineno())
80  return files
81 
82 
83 def makecff(file_list,namefile):
84  file1 = open(namefile ,'w')
85  stringS = "process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )\n"
86  stringS = stringS + "readFiles = cms.untracked.vstring()\n"
87  stringS = stringS + "secFiles = cms.untracked.vstring()\n"
88  stringS = stringS + "\n"
89  file1.write(stringS)
90 
91  filecount = 0
92  extendOpen = 0
93  for filename in file_list:
94 
95  if extendOpen == 0:
96  stringS = "readFiles.extend([\n"
97  file1.write(stringS)
98  extendOpen = 1
99 
100  stringS = " '"
101  stringS = stringS + str(filename)
102  stringS = stringS + "',\n"
103  file1.write(stringS)
104  filecount = filecount + 1
105  if filecount == 50:
106  stringS = "])\n\n"
107  file1.write(stringS)
108  filecount = 0
109  extendOpen = 0
110 
111  if extendOpen == 1:
112  stringS = "])\n\n"
113  file1.write(stringS)
114 
115  stringS = "process.source = cms.Source(\"PoolSource\",\n"
116  stringS = stringS + " fileNames = readFiles,\n"
117  stringS = stringS + " secondaryFileNames = secFiles\n"
118  stringS = stringS + ")\n"
119  file1.write(stringS)
120  file1.close()
121 
122 
124  parser = OptionParser()
125  parser.add_option("-w", "--workspace",
126  dest="workspaceName",
127  default="GLOBAL",
128  help="define workspace: GLOBAL TRACKER ")
129 
130  parser.add_option("-r", "--regexp",
131  dest="regexp",
132  type="string",
133  default='groupName : LIKE %Collisions12% , runNumber : = 190000 ',
134  help=" \"{runNumber} >= 148127 and {runNumber} < 148128 \" ")
135 
136  parser.add_option("-d", "--datasetPath",
137  dest="dataset", \
138  default="/MinimumBias/Run2010A-TkAlMinBias-Dec22ReReco_v1/ALCARECO",
139  help="For example : --datasetPath /MinimumBias/Run2010A-TkAlMinBias-Dec22ReReco_v1/ALCARECO")
140 
141  parser.add_option("-s", "--site",
142  dest="site",
143  default="T2_CH_CAF",
144  help="For example : site T2_CH_CAF")
145 
146  parser.add_option("-i", "--info",
147  action="store_true",
148  dest="info",
149  default=False,
150  help="printout the column names on which it's possible to cut")
151 
152  (options, args) = parser.parse_args()
153  if len(sys.argv) == 1:
154  print("\nUsage: %s --help"%sys.argv[0])
155  sys.exit(0)
156 
157  return options
158 
159 
160 def serverQuery(workspaceName,regexp):
161 
162  # get handler to RR XML-RPC server
163  server = xmlrpclib.ServerProxy('http://cms-service-runregistry-api.web.cern.ch/cms-service-runregistry-api/xmlrpc')
164  if DEBUG:
165  print(lineno(), regexp)
166  data = server.RunDatasetTable.export(workspaceName,'xml_all' ,regexp)
167  return data
168 
169 #----------------------------------------------------
170 
171 def getText(nodelist):
172  rc = ""
173  for node in nodelist:
174  if node.nodeType == node.TEXT_NODE:
175  rc = rc + node.data
176  return rc
177 
178 def getElement(obj,name):
179  return obj.getElementsByTagName(name)
180 
181 def printObj(obj,name):
182  return getText(getElement(obj,name)[0].childNodes)
183 
184 
185 def getData(doc,options,dataset,site):
186  if DEBUG:
187  print(lineno(), 'getData')
188  server = xmlrpclib.ServerProxy('http://cms-service-runregistry-api.web.cern.ch/cms-service-runregistry-api/xmlrpc')
189  runs = getElement(doc,'RUN')
190  txtLongData=""
191  txtkey=""
192  lista=[]
193 
194  sep="\t"
195 
196  for run in runs:
197  txtrun=printObj(run,'NUMBER') + sep + printObj(run,'HLTKEY')
198  txtLongData+= txtrun + sep + "\n"
199  for run in runs:
200  test=printObj(run,'HLTKEY')
201  if not (test in lista):
202  lista.append(test)
203 
204  file2=open("lista_key.txt",'w')
205  for pkey in range(len(lista)):
206  pwkey = lista[pkey] +"\n"
207  file2.write(pwkey)
208  if DEBUG:
209  print(lineno(), lista[pkey])
210 
211  file2.close()
212 
213  for i in range(len(lista)):
214  if DEBUG:
215  print(lineno(), lista[i])
216  nameDBS=""
217  nameDBS=str(i)+".data"
218  name=""
219  name=str(i)+".json"
220  nameLumi=""
221  nameLumi=str(i)+".lumi"
222  file1 = open( name ,'w')
223  listaDBS = []
224  if DEBUG:
225  print(lineno(), nameDBS)
226  for run in runs:
227  key=printObj(run,'HLTKEY')
228  if (key == lista[i]):
229  print("running......", key)
230  if DEBUG:
231  print(lineno(), printObj(run,'NUMBER'))
232  txtruns = "{runNumber} >= " + printObj(run,'NUMBER') + " and {runNumber} <= " + str(int(printObj(run,'NUMBER')))
233  txtriv = txtruns + " and {cmpDcsBPix} = 1 and {cmpDcsFPix} = 1 and {cmpDcsTibTid} = 1 and {cmpDcsTob} = 1 and {cmpDcsTecM} = 1 and {cmpDcsTecP} = 1"
234  lumirun = server.RunLumiSectionRangeTable.export('GLOBAL', 'json',txtriv)
235  if DEBUG:
236  print(lineno(), lumirun)
237 
238  if lumirun:
239  file = DBSquery(dataset,site,str(printObj(run,'NUMBER')))
240 
242  if (file != "") :
243  for uno in file:
244  stringDBS = {}
245  stringDBS = uno['LogicalFileName']
246  listaDBS += [stringDBS]
247  if DEBUG:
248  print(lineno(), lumirun)
249  comp="{}"
250  if (lumirun == comp):
251  print("LUMI ZERO")
252  else:
253  file1.write(lumirun)
254 
255  file1.close()
256  string=""
257  string="sed -i 's/}{/, /g'"
258  string2=""
259  string2= string + " " + name
260  os.system(string2)
261  printLumi(name,nameLumi)
262  os.system("sed -i 's/\//_/g' lista_key.txt")
263  listaDBS2 =[]
264  for rootLSF in listaDBS:
265  if not (rootLSF in listaDBS2):
266  listaDBS2.append(rootLSF)
267  makecff(listaDBS2,nameDBS)
268 
269  return txtLongData
270 
271 #---------------------------------------------
272 
273 def extractData(mode,reg,dataset,site,options):
274  doc = minidom.parseString(serverQuery(mode,reg))
275  return getData(doc,options,dataset,site)
276 
277 def getRegExp(regexp):
278  items = regexp.split(',')
279  dd = {}
280  for item in items:
281  key,value = item.split(':')
282  dd[key.replace(' ','')] = value
283  return dd
284 
285 
286 #---------------------------------------------MAIN
287 
288 options = defineOptions()
289 data=extractData(options.workspaceName,options.regexp,options.dataset,options.site,options)
290 
def getData(doc, options, dataset, site)
def makecff(file_list, namefile)
file cff data
def DBSquery(dataset, site, run)
file dbs
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def extractData(mode, reg, dataset, site, options)
#define str(s)
if(threadIdxLocalY==0 &&threadIdxLocalX==0)