Belle II Software  release-06-02-00
test_variables_utils.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import unittest
13 from variables.utils import create_aliases_for_selected
14 from variables import variables as vm
15 
16 
17 class TestVariableUtilities(unittest.TestCase):
18  """Test case for the variables.utils functions"""
19 
20 
21  _list_of_variables = ['M', 'p']
22 
23  def _expand_expected(self, expected):
24  """Expand the list of expected aliases by appending _{name} for each variable in the _list_of_variables"""
25  result = []
26  for p in expected:
27  if p:
28  result += [f"{p}_{e}" for e in self._list_of_variables_list_of_variables]
29  else:
30  result += self._list_of_variables_list_of_variables
31  return result
32 
33  def assertAliases(self, decaystring, expected, **argk):
34  """Make sure the aliases created for given decaystring are as expected
35  Also, pass any additional keyword arguments to the create_aliases_for_selected function
36  """
37  expected = self._expand_expected_expand_expected(expected)
38  actual = create_aliases_for_selected(self._list_of_variables_list_of_variables, decaystring, **argk)
39  self.assertEqual(expected, actual, f"decaystring: {decaystring}, arguments: {argk}")
40 
41 
44 
46  """Make sure we get an error if the decaystring is not valid"""
47  with self.assertRaises(ValueError):
48  create_aliases_for_selected(["p"], "eeh <- ok")
49 
51  """Make sure we get an error if o particle is selected"""
52  with self.assertRaises(ValueError):
53  create_aliases_for_selected(["p"], "B0 -> pi0")
54 
56  """Make sure we get an error if the number of supplied prefixes doesn't
57  match the number of selected particles"""
58  with self.assertRaises(ValueError):
59  create_aliases_for_selected(["p"], "^B0 -> ^pi0", prefix="one")
60  with self.assertRaises(ValueError):
61  create_aliases_for_selected(["p"], "B0 -> ^pi0", prefix=["one", "two"])
62 
64  """Make sure we got an error if the supplied provided prefixes are not unique"""
65  with self.assertRaises(ValueError):
66  create_aliases_for_selected(["p"], "^B0 -> ^pi0", prefix=["mine", "mine"])
67 
69  """Check daughter can be selected for an specific named alias"""
70  self.assertAliasesassertAliases('B0 -> [^D0 -> pi+ K-] pi0', ['dzero'], prefix='dzero')
71 
73  """Check mother and daughter can be selected for an specific named alias"""
74  self.assertAliasesassertAliases('^B0 -> [^D0 -> pi+ ^K-] pi0', ['MyB', 'MyD', 'MyK'], prefix=['MyB', 'MyD', 'MyK'])
75 
77  """Check daughter can be selected w/o an specific named alias"""
78  self.assertAliasesassertAliases('B0 -> [^D0 -> ^pi+ ^K-] pi0', ['d0', 'd0_d0', 'd0_d1'], use_names=False)
79 
81  """Check daughter can be selected w/o an specific named alias"""
82  self.assertAliasesassertAliases('^B0 -> pi0 ^pi0', ['', 'd1'], use_names=False)
83  # also make sure we ignore ``use_relative_indices``
84  self.assertAliasesassertAliases('^B0 -> pi0 ^pi0', ['', 'd1'], use_names=False, use_relative_indices=True)
85 
87  """Check granddaughter can be selected for an automatic name alias"""
88  self.assertAliasesassertAliases('B0 -> [D0 -> ^pi+ K-] pi0', ['D0_pi'])
89 
91  """Check multiple granddaughters can be selected for automatic name aliases"""
92  self.assertAliasesassertAliases(
93  'B0 -> [D0 -> ^pi+ ^pi- ^pi0] ^pi0',
94  [
95  'D0_pi_0',
96  'D0_pi_1',
97  'D0_pi0',
98  'pi0',
99  ])
100 
101  def test_autoindex(self):
102  """ check decay-string-of-doom with automatic names """
103  self.assertAliasesassertAliases(
104  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
105  [
106  "",
107  "D0_0_pi_0",
108  "D0_0_pi_1",
109  "D_pi",
110  "D0_2_pi_0",
111  "D0_2_pi_1",
112  "K_pi_0",
113  "K_pi_1",
114  "pi_4",
115  "pi_5",
116  ])
117 
119  """ check decay-string-of-doom with automatic names and relative indexing"""
120  self.assertAliasesassertAliases(
121  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
122  [
123  "",
124  "D0_0_pi_0",
125  "D0_0_pi_1",
126  "D_pi",
127  "D0_1_pi_0",
128  "D0_1_pi_1",
129  "K_pi_0",
130  "K_pi_1",
131  "pi_0",
132  "pi_1",
133  ], use_relative_indices=True)
134 
136  """ check decay-string-of-doom with automatic names and forced indices"""
137  self.assertAliasesassertAliases(
138  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
139  [
140  "",
141  "D0_0_pi_0",
142  "D0_0_pi_1",
143  "D_1_pi_0",
144  "D0_2_pi_0",
145  "D0_2_pi_1",
146  "K_3_pi_0",
147  "K_3_pi_1",
148  "pi_4",
149  "pi_5",
150  ], always_include_indices=True)
151 
153  """ check decay-string-of-doom with automatic names, relative indexing and forced indices"""
154  self.assertAliasesassertAliases(
155  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
156  [
157  "",
158  "D0_0_pi_0",
159  "D0_0_pi_1",
160  "D_0_pi_0",
161  "D0_1_pi_0",
162  "D0_1_pi_1",
163  "K_0_pi_0",
164  "K_0_pi_1",
165  "pi_0",
166  "pi_1",
167  ], use_relative_indices=True, always_include_indices=True)
168 
169  def test_indexed(self):
170  """ check decay-string-of-doom w/o automatic names """
171  self.assertAliasesassertAliases(
172  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
173  [
174  "",
175  "d0_d0",
176  "d0_d1",
177  "d1_d0",
178  "d2_d0",
179  "d2_d1",
180  "d3_d0",
181  "d3_d1",
182  "d4",
183  "d5",
184  ], use_names=False)
185 
187  """ check decay-string-of-doom w/o automatic names and make sure relative indexing is **not** honored"""
188  self.assertAliasesassertAliases(
189  "^B0 -> [D0 -> ^pi+ ^pi-] [D+ -> ^pi+ pi0] [D0 -> ^pi+ ^pi-] [K- -> ^pi+ ^pi-] ^pi+ ^pi- pi0",
190  [
191  "",
192  "d0_d0",
193  "d0_d1",
194  "d1_d0",
195  "d2_d0",
196  "d2_d1",
197  "d3_d0",
198  "d3_d1",
199  "d4",
200  "d5",
201  ], use_names=False, use_relative_indices=True)
202 
203  def test_threedkp(self):
204  """ check if the indexing works with more than two ... """
205  self.assertAliasesassertAliases(
206  "B0 -> [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]]",
207  [
208  "D_0_K_pi",
209  "D_1_K_pi",
210  "D_2_K_pi",
211  ])
212 
213  def test_fourdkp(self):
214  """ check if the indexing works with more than two ... """
215  self.assertAliasesassertAliases(
216  "B0 -> [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [K+ -> ^pi+]]",
217  [
218  "D_0_K_pi",
219  "D_1_K_pi",
220  "D_2_K_pi",
221  "D_3_K_pi",
222  ])
223  self.assertAliasesassertAliases(
224  "B0 -> [D+ -> [K+ -> pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [^K+ -> pi+]] [D+ -> [K+ -> ^pi+]]",
225  [
226  "D_1_K_pi",
227  "D_2_K",
228  "D_3_K_pi",
229  ])
230  self.assertAliasesassertAliases(
231  "B0 -> [D+ -> [K+ -> pi+]] [D+ -> [K+ -> ^pi+]] [D+ -> [^K+ -> pi+]] [D+ -> [K+ -> ^pi+]]",
232  [
233  "D_0_K_pi",
234  "D_1_K",
235  "D_2_K_pi",
236  ], use_relative_indices=True)
237 
239  """Test many many children"""
240  self.assertAliasesassertAliases(
241  "B0 -> e+:0 ^e+:1 ^e+:2 e+:3 ^e+:4 e+:5 ^e+:6 mu+:7 ^mu+:8 mu+:9 ^mu+:10 ^mu+:11 mu+:12",
242  [
243  "e_1",
244  "e_2",
245  "e_4",
246  "e_6",
247  "mu_8",
248  "mu_10",
249  "mu_11",
250  ])
251 
253  """Test many many children with relative indices"""
254  self.assertAliasesassertAliases(
255  "B0 -> e+:0 ^e+:1 ^e+:2 e+:3 ^e+:4 e+:5 ^e+:6 mu+:7 ^mu+:8 mu+:9 ^mu+:10 ^mu+:11 mu+:12",
256  [
257  "e_0",
258  "e_1",
259  "e_2",
260  "e_3",
261  "mu_0",
262  "mu_1",
263  "mu_2",
264  ], use_relative_indices=True)
265 
267  """Test many many children without names"""
268  for use_relative_indices in True, False:
269  for always_include_indices in True, False:
270  self.assertAliasesassertAliases(
271  "B0 -> e+:0 ^e+:1 ^e+:2 e+:3 ^e+:4 e+:5 ^e+:6 mu+:7 ^mu+:8 mu+:9 ^mu+:10 ^mu+:11 mu+:12",
272  [
273  "d1",
274  "d2",
275  "d4",
276  "d6",
277  "d8",
278  "d10",
279  "d11",
280  ], use_names=False,
281  use_relative_indices=use_relative_indices,
282  always_include_indices=always_include_indices,
283  )
284 
285  def test_inclusive(self):
286  """Select a decay with the inclusive particle marker"""
287  self.assertAliasesassertAliases("B0 -> ^Xsd e+:loose e-:loose", ["Xsd"])
288 
289  def test_zfinal(self):
290  """Print all aliases as a final check"""
291  vm.printAliases()
292 
293 
294 if __name__ == '__main__':
295  unittest.main()
def test_wrong_decaystring(self)
to add to the tests here, please add a test_something_something for you favourite complicated decay s...
list _list_of_variables
list of variables to test
def assertAliases(self, decaystring, expected, **argk)