Reading data from external files using HFile
Code: "io_data.py". Programming language: Python DMelt Version 1. Last modified: 03/03/2021. License: Pro
https://datamelt.org/code/cache/io_data_5236.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


 
from jhplot import *

print "P1D write & read"
data="""# example of P1D data
12 33
44 32
25 11
23 13
33 2
22 33
44 3
64 26
61 46
26 26
"""
file = open("p1d.d", "w")
file.write(data)
file.close()
# no reading this file
p1=P1D("data1","p1d.d") # create P1D from external file 
print p1.toString()


print "PND write & read"
data="""# example of PND data
2 3 4 2
8 2 3 1
7 1 2 3
7 2 3 4
2 3 4 5
1 2 2 4
3 3 4 5
2 2 1 3
8 8 8 3
1 3 5 1
2 3 1 3
4 5 6 1
2 1 0 0
2 3 4 5
6 7 8 2
1 2 3 5
2 3 1 2
1 2 4 2
3 5 5 5
1 2 3 4
2 8 9 1
2 9 7 1
1 1 2 1
1 2 1 1
8 2 3 1
9 9 9 1
1 2 2 1
7 5 6 1
8 8 6 1
0 0 1 0
4 4 3 1
"""
file = open("pnd.d", "w")
file.write(data)
file.close()
pn=PND("data3","pnd.d") # create PND from external file 
print pn.toString()

print "Read the same data but from URL"
www="https://datamelt.org/examples/data/"

p0=P0D("URL data0",www+"p0d.d") # create P0D from external file 
print p0.toString()

p1=P1D("URL data1",www+"p1d.d") # create P1D from external file 
print p1.toString()

pn=PND("URL data3",www+"pnd.d") # create PND from external file 
print pn.toString()

# write a serialized file  
from jhplot.io  import *
f=HFile("output_io_hfile.ser", "w")
list=[p0,p1,pn]
f.write(list)
f.close()


# read serialized data from URL  
from jhplot.io  import *
f=HFile("output_io_hfile.ser")
while(1):
       event=f.read()
       if event == None:
                  print "End of events"
                  break
       print "read=",type(event[0]),type(event[1]),type(event[2])
       p=event[1]
       h=event[2]
       # print p.toString()
       # print h.toString()
print "No of processed events=",f.getEntries()


Ads help maintain this website.