CMS 3D CMS Logo

branchselection.py
Go to the documentation of this file.
1 import re
2 try:
3  Pattern = re._pattern_type
4 except AttributeError:
5  # Python 3.7
6  Pattern = re.Pattern
7 
8 
10  def __init__(self, branchsel):
11  comment = re.compile(r"#.*")
12  ops = []
13 
14  if isinstance(branchsel, list):
15  # branchsel is a list of commands
16  lines = branchsel
17  elif isinstance(branchsel, str):
18  # branchsel is a filename
19  lines=[]
20  for line in open(branchsel, 'r'):
21  line = line.strip()
22  if len(line) == 0 or line[0] == '#':
23  continue
24  line = re.sub(comment, "", line)
25  while line[-1] == "\\":
26  line = line[:-1] + " " + file.next().strip()
27  line = re.sub(comment, "", line)
28  lines.append(line)
29 
30  for line in lines:
31  try:
32  (op, sel) = line.split()
33  if op == "keep":
34  ops.append((sel, 1))
35  elif op == "drop":
36  ops.append((sel, 0))
37  elif op == "keepmatch":
38  ops.append((re.compile("(:?%s)$" % sel), 1))
39  elif op == "dropmatch":
40  ops.append((re.compile("(:?%s)$" % sel), 0))
41  else:
42  print("Error in branchsel: line '%s': "% (line)
43  + "it's not (keep|keepmatch|drop|dropmatch) "
44  + "<branch_pattern>"
45  )
46  except ValueError as e:
47  print("Error in branchsel: line '%s': " % (line)
48  + "it's not (keep|keepmatch|drop|dropmatch) "
49  + "<branch_pattern>"
50  )
51  self._ops = ops
52 
53  def selectBranches(self, tree):
54  tree.SetBranchStatus("*", 1)
55  branchNames = [b.GetName() for b in tree.GetListOfBranches()]
56  for bre, stat in self._ops:
57  if type(bre) == Pattern:
58  for n in branchNames:
59  if re.match(bre, n):
60  tree.SetBranchStatus(n, stat)
61  else:
62  tree.SetBranchStatus(bre, stat)
def __init__(self, branchsel)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47