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<1440; px++) {
|
---|
64 | lastslice = npcalevent(px*300);
|
---|
65 | for(int sl=1; sl<300-14; sl++) {
|
---|
66 | slice = npcalevent(px*300+sl);
|
---|
67 | if((lastslice<threshold)&&(slice>threshold)) {
|
---|
68 | integral = 0;
|
---|
69 | for(int l=5; l<15; l++) {
|
---|
70 | integral += npcalevent(px*1440+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 | weave.inline(cppcode, ['c_integrals', 'npcalevent'], type_converters=converters.blitz)
|
---|
82 |
|
---|
83 | # print "CalFitsPerformanceWeave"
|
---|
84 | # for i in range(10):
|
---|
85 | # print i, npcalevent[i], c_integrals[i]
|
---|
86 | # for x in c_integrals:
|
---|
87 | # if x:
|
---|
88 | # print x,
|
---|
89 |
|
---|
90 | # #Old attempts using numpy functions
|
---|
91 | # npcalevent = npcalevent.reshape((caltest.npix, caltest.nroi))
|
---|
92 | # npthr = npcalevent>2.5 #true if above threshold
|
---|
93 | # npthr = npthr[:,1:-14] * np.logical_not(npthr[:,:-15]) #only true for the zero crossings, shape (1400,285) [smaller due to necessary integration range]
|
---|
94 | ## print [(x,y) for x,y in zip(npthr.nonzero()[0],npthr.nonzero()[1])] #print the coordinates, range 0-1399,0-284
|
---|
95 | #
|
---|
96 | # #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
|
---|
97 | # #Missing: add deadtime after an integration, remove start & end of the array
|
---|
98 | ## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in np.transpose(npthr.nonzero())]
|
---|
99 | ## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in zip(npthr.nonzero()[0],npthr.nonzero()[1])]
|
---|
100 | ## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in itertools.izip(npthr.nonzero()[0],npthr.nonzero()[1])]
|
---|
101 | ## integrals = map((lambda index_a,index_b: np.sum(npcalevent[index_a,index_b+5:index_b+15])),npthr.nonzero()[0],npthr.nonzero()[1])
|
---|
102 | # integrals = vecoffsum(npthr.nonzero()[0],npthr.nonzero()[1])
|
---|
103 | # print len(integrals)
|
---|
104 |
|
---|
105 | # print caltest.event_id, caltest.event_triggertype, caltest.event_caldata[10]
|
---|
106 | # pass
|
---|
107 |
|
---|
108 | del caltest
|
---|