Example #1
0
def extractBINS(configPy, var):
    """
	extractBINS( 'nameConfig.py', ['var1','var2'] ) --> cms.PSet.BINS, 'PT-like', 'ETA-like'

	"""

    #TODO: Better a temporary file
    try:
        shutil.copy(configPy, '_tmpPy.py')
    except IOError:
        message = '\033[1;31mError: There is no config File named %s\033[1;m' % configPy
        raise IOError, message
    # To be sure the first import is FWCore.ParameterSet.Config
    # in order to extract BINS
    _file = open('_tmpPy.py', 'r')
    _lines = _file.readlines()
    _file.close()
    _lines.insert(0, 'import FWCore.ParameterSet.Config as cms\n')
    _file = open('_tmpPy.py', 'w')
    _file.writelines(_lines)
    _file.close()
    # Append the working directory to do the import
    sys.path.append(os.getcwd())
    #------------------------------------------------------------

    try:
        from _tmpPy import BINS
    except ImportError:
        message = '\033[1;31mError: There is no BINS in %s file. Are you sure this is a config python to do the fit?\033[1;m' % configPy
        os.remove('_tmpPy.py')
        raise ImportError, message

    variables = BINS.parameterNames_()
    # Check if the variables introduced by the user are inside
    # the fit config python
    for i in var:
        if i not in variables:
            os.remove('_tmpPy.py')
            message = """\033[1;31mError: The variable %s is not in the parameter BINS of the config python %s. 
Check your config or change your input variable with --var option\033[1;m """ % (
                i, configPy)
            print message
            raise KeyError

    # All was fine. Remember: first variable is the pt-like (construct the weights respect it)
    PT = var[0]
    ETA = var[1]

    #bins = BINS
    try:
        os.remove('_tmpPy.py')
        os.remove('_tmpPy.pyc')
    except OSError:
        pass

    return BINS, PT, ETA
Example #2
0
def extractBINS( configPy, var ):
	"""
	extractBINS( 'nameConfig.py', ['var1','var2'] ) --> cms.PSet.BINS, 'PT-like', 'ETA-like'

	"""

	#TODO: Better a temporary file
	try:
		shutil.copy( configPy, '_tmpPy.py')
	except IOError:
		message = '\033[1;31mError: There is no config File named %s\033[1;m' % configPy
		raise IOError, message
	# To be sure the first import is FWCore.ParameterSet.Config 
	# in order to extract BINS
	_file = open('_tmpPy.py','r')
	_lines = _file.readlines()
	_file.close()
	_lines.insert(0,'import FWCore.ParameterSet.Config as cms\n')
	_file = open('_tmpPy.py','w')
	_file.writelines(_lines)
	_file.close()
	# Append the working directory to do the import
	sys.path.append( os.getcwd() )
	#------------------------------------------------------------     
	
	try:
		from _tmpPy import BINS
	except ImportError:
		message = '\033[1;31mError: There is no BINS in %s file. Are you sure this is a config python to do the fit?\033[1;m' % configPy
		os.remove('_tmpPy.py')
		raise ImportError, message

	variables = BINS.parameterNames_()
	# Check if the variables introduced by the user are inside
	# the fit config python
	for i in var:
		if i not in variables:
			os.remove('_tmpPy.py')
			message = """\033[1;31mError: The variable %s is not in the parameter BINS of the config python %s. 
Check your config or change your input variable with --var option\033[1;m """ % ( i, configPy)
		        print message
                        raise KeyError

	# All was fine. Remember: first variable is the pt-like (construct the weights respect it)
	PT = var[0]
	ETA = var[1]

	#bins = BINS
	try:
		os.remove( '_tmpPy.py' )
		os.remove( '_tmpPy.pyc' )
	except OSError:
		pass

	return BINS,PT,ETA