CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/HLTrigger/Tools/python/getHLTprescales.py

Go to the documentation of this file.
00001 #!/usr/bin/env python 
00002 from sys import stderr, exit
00003 import commands
00004 
00005 from optparse import OptionParser
00006 parser = OptionParser(usage="usage: %prog [options] Trigger_Path")
00007 parser.add_option("--firstRun",  dest="firstRun",  help="first run", type="int", metavar="RUN", default="1")
00008 parser.add_option("--lastRun",   dest="lastRun",   help="last run",  type="int", metavar="RUN", default="9999999")
00009 parser.add_option("--groupName", dest="groupName", help="select runs of name like NAME", metavar="NAME", default="Collisions%")
00010 parser.add_option("--rrurl",     dest="rrurl",     help="run registry xmlrpc url", metavar="URL", default="https://cms-service-runregistry-api.web.cern.ch/cms-service-runregistry-api/xmlrpc")
00011 parser.add_option("--jsonOut",   dest="jsonOut",   help="dump prescales in JSON format on FILE", metavar="FILE")
00012 (options, args) = parser.parse_args()
00013 if len(args) != 1:
00014     parser.print_usage()
00015     exit(2)
00016 path = args[0]
00017 
00018 
00019 edmCfgFromDB = "edmConfigFromDB  --orcoff --format summary.ascii --paths " + path;
00020 ## my $pyPrintTable = "echo 'for X in process.PrescaleService.prescaleTable: print \"\%s \%s\" % (X.pathName.value(), X.prescales[0])'";
00021 def getPrescalesFromKey(key):
00022     #stderr.write("\t%s ...\n" % key);
00023     cmd = ( edmCfgFromDB +" --configName "+key + " | grep -i "+ path + " | tail -1 | awk ' $2 ==\"%s\" {print $NL}' " ) % path
00024     res = commands.getoutput(cmd)
00025     res_split = res.split()
00026     psCols = []
00027     if len(res)>0:
00028         for uu in range(3,len(res_split)-1):
00029                 if uu % 2 == 1:
00030                    psCols.append(res_split[uu])
00031     return psCols
00032 
00033 
00034 def queryRR():
00035     stderr.write("Querying run registry for range [%d, %d], group name like %s ...\n" % (options.firstRun, options.lastRun, options.groupName))
00036     import xmlrpclib
00037     import xml.dom.minidom
00038     server = xmlrpclib.ServerProxy(options.rrurl)
00039     run_data = server.DataExporter.export('RUN', 'GLOBAL', 'xml_datasets', "{runNumber} >= %d AND {runNumber} <= %d AND {groupName} like '%s' AND {datasetName} = '/Global/Online/ALL'"  % (options.firstRun, options.lastRun, options.groupName))
00040     ret = {}
00041     xml_data = xml.dom.minidom.parseString(run_data)
00042     xml_runs = xml_data.documentElement.getElementsByTagName("RUN_DATASET")
00043     for xml_run in xml_runs:
00044         ret[xml_run.getElementsByTagName("RUN_NUMBER")[0].firstChild.nodeValue] = xml_run.getElementsByTagName("RUN_HLTKEY")[0].firstChild.nodeValue
00045     return ret
00046 
00047 def readIndex():
00048         asciiFile=open("columns.txt","read")
00049         mapIndex={}
00050         fl="go"
00051         while fl:
00052           fl=asciiFile.readline()
00053           if len(fl)>0:
00054                 ll=fl.split()
00055                 runnumber=ll[0]
00056                 pindex=ll[1]
00057                 mapIndex[runnumber]=pindex
00058         asciiFile.close()
00059         return mapIndex
00060 
00061         
00062 MapIndex=readIndex()
00063 runKeys = queryRR()
00064 prescaleTable = {}
00065 Absent = []
00066 runs = runKeys.keys(); runs.sort()
00067 stderr.write("Querying ConfDB for prescales for path %s...\n" % (path));
00068 jsout = {}
00069 for run in runs:
00070     key = runKeys[run]
00071     if not prescaleTable.has_key(key):
00072         prescaleTable[key] = getPrescalesFromKey(key)
00073     psfactor = 1
00074     absent=0
00075     if len(prescaleTable[key]) == 0:
00076         psfactor = 0
00077     else:
00078         if MapIndex.has_key(run):
00079                 index = int(MapIndex[run])
00080                 psfactor = prescaleTable[key][index]
00081         else:
00082                 if int(run) < 138564:
00083                         index = 0
00084                         psfactor = prescaleTable[key][index]
00085                 else:
00086                         #print "... the run ",run," is not found in columns.txt ... Index is set to zero, need to check independently..."
00087                         index=0
00088                         psfactor = prescaleTable[key][index]
00089                         Absent.append(run)
00090                         absent=1
00091     if absent==0:
00092         print "%s\t%s" % (run, psfactor)
00093     else:
00094         print "%s\t%s\t (*)" % (run, psfactor)
00095     jsout[run] = psfactor
00096 
00097 if len(Absent)>0:
00098         print ""
00099         print "(*) The following runs were not found in columns.txt (the run may be too recent, or the prescale index is not in OMDS)."
00100         print "For these runs, the prescale_index was assumed to be zero. You need to check independently."
00101         for r in Absent:
00102                 print "\t",r
00103         print ""
00104 
00105 if options.jsonOut:
00106     stderr.write("Exporting to JSON file %s...\n" % (options.jsonOut))
00107     import json
00108     jsonFile = open(options.jsonOut, "w")
00109     jsonFile.write(json.dumps(jsout))
00110     jsonFile.close()