15from sysvar
import Reweighter
20 """Test case of SysVar module"""
25 cosTheta_min = [-0.7, -0.5, 0, 0.2, 0.5, 0.8, 0.9]
26 cosTheta_max = [-0.5, 0, 0.2, 0.5, 0.8, 0.9, 0.95]
27 cosTheta_var = [list(c) for c
in zip(cosTheta_min, cosTheta_max)]
28 p_min = [0.5, 1, 2, 3, 4, 5]
29 p_max = [1, 2, 3, 4, 5, 6]
30 charge_var = [[-2, 0], [0, 2]]
31 p_var = [list(p)
for p
in zip(p_min, p_max)]
32 eff_binning = np.array([list(c)
for c
in itertools.product(p_var, cosTheta_var, charge_var)]).reshape(-1, 6)
33 p_min = [0.5, 1, 2, 3, 4]
34 p_max = [1, 2, 3, 4, 5]
35 p_var = [list(p)
for p
in zip(p_min, p_max)]
36 fake_binning = np.array([list(c)
for c
in itertools.product(p_var, cosTheta_var, charge_var)]).reshape(-1, 6)
38 eff_df = pd.DataFrame(eff_binning, columns=[
'p_min',
'p_max',
'cosTheta_min',
'cosTheta_max',
'charge_min',
'charge_max'])
39 eff_df[
'data_MC_ratio'] = np.random.normal(1, 0.1, size=len(eff_df))
40 eff_df[
'data_MC_uncertainty_stat_dn'] = np.random.uniform(0.001, 0.01, size=len(eff_df))
41 eff_df[
'data_MC_uncertainty_stat_up'] = np.random.uniform(0.001, 0.01, size=len(eff_df))
42 eff_df[
'data_MC_uncertainty_sys_up'] = np.random.uniform(0.001, 0.01, size=len(eff_df))
43 eff_df[
'data_MC_uncertainty_sys_dn'] = np.random.uniform(0.001, 0.01, size=len(eff_df))
44 eff_df[
'variable'] =
'electronID'
45 eff_df[
'threshold'] = 0.8
47 fake_df = pd.DataFrame(fake_binning, columns=[
'p_min',
'p_max',
'cosTheta_min',
'cosTheta_max',
'charge_min',
'charge_max'])
48 fake_df[
'data_MC_ratio'] = np.random.normal(2, 0.2, size=len(fake_df))
49 fake_df[
'data_MC_uncertainty_stat_dn'] = np.random.uniform(0.01, 0.02, size=len(fake_df))
50 fake_df[
'data_MC_uncertainty_stat_up'] = np.random.uniform(0.01, 0.02, size=len(fake_df))
51 fake_df[
'data_MC_uncertainty_sys_up'] = np.random.uniform(0.01, 0.02, size=len(fake_df))
52 fake_df[
'data_MC_uncertainty_sys_dn'] = np.random.uniform(0.01, 0.02, size=len(fake_df))
53 fake_df[
'variable'] =
'electronID'
54 fake_df[
'threshold'] = 0.8
59 'cosTheta': np.array([0.25, 0.55, 0.85, 0.925]),
60 'p': np.array([1.5, 2.5, 3.5, 4.5]),
61 'charge': np.array([-1, 1, 1, 1]),
62 'electronID': np.array([0.85, 0.9, 0.95, 0.975]),
63 'PDG': np.array([11, -11, -11, -11]),
64 'mcPDG': np.array([11, 211, -11, 211]),
65 'B0_dec_mode': [0, 1, 2, 3],
66 'B0_PDG': [511, -511, 511, -511],
67 'B0_sigProb': [0.02, 0.03, 0.02, 0.9]
74 fei_dict = {
'dec_mode': [
'All',
'Rest',
'mode0',
'mode2'],
75 'cal': [0.8, 0.85, 0.75, 0.82],
76 'cal_stat_error': [0.1, 0.02, 0.03, 0.05],
77 'cal_sys_error': [0.1, 0.02, 0.03, 0.05],
78 'sig_prob_threshold': [0.01, 0.01, 0.01, 0.01],
79 'Btag': [
'B0',
'B0',
'B0',
'B0']
85 """Tests merging of PID tables"""
87 thresholds = {11: (
'electronID', 0.8)}
88 merged_table = reweighter.merge_pid_weight_tables(self.
pid_tables, thresholds)
89 self.assertEqual(len(merged_table), 154)
90 self.assertIn(
'PDG', merged_table.columns)
91 self.assertIn(
'mcPDG', merged_table.columns)
92 with self.assertRaises(ValueError):
93 wrong_thresholds = {11: (
'electronID', 0.9)}
94 reweighter.merge_pid_weight_tables(self.
pid_tables, wrong_thresholds)
97 """Tests binning of PID tables"""
99 binning = reweighter.get_binning(self.
pid_tables[(11, 11)])
101 self.assertEqual(len(binning), 3)
102 self.assertCountEqual(binning.keys(), [
'p',
'cosTheta',
'charge'])
103 self.assertEqual(len(binning[
'p']), 7)
104 self.assertEqual(len(binning[
'cosTheta']), 8)
105 self.assertEqual(len(binning[
'charge']), 3)
106 binning = reweighter.get_binning(self.
pid_tables[(11, 211)])
107 self.assertEqual(len(binning), 3)
108 self.assertEqual(len(binning[
'p']), 6)
111 """Tests adding PID particle"""
113 thresholds = {11: (
'electronID', 0.8)}
114 reweighter.add_pid_particle(
'', self.
pid_tables, thresholds)
115 particle = reweighter.get_particle(
'')
116 self.assertIsNotNone(particle)
117 self.assertEqual(len(particle.merged_table), 154)
118 self.assertCountEqual(particle.get_binning_variables(), [
'p',
'cosTheta',
'charge'])
120 with self.assertRaises(ValueError):
121 reweighter.add_pid_particle(
'', self.
pid_tables, thresholds)
124 """Tests add_fei_particle method"""
125 print(
'==================================================')
127 reweighter.add_fei_particle(
'', self.
fei_table, 0.01,
None)
128 particle = reweighter.get_particle(
'')
129 self.assertEqual(len(particle.merged_table), 4)
130 self.assertCountEqual(particle.get_binning_variables(), [
'dec_mode'])
132 with self.assertRaises(ValueError):
133 reweighter.add_fei_particle(
'', self.
fei_table, 0.001,
None)
136 """Tests reweighting of PID and FEI particles"""
138 reweighter =
Reweighter(n_variations=n_variations)
139 thresholds = {11: (
'electronID', 0.8)}
140 reweighter.add_pid_particle(
'', self.
pid_tables, thresholds)
141 reweighter.add_fei_particle(
'B0', self.
fei_table, 0.01,
None)
142 local_data = self.
user_data.copy(deep=
True)
143 reweighter.reweight(local_data)
144 self.assertEqual(len(local_data), 4)
145 self.assertIn(
'Weight', local_data.columns)
146 self.assertIn(
'B0_Weight', local_data.columns)
147 cols = [f
'Weight_{i}' for i
in range(0, n_variations)]
148 self.assertCountEqual(cols, [col
for col
in local_data.columns
if col.startswith(
'Weight_')])
149 self.assertTrue((local_data.query(
'abs(mcPDG) == 11')[[
'Weight']+cols] < 1.5).all().all())
150 self.assertTrue((local_data.query(
'abs(mcPDG) != 11')[[
'Weight']+cols] > 1.5).all().all())
151 print(local_data.query(
'abs(mcPDG) != 11')[[
'Weight']+cols].
std(axis=1))
152 self.assertTrue((local_data.query(
'abs(mcPDG) == 11')[[
'Weight']+cols].
std(axis=1) < 0.015).all())
153 self.assertTrue((local_data.query(
'abs(mcPDG) != 11')[[
'Weight']+cols].
std(axis=1) > 0.015).all())
155 cols = [f
'B0_Weight_{i}' for i
in range(0, n_variations)]
156 self.assertCountEqual(cols, [col
for col
in local_data.columns
if col.startswith(
'B0_Weight_')])
157 print(local_data[[
'B0_Weight']+cols])
158 self.assertFalse((local_data[[
'B0_Weight']+cols].isna()).any().any())
159 self.assertTrue((local_data[[
'B0_Weight']+cols] > 0).all().all())
162if __name__ ==
'__main__':
def test_add_pid_particle(self)
def __init__(self, *args, **kwargs)
fei_table
FEI tables for testing.
pid_tables
PID tables for testing.
def test_add_fei_particle(self)
user_data
User data for testing.
def test_merge_tables(self)