CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/Alignment/CommonAlignmentProducer/scripts/AlCaHLTBitMon_QueryRunRegistry.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #-----------------------------------------------------
00004 # original author: Andrea Lucaroni
00005 # Revision:        $Revision: 1.4 $
00006 # Last update:     $Date: 2012/05/24 13:00:56 $
00007 # by:              $Author: taroni $
00008 #-----------------------------------------------------
00009 
00010 from xml.dom import minidom
00011 import re
00012 import json
00013 import os 
00014 import stat
00015 import sys
00016 
00017 #include DBS
00018 from DBSAPI.dbsApiException import DbsException
00019 import DBSAPI.dbsApi
00020 from DBSAPI.dbsApiException import *
00021 
00022 # include XML-RPC client library
00023 # RR API uses XML-RPC webservices interface for data access
00024 import xmlrpclib
00025 
00026 import array
00027 import pickle as pk
00028 
00029 from optparse import OptionParser
00030 #####DEBUG
00031 DEBUG=0
00032 import inspect
00033 
00034 def lineno():
00035     """Returns the current line number in our program."""
00036     return inspect.currentframe().f_back.f_lineno
00037 
00038 #size file
00039 def filesize1(n):    
00040     info = os.stat(n)
00041     sz = info[stat.ST_SIZE]
00042     return sz
00043             
00044 ### lumiCalc
00045 def printLumi(file,namefile):
00046     if(filesize1(file) != 0):
00047         string= "lumiCalc2.py -c frontier://LumiCalc/CMS_LUMI_PROD -i "
00048         string1= " --nowarning overview >"
00049         string2= string + file + string1 + namefile
00050         data = os.system(string2)
00051     else:
00052         data = ""
00053         print "0 lumi are not avaible"
00054     return data
00055 
00056 ###file  dbs
00057 def DBSquery(dataset,site,run):
00058 
00059     url = "https://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
00060     if DEBUG:
00061         print lineno()
00062     args = {}
00063     args['url']     = url
00064     args['level']   = 'CRITICAL'
00065     if DEBUG:
00066         print lineno()
00067     api = DBSAPI.dbsApi.DbsApi(args)
00068     if DEBUG:
00069         print lineno()
00070     try:
00071         files = api.listFiles(path=dataset,tier_list =site,runNumber=run)
00072     except DbsApiException, ex:
00073         print "Caught API Exception %s: %s "  % (ex.getClassName(), ex.getErrorMessage() )
00074         files = ""
00075         if ex.getErrorCode() not in (None, ""):
00076             print "DBS Exception Error Code: ", ex.getErrorCode()
00077     if DEBUG:
00078         print lineno()
00079     return files
00080 
00081 ###file cff data
00082 def makecff(file_list,namefile):
00083     file1 = open(namefile ,'w')
00084     stringS  =           "process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )\n"
00085     stringS  = stringS + "readFiles = cms.untracked.vstring()\n"
00086     stringS  = stringS + "secFiles = cms.untracked.vstring()\n"
00087     stringS  = stringS + "\n"
00088     file1.write(stringS)
00089     
00090     filecount = 0
00091     extendOpen = 0
00092     for filename in file_list:
00093         
00094         if extendOpen == 0:
00095             stringS  = "readFiles.extend([\n"
00096             file1.write(stringS)
00097             extendOpen = 1
00098             
00099         stringS  =           "     '"
00100         stringS  = stringS + str(filename)
00101         stringS  = stringS + "',\n"
00102         file1.write(stringS)
00103         filecount = filecount + 1
00104         if filecount == 50:
00105             stringS  = "])\n\n"
00106             file1.write(stringS)
00107             filecount = 0
00108             extendOpen = 0
00109 
00110     if extendOpen == 1:
00111         stringS  =           "])\n\n"
00112         file1.write(stringS)
00113     
00114     stringS  =           "process.source = cms.Source(\"PoolSource\",\n"
00115     stringS  = stringS + "         fileNames = readFiles,\n"
00116     stringS  = stringS + "         secondaryFileNames = secFiles\n"
00117     stringS  = stringS + ")\n"
00118     file1.write(stringS)
00119     file1.close()
00120 
00121 
00122 def defineOptions():
00123     parser = OptionParser()
00124     parser.add_option("-w", "--workspace",
00125                       dest="workspaceName",
00126                       default="GLOBAL",
00127                       help="define workspace: GLOBAL TRACKER ")
00128 
00129     parser.add_option("-r", "--regexp",
00130                       dest="regexp",
00131                       type="string",
00132                       default='groupName : LIKE %Collisions12% , runNumber : = 190000 ',
00133                       help=" \"{runNumber} >= 148127 and {runNumber} < 148128 \" ")
00134 
00135     parser.add_option("-d", "--datasetPath",
00136                       dest="dataset", \
00137                       default="/MinimumBias/Run2010A-TkAlMinBias-Dec22ReReco_v1/ALCARECO",
00138                       help="For example : --datasetPath /MinimumBias/Run2010A-TkAlMinBias-Dec22ReReco_v1/ALCARECO")
00139     
00140     parser.add_option("-s", "--site",
00141                       dest="site",
00142                       default="T2_CH_CAF",
00143                       help="For example : site T2_CH_CAF")                 
00144 
00145     parser.add_option("-i", "--info",
00146                       action="store_true",
00147                       dest="info",
00148                       default=False,
00149                       help="printout the column names on which it's possible to cut")
00150     
00151     (options, args) = parser.parse_args()
00152     if len(sys.argv) == 1:
00153         print("\nUsage: %s --help"%sys.argv[0])
00154         sys.exit(0)
00155     
00156     return options
00157 
00158     
00159 def serverQuery(workspaceName,regexp):
00160 
00161     # get handler to RR XML-RPC server
00162     server = xmlrpclib.ServerProxy('https://cms-service-runregistry-api.web.cern.ch/cms-service-runregistry-api/xmlrpc')
00163     if DEBUG:
00164         print lineno(), regexp
00165     data = server.RunDatasetTable.export(workspaceName,'xml_all' ,regexp)
00166     return data
00167 
00168 #----------------------------------------------------
00169 
00170 def getText(nodelist):
00171     rc = ""
00172     for node in nodelist:
00173         if node.nodeType == node.TEXT_NODE:
00174             rc = rc + node.data
00175     return rc
00176 
00177 def getElement(obj,name):
00178     return obj.getElementsByTagName(name)
00179 
00180 def printObj(obj,name):
00181     return getText(getElement(obj,name)[0].childNodes)
00182 
00183 
00184 def getData(doc,options,dataset,site):
00185     if DEBUG:
00186         print lineno(), 'getData'
00187     server = xmlrpclib.ServerProxy('https://cms-service-runregistry-api.web.cern.ch/cms-service-runregistry-api/xmlrpc')
00188     runs = getElement(doc,'RUN')
00189     txtLongData=""
00190     txtkey=""
00191     lista=[]
00192     
00193     sep="\t"
00194 
00195     for run in runs:
00196         txtrun=printObj(run,'NUMBER') + sep + printObj(run,'HLTKEY')
00197         txtLongData+= txtrun + sep + "\n" 
00198     for run in runs:
00199         test=printObj(run,'HLTKEY')
00200         if not (test in lista):
00201             lista.append(test)
00202 
00203         file2=open("lista_key.txt",'w')
00204         for pkey in range(len(lista)):
00205             pwkey = lista[pkey] +"\n"
00206             file2.write(pwkey)
00207             if DEBUG:
00208                 print lineno(),  lista[pkey]
00209 
00210         file2.close()
00211 
00212     for i in range(len(lista)):
00213         if DEBUG:
00214             print lineno(), lista[i]
00215         nameDBS=""
00216         nameDBS=str(i)+".data"
00217         name=""
00218         name=str(i)+".json"
00219         nameLumi=""
00220         nameLumi=str(i)+".lumi"
00221         file1 = open( name ,'w')
00222         listaDBS = []
00223         if DEBUG:
00224             print lineno(), nameDBS
00225         for run in runs:
00226             key=printObj(run,'HLTKEY')
00227             if (key == lista[i]):
00228                 print "running......", key
00229                 if DEBUG:
00230                     print lineno(), printObj(run,'NUMBER')
00231                 txtruns = "{runNumber} >= " + printObj(run,'NUMBER') +  " and {runNumber} <= " + str(int(printObj(run,'NUMBER')))
00232                 txtriv = txtruns + " and {cmpDcsBPix} = 1  and {cmpDcsFPix} = 1 and {cmpDcsTibTid} = 1 and {cmpDcsTob} = 1 and {cmpDcsTecM} = 1 and {cmpDcsTecP} = 1"
00233                 lumirun = server.RunLumiSectionRangeTable.export('GLOBAL', 'json',txtriv)
00234                 if DEBUG:
00235                     print lineno(), lumirun
00236                   ###dbs file
00237                 if lumirun:
00238                     file = DBSquery(dataset,site,str(printObj(run,'NUMBER')))                    
00239 ##                 if DEBUG:
00240 ##                     print lineno(), file
00241                 if (file != "") :
00242                     for uno in file:
00243                         stringDBS = {}
00244                         stringDBS = uno['LogicalFileName']
00245                         listaDBS    += [stringDBS]
00246                     if DEBUG:
00247                         print lineno(), lumirun
00248                     comp="{}"
00249                     if (lumirun == comp):
00250                         print "LUMI ZERO"
00251                     else:
00252                         file1.write(lumirun)
00253                
00254         file1.close()
00255         string=""
00256         string="sed -i 's/}{/, /g'"
00257         string2=""
00258         string2= string + " " + name
00259         os.system(string2)
00260         printLumi(name,nameLumi)
00261         os.system("sed -i 's/\//_/g' lista_key.txt")
00262         listaDBS2 =[]
00263         for rootLSF in listaDBS:
00264             if not (rootLSF in listaDBS2):
00265                 listaDBS2.append(rootLSF)
00266         makecff(listaDBS2,nameDBS)
00267           
00268     return txtLongData
00269 
00270 #---------------------------------------------
00271 
00272 def extractData(mode,reg,dataset,site,options):
00273     doc = minidom.parseString(serverQuery(mode,reg))
00274     return getData(doc,options,dataset,site)
00275 
00276 def getRegExp(regexp):
00277     items = regexp.split(',') 
00278     dd = {}
00279     for item in items:
00280         key,value = item.split(':')
00281         dd[key.replace(' ','')] = value
00282     return dd
00283 
00284 
00285 #---------------------------------------------MAIN
00286 
00287 options = defineOptions()
00288 data=extractData(options.workspaceName,options.regexp,options.dataset,options.site,options)
00289