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