ATLAS Offline Software
Public Member Functions | Public Attributes | Private Attributes | List of all members
python.MenuXML2JSONConverter.XML2JsonConverter Class Reference
Inheritance diagram for python.MenuXML2JSONConverter.XML2JsonConverter:
Collaboration diagram for python.MenuXML2JSONConverter.XML2JsonConverter:

Public Member Functions

def __init__ (self, tabsize=4, **args)
 
def convertFile (self, inputfilename, jsonfilename=None)
 
def parseXMLFile (self, filename)
 
def loadRules (self, docname)
 
def renameKey (self, key)
 
def createJsonStruct (self, node)
 
def writeJsonFile (self, filename=None)
 
def checkJsonConformity (self, filename)
 

Public Attributes

 tabsize
 
 xmlfilename
 
 doc
 
 root
 
 rules
 

Private Attributes

 __doc
 
 __root
 
 __rules
 

Detailed Description

Definition at line 6 of file MenuXML2JSONConverter.py.

Constructor & Destructor Documentation

◆ __init__()

def python.MenuXML2JSONConverter.XML2JsonConverter.__init__ (   self,
  tabsize = 4,
**  args 
)

Definition at line 7 of file MenuXML2JSONConverter.py.

7  def __init__(self, tabsize = 4, **args):
8  self.tabsize = tabsize
9 
10  self.__doc = None
11  self.__root = None
12  self.__rules = {}
13  self.xmlfilename = None
14 
15 

Member Function Documentation

◆ checkJsonConformity()

def python.MenuXML2JSONConverter.XML2JsonConverter.checkJsonConformity (   self,
  filename 
)

Definition at line 203 of file MenuXML2JSONConverter.py.

203  def checkJsonConformity(self, filename):
204  print("Checking json conformity", end=' ')
205  import json
206  FH = open(filename, "rb")
207  try:
208  json.load(FH)
209  print(" ... OK")
210  except ValueError as e:
211  print(" ... FAILED")
212  print(e)
213  FH.close()
214 
215 
216 

◆ convertFile()

def python.MenuXML2JSONConverter.XML2JsonConverter.convertFile (   self,
  inputfilename,
  jsonfilename = None 
)

Definition at line 16 of file MenuXML2JSONConverter.py.

16  def convertFile(self, inputfilename, jsonfilename = None ):
17  if not inputfilename.endswith(".xml"):
18  print("judging by the suffix, %s is not an xml file" % inputfilename)
19  return 1
20  self.parseXMLFile( inputfilename )
21  self.writeJsonFile( jsonfilename )
22 

◆ createJsonStruct()

def python.MenuXML2JSONConverter.XML2JsonConverter.createJsonStruct (   self,
  node 
)

Definition at line 146 of file MenuXML2JSONConverter.py.

146  def createJsonStruct(self, node):
147  if node.tag in self.rules['asArray']:
148  return [ {element.tag : self.createJsonStruct(element)} for element in node ]
149 
150  else:
151  from collections import OrderedDict
152  d = OrderedDict()
153  # writing attributs of the element to json structure in alphabetical order
154  # turn values into int according to the asInt rule
155  for attr in sorted(node.attrib.keys()):
156  if (node.tag, attr) in self.rules['ignoreAttrib']: # attributes to ignore
157  continue
158  if node.tag in self.rules['asInt'] and attr in self.rules['asInt'][node.tag]:
159  d[self.renameKey(attr)] = int(node.attrib[attr])
160  else:
161  d[self.renameKey(attr)] = node.attrib[attr]
162  # looping over children
163  for element in node:
164  if element.tag in self.rules['ignoreElement']:
165  continue
166  if (node.tag, element.tag) in self.rules['asMultiArray']:
167  # list of multiple identical elements (e.g. CHAINs)
168  key = self.renameKey(element.tag)
169  if key not in d: d[key] = list()
170  d[key] += [ self.createJsonStruct(element) ]
171  else:
172  # single element (e.g. CHAIN_LIST)
173  if element.tag in d:
174  raise RuntimeError("element %s already attached to node %s and not declared as MultiArray" % (element.tag, node.tag))
175 
176  if element.tag in self.rules['promote']:
177  obj = self.createJsonStruct(element)
178  promoted = self.renameKey( self.rules['promote'][element.tag]) #e.g. Chains
179  d[promoted] = obj[promoted]
180  else:
181  d[self.renameKey(element.tag)] = self.createJsonStruct(element)
182 
183  return d
184 
185 
186 

◆ loadRules()

def python.MenuXML2JSONConverter.XML2JsonConverter.loadRules (   self,
  docname 
)

Definition at line 31 of file MenuXML2JSONConverter.py.

31  def loadRules(self,docname):
32  self.rules = { 'asInt' : {},
33  'asArray' : [],
34  'asMultiArray' : [],
35  'ignoreElement' : [],
36  'ignoreAttrib' : [],
37  'promote' : {},
38  'rename' : {} # renaming will be applied last
39  }
40 
41  if docname == "LVL1Config":
42  self.rules['asInt'] = {
43  "TriggerItem" : ["partition", "complex_deadtime", "ctpid"],
44  "TriggerThreshold" : ["bitnum", "mapping", "version", "active", "id"],
45  "TriggerThresholdValue" : [ "thresholdval", "etamin", "etamax", "phimin", "phimax",
46  "em_isolation", "window", "priority", "had_veto", "had_isolation"],
47  "Signal" : [ "range_end", "range_begin" ],
48  "Random" : ["cut0", "cut1", "cut2", "cut3"],
49  "PrescaleSet" : ["menuPartition"],
50  "Prescale" : ["ctpid"],
51  "TriggerCondition" : ["multi"],
52  "METSignificance" : ["xeMax", "teSqrtMax", "xsSigmaOffset", "teSqrtMin", "xsSigmaScale", "xeMin"],
53  "MinimumTOBPt" : [ "priority", "etamin", "ptmin", "etamax", "window" ],
54  "Parametrization" : [ "slope", "isobit", "priority", "etamin", "offset", "mincut", "etamax", "upperlimit"],
55  "CaloInfo" : [ "global_em_scale", "global_jet_scale"],
56  "BunchGroupSet" : [ "menuPartition" ],
57  "BunchGroup" : [ "internalNumber" ],
58  "MuctpiInfo" : [ "high_pt", "max_cand", "low_pt" ],
59  "PrescaledClock" : [ "clock1", "clock2" ],
60  "LVL1Config" : [ "l1Version", "ctpVersion" ]
61  }
62  self.rules['asArray'] = [ 'AND', 'OR' ]
63  self.rules['asMultiArray'] = [ ('PrescaleSet', 'Prescale'),
64  ('TriggerThreshold', 'TriggerThresholdValue'),
65  ('TriggerCounterList', 'TriggerCounter'),
66  ('Isolation', 'Parametrization'),
67  ('TriggerMenu', 'TriggerItem'),
68  ('CaloInfo', 'MinimumTOBPt'),
69  ('CaloInfo', 'Isolation'),
70  ('BunchGroupSet', 'BunchGroup'),
71  ('OR', 'TriggerCondition'),
72  ('TriggerThresholdList', 'TriggerThreshold'),
73  ('AND', 'TriggerCondition'),
74  ('AND', 'OR'),
75  ('AND', 'InternalTrigger')
76  ]
77  self.rules['ignoreElement'] = []
78  self.rules['promote'] = {"TriggerThresholdList" : "TriggerThreshold"}
79  self.rules['rename'] = { "TriggerItem" : "items", "TriggerMenu" : "menu", "l1Version" : "version", "ctpid" : "ctpId",
80  "complex_deadtime" : "complexDeadtime", "trigger_type" : "triggerType", "TriggerThreshold" : "thresholds",
81  "triggerthreshold" : "triggerThreshold", "TriggerCondition" : "triggerCondition", "InternalTrigger" : "internalTrigger",
82  "Cable" : "cable", "Signal" : "signal", "range_begin" : "rangeBegin", "range_end" : "rangeEnd",
83  "TriggerThresholdValue" : "thresholdValues" }
84 
85  elif docname == "HLT_MENU":
86 
87  self.rules['asInt'] = {
88  "CHAIN": ["EBstep", "chain_counter"],
89  "SIGNATURE": ["signature_counter"]
90  }
91  self.rules['asMultiArray'] = [ ('SEQUENCE_LIST', 'SEQUENCE'),
92  ('GROUP_LIST', 'GROUP'),
93  ('SIGNATURE_LIST', 'SIGNATURE'),
94  ('CHAIN_LIST', 'CHAIN'),
95  ('STREAMTAG_LIST', 'STREAMTAG'),
96  ('SIGNATURE', 'TRIGGERELEMENT')
97  ]
98  self.rules['ignoreElement'] = [ "SEQUENCE_LIST", "SIGNATURE_LIST", "TRIGGERTYPE_LIST" ]
99  self.rules['ignoreAttrib'] = [ ("CHAIN", "level"),
100  ("CHAIN", "EBstep"),
101  ("CHAIN", "pass_through"),
102  ("CHAIN", "prescale"),
103  ("CHAIN", "rerun_prescale"),
104  ("HLT_MENU", "prescale_set_name")]
105  self.rules['promote'] = {"GROUP_LIST" : "GROUP", "STREAMTAG_LIST" : "STREAMTAG", "CHAIN_LIST" : "CHAIN"}
106  self.rules['rename'] = { "GROUP" : "groups", "STREAMTAG" : "streams", "CHAIN" : "chains",
107  "menu_name" : "name",
108  "chain_name" : "name", "chain_counter" : "counter", "lower_chain_name" : "l1item",
109  "stream" : "name"}
110 
111  elif docname == "TOPO_MENU":
112 
113  self.rules['asInt'] = {
114  "Parameter": ["pos", "value"],
115  "Bit": ["selection"],
116  "Input": ["position"],
117  "Output": ["bits", "fpga", "clock", "algoId", "module", "firstbit"],
118  "SortAlgo": ["algoId"],
119  "DecisionAlgo": ["algoId"],
120  }
121  self.rules['asMultiArray'] = [ ('OutputList', 'Output'),
122  ('Fixed', 'Generic'),
123  ('Fixed', 'Input'),
124  ('TOPO_MENU', 'SortAlgo'),
125  ('TOPO_MENU', 'DecisionAlgo'),
126  ('Variable', 'Parameter'),
127  ('Output', 'Bit'),
128  ('SIGNATURE', 'TRIGGERELEMENT')
129  ]
130  self.rules['promote'] = { "OutputList" : "Output" }
131  self.rules['rename'] = { "menu_name" : "name", "menu_version" : "version",
132  "algname" : "algName", "algoId" : "algId", "firstbit" : "firstBit", "module" : "board", "triggerline" : "triggerLines",
133  "SortAlgo" : "sortingAlgorithms", "DecisionAlgo" : "decisionAlgorithms",
134  "Fixed" : "fixedParameters", "Input" : "inputs", "Output" : "outputs", "Generic" : "generics",
135  "Variable" : "variableParameters", "Parameter" : "parameters"}
136  else:
137  print("Unknown document with root element %s" % docname)
138  return 1
139  return 0
140 
141 

◆ parseXMLFile()

def python.MenuXML2JSONConverter.XML2JsonConverter.parseXMLFile (   self,
  filename 
)

Definition at line 23 of file MenuXML2JSONConverter.py.

23  def parseXMLFile(self,filename):
24  self.xmlfilename = filename
25  import xml.etree.cElementTree as ET
26  self.doc = ET.parse(filename)
27  self.root = self.doc.getroot()
28  print("Parsed file %s" % filename)
29  return self.doc
30 

◆ renameKey()

def python.MenuXML2JSONConverter.XML2JsonConverter.renameKey (   self,
  key 
)

Definition at line 142 of file MenuXML2JSONConverter.py.

142  def renameKey(self,key):
143  return self.rules['rename'][key] if key in self.rules['rename'] else key
144 
145 

◆ writeJsonFile()

def python.MenuXML2JSONConverter.XML2JsonConverter.writeJsonFile (   self,
  filename = None 
)

Definition at line 187 of file MenuXML2JSONConverter.py.

187  def writeJsonFile(self, filename = None):
188  import json
189  if not filename:
190  # use xml filename base
191  filename = self.xmlfilename.rstrip(".xml")+".json"
192  elif os.path.isdir(filename):
193  filename = filename.rstrip("/") + "/" + self.xmlfilename.split("/")[-1].rstrip(".xml")+".json"
194  self.loadRules(self.root.tag)
195  d = self.createJsonStruct(self.root)
196  FH = open(filename, "w")
197  json.dump(d, FH, indent=self.tabsize, separators=(',', ': '))
198  FH.close()
199  print("Wrote file %s" % filename)
200 
201 
202 

Member Data Documentation

◆ __doc

python.MenuXML2JSONConverter.XML2JsonConverter.__doc
private

Definition at line 10 of file MenuXML2JSONConverter.py.

◆ __root

python.MenuXML2JSONConverter.XML2JsonConverter.__root
private

Definition at line 11 of file MenuXML2JSONConverter.py.

◆ __rules

python.MenuXML2JSONConverter.XML2JsonConverter.__rules
private

Definition at line 12 of file MenuXML2JSONConverter.py.

◆ doc

python.MenuXML2JSONConverter.XML2JsonConverter.doc

Definition at line 26 of file MenuXML2JSONConverter.py.

◆ root

python.MenuXML2JSONConverter.XML2JsonConverter.root

Definition at line 27 of file MenuXML2JSONConverter.py.

◆ rules

python.MenuXML2JSONConverter.XML2JsonConverter.rules

Definition at line 32 of file MenuXML2JSONConverter.py.

◆ tabsize

python.MenuXML2JSONConverter.XML2JsonConverter.tabsize

Definition at line 8 of file MenuXML2JSONConverter.py.

◆ xmlfilename

python.MenuXML2JSONConverter.XML2JsonConverter.xmlfilename

Definition at line 13 of file MenuXML2JSONConverter.py.


The documentation for this class was generated from the following file:
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
Trk::open
@ open
Definition: BinningType.h:40
Trk::split
@ split
Definition: LayerMaterialProperties.h:38