source: fact/tools/pyscripts/sandbox/vogler/CalFitsPerformanceWeave.py@ 17409

Last change on this file since 17409 was 14173, checked in by vogler, 12 years ago
inital filling of my sandbox
File size: 4.4 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<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
114del caltest
Note: See TracBrowser for help on using the repository browser.