Belle II Software  release-08-01-10
__init__.py
1 
8 
9 import argparse
10 import basf2.utils as b2utils
11 
12 
14  '''
15  Wrapper around the variable manager class.
16  This is necessary for avoiding to import ROOT globally when 'variables' is imported.
17  '''
18 
19  @classmethod
20  def _instance(self):
21  '''
22  Return an instance to the variable manager.
23  '''
24  # Always avoid the top-level 'import ROOT'.
25  import ROOT # noqa
26  instance = ROOT.Belle2.Variable.Manager.Instance()
27  return instance
28 
29  def getVariable(self, *args):
30  '''
31  Wrapper around Manager::getVariable(std::string name) and
32  Manager::getVariable(const std::string& functionName, const std::vector<std::string>& functionArguments).
33  '''
34  instance = PythonVariableManager._instance()
35  return instance.getVariable(*args)
36 
37  def getVariables(self, *args):
38  '''
39  Wrapper around Manager::getVariables(const std::vector<std::string>& variables) and
40  Manager::getVariables().
41  '''
42  instance = PythonVariableManager._instance()
43  return instance.getVariables(*args)
44 
45  def addAlias(self, alias, variable):
46  '''
47  Wrapper around Manager::addAlias(const std::string& alias, const std::string& variable).
48  '''
49  instance = PythonVariableManager._instance()
50  assert(instance.addAlias(alias, variable))
51 
52  def printAliases(self):
53  '''
54  Wrapper around Manager::printAliases().
55  '''
56  instance = PythonVariableManager._instance()
57  instance.printAliases()
58 
59  def clearAliases(self):
60  '''
61  Wrapper around Manager::clearAliases().
62  '''
63  instance = PythonVariableManager._instance()
64  instance.clearAliases()
65 
66  def resolveAlias(self, alias):
67  '''
68  Wrapper around Manager::resolveAlias(const std::string& alias).
69  '''
70  instance = PythonVariableManager._instance()
71  return instance.resolveAlias(alias)
72 
73  def addCollection(self, *args):
74  '''
75  Wrapper around Manager::addCollection(const std::string& collection, const std::vector<std::string>& variables)
76  '''
77  instance = PythonVariableManager._instance()
78  assert(instance.addCollection(*args))
79 
80  def getCollection(self, collection):
81  '''
82  Wrapper around Manager::getCollection(const std::string& collection).
83  '''
84  instance = PythonVariableManager._instance()
85  return instance.getCollection(collection)
86 
87  def resolveCollections(self, *args):
88  '''
89  Wrapper around Manager::resolveCollections(const std::vector<std::string>& variables).
90  '''
91  instance = PythonVariableManager._instance()
92  return instance.resolveCollections(*args)
93 
94  def checkDeprecatedVariable(self, variable):
95  '''
96  Wrapper around Manager::checkDeprecatedVariable(const std::string& name).
97  '''
98  instance = PythonVariableManager._instance()
99  instance.checkDeprecatedVariable(variable)
100 
101  def evaluate(self, variable, particle):
102  '''
103  Wrapper around Manager::evaluate(const std::string& varName, const Particle* p).
104  '''
105  instance = PythonVariableManager._instance()
106  return instance.evaluate(variable, particle)
107 
108  def getNames(self):
109  '''
110  Wrapper around Manager::getNames().
111  '''
112  instance = PythonVariableManager._instance()
113  return instance.getNames()
114 
115  def getAliasNames(self):
116  '''
117  Wrapper around Manager::getAliasNames().
118  '''
119  instance = PythonVariableManager._instance()
120  return instance.getAliasNames()
121 
122  def assertValidName(self, variable):
123  '''
124  Wrapper around Manager::assertValidName(const std::string& name).
125  '''
126  instance = PythonVariableManager._instance()
127  instance.assertValidName(variable)
128 
129 
130 
131 variables = PythonVariableManager()
132 
133 
134 def std_vector(*args):
135  """
136  Creates an std::vector which can be passed to pyROOT
137  """
138  # Always avoid the top-level 'import ROOT'.
139  import ROOT # noqa
140  v = ROOT.std.vector(type(args[0]))()
141  for x in args:
142  v.push_back(x)
143  return v
144 
145 
146 def getCommandLineOptions():
147  """ Parses the command line options and returns the corresponding arguments. """
148  parser = argparse.ArgumentParser()
149  parser.add_argument('--no-pager', dest='pager', default=True, action='store_false',
150  help='Use a pager to show output or print to terminal.')
151  args = parser.parse_args()
152  return args
153 
154 
155 def printVars(changedVariableNames=None, listVars=None):
156  """
157  Print list of all available variables.
158  """
159 
160  if changedVariableNames:
161  print(changedVariableNames)
162  print('Available variables in Variable::Manager:')
163 
164  if listVars is None:
165  listVars = variables.getVariables()
166  else:
167  listVars = [variables.getVariable(name) for name in listVars]
168 
169  dict_VariableDataType = {0: 'double', 1: 'int', 2: 'bool'}
170 
171  vars = []
172  for v in listVars:
173  vars.append((v.group, v.name, v.description,
174  dict_VariableDataType[v.variabletype]))
175 
176  rows = []
177  current_group = ''
178  for (group, name, description, vartype) in sorted(vars):
179  if current_group != group:
180  current_group = group
181  rows.append([group])
182  rows.append([name, description, vartype])
183  b2utils.pretty_print_description_list(rows)
184 
185 
186 def printVariableType(listVars=None):
187 
188  if isinstance(listVars, str):
189  listVars = [listVars]
190 
191  dict_VariableDataType = {0: 'double', 1: 'int', 2: 'bool'}
192 
193  for var_name in listVars:
194  var = variables.getVariable(var_name)
195  print(f"{var_name}: {dict_VariableDataType[var.variabletype]}")
196 
197 
198 def getAllTrgNames():
199  """
200  Return all PSNM trigger bit names
201  """
202  # Always avoid the top-level 'import ROOT'.
203  import ROOT # noqa
204  bits = ROOT.Belle2.PyDBObj('TRGGDLDBFTDLBits')
205  evt = ROOT.Belle2.EventMetaData()
206  ROOT.Belle2.DBStore.Instance().update(evt)
207  size = ROOT.Belle2.TRGSummary.c_ntrgWords * ROOT.Belle2.TRGSummary.c_trgWordSize
208  return [bits.getoutbitname(i) for i in range(size) if bits.getoutbitname(i) != '']
def evaluate(self, variable, particle)
Definition: __init__.py:101
def assertValidName(self, variable)
Definition: __init__.py:122
def addAlias(self, alias, variable)
Definition: __init__.py:45
def resolveAlias(self, alias)
Definition: __init__.py:66
def addCollection(self, *args)
Definition: __init__.py:73
def getCollection(self, collection)
Definition: __init__.py:80
def getVariables(self, *args)
Definition: __init__.py:37
def checkDeprecatedVariable(self, variable)
Definition: __init__.py:94
def getVariable(self, *args)
Definition: __init__.py:29
def resolveCollections(self, *args)
Definition: __init__.py:87