source: fact/tools/pyscripts/sandbox/kraehenb/CalFitsPerformanceWeave.py@ 13449

Last change on this file since 13449 was 13448, checked in by kraehenb, 12 years ago
Newest performance test script files for the CalFits class, including a fast version using scipy.weave.
File size: 4.3 KB
Line 
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
10datafilename = '/fact/raw/2012/04/17/20120417_004.fits.gz'
11calibfilename = '/fact/raw/2012/04/17/20120417_003.drs.fits.gz'
12
13import numpy as np
14import itertools
15from scipy import weave
16from scipy.weave import converters
17
18from ROOT import gSystem
19gSystem.Load("calfits_h.so")
20from ROOT import *
21print "Testing object creation: "
22caltest = CalFits(datafilename,calibfilename)
23npcalevent = np.empty( caltest.npix * caltest.nroi, np.float64) #.reshape(caltest.npix ,caltest.nroi)
24caltest.SetNpcaldataPtr(npcalevent)
25
26print "Common variables:"
27print "ROI: ", caltest.nroi
28print "#Pix: ", caltest.npix
29print "Number of events: ", caltest.nevents
30print
31
32print "Information per Event:"
33caltest.GetCalEvent()
34print "Event ID: ", caltest.event_id
35print "Trigger type: ", caltest.event_triggertype
36print "Uncalibrated data: ", caltest.event_data
37print "Calibrated data: ", caltest.npcaldata
38print "Board times: ", caltest.event_boardtimes
39print "Trigger offsets: ", caltest.event_offset
40print
41
42print "Examples of other information"
43print "Calibfile ROI: ", caltest.calib_nroi
44print "Column size BaselineMean: ", caltest.calibfile.GetN("BaselineMean")
45print "Datafile ROI: ", caltest.data_nroi
46print "Data: ", caltest.datafile.GetN("Data")
47print "StartCellData: ", caltest.datafile.GetN("StartCellData")
48print "Direct datafile access: ", caltest.datafile.GetN("StartCellData")
49print
50print "Columns of the datafile: "
51caltest.datafile.PrintColumns()
52
53def offsum(x,y):
54 return np.sum(npcalevent[x,y+5:y+15])
55vecoffsum = np.vectorize(offsum) #specifying otypes=[np.float/double] does not improve speed
56
57#AND WE HAVE A WINNER: 35 Hz with scipy.weave!
58while caltest.GetCalEvent():
59 c_integrals = np.zeros(40000, np.float64) #Allocate the memory for about 40000 singles
60 if caltest.event_id>300:
61 break
62
63 cppcode = """
64 //ToDo: get nroi and npix, check singles<40000
65 double slice, lastslice, integral;
66 int singles = 0;
67 double threshold = 2.5;
68 for(int px=0; px<1440; px++) {
69 lastslice = npcalevent(px*300);
70 for(int sl=1; sl<300-14; sl++) {
71 slice = npcalevent(px*300+sl);
72 if((lastslice<threshold)&&(slice>threshold)) {
73 integral = 0;
74 for(int l=5; l<15; l++) {
75 integral += npcalevent(px*1440+sl+l);
76 }
77 c_integrals(singles) = integral;
78 ++singles;
79// std::cout << sl << " ";
80 }
81 lastslice = slice;
82 }
83 }
84// std::cout << std::endl << singles << std::endl;
85
86 """
87 weave.inline(cppcode, ['c_integrals', 'npcalevent'], type_converters=converters.blitz)
88# print "CalFitsPerformanceWeave"
89# for i in range(10):
90# print i, npcalevent[i], c_integrals[i]
91# for x in c_integrals:
92# if x:
93# print x,
94
95# npcalevent = npcalevent.reshape((caltest.npix, caltest.nroi))
96# npthr = npcalevent>2.5 #true if above threshold
97# npthr = npthr[:,1:-14] * np.logical_not(npthr[:,:-15]) #only true for the zero crossings, shape (1400,285) [smaller due to necessary integration range]
98## print [(x,y) for x,y in zip(npthr.nonzero()[0],npthr.nonzero()[1])] #print the coordinates, range 0-1399,0-284
99#
100# #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
101# #Missing: add deadtime after an integration, remove start & end of the array
102## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in np.transpose(npthr.nonzero())]
103## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in zip(npthr.nonzero()[0],npthr.nonzero()[1])]
104## integrals = [np.sum(npcalevent[x,y+5:y+15]) for x,y in itertools.izip(npthr.nonzero()[0],npthr.nonzero()[1])]
105## integrals = map((lambda index_a,index_b: np.sum(npcalevent[index_a,index_b+5:index_b+15])),npthr.nonzero()[0],npthr.nonzero()[1])
106# integrals = vecoffsum(npthr.nonzero()[0],npthr.nonzero()[1])
107# print len(integrals)
108
109# print caltest.event_id, caltest.event_triggertype, caltest.event_caldata[10]
110# pass
111
112del caltest
Note: See TracBrowser for help on using the repository browser.