Belle II Software development
variable_modules.py
1
8import numbers
9import pickle
10
11import basf2
12import collections
13from ROOT import Belle2
14
15from tracking.harvest.harvesting import HarvestingModule, coroutine
16
17
18# Helper modules to get the stored information of the SoftwareTriggerModule from the data store after the processing
19# and write them out to a ROOT file for analysis. Uses the tracking harvester for this.
20
21
23 """
24 Overloaded harvester, that stores its data into a pandas data frame instead of a numpy array,
25 because they are more flexible when it comes to changing columns and value types to be stored.
26 """
27 @coroutine
28 def barn(self):
29 """Coroutine that receives the dictionaries of names and values from peel and store them into a pandas df."""
30 crop = (yield)
31 crops = []
32
33 if isinstance(crop, numbers.Number):
34 try:
35 while True:
36 crops.append({"value": crop})
37 # next crop
38 crop = (yield)
39 except GeneratorExit:
40 pass
41
42 elif isinstance(crop, collections.abc.MutableMapping):
43 try:
44 while True:
45 crops.append(crop)
46 crop = (yield)
47 except GeneratorExit:
48 pass
49
50 else:
51 msg = f"Unrecognised crop {crop} of type {type(crop)}"
52 raise ValueError(msg)
53
54
55 self.cropscrops = crops
56
57 def refine(self, crops):
58 """Receive the gathered crops and saves them into a ROOT file."""
59 with open(self.output_file_name, "wb") as f:
60 pickle.dump(crops, f)
61
62
64 """
65 Read in the trigger results stored in the data store and write them out into a ROOT file after all events have
66 passed.
67 """
68
69 def __init__(self, root_file_name="save_results.pkl", store_object_name="SoftwareTriggerResult"):
70 """
71 Create a new module to get the stored trigger decisions from the data store and save them to a root file.
72 :param root_file_name: The file name where to store the results.
73 :param store_object_name: The store object name where the trigger results an be found in the data store.
74 """
75 super().__init__(foreach=store_object_name, output_file_name=root_file_name)
76
77 def peel(self, result):
78 """
79 For each result ( = event), get the list of all trigger decisions and also add the total decision.
80 Write them back into the internal storage ( = pandas.DataFrame) of all events.
81 """
82 return_dict = {identifier: result for identifier, result in result.getResults()}
83
84 # Make it easier for the user to get the total result of everything
85 getFinalTriggerDecision = Belle2.SoftwareTrigger.FinalTriggerDecisionCalculator.getFinalTriggerDecision
86 return_dict["final_decision"] = getFinalTriggerDecision(result)
87 yield return_dict
88
89
91 """
92 Read in the trigger variables stored in the data store and write them out into a ROOT file after all events have
93 passed.
94 """
95
96 def __init__(self, root_file_name="save_vars.pkl", store_object_name="SoftwareTriggerVariables"):
97 """
98 Create a new module to get the stored trigger variables from the data store and save them to a root file.
99 :param root_file_name: The file name where to store the variables.
100 :param store_object_name: The store object name where the trigger variables an be found in the data store.
101 """
102 super().__init__(foreach=store_object_name, output_file_name=root_file_name)
103
104 def peel(self, variables):
105 """
106 For each collection of variables ( = event), get the list of all variables.
107 Write them back into the internal storage ( = pandas.DataFrame) of all events.
108 """
109 yield {identifier: value for identifier, value in variables.get()}
110
111
112if __name__ == "__main__":
113 path = basf2.create_path()
114
115 # Take the output of the __init__.py script
116 path.add_module("SeqRootInput", inputFileName="raw.sroot")
117
118 path.add_module(SummarizeTriggerResults())
119 path.add_module(SummarizeTriggerVariables())
120
121 basf2.process(path)
def __init__(self, root_file_name="save_results.pkl", store_object_name="SoftwareTriggerResult")
def __init__(self, root_file_name="save_vars.pkl", store_object_name="SoftwareTriggerVariables")
output_file_name
Name of the ROOT output file to be generated.
Definition: harvesting.py:199