Belle II Software  release-06-00-14
treeFit_referenced.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 import unittest
12 import tempfile
13 import basf2
14 import b2test_utils
15 import modularAnalysis as ma
16 from ROOT import TFile
17 
18 
19 class 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  # we want to use the latest grated globaltag, not the old one from the
28  # file
29  basf2.conditions.disable_globaltag_replay()
30 
31  main = basf2.create_path()
32 
33  inputfile = b2test_utils.require_file('analysis/tests/100_noBKG_B0ToPiPiPi0.root', py_case=self)
34  ma.inputMdst('default', inputfile, path=main)
35 
36  ma.fillParticleList('pi+:a', 'pionID > 0.5', path=main)
37 
38  ma.fillParticleList('gamma:a', '', path=main)
39  ma.reconstructDecay('pi0:a -> gamma:a gamma:a', '0.125 < InvM < 0.145', 0, path=main)
40 
41  ma.reconstructDecay('B0:rec -> pi-:a pi+:a pi0:a', '', 0, path=main)
42  ma.matchMCTruth('B0:rec', path=main)
43 
44  conf = 0
45  main.add_module('TreeFitter',
46  particleList='B0:rec',
47  confidenceLevel=conf,
48  massConstraintList=[],
49  massConstraintListParticlename=[],
50  expertUseReferencing=True,
51  ipConstraint=False,
52  updateAllDaughters=True)
53 
54  ntupler = basf2.register_module('VariablesToNtuple')
55  ntupler.param('fileName', testFile.name)
56  ntupler.param('variables', ['chiProb', 'M', 'isSignal'])
57  ntupler.param('particleList', 'B0:rec')
58  main.add_module(ntupler)
59 
60  basf2.process(main)
61 
62  ntuplefile = TFile(testFile.name)
63  ntuple = ntuplefile.Get('ntuple')
64 
65  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
66 
67  allBkg = ntuple.GetEntries("isSignal == 0")
68  allSig = ntuple.GetEntries("isSignal > 0")
69 
70  truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
71  falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
72 
73  mustBeZero = ntuple.GetEntries(f"(chiProb < {conf})")
74 
75  print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
76  print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
77 
78  self.assertFalse(truePositives == 0, "No signal survived the fit.")
79 
80  self.assertTrue(falsePositives < 2107, "Background rejection too small.")
81 
82  self.assertTrue(truePositives > 32, "Signal rejection too high")
83  self.assertFalse(mustBeZero, f"We should have dropped all candidates with confidence level less than {conf}.")
84 
85  print("Test passed, cleaning up.")
86 
87 
88 if __name__ == '__main__':
89  unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54