1 |
< |
import re |
1 |
> |
#!/usr/bin/env python |
2 |
> |
|
3 |
|
import sys |
4 |
+ |
from optparse import OptionParser |
5 |
+ |
from xml.dom.minidom import parseString |
6 |
|
|
7 |
< |
lines = [] |
8 |
< |
for i in range(1, len(sys.argv)): |
6 |
< |
lines += file(sys.argv[i], "r").readlines() |
7 |
> |
parser = OptionParser() |
8 |
> |
parser.add_option("-f", "--file", dest="filename", help="list of jfr files", metavar="FILE") |
9 |
|
|
10 |
< |
# TODO could a file contain more than one run? |
9 |
< |
runPat = re.compile(r'\s*\{([0-9]+):[ ]\[([0-9, ]+)\]\}') |
10 |
> |
(options, args) = parser.parse_args() |
11 |
|
|
12 |
< |
inRunBlock = False |
12 |
> |
filenames = [] |
13 |
|
|
14 |
< |
lumilist = dict() |
14 |
> |
if options.filename: |
15 |
> |
filelist = file(options.filename, "r") |
16 |
> |
for f in filelist: |
17 |
> |
filenames.append(f.strip()) |
18 |
|
|
19 |
< |
for line in lines: |
20 |
< |
if '<Runs>' in line: |
21 |
< |
inRunBlock = True |
18 |
< |
continue |
19 |
> |
filelist.close() |
20 |
> |
else: |
21 |
> |
filenames = args |
22 |
|
|
23 |
< |
if '</Runs>' in line: |
24 |
< |
inRunBlock = False |
25 |
< |
continue |
23 |
> |
# hack to make multiple file processing possible |
24 |
> |
lines = '<Files>' |
25 |
> |
|
26 |
> |
for f in filenames: |
27 |
> |
try: |
28 |
> |
lines += file(f, "r").read() |
29 |
> |
except IOError: |
30 |
> |
sys.stderr.write("Cannot open " + f + "\n") |
31 |
> |
|
32 |
> |
lines += '</Files>' |
33 |
|
|
34 |
< |
if inRunBlock: |
35 |
< |
matches = runPat.match(line) |
36 |
< |
if not matches: |
37 |
< |
continue |
38 |
< |
|
39 |
< |
run = int(matches.group(1)) |
40 |
< |
if run not in lumilist: |
41 |
< |
lumilist[run] = list() |
42 |
< |
|
43 |
< |
lumis = re.findall('[0-9]+', matches.group(2)) |
44 |
< |
for lumi in lumis: |
45 |
< |
lumilist[run].append(int(lumi)) |
34 |
> |
dom = parseString(lines) |
35 |
> |
|
36 |
> |
runBlocks = dom.getElementsByTagName('Run') |
37 |
> |
|
38 |
> |
lumilist = dict() |
39 |
> |
|
40 |
> |
for runTag in runBlocks: |
41 |
> |
run = 0 |
42 |
> |
for i in range(runTag.attributes.length): |
43 |
> |
attr = runTag.attributes.item(i) |
44 |
> |
if attr.name == "ID": |
45 |
> |
run = attr.value |
46 |
> |
|
47 |
> |
if run == 0: |
48 |
> |
raise RuntimeError("No runnumber") |
49 |
> |
|
50 |
> |
if run not in lumilist: |
51 |
> |
lumilist[run] = list() |
52 |
> |
|
53 |
> |
children = runTag.childNodes |
54 |
> |
|
55 |
> |
for child in children: |
56 |
> |
if child.localName == "LumiSection": |
57 |
> |
for i in range(child.attributes.length): |
58 |
> |
attr = child.attributes.item(i) |
59 |
> |
if attr.name == "ID": |
60 |
> |
lumilist[run].append(int(attr.value)) |
61 |
|
|
62 |
|
jsonTxt = "{" |
63 |
|
|
64 |
< |
for run in lumilist: |
64 |
> |
runs = lumilist.keys() |
65 |
> |
runs.sort() |
66 |
> |
|
67 |
> |
for run in runs: |
68 |
|
lumis = lumilist[run] |
69 |
|
if len(lumis) == 0: |
70 |
|
continue |