Belle II Software  release-05-02-19
python_modules.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import basf2.core as _basf2
5 from ROOT import Belle2
6 
7 from hep_ipython_tools.entities import StoreContentList, StoreContent
8 
9 
10 class PrintCollections(_basf2.Module):
11 
12  """
13  A small module to catch the content of the store array for some events and store them in the queue.
14  """
15 
16  def __init__(self, queue):
17  """
18  Create the module with the given queue.
19  """
20  super().__init__()
21 
22  self.queue = queue
23 
25 
26  self.event_number = 0
27 
29 
30  def initialize(self):
31  """
32  Get the total number of events from C++
33  """
34  self.total_number_of_events = Belle2.Environment.Instance().getNumberOfEvents()
35 
36  def store_content(self):
37  """
38  Store the current content of the store array into the internal list.
39  """
40  registered_store_arrays = Belle2.PyStoreArray.list()
41  registered_store_objects = Belle2.PyStoreObj.list()
42 
43  event_store_content_list = []
44 
45  for store_array_name in registered_store_arrays:
46  store_array = Belle2.PyStoreArray(store_array_name)
47  event_store_content_list.append(StoreContent(store_array_name, len(store_array)))
48 
49  for store_array_name in registered_store_objects:
50  event_store_content_list.append(StoreContent(store_array_name, 0))
51 
52  event_store_content = StoreContentList(content=event_store_content_list, event_number=self.event_number)
53  self.store_content_list.append(event_store_content)
54 
55  def event(self):
56  """
57  Write the store array content into a list for later.
58  """
59  if self.total_number_of_events == 0:
60  if self.event_number % 1000 == 0:
61  self.store_content()
62 
63  else:
64  current_percentage = 1.0 * self.event_number / self.total_number_of_events
65 
66  if 100 * current_percentage % 10 == 0:
67  self.store_content()
68 
69  self.event_number += 1
70 
71  def terminate(self):
72  """
73  Write the store array contents from the events to the queue.
74  """
75  self.queue.put("ipython.store_content", self.store_content_list)
76 
77 
78 class ProgressPython(_basf2.Module):
79 
80  """
81  A small module that prints every now and then the event number to the given connection.
82  It is used for status viewers. Do not call it by yourself.
83  """
84 
85  def __init__(self, queue):
86  """ Init the module """
87  super().__init__()
88 
89  self.queue = queue
90 
91  self.event_number = 0
92 
94  self.queue.send("init")
95 
96  def initialize(self):
97  """ Send start to the connection """
98  self.queue.send("start")
99 
100  # Receive the total number of events
101 
102  self.total_number_of_events = Belle2.Environment.Instance().getNumberOfEvents()
103 
104  def event(self):
105  """ Send the event number to the connection """
106  if self.total_number_of_events == 0:
107  return
108 
109  current_percentage = 1.0 * self.event_number / self.total_number_of_events
110 
111  if 100 * current_percentage % 5 == 0:
112  self.queue.send(current_percentage)
113 
114  self.event_number += 1
115 
116  def terminate(self):
117  """ Send stop to the connection """
118  self.queue.send(1)
119  self.queue.send("end")
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.__init__
def __init__(self, queue)
Definition: python_modules.py:16
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.event
def event(self)
Definition: python_modules.py:104
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections
Definition: python_modules.py:10
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.queue
queue
The queue to handle.
Definition: python_modules.py:22
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython
Definition: python_modules.py:78
basf2.core
Definition: core.py:1
Belle2::PyStoreObj::list
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available objects for given durability.
Definition: PyStoreObj.cc:21
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.entities
Definition: entities.py:1
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.event
def event(self)
Definition: python_modules.py:55
hep_ipython_tools.entities.StoreContentList
Definition: entities.py:20
hep_ipython_tools.entities.StoreContent
Definition: entities.py:1
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.event_number
event_number
The current event number.
Definition: python_modules.py:26
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.event_number
event_number
The current event number.
Definition: python_modules.py:91
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.store_content
def store_content(self)
Definition: python_modules.py:36
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.total_number_of_events
total_number_of_events
The total number of events.
Definition: python_modules.py:28
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.total_number_of_events
total_number_of_events
The total number of events.
Definition: python_modules.py:93
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.terminate
def terminate(self)
Definition: python_modules.py:116
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.terminate
def terminate(self)
Definition: python_modules.py:71
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.store_content_list
store_content_list
The contents of the store arrays for some events.
Definition: python_modules.py:24
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.queue
queue
The queue to handle.
Definition: python_modules.py:89
Belle2::Environment::Instance
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:31
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.initialize
def initialize(self)
Definition: python_modules.py:96
hep_ipython_tools.ipython_handler_basf2.python_modules.ProgressPython.__init__
def __init__(self, queue)
Definition: python_modules.py:85
hep_ipython_tools.ipython_handler_basf2.python_modules.PrintCollections.initialize
def initialize(self)
Definition: python_modules.py:30