2 from __future__
import print_function
17 if not inputfilename.endswith(
".xml"):
18 print(
"judging by the suffix, %s is not an xml file" % inputfilename)
25 import xml.etree.cElementTree
as ET
26 self.
doc = ET.parse(filename)
28 print(
"Parsed file %s" % filename)
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" ]
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'),
75 (
'AND',
'InternalTrigger')
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" }
85 elif docname ==
"HLT_MENU":
87 self.
rules[
'asInt'] = {
88 "CHAIN": [
"EBstep",
"chain_counter"],
89 "SIGNATURE": [
"signature_counter"]
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')
98 self.
rules[
'ignoreElement'] = [
"SEQUENCE_LIST",
"SIGNATURE_LIST",
"TRIGGERTYPE_LIST" ]
99 self.
rules[
'ignoreAttrib'] = [ (
"CHAIN",
"level"),
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",
111 elif docname ==
"TOPO_MENU":
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"],
121 self.
rules[
'asMultiArray'] = [ (
'OutputList',
'Output'),
122 (
'Fixed',
'Generic'),
124 (
'TOPO_MENU',
'SortAlgo'),
125 (
'TOPO_MENU',
'DecisionAlgo'),
126 (
'Variable',
'Parameter'),
128 (
'SIGNATURE',
'TRIGGERELEMENT')
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"}
137 print(
"Unknown document with root element %s" % docname)
143 return self.
rules[
'rename'][key]
if key
in self.
rules[
'rename']
else key
147 if node.tag
in self.
rules[
'asArray']:
148 return [ {element.tag : self.
createJsonStruct(element)}
for element
in node ]
151 from collections
import OrderedDict
155 for attr
in sorted(node.attrib.keys()):
156 if (node.tag, attr)
in self.
rules[
'ignoreAttrib']:
158 if node.tag
in self.
rules[
'asInt']
and attr
in self.
rules[
'asInt'][node.tag]:
161 d[self.
renameKey(attr)] = node.attrib[attr]
164 if element.tag
in self.
rules[
'ignoreElement']:
166 if (node.tag, element.tag)
in self.
rules[
'asMultiArray']:
169 if key
not in d: d[key] =
list()
174 raise RuntimeError(
"element %s already attached to node %s and not declared as MultiArray" % (element.tag, node.tag))
176 if element.tag
in self.
rules[
'promote']:
179 d[promoted] = obj[promoted]
192 elif os.path.isdir(filename):
193 filename = filename.rstrip(
"/") +
"/" + self.
xmlfilename.
split(
"/")[-1].rstrip(
".xml")+
".json"
196 FH =
open(filename,
"w")
197 json.dump(d, FH, indent=self.
tabsize, separators=(
',',
': '))
199 print(
"Wrote file %s" % filename)
204 print(
"Checking json conformity", end=
' ')
206 FH =
open(filename,
"rb")
210 except ValueError
as e:
219 print(
"please specify xml file for conversion")
223 converter.convertFile( sys.argv[1] )
227 if __name__==
"__main__":