File indexing completed on 2024-09-27 07:02:50
0001 '''
0002 eicpy package-level code.
0003 Used for initialising the ROOT environment with eic-smear code.
0004 '''
0005
0006 import os, subprocess, sys
0007
0008 def initialise(logon=False):
0009 '''
0010 Initialise the ROOT environment and load eic-smear code.
0011 Optionally load the ROOT logon script.
0012 '''
0013 try:
0014 set_rootlib_path()
0015 import ROOT
0016
0017
0018
0019 ROOT.PyConfig.IgnoreCommandLineOptions = True
0020
0021 if logon:
0022 root_logon()
0023 if ROOT.gSystem.Load('libeicsmear') < 0:
0024 raise IOError('libeicsmear cannot be located')
0025 except Exception as e:
0026 print 'initialise Error:', e
0027
0028 def set_rootlib_path():
0029 '''
0030 Add the ROOT library path to the Python module search path
0031 if it is not already in it.
0032 Raises an exception if the path cannot be determined or
0033 ROOT cannot be imported.
0034 '''
0035 try:
0036
0037 import ROOT
0038 except:
0039
0040
0041 rootlib = find_root_lib_dir()
0042 if rootlib not in sys.path:
0043 sys.path.append(rootlib)
0044
0045
0046 import ROOT
0047
0048 def find_root_lib_dir():
0049 '''
0050 Attempt to locate the ROOT library directory.
0051 Return the path to it, or raise an exception if it cannot be found.
0052 '''
0053 try:
0054 rootlib = ''
0055
0056 rootsys = os.path.expandvars('$ROOTSYS')
0057 if os.path.exists(rootsys):
0058
0059
0060 rootlib = '/'.join([rootsys, 'lib'])
0061 else:
0062
0063
0064
0065 rootbin = subprocess.check_output(['which', 'root'])
0066
0067
0068 rootlib = rootbin.replace('bin/root\n', 'lib')
0069
0070
0071 if not os.path.exists(rootlib):
0072 raise IOError(
0073 ' '.join(['ROOT lib directory', rootlib, 'does not exist']))
0074
0075 libpyroot = '/'.join([rootlib, 'libPyROOT.so'])
0076 if not os.path.exists(libpyroot):
0077 raise IOError('PyROOT library not found at ' + libpyroot)
0078 return rootlib
0079
0080
0081 except subprocess.CalledProcessError as e:
0082 print 'Failed to locate ROOT; is it installed?'
0083 raise
0084
0085 def root_logon():
0086 '''
0087 Load the user's ROOT logon script, if it can be located.
0088 Raise an IOError if it cannot.
0089 '''
0090 import ROOT
0091 logon = ROOT.gEnv.GetValue('Rint.Logon', '')
0092 if os.path.exists(logon):
0093 ROOT.gROOT.Macro(logon)
0094 else:
0095 raise IOError('Unable to load user\'s ROOT logon script')