Belle II Software development
treeFit_referenced_pipipi0.py
1#!/usr/bin/env python3
2
3
10import unittest
11import tempfile
12import basf2
13import b2test_utils
14import modularAnalysis as ma
15from ROOT import TFile
16
17
18class TestTreeFits(unittest.TestCase):
19 """The unit test case for TreeFitter"""
20
21 def testFit(self):
22 """Run the test fit"""
23
24 testFile = tempfile.NamedTemporaryFile()
25
26 main = basf2.create_path()
27
28 inputfile = b2test_utils.require_file('analysis/B0ToPiPiPi0.root', 'validation', py_case=self)
29 ma.inputMdst(inputfile, 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 conf = 0
40 main.add_module('TreeFitter',
41 particleList='B0:rec',
42 confidenceLevel=conf,
43 massConstraintList=[],
44 massConstraintListParticlename=[],
45 expertUseReferencing=True,
46 ipConstraint=False,
47 updateAllDaughters=True)
48
49 ntupler = basf2.register_module('VariablesToNtuple')
50 ntupler.param('fileName', testFile.name)
51 ntupler.param('variables', ['chiProb', 'M', 'isSignal'])
52 ntupler.param('particleList', 'B0:rec')
53 main.add_module(ntupler)
54
55 basf2.process(main)
56
57 ntuplefile = TFile(testFile.name)
58 ntuple = ntuplefile.Get('ntuple')
59
60 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
61
62 allBkg = ntuple.GetEntries("isSignal == 0")
63 allSig = ntuple.GetEntries("isSignal > 0")
64
65 truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
66 falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
67
68 mustBeZero = ntuple.GetEntries(f"(chiProb < {conf})")
69
70 print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
71 print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
72
73 self.assertFalse(truePositives == 0, "No signal survived the fit.")
74
75 self.assertTrue(falsePositives < 8299, "Background rejection increased.")
76
77 self.assertTrue(truePositives > 212, "Signal rejection too high")
78 self.assertFalse(mustBeZero, f"We should have dropped all candidates with confidence level less than {conf}.")
79
80 print("Test passed, cleaning up.")
81
82
83if __name__ == '__main__':
85 unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189