CMS 3D CMS Logo

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