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