Belle II Software  release-08-01-10
db_access.py
1 
8 
9 
10 def list_to_vector(lst):
11  """
12  Helper function to convert a python list into a std::vector of the same type.
13  Is not a very general and good method, but works for the different use cases in
14  the STM.
15  :param lst: The list to convert
16  :return: A std::vector with the same content as the input list.
17  """
18  from ROOT import std
19  type_of_first_element = type(lst[0]).__name__
20  if type_of_first_element == "str":
21  type_of_first_element = "string"
22 
23  vec = std.vector(type_of_first_element)()
24 
25  for x in lst:
26  vec.push_back(x)
27 
28  return vec
29 
30 
31 def upload_cut_to_db(
32  cut_string,
33  base_identifier,
34  cut_identifier,
35  prescale_factor=1,
36  reject_cut=False,
37  iov=None):
38  """
39  Python function to upload the given software trigger cut to the database.
40  Additional to the software trigger cut, the base- as well as the cut identifier
41  have to be given. Optionally, the interval of validity can be defined
42  (default is always valid).
43  """
44  from ROOT import Belle2
45 
46  if not iov:
47  iov = Belle2.IntervalOfValidity(0, 0, -1, -1)
48 
49  if isinstance(prescale_factor, list):
50  raise AttributeError(
51  "The only allowed type for the prescaling is a single factor")
52 
55  cut_string, prescale_factor, reject_cut)
56 
57  db_handler.upload(
58  software_trigger_cut,
59  base_identifier,
60  cut_identifier,
61  iov)
62 
63 
64 def upload_trigger_menu_to_db(
65  base_identifier,
66  cut_identifiers,
67  accept_mode=False,
68  iov=None):
69  """
70  Python function to upload the given software trigger enu to the database.
71  Additional to the software trigger menu, the base identifier
72  has to be given. Optionally, the interval of validity can be defined
73  (default is always valid).
74  """
75  from ROOT import Belle2
76 
77  if not iov:
78  iov = Belle2.IntervalOfValidity(0, 0, -1, -1)
79 
80  cut_identifiers = list_to_vector(cut_identifiers)
81 
83  db_handler.uploadTriggerMenu(
84  base_identifier,
85  cut_identifiers,
86  accept_mode,
87  iov)
88 
89 
90 def download_cut_from_db(base_name, cut_name, do_set_event_number=True):
91  """
92  Python function to download a cut from the database. As each cut is uniquely identified by a
93  base and a cut name, you have to give in both here.
94  Please remember that the database access depends on the current event number. If you do not call
95  this function in a basf2 module environment, you can use the set_event_number function in this
96  python file to set the event number correctly nevertheless.
97  :param base_name: the base name of the cut
98  :param cut_name: the specific name of the cut
99  :param do_set_event_number: it is important to always have a proper event number set for the database to work.
100  This is why this functions sets the event number in all cases to (1, 0, 0). If you want to prevent this
101  (because you maybe want to use another event number), set this flag to False.
102  :return: the downloaded cut or None, if the name can not be found.
103  """
104  from ROOT import Belle2
105 
106  if do_set_event_number:
107  set_event_number(1, 0, 0)
108 
110  result = db_handler.download(base_name, cut_name)
111  if not result:
112  result = None
113  return result
114 
115 
116 def download_trigger_menu_from_db(base_name, do_set_event_number=True):
117  """
118  Python function to download a trigger menu from the database. As each trigger menu is uniquely identified by a
119  base name, you have to give here.
120  Please remember that the database access depends on the current event number. If you do not call
121  this function in a basf2 module environment, you can use the set_event_number function in this
122  python file to set the event number correctly nevertheless.
123  :param base_name: the base name of the menu
124  :param do_set_event_number: it is important to always have a proper event number set for the database to work.
125  This is why this functions sets the event number in all cases to (1, 0, 0). If you want to prevent this
126  (because you maybe want to use another event number), set this flag to False.
127  :return: the downloaded cut or None, if the name can not be found.
128  """
129  from ROOT import Belle2
130 
131  if do_set_event_number:
132  set_event_number(1, 0, 0)
133 
135  result = db_handler.downloadTriggerMenu(base_name)
136  if not result:
137  result = None
138  return result
139 
140 
141 def set_event_number(evt_number, run_number, exp_number):
142  """
143  Helper function to set the event number although we are not in a typical
144  "module-path" setup. This is done by initializing the database,
145  creating an EventMetaData and string the requested numbers in it.
146  """
147  from ROOT import Belle2
148 
149  event_meta_data_pointer = Belle2.PyStoreObj(
150  Belle2.EventMetaData.Class(), "EventMetaData")
151 
152  Belle2.DataStore.Instance().setInitializeActive(True)
153 
154  event_meta_data_pointer.registerInDataStore()
155  event_meta_data_pointer.create(False)
156  event_meta_data_pointer.setEvent(evt_number)
157  event_meta_data_pointer.setRun(run_number)
158  event_meta_data_pointer.setExperiment(exp_number)
159 
160  Belle2.DataStore.Instance().setInitializeActive(False)
161 
162 
163 def get_all_cuts():
164  for base_identifier in ["filter", "skim"]:
165  menu = download_trigger_menu_from_db(
166  base_name=base_identifier, do_set_event_number=False)
167  if not menu:
168  continue
169  cuts = [str(cut) for cut in menu.getCutIdentifiers()]
170  for cut_identifier in cuts:
171  cut = download_cut_from_db(
172  base_name=base_identifier,
173  cut_name=cut_identifier,
174  do_set_event_number=False)
175  yield {
176  "Base Identifier": base_identifier,
177  "Reject Menu": menu.isAcceptMode(),
178  "Cut Identifier": cut_identifier,
179  "Cut Condition": cut.decompile(),
180  "Cut Prescaling": cut.getPreScaleFactor(),
181  "Reject Cut": cut.isRejectCut()
182  }
183 
184 
185 if __name__ == "__main__":
186  print("This tool is now replaced by 'b2hlt_triggers print'.")
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
A class that describes the interval of experiments/runs for which an object in the database is valid.
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
static std::unique_ptr< SoftwareTriggerCut > compile(const std::string &cut_string, const unsigned int prescaleFactor, const bool rejectCut=false)
Compile a new SoftwareTriggerCut from a cut string (by using the GeneralCut::compile function) and an...
Helper class for performing up- and downloads of SoftwareTriggerCuts from the database.