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