Skip to content

Instantly share code, notes, and snippets.

@mgmarino
Created June 28, 2012 10:05
Show Gist options
  • Save mgmarino/3010410 to your computer and use it in GitHub Desktop.
Save mgmarino/3010410 to your computer and use it in GitHub Desktop.
pyroot 'executable'
#!/usr/bin/env python
import readline
import os
import atexit
import rlcompleter
import code
import sys
import exceptions
import __main__
import re
class irlcompleter(rlcompleter.Completer):
def complete(self, text, state):
if text == "":
readline.insert_text('\t')
return None
else:
return rlcompleter.Completer.complete(self,text,state)
# You could change this line to bind another key instead tab.
readline.parse_and_bind("tab: complete")
readline.set_completer(irlcompleter().complete)
# Restore our command-line history, and save it when Python exits.
historyPath = os.path.expanduser("~/.pyhistory")
if not os.path.exists(historyPath):
f = open(historyPath, "w")
f.close()
try:
readline.read_history_file(historyPath)
except IOError:
pass
atexit.register(lambda x=historyPath: readline.write_history_file(x))
try:
import ROOT
base_file_name = "_file" # Just like ROOT
file_counter = 0
module_dict = globals()
for i in range (1, len(sys.argv)):
# Check to see if these are files...
file_temp = ROOT.TFile(sys.argv[i])
if file_temp.IsZombie(): continue
# else export the file
file_name = "%s%i" %(base_file_name, file_counter)
print "Mounting file %s as: %s" % (sys.argv[i], file_name)
module_dict[file_name] = file_temp
file_counter += 1
print "Exporting the following keys:"
j = 0
while 1:
next_key = file_temp.GetListOfKeys().At(j)
if not next_key: break
name = next_key.GetName()
name = name.replace('.','')
name = name.replace(' ','')
while name in module_dict.keys(): name += "_"
print " %s as: %s" % (next_key.GetName(),name)
module_dict[name] = file_temp.Get(next_key.GetName())
j += 1
except ImportError:
print "ROOT not found..."
pass
"""
Class defines an interactive interface.
Handles NameErrors by first checking to
see if the object exists and then re-executing
the code.
"""
class pyROOTInt(code.InteractiveConsole):
def showtraceback(self):
atype, value, tb = sys.exc_info()
checker = re.compile("name '(.*)' is not defined")
if atype == exceptions.NameError:
test = checker.match(str(value))
if test:
obj = ROOT.gROOT.FindObject(test.group(1))
if obj:
globals()[test.group(1)] = obj
self.runcode(tb.tb_frame.f_locals["code"])
return
code.InteractiveConsole.showtraceback(self)
console = pyROOTInt(globals())
console.interact(banner="Entering pyROOT interactive session")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment