vmod/vmodttl: fixed bug related to luns not ordered and/or not starting from zero.
[ht-drivers.git] / sis33 / test / sis3300cont_acq.py
blob8c54973bd0830f06de51e43bf9ae4698d5a358c2
1 #!/usr/bin/python
2 # It needs Python 2.7 or higher!
4 from ctypes import *
5 from multiprocessing import Process
6 import string
7 import optparse
8 import time
10 # -------------------------------------------------------------
11 # Preliminary declarations
12 # -------------------------------------------------------------
13 class SIS33_ACQ (Structure):
14 _fields_ = [("data",POINTER(c_uint16)),
15 ("nr_samples" , c_uint32),
16 ("prevticks", c_uint64),
17 ("size", c_uint32)]
19 class TIMEVAL (Structure):
20 _fields_ = [("tv_sec", c_uint32),
21 ("tv_usec", c_uint32)]
23 def print_header():
24 print ('')
25 print ('\t SIS3300 Testing program \t')
26 print ('\t for continous acquisition \t')
27 print ('')
28 print ('Author: Samuel I. Gonsalvez - BE/CO/HT, CERN ')
29 print ('Version: 1.0')
30 print ('License: 3-clause BSD license.')
31 print ('')
32 raise SystemExit
34 def check_arguments(lun, chan):
35 if chan < 0 or chan > 7 :
36 print "Error invalid channel:", chan
37 raise SystemExit
39 f = open('/sys/class/sis33/sis33.' + repr(lun) + '/description', 'r')
40 desc = f.readline()
41 f.close()
42 if not "SIS3300" in desc :
43 print "It is not a SIS3300:", desc
44 raise SystemExit
47 def config_sis(device, chan):
48 libsis.sis33_set_clock_source(device, 1) # External clock
49 libsis.sis33_set_start_auto(device, 1)
50 libsis.sis33_set_stop_auto(device, 1)
51 libsis.sis33_set_start_delay(device, 0)
52 libsis.sis33_set_stop_delay(device, 0)
54 def acquisition(device, lun, chan, segment, n_acqs, path, num_samp):
55 puntero = POINTER(SIS33_ACQ)
56 libsis.sis33_acqs_zalloc.restype = puntero
57 acqs = libsis.sis33_acqs_zalloc(1, num_samp)
59 endtime = TIMEVAL()
60 fh = None
61 s = '/tmp/sis33loop.' + repr(lun) +'.ch'+repr(chan) +'.seg'+repr(segment)+'.num'+repr(0)+'.dat'
62 t1 = time.time()
63 print "Using", num_samp,"samples for each file"
64 print "Start time: ", t1
65 for i in range(0,n_acqs/2):
66 if libsis.sis33_acq_wait(device, 0, 1, num_samp) < 0 :
67 libsis.sis33_perror("sis33_acq_wait")
68 found_error(device)
70 if libsis.sis33_acq(device, 1, 1, num_samp) < 0 :
71 libsis.sis33_perror("sis33_acq_wait")
72 found_error(device)
74 if libsis.sis33_fetch(device, 0, chan, byref(acqs[0]), 1, byref(endtime)) < 0 :
75 libsis.sis33_perror("sis33_fetch")
76 found_error(device)
78 p = Process(target=write_data, args=(acqs[0].data, 131072, i, lun, chan, 0, path))
79 p.start()
81 if libsis.sis33_fetch_wait(device, 1, chan, byref(acqs[0]), 1, byref(endtime)) < 0 :
82 libsis.sis33_perror("sis33_fetch")
83 found_error(device)
85 p = Process(target=write_data, args=(acqs[0].data, 131072, i, lun, chan, 1, path))
86 p.start()
88 t2 = time.time()
89 print "Finish time: ", t2
90 print "Acquisition Time:", (t2 - t1), "seconds"
91 libsis.sis33_acqs_free(acqs, 1)
93 def calculate_time(time, freq, num_samp):
94 tmp = freq*time
95 tmp = tmp / num_samp + 1
96 return tmp
98 def write_data(data, size, num_fich, lun, chan, segment, path):
99 s = path+'/sis33loop.' + repr(lun) +'.ch'+repr(chan) +'.seg'+repr(segment)+'.num'+repr(num_fich)+'.dat'
100 f = open(s, 'w')
101 f.write("#event segment " + repr(segment) + "\n")
102 for i in range(0, size-1):
103 s = hex(data[i]) + '\n'
104 f.write(s)
106 def found_error(device):
107 libsis.sis33_acq_cancel(device)
108 exit_program(device)
110 def exit_program(device):
111 libsis.sis33_close(device)
112 raise SystemExit
114 if __name__ == '__main__' :
116 parser = optparse.OptionParser()
117 parser.add_option("-d", "--device", dest="lun", type="int",
118 help =("Logical Unit Number of the SIS33xx device. [default: %default]"))
119 parser.add_option("-c", "--channel", dest="chan", type="int",
120 help =("Channel to setup the acquisition. [default: %default]"))
121 parser.add_option("-t", "--time", dest="time", type="int",
122 help =("Time acquisition in seconds. [default: %default]"))
123 parser.add_option("-p", "--path", dest="path", type="str",
124 help =("Path to save the data. [default: %default]"))
125 parser.add_option("-f", "--frequency", dest="freq", type="int",
126 help =("Frequency of external clock [Hz]. [default: %default]"))
127 parser.add_option("-l", "--lib", dest="lib", type="str",
128 help =("Path to the shared library libsis33.L865.so. [default: %default]"))
129 parser.add_option("-v", "--version", action="store_true", dest="version")
130 parser.set_defaults(lun=0, chan=0, time=1, version=0, path='/tmp', freq=1000000, lib="../lib")
131 opts, args = parser.parse_args()
133 s = opts.lib + '/libsis33.L865.so'
134 libsis = cdll.LoadLibrary(s);
136 if opts.version :
137 print_header()
139 check_arguments(opts.lun, opts.chan)
141 device = libsis.sis33_open (opts.lun)
142 config_sis(device, opts.chan)
144 num_samp = 131072
145 n_acqs = calculate_time(opts.time, opts.freq, num_samp)
146 acquisition(device, opts.lun, opts.chan, 0, n_acqs, opts.path, num_samp)
148 exit_program(device)