Belle II Software light-2406-ragdoll
test_std_v0.py
1#!/usr/bin/env python3
2
3
10
11import unittest
12import tempfile
13import b2test_utils
14
15from basf2 import create_path, process
16import modularAnalysis as ma
17import stdV0s
18from ROOT import TFile
19
20
21class TestStdV0(unittest.TestCase):
22 """Test case for standard V0 lists"""
23
25 self,
26 std_function=stdV0s.stdKshorts,
27 expected_modules=[
28 "ParticleLoader",
29 "ParticleListManipulator",
30 "TreeFitter",
31 "ParticleSelector",
32 "ParticleLoader",
33 "ParticleCombiner",
34 "TreeFitter",
35 "ParticleSelector",
36 "ParticleListManipulator"],
37 expected_lists=['V0_ToFit', 'V0_ToFit', 'all', 'RD', 'merged']):
38 """check that a given function works"""
39 testpath = create_path()
40 std_function(path=testpath)
41
42 loaded_modules = []
43 built_lists = []
44 for module in testpath.modules():
45 loaded_modules.append(module.type())
46 for param in module.available_params():
47 if module.type() == 'ParticleLoader' and param.name == 'decayStrings':
48 name = param.values[0].split(':')[1].split(' -> ')[0]
49 built_lists.append(name)
50 if module.type() == 'ParticleListManipulator' and param.name == 'outputListName':
51 name = str(param.values).split(':')[1].split(' -> ')[0]
52 built_lists.append(name)
53 if module.type() == 'ParticleCombiner' and param.name == 'decayString':
54 name = param.values.split(':')[1].split(' -> ')[0]
55 built_lists.append(name)
56
57 # Check that we load the correct modules
58 self.assertListEqual(
59 loaded_modules, expected_modules,
60 f"Loaded modules do not match the expected ones (function {std_function.__name__})")
61
62 # Check that we load the correct particle lists
63 self.assertListEqual(
64 built_lists, expected_lists,
65 f"Built particles lists do not match the expected ones (function {std_function.__name__})")
66
68 """check that the builder function works with the stdKshorts list"""
69 self._check_list()
70
71 def test_belle_list(self):
72 """check that the builder function works with the legacy Belle Kshorts list"""
73 expected_modules = ["ParticleLoader",
74 "ParticleListManipulator",
75 "ParticleSelector",
76 "ParticleVertexFitter",
77 "ParticleSelector"]
78 self._check_list(std_function=stdV0s.goodBelleKshort, expected_modules=expected_modules,
79 expected_lists=["legacyGoodKS", "legacyGoodKS"])
80
82 """check that the builder function works with the stdLambdas list"""
83 expected_modules = ["ParticleLoader",
84 "ParticleListManipulator",
85 "TreeFitter",
86 "ParticleSelector",
87 "DuplicateVertexMarker",
88 "ParticleSelector",
89 "ParticleLoader",
90 "ParticleLoader",
91 "ParticleCombiner",
92 "TreeFitter",
93 "ParticleSelector",
94 "DuplicateVertexMarker",
95 "ParticleSelector",
96 "ParticleListManipulator"]
97 expected_lists = ['V0_ToFit', 'V0_ToFit', 'all', 'all', 'RD', 'merged']
98 self._check_list(std_function=stdV0s.stdLambdas, expected_modules=expected_modules, expected_lists=expected_lists)
99
101 """check the number of signals in K_S0:merged and K_S0:scaled lists"""
102
103 main = create_path()
104
105 inputfile = b2test_utils.require_file('analysis/1000_B_Jpsi_ks_pipi.root', 'validation', py_case=self)
106 ma.inputMdst(inputfile, path=main)
107
108 stdV0s.stdKshorts(path=main) # -> K_S0:merged
109 stdV0s.scaleErrorKshorts(path=main) # -> K_S0:scaled
110
111 ma.matchMCTruth('K_S0:merged', path=main)
112 ma.matchMCTruth('K_S0:scaled', path=main)
113
114 testFile = tempfile.NamedTemporaryFile()
115 ma.variablesToNtuple('K_S0:merged', ['isSignal', 'M'], filename=testFile.name, treename='merged', path=main)
116 ma.variablesToNtuple('K_S0:scaled', ['isSignal', 'M'], filename=testFile.name, treename='scaled', path=main)
117
118 process(main)
119
120 ntuplefile = TFile(testFile.name)
121 ntuple_merged = ntuplefile.Get('merged')
122 ntuple_scaled = ntuplefile.Get('scaled')
123
124 allSig_merged = ntuple_merged.GetEntries("isSignal == 1")
125 allSig_scaled = ntuple_scaled.GetEntries("isSignal == 1")
126
127 print(f"Number of signal K_S0:merged: {allSig_merged}")
128 print(f"Number of signal K_S0:scaled: {allSig_scaled}")
129
130 self.assertTrue(allSig_merged > 999, "Number of signal K_S0:merged is too small.")
131 self.assertTrue(allSig_scaled > 1000, "Number of signal K_S0:scaled is too small.")
132
133 tightMSig_merged = ntuple_merged.GetEntries("isSignal == 1 && M > 0.48 && M < 0.52")
134 tightMSig_scaled = ntuple_scaled.GetEntries("isSignal == 1 && M > 0.48 && M < 0.52")
135
136 print(f"Number of signal K_S0:merged with 0.48<M<0.52: {tightMSig_merged}")
137 print(f"Number of signal K_S0:scaled with 0.48<M<0.52: {tightMSig_scaled}")
138
139 self.assertTrue(tightMSig_merged > 962, "Number of signal K_S0:merged with 0.48<M<0.52 is too small.")
140 self.assertTrue(tightMSig_scaled > 962, "Number of signal K_S0:scaled with 0.48<M<0.52 is too small.")
141
142 print("Test passed, cleaning up.")
143
144
145if __name__ == '__main__':
146 unittest.main()
def _check_list(self, std_function=stdV0s.stdKshorts, expected_modules=["ParticleLoader", "ParticleListManipulator", "TreeFitter", "ParticleSelector", "ParticleLoader", "ParticleCombiner", "TreeFitter", "ParticleSelector", "ParticleListManipulator"], expected_lists=['V0_ToFit', 'V0_ToFit', 'all', 'RD', 'merged'])
Definition: test_std_v0.py:37
def test_stdlambdas_list(self)
Definition: test_std_v0.py:81
def test_stdkshorts_list(self)
Definition: test_std_v0.py:67
def test_kshort_signals(self)
Definition: test_std_v0.py:100
def test_belle_list(self)
Definition: test_std_v0.py:71
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def stdKshorts(prioritiseV0=True, fitter='TreeFit', path=None, updateAllDaughters=False, writeOut=False)
Definition: stdV0s.py:17
def scaleErrorKshorts(prioritiseV0=True, fitter='TreeFit', scaleFactors_V0=[1.125927, 1.058803, 1.205928, 1.066734, 1.047513], scaleFactorsNoPXD_V0=[1.125927, 1.058803, 1.205928, 1.066734, 1.047513], d0Resolution_V0=[0.001174, 0.000779], z0Resolution_V0=[0.001350, 0.000583], d0MomThr_V0=0.500000, z0MomThr_V0=0.00000, scaleFactors_RD=[1.149631, 1.085547, 1.151704, 1.096434, 1.086659], scaleFactorsNoPXD_RD=[1.149631, 1.085547, 1.151704, 1.096434, 1.086659], d0Resolution_RD=[0.00115328, 0.00134704], z0Resolution_RD=[0.00124327, 0.0013272], d0MomThr_RD=0.500000, z0MomThr_RD=0.500000, path=None)
Definition: stdV0s.py:101