Belle II Software development
fill_array_example.py
1#!/usr/bin/env python3
2
3
10
11
15
16import basf2 # noqa: needed to load pythonizations
17from ROOT.Belle2 import PyStoreArray, DataStore
18import numpy as np
19
20
21def create_digits():
22 # number of events and digits for each event
23 n_events = 10
24 ndigits = 10
25
26 digits = []
27 for _ in range(n_events):
28 # generate some digits for the PXD sensor
29 sensor_ids = np.linspace(0, 40, ndigits, dtype=int)
30 ucell_ids = np.linspace(0, 250, ndigits, dtype=int)
31 vcell_ids = np.linspace(0, 756, ndigits, dtype=int)
32 charges = np.linspace(0, 255, ndigits, dtype=int)
33 digits.append({"uCellID": ucell_ids, "vCellID": vcell_ids, "charge": charges, "sensorID": sensor_ids})
34
35 return digits
36
37
38class InjectPXDDigits(basf2.Module):
39 """
40 Simple basf2 module to fill a PyStoreArray with PXD digits.
41 Takes as arguments the digits to fill the PyStoreArray with,
42 and then for each event it calls the fillArray function.
43 """
44
45 def __init__(self, digits_arrays):
46 '''
47 Initialization
48 --------------
49 digits_array: array of digits with lenght equal to the number of events
50 '''
51
52 self.digits_arrays = digits_arrays
53 super().__init__()
54
55 def initialize(self):
56 '''
57 Initialization of the PyStoreArray and registration in the DataStore
58 '''
59
60 self.pxddigits = PyStoreArray("PXDDigits", DataStore.c_Event)
61 self.pxddigits.registerInDataStore("PXDDigits")
62
63 self.it_digits = iter(self.digits_arrays)
64
65 def event(self):
66 '''
67 For each event take the corresponding entry of digits_array and write it
68 into the PyStoreArray
69 '''
70 digits = next(self.it_digits)
71 keys = ["sensorID", "uCellID", "vCellID", "charge"]
72 sensorID, uCellID, vCellID, charge = [
73 # it's important we ensure the correct type here (unsigned short)
74 digits[key].astype(np.ushort) for key in keys]
75
76 # call the function fillArray to fill the PyStoreArray
77 # Note: this function appends the new digits to the array if it is non empty
78 self.pxddigits.fillArray(uCellID=uCellID, vCellID=vCellID, sensorID=sensorID, charge=charge)
79
80
81digits = create_digits()
82path = basf2.create_path()
83path.add_module("EventInfoSetter", evtNumList=len(digits))
84path.add_module("Geometry")
85path.add_module(InjectPXDDigits(digits))
86path.add_module("RootOutput", branchNames=["PXDDigits"], outputFileName="digits_test.root")
87path.add_module("Progress")
88
89basf2.process(path)
90basf2.statistics
it_digits
Iterator of the digits array.