6 from xml.dom
import minidom
9 from AthenaCommon.Logging
import logging
10 log = logging.getLogger(
'MergeEBWeigtsFiles.py')
19 log.debug(
"Processing file {0}".
format(
list(input_files.keys())[0]))
20 weights =
list(input_files.values())[0].getElementsByTagName(
"weight")
21 for weight
in weights:
22 weightsList.append({
"value":weight.getAttribute(
"value"),
"unbiased":weight.getAttribute(
"unbiased"),
"id":
str(new_id)})
23 check_weight.append({
"value": weight.getAttribute(
"value"),
"unbiased":weight.getAttribute(
"unbiased")})
25 events =
list(input_files.values())[0].getElementsByTagName(
"e")
27 eventsList.append({
"n":event.getAttribute(
"n"),
"w":event.getAttribute(
"w")})
29 for fin_name, fin
in list(input_files.items())[1:]:
30 log.debug(
"Processing file {0}".
format(fin_name))
32 weights = fin.getElementsByTagName(
"weight")
33 for weight
in weights:
34 element = {
"value": weight.getAttribute(
"value"),
"unbiased":weight.getAttribute(
"unbiased")}
37 if element
not in check_weight:
38 check_weight.append(element.copy())
39 element[
"id"] =
str(new_id)
40 weightsList.append(element)
41 id_map[weight.getAttribute(
"id")]=element[
"id"]
44 id_map[weight.getAttribute(
"id")]=weightsList[check_weight.index(element)][
"id"]
45 events = fin.getElementsByTagName(
"e")
48 eventsList.append({
"n":event.getAttribute(
"n"),
"w":id_map[event.getAttribute(
"w")]})
49 return weightsList, eventsList
53 weights_element = xmlDoc.createElement(
"weights")
54 xmlRoot.appendChild(weights_element)
55 for weight
in weights_list:
56 w=doc.createElement(
"weight")
57 w.setAttribute(
"id", weight[
"id"])
58 w.setAttribute(
"value", weight[
"value"])
59 w.setAttribute(
"unbiased", weight[
"unbiased"])
60 weights_element.appendChild(w)
64 events_element = xmlDoc.createElement(
"events")
65 xmlRoot.appendChild(events_element)
66 for event
in events_list:
67 e=doc.createElement(
"e")
68 e.setAttribute(
"n", event[
"n"])
69 e.setAttribute(
"w", event[
"w"])
70 events_element.appendChild(e)
74 for fin
in input_files[1:]:
75 rn = re.compile(
r'\d+')
77 if rn[-1] != runNumber[-1]:
78 sys.exit(
"ERROR: run number should be the same for all input files. Exiting.")
80 if __name__==
'__main__':
82 from argparse
import ArgumentParser
83 parser = ArgumentParser()
84 parser.add_argument(
'--inputfiles', nargs=
'*', help=
'Insert input xml files')
85 parser.add_argument(
'--outputfile', default=
"EnhancedBiasWeights_[RUN_NUMBER]_merged.xml", help=
'Optional: insert output file name')
86 parser.add_argument(
'--loglevel', type=int, default=3, help=
'Verbosity level: 1 - VERBOSE, 2 - DEBUG, 3 - INFO')
87 args = parser.parse_args()
89 log.setLevel(args.loglevel)
91 input_files = args.inputfiles
93 if len(input_files) < 2:
94 sys.exit(
"ERROR: insert at least 2 input files. Exiting.")
96 sys.exit(
"ERROR: insert at least 2 input files. Exiting.")
99 runNumber = re.compile(
r'\d+')
100 runNumber = runNumber.findall(input_files[0])
104 for fin
in input_files:
105 parsed_files[fin] = minidom.parse(fin)
111 doc = minidom.parseString(
"<run/>")
112 root = doc.documentElement
119 output_filename = args.outputfile
120 if '[RUN_NUMBER]' in output_filename:
121 output_filename = output_filename.replace(
'[RUN_NUMBER]', runNumber[-1])
122 log.debug(
"Weights written to file {0}".
format(output_filename))
124 xml_str = root.toprettyxml(indent =
" ")
125 with open(output_filename,
"w")
as f: