4. Jython scripting with jHepWork

In Chapter :autorefGetting started with jHepWork Java classes3 we have considered a programming style with the direct calls to the jHPlot Java classes. But Java does not support operator overloading, which is one of important features of object-oriented programming. One can easily see a problem if one wants to do a lot of elementary operations with objects, like to add them, subtract etc. histograms or data containers. In this case, one should use rather long Java methods. Of course, for mathematical operations, it is more convenient to use +,-,*,/.

JHepWork allows to do this by calling the corresponding Jython classes, which are directly mapped to the Java jHPlot classes. Such Jython classes inherit all methods of the Java jHPlot classes. At the same time, the most common arithmetical operators are overloaded. The package which allows to do this is called shplot (i.e. "scripted" or "simple" h-plot), in contrast to jhplot ("Java h-plot")

Below is an example of how build Jython histograms and to perform the some common operations

>>> from java.util import Random
>>> from  shplot import *
>>>
>>> c1=hplot("scripting",1,2) # build a canvas with 2 plots
>>> h1=h1d("histogram1",200,-5,5) # make histogram 1
>>> h2=h1d("histogram2",200,-5,5) # make histogram 1
>>> r=Random()                    # fill histograms
>>> for i in range(500):
          h1.fill(r.nextGaussian())
          h2.fill(0.2*r.nextGaussian())
>>> h1=h1+h2                      # add 2 histograms
>>> c1.draw(h1)                   # draw
>>> h1=h1*2                       # scale by a factor 2
>>> c1.cd(1,2)                    # go to the next plot
>>> h1=h1-h2                      # subtract 2 histograms
>>> h1=h1/h2                      # divide  2 histograms 
>>> h1=h1*10                      # scale by factor 10 
>>> h1=h1/100                     # divide by 100

One should note that all Jython classes which inherit Java jHPlot classes have exactly the same names as the corresponding Java classes of jHPlot. The only difference is that Jython classes have to be typed in lower case. This means that the statement:

>>> c1=HPlot("scripting",1,2) # calls Java HPlot class
creates an instance of the Java class HPlot. However, when one uses the lower case for the same class name, the corresponding Jython instance is created:

>>> c1=hplot("scripting",1,2) # call Jython "hplot"  class
In this case, some arithmetical operators are overloaded. If the user wants to subtract, add, divide, scale the data, he/she should use -, +, /, *, *factor, instead of the long oper(...) methods.

When the user draws an object, say a histogram, on a canvas, one can say that a histogram was added to the canvas. So, why not instead of draw() method just to use +? Yes, this is also possible: The code below shows how to draw 3 histograms on the same canvas using a single line:

>>> c1+h1+h2+h3  # draw 3 histograms h1,h2,h3 on the canvas c1

Of course, the same operation can be done for any other class which can be drawn on the canvas (H1D,P1D). Note: subtraction of objects is not yet implemented for hplot Jython class.

In the same way, data can be shown in a table.

>>> t=htable()
>>> t+h1        # show histogram in a table

One can write even a shorter program without introducing the variable t:

>>> htable()+h1        # show histogram in a table

As before, one should use the jHepWork Code assist to learn more about Jython/Java methods. You may note that not all methods have been mapped to the corresponding Java class (due to luck of time). For example, one can look at the Jython classes of a H1D histogram as

>>> from  shplot import *
>>> h1=h1d("test",200,-5,5) # build a Jython histogram based on H1D 
>>> h1.                     # press [F4] to see all methods

You can notice that all jHplot Java methods are inherited by the Jython class h1d. Thus, even if you write a code using Jython shplot classes, one can easily access the Java classes:

>>> from  shplot import *
>>> h1=h1d("test",200,-5,5) # build a Jython histogram based on H1D 
>>> h1.setFill(1)           # accesses H1D and calls setFill() Java method.
In the example above, setFill() was applied directly to the Java class H1D, rather than to the Jython class h1d.

All Jython shplot classes created so far are located in macros/shplot directory. In the same directory, one can find examples as well (their names contains _test).



Subsections