Belle II Software  release-06-02-00
allparticlecombiner.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 variables import variables
17 from vertex import kFit
18 from ROOT import TFile
19 
20 
21 class TestAllParticleCombiner(unittest.TestCase):
22  """The unit test case for the AllParticleCombiner"""
23 
24  def testCombination(self):
25  """Run the test fit"""
26 
27  testFile = tempfile.NamedTemporaryFile()
28  # make logging more reproducible by replacing some strings.
29  # Also make sure the testfile name is replaced if necessary
30  b2test_utils.configure_logging_for_tests({testFile.name: "${testfile}"})
31 
32  basf2.set_random_seed("1234")
33  main = basf2.create_path()
34  inputfile = b2test_utils.require_file(
35  'analysis/tests/mdst.root', py_case=self)
36  main.add_module(
37  'RootInput',
38  inputFileNames=[inputfile],
39  logLevel=basf2.LogLevel.ERROR)
40 
41  ma.fillParticleList('pi+:fromPV', 'dr < 2 and abs(dz) < 5', path=main)
42 
43  ma.variablesToExtraInfo('pi+:fromPV', {'medianValueInList(pi+:fromPV, dz)': 'medianDZ'}, path=main)
44  variables.addAlias('medianDzFromPV', 'extraInfo(medianDZ)')
45  variables.addAlias('dzFromMedianDz', 'formula(dz - medianDzFromPV)')
46  ma.applyCuts('pi+:fromPV', 'abs(dzFromMedianDz) < 0.05', path=main)
47 
48  ma.combineAllParticles(['pi+:fromPV'], 'vpho:PVFit', path=main)
49 
50  kFit('vpho:PVFit', conf_level=-1, fit_type='vertex', constraint='iptube', path=main)
51 
52  variables.addAlias('PVX', 'x')
53  variables.addAlias('PVY', 'y')
54  variables.addAlias('PVZ', 'z')
55  variables.addAlias('nGoodTracksFromPV', 'nParticlesInList(pi+:fromPV)')
56 
57  main.add_module(
58  'VariablesToNtuple',
59  particleList='vpho:PVFit',
60  fileName=testFile.name,
61  variables=[
62  'PVX',
63  'PVY',
64  'PVZ',
65  'chiProb',
66  'nGoodTracksFromPV',
67  'distance'])
68 
69  ma.summaryOfLists(['vpho:PVFit'], path=main)
70 
71  basf2.process(main)
72 
73  ntuplefile = TFile(testFile.name)
74  ntuple = ntuplefile.Get('ntuple')
75 
76  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
77 
78  converged = ntuple.GetEntries("chiProb > 0")
79 
80  self.assertFalse(converged == 0, "No fit converged.")
81 
82  print("Test passed, cleaning up.")
83 
84 
85 if __name__ == '__main__':
86  unittest.main()
def configure_logging_for_tests(user_replacements=None)
Definition: __init__.py:106
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54