Belle II Software  release-08-01-10
descriptcheck.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # --------------------------------------
13 # Class for comparing decfile descriptors
14 # --------------------------------------
15 
16 import settings
17 from colours import warning
18 
19 partdict = {}
20 mesgdict = []
21 tag = False
22 for line in open(settings.partdictpath):
23  if not tag:
24  if '<DICTIONARY>' in line:
25  tag = True
26  continue
27  if tag:
28  partdict[line.split()[1]] = line.split()[0]
29 
30 
31 # --------------------------------------
32 # Functions for comparing descriptors
33 # --------------------------------------
34 
35 def inlist(partA, listA):
36  if isinstance(listA, list):
37  for part in listA:
38  if partA == part:
39  return True
40  if isinstance(part, list):
41  if inlist(partA, part):
42  return True
43  else:
44  return partA == listA
45 
46 
47 def convertToList(stringA):
48  stringA = stringA.replace('(', ' ( ')
49  stringA = stringA.replace(')', ' ) ')
50  stringA = stringA.replace(']', ' ] ')
51  stringA = stringA.replace('[', ' [ ')
52  stringA = stringA.replace('}', ' } ')
53  stringA = stringA.replace('{', ' { ')
54  list_base = stringA.split()
55  list_base = cleanList('{}', list_base)
56  list_base = cleanList('[]', list_base)
57  list_base = cleanList('()', list_base)
58  while len(list_base) == 1:
59  list_base = list_base[0]
60  list_base = postProcess(list_base)
61  return list_base
62 
63 
64 def postProcess(list_base):
65  if not isinstance(list_base, list):
66  warning('Error building descriptor, not a list')
67  i = 0
68  while i < len(list_base):
69  if isinstance(list_base[i], list):
70  if len(list_base[i]) == 1:
71  if i > 0:
72  if isinstance(list_base[i - 1], str):
73  if list_base[i][0] == 'os' or list_base[i][0] == 'nos':
74  list_base[i - 1] = list_base[i - 1] + '[' \
75  + list_base[i][0] + ']'
76  list_base.pop(i)
77  i -= 1
78  else:
79  list_base[i - 1] = list_base[i - 1] + '(' \
80  + list_base[i][0] + ')'
81  list_base.pop(i)
82  i -= 1
83  else:
84  list_base[i] = list_base[i][0]
85  warning("You have a lone particle enclosed in '()' or some such stuff: " + list_base[i])
86  else:
87  list_base[i] = '(' + list_base[i][0] + ')'
88  else:
89  list_base[i] = postProcess(list_base[i])
90  i += 1
91  return list_base
92 
93 
94 def cleanList(stringA, list_base):
95  tag1 = stringA[0]
96  tag2 = stringA[1]
97 
98  while inlist(tag1, list_base):
99  start_index = 0
100  for i in range(len(list_base)):
101  if isinstance(list_base[i], list):
102  list_base[i] = cleanList(stringA, list_base[i])
103  if tag1 == list_base[i]:
104  start_index = i
105  if tag2 == list_base[i]:
106  newlist = list_base[start_index + 1:i]
107  list_base = list_base[:start_index] + [newlist] + list_base[i + 1:]
108  break
109  return list_base
110 
111 
112 # converts the particle to the appropriate pdg id
113 
114 def convertPart(partA):
115  if partA in partdict:
116  return partdict[partA]
117  else:
118  if partA not in [partdict[x] for x in partdict] and not partA.lower() == 'cc' and not partA == '->':
119  # mesgdict+=["Particle "+partA+" not recognised!"]
120  pass
121  return partA
122 
123 
124 def compareList(listA, listB):
125  while not (listA == [] and listB == []):
126  toBreak = False
127  for partA in listA:
128  if toBreak:
129  break
130  for partB in listB:
131  if comparePart(partA, partB):
132  listA.remove(partA)
133  listB.remove(partB)
134  toBreak = True
135  break
136  # if this ever processes then the for loops went through without matching a particle and lists are not empty
137  if toBreak:
138  continue
139  return False
140  return True
141 
142 
143 def comparePart(partA, partB):
144  if isinstance(partA, list) and isinstance(partB, list):
145  return compareList(partA, partB)
146  elif not isinstance(partA, list) and not isinstance(partB, list):
147  if convertPart(partA) == convertPart(partB):
148  return True
149  return False