Belle II Software  release-06-02-00
pxd_packer_unpacker.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 
13 # print("TEST SKIPPED: Test fails due to changes in packer which were not propagated to unpacker. See BII-1647", file=sys.stderr)
14 # sys.exit(1)
15 
16 import basf2 as b2
17 from ROOT import Belle2
18 import numpy
19 
20 import simulation
21 from rawdata import add_packers
22 from rawdata import add_unpackers
23 
24 pxd_rawhits_pack_unpack_collection = "PXDRawHits_test"
25 pxd_rawhits_pack_unpack_collection_digits = "PXDDigits_test"
26 pxd_rawhits_pack_unpack_collection_adc = pxd_rawhits_pack_unpack_collection + "_adc"
27 pxd_rawhits_pack_unpack_collection_roi = pxd_rawhits_pack_unpack_collection + "_roi"
28 b2.set_random_seed(42)
29 
30 
31 class PxdPackerUnpackerTestModule(b2.Module):
32 
33  """
34  Module which checks if a collection of PXDDigits and
35  a collection of PXDRawHits from the packing/unpacking procedure are equal.
36  The PXDUnpacker does not create PXDDigits but PXDRawHits and therefore these two lists
37  must be compared.
38  """
39 
40  def __init__(self, rawhits_collection='PXDRawHits', digits_collection="PXDDigits"):
41  """constructor"""
42  # call constructor of base class, required if you implement __init__ yourself!
43  super().__init__()
44  # and do whatever else is necessary like declaring member variables
45 
46  self.rawhits_collectionrawhits_collection = rawhits_collection
47 
48  self.digits_collectiondigits_collection = digits_collection
49 
50  def sortDigits(self, unsortedPyStoreArray):
51  """ use a some digit information to sort the PXDDigits list
52  Returns a python-list containing the PXDDigts
53  """
54 
55  # first convert to a python-list to be abple to sort
56  py_list = [x for x in unsortedPyStoreArray]
57 
58  # sort via a hierachy of sort keys
59  return sorted(
60  py_list,
61  key=lambda x: (
62  x.getSensorID(),
63  x.getVCellID(),
64  x.getUCellID()))
65 
66  def sortRawHits(self, unsortedPyStoreArray):
67  """ use a some digit information to sort the PXDRawHits list
68  Returns a python-list containing the PXDRawHits
69  """
70 
71  # first convert to a python-list to be able to sort
72  py_list = [x for x in unsortedPyStoreArray]
73 
74  # sort via a hierachy of sort keys
75  return sorted(
76  py_list,
77  key=lambda x: (
78  x.getSensorID(),
79  x.getRow(),
80  x.getColumn()))
81 
82  def event(self):
83  """ load the PXD Digits of the simulation and the packed/unpacked ones
84  and compare them"""
85 
86  # load the digits and the collection which results from the packer and unpacker
87  # processed by packer and unpacker
88  pxdRawHitsPackedUnpacked_unsorted = Belle2.PyStoreArray(self.rawhits_collectionrawhits_collection)
89  # direct from simulation
90  pxdDigits_unsorted = Belle2.PyStoreArray(self.digits_collectiondigits_collection)
91 
92  # sort the digits, because the order gets
93  # lost during the packing/unpacking process
94  pxdDigits = self.sortDigitssortDigits(pxdDigits_unsorted)
95  pxdRawHitsPackedUnpacked = self.sortRawHitssortRawHits(pxdRawHitsPackedUnpacked_unsorted)
96 
97  if not len(pxdDigits) == len(pxdRawHitsPackedUnpacked):
98  b2.B2FATAL("PXDDigits and PXDRawHits count not equal after packing and unpacking")
99 
100  print("Comparing %i pxd digits " % len(pxdDigits))
101 
102  # check all quantities between the direct and the packed/unpacked pxd digits
103  for i in range(len(pxdDigits)):
104  digit = pxdDigits[i]
105  rawHitPackedUnpacked = pxdRawHitsPackedUnpacked[i]
106 
107  # compare all available quantities
108  # cannot compare frame number, because it is not availabl on PXDDigits
109  assert rawHitPackedUnpacked.getSensorID().getID() == digit.getSensorID().getID()
110  assert rawHitPackedUnpacked.getRow() == digit.getVCellID()
111  assert rawHitPackedUnpacked.getColumn() == digit.getUCellID()
112  # There are some rare cases (~ every 10th event), where the PXD Digits have a charge
113  # larger than 255 which will be clipped by the packer to 8bit (at most 255)
114  # therefor, limit the maximal charge of the digit here in the comparison
115  assert numpy.isclose(min(255.0, digit.getCharge()), rawHitPackedUnpacked.getCharge())
116 
117 
118 # to run the framework the used modules need to be registered
119 particlegun = b2.register_module('ParticleGun')
120 particlegun.param('pdgCodes', [13, -13])
121 particlegun.param('nTracks', 10)
122 
123 # Create Event information
124 eventinfosetter = b2.register_module('EventInfoSetter')
125 eventinfosetter.param({'evtNumList': [50]})
126 # Show progress of processing
127 progress = b2.register_module('Progress')
128 
129 main = b2.create_path()
130 # init path
131 main.add_module(eventinfosetter)
132 main.add_module(particlegun)
133 # add simulation for pxd only
134 simulation.add_simulation(main, components=['PXD'], forceSetPXDDataReduction=True, usePXDDataReduction=False)
135 b2.set_module_parameters(main, type="Geometry", useDB=False, components=["PXD"])
136 
137 main.add_module(progress)
138 
139 # Create raw data form simulated digits
140 add_packers(main, components=['PXD'])
141 
142 # Unpack raw data to pack_unpack_collections
143 add_unpackers(main, components=['PXD'])
144 
145 # Change names of collections to avoid conflicts
146 for e in main.modules():
147  if e.name() == 'PXDUnpacker':
148  e.param("PXDRawHitsName", pxd_rawhits_pack_unpack_collection)
149  e.param("PXDRawAdcsName", pxd_rawhits_pack_unpack_collection_adc)
150  e.param("PXDRawROIsName", pxd_rawhits_pack_unpack_collection_roi)
151 
152  if e.name() == 'PXDRawHitSorter':
153  e.param('rawHits', pxd_rawhits_pack_unpack_collection)
154  e.param('digits', pxd_rawhits_pack_unpack_collection_digits)
155 
156 
157 # run custom test module to check if the simulated PXDDigits and the
158 # pxd_digits_pack_unpack_collection collections are equal
159 main.add_module(PxdPackerUnpackerTestModule(rawhits_collection=pxd_rawhits_pack_unpack_collection, digits_collection="PXDDigits"))
160 
161 
162 # run custom test module 2nd time to check if the collection pxd_rawhits_pack_unpack_collection_digits
163 # and the pxd_digits_pack_unpack_collection collections are equal
164 main.add_module(
166  rawhits_collection=pxd_rawhits_pack_unpack_collection,
167  digits_collection=pxd_rawhits_pack_unpack_collection_digits))
168 
169 
170 # Process events
171 b2.process(main)
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56
def __init__(self, rawhits_collection='PXDRawHits', digits_collection="PXDDigits")
int getID(const std::vector< double > &breaks, double t)
get id of the time point t
Definition: calibTools.h:60
def add_simulation(path, components=None, bkgfiles=None, bkgOverlay=True, forceSetPXDDataReduction=False, usePXDDataReduction=True, cleanupPXDDataReduction=True, generate_2nd_cdc_hits=False, simulateT0jitter=True, isCosmics=False, FilterEvents=False, usePXDGatedMode=False, skipExperimentCheckForBG=False)
Definition: simulation.py:135