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