Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:17:07

0001 #!/usr/bin/env python3
0002 """getDatasets.py: create Datasets-cff file of an HLT configuration from the ConfDB database
0003 """
0004 import argparse
0005 import subprocess
0006 import os
0007 import re
0008 
0009 import FWCore.ParameterSet.Config as cms
0010 import HLTrigger.Configuration.Tools.pipe as pipe
0011 import HLTrigger.Configuration.Tools.options as options
0012 
0013 def getHLTProcess(config):
0014   '''return cms.Process containing Streams and Datasets of the HLT configuration
0015   '''
0016   # cmd-line args to select HLT configuration
0017   if config.menu.run:
0018     configline = f'--runNumber {config.menu.run}'
0019   else:
0020     configline = f'--{config.menu.database} --{config.menu.version} --configName {config.menu.name}'
0021 
0022   # cmd to download HLT configuration
0023   cmdline = f'hltConfigFromDB {configline} --noedsources --noes --noservices --nopsets --nooutput --nopaths'
0024   if config.proxy:
0025     cmdline += f' --dbproxy --dbproxyhost {config.proxy_host} --dbproxyport {config.proxy_port}'
0026 
0027   # load HLT configuration
0028   try:
0029     foo = {'process': None}
0030     exec(pipe.pipe(cmdline).decode(), foo)
0031     process = foo['process']
0032   except:
0033     raise Exception(f'query did not return a valid python file:\n query="{cmdline}"')
0034 
0035   if not isinstance(process, cms.Process):
0036     raise Exception(f'query did not return a valid HLT menu:\n query="{cmdline}"')
0037 
0038   return process
0039 
0040 ###
0041 ### main
0042 ###
0043 if __name__ == '__main__':
0044 
0045   # defaults of cmd-line arguments
0046   defaults = options.HLTProcessOptions()
0047 
0048   parser = argparse.ArgumentParser(
0049     prog = './'+os.path.basename(__file__),
0050     formatter_class = argparse.RawDescriptionHelpFormatter,
0051     description = __doc__)
0052 
0053   # required argument
0054   parser.add_argument('menu',
0055                       action  = 'store',
0056                       type    = options.ConnectionHLTMenu,
0057                       metavar = 'MENU',
0058                       help    = 'HLT menu to dump from the database. Supported formats are:\n  - /path/to/configuration[/Vn]\n  - [[{v1|v2|v3}/]{run3|run2|online|adg}:]/path/to/configuration[/Vn]\n  - run:runnumber\nThe possible converters are "v1", "v2, and "v3" (default).\nThe possible databases are "run3" (default, used for offline development), "run2" (used for accessing run2 offline development menus), "online" (used to extract online menus within Point 5) and "adg" (used to extract the online menus outside Point 5).\nIf no menu version is specified, the latest one is automatically used.\nIf "run:" is used instead, the HLT menu used for the given run number is looked up and used.\nNote other converters and databases exist as options but they are only for expert/special use.' )
0059 
0060   # options
0061   parser.add_argument('--dbproxy',
0062                       dest    = 'proxy',
0063                       action  = 'store_true',
0064                       default = defaults.proxy,
0065                       help    = 'Use a socks proxy to connect outside CERN network (default: False)' )
0066   parser.add_argument('--dbproxyport',
0067                       dest    = 'proxy_port',
0068                       action  = 'store',
0069                       metavar = 'PROXYPORT',
0070                       default = defaults.proxy_port,
0071                       help    = 'Port of the socks proxy (default: 8080)' )
0072   parser.add_argument('--dbproxyhost',
0073                       dest    = 'proxy_host',
0074                       action  = 'store',
0075                       metavar = 'PROXYHOST',
0076                       default = defaults.proxy_host,
0077                       help    = 'Host of the socks proxy (default: "localhost")' )
0078 
0079   # parse command line arguments and options
0080   config = parser.parse_args()
0081 
0082   process = getHLTProcess(config)
0083 
0084   print('''# %s
0085 
0086 import FWCore.ParameterSet.Config as cms
0087 ''' % config.menu.name)
0088 
0089   for stream in sorted(process.streams.__dict__):
0090     if re.match(r'^(Physics|Parking)', stream):
0091       print('''
0092 # stream %s
0093 ''' % stream)
0094       ds = sorted(process.streams.__dict__[stream])
0095       for dataset in ds:
0096         if dataset in process.datasets.__dict__:
0097           name = 'stream%s_dataset%s_selector' % (stream, dataset)
0098           dump = '''from HLTrigger.HLTfilters.triggerResultsFilter_cfi import triggerResultsFilter as %s
0099 %s.hltResults = cms.InputTag('TriggerResults', '', 'HLT')
0100 %s.l1tResults = cms.InputTag('')
0101 %s.throw      = cms.bool(False)
0102 %s.triggerConditions = %s
0103 ''' % (name, name, name, name, name, process.datasets.__dict__[dataset])
0104         else:
0105           dump = '''# dataset %s not found
0106 ''' % (dataset, )
0107         print(dump)