1 | #!/usr/bin/python -tt
|
---|
2 | # ********************************
|
---|
3 | # Test script for the CalFits class
|
---|
4 | #
|
---|
5 | # written by Thomas Kraehenbuehl, ETH Zurich
|
---|
6 | # tpk@phys.ethz.ch, +41 44 633 3973
|
---|
7 | # April 2012
|
---|
8 | # ********************************
|
---|
9 |
|
---|
10 | datafilename = '/fact/raw/2012/04/17/20120417_004.fits.gz'
|
---|
11 | calibfilename = '/fact/raw/2012/04/17/20120417_003.drs.fits.gz'
|
---|
12 |
|
---|
13 | import numpy as np
|
---|
14 | from scipy import weave
|
---|
15 | from scipy.weave import converters
|
---|
16 |
|
---|
17 | from ROOT import gSystem
|
---|
18 | gSystem.Load("calfits_h.so")
|
---|
19 | from ROOT import *
|
---|
20 | print "Testing object creation: "
|
---|
21 | caltest = CalFits(datafilename,calibfilename)
|
---|
22 | npcalevent = np.empty( caltest.npix * caltest.nroi, np.float64) #.reshape(caltest.npix ,caltest.nroi)
|
---|
23 | caltest.SetNpcaldataPtr(npcalevent)
|
---|
24 |
|
---|
25 | print "Common variables:"
|
---|
26 | print "ROI: ", caltest.nroi
|
---|
27 | print "#Pix: ", caltest.npix
|
---|
28 | print "Number of events: ", caltest.nevents
|
---|
29 | print
|
---|
30 |
|
---|
31 | print "Information per Event:"
|
---|
32 | caltest.GetCalEvent()
|
---|
33 | print "Event ID: ", caltest.event_id
|
---|
34 | print "Trigger type: ", caltest.event_triggertype
|
---|
35 | print "Uncalibrated data: ", caltest.event_data
|
---|
36 | print "Calibrated data: ", caltest.npcaldata
|
---|
37 | print "Board times: ", caltest.event_boardtimes
|
---|
38 | print "Trigger offsets: ", caltest.event_offset
|
---|
39 | print
|
---|
40 |
|
---|
41 | print "Examples of other information"
|
---|
42 | print "Calibfile ROI: ", caltest.calib_nroi
|
---|
43 | print "Column size BaselineMean: ", caltest.calibfile.GetN("BaselineMean")
|
---|
44 | print "Datafile ROI: ", caltest.data_nroi
|
---|
45 | print "Data: ", caltest.datafile.GetN("Data")
|
---|
46 | print "StartCellData: ", caltest.datafile.GetN("StartCellData")
|
---|
47 | print "Direct datafile access: ", caltest.datafile.GetN("StartCellData")
|
---|
48 | print
|
---|
49 | print "Columns of the datafile: "
|
---|
50 | caltest.datafile.PrintColumns()
|
---|
51 |
|
---|
52 | #AND WE HAVE A WINNER: 43 Hz with scipy.weave!
|
---|
53 | while caltest.GetCalEvent():
|
---|
54 | c_integrals = np.zeros(40000, np.float64) #Allocate the memory for about 40000 singles
|
---|
55 | if caltest.event_id>300:
|
---|
56 | break
|
---|
57 |
|
---|
58 | cppcode = """
|
---|
59 | //ToDo: get nroi and npix, check singles<40000
|
---|
60 | double slice, lastslice, integral;
|
---|
61 | int singles = 0;
|
---|
62 | double threshold = 2.5;
|
---|
63 | for(int px=0; px<npix; px++) {
|
---|
64 | lastslice = npcalevent(px*nroi);
|
---|
65 | for(int sl=1; sl<nroi-14; sl++) {
|
---|
66 | slice = npcalevent(px*nroi+sl);
|
---|
67 | if((lastslice<threshold)&&(slice>threshold)) {
|
---|
68 | integral = 0;
|
---|
69 | for(int l=5; l<15; l++) {
|
---|
70 | integral += npcalevent(px*nroi+sl+l);
|
---|
71 | }
|
---|
72 | c_integrals(singles) = integral;
|
---|
73 | ++singles;
|
---|
74 | // std::cout << sl << " ";
|
---|
75 | }
|
---|
76 | lastslice = slice;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | // std::cout << std::endl << singles << std::endl;
|
---|
80 | """
|
---|
81 |
|
---|
82 | #Passing the caltest object to weave is not possible
|
---|
83 | #The int() statement is necessary to add additional data type information to the variable (ie. otherwise some things would work in the C-code, other would not)
|
---|
84 | nroi = int(caltest.nroi)
|
---|
85 | npix = int(caltest.npix)
|
---|
86 |
|
---|
87 | weave.inline(cppcode, ['c_integrals', 'npcalevent','nroi','npix'], type_converters=converters.blitz)
|
---|
88 |
|
---|
89 | # print "CalFitsPerformanceWeave"
|
---|
90 | # for i in range(10):
|
---|
91 | # print i, npcalevent[i], c_integrals[i]
|
---|
92 | # for x in c_integrals:
|
---|
93 | # if x:
|
---|
94 | # print x,
|
---|
95 |
|
---|
96 | # #Old attempts using numpy functions
|
---|
97 | # npcalevent = npcalevent.reshape((caltest.npix, caltest.nroi))
|
---|
98 | # npthr = npcalevent>2.5 #true if above threshold
|
---|
99 | # npthr = npthr[:,1:-14] * np.logical_not(npthr[:,:-15]) #only true for the zero crossings, shape (1400,285) [smaller due to necessary integration range]
|
---|
100 | ## print [(x,y) for x,y in zip(npthr.nonzero()[0],npthr.nonzero()[1])] #print the coordinates, range 0-1399,0-284
|
---|
101 | #
|
---|
102 | # #Various versions to get the integral, all with approximately the same miserable speed (3 Hz), except for the last one (vectorized function) with ~4.3 Hz
|
---|
103 | # #Missing: add deadtime after an integration, remove start & end of the array
|
---|
104 | ## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in np.transpose(npthr.nonzero())]
|
---|
105 | ## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in zip(npthr.nonzero()[0],npthr.nonzero()[1])]
|
---|
106 | ## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in itertools.izip(npthr.nonzero()[0],npthr.nonzero()[1])]
|
---|
107 | ## integrals = map((lambda index_a,index_b: np.sum(npcalevent[index_a,index_b+5:index_b+15])),npthr.nonzero()[0],npthr.nonzero()[1])
|
---|
108 | # integrals = vecoffsum(npthr.nonzero()[0],npthr.nonzero()[1])
|
---|
109 | # print len(integrals)
|
---|
110 |
|
---|
111 | # print caltest.event_id, caltest.event_triggertype, caltest.event_caldata[10]
|
---|
112 | # pass
|
---|
113 |
|
---|
114 | del caltest
|
---|