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