1+ ##################################################################
2+ ## Python interface for reading HIPO files. ##
3+ ## This interface is using fusion class from the hipo4 module ##
4+ ## with "C" extern intraface defined in wrapper.cpp class. ##
5+ ## the fusion interface allows opening multiple files through ##
6+ ## providing unique handles for each opened file. ##
7+ ## ##
8+ ## Author: G.Gavalian (2022) ##
9+ ## Jefferson Lab -------------------------------- ##
10+ ##################################################################
11+
12+ import ctypes
13+
14+ class hreader :
15+
16+ def __init__ (self ,libfilename ):
17+ """ hipo reader python class interface to hipo fusion wrapper
18+ (use it wisely)
19+ """
20+ self .libPath = libfilename
21+ self .hipo4lib = ctypes .CDLL (self .libPath + '/libhipo4.so' )
22+ self .status = ctypes .c_int (0 )
23+ self .handle = - 1
24+
25+ def open (self ,filename ):
26+ """ open the file and save the handle returned by fusion wrapper.
27+ each opened file has unique handle, so many files can be opened
28+ in parallel.
29+ """
30+ self .inputFile = filename
31+ self .handle = self .hipo4lib .fusion_open (ctypes .c_char_p (self .inputFile .encode ('ascii' )))
32+ print ('file open handle = ' ,self .handle )
33+
34+ def open_with_tag (self ,filename , tag ):
35+ """ open the file and save the handle returned by fusion wrapper.
36+ each opened file has unique handle, so many files can be opened
37+ in parallel.
38+ """
39+ self .inputFile = filename
40+ self .handle = self .hipo4lib .fusion_open_with_tag (ctypes .c_char_p (self .inputFile .encode ('ascii' )), tag )
41+ print ('file open handle = ' ,self .handle )
42+
43+ def define (self ,bankname ):
44+ """ define is used to declare a bank that will be read by the fusion wrapper
45+ each time next() is called. The banks are stored in the internal map for
46+ each opened file handle.
47+ """
48+ self .hipo4lib .fusion_define (self .handle , ctypes .c_char_p (bankname .encode ('ascii' )))
49+
50+ def describe (self ,bankname ):
51+ """ define is used to declare a bank that will be read by the fusion wrapper
52+ each time next() is called. The banks are stored in the internal map for
53+ each opened file handle.
54+ """
55+ self .hipo4lib .fusion_describe (self .handle ,ctypes .c_char_p (bankname .encode ('ascii' )))
56+
57+ def getSize (self ,bankname ):
58+ """ returns size of the bank that was read for current event.
59+ """
60+ size = self .hipo4lib .fusion_bankSize (self .handle ,ctypes .c_char_p (bankname .encode ('ascii' )))
61+ return size
62+
63+ def next (self ):
64+ """ reads next event in the file, and reads all the banks that were decalred with
65+ hreader.define(bankname) function call.
66+ """
67+ status = self .hipo4lib .fusion_next (self .handle )
68+ return status == 1
69+
70+ def getInt (self ,bank ,entry ,row ):
71+ """ returns an integer value for entry and row from requested bank. call getSize()
72+ first to make sure that the row is within the allowable range to avoid hard crashes.
73+ """
74+ a1 = ctypes .c_char_p (bank .encode ('ascii' ))
75+ a2 = ctypes .c_char_p (entry .encode ('ascii' ))
76+ self .hipo4lib .fusion_get_float .restype = ctypes .c_int
77+ value = self .hipo4lib .fusion_get_int (self .handle ,a1 ,a2 ,row )
78+ return value
79+
80+ def getLong (self ,bank ,entry ,row ):
81+ """ returns an integer value for entry and row from requested bank. call getSize()
82+ first to make sure that the row is within the allowable range to avoid hard crashes.
83+ """
84+ a1 = ctypes .c_char_p (bank .encode ('ascii' ))
85+ a2 = ctypes .c_char_p (entry .encode ('ascii' ))
86+ self .hipo4lib .fusion_get_long .restype = ctypes .c_ulonglong
87+ value = self .hipo4lib .fusion_get_long (self .handle ,a1 ,a2 ,row )
88+ return value
89+
90+ def getFloat (self ,bank ,entry ,row ):
91+ """ returns an float value for entry and row from requested bank. call getSize()
92+ """
93+ a1 = ctypes .c_char_p (bank .encode ('ascii' ))
94+ a2 = ctypes .c_char_p (entry .encode ('ascii' ))
95+ self .hipo4lib .fusion_get_float .restype = ctypes .c_float
96+ value = ctypes .c_float (self .hipo4lib .fusion_get_float (self .handle ,a1 ,a2 ,row )).value
97+ return value
98+
99+ def getDouble (self ,bank ,entry ,row ):
100+ """ returns an float value for entry and row from requested bank. call getSize()
101+ """
102+ a1 = ctypes .c_char_p (bank .encode ('ascii' ))
103+ a2 = ctypes .c_char_p (entry .encode ('ascii' ))
104+ self .hipo4lib .fusion_get_float .restype = ctypes .c_double
105+ value = ctypes .c_double (self .hipo4lib .fusion_get_float (self .handle ,a1 ,a2 ,row )).value
106+ return value
107+
108+ def getType (self ,bank ,entry ):
109+ """ returns type of the entry for the given bank. this is used to determine weather to
110+ use getInt or getFloat function.
111+ """
112+ a1 = ctypes .c_char_p (bank .encode ('ascii' ))
113+ a2 = ctypes .c_char_p (entry .encode ('ascii' ))
114+ type = self .hipo4lib .fusion_entry_type (self .handle ,a1 ,a2 )
115+ return type
116+
117+ def getEntry (self ,bank ,entry ):
118+ """ returns a python array containing all rows for given column. the determination of the
119+ is done internaly.
120+ """
121+ rows = self .getSize (bank )
122+ type = self .getType (bank ,entry )
123+ array = []
124+ if (type == 1 or type == 2 or type == 3 ):
125+ for row in range (rows ):
126+ array .append (self .getInt (bank ,entry ,row ))
127+ if (type == 4 ):
128+ for row in range (rows ):
129+ array .append (self .getFloat (bank ,entry ,row ))
130+ if (type == 5 ):
131+ for row in range (rows ):
132+ array .append (self .getDouble (bank ,entry ,row ))
133+ if (type == 8 ):
134+ for row in range (rows ):
135+ array .append (self .getLong (bank ,entry ,row ))
136+
137+ return array
138+
139+ def get_entries (self ):
140+ """Returns the total number of entries (events) in the currently opened file."""
141+ self .hipo4lib .fusion_get_entries .restype = ctypes .c_int
142+ return self .hipo4lib .fusion_get_entries (self .handle )
143+
144+ def show (self , bank : str ):
145+ self .hipo4lib .fusion_show .restype = ctypes .c_voidp
146+ self .hipo4lib .fusion_show (self .handle , ctypes .c_char_p (bank .encode ('ascii' )))
0 commit comments