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

Last change on this file since 13452 was 13452, checked in by kraehenb, 13 years ago
Reinserted the unused code parts in CalFitsPerformanceWeave for better comparison of the Weave and numpy approaches.
File size: 4.1 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
14from scipy import weave
15from scipy.weave import converters
16
17from ROOT import gSystem
18gSystem.Load("calfits_h.so")
19from ROOT import *
20print "Testing object creation: "
21caltest = CalFits(datafilename,calibfilename)
22npcalevent = np.empty( caltest.npix * caltest.nroi, np.float64) #.reshape(caltest.npix ,caltest.nroi)
23caltest.SetNpcaldataPtr(npcalevent)
24
25print "Common variables:"
26print "ROI: ", caltest.nroi
27print "#Pix: ", caltest.npix
28print "Number of events: ", caltest.nevents
29print
30
31print "Information per Event:"
32caltest.GetCalEvent()
33print "Event ID: ", caltest.event_id
34print "Trigger type: ", caltest.event_triggertype
35print "Uncalibrated data: ", caltest.event_data
36print "Calibrated data: ", caltest.npcaldata
37print "Board times: ", caltest.event_boardtimes
38print "Trigger offsets: ", caltest.event_offset
39print
40
41print "Examples of other information"
42print "Calibfile ROI: ", caltest.calib_nroi
43print "Column size BaselineMean: ", caltest.calibfile.GetN("BaselineMean")
44print "Datafile ROI: ", caltest.data_nroi
45print "Data: ", caltest.datafile.GetN("Data")
46print "StartCellData: ", caltest.datafile.GetN("StartCellData")
47print "Direct datafile access: ", caltest.datafile.GetN("StartCellData")
48print
49print "Columns of the datafile: "
50caltest.datafile.PrintColumns()
51
52#AND WE HAVE A WINNER: 43 Hz with scipy.weave!
53while 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
108del caltest
Note: See TracBrowser for help on using the repository browser.