Belle II Software light-2406-ragdoll
treeFit.py
1#!/usr/bin/env python3
2
3
10import unittest
11import tempfile
12import basf2
13import modularAnalysis as ma
14from vertex import treeFit
15import b2test_utils
16from ROOT import TFile
17
18
19class TestTreeFits(unittest.TestCase):
20 """The unit test case for TreeFitter"""
21
22 def testFit(self):
23 """Run the test fit"""
24
25 testFile = tempfile.NamedTemporaryFile()
26
27 main = basf2.create_path()
28
29 ma.inputMdst(b2test_utils.require_file('analysis/B0ToPiPiPi0.root', 'validation'), path=main)
30
31 ma.fillParticleList('pi+:a', 'pionID > 0.5', path=main)
32
33 ma.fillParticleList('gamma:a', '', path=main)
34 ma.reconstructDecay('pi0:a -> gamma:a gamma:a', '0.125 < InvM < 0.145', 0, path=main)
35
36 ma.reconstructDecay('B0:rec -> pi-:a pi+:a pi0:a', '', 0, path=main)
37 ma.matchMCTruth('B0:rec', path=main)
38
39 treeFit('B0:rec', conf_level=-1, ipConstraint=False, updateAllDaughters=True, path=main)
40
41 ntupler = basf2.register_module('VariablesToNtuple')
42 ntupler.param('fileName', testFile.name)
43 ntupler.param('variables', ['chiProb', 'M', 'isSignal'])
44 ntupler.param('particleList', 'B0:rec')
45 main.add_module(ntupler)
46
47 basf2.process(main)
48
49 ntuplefile = TFile(testFile.name)
50 ntuple = ntuplefile.Get('ntuple')
51
52 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
53
54 allBkg = ntuple.GetEntries("isSignal == 0")
55 allSig = ntuple.GetEntries("isSignal > 0")
56
57 truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
58 falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
59
60 print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
61 print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
62
63 self.assertTrue(truePositives > 212, f"Signal rejection too high. True positives: {truePositives}")
64
65 self.assertTrue(falsePositives < 8299, f"Background rejection got worse. False positives: {falsePositives}")
66
67 print("Test passed, cleaning up.")
68
69
70if __name__ == '__main__':
72 unittest.main()
def testFit(self)
Definition: treeFit.py:22
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189