3.25 Input and Output.

jHepWork can write and read any collection of events in serialized form. This was already discussed in Sect. :autorefGetting started with jHepWork Java classes3.7 and :autorefGetting started with jHepWork Java classes3.11.3 for few classes.

For large arrays of data, one can write and read data directly to/from files, without keeping the objects in the memory before serialization. Such persistence can be done using the three classes: HFile, HFileXML and HDataBase from the package jhplot.io. The first class writes objects to a serialized file, the second writes to a XML file. The last class builds a raw-level database, so the objects can be extracted using keys.

The example below shows how to write a list of events to an external serialized file. Each event was constructed from several objects: a string, P0D array and H1D histogram. Run this scrip in jHepWork editor to see what it does:

from jhplot  import *
from jhplot.io import *
import os.path

# make event as a list of strind, P0D and histogram
def makeEvent(entry):
    event=[]
    label="Event="+str(i)
    p=P0D(label)
    p.randomUniform(10,0,1)
    h=H1D(label,10,-1,1)
    h.fill(i)
    event.append(label)
    event.append(p)
    event.append(h)
    return event

# write events to serialized files
file="output.ser"
f=HFile(file,"w")
Events=1000
for i in range(Events):
       event=makeEvent(i)
       if (i%100 == 0):
           print event[0]+" size=",os.path.getsize(file)
       f.write(event)
f.close()

# read all the entries 
f=HFile(file)
while(1):
       event=f.read()
       if event == None:
                  print "End of events"
                  break
       print event[0]
       p=event[1]
       h=event[2]
       # print p.toString()
       # print h.toString()
print "No of processed events=",f.getEntries()
f.close()

One can also write events to a XML file (replace HFile with HFileXML. Events can also be put to a database and extracted using keys (should be strings).

Look at the examples: io_hfile.py, io_hfileXML.py and io_dbase.py.