Belle II Software  release-08-01-10
pxd_roi_payload.py
1 #!/usr/bin/env python3
2 
3 
10 
11 
12 import basf2 as b2
13 from ROOT import Belle2
14 
15 import simulation
16 
17 import PXDROIUnpackerModule
18 
19 b2.set_random_seed(42)
20 
21 
22 class PxdROIPayloadTestModule(b2.Module):
23 
24  """
25  module which checks if the roy payload from HLT can be created and depacked correctly
26  """
27 
28  def sortROIs(self, unsortedPyStoreArray):
29  """ sort ROI list
30  Returns a python-list containing the ROIs
31  """
32 
33  # first convert to a python-list to be able to sort
34  py_list = list(unsortedPyStoreArray)
35 
36  # sort via a hierachy of sort keys
37  return sorted(py_list,
38  key=lambda x: (
39  x.getSensorID(),
40  x.getMinUid(),
41  x.getMaxUid(),
42  x.getMinVid(),
43  x.getMaxVid()))
44 
45  def event(self):
46  """ load the PXD Digits of the simulation and the packed/unpacked ones
47  and compare them"""
48 
49  orgroisuns = Belle2.PyStoreArray('ROIs')
50  if not orgroisuns:
51  b2.B2FATAL("ROIs not in file")
52  return
53 
54  unpackedroisuns = Belle2.PyStoreArray('PXDROIsPayHLT')
55  if not unpackedroisuns:
56  b2.B2FATAL("PXDROIsPayHLT not in file")
57  return
58 
59  # To make a 1:1 comparison, we have to sort both arrays.
60  # As the order of the payload differs from the original array.
61  # (its sorted by DHHID)
62  # We have to sort both, because sorting by coordinate is not defined.
63 
64  orgrois = self.sortROIssortROIs(orgroisuns)
65  unpackedrois = self.sortROIssortROIs(unpackedroisuns)
66 
67  # For some unknown reason, the ROI array contains a lot of
68  # doubles. For creating the payload, these have been removed. to make a 1:1
69  # comparison, we have to skip the following check and lateron skip ROIs
70  # which are identical to the one before (ordered array).
71 
72  # if not len(orgrois) == len(unpackedrois):
73  # B2FATAL("Org. ROIs and Unpacked ROIs count not equal after packing and unpacking")
74 
75  print(f"Comparing {len(orgrois)} ROIs ")
76 
77  def f(x):
78  return (
79  x.getSensorID(),
80  x.getMinUid(),
81  x.getMaxUid(),
82  x.getMinVid(),
83  x.getMaxVid())
84 
85  # check all quantities between the direct and the packed/unpacked pxd digits
86  # for i in range(len(orgrois)):
87  # org = orgrois[i]
88  # if i == 0 or f(org) != f(orgrois[i - 1]):
89  # B2INFO(" Org $%X %3d %3d %3d %3d" % (org.getSensorID().getID(), org.getMinUid(),
90  # org.getMaxUid(), org.getMinVid(), org.getMaxVid()))
91 
92  # for i in range(len(unpackedrois)):
93  # unp = unpackedrois[i]
94  # B2INFO(" Unp $%X %3d %3d %3d %3d" % (unp.getSensorID().getID(),
95  # unp.getMinUid(), unp.getMaxUid(), unp.getMinVid(), unp.getMaxVid()))
96 
97  j = 0
98  for i in range(len(orgrois)):
99  org = orgrois[i]
100  if i != 0 and f(org) == f(orgrois[i - 1]):
101  b2.B2WARNING("Found the same ROI a second time (Double ROI)!")
102  b2.B2WARNING(
103  f"Check ${org.getSensorID().getID():X} {int(org.getMinUid()):3d} {int(org.getMaxUid()):3d} " +
104  f"{int(org.getMinVid()):3d} {int(org.getMaxVid()):3d}")
105  if i == 0 or f(org) != f(orgrois[i - 1]):
106  if j == len(unpackedrois):
107  b2.B2FATAL("Unpacked ROIs comparison exceeds array limit!")
108  break
109 
110  unp = unpackedrois[j]
111 
112  b2.B2INFO(
113  f"Check Org ${org.getSensorID().getID():X} {org.getMinUid():3d} {org.getMaxUid():3d} " +
114  f"{org.getMinVid():3d} {org.getMaxVid():3d} Unp ${unp.getSensorID().getID():X} {unp.getMinUid():3d} " +
115  f"{unp.getMaxUid():3d} {unp.getMinVid():3d} {unp.getMaxVid():3d}")
116  # compare all available quantities
117  if unp.getMinUid() == 0 and unp.getMinVid() == 0 and unp.getMaxUid() == 250 - 1 and unp.getMaxVid() == 768 - 1:
118  b2.B2INFO("Full size ROI")
119  if org.getSensorID().getID() != unp.getSensorID().getID():
120  b2.B2INFO("DHHID changed")
121  if j == len(unpackedrois):
122  b2.B2FATAL("Unpacked ROIs comparison exceeds array limit!")
123  break
124  j += 1
125  unp = unpackedrois[j]
126 
127  if not(unp.getMinUid() == 0 and unp.getMinVid() == 0 and unp.getMaxUid() == 250 - 1 and unp.getMaxVid() == 768 - 1):
128  assert org.getSensorID().getID() == unp.getSensorID().getID()
129  assert org.getMinUid() == unp.getMinUid()
130  assert org.getMaxUid() == unp.getMaxUid()
131  assert org.getMinVid() == unp.getMinVid()
132  assert org.getMaxVid() == unp.getMaxVid()
133  j += 1
134 
135 
136 # to run the framework the used modules need to be registered
137 particlegun = b2.register_module('ParticleGun')
138 particlegun.param('pdgCodes', [13, -13])
139 particlegun.param('nTracks', 40)
140 
141 # Create Event information
142 eventinfosetter = b2.register_module('EventInfoSetter')
143 eventinfosetter.param({'evtNumList': [10]})
144 
145 main = b2.create_path()
146 # init path
147 main.add_module(eventinfosetter)
148 main.add_module(particlegun)
149 # add simulation for pxd only
150 # turn off the cleanup as the storearrays are needed
151 simulation.add_simulation(main, components=['PXD', 'SVD'], forceSetPXDDataReduction=True,
152  usePXDDataReduction=True, cleanupPXDDataReduction=False)
153 b2.set_module_parameters(main, type="Geometry", useDB=False, components=['PXD', 'SVD', 'MagneticFieldConstant4LimitedRSVD'])
154 
155 roiPayloadAssembler = b2.register_module('ROIPayloadAssembler')
156 roiPayloadAssembler.param({"ROIListName": "ROIs", "SendAllDownscaler": 0,
157  "SendROIsDownscaler": 0, "CutNrROIs": 5, "AcceptAll": True})
158 
159 main.add_module(roiPayloadAssembler)
160 
161 # Show progress of processing
162 main.add_module('Progress')
163 
165 
166 # run custom test module to check ROI befor and after packing/unpacking
167 main.add_module(PxdROIPayloadTestModule())
168 
169 # Process events
170 b2.process(main)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def sortROIs(self, unsortedPyStoreArray)
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, save_slow_pions_in_mc=False)
Definition: simulation.py:121