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