Belle II Software  release-08-01-10
variable_modules.py
1 
8 import numbers
9 import pickle
10 
11 import basf2
12 import collections
13 from ROOT import Belle2
14 
15 from 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 = "Unrecognised crop {} of type {}".format(
52  crop,
53  type(crop)
54  )
55  raise ValueError(msg)
56 
57 
58  self.cropscropscrops = crops
59 
60  def refine(self, crops):
61  """Receive the gathered crops and saves them into a ROOT file."""
62  with open(self.output_file_nameoutput_file_name, "wb") as f:
63  pickle.dump(crops, f)
64 
65 
67  """
68  Read in the trigger results stored in the data store and write them out into a ROOT file after all events have
69  passed.
70  """
71 
72  def __init__(self, root_file_name="save_results.pkl", store_object_name="SoftwareTriggerResult"):
73  """
74  Create a new module to get the stored trigger decisions from the data store and save them to a root file.
75  :param root_file_name: The file name where to store the results.
76  :param store_object_name: The store object name where the trigger results an be found in the data store.
77  """
78  super().__init__(foreach=store_object_name, output_file_name=root_file_name)
79 
80  def peel(self, result):
81  """
82  For each result ( = event), get the list of all trigger decisions and also add the total decision.
83  Write them back into the internal storage ( = pandas.DataFrame) of all events.
84  """
85  return_dict = {identifier: result for identifier, result in result.getResults()}
86 
87  # Make it easier for the user to get the total result of everything
88  getFinalTriggerDecision = Belle2.SoftwareTrigger.FinalTriggerDecisionCalculator.getFinalTriggerDecision
89  return_dict["final_decision"] = getFinalTriggerDecision(result)
90  yield return_dict
91 
92 
94  """
95  Read in the trigger variables stored in the data store and write them out into a ROOT file after all events have
96  passed.
97  """
98 
99  def __init__(self, root_file_name="save_vars.pkl", store_object_name="SoftwareTriggerVariables"):
100  """
101  Create a new module to get the stored trigger variables from the data store and save them to a root file.
102  :param root_file_name: The file name where to store the variables.
103  :param store_object_name: The store object name where the trigger variables an be found in the data store.
104  """
105  super().__init__(foreach=store_object_name, output_file_name=root_file_name)
106 
107  def peel(self, variables):
108  """
109  For each collection of variables ( = event), get the list of all variables.
110  Write them back into the internal storage ( = pandas.DataFrame) of all events.
111  """
112  yield {identifier: value for identifier, value in variables.get()}
113 
114 
115 if __name__ == "__main__":
116  path = basf2.create_path()
117 
118  # Take the output of the __init__.py script
119  path.add_module("SeqRootInput", inputFileName="raw.sroot")
120 
121  path.add_module(SummarizeTriggerResults())
122  path.add_module(SummarizeTriggerVariables())
123 
124  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:201