Belle II Software development
test_chargedCluster.py
1#!/usr/bin/env python3
2
3
10
11import unittest
12import tempfile
13from basf2 import create_path, register_module
14import b2test_utils
15import modularAnalysis as ma
16from variables import variables as vm
17
18from ROOT import TFile
19
20
21class TestChargedCluster(unittest.TestCase):
22 """The unit test"""
23
24 def test(self):
25 """ Test of fillParticleListFromChargedCluster """
26 testFile = tempfile.NamedTemporaryFile()
27
28 main = create_path()
29
30 inputfile = b2test_utils.require_file('mdst16.root', 'validation', py_case=self)
31 ma.inputMdst(inputfile, path=main)
32
33 ma.fillParticleList('pi+:ecl', 'clusterE>0', path=main)
34 ma.fillParticleList('pi+:klm', 'klmClusterEnergy>0', path=main)
35
36 ma.fillParticleListFromChargedCluster('gamma:bestecl', 'pi+:ecl', '',
37 useOnlyMostEnergeticECLCluster=True, path=main)
38 ma.fillParticleListFromChargedCluster('gamma:allecl', 'pi+:ecl', '',
39 useOnlyMostEnergeticECLCluster=False, path=main)
40
41 ma.fillParticleListFromChargedCluster('gamma:klmT', 'pi+:klm', '',
42 useOnlyMostEnergeticECLCluster=True, path=main)
43 ma.fillParticleListFromChargedCluster('gamma:klmF', 'pi+:klm', '',
44 useOnlyMostEnergeticECLCluster=False, path=main)
45
46 ma.fillParticleListFromChargedCluster('K_L0:ecl', 'pi+:ecl', '', path=main)
47 ma.fillParticleListFromChargedCluster('K_L0:klm', 'pi+:klm', '', path=main)
48
49 ma.applyCuts('gamma:bestecl', 'isFromECL', path=main)
50 ma.applyCuts('gamma:allecl', 'isFromECL', path=main)
51 ma.applyCuts('gamma:klmT', 'isFromKLM', path=main)
52 ma.applyCuts('gamma:klmF', 'isFromKLM', path=main)
53 ma.applyCuts('K_L0:ecl', 'isFromECL', path=main)
54 ma.applyCuts('K_L0:klm', 'isFromKLM', path=main)
55
56 vm.addAlias('n_pi_ecl', 'nParticlesInList(pi+:ecl)')
57 vm.addAlias('n_pi_klm', 'nParticlesInList(pi+:klm)')
58 vm.addAlias('n_gamma_bestecl', 'nParticlesInList(gamma:bestecl)')
59 vm.addAlias('n_gamma_allecl', 'nParticlesInList(gamma:allecl)')
60 vm.addAlias('n_gamma_klmT', 'nParticlesInList(gamma:klmT)')
61 vm.addAlias('n_gamma_klmF', 'nParticlesInList(gamma:klmF)')
62 vm.addAlias('n_KL_ecl', 'nParticlesInList(K_L0:ecl)')
63 vm.addAlias('n_KL_klm', 'nParticlesInList(K_L0:klm)')
64
65 ntupler = register_module('VariablesToNtuple')
66 ntupler.param('fileName', testFile.name)
67 ntupler.param('variables',
68 ['n_pi_ecl', 'n_pi_klm',
69 'n_gamma_bestecl', 'n_gamma_allecl', 'n_gamma_klmT', 'n_gamma_klmF',
70 'n_KL_ecl', 'n_KL_klm'])
71 main.add_module(ntupler)
72
74
75 ntuplefile = TFile(testFile.name)
76 ntuple = ntuplefile.Get('ntuple')
77
78 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
79
80 nEntries = ntuple.GetEntries()
81
82 # charged cluster seems always having flag of N1 (n photon). Is it true?
83 sameNumberForPiAndGamma_ECL = ntuple.GetEntries("n_pi_ecl == n_gamma_bestecl")
84 # not the case for N2?
85 sameNumberForPiAndHadron_ECL = ntuple.GetEntries("n_pi_ecl >= n_KL_ecl")
86 # KLM cluster has no preference for particle type
87 sameNumberForPiAndBothGammaHadron_KLM = ntuple.GetEntries("(n_pi_klm == n_gamma_klmT) && (n_pi_klm == n_KL_klm)")
88
89 self.assertTrue(nEntries == sameNumberForPiAndGamma_ECL,
90 "Charged ECL-cluster is not correctly loaded for gamma.")
91 self.assertTrue(nEntries == sameNumberForPiAndHadron_ECL,
92 "Charged ECL-cluster is not correctly loaded for neutral hadron.")
93 self.assertTrue(nEntries == sameNumberForPiAndBothGammaHadron_KLM,
94 "Charged KLM-cluster is not correctly loaded for both gamma and neutral hadron.")
95
96 allIsGreaterThanBest = ntuple.GetEntries("n_gamma_allecl >= n_gamma_bestecl")
97 self.assertTrue(nEntries == allIsGreaterThanBest, "Charged ECL-cluster is missed with useOnlyMostEnergeticECLCluster=False")
98
99 optionDoesNotChangeKLM = ntuple.GetEntries("n_gamma_klmT == n_gamma_klmF")
100 self.assertTrue(nEntries == optionDoesNotChangeKLM, "Charged KLM-cluster is affected by useOnlyMostEnergeticECLCluster")
101
102 print("Test passed, cleaning up.")
103
104
105if __name__ == '__main__':
107 unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189
def safe_process(*args, **kwargs)
Definition: __init__.py:236