Back to home page

EIC code displayed by LXR

 
 

    


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         # Set this before we do anthing ROOT-related to ROOT
0017         # doesn't grab the command line arguments instead
0018         # of the user's own programmes.
0019         ROOT.PyConfig.IgnoreCommandLineOptions = True
0020         # Load ROOT logon script if requested.
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         # Test if the path is already set by trying to import ROOT
0037         import ROOT
0038     except:
0039         # That failed, so try to determine the ROOT path and add
0040         # it to the Python module search path.
0041         rootlib = find_root_lib_dir()
0042         if rootlib not in sys.path:
0043             sys.path.append(rootlib)
0044         # Test that it worked by trying to import ROOT again.
0045         # If not, this will raise an ImportError to be handled by the caller.
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         # First check with the ROOTSYS environment variable.
0056         rootsys = os.path.expandvars('$ROOTSYS')
0057         if os.path.exists(rootsys):
0058             # This will give the ROOT top-level directory, while
0059             # we want the lib subdirectory.
0060             rootlib = '/'.join([rootsys, 'lib'])
0061         else:
0062             # If that's empty, attempt to locate via the location
0063             # of the ROOT binary.
0064             # If 'which root' fails a CalledProcessError is raised.
0065             rootbin = subprocess.check_output(['which', 'root'])
0066             # rootbin should be of the form '$ROOTSYS/bin/root\n'.
0067             # We want the corresponding lib directory.
0068             rootlib = rootbin.replace('bin/root\n', 'lib')
0069         # Check that the ROOT library directory we found does
0070         # indeed exist. If not, raise an exception.
0071         if not os.path.exists(rootlib):
0072             raise IOError(
0073                 ' '.join(['ROOT lib directory', rootlib, 'does not exist']))
0074         # Check that the lib directory contains the Python library.
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     # If 'which root' fails, catch the exception and re-raise it,
0080     # printing something more informative.
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')