Belle II Software  release-08-01-10
treeFit_referenced_single_displaced_vertex.py
1 #!/usr/bin/env python3
2 
3 
10 import unittest
11 import tempfile
12 import basf2
13 import modularAnalysis as ma
14 import b2test_utils
15 from ROOT import TFile
16 
17 
18 class 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(
29  'analysis/1000_B_Jpsi_ks_pipi.root', 'validation', py_case=self)
30  ma.inputMdst(inputfile, path=main)
31 
32  ma.fillParticleList('pi+:a', 'pionID > 0.5', path=main)
33 
34  ma.reconstructDecay('K_S0:pipi -> pi+:a pi-:a', '', 0, path=main)
35  ma.matchMCTruth('K_S0:pipi', path=main)
36 
37  conf = 0
38  main.add_module('TreeFitter',
39  particleList='K_S0:pipi',
40  confidenceLevel=conf,
41  massConstraintList=[],
42  massConstraintListParticlename=[],
43  expertUseReferencing=True,
44  ipConstraint=False,
45  updateAllDaughters=False)
46 
47  ma.printMCParticles(path=main, suppressPrint=True, showStatus=True, showMomenta=True)
48 
49  ntupler = basf2.register_module('VariablesToNtuple')
50  ntupler.param('fileName', testFile.name)
51  ntupler.param('variables', ['chiProb', 'M', 'isSignal', 'mcErrors', 'genParticleID',
52  'daughter(0,genParticleID)', 'daughter(1,genParticleID)'])
53  ntupler.param('particleList', 'K_S0:pipi')
54  main.add_module(ntupler)
55 
56  basf2.process(main)
57 
58  ntuplefile = TFile(testFile.name)
59  ntuple = ntuplefile.Get('ntuple')
60 
61  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
62 
63  allBkg = ntuple.GetEntries("isSignal == 0")
64  allSig = ntuple.GetEntries("isSignal > 0")
65 
66  truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
67  falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
68 
69  mustBeZero = ntuple.GetEntries(f"(chiProb < {conf})")
70 
71  print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
72  print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
73 
74  self.assertFalse(truePositives == 0, "No signal survived the fit.")
75 
76  self.assertTrue(falsePositives == 3449, f"Too many false positives: {falsePositives} out of {allBkg} total bkg events.")
77 
78  self.assertTrue(truePositives == 555, "Signal rejection too high")
79  self.assertFalse(mustBeZero, f"We should have dropped all candidates with confidence level less than {conf}.")
80 
81  print("Test passed, cleaning up.")
82 
83 
84 if __name__ == '__main__':
86  unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189