Root in a browser

I’m trying to make a simple python cgi that will allow me to view a root file in a browser. Unfortunately it dies at the point where it tries to save the png file.

Here’s what I have so far:

import os, sys, cgi
import cgitb; cgitb.enable()
sys.path.insert(0, '/usr/local/root/lib')
from ROOT import *
display = None
mydir = "/path/to/my/data"

print "Content-Type: text/html"
print """
<h1>hello world</h1>
form = cgi.FieldStorage()
if form.has_key('file') :
        rf = TFile(mydir+'/'+form['file'].value)
        rkeys = rf.GetListOfKeys()
        if not form.has_key('key') :
                for k in rkeys :
                        print '<a href="'+form['file'].value+ '&key='+k.GetName()+'">'+k.GetName()+'</a><br/>'
        else :
                hist = rf.Get(form['key'].value)
                canvas = TCanvas()
                print '<img src="foo.png"/>'
else :
        l = os.listdir(mydir)
        for i in l :
                if i.find('.root') > -1:
                        print '<a href="'+i+'">'+i+'</a><br/>'

You should either start ROOT in batch mode or use Xvfb

I think for the off-screen rendering the TCanvas-less TPad subclass is a better choice for Web application (there no overhead and no event loop is required). I have prototyped such class: … escription

It is included as part of Qt/Root (there is a short “HelloLife” working example there to test). However it has nothing to do with Qt and can be used with any other ROOT GUI layers. (another way to implement this is to add one extra constructor to the TPad class directly.

Which version of ROOT are you using? a few bugs have been fixed in the code
generating gif,jpg,png files in the recent versions, like 5.10.



in addition, this:from ROOT import * gROOT.SetBatch(1) doesn’t quite work as you’d expect it to. I have a chicken & egg problem that I do not know how to solve elegantly.

Please do:sys.argv.append( '-b' ) from ROOT import * which will forward the ‘-b’ to ROOT with the same effect as ‘root -b’. Normally, “python <> -b” would be the way to go.

Note also that “from ROOT import *” incurs an overhead compared to “import ROOT” and that overhead grows fast with increased complexitiy of your script.



not to encourage the usage of “from ROOT import *”, but HEAD CVS now has an implementation that performs and scales much better, even when used in scripts.