Histograms in 1D

To construct a histogram representing a density distribution of some variable one should follow these 2 steps: construct a histogram object using the H1D class and then fill it.

This is an example using the JHPLOT package (here we are using the Jython syntax instead of Java):

example1.py
from java.util import Random
from jhplot  import *
 
c1 = HPlot("Canvas")
c1.setGTitle("A histogram")
c1.visible()
c1.setAutoRange()
 
h1 = H1D("1D histogram",100, -2, 2)
rand = Random()
for i in range(100):
    h1.fill(rand.nextGaussian())
c1.draw(h1)
c1.drawStatBox(h1)

The output with a statistical summary is plotted as well (the method drawStatBox). By default, the plot shows statistical uncertainties in each bin.

 H1D histogram

In the above example, “100” is the number of bins between -2 and 2, thus all bins are of the same size. You can get some information about histograms as:

  • getBinSize() Get bin width in case of fixed-size bins
  • getDensity() Get a density distribution dividing each bin of the histogram by the bin width and the total number of heights for all bins.
  • getProbability() Return a probability distribution derived from a histogram.
  • integral(i1,i2) Integrate a histogram between two bin indices

See more details in the jHepWork H1D API

Histograms in 2D

Build a histogram in 2 dimensions using the Java class H2D. This is an example using the JHPLOT package (here we are using again Jython syntax, instead of Java):

example.py
from java.util import Random
from jhplot  import *
 
# build a standard 3D canvas
c1 = HPlot3D("Canvas")
c1.setGTitle("Global title")
c1.setNameX("X")
c1.setNameY("Y")
c1.visible(1)
 
h1 = H2D("2D histogram",10,-3.0, 3.0,10,-3.0, 3.0)
rand = Random();
for i in range(200):
    h1.fill(rand.nextGaussian(),rand.nextGaussian())
c1.draw(h1);

The output with statistical summary is shown here. By default, the plot shows statistical uncertainties in each bin.

Histograms in 3D

Similarly, histograms can be defined in 3D using the class H3D

Histograms with variable bin size

One can use also variable-size bins as:

h1 = H1D("Variable-size bins",[-2,-1,0,2,10])

where the list used in the H1D constructor specifies edges of the bins. Similarly, one can define H2D and H3D histogram by passing 2 lists (one for X, one for Y) or 3 lists (X,Y,Z).

Histogram operations

The histogram classes support many mathematical operations (division, subtraction, multiplication, scaling, shifting, smoothing etc). Histogram arithmetic can be done with the method “oper(h,”New Title”,”operation”)”, where “h” is an object represented a histogram which is used to subtract, divide, multiply and add. All these operations should be defined by a string operation as ”-, /, *, +”, and the histograms must have the same binning. It should also be noted that all such operations take into account propagation of statistical errors for each bin assuming that histograms do not correlate.

from java.util import Random
from jhplot  import * 
h1 = H1D("First",10, -2.0, 2.0)
h2 = H1D("Second",10, -2.0, 2.0)
r = Random()
for i in range(5000):  h1.fill(r.nextGaussian())
for i in range(5000):  h2.fill(r.nextGaussian())
h3=h1.oper(h2,"subtract","-")
h4=h1.oper(h2,"add","+")
h5=h1.oper(h2,"multiply","*")
h6=h1.oper(h2,"divide","/")

A histogram can be scaled by a constant using the method “operScale(title,scaleFactor)”

Jython scripting using histograms

Instead of calling Java classes using the Jython (or Python) language, one can use the native Jython classes based on the Java classes. In this case, many Java methods can conveniently be overloaded. For example, histograms can be added, subtracted, divided and multiplied using the conventional arithmetic operators ”+,-,/,*”. To be able to use Python-derived classes for the histogram objects, import the class “shplot” (“scripting” HPlot package). The histogram classes have the same names, but they start from a lower case. Let us give an example:

scripting.py
import shplot
from java.awt import Color
from java.util import Random
from shplot import *
print shplot.__doc__
 
c1=hplot("scripting",1,2) # build a canvas with 2 plot regions
c1.visible()
c1.auto()
h1=h1d("h1",40,-3,3) # define histograms
h2=h1d("h2",40,-3,3)
h3=h1d("h3",40,-3,3)
r = Random()
 
for i in range(2000):              # fill histograms
      h1.fill(r.nextGaussian())
      h2.fill(0.6*r.nextGaussian()+2)
      if i<1000:  h3.fill(0.5*r.nextGaussian()+1.0)
 
h1.setColor(Color.red)
c1.draw(h1)
 
h12=h1+h2           # add 2 histograms
h12.setFill(1)
h12.setFillColor(Color(20,30,20))
h12.setColor(Color.blue)
c1.draw(h12)
 
h13=h12+h3             # sum 2 histogram and draw
h13.setFill(1)
h13.setFillColor(Color(50,90,20))
c1.draw(h13)
 
scaled=h1*2.5           # scale a histogram by 2.5
scaled.setColor(Color.green)
c1.draw(scaled)
 
c1.cd(1,2)               # create a new plotting region
c1.auto()
h13=h1+h3                # draw the sum of 2 histograms
h13.setColor(Color.blue)
c1.draw(h13)
 
h113=h1-h3                # subtract 2 histograms 
h113.setFill(1)
h113.setColor(Color(10,200,100))
h113.setFillColor(Color(20,200,90))
c1.draw(h113)

The resulting figure is shown here

Histogram scripting using shplot package

histograms.txt · Last modified: 2010/03/28 22:50 by admin
Back to top
GNU Free Documentation License 1.2
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0