Belle II Software development
klm_packer_unpacker.py
1#!/usr/bin/env python
2
3
10
11import basf2
12import simulation as sim
13from ROOT import Belle2
14
15basf2.set_random_seed(321)
16basf2.set_log_level(basf2.LogLevel.INFO)
17
18
19class PackerUnpackerTest(basf2.Module):
20
21 """
22 module which checks if two collections of EKLMDigits and BKLMDigits are
23 equal
24 """
25
26 def sort_klm_digits(self, unsortedPyStoreArray):
27 """
28 Use some digit information to sort the digits
29 Returns a python-list containing the sorted digits
30 """
31
32 # first convert to a python-list to be able to sort
33 py_list = [x for x in unsortedPyStoreArray]
34
35 # sort via a hierarchy of sort keys
36 return sorted(
37 py_list,
38 key=lambda x: (
39 x.getSubdetector(),
40 x.getSection(),
41 x.getSector(),
42 x.getLayer(),
43 x.getPlane(),
44 x.getStrip())
45 )
46
47 def event(self):
48 """
49 load original digits and the packed/unpacked ones, sort and compare them
50 """
51
52 # direct from simulation
53 klm_digits = Belle2.PyStoreArray("KLMDigits")
54 # processed by packer and unpacker
55 klm_digits_unpacked = Belle2.PyStoreArray("KLMDigitsUnpacked")
56
57 # sort digits
58 klm_digits_sorted = self.sort_klm_digits(klm_digits)
59 klm_digits_unpacked_sorted = self.sort_klm_digits(klm_digits_unpacked)
60
61 # check the size
62 if not len(klm_digits_sorted) == len(klm_digits_unpacked_sorted):
63 basf2.B2FATAL("KLMDigits: size not equal after packing and unpacking")
64 # check all quantities between the direct and the packed/unpacked
65 for i in range(len(klm_digits_sorted)):
66
67 digit = klm_digits_sorted[i]
68 digit_unpacked = klm_digits_unpacked_sorted[i]
69
70 # check the content of the digit
71 assert digit.getSubdetector() == digit_unpacked.getSubdetector()
72 assert digit.getSection() == digit_unpacked.getSection()
73 assert digit.getSector() == digit_unpacked.getSector()
74 assert digit.getLayer() == digit_unpacked.getLayer()
75 assert digit.getPlane() == digit_unpacked.getPlane()
76 assert digit.getStrip() == digit_unpacked.getStrip()
77 assert digit.inRPC() == digit_unpacked.inRPC()
78 assert digit.getCharge() == digit_unpacked.getCharge()
79 assert digit.getCTime() == digit_unpacked.getCTime()
80 assert digit.getTDC() == digit_unpacked.getTDC()
81 assert digit.getFitStatus() == digit_unpacked.getFitStatus()
82
83
84main = basf2.create_path()
85
86eventinfosetter = basf2.register_module('EventInfoSetter')
87eventinfosetter.param('evtNumList', 50)
88main.add_module(eventinfosetter)
89
90particlegun = basf2.register_module('ParticleGun')
91particlegun.param('pdgCodes', [13, -13])
92particlegun.param('nTracks', 10)
93particlegun.param('momentumParams', [0.5, 4.0])
94main.add_module(particlegun)
95
96sim.add_simulation(main, components=['KLM'])
97basf2.set_module_parameters(main, type='Geometry', useDB=False, components=['KLM'])
98
99klm_packer = basf2.register_module('KLMPacker')
100main.add_module(klm_packer)
101
102unpacker = basf2.register_module('KLMUnpacker')
103unpacker.param('outputKLMDigitsName', 'KLMDigitsUnpacked')
104main.add_module(unpacker)
105
106main.add_module(PackerUnpackerTest())
107
108progress = basf2.register_module('Progress')
109main.add_module(progress)
110
111basf2.process(main)
112print(basf2.statistics)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def sort_klm_digits(self, unsortedPyStoreArray)