Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users...
-
Upload
everett-adley -
Category
Documents
-
view
216 -
download
0
Transcript of Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users...
Wim Lavrijsen, LBNL 1 PyROOT, LBNL May 2004
PyROOTReference Talkpresented at:
ROOT 2004 Users Workshop, SLAC
LBNL, February 2004 (updated, May 2004)
Wim Lavrijsen, LBNL
Wim Lavrijsen, LBNL 2 PyROOT, LBNL May 2004
OutlineMotivation, history, and context
Goal
Overview
User-level and examples
Design and implementation
Status, future development
Resources
Wim Lavrijsen, LBNL 3 PyROOT, LBNL May 2004
MotivationThe case for Python:
Used for scientific programming (www.scipy.org)
Including high performance and distributed parallel code
2nd most popular scripting language
Wide-spread use as a “glue language”
The case for PyROOT:
Access ROOT from non-ROOT applications
Utilize pre-existing Python bindings from ROOT
Wim Lavrijsen, LBNL 4 PyROOT, LBNL May 2004
Example: pyQtimport sys, ROOT
from qt import *
theApp = QApplication( sys.argv )
box = QVBox()
box.resize( QSize(40,10).expandedTo(box.minimumSizeHint()) )
class myButton ( QPushButton ):
def __init__( self, label, master ):
QPushButton.__init__( self, label, master )
self.setFont( QFont( "Times", 18, QFont.Bold ) )
def browse( self ):
self.b = ROOT.TBrowser()
bb = myButton( "browser", box )
QObject.connect( bb, SIGNAL( "clicked()" ), bb.browse )
theApp.setMainWidget( box )
box.show()
theApp.exec_loop()
Wim Lavrijsen, LBNL 5 PyROOT, LBNL May 2004
HistoryPyROOT saw light as “RootPython”
Developed by Pere Mato
Resides in the Gaudi (the LHCb framework) repository
PyROOT inherited ideas, but all code rewritten:
Use Python C-API only
Closer in style to Python, more generic towards ROOT
Large improvement in performance
Was part of SEAL, now released with ROOT 4
Wim Lavrijsen, LBNL 6 PyROOT, LBNL May 2004
SEALShared Environment for Applications at LHC
Provide LHC common core and services libraries
Foundation class libraries (system, utility, etc. libs)
Framework services (includes scripting)
Improve coherency of LCG apps
Strategy of pluggable components
One such component is PyROOT
Wim Lavrijsen, LBNL 7 PyROOT, LBNL May 2004
GoalA “bridge” between ROOT and Python
Main library loadable in both CINT and Python
Objects can cross interpreters' boundary
Note: work in progress ... see later in this presentation
Allow Python callbacks from CINT and vv.
Completely reflection-based
No code generation necessary
Run-time binding generated as needed
Wim Lavrijsen, LBNL 8 PyROOT, LBNL May 2004
ROOT FeaturesROOT basically uses only ROOT classes
Almost all derive from TObject and have a TClass
Rather consistent in class, variable naming
Simple C++ (no cov. return, templates, fct hiding, etc.)
None of the above is needed, but makes life real easy!
Extensive reflection information
CINT dictionary
Includes signatures, scopes, default arguments, etc.
Wim Lavrijsen, LBNL 9 PyROOT, LBNL May 2004
Ex.: ROOT from Python>>> from ROOT import gRandom, TCanvas, TH1F
>>> c1 = TCanvas('c1','Example',200,10,700,500)
>>> hpx = TH1F('hpx','px',100,-4,4)
>>> for i in xrange(25000):
... px = gRandom.Gaus()
... hpx.Fill(px)
...
>>> hpx.Draw()
>>> c1.Update()
Wim Lavrijsen, LBNL 10 PyROOT, LBNL May 2004
Ex.: Python from ROOTroot[0] gSystem->Load( “libPyROOT” )
root[1] TPython::Exec( “print 1 + 1” )
2
root[2] Tbrowser* b = (Tbrowser*) TPython::Eval( “ROOT.TBrowser()” )
(class TBrowser*)0x8d1daa0
root[3] TPython::Prompt()
>>> i = 2; ^D
root[4] TPython::Prompt()
>>> print i
2
Wim Lavrijsen, LBNL 11 PyROOT, LBNL May 2004
PyROOT
Conceptual Design - 1Python interpreter
Lookup in ROOT namespace
ROOTlibraries
ROOT class? TClass
PyClass PyClass = PyClass_New( TClass::GetName() ) PyDict = PyClass.__dict__
for all TMethods from TClass :name = TMethod::GetName()if not name in PyDict :
PyDict[ name ] =
MethodDispatcher( TMethod* )else :
PyDict[ name ].addMethod( TMethod* )
return PyClassCINT
PyClass method call
Wim Lavrijsen, LBNL 12 PyROOT, LBNL May 2004
Example in Detail ...>>> from ROOT import TCanvas
[ import ROOT.py, attempts to retrieve TCanvas ]
>>> c1 = TCanvas('c1','Example',200,10,700,500)
[ object construction in both Python and ROOT ]
>>> c1.Update()
[ member call into ROOT from Python ]
Wim Lavrijsen, LBNL 13 PyROOT, LBNL May 2004
ROOT.pyLoads PyROOT.so module
Exc. hook and module facade to capture names
Lookup names by cache or by ROOT
Instantiated/bound if correctable
Re-execute code in the proper stack frame
Transparent to the end-user
Fires up a thread to feed system events to ROOT
Wim Lavrijsen, LBNL 14 PyROOT, LBNL May 2004
Example in Detail ...>>> from ROOT import TCanvas
[ import ROOT.py, attempts to retrieve TCanvas ]
>>> c1 = TCanvas('c1','Example',200,10,700,500)
[ object construction in both Python and ROOT ]
>>> c1.Update()
[ member call into ROOT from Python ]
Wim Lavrijsen, LBNL 15 PyROOT, LBNL May 2004
ConstructorDispatcherPython CINT
TCanvas.__init__( *args ) execute selected G__CallFunchash args and select overload
wrap result (ObjectHolder)
c1 = TCanvas( 'c1', 'Example', ....
new TCanvas( arg1, arg2, ... )<ROOT.TCanvas instance at ... >
setup G__CallFunc (MethodHolder)
PyROOT
Wim Lavrijsen, LBNL 16 PyROOT, LBNL May 2004
Example in Detail ...>>> from ROOT import TCanvas
[ import ROOT.py, attempts to retrieve TCanvas ]
>>> c1 = TCanvas('c1','Example',200,10,700,500)
[ object construction in both Python and ROOT ]
>>> c1.Update()
[ member call into ROOT from Python ]
Wim Lavrijsen, LBNL 17 PyROOT, LBNL May 2004
MethodDispatcherPython
TCanvas.Update( c1 ) execute selected G__CallFuncsetup G__CallFunc (MethodHolder)
wrap result if necessary
c1.Update()
TCanvas_object->Update()NonePyObject or ObjectHolder
CINTPyROOT
extract TObject* (from PyObject)
Wim Lavrijsen, LBNL 18 PyROOT, LBNL May 2004
PyROOT
Conceptual Design - 2Python interpreter
ROOTlibraries
root [0] Python::eval( “expression” )
python, context = GetInterpreter(), GetContext() result = python.evaluate( “expresion”, context ) if result : TObject* robj = convertToROOT( result ) else: reportError() TObject* robj = 0
return robjCINT
TPython.h + rootcint
Run string
TObject*
Wim Lavrijsen, LBNL 19 PyROOT, LBNL May 2004
TPython.hUse rootcint and appropriate LinkDef.h:class TPython {
public:
void Exec( char* statement );
TPyReturn& Eval( char* expression );
bool Bind( TObject* obj, char* label );
void Prompt();
// ...
}
Wim Lavrijsen, LBNL 20 PyROOT, LBNL May 2004
Callbacks>>> from ROOT import TControlBar, TBrowser
>>> bar = TControlBar('vertical','Demo')
>>> bar.AddButton('browser',
... r'TPython::Exec("b = TBrowser()”);',
... 'The ROOT browser')
...
>>> bar.Show()
Wim Lavrijsen, LBNL 21 PyROOT, LBNL May 2004
Interpreter CrossingFor now, only for objects w/ CINT dictionary:root [1] TBrowser* b = new TBrowser()
root [2] TPython::Bind( b, “b” )
(bool)1
root [3] Tbrowser* b1 = (Tbrowser*) Python::Eval( “b” )
(class TBrowser*)0x894d3d8
root [4] b == b1
(int)1
Wim Lavrijsen, LBNL 22 PyROOT, LBNL May 2004
StatusROOT tutorial examples all work
Performance is good enough
Python from ROOT is minimal so far
Little (natural) support for basic ROOT classes
TString, TArrayF, etc.
Some functionality got lost in the move to C-API
Wim Lavrijsen, LBNL 23 PyROOT, LBNL May 2004
PyROOT ResourcesReleased as part of SEAL
/afs/cern.ch/sw/lcg/app/releases/SEAL
SEAL repository: Scripting/PyROOT
Documentation:http://root.cern.ch/root/HowtoPyROOT.html
http://cern.ch/seal/snapshot/devguide/PyROOT-howto.html
http://cern.ch/wlav/scripting/scripting.html#PyROOT
Examples provided with PyROOT installation
Wim Lavrijsen, LBNL 24 PyROOT, LBNL May 2004
Other ResourcesPython, boost.python bindings
http://www.python.org
http://www.boost.org/libs/python/doc
ROOT
http://root.cern.ch
SEAL project portal
http://cern.ch/seal