Belle II Software  release-08-01-10
cdc_packer_unpacker.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import basf2 as b2
13 from ROOT import Belle2
14 
15 import simulation
16 
17 cdc_hits = "CDCHits"
18 cdc_hits_pack_unpack_collection = "CDCHits_test_output"
19 b2.set_random_seed(42)
20 
21 
22 class PackerUnpackerTestModule(b2.Module):
23 
24  """
25  module which checks if two collection of CDCHits are equal
26  """
27 
28  def sortHits(self, unsortedPyStoreArray):
29  """ use a some information to sort the CDCHits list
30  Returns a python-list containing the CDCHits
31  """
32 
33  # first convert to a python-list to be able to sort
34  py_list = [x for x in unsortedPyStoreArray]
35 
36  # sort via a hierachy of sort keys
37  return sorted(
38  py_list,
39  key=lambda x: (
40  x.getILayer(),
41  x.getISuperLayer(),
42  x.getIWire()))
43 
44  def event(self):
45  """
46  event function
47  """
48 
49  # load the cdcHits and the collection which results from the packer and unpacker
50  # processed by packer and unpacker
51  cdcHitsPackedUnpacked_unsorted = Belle2.PyStoreArray(cdc_hits_pack_unpack_collection)
52  # direct from simulation
53  cdcHits_unsorted = Belle2.PyStoreArray(cdc_hits)
54 
55  # sort the hits, because they have been shuffled in the packing/unpacking process
56  cdcHitsPackedUnpacked = self.sortHitssortHits(cdcHitsPackedUnpacked_unsorted)
57  cdcHits = self.sortHitssortHits(cdcHits_unsorted)
58 
59  if not len(cdcHits) == len(cdcHitsPackedUnpacked):
60  b2.B2FATAL("CDC Hit count not equal after packing and unpacking")
61 
62  # check all quantities between the direct and the packed/unpacked CDC hit
63  for i in range(len(cdcHits)):
64  hit = cdcHits[i]
65  hitPackedUnpacked = cdcHitsPackedUnpacked[i]
66 
67  assert hit.getILayer() == hitPackedUnpacked.getILayer()
68  assert hit.getISuperLayer() == hitPackedUnpacked.getISuperLayer()
69  assert hit.getIWire() == hitPackedUnpacked.getIWire()
70  assert hit.getID() == hitPackedUnpacked.getID()
71  assert hit.getTDCCount() == hitPackedUnpacked.getTDCCount()
72  assert hit.getADCCount() == hitPackedUnpacked.getADCCount()
73  assert hit.getStatus() == hitPackedUnpacked.getStatus()
74 
75 
76 main = b2.create_path()
77 # Create Event information
78 eventinfosetter = b2.register_module('EventInfoSetter')
79 eventinfosetter.param({'evtNumList': [10]})
80 main.add_module(eventinfosetter)
81 
82 # to run the framework the used modules need to be registered
83 particlegun = b2.register_module('ParticleGun')
84 particlegun.param('pdgCodes', [13, -13])
85 particlegun.param('nTracks', 10)
86 main.add_module(particlegun)
87 
88 # add simulation for CDC only
89 simulation.add_simulation(main, components=['CDC'])
90 b2.set_module_parameters(main, type="Geometry", useDB=False, components=["CDC"])
91 
92 # add the packer which packs the CDCHits resulting from the simulation
93 cdc_packer = b2.register_module('CDCPacker')
94 cdc_packer.param('cdcHitName', "CDCHits")
95 main.add_module(cdc_packer)
96 
97 # add the unpacker which unpacks the RawCDC hits and stores
98 # them in the dedicated store array "cdcHit_test_output"
99 cdc_unpacker = b2.register_module('CDCUnpacker')
100 cdc_unpacker.param('cdcHitName', cdc_hits_pack_unpack_collection)
101 cdc_unpacker.param('enablePrintOut', False)
102 cdc_unpacker.param('pedestalSubtraction', False)
103 main.add_module(cdc_unpacker)
104 
105 # run custom test module to check if the CDCHits and the
106 # cdcHit_test_output collections are equal
107 main.add_module(PackerUnpackerTestModule())
108 
109 # Process events
110 b2.process(main)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def sortHits(self, unsortedPyStoreArray)
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