3.7 Histogram input/output

All jHepWork objects can be saved in a serialized file. One can save a collection of histograms (or any objects) in a file as shown here:

from java.awt import Color,Font
from jhplot  import *
from jhplot.io import *
from java.util import Random

# define dictionary
hold = {}

h1=H1D('Simple1',20, -2.0, 2.0)
h1.setFill(1)
h1.setFillColor(Color.green)

h11=H1D('Simple2',20, -2.0, 2.0)
h11.setFill(0)

# fill histograms
r=Random()
for i in range(1000):
      h1.fill(r.nextGaussian())
      h11.fill(r.nextGaussian()+1)

hold["describe"] = "Collection of histograms"
hold["h1"]=h1
hold["h11"]=h11

# write to a file in the user macros directory
Serialized.write(hold,'file.ser')

In this example, we have created a dictionary and filled it with two histograms and a short description. Two keys are used to access histogram from the serialized file.

The example below shows how to read the serialized file: first we read the dictionary and then we use the keys to read the histograms:

c1 = HPlot('Canvas',600,400)
c1.visible(1)
c1.setAutoRange()

# deserialize dictionary from the file
hold=Serialized.read('file.ser' )

# print all keys
print hold.keys()

print "Description: "+hold["describe"]
c1.draw(hold["h1"])
c1.draw(hold["h11"])

Use the same approach to read and write any jHepWork object, including H2D, HProf1D, H3D, HProf2D, P0D, P1D, P2D, PND objects.