Using ROOT classes from Python ROOT Workshop 14-16 October 2002 P. Mato / CERN.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Wim Lavrijsen, LBNL1PyROOT, LBNL May 2004 PyROOT Reference Talk presented at: ROOT 2004 Users Workshop, SLAC LBNL, February 2004 (updated, May 2004) Wim.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Ideas on the LCG Application Architecture Application Architecture Blueprint RTAG 12 th June 2002 P. Mato / CERN.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
SCRIPTING LANGUAGE. The first interactive shells were developed in the 1960s to enable remote operation of the first time-sharing systems, and these,
Scripting Languages CS351 – Programming Paradigms.
ASP.NET Programming with C# and SQL Server First Edition
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
SEAL V1 Status 12 February 2003 P. Mato / CERN Shared Environment for Applications at LHC.
Programming 101 with Python: an open-source, cross-platform, and fun language By J. Burton Browning, Ed.D. Copyright © J. Burton Browning All rights reserved.
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
Language Evaluation Criteria
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Groovy WHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
- Python & Grid (19dec02 - Caltech) Python Scripting and Grid User Environments Craig E. Tull Trillium Analysis Environment for.
Java Analysis Studio Status Update 12 May 2000 Altas Software Week Tony Johnson
JAS3 + AIDA LC Simulations Workshop SLAC 19 th May 2003.
Blueprint RTAG Status Torre Wenaus, BNL/CERN SC2 Meeting July 5, 2002.
Tutorial C#. PLAN I. Introduction II. Example of C# program :Hello World III. How to use C# language GUI elements Primitives Types Expressions and operators.
21-Apr-2005J. Adamczewski1 DVEE C++ course 2005 The ROOT framework 1: Introduction.
THttpServer class Sergey Linev (GSI). Some history Development was inspired by JSRootIO why not achieve similar functionality with online ROOT application?
Sadegh Aliakbary Sharif University of Technology Fall 2012.
LC Software Workshop, May 2009, CERN P. Mato /CERN.
JavaScript, Fourth Edition
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Ch 1. A Python Q&A Session Spring Why do people use Python? Software quality Developer productivity Program portability Support libraries Component.
March 27, 2007HPC 07 - Norfolk, VA1 C++ Reflection for High Performance Problem Solving Environments Tharaka Devadithya 1, Kenneth Chiu 2, Wei Lu 1 1.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
W. Pokorski - CERN Simulation Project1 Python binding for Geant4 toolkit using Reflex/PyROOT tool Witek Pokorski EuroPython 2006, CERN, Geneva
JAS3 - A general purpose data analysis framework for HENP and beyond Tony Johnson, Victor Serbo, Max Turri, Mark Dönszelmann, Joseph Perl SLAC.
SiD Workshop October 2013, SLACDmitry Onoprienko SiD Workshop SLAC, October 2013 Dmitry Onoprienko SLAC, SCA FreeHEP based software status: Jas 3, WIRED,
SEAL Core Libraries and Services CLHEP Workshop 28 January 2003 P. Mato / CERN Shared Environment for Applications at LHC.
Update on IS performance issues Classes for storing and retrieving scan data from IS GUI updates –Scripting support –Histogramming Overview.
SEAL Project Core Libraries and Services 18 December 2002 P. Mato / CERN Shared Environment for Applications at LHC.
Java Analysis Studio - Status CHEP 98 - September 1998 Tony Johnson - SLAC Jonas Gifford + Kevin Garwood - University of Victoria.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
LC Software Workshop, May 2009, CERN P. Mato /CERN.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
9-Nov-97Tri-Ada '971 TASH An Alternative to the Windows API TRI-Ada ‘97 Terry J. Westley
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
Gaudi Framework Tutorial, Interfacing Gaudi with Python.
Introduction Mehdi Einali Advanced Programming in Java 1.
23/2/2000Status of GAUDI 1 P. Mato / CERN Computing meeting, LHCb Week 23 February 2000.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Classes, Interfaces and Packages
JAS and JACO – Status Report Atlas Graphics Group August 2000 Tony Johnson.
D. Duellmann - IT/DB LCG - POOL Project1 The LCG Dictionary and POOL Dirk Duellmann.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Python PVA Prototype Siniša Veseli Software Engineer AES / Software Services Group EPICS v4 Group Meeting November 19, 2013.
Pyragen A PYTHON WRAPPER GENERATOR TO APPLICATION CORE LIBRARIES Fernando PEREIRA, Christian THEIS - HSE/RP EDMS tech note:
Use of CMT in LHCb CMT Workshop, LAL (Orsay) 28 th February - 1 st March 2002 P. Mato / CERN.
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
CST 1101 Problem Solving Using Computers
VBA - Excel VBA is Visual Basic for Applications
Jim Fawcett CSE687 – Object Oriented Design Spring 2016
Introduction to Python
Java Analysis Studio - Status
The ROOT framework 1: Introduction
The Lua Chunk Vault, an enhancement to epics base
Some thoughts on scripting languages
Presentation transcript:

Using ROOT classes from Python ROOT Workshop October 2002 P. Mato / CERN

8/3/02Using ROOT classes in Python2 Contents  Motivation  Examples  Why Python?  Software requirements, Design, Status  Integration using Python as a Software Bus  Summary

8/3/02Using ROOT classes in Python3 Motivation  Be able to use any ROOT class from Python in a generic way. –Without the need of wrapping each class –Using the ROOT object dictionary information  Facilitate access of ROOT files and other facilities from non-ROOT applications  Proof-of-concept that Python can be viewed as Software Bus –In analogy to a “hardware bus” where you can plug a variety of modules and interface adaptors to other buses.

8/3/02Using ROOT classes in Python4 C:\> python... >>> from rootmodule import * >>> f1 = TF1('func1','sin(x)/x',0,10) >>> f1.Eval(3) >>> f1.Derivative(3) >>> f1.Integral(0,3) >>> f1.Draw() : created default TCanvas with name c1 Example  No much difference between CINT and Python !

8/3/02Using ROOT classes in Python5 Why Python?  Interpreted –It is quite fast (byte code idea from Java)  Dynamically typed –No need to declare any variable  Simple syntax –Emphasis by the authors to minimize typing  Variety of available shells  Powerful built-in types and modules  Ideal for Scripting and Prototyping  Very popular nowadays (many users, many books(>20), many available modules (~1500), etc.)

8/3/02Using ROOT classes in Python6 Python Language  Features: –Variables & Arithmetic expressions –String manipulations –Conditionals (if/else statements) –Loops (for/while) –Functions –Lists –Dictionaries –Classes (Objects) –Exceptions –Modules

8/3/02Using ROOT classes in Python7 Another example import sys, string infile = open( 'aptuple.txt', 'r' ) lines = infile.readlines() title = lines[0] labels = string.split( lines[1] ) from rootmodule import TFile,TNtuple outfile = TFile('aptuple.root','RECREATE',‘NTuple file') ntuple = TNtuple('ntuple', title, string.join(labels,':')) for line in lines[2:]: words = string.split( line ) row = map( float, words ) apply( ntuple.Fill, row ) outfile.Write() CERN Personnel Category Division Flag Age Service … … makerootntuple.py For each line in the file, split the line into a list of words. Convert the list of words into a list of floating-point numbers. And finally, call the Fill() method of the ntuple with it.

8/3/02Using ROOT classes in Python8 Software requirements  Generic Interface –Make use of the generic python functions (__call__, __getattr__, …) to give illusion that all types are known to Python –Use ROOT dictionary information at run-time (perhaps sacrificing some performance). No need to generate any code/library.  Emulating ROOT look and semantics while being Python natural –Same ROOT class names, method names, global variables, etc. –Python object instantiation, namespaces, garbage collection, etc.  The “rootmodule” extension module should be self- contained –Nothing else should be required except rootmodule.so[.dll]

8/3/02Using ROOT classes in Python9 Design TClassTClassWrap TMethodWrap TObjectWrap TCArrayWrap TObject TMethod RootModule CINT Boost.Python Other ROOT Libraries TROOTWrap Python interpreter

8/3/02Using ROOT classes in Python10 Boost.Python  Boost.Python library helps exporting C++ classes to Python.  It is designed to be minimally intrusive on your C++ design.  You need to reflect the C++ classes and functions into Python python::class_builder tobject(this_module, "TObjectW"); tobject.def(&TObjectWrap::getValue, "__getattr__"); tobject.def(&TObjectWrap::getName, "GetName"); tobject.def(&TObjectWrap::className, "ClassName"); tobject.def(&TObjectWrap::members, "members"); tobject.def(&TObjectWrap::repr, "__repr__"); tobject.def(&TObjectWrap::del, "__del__"); Python names C++ class/method

8/3/02Using ROOT classes in Python11 Boost.Python (cont’d) >>> f1 = TF1('func1','sin(x)/x',0,10) >>> dir(f1) ['ClassName', 'GetName', '__del__', '__doc__', '__getattr__', '__module__', '__repr__', 'members'] >>> type(TF1) >>> type(f1) >>> type(f1.Draw)  From the point of view of Python all classes are of type TClassW, all objects are of type TObjectW, all methods are of type TMethodW

8/3/02Using ROOT classes in Python12 Mapping ROOT from Python  Types –Trivial mapping of basic types (float, string, …) –ROOT Classes into instances of TClassW python class –Not yet mapping of TCollection into python built-in types (list?, dictionary?).  Global variables –Created instances with names gROOT, gSystem, etc.  C-arrays –ROOT uses C arrays (e.g. float[], float*) and Python can not handle them. Created special type to handle it.

8/3/02Using ROOT classes in Python13 Known Problems  The overall performance on calling methods is not optimal. –This is mainly due to the overheads in argument conversions. (Python types->C++ types->text string-> CINT interpretation->method) –Using the CINT dictionary directly should improve it.  Calling python from CINT is not working if in different thread. –E.g. GUI call-backs on Windows  Not all the basic types are probably supported yet. Specially of the "C" like types like arrays or pointers.

8/3/02Using ROOT classes in Python14 Known problems (cont’d)  Naïve memory management (object ownership). ROOT objects created by Python are deleted by Python.  The ROOT classes are not yet 100% equivalent to native Python classes, therefore some built-in functionality in shells (e.g. command completion) is not working.

8/3/02Using ROOT classes in Python15 It is available for testing  The code can be downloaded from for tests  It currently uses –Boost 1.27, Python 2.2, ROOT 3.03  It is built using CMT for the time being  Some python scripts examples exists at …/tutorials

8/3/02Using ROOT classes in Python16 Python as a Software-Bus  Python could also be seen as a framework where you can plug easily “extension modules” in binary form implemented using other languages. –Very easy and natural to interface to C++ classes (C++ API) –Python should only be the “glue” between modules developed in C++ or other languages –The interface (API) for Python extension modules is quite simple and at the same time very flexible (generic functions)

8/3/02Using ROOT classes in Python17 Module Communication  Communication between two extension modules is always possible using Python primitive types (a) –Example: I would like to display an histogram of Gaudi Gaudi (C++)Lizard (C++) Python IH1D IH1D* Plotter Python Primitives  It is more efficient to communicate using a reference to an interface (AIDA?) (b) –The objects in Python are only references (a) (b) Both ways are possible!!

8/3/02Using ROOT classes in Python18 GUI What is Feasible Python math shell gaudipython DatabaseEDG API GUI Very rich set of Python standard modules Several GUI toolkits XML Very rich set specialized generic modules Gaudi Framework rootpython Root Classes PVSS JPE Java Classes LHC modules Gateways to other frameworks

8/3/02Using ROOT classes in Python19 Example Filling an Excel spreadsheet from a ROOT ntuple # Get the ntuple from the ROOT file import rootmodule hfile = rootmodule.TFile('hsimple.root') ntuple = rootmodule.gROOT.FindObject('ntuple') entries = ntuple.GetEntries() nvar = ntuple.GetNvar() tuple = ntuple.GetArgs() # Initialize Excel import win32com.client excel = win32com.client.Dispatch('Excel.Application') wbook = excel.Workbooks.Add() wsheet = wbook.WorkSheets.Add() wsheet.Name = ntuple.GetTitle() # Fill Excel sheet for i in xrange(500) : ntuple.GetEntry(i) for j in range(nvar) : wsheet.Cells(i+1,j+1).value = tuple[j] # Make Excel sheet visible excel.Visible = 1

8/3/02Using ROOT classes in Python20 Summary  Exercise to proof that is possible to interface ROOT with Python with very little effort. –Demonstrate the power of having an object dictionary  The module is functional and it is already in use to provide access to ROOT files from other applications (e.g. HippoDraw) –Of course, more work is needed to make it complete and fix the current performance problems  Step in the direction of high level integration using a “software-bus” –One more Python “gateway” to other frameworks or languages like DCOM, Java, Gaudi, etc.