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