Project

General

Profile

Python Example (Test Page) » History » Version 6

Thomas Bretz, 2018-09-16 13:05

1 3 Thomas Bretz
{{>toc}}
2 6 Thomas Bretz
3 1 Thomas Bretz
Here is an example how to do a simple database based analysis in Python. This examples illustrates how to fill theta-square plots and are the Python counterparts to the example given on the main page. 
4 1 Thomas Bretz
5 1 Thomas Bretz
The following Python code was kindly provided by Julian Kemp MSc.
6 1 Thomas Bretz
7 1 Thomas Bretz
# Query 
8 1 Thomas Bretz
9 1 Thomas Bretz
Both examples on this page read the following query from an ascii file `query.txt`:
10 1 Thomas Bretz
11 2 Thomas Bretz
~~~ sql
12 1 Thomas Bretz
SELECT
13 1 Thomas Bretz
   Images.*,
14 1 Thomas Bretz
   Position.X,
15 1 Thomas Bretz
   Position.Y
16 1 Thomas Bretz
FROM
17 1 Thomas Bretz
   Images
18 1 Thomas Bretz
LEFT JOIN Position USING (FileId, EvtNumber)
19 1 Thomas Bretz
LEFT JOIN RunInfo  USING (FileId)
20 1 Thomas Bretz
WHERE
21 1 Thomas Bretz
   fSourceKey=5
22 1 Thomas Bretz
AND
23 1 Thomas Bretz
   fRunTypeKey=1
24 1 Thomas Bretz
AND
25 1 Thomas Bretz
   FileId BETWEEN 180100000 AND 180200000
26 1 Thomas Bretz
AND
27 1 Thomas Bretz
   fZenithDistanceMax<35
28 1 Thomas Bretz
AND
29 1 Thomas Bretz
   fR750Cor>0.9*fR750Ref
30 1 Thomas Bretz
AND
31 1 Thomas Bretz
   NumUsedPixels>5.5
32 1 Thomas Bretz
AND
33 1 Thomas Bretz
   NumIslands<3.5
34 1 Thomas Bretz
AND
35 1 Thomas Bretz
   Leakage1<0.1
36 1 Thomas Bretz
AND
37 1 Thomas Bretz
   Width*Length*PI() < LOG10(Size)*898-1535; 
38 2 Thomas Bretz
~~~
39 1 Thomas Bretz
40 1 Thomas Bretz
# One shot example
41 1 Thomas Bretz
42 1 Thomas Bretz
The first example runs the query and filling the on- and off-histograms in a single python program: 
43 1 Thomas Bretz
44 2 Thomas Bretz
~~~ python
45 1 Thomas Bretz
import numpy as np
46 1 Thomas Bretz
import matplotlib.pyplot as plt
47 1 Thomas Bretz
import mysql.connector
48 1 Thomas Bretz
49 1 Thomas Bretz
def FillHistogram(bins,n,value,weight=1):
50 1 Thomas Bretz
    idx = np.searchsorted(bins,value)
51 1 Thomas Bretz
    if not idx==0 and not idx>n.size:
52 1 Thomas Bretz
        n[idx-1]+=weight
53 1 Thomas Bretz
54 1 Thomas Bretz
mm2deg = 0.0117193246260285378
55 1 Thomas Bretz
56 1 Thomas Bretz
# Create bins for the histograms
57 1 Thomas Bretz
bins = np.linspace(0,1,56)
58 1 Thomas Bretz
non = np.zeros(55)
59 1 Thomas Bretz
noff = np.zeros(55)
60 1 Thomas Bretz
61 1 Thomas Bretz
# Read the MySQL query from a text file
62 1 Thomas Bretz
queryFile = open('query.txt')
63 1 Thomas Bretz
query = queryFile.read()
64 1 Thomas Bretz
queryFile.close()
65 1 Thomas Bretz
66 1 Thomas Bretz
# Open a connection to the MySQL database
67 1 Thomas Bretz
conn = mysql.connector.connect(user='fact',password='[...]',host='ihp-pc45.ethz.ch',database='factdata',port='3306')
68 1 Thomas Bretz
cursor = conn.cursor(dictionary=True)
69 1 Thomas Bretz
cursor.execute(query)
70 1 Thomas Bretz
71 1 Thomas Bretz
# Loop over all events received from the database
72 1 Thomas Bretz
for Event in cursor:
73 1 Thomas Bretz
    # Intialize all variables needed in the calculations
74 1 Thomas Bretz
    Length = Event['Length']
75 1 Thomas Bretz
    Width = Event['Width']
76 1 Thomas Bretz
    NumIslands = Event['NumIslands']
77 1 Thomas Bretz
    NumUsedPixels = Event['NumUsedPixels']
78 1 Thomas Bretz
    Leakage1 = Event['Leakage1']
79 1 Thomas Bretz
    Size = Event['Size']
80 1 Thomas Bretz
    X = Event['X']
81 1 Thomas Bretz
    Y = Event['Y']
82 1 Thomas Bretz
    CosDelta = Event['CosDelta']
83 1 Thomas Bretz
    SinDelta = Event['SinDelta']
84 1 Thomas Bretz
    M3Long = Event['M3Long']
85 1 Thomas Bretz
    SlopeLong = Event['SlopeLong']
86 1 Thomas Bretz
    MeanX = Event['MeanX']
87 1 Thomas Bretz
    MeanY = Event['MeanY']
88 1 Thomas Bretz
89 1 Thomas Bretz
    # First calculate all cuts to speedup the analysis
90 1 Thomas Bretz
    area =  np.pi*Width*Length
91 1 Thomas Bretz
92 1 Thomas Bretz
    # The abberation correction does increase also Width and Length by 1.02
93 1 Thomas Bretz
    cutq = (NumIslands<3.5 and NumUsedPixels>5.5 and Leakage1<0.1)
94 1 Thomas Bretz
    if not cutq:
95 1 Thomas Bretz
        continue
96 1 Thomas Bretz
97 1 Thomas Bretz
    cut0 = area<(np.log10(Size)*898-1535)
98 1 Thomas Bretz
    if not cut0:
99 1 Thomas Bretz
        continue
100 1 Thomas Bretz
101 1 Thomas Bretz
    # Loop over all wobble positions in the camera
102 1 Thomas Bretz
    for angle in range(0,360,60):
103 1 Thomas Bretz
        # ----------- Source dependent parameter calculation ----------
104 1 Thomas Bretz
        cr = np.cos(np.deg2rad(angle))
105 1 Thomas Bretz
        sr = np.sin(np.deg2rad(angle))
106 1 Thomas Bretz
107 1 Thomas Bretz
        px = cr*X-sr*Y
108 1 Thomas Bretz
        py = cr*Y+sr*X
109 1 Thomas Bretz
110 1 Thomas Bretz
        dx = MeanX-px*1.02
111 1 Thomas Bretz
        dy = MeanY-py*1.02
112 1 Thomas Bretz
113 1 Thomas Bretz
        norm = np.sqrt(dx*dx+dy*dy)
114 1 Thomas Bretz
        dist = norm*mm2deg
115 1 Thomas Bretz
116 1 Thomas Bretz
        lx = np.min([np.max([(CosDelta*dy-SinDelta*dx)/norm,-1.]),1.])
117 1 Thomas Bretz
        ly = np.min([np.max([(CosDelta*dx+SinDelta*dy)/norm,-1.]),1.])
118 1 Thomas Bretz
119 1 Thomas Bretz
        alpha = np.arcsin(lx)
120 1 Thomas Bretz
        sgn = np.sign(ly)
121 1 Thomas Bretz
122 1 Thomas Bretz
        # ------------------------ Application ---------------------
123 1 Thomas Bretz
        m3l = M3Long*sgn*mm2deg
124 1 Thomas Bretz
        slope = SlopeLong*sgn/mm2deg
125 1 Thomas Bretz
126 1 Thomas Bretz
        # -------------------------- Analysis ----------------------
127 1 Thomas Bretz
        xi = 1.39252+0.154247*slope+1.67972*(1-1/(1+4.86232*Leakage1))
128 1 Thomas Bretz
129 1 Thomas Bretz
        sign1 = m3l+0.07
130 1 Thomas Bretz
        sign2 = (dist-0.5)*7.2-slope
131 1 Thomas Bretz
132 1 Thomas Bretz
        disp = (-xi if (sign1<0 or sign2<0) else xi)*(1-Width/Length)
133 1 Thomas Bretz
134 1 Thomas Bretz
        thetasq = disp*disp+dist*dist-2*disp*dist*np.sqrt(1-lx*lx)
135 1 Thomas Bretz
136 1 Thomas Bretz
        if angle==0:
137 1 Thomas Bretz
            FillHistogram(bins,non,thetasq)
138 1 Thomas Bretz
        else:
139 1 Thomas Bretz
            FillHistogram(bins,noff,thetasq,weight=1./5.)
140 1 Thomas Bretz
141 1 Thomas Bretz
conn.close()
142 1 Thomas Bretz
143 1 Thomas Bretz
plt.hist(bins[:-1],bins=bins,histtype='step',weights=non)
144 1 Thomas Bretz
plt.hist(bins[:-1],bins=bins,histtype='step',weights=noff)
145 1 Thomas Bretz
plt.show()
146 2 Thomas Bretz
~~~
147 1 Thomas Bretz
148 2 Thomas Bretz
# Two step example 
149 1 Thomas Bretz
150 1 Thomas Bretz
This example works similar to `rootifysql` and a ROOT macro. It first requests the data from a database an stores it into a file and then processed the file with a second program.
151 1 Thomas Bretz
152 2 Thomas Bretz
## Requesting and storing the data 
153 1 Thomas Bretz
154 2 Thomas Bretz
~~~ python
155 1 Thomas Bretz
import numpy as np
156 1 Thomas Bretz
import pickle
157 1 Thomas Bretz
import mysql.connector
158 1 Thomas Bretz
159 1 Thomas Bretz
# Read the MySQL query from a text file
160 1 Thomas Bretz
queryFile = open('query.txt')
161 1 Thomas Bretz
query = queryFile.read()
162 1 Thomas Bretz
queryFile.close()
163 1 Thomas Bretz
164 1 Thomas Bretz
# Open a connection to the MySQL database
165 1 Thomas Bretz
conn = mysql.connector.connect(user='fact',password='[...]',host='ihp-pc45.ethz.ch',database='factdata',port='3306')
166 1 Thomas Bretz
cursor = conn.cursor()
167 1 Thomas Bretz
cursor.execute(query)
168 1 Thomas Bretz
169 1 Thomas Bretz
with open('crab-data-only.p','wb') as outFile:
170 1 Thomas Bretz
    description = np.array(cursor.description).T[0]
171 1 Thomas Bretz
    pickle.dump(description,outFile,pickle.HIGHEST_PROTOCOL)
172 1 Thomas Bretz
    for Event in cursor:
173 1 Thomas Bretz
        pickle.dump(Event,outFile,pickle.HIGHEST_PROTOCOL)
174 1 Thomas Bretz
175 1 Thomas Bretz
conn.close()
176 4 Thomas Bretz
~~~
177 1 Thomas Bretz
178 4 Thomas Bretz
## Reading and processing the data
179 1 Thomas Bretz
180 5 Thomas Bretz
~~~ python
181 1 Thomas Bretz
import numpy as np
182 1 Thomas Bretz
import pickle
183 1 Thomas Bretz
import matplotlib.pyplot as plt
184 1 Thomas Bretz
185 1 Thomas Bretz
def FillHistogram(bins,n,value,weight=1):
186 1 Thomas Bretz
    idx = np.searchsorted(bins,value)
187 1 Thomas Bretz
    if not idx==0 and not idx>n.size:
188 1 Thomas Bretz
        n[idx-1]+=weight
189 1 Thomas Bretz
190 1 Thomas Bretz
inFile = open('crab-data-only.p','rb')
191 1 Thomas Bretz
description = pickle.load(inFile)
192 1 Thomas Bretz
193 1 Thomas Bretz
# Find indices corresponding to the different variables
194 1 Thomas Bretz
Length_idx = np.nonzero(description=='Length')[0][0]
195 1 Thomas Bretz
Width_idx = np.nonzero(description=='Width')[0][0]
196 1 Thomas Bretz
NumIslands_idx = np.nonzero(description=='NumIslands')[0][0]
197 1 Thomas Bretz
NumUsedPixels_idx = np.nonzero(description=='NumUsedPixels')[0][0]
198 1 Thomas Bretz
Leakage1_idx = np.nonzero(description=='Leakage1')[0][0]
199 1 Thomas Bretz
Size_idx = np.nonzero(description=='Size')[0][0]
200 1 Thomas Bretz
X_idx = np.nonzero(description=='X')[0][0]
201 1 Thomas Bretz
Y_idx = np.nonzero(description=='Y')[0][0]
202 1 Thomas Bretz
CosDelta_idx = np.nonzero(description=='CosDelta')[0][0]
203 1 Thomas Bretz
SinDelta_idx = np.nonzero(description=='SinDelta')[0][0]
204 1 Thomas Bretz
M3Long_idx = np.nonzero(description=='M3Long')[0][0]
205 1 Thomas Bretz
SlopeLong_idx = np.nonzero(description=='SlopeLong')[0][0]
206 1 Thomas Bretz
MeanX_idx = np.nonzero(description=='MeanX')[0][0]
207 1 Thomas Bretz
MeanY_idx = np.nonzero(description=='MeanY')[0][0]
208 1 Thomas Bretz
209 1 Thomas Bretz
mm2deg = 0.0117193246260285378
210 1 Thomas Bretz
211 1 Thomas Bretz
# Create bins for the histogram
212 1 Thomas Bretz
bins = np.linspace(0,1,56)
213 1 Thomas Bretz
non = np.zeros(55)
214 1 Thomas Bretz
noff = np.zeros(55)
215 1 Thomas Bretz
216 1 Thomas Bretz
while True:
217 1 Thomas Bretz
    try:
218 1 Thomas Bretz
        Event = pickle.load(inFile)
219 1 Thomas Bretz
    except:
220 1 Thomas Bretz
        break
221 1 Thomas Bretz
222 1 Thomas Bretz
    # Intialize all variables needed in the calculations
223 1 Thomas Bretz
    Length = Event[Length_idx]
224 1 Thomas Bretz
    Width = Event[Width_idx]
225 1 Thomas Bretz
    NumIslands = Event[NumIslands_idx]
226 1 Thomas Bretz
    NumUsedPixels = Event[NumUsedPixels_idx]
227 1 Thomas Bretz
    Leakage1 = Event[Leakage1_idx]
228 1 Thomas Bretz
    Size = Event[Size_idx]
229 1 Thomas Bretz
    X = Event[X_idx]
230 1 Thomas Bretz
    Y = Event[Y_idx]
231 1 Thomas Bretz
    CosDelta = Event[CosDelta_idx]
232 1 Thomas Bretz
    SinDelta = Event[SinDelta_idx]
233 1 Thomas Bretz
    M3Long = Event[M3Long_idx]
234 1 Thomas Bretz
    SlopeLong = Event[SlopeLong_idx]
235 1 Thomas Bretz
    MeanX = Event[MeanX_idx]
236 1 Thomas Bretz
    MeanY = Event[MeanY_idx]
237 1 Thomas Bretz
238 1 Thomas Bretz
    # First calculate all cuts to speedup the analysis
239 1 Thomas Bretz
    area =  np.pi*Width*Length
240 1 Thomas Bretz
241 1 Thomas Bretz
    # The abberation correction does increase also Width and Length by 1.02
242 1 Thomas Bretz
    cutq = (NumIslands<3.5 and NumUsedPixels>5.5 and Leakage1<0.1)
243 1 Thomas Bretz
    if not cutq:
244 1 Thomas Bretz
        continue
245 1 Thomas Bretz
246 1 Thomas Bretz
    cut0 = area<(np.log10(Size)*898-1535)
247 1 Thomas Bretz
    if not cut0:
248 1 Thomas Bretz
        continue
249 1 Thomas Bretz
250 1 Thomas Bretz
    #print(area,cutq,cut0)
251 1 Thomas Bretz
252 1 Thomas Bretz
    # Loop over all wobble positions in the camera
253 1 Thomas Bretz
    for angle in range(0,360,60):
254 1 Thomas Bretz
        # ----------- Source dependent parameter calculation ----------
255 1 Thomas Bretz
        cr = np.cos(np.deg2rad(angle))
256 1 Thomas Bretz
        sr = np.sin(np.deg2rad(angle))
257 1 Thomas Bretz
258 1 Thomas Bretz
        px = cr*X-sr*Y
259 1 Thomas Bretz
        py = cr*Y+sr*X
260 1 Thomas Bretz
261 1 Thomas Bretz
        dx = MeanX-px*1.02
262 1 Thomas Bretz
        dy = MeanY-py*1.02
263 1 Thomas Bretz
264 1 Thomas Bretz
        norm = np.sqrt(dx*dx+dy*dy)
265 1 Thomas Bretz
        dist = norm*mm2deg
266 1 Thomas Bretz
267 1 Thomas Bretz
        lx = np.min([np.max([(CosDelta*dy-SinDelta*dx)/norm,-1.]),1.])
268 1 Thomas Bretz
        ly = np.min([np.max([(CosDelta*dx+SinDelta*dy)/norm,-1.]),1.])
269 1 Thomas Bretz
270 1 Thomas Bretz
        alpha = np.arcsin(lx)
271 1 Thomas Bretz
        sgn = np.sign(ly)
272 1 Thomas Bretz
273 1 Thomas Bretz
        # ------------------------ Application ---------------------
274 1 Thomas Bretz
        m3l = M3Long*sgn*mm2deg
275 1 Thomas Bretz
        slope = SlopeLong*sgn/mm2deg
276 1 Thomas Bretz
277 1 Thomas Bretz
        # -------------------------- Analysis ----------------------
278 1 Thomas Bretz
        xi = 1.39252+0.154247*slope+1.67972*(1-1/(1+4.86232*Leakage1))
279 1 Thomas Bretz
280 1 Thomas Bretz
        sign1 = m3l+0.07
281 1 Thomas Bretz
        sign2 = (dist-0.5)*7.2-slope
282 1 Thomas Bretz
283 1 Thomas Bretz
        disp = (-xi if (sign1<0 or sign2<0) else xi)*(1-Width/Length)
284 1 Thomas Bretz
285 1 Thomas Bretz
        thetasq = disp*disp+dist*dist-2*disp*dist*np.sqrt(1-lx*lx)
286 1 Thomas Bretz
287 1 Thomas Bretz
        if angle==0:
288 1 Thomas Bretz
            FillHistogram(bins,non,thetasq)
289 1 Thomas Bretz
        else:
290 1 Thomas Bretz
            FillHistogram(bins,noff,thetasq,weight=1./5.)
291 1 Thomas Bretz
292 1 Thomas Bretz
plt.hist(bins[:-1],bins=bins,histtype='step',weights=non)
293 1 Thomas Bretz
plt.hist(bins[:-1],bins=bins,histtype='step',weights=noff)
294 1 Thomas Bretz
plt.show()
295 2 Thomas Bretz
~~~