Belle II Software  release-08-01-10
calculation_process.py
1 
8 import basf2.core as _basf2
9 import sys
10 import os
11 
12 from hep_ipython_tools.calculation_process import CalculationProcess
13 from hep_ipython_tools.ipython_handler_basf2.entities import Basf2CalculationQueueStatistics
14 import json
15 
16 
18 
19  """
20  Overload implementation of the HEPProcess with the correct handling of the path calculation.
21  """
22 
23  def __init__(self, result_queue, log_file_name, parameters, path, random_seed=None, max_event=0):
24  """Create a new basf2 calculation process."""
25 
26  self.random_seedrandom_seed = random_seed
27 
28  self.pathpath = path
29 
30  self.max_eventmax_event = max_event
31 
32  super().__init__(result_queue=result_queue, log_file_name=log_file_name,
33  parameters=parameters)
34 
35  def prepare(self):
36  """
37  A function to prepare a path with the modules given in path.
38  """
39  if self.pathpath:
40  # import late due to side effects with importing ROOT
41  from hep_ipython_tools.ipython_handler_basf2 import python_modules
42  # Add the progress python module
43  self.pathpath.add_module(python_modules.ProgressPython(self.progress_queue_remoteprogress_queue_remote))
44  # Add the print collections python module
45  self.pathpath.add_module(python_modules.PrintCollections(self.result_queueresult_queue))
46  else:
47 
48  self.is_validis_validis_valid = False
49 
50  def initialize_output(self):
51  """
52  Make sure all output by python and or C is written to the same output file
53  """
54  # reset stdout/stderr (notebooks forward them over zmq)
55  sys.stdout = sys.__stdout__
56  sys.stderr = sys.__stderr__
57 
58  # open filename
59  logfile = open(self.log_file_namelog_file_name, "wb", 0)
60 
61  # redirect stdout/stderr to logfile
62  os.dup2(logfile.fileno(), sys.stdout.fileno())
63  os.dup2(logfile.fileno(), sys.stderr.fileno())
64 
65  # reset logging to use the new file descriptors
66  _basf2.reset_log()
67  _basf2.logging.zero_counters()
68  _basf2.logging.add_json()
69 
70  def start_process(self):
71  """
72  The function given to the process to start the calculation.
73  Do not call by yourself.
74  Resets the logging system, logs onto console and a file and sets the queues
75  (the result queue and the process queue) correctly.
76  """
77 
78  if not self.pathpath:
79  return
80 
81  try:
82  if self.random_seedrandom_seed is not None:
83  _basf2.set_random_seed(self.random_seedrandom_seed)
84 
85  # setup output capture
86  self.initialize_outputinitialize_output()
87 
88  # and start processing
89  _basf2.process(self.pathpath, self.max_eventmax_event)
90 
91  self.result_queueresult_queue.put("ipython.statistics", Basf2CalculationQueueStatistics(_basf2.statistics))
92 
93  # import ROOT late due to all the side effects
94  from ROOT import Belle2 # noqa
95  store_arrays = list(Belle2.PyStoreArray.list())
96  all_arrays = list(dict(Belle2.DataStore.Instance().getStoreEntryMap(Belle2.DataStore.c_Event)).keys())
97 
98  nodes = dict(
99  name="",
100  children=[
101  dict(
102  index=i,
103  name=store_array,
104  key=store_array,
105  relation=[
106  other_store_array for other_store_array in store_arrays if Belle2.DataStore.relationName(
107  store_array,
108  other_store_array) in all_arrays]) for i,
109  store_array in enumerate(store_arrays)])
110  nodes_json = json.dumps(nodes)
111  self.result_queueresult_queue.put("ipython.dependencies", nodes_json)
112 
113  finally:
114  self.result_queueresult_queue.queue.close()
115 
116  self.progress_queue_remoteprogress_queue_remote.send("end")
117  self.progress_queue_remoteprogress_queue_remote.close()
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
static std::string relationName(const std::string &fromName, const std::string &toName, std::string const &namedRelation="")
Return storage name for a relation between two arrays of the given names.
Definition: DataStore.h:180
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
Definition: PyStoreArray.cc:28
progress_queue_remote
Create the queue for the progress python module.
is_valid
Set to false, if you do not want this process to show up in the process bar calculations.
def __init__(self, result_queue, log_file_name, parameters, path, random_seed=None, max_event=0)
is_valid
Set is_valid to false to not show this process in any listings.