Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users Workshop, SLAC LBNL, February 2004 (updated, May 2004) Wim.

Similar presentations


Presentation on theme: "Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users Workshop, SLAC LBNL, February 2004 (updated, May 2004) Wim."— Presentation transcript:

1 Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users Workshop, SLAC LBNL, February 2004 (updated, May 2004) Wim Lavrijsen, LBNL

2 2PyROOT, LBNL May 2004 Outline Motivation, history, and context Goal Overview User-level and examples Design and implementation Status, future development Resources

3 Wim Lavrijsen, LBNL3PyROOT, LBNL May 2004 Motivation The case for Python: Used for scientific programming (www.scipy.org)www.scipy.org Including high performance and distributed parallel code 2 nd 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

4 Wim Lavrijsen, LBNL4PyROOT, LBNL May 2004 Example: pyQt import 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()

5 Wim Lavrijsen, LBNL5PyROOT, LBNL May 2004 History PyROOT 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

6 Wim Lavrijsen, LBNL6PyROOT, LBNL May 2004 SEAL Shared 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

7 Wim Lavrijsen, LBNL7PyROOT, LBNL May 2004 Goal A “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

8 Wim Lavrijsen, LBNL8PyROOT, LBNL May 2004 ROOT Features ROOT 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.

9 Wim Lavrijsen, LBNL9PyROOT, 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()

10 Wim Lavrijsen, LBNL10PyROOT, LBNL May 2004 Ex.: Python from ROOT root[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

11 Wim Lavrijsen, LBNL11PyROOT, LBNL May 2004 PyROOT Conceptual Design - 1 Python interpreter Lookup in ROOT namespace ROOT libraries 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 PyClass CINT PyClass method call

12 Wim Lavrijsen, LBNL12PyROOT, 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 ]

13 Wim Lavrijsen, LBNL13PyROOT, LBNL May 2004 ROOT.py Loads 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

14 Wim Lavrijsen, LBNL14PyROOT, 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 ]

15 Wim Lavrijsen, LBNL15PyROOT, LBNL May 2004 ConstructorDispatcher PythonCINT TCanvas.__init__( *args )execute selected G__CallFunc hash args and select overload wrap result (ObjectHolder) c1 = TCanvas( 'c1', 'Example',.... new TCanvas( arg1, arg2,... ) setup G__CallFunc (MethodHolder) PyROOT

16 Wim Lavrijsen, LBNL16PyROOT, 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 ]

17 Wim Lavrijsen, LBNL17PyROOT, LBNL May 2004 MethodDispatcher Python TCanvas.Update( c1 )execute selected G__CallFunc setup G__CallFunc (MethodHolder) wrap result if necessary c1.Update() TCanvas_object -> Update() None PyObject or ObjectHolder CINTPyROOT extract TObject* (from PyObject)

18 Wim Lavrijsen, LBNL18PyROOT, LBNL May 2004 PyROOT Conceptual Design - 2 Python interpreter ROOT libraries 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 robj CINT TPython.h + rootcint Run string TObject*

19 Wim Lavrijsen, LBNL19PyROOT, LBNL May 2004 TPython.h Use rootcint and appropriate LinkDef.h: class TPython { public: void Exec( char* statement ); TPyReturn& Eval( char* expression ); bool Bind( TObject* obj, char* label ); void Prompt(); //... }

20 Wim Lavrijsen, LBNL20PyROOT, 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()

21 Wim Lavrijsen, LBNL21PyROOT, LBNL May 2004 Interpreter Crossing For 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

22 Wim Lavrijsen, LBNL22PyROOT, LBNL May 2004 Status ROOT 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

23 Wim Lavrijsen, LBNL23PyROOT, LBNL May 2004 PyROOT Resources Released 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

24 Wim Lavrijsen, LBNL24PyROOT, LBNL May 2004 Other Resources Python, 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


Download ppt "Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users Workshop, SLAC LBNL, February 2004 (updated, May 2004) Wim."

Similar presentations


Ads by Google