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