CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/HLTriggerOffline/Common/python/HltComparatorCreateWorkflow.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Original Author: James Jackson
00003 # $Id: HltComparatorCreateWorkflow.py,v 1.2 2009/07/19 14:34:19 wittich Exp $
00004 # $Log: HltComparatorCreateWorkflow.py,v $
00005 # Revision 1.2  2009/07/19 14:34:19  wittich
00006 # Add HltComparator file back in for 3.1.
00007 # Tweaks to scripts and cfg files for HLT re-running.
00008 #
00009 
00010 # MakeValidationConfig.py
00011 #   Makes CMSSW config for prompt validation of a given run number,
00012 #   HLT Key or HLT Config
00013 
00014 from optparse import OptionParser
00015 import os, time, re
00016 
00017 jobHash = "%s_%s_%s" % (os.getuid(), os.getpid(), int(time.time()))
00018 
00019 usage = '%prog [options]. \n\t-a and -k required, -h for help.'
00020 parser = OptionParser(usage)
00021 parser.add_option("-a", "--analysis", dest="analysis", 
00022                   help="analysis configuration file")
00023 # not yet implemented
00024 #parser.add_option("-r", "--run", dest="run", 
00025 #                  help="construct config for runnumber RUN", metavar="RUN")
00026 parser.add_option("-k", "--hltkey", dest="hltkey", 
00027                   help="ignore RunRegistry and force the use of HLT key KEY", 
00028                   metavar="KEY")
00029 parser.add_option("-c", "--hltcff", dest="hltcff", 
00030                   help="use the config fragment CFF to define HLT configuration", 
00031                   metavar="CFF")
00032 parser.add_option("-f", "--frontier", dest="frontier", 
00033                   help="frontier connection string to use, defaults to frontier://FrontierProd/CMS_COND_21X_GLOBALTAG", 
00034                   default="frontier://FrontierProd/CMS_COND_31X_GLOBALTAG")
00035 
00036 # Parse options and perform sanity checks
00037 (options, args) = parser.parse_args()
00038 #if options.run == None and options.hltkey == None and options.hltcff == None:
00039 if options.hltkey == None and options.hltcff == None:
00040    parser.error("I don't have all the required options.")
00041    raise SystemExit(
00042       "Please specify one of --hltkey (-k) or --hltcff (-s)")
00043 if options.hltkey != None and options.hltcff != None:
00044    raise SystemExit("Please only specify --hltkey (-k) or --hltcff (-c)")
00045 # if options.run != None and options.hltkey != None:
00046 #    raise SystemExit("Please only specify --run (-r) or --hltkey (-k)")
00047 # if options.run != None and options.hltcff != None: 
00048 #    raise SystemExit("Please only specify --run (-r) or --hltcff (-c)")
00049 if options.analysis == None:
00050    raise SystemExit(
00051       "Please specify an analysis configuration: -a or --analysis")
00052 
00053 # Checks the runtime environment is suitable
00054 def CheckEnvironment():
00055    cwd = os.getcwd()
00056    t = cwd.split("/")
00057    if t[-1] != "python":
00058       raise SystemExit("Must run from a Module/Package/python directory")
00059 
00060 # Converts an online HLT configuration into offline suitable format
00061 def ConvertHltOnlineToOffline(config, frontierString):
00062    # Replace the frontier string
00063    onlineFrontier = re.search('"(frontier:.*)"', config)
00064    if not onlineFrontier:
00065       print "WARNING: Could not find Frontier string in HLT configuration. Will ignore."
00066    else:
00067       config = config.replace(onlineFrontier.group(1), frontierString)
00068 
00069    # Replace the global tag
00070    config = config.replace("H::All", "P::All")
00071 
00072    # print config -- debugging
00073    # Remove unwanted PSets
00074    config = RemovePSet(config, "MessageLogger")
00075 #   config = RemovePSet(config, "DQMStore")
00076    config = RemovePSet(config, "DQM")
00077    config = RemovePSet(config, "FUShmDQMOutputService")
00078 
00079    return config
00080 
00081 def RemovePSet(config, pset):
00082    startLoc = config.find(pset)
00083    started = False
00084    count = 0
00085    curLoc = startLoc
00086    endLoc = 0
00087 
00088    # Find starting parenthesis
00089    while not started:
00090       if config[curLoc] == "(":
00091          started = True
00092          count = 1
00093       curLoc += 1
00094 
00095    # Find end parenthesis
00096    while endLoc == 0:
00097       if config[curLoc] == "(":
00098          count += 1
00099       elif config[curLoc] == ")":
00100          count -= 1
00101       if count == 0:
00102          endLoc = curLoc
00103       curLoc += 1
00104 
00105    config = config.replace(config[startLoc:endLoc + 1], "")
00106 
00107    return config
00108 
00109 # Fetches the HLT configuration from ConfDB
00110 def GetHltConfiguration(hltKey, frontierString):
00111    # Format the config path for include
00112    cwd = os.getcwd()
00113    t = cwd.split("/")
00114    module = t[-3]
00115    package = t[-2]
00116 
00117    # Get the HLT config
00118    config = os.popen2('wget "http://cms-project-confdb-hltdev.web.cern.ch/cms-project-confdb-hltdev/get.jsp?dbName=ORCOFF&configName=%s&cff=&nooutput=&format=Python" -O- -o /dev/null' % hltKey)[1].read()
00119    #config = os.popen2("edmConfigFromDB --debug --configName %s --orcoff --format Python --nooutput --cff" % hltKey)[1].read() 
00120    configName = "JobHLTConfig_%s_cff.py" % jobHash
00121 
00122    # Perform online --> offline conversion
00123    config = ConvertHltOnlineToOffline(config, frontierString)
00124 
00125    # Write the config
00126    f = open(configName, "w")
00127    f.write(config)
00128    f.close()
00129 
00130    return 'process.load("%s.%s.%s")' % (module, package, configName.split(".")[0])
00131 
00132 # Fetches the HLT key from ConfDB - turned off in options for now
00133 def GetHltKeyForRun(run):
00134    raise SystemExit("Not implemented yet")
00135 
00136 # Formats an HLT CFF path into a suitable python include statement
00137 def FormatHltCff(cffPath):
00138    pathParts = cffPath.split(".")
00139    if not re.match("^[_A-Za-z0-9]*\.[_A-Za-z0-9]*\.[_A-Za-z0-9]*$", cffPath):
00140       raise SystemExit("Expected cff in form Package.Module.configName_cff")
00141    return 'process.load("%s")' % cffPath
00142 
00143 # Returns python code to compile an HLT configuration
00144 def GetHltCompileCode(subsystem, package, hltConfig):
00145    tmpCode = compileCode.replace("SUBSYSTEM", subsystem)
00146    tmpCode = tmpCode.replace("PACKAGE", package)
00147    tmpCode = tmpCode.replace("CONFIG", hltConfig + "c")
00148    return tmpCode
00149 
00150 # Prepares the analysis configuration
00151 def CreateAnalysisConfig(analysis, hltInclude):
00152    anaName = "JobAnalysisConfig_%s_cfg.py" % jobHash
00153    f = open(analysis)
00154    g = open(anaName, "w")
00155    g.write(f.read())
00156    g.write("\n")
00157    g.write(hltInclude)
00158    g.close()
00159    f.close()
00160    return anaName
00161 
00162 # Quick sanity check of the environment
00163 CheckEnvironment()
00164 
00165 # Get the required HLT config snippet
00166 hltConfig = None
00167 if options.hltkey != None:
00168    hltConfig = GetHltConfiguration(options.hltkey, options.frontier)
00169 elif options.hltcff != None:
00170    hltConfig = FormatHltCff(options.hltcff)
00171 else:
00172    hltKey = GetHltKeyForRun(0)
00173    hltConfig = GetHltConfiguration(hltKey)
00174 
00175 # Prepare the analysis configuration
00176 anaConfig = CreateAnalysisConfig(options.analysis, hltConfig)
00177 
00178 if options.hltcff:
00179     print "Using HLT configuration:           %s" % hltConfig
00180 else:
00181     print "Created HLT configuration:         %s" % hltConfig
00182 print "Created analysis configuration:    %s" % anaConfig
00183