Belle II Software  release-08-01-10
test_fei_config.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import tempfile
12 import os
13 import shutil
14 import atexit
15 import unittest
16 
17 import fei.config
18 
19 # @cond
20 
21 
22 class TestFeiConfiguration(unittest.TestCase):
23 
24  def test_Defaults(self):
25  config = fei.config.FeiConfiguration()
26  self.assertEqual(config.prefix, 'FEI_TEST')
27  self.assertEqual(config.cache, None)
28  self.assertEqual(config.monitor, True)
29  self.assertEqual(config.legacy, None)
30  self.assertEqual(config.externTeacher, 'basf2_mva_teacher')
31  self.assertEqual(config.training, False)
32 
33 
34 class TestMVAConfiguration(unittest.TestCase):
35 
36  def test_Defaults(self):
37  config = fei.config.MVAConfiguration()
38  self.assertEqual(config.method, 'FastBDT')
39  self.assertEqual(config.config, '--nTrees 400 --nCutLevels 10 --nLevels 3 --shrinkage 0.1 --randRatio 0.5')
40  # We want None, because [] can have nasty side effects when used as default parameter!
41  self.assertEqual(config.variables, None)
42  self.assertEqual(config.target, 'isSignal')
43  self.assertEqual(config.sPlotVariable, None)
44 
45 
46 class TestPreCutConfiguration(unittest.TestCase):
47 
48  def test_Defaults(self):
49  config = fei.config.PreCutConfiguration()
50  self.assertEqual(config.userCut, '')
51  self.assertEqual(config.vertexCut, -2)
52  self.assertEqual(config.bestCandidateVariable, None)
53  self.assertEqual(config.bestCandidateCut, 0)
54  self.assertEqual(config.bestCandidateMode, 'lowest')
55 
56 
57 class TestPostCutConfiguration(unittest.TestCase):
58 
59  def test_Defaults(self):
60  config = fei.config.PostCutConfiguration()
61  self.assertEqual(config.value, 0.0)
62  self.assertEqual(config.bestCandidateCut, 0)
63 
64 
65 class TestDecayChannel(unittest.TestCase):
66 
67  def test_Defaults(self):
68  channel = fei.config.DecayChannel(name='D0:23', label='D0 ==> K- pi+',
69  decayString='D0:23 -> K- pi+', daughters=['K-', 'pi+'],
70  mvaConfig=fei.config.MVAConfiguration(variables=['E']),
71  preCutConfig=fei.config.PreCutConfiguration(userCut='test'),
72  decayModeID=23)
73  self.assertEqual(channel.name, 'D0:23')
74  self.assertEqual(channel.label, 'D0 ==> K- pi+')
75  self.assertEqual(channel.decayString, 'D0:23 -> K- pi+')
76  self.assertEqual(channel.daughters, ['K-', 'pi+'])
77  self.assertEqual(channel.mvaConfig, fei.config.MVAConfiguration(variables=['E']))
78  self.assertEqual(channel.preCutConfig, fei.config.PreCutConfiguration(userCut='test'))
79  self.assertEqual(channel.decayModeID, 23)
80 
81 
82 class TestParticle(unittest.TestCase):
83 
84  def test_Defaults(self):
85  mvaConfig = fei.config.MVAConfiguration(variables=['E'])
86  preCutConfig = fei.config.PreCutConfiguration()
87  postCutConfig = fei.config.PostCutConfiguration()
88  particle = fei.config.Particle('D0', mvaConfig)
89 
90  self.assertEqual(particle.identifier, 'D0:generic')
91  self.assertEqual(particle.name, 'D0')
92  self.assertEqual(particle.label, 'generic')
93  self.assertEqual(particle.mvaConfig, mvaConfig)
94  self.assertEqual(particle.preCutConfig, preCutConfig)
95  self.assertEqual(particle.postCutConfig, postCutConfig)
96  self.assertListEqual(particle.channels, [])
97  self.assertListEqual(particle.daughters, [])
98 
99  def test_NonDefaults(self):
100  mvaConfig = fei.config.MVAConfiguration(variables=['E'])
101  preCutConfig = fei.config.PreCutConfiguration(userCut='test')
102  postCutConfig = fei.config.PostCutConfiguration(value=0.1)
103  particle = fei.config.Particle('D0', mvaConfig, preCutConfig, postCutConfig)
104 
105  self.assertEqual(particle.identifier, 'D0:generic')
106  self.assertEqual(particle.name, 'D0')
107  self.assertEqual(particle.label, 'generic')
108  self.assertEqual(particle.mvaConfig, mvaConfig)
109  self.assertEqual(particle.preCutConfig, preCutConfig)
110  self.assertEqual(particle.postCutConfig, postCutConfig)
111  self.assertListEqual(particle.channels, [])
112  self.assertListEqual(particle.daughters, [])
113 
114  def test_Label(self):
115  mvaConfig = fei.config.MVAConfiguration(variables=['E'])
116  particle = fei.config.Particle('D0:specific', mvaConfig)
117  self.assertEqual(particle.identifier, 'D0:specific')
118  self.assertEqual(particle.name, 'D0')
119  self.assertEqual(particle.label, 'specific')
120 
121  def test_Equality(self):
122  a = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
123  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
124  a.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
125  a.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
126 
127  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
128  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
129  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
130  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
131  self.assertEqual(a, b)
132 
133  b = fei.config.Particle('D+', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
134  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
135  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
136  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
137  self.assertNotEqual(a, b)
138 
139  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
140  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
141  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
142  self.assertNotEqual(a, b)
143 
144  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['daughter({}, p)', 'daughterAngle({}, {})']),
145  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
146  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
147  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
148  self.assertNotEqual(a, b)
149 
150  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
151  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
152  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['daughter({}, p)']))
153  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
154  self.assertNotEqual(a, b)
155 
156  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
157  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration())
158  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
159  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='test'))
160  self.assertNotEqual(a, b)
161 
162  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
163  fei.config.PreCutConfiguration(userCut='test'), fei.config.PostCutConfiguration(value=0.1))
164  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
165  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
166  self.assertNotEqual(a, b)
167 
168  b = fei.config.Particle('D0', fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})']),
169  fei.config.PreCutConfiguration(userCut='dummy'), fei.config.PostCutConfiguration())
170  b.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
171  b.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
172  self.assertNotEqual(a, b)
173 
174  def test_AddChannel(self):
175  mvaConfig = fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)', 'daughterAngle({}, {})'])
176  preCutConfig = fei.config.PreCutConfiguration(userCut='test')
177  postCutConfig = fei.config.PostCutConfiguration()
178  particle = fei.config.Particle('D0', mvaConfig, preCutConfig, postCutConfig)
179 
180  particle.addChannel(['pi-', 'pi+'], mvaConfig=fei.config.MVAConfiguration(variables=['E', 'daughter({}, p)']))
181  particle.addChannel(['K-', 'pi+'], preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'))
182  particle.addChannel(['K-:specific', 'pi+', 'pi0'])
183  particle.addChannel(['K-', 'K+'])
184 
185  channels = []
186  channels.append(fei.config.DecayChannel(name='D0:generic_0',
187  label='D0:generic ==> pi-:generic pi+:generic',
188  decayString='D0:generic_0 -> pi-:generic pi+:generic',
189  daughters=['pi-:generic', 'pi+:generic'],
190  mvaConfig=fei.config.MVAConfiguration(variables=['E',
191  'daughter(0, p)',
192  'daughter(1, p)']),
193  preCutConfig=fei.config.PreCutConfiguration(userCut='test'),
194  decayModeID=0))
195  channels.append(fei.config.DecayChannel(name='D0:generic_1',
196  label='D0:generic ==> K-:generic pi+:generic',
197  decayString='D0:generic_1 -> K-:generic pi+:generic',
198  daughters=['K-:generic', 'pi+:generic'],
199  mvaConfig=fei.config.MVAConfiguration(variables=['E',
200  'daughter(0, p)',
201  'daughter(1, p)',
202  'daughterAngle(0, 1)']),
203  preCutConfig=fei.config.PreCutConfiguration(userCut='alternative'),
204  decayModeID=1))
205  channels.append(fei.config.DecayChannel(name='D0:generic_2',
206  label='D0:generic ==> K-:specific pi+:generic pi0:generic',
207  decayString='D0:generic_2 -> K-:specific pi+:generic pi0:generic',
208  daughters=['K-:specific', 'pi+:generic', 'pi0:generic'],
209  mvaConfig=fei.config.MVAConfiguration(variables=['E',
210  'daughter(0, p)',
211  'daughter(1, p)',
212  'daughter(2, p)',
213  'daughterAngle(0, 1)',
214  'daughterAngle(0, 2)',
215  'daughterAngle(1, 2)']),
216  preCutConfig=fei.config.PreCutConfiguration(userCut='test'),
217  decayModeID=2))
218  channels.append(fei.config.DecayChannel(name='D0:generic_3',
219  label='D0:generic ==> K-:generic K+:generic',
220  decayString='D0:generic_3 -> K-:generic K+:generic',
221  daughters=['K-:generic', 'K+:generic'],
222  mvaConfig=fei.config.MVAConfiguration(variables=['E',
223  'daughter(0, p)',
224  'daughter(1, p)',
225  'daughterAngle(0, 1)']),
226  preCutConfig=fei.config.PreCutConfiguration(userCut='test'),
227  decayModeID=3))
228  self.assertEqual(particle.channels, channels)
229  self.assertEqual(sorted(particle.daughters), sorted(['K+:generic', 'K-:generic', 'pi+:generic',
230  'pi-:generic', 'pi0:generic', 'K-:specific']))
231 
232 
233 class TestVariables2MonitoringBinning(unittest.TestCase):
234 
235  def test_Variables2MonitoringBinning1D(self):
236  self.assertEqual(fei.config.variables2binnings(['mcErrors']), [('mcErrors', 513, -0.5, 512.5)])
237  self.assertEqual(fei.config.variables2binnings(['default']), [('default', 100, -10.0, 10.0)])
238 
239  def test_Variables2MonitoringBinning2D(self):
240  self.assertEqual(fei.config.variables2binnings_2d([('mcErrors', 'default')]),
241  [('mcErrors', 513, -0.5, 512.5, 'default', 100, -10.0, 10.0)])
242  self.assertEqual(fei.config.variables2binnings_2d([('default', 'default'), ('mcErrors', 'mcErrors')]),
243  [('default', 100, -10.0, 10.0, 'default', 100, -10.0, 10.0),
244  ('mcErrors', 513, -0.5, 512.5, 'mcErrors', 513, -0.5, 512.5)])
245 
246 
247 if __name__ == '__main__':
248  tempdir = tempfile.mkdtemp()
249  os.chdir(tempdir)
250  # main() never returns, so install exit handler to do our cleanup
251  atexit.register(shutil.rmtree, tempdir)
252  unittest.main()
253 
254 # @endcond