source: fact/tools/pyscripts/doc/examples.rst@ 18752

Last change on this file since 18752 was 14471, checked in by neise, 12 years ago
fixed identation of former checkin
File size: 4.5 KB
Line 
1========
2Examples
3========
4
5
6RawData
7========
8
9one may interactively play with the two most important classes *RawData* and
10*SlowData* from module pyfact...
11
12*datafilepath* and *calibfilepath* should be adjusted in case you are not working
13on the ISDC cluster.
14
15explore class RawData::
16
17 from pyfact import RawData
18 # or from pyfact import *
19
20 datafilepath = '/fact/raw/2012/03/04/20120304_018.fits.gz'
21 calibfilepath = '/fact/raw/2012/03/04/20120304_012.drs.fits.gz'
22 run = RawData(datafilepath, calibfilepath)
23 event = run.next()
24
25 type(event)
26
27 print 70*'*'
28 for key in event:
29 print 'key :', key
30 print 'value:', event[key]
31 print 70*'*'
32
33for historical reasons, an event contains *data* and *acal_data*, where *acal_data*
34would be the DRS amplitude calibrated data, and *data* would be uncalibrated.
35But since the calibration was moved into a C++ class, for better performance,
36these keys now contains the same data.
37
38loop over RawData::
39
40 from pyfact import RawData
41 # or from pyfact import *
42
43 datafilepath = '/fact/raw/2012/03/04/20120304_018.fits.gz'
44 calibfilepath = '/fact/raw/2012/03/04/20120304_012.drs.fits.gz'
45 run = RawData(datafilepath, calibfilepath)
46
47 for event in run:
48 print 'event_id:', event['event_id']
49 # the data can be found in event['data']
50
51SlowData
52=========
53
54have a look at some SlowData::
55
56 :~$ cd /fact/aux/2012/05/30
57 :/fact/aux/2012/05/30$ python
58
59 from pyfact import SlowData
60 file = SlowData('20120530.FTM_CONTROL_TRIGGER_RATES.fits')
61 file.show()
62
63
64have a look at the *columns*, that are available::
65
66 'columns': {'BoardRate': (40L, 4L, 'E', 'Hz'),
67 'ElapsedTime': (1L, 4L, 'E', 'sec'),
68 'FTMtimeStamp': (1L, 8L, 'K', 'us'),
69 'OnTime': (1L, 4L, 'E', 'sec'),
70 'OnTimeCounter': (1L, 8L, 'K', 'us'),
71 'PatchRate': (160L, 4L, 'E', 'Hz'),
72 'QoS': (1L, 4L, 'J', ''),
73 'Time': (1L, 8L, 'D', 'MJD'),
74 'TriggerCounter': (1L, 4L, 'J', 'int'),
75 'TriggerRate': (1L, 4L, 'E', 'Hz')},
76
77choose the *columns* you would like to retrieve from the file::
78
79 file.register('TriggerRate')
80 file.register('Time')
81 # or in case you are unsure
82 file.register('all')
83
84check, what happened. file has got some new members::
85
86 file.show()
87
88but they are all zero. Now one should call *next()* in order to get
89the file contents row by row::
90
91 file.next()
92 file.show()
93
94or loop over the file::
95
96 for row in file:
97 print row.Time, row. OnTime
98
99*row* is just be a copy of *file*, but inside the for loop, I think it is
100convenient to access members of *row* rather than members of *file*.
101I is just easier to understand for the reader, I think.
102
103
104Stack some slow Data into numpy arrays
105======================================
106
107This example shows how to stack some columns from slow data into numpy arrays.
108One first open the file as usual, and *registers* the columns one is interested in.
109Then one informs the SlowData object about ones intention to have the data stacked.
110Now the SlowData object will create 2D-arrays, which contain the registered data
111while one loops over the slowdata file.
112
113The looping is important!
114
115Have a look::
116
117 #!/usr/bin/python -tti
118
119 import time
120 import numpy as np
121 from pyfact import SlowData
122
123 f = SlowData('20120601.FTM_STATIC_DATA.fits')
124 #
125 # do here --> f.show() if you are not sure about the column names
126 #
127 f.register('PatchThresh')
128 f.register('Time')
129 f.stack()
130
131 # now loop over the file and let SlowData do the magic
132 for row in f:
133 pass
134
135 # the stacked data can be found in the dict SlowData.stacked_cols
136 # put the Time array into a variable and convert into seconds since 01.01.1970
137 t = f.stacked_cols['Time'] * 24. * 3600
138
139 # put the thresholds into another var, for convenience
140 dtr = f.stacked_cols['PatchThresh']
141
142 # give the user some diagnostic info
143 print 'start time:', time.asctime(time.gmtime(t[0]))
144 print 'stop time:', time.asctime(time.gmtime(t[-1]))
145
146 print 'mean/median threshold:', dtr.mean(), np.median(dtr)
147 print 'min/max threshold:', dtr.min(), dtr.max()
148 print 'std deviation:', dtr.std()
149
150
151
152
153calling a system command
154========================
155Using the os module any command executable on the command line can be called within a script. This is in particular true for your own python scripts::
156
157 import os
158 os.system('echo long listing of dir; pwd; ls -l')
159
160or suppose you created a script my_script.py::
161
162 from os import system
163 system('python my_scrip.py')
164
165
166
Note: See TracBrowser for help on using the repository browser.