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