jHepWork uses for histogramming package JAIDAhttp://java.freehep.org/jaida/index.html from FreeHEPhttp://java.freehep.org/. The main class for an one-dimensional histogram is the , which is just a simple extension of the JAIDA histogram class ''Histogram1D''. Let us create a 1D histogram and fill it with Gaussian numbers:
>>> from java.util import Random >>> from jhplot import HPlot >>> from jhplot import H1D >>> c1=HPlot("Histogram",600,400) >>> c1.visible(1) >>> c1.setAutoRange() >>> c1.setGTitle("Global title") # set a Title >>> h1=H1D("Simple1",20, -2.0, 2.0) >>> rand=Random() >>> for i in range(100): # fill histogram ... h1.fill(rand.nextGaussian()) >>> c1.draw(h1) # draw histogram
You may notice that the code is significantly shorter than the equivalent code written using JAIDAhttp://java.freehep.org/jaida/index.html. You can access the JAIDA Histogram1D package using the method ``get()''
>>> jaida_histogram = h1.get()
(you have to export the corresponding JAIDA class). You can also convert the standard JAIDA histogram into ``H1D'' class
>>> xAx = new FixedAxis(20,-2.0, 2.0) >>> haida = new Histogram1D("histogram1","histogram1", xAx ) >>> h2 = H1D(haida) # conversion
As before, you can learn about the public methods of H1D by typing ``h2. <Ctrl>+<Space>'', or using ``F4'' key. See Section. :autorefsection10 for more details.
One can also define a histogram with variable size of bins. In this case, you should specify an array of bin edges when constructing the histogram (read the API for details).
Here are the major methods associated with the H1D class:
>>> h1.setFill(1) # fill the histogram (set 0 if no fill) >>> h1.fillColor(Color.green) # fill color >>> h1.errX(0) # do not show horizontal error bar >>> h1.errY(1) # show vertical error bar >>> h1.setPenWidthErr(2) # width of the line >>> h1.fillColorTransparency(0.7) # set transparency level in case of filled histogram >>> h1.errColorY(Color.black) # set color for error bars
It should be noted that the integers in setFill(), errX(), errY() means logical true (jython sysntax). For BeanShell or JAVA, you should use ``true'' and ``false''. You can edit the plot using a pop-up menu ``Edit'' abd set all the attribute using the style editor.
If you have two H1D histograms, one can do the following operations:
>>> h3=h1.oper(h2,"New Title","+") # add h2 to h1 >>> h3=h1.oper(h2,"New Title","-") # subtract h2 from h1 >>> h3=h1.oper(h2,"New Title","*") # multiply h1 by h2 >>> h3=h1.oper(h2,"New Title","/") # divide h1 by h2
One can skip the string with a new title if you want to keep the same title as for the original histogram. For example, in this case, the additive operation will be ``h3=h1.oper(h1,"+")''
To scale a histogram, use a similar statement:
>>> h3=h1.oper("New Title", scaleFactor )where ``scaleFactor'' is double or integer number. The title string is optional.
Here are some other useful methods associated with H1D:
>>> h1=H1D(Histogram1D h1) # create H1D histogram from JAIDA (Histogram1D) >>> h1=H1D(IHistogram1D h1) # create H1D histogram from JAIDA (IHistogram1D) >>> h1.setTitle("Title") # set Title >>> h1.getTitle() # get the Title (return string) >>> hh=h1.get() # return JAIDA Histogram1D >>> h1.getMin() # get Min value (return double) >>> h1.getMax() # get Max value >>> h1.fill(value) # fill a histogram with "value" >>> h1.fill(value, weight) # fill a histogram, using "weight" >>> h1.mean() # get mean value (return double) >>> h1.rms() # get RMS (return double) >>> h1.allEntries() # all entries (return int) >>> h1.extraEntries() # under and overflow entries >>> h1.entries() # number of in-range entries >>> h1.maxBinHeight() # get the maximum height of in-range bins >>> h1.minBinHeight() # get the minimum height of in-range bins >>> h1.sumAllBinHeights() # get sum of the bin heights for all the entries >>> h1.print() # print H1D to the screen >>> h1.toFile("File") # write H1D to a file with the name "File" >>> h1.toTable() # write H1D to Table for sorting and searching
One can access the information in each bin which is characterized by the index "i" using the following methods
>>> h1.binEntries(i) # get entries in the corresponding bin >>> h1.binError(i) # error on this bin >>> h1.binHeight(i) # total height of the corresponding bin >>> h1.binMean(i) # get the mean of a bin >>> h1.binRms(i) # get the RMS of a bin
Read the full documentation of JHPlot package.http://hepforge.cedar.ac.uk/jhepwork/api/jhplot/package-summary.html.
ny Johnson's JHPlot package.hep.io.root package. To view ROOT histograms, open a Root Histogram viewer as:
>>> BRoot() # open a viewer in a separate frame, or >>> BRoot("input.root") # open a ROOT file
The ``BRoot'' class is a simple wrapper of the HistogramBrowser class from FreeHEP. You can find the relevant example in ``root_browser.py'' file.
You can read ROOT histogram files inside the Jython program and convert them to the H1D class using the following construction:
>>> key=rfr.getKey("mainHistogram") # get a ROOT key >>> main=rfr.getKey("mainHistogram").getObject() # get the standard Jaida histogram >>> h1=H1D(main) # convert it to H1D histogram
Then the ``h1'' histogram can be plotted in the usual way using ``draw()'' method and ``HPlot'' canvas. Look at the example file in ``histo_root.py'' which creates and displays ROOT histograms.