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