Belle II Software development
top_packer_unpacker.py
1#!/usr/bin/env python
2
3
10
11import basf2 as b2
12from ROOT import Belle2
13from simulation import add_simulation
14
15
16b2.set_random_seed(12345)
17
18
19class DigitsTest(b2.Module):
20
21 """
22 module which ckecks if two collections of TOPDigits are equal
23 """
24
25 def sortDigits(self, unsortedPyStoreArray):
26 """
27 Use some digit information to sort the digits
28 Returns a python-list containing the sorted digits
29 """
30
31 # first convert to a python-list to be able to sort
32 py_list = [x for x in unsortedPyStoreArray]
33
34 # sort via a hierachy of sort keys
35 return sorted(
36 py_list,
37 key=lambda x: (
38 x.getModuleID(),
39 x.getChannel(),
40 x.getRawTime())
41 )
42
43 def event(self):
44 """
45 load original TOPDigits and the packed/unpacked ones, sort and compare them
46 """
47
48 # direct from simulation
49 digits = Belle2.PyStoreArray("TOPDigits")
50 # processed by packer and unpacker
51 digitsUnpacked = Belle2.PyStoreArray("TOPDigitsUnpacked")
52
53 # sort digits
54 digits_sorted = self.sortDigits(digits)
55 digitsUnpacked_sorted = self.sortDigits(digitsUnpacked)
56
57 # check the sizes
58 if not len(digits_sorted) == len(digitsUnpacked_sorted):
59 print('size: ', len(digits_sorted), len(digitsUnpacked_sorted))
60 b2.B2FATAL("TOPDigits: size not equal after packing and unpacking")
61
62 # check all quantities between the direct and the packed/unpacked
63 precision = 0.0001 # precision for floats (e.g. in [ns])
64 for i in range(len(digits_sorted)):
65
66 digit = digits_sorted[i]
67 digitUnpacked = digitsUnpacked_sorted[i]
68
69 # check the content of the digit
70 assert digit.getModuleID() == digitUnpacked.getModuleID()
71 assert digit.getPixelID() == digitUnpacked.getPixelID()
72 assert digit.getChannel() == digitUnpacked.getChannel()
73 assert digit.getRawTime() == digitUnpacked.getRawTime()
74 assert abs(digit.getTime() - digitUnpacked.getTime()) < precision
75 assert abs(digit.getTimeError() - digitUnpacked.getTimeError()) < precision
76 assert digit.getPulseHeight() == digitUnpacked.getPulseHeight()
77 assert abs(digit.getPulseWidth() - digitUnpacked.getPulseWidth()) < precision
78 # assert digit.getIntegral() == digitUnpacked.getIntegral()
79 assert digit.getFirstWindow() == digitUnpacked.getFirstWindow()
80 assert digit.getHitQuality() == digitUnpacked.getHitQuality()
81 assert digit.getStatus() == digitUnpacked.getStatus()
82 assert digit.isChargeShare() == digitUnpacked.isChargeShare()
83 assert digit.isPrimaryChargeShare() == digitUnpacked.isPrimaryChargeShare()
84
85
86class RawDigitsTest(b2.Module):
87
88 """
89 module which ckecks if two collections of TOPRawDigits are equal
90 """
91
92 def sortDigits(self, unsortedPyStoreArray):
93 """
94 Use some digit information to sort the digits
95 Returns a python-list containing the sorted digits
96 """
97
98 # first convert to a python-list to be able to sort
99 py_list = [x for x in unsortedPyStoreArray]
100
101 # sort via a hierachy of sort keys
102 return sorted(
103 py_list,
104 key=lambda x: (
105 x.getScrodID(),
106 x.getCarrierNumber(),
107 x.getASICNumber(),
108 x.getASICChannel(),
109 x.getSampleRise())
110 )
111
112 def event(self):
113 """
114 load original TOPRawDigits and the packed/unpacked ones, sort and compare them
115 """
116
117 # direct from simulation
118 digits = Belle2.PyStoreArray("TOPRawDigits")
119 # processed by packer and unpacker
120 digitsUnpacked = Belle2.PyStoreArray("TOPRawDigitsUnpacked")
121
122 # sort digits
123 digits_sorted = self.sortDigits(digits)
124 digitsUnpacked_sorted = self.sortDigits(digitsUnpacked)
125
126 # check the sizes
127 if not len(digits_sorted) == len(digitsUnpacked_sorted):
128 print('size: ', len(digits_sorted), len(digitsUnpacked_sorted))
129 b2.B2FATAL("TOPRawDigits: size not equal after packing and unpacking")
130
131 # check all quantities between the direct and the packed/unpacked
132 for i in range(len(digits_sorted)):
133
134 digit = digits_sorted[i]
135 digitUnpacked = digitsUnpacked_sorted[i]
136
137 # check the content of the digit
138 assert digit.getScrodID() == digitUnpacked.getScrodID()
139 assert digit.getCarrierNumber() == digitUnpacked.getCarrierNumber()
140 assert digit.getASICNumber() == digitUnpacked.getASICNumber()
141 assert digit.getASICChannel() == digitUnpacked.getASICChannel()
142 assert digit.getASICWindow() == digitUnpacked.getASICWindow()
143 assert digit.getTFine() == digitUnpacked.getTFine()
144 assert digit.getSampleRise() == digitUnpacked.getSampleRise()
145 assert digit.getDeltaSamplePeak() == digitUnpacked.getDeltaSamplePeak()
146 assert digit.getDeltaSampleFall() == digitUnpacked.getDeltaSampleFall()
147 assert digit.getValueRise0() == digitUnpacked.getValueRise0()
148 assert digit.getValueRise1() == digitUnpacked.getValueRise1()
149 assert digit.getValuePeak() == digitUnpacked.getValuePeak()
150 assert digit.getValueFall0() == digitUnpacked.getValueFall0()
151 assert digit.getValueFall1() == digitUnpacked.getValueFall1()
152 # assert digit.getIntegral() == digitUnpacked.getIntegral()
153 assert digit.getRevo9Counter() == digitUnpacked.getRevo9Counter()
154 assert digit.getPhase() == digitUnpacked.getPhase()
155 assert digit.getErrorFlags() == digitUnpacked.getErrorFlags()
156 assert digit.getDataType() == digitUnpacked.getDataType()
157
158
159main = b2.create_path()
160
161eventinfosetter = b2.register_module('EventInfoSetter')
162eventinfosetter.param({'evtNumList': [10]})
163main.add_module(eventinfosetter)
164
165particlegun = b2.register_module('ParticleGun')
166particlegun.param('pdgCodes', [13, -13])
167particlegun.param('nTracks', 10)
168main.add_module(particlegun)
169
170add_simulation(main, components=['TOP'])
171b2.set_module_parameters(main, type="Geometry", useDB=False, components=["TOP"])
172
173Packer = b2.register_module('TOPPacker')
174main.add_module(Packer)
175
176unPacker = b2.register_module('TOPUnpacker')
177unPacker.param('outputRawDigitsName', 'TOPRawDigitsUnpacked')
178unPacker.param('outputDigitsName', 'TOPDigitsUnpacked')
179main.add_module(unPacker)
180
181converter = b2.register_module('TOPRawDigitConverter')
182converter.param('inputRawDigitsName', 'TOPRawDigitsUnpacked')
183converter.param('outputDigitsName', 'TOPDigitsUnpacked')
184converter.param('minPulseWidth', 0.0)
185converter.param('maxPulseWidth', 1000.0)
186main.add_module(converter)
187
188main.add_module(RawDigitsTest())
189main.add_module(DigitsTest())
190
191progress = b2.register_module('Progress')
192main.add_module(progress)
193
194b2.process(main)
195print(b2.statistics)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def sortDigits(self, unsortedPyStoreArray)
def sortDigits(self, unsortedPyStoreArray)