Project

General

Profile

Python Example (Test Page) » History » Version 5

Thomas Bretz, 2018-09-14 15:53

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