forked from zfit/zfit-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
B2KstLL.py
354 lines (275 loc) · 13.3 KB
/
B2KstLL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# @file B2KstLL.py
# @author Albert Puig ([email protected])
# @date 11.04.2019
# =============================================================================
"""B -> K*ll angular distribution in zfit."""
from math import pi
from typing import Type
import tensorflow as tf
import numpy as np
import pandas as pd
import zfit
ztf = zfit.ztf
ztyping = zfit.util.ztyping
ztypes = zfit.settings.ztypes
# The PDFs
class P4pPDF(zfit.pdf.ZPDF):
"""P4prime observable from Bd -> Kst ll (l=e,mu).
Angular distribution obtained from a fold tecnhique,
i.e. the valid of the angles is given for
- phi: [0, pi]
- theta_K: [0, pi]
- theta_l: [0, pi/2]
The function is normalized over a finite range and therefore a PDF.
Args:
FL (`zfit.Parameter`): Fraction of longitudinal polarisation of the Kst
AT2 (`zfit.Parameter`): Transverse asymmetry
P4p (`zfit.Parameter`): Defined as S4/sqrt(FL(1-FL))
obs (`zfit.Space`):
name (str):
dtype (tf.DType):
"""
_PARAMS = ['FL', 'AT2', 'P4p']
_N_OBS = 3
def _unnormalized_pdf(self, x):
FL = self.parameters['FL']
AT2 = self.parameters['AT2']
P4p = self.parameters['P4p']
costheta_k, costheta_l, phi = ztf.unstack_x(x)
sintheta_k = tf.sqrt(1.0 - costheta_k * costheta_k)
sintheta_l = tf.sqrt(1.0 - costheta_l * costheta_l)
sintheta_2k = (1.0 - costheta_k * costheta_k)
sintheta_2l = (1.0 - costheta_l * costheta_l)
sin2theta_k = (2.0 * sintheta_k * costheta_k)
cos2theta_l = (2.0 * costheta_l * costheta_l - 1.0)
pdf = (3.0 / 4.0) * (1.0 - FL) * sintheta_2k + \
FL * costheta_k * costheta_k + \
(1.0 / 4.0) * (1.0 - FL) * sintheta_2k * cos2theta_l + \
-1.0 * FL * costheta_k * costheta_k * cos2theta_l + \
(1.0 / 2.0) * (1.0 - FL) * AT2 * sintheta_2k * sintheta_2l * tf.cos(2.0 * phi) + \
tf.sqrt(FL * (1 - FL)) * P4p * sin2theta_k * sin2theta_l * tf.cos(phi)
return pdf
class P5pPDF(zfit.pdf.ZPDF):
"""P5prime observable from Bd -> Kst ll (l=e,mu).
Angular distribution obtained from a fold tecnhique,
i.e. the valid of the angles is given for
- phi: [0, pi]
- theta_K: [0, pi]
- theta_l: [0, pi/2]
The function is normalized over a finite range and therefore a PDF.
Args:
FL (`zfit.Parameter`): Fraction of longitudinal polarisation of the Kst
AT2 (`zfit.Parameter`): Transverse asymmetry
P5p (`zfit.Parameter`): Defined as S5/sqrt(FL(1-FL))
obs (`zfit.Space`):
name (str):
dtype (tf.DType):
"""
_PARAMS = ['FL', 'AT2', 'P5p']
_N_OBS = 3
def _unnormalized_pdf(self, x):
FL = self.parameters['FL']
AT2 = self.parameters['AT2']
P5p = self.parameters['P5p']
costheta_k, costheta_l, phi = ztf.unstack_x(x)
sintheta_k = tf.sqrt(1.0 - costheta_k * costheta_k)
sintheta_l = tf.sqrt(1.0 - costheta_l * costheta_l)
sintheta_2k = (1.0 - costheta_k * costheta_k)
sintheta_2l = (1.0 - costheta_l * costheta_l)
sin2theta_k = (2.0 * sintheta_k * costheta_k)
cos2theta_l = (2.0 * costheta_l * costheta_l - 1.0)
pdf = (3.0 / 4.0) * (1.0 - FL) * sintheta_2k + \
FL * costheta_k * costheta_k + \
(1.0 / 4.0) * (1.0 - FL) * sintheta_2k * cos2theta_l + \
-1.0 * FL * costheta_k * costheta_k * cos2theta_l + \
(1.0 / 2.0) * (1.0 - FL) * AT2 * sintheta_2k * sintheta_2l * tf.cos(2.0 * phi) + \
tf.sqrt(FL * (1 - FL)) * P5p * sin2theta_k * sintheta_l * tf.cos(phi)
return pdf
class P6pPDF(zfit.pdf.ZPDF):
"""P6prime observable from Bd -> Kst ll (l=e,mu).
Angular distribution obtained from a fold tecnhique,
i.e. the valid of the angles is given for
- phi: [-pi/2, pi/2]
- theta_K: [0, pi]
- theta_l: [0, pi/2]
The function is normalized over a finite range and therefore a PDF.
Args:
FL (`zfit.Parameter`): Fraction of longitudinal polarisation of the Kst
AT2 (`zfit.Parameter`): Transverse asymmetry
P6p (`zfit.Parameter`): Defined as S5/sqrt(FL(1-FL))
obs (`zfit.Space`):
name (str):
dtype (tf.DType):
"""
_PARAMS = ['FL', 'AT2', 'P6p']
_N_OBS = 3
def _unnormalized_pdf(self, x):
FL = self.parameters['FL']
AT2 = self.parameters['AT2']
P6p = self.parameters['P6p']
costheta_k, costheta_l, phi = ztf.unstack_x(x)
sintheta_k = tf.sqrt(1.0 - costheta_k * costheta_k)
sintheta_l = tf.sqrt(1.0 - costheta_l * costheta_l)
sintheta_2k = (1.0 - costheta_k * costheta_k)
sintheta_2l = (1.0 - costheta_l * costheta_l)
sin2theta_k = (2.0 * sintheta_k * costheta_k)
cos2theta_l = (2.0 * costheta_l * costheta_l - 1.0)
pdf = (3.0 / 4.0) * (1.0 - FL) * sintheta_2k + \
FL * costheta_k * costheta_k + \
(1.0 / 4.0) * (1.0 - FL) * sintheta_2k * cos2theta_l + \
-1.0 * FL * costheta_k * costheta_k * cos2theta_l + \
(1.0 / 2.0) * (1.0 - FL) * AT2 * sintheta_2k * sintheta_2l * tf.cos(2.0 * phi) + \
tf.sqrt(FL * (1 - FL)) * P6p * sin2theta_k * sintheta_l * tf.sin(phi)
return pdf
class P8pPDF(zfit.pdf.ZPDF):
"""P8prime observable from Bd -> Kst ll (l=e,mu).
Angular distribution obtained from a fold tecnhique,
i.e. the valid of the angles is given for
- phi: [-pi/2, pi/2]
- theta_K: [0, pi]
- theta_l: [0, pi/2]
The function is normalized over a finite range and therefore a PDF.
Args:
FL (`zfit.Parameter`): Fraction of longitudinal polarisation of the Kst
AT2 (`zfit.Parameter`): Transverse asymmetry
P8p (`zfit.Parameter`): Defined as S5/sqrt(FL(1-FL))
obs (`zfit.Space`):
name (str):
dtype (tf.DType):
"""
_PARAMS = ['FL', 'AT2', 'P8p']
_N_OBS = 3
def _unnormalized_pdf(self, x):
FL = self.parameters['FL']
AT2 = self.parameters['AT2']
P8p = self.parameters['P8p']
costheta_k, costheta_l, phi = ztf.unstack_x(x)
sintheta_k = tf.sqrt(1.0 - costheta_k * costheta_k)
sintheta_l = tf.sqrt(1.0 - costheta_l * costheta_l)
sintheta_2k = (1.0 - costheta_k * costheta_k)
sintheta_2l = (1.0 - costheta_l * costheta_l)
sin2theta_k = (2.0 * sintheta_k * costheta_k)
cos2theta_l = (2.0 * costheta_l * costheta_l - 1.0)
pdf = (3.0 / 4.0) * (1.0 - FL) * sintheta_2k + \
FL * costheta_k * costheta_k + \
(1.0 / 4.0) * (1.0 - FL) * sintheta_2k * cos2theta_l + \
-1.0 * FL * costheta_k * costheta_k * cos2theta_l + \
(1.0 / 2.0) * (1.0 - FL) * AT2 * sintheta_2k * sintheta_2l * tf.cos(2.0 * phi) + \
tf.sqrt(FL * (1 - FL)) * P8p * sin2theta_k * sin2theta_l * tf.sin(phi)
return pdf
# Folding data
def fold_P4p(data, costheta_k, costheta_l, phi):
theta_l = np.acos(data[costheta_l])
data[f'{costheta_k}_P4p'] = data[costheta_k]
data[f'{phi}_P4p'] = np.where(data[phi] < 0,
-data[phi],
data[phi])
data[f'{phi}_P4p'] = np.where(theta_l > 0.5*pi,
pi - data[f'{phi}_P4p'],
data[f'{phi}_P4p'])
data[f'{costheta_l}_P4p'] = np.where(theta_l > 0.5*pi,
np.cos(pi - theta_l),
data[costheta_l])
return zfit.data.Data.from_pandas(data[f'{costheta_l}_P4p',
f'{costheta_k}_P4p',
f'{phi}_P4p'].copy()
.rename(index=str,
columns={f'{costheta_l}_P4p': costheta_l,
f'{costheta_k}_P4p': costheta_k,
f'{phi}_P4p': phi}))
def fold_P5p(data, costheta_k, costheta_l, phi):
theta_l = np.acos(data[costheta_l])
data[f'{costheta_k}_P5p'] = data[costheta_k]
data[f'{phi}_P5p'] = np.where(data[f'{phi}_P5p'] < 0,
-data[f'{phi}_P5p'],
data[f'{phi}_P5p'])
data[f'{costheta_l}_P5p'] = np.where(theta_l > 0.5*pi,
np.cos(pi - theta_l),
data[costheta_l])
return zfit.data.Data.from_pandas(data[f'{costheta_l}_P5p',
f'{costheta_k}_P5p',
f'{phi}_P5p'].copy()
.rename(index=str,
columns={f'{costheta_l}_P5p': costheta_l,
f'{costheta_k}_P5p': costheta_k,
f'{phi}_P5p': phi}))
def fold_P6p(data, costheta_k, costheta_l, phi):
theta_l = np.acos(data[costheta_l])
data[f'{costheta_k}_P6p'] = data[costheta_k]
data[f'{phi}_P6p'] = np.where(data[phi] > 0.5*pi,
pi - data[phi],
data[phi])
data[f'{phi}_P6p'] = np.where(data[f'{phi}_P6p'] < - 0.5*pi,
- pi - data[f'{phi}_P6p'],
data[f'{phi}_P6p'])
data[f'{costheta_l}_P6p'] = np.where(theta_l > 0.5*pi,
np.cos(pi - theta_l),
data[costheta_l])
return zfit.data.Data.from_pandas(data[f'{costheta_l}_P6p',
f'{costheta_k}_P6p',
f'{phi}_P6p'].copy()
.rename(index=str,
columns={f'{costheta_l}_P6p': costheta_l,
f'{costheta_k}_P6p': costheta_k,
f'{phi}_P6p': phi}))
def fold_P8p(data, costheta_k, costheta_l, phi):
theta_k = np.acos(data[costheta_k])
theta_l = np.acos(data[costheta_l])
data[f'{costheta_k}_P8p'] = np.where(theta_l > 0.5*pi,
np.cos(pi - theta_k),
data[costheta_k])
data[f'{phi}_P8p'] = np.where(data[phi] > 0.5*pi,
pi - data[phi],
data[phi])
data[f'{phi}_P8p'] = np.where(data[f'{phi}_P8p'] < - 0.5*pi,
- pi - data[f'{phi}_P8p'],
data[f'{phi}_P8p'])
data[f'{costheta_l}_P8p'] = np.where(theta_l > 0.5*pi,
np.cos(pi - theta_l),
data[costheta_l])
return zfit.data.Data.from_pandas(data[f'{costheta_l}_P8p',
f'{costheta_k}_P8p',
f'{phi}_P8p'].copy()
.rename(index=str,
columns={f'{costheta_l}_P8p': costheta_l,
f'{costheta_k}_P8p': costheta_k,
f'{phi}_P8p': phi}))
# A bit of handling
class B2Kstll:
FOLDS = {'P4p': (P4pPDF, ['FL', 'AT2', 'P4p'], fold_P4p),
'P5p': (P5pPDF, ['FL', 'AT2', 'P5p'], fold_P5p),
'P6p': (P6pPDF, ['FL', 'AT2', 'P6p'], fold_P6p),
'P8p': (P8pPDF, ['FL', 'AT2', 'P8p'], fold_P8p)}
def __init__(self, costheta_l, costheta_k, phi):
self._obs_names = {'costheta_l': costheta_l.obs,
'costheta_k': costheta_k.obs,
'phi': phi.obs}
self.obs = costheta_l * costheta_k * phi
self.params = {}
def get_folded_pdf(self, name):
pdf_class, param_names, _ = self.FOLDS[name]
def get_params(param_list):
out = {}
for param in param_list:
if param not in self.params:
config = [0.8, 0, 1] if param == 'FL' else [0.0, -1, 1]
self.params.update({param: zfit.Parameter(param, *config)})
out[param] = self.params[param]
return out
# Make sure params exist
params = get_params(param_names)
pdf = pdf_class(obs=self.obs, **params)
return pdf
def fold_dataset(self, name, dataset):
*_, data_transform = self.FOLDS[name]
return data_transform(dataset, self.obs.obs)
if __name__ == "__main__":
costheta_l = zfit.Space("costhetal", limits=(-1.0, 1.0))
costheta_k = zfit.Space("costhetaK", limits=(-1.0, 1.0))
phi = zfit.Space("phi", limits=(-pi, pi))
decay = B2Kstll(costheta_l, costheta_k, phi)
pdf = decay.get_folded_pdf('P5p')
# EOFs