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